Class DrmUtils

java.lang.Object
com.castlabs.android.drm.DrmUtils

public class DrmUtils extends Object
Utility methods for the widevine applications.
  • Constructor Details

    • DrmUtils

      public DrmUtils()
  • Method Details

    • bytesToString

      @NonNull public static String bytesToString(@Nullable byte[] byteArray)
      Converts byte array to String representation, used for debugging
      Parameters:
      byteArray - The byte array
      Returns:
      String representation
    • isDisabled

      public static boolean isDisabled(@Nullable Drm drm, @Nullable KeyStatus keyStatus)
      Returns:
      true if the track with specified Drm and KeyStatus is not playable and should be disabled, false otherwise
    • isDisabled

      public static boolean isDisabled(@Nullable KeyStatus keyStatus)
      Returns:
      true if the track with specified KeyStatus is not playable and should be disabled, false otherwise
    • selectBestDrm

      @Nullable public static Drm selectBestDrm(@Nullable Drm drm)
      Select the best available DRM if no explicit DRM is specified.
      Parameters:
      drm - The source DRM
      Returns:
      The source DRM or the best available DRM if the source selection is Drm.BestAvailable
    • isUnsupportedSchemeException

      public static boolean isUnsupportedSchemeException(@NonNull Exception e)
    • isNotProvisionedException

      public static boolean isNotProvisionedException(@NonNull Exception e)
    • isDeniedByServerException

      public static boolean isDeniedByServerException(@NonNull Exception e)
    • selectBestDrm

      @Nullable public static Drm selectBestDrm(@NonNull Set<Drm> excludeDrms)
    • selectAudioDrm

      public static Drm selectAudioDrm(@NonNull Drm videoDrm, @Nullable Drm audioDrm)
      Select the best available DRM if no explicit DRM is specified.
      Parameters:
      videoDrm - The video DRM system
      audioDrm - The video DRM system
      Returns:
      Selects the audio drm system based on the given selection and the device
    • selectAudioDrm

      @Nullable public static Drm selectAudioDrm(@NonNull Drm videoDrm, @Nullable Drm audioDrm, @NonNull Set<Drm> excludeDrms)
    • executeModifierPost

      public static byte[] executeModifierPost(@NonNull String url, @Nullable byte[] data, @Nullable Map<String,String> requestProperties, @Nullable HashMap<String,List<String>> responseHeaders, int connectionTimeoutMs, int readTimeoutMs, int acquisitionTimeoutMs, @Nullable SSLSocketFactory sslSocketFactory, @Nullable List<ResponseModifier> responseModifiers, int responseType) throws IOException
      Throws:
      IOException
    • executePost

      @NonNull public static Pair<byte[],Integer> executePost(Uri uri, byte[] data, Map<String,String> requestProperties) throws IOException
      Throws:
      IOException
    • executePost

      @NonNull public static Pair<byte[],Integer> executePost(String url, byte[] data, Map<String,String> requestProperties) throws IOException
      Throws:
      IOException
    • executePost

      @NonNull public static Pair<byte[],Integer> executePost(String url, byte[] data, Map<String,String> requestProperties, @Nullable Map<String,List<String>> responseHeaders, int connectTimeoutMs, int readTimeoutMs, int acquisitionTimeoutMs, @Nullable SSLSocketFactory sslSocketFactory) throws IOException
      Throws:
      IOException
    • generateWidvineCencHeader

      public static byte[] generateWidvineCencHeader(@NonNull UUID cencKeyId, @NonNull byte[] contentId, @NonNull String provider, @NonNull String policy)
      Generates a Widevine Header from a CENC UUID and a merchant. The key ID will be used for both keyID and contentID in the generated header.
      Parameters:
      cencKeyId - the CENC key ID
      contentId - the content ID
      provider - the provider
      policy - the policy
      Returns:
      The generated Widevine CENC header
    • cencFromPlayreadyKID

      public static UUID cencFromPlayreadyKID(String base64KID)
      Creates a CENC UUID from a base 64 encoded playready key id
      Parameters:
      base64KID - the base 64 encoded playready key ID
      Returns:
      Teh CENC UUID
    • cencFromPlayreadyKID

      public static UUID cencFromPlayreadyKID(byte[] decode)
      Creates a CENC UUID from the decoded playready KeyID
      Parameters:
      decode - the decoded playready key id
      Returns:
      The CENC UUID
    • getKIDFromPlayreadySchemeData

      public static UUID getKIDFromPlayreadySchemeData(String data)
      Takes a Base64 encoded string representation of a Playready Header Object and extracts the KID converted to CENC or null.

      See this Document for the spec and what data are expected.
      Parameters:
      data - the base64 encoded data
      Returns:
      The KIDs or null
      Throws:
      IllegalArgumentException - in case parsing fails
    • getKIDFromPlayreadySchemeData

      @Nullable public static UUID getKIDFromPlayreadySchemeData(byte[] decode)
      Takes a Playready Header Object and extracts the KID converted to CENC or null.

      See this Document for the spec and what data are expected.
      Parameters:
      decode - the header object
      Returns:
      The KIDs or null
      Throws:
      IllegalArgumentException - in case parsing fails
    • getKIDFromPlayreadySchemeData

      @Nullable public static UUID getKIDFromPlayreadySchemeData(@NonNull com.google.android.exoplayer2.drm.DrmInitData.SchemeData schemeData)
    • generateWidvineCencHeader

      public static byte[] generateWidvineCencHeader(byte[] keyId, String widevineProviderId, byte[] widevineContentId, String widevinePolicy)
      Generates a Widevine CENC header
      Parameters:
      keyId - the keyID
      widevineProviderId - the provider
      widevineContentId - the content ID
      widevinePolicy - the policy
      Returns:
      The Widevine header
    • generatePlayreadyHeader

      @Nullable public static byte[] generatePlayreadyHeader(@NonNull byte[] initData, @NonNull String licenseServerUrl)
      Takes the raw bytes from a Widevine CENC header and generates a Playready Header Object
      Parameters:
      initData - The widevine cenc header raw data
      licenseServerUrl - The playready license server URL
      Returns:
      The Playready Header Object or null if the object could not be generated
    • generateWiseplayHeader

      @Nullable public static byte[] generateWiseplayHeader(@NonNull byte[] initData, @NonNull String licenseServerUrl)
      Takes the raw bytes from a Widevine CENC header and generates a Wiseplay init data
      Parameters:
      initData - The widevine cenc header raw data
      licenseServerUrl - The wiseplay license server URL
      Returns:
      The Wiseplay Header Object or null if the object could not be generated
    • extractPsshData

      @Nullable public static byte[] extractPsshData(@NonNull UUID drmScheme, @Nullable byte[] data)
      Extract the atom data in a given PSSH box. This method will return the original data if data is not a PSSH box.
      Parameters:
      drmScheme - The DRM scheme, i.e. SdkConsts.WIDEVINE_UUID or SdkConsts.PLAYREADY_UUID
      data - The PSSH box
      Returns:
      The data inside the given PSSH box or the original data if the data is not a PSSH box
    • getBytes

      public static byte[] getBytes(@NonNull UUID uuid)
      Converts a UUID to bytes
      Parameters:
      uuid - the UUID
      Returns:
      The UUID as bytes
    • createUuid

      public static UUID createUuid(@NonNull String base64)
      Create a UUID from a encoded base64 String
      Parameters:
      base64 - base64 encoded UUID
      Returns:
      The UUID
    • createUuid

      public static UUID createUuid(byte[] bytes)
      Create a UUID from a byte array
      Parameters:
      bytes - the source bytes
      Returns:
      The UUID
    • getDeviceSecurityLevel

      @Nullable public static SecurityLevel getDeviceSecurityLevel(@Nullable Drm drm)
      Returns the security level supported by the current device and the given DRM if the devices supports the DRM. Returns null if the DRM is not supported. Please note that this method will always return SecurityLevel.SOFTWARE if the current API level is below 18.
      Parameters:
      drm - The drm system for which the security level is requested
      Returns:
      The security level supported for the given DRM or null if the DRM is not supported
    • getSupportedDRM

      public static List<Drm> getSupportedDRM()
      Returns:
      plugins an unmodifiable list of currently supported DRM based on the device and player plugins
    • isDrmSupported

      public static boolean isDrmSupported(Drm drm)
      Helper function. Checks if drm is supported in the platform.
      Parameters:
      drm - Drm to be checked
      Returns:
      if drm is supported on the device with the current plugin configuration.
    • getKIDFromWidevineHeader

      @Nullable public static UUID getKIDFromWidevineHeader(byte[] wvHeader)
    • getKIDFromWidevineSchemeData

      @Nullable public static UUID getKIDFromWidevineSchemeData(@NonNull com.google.android.exoplayer2.drm.DrmInitData.SchemeData schemeData)
    • isWidevineHeader

      public static boolean isWidevineHeader(byte[] data)
      Parameters:
      data - The data
      Returns:
      True if the given data represent a Widevine Header object
    • isPlayreadyHeader

      public static boolean isPlayreadyHeader(byte[] data)
      Parameters:
      data - The data
      Returns:
      True if the given data represent a Playready Header object
    • createMediaDrm

      public static CastlabsMediaDrm createMediaDrm(UUID uuid, boolean forceWidevineL3) throws UnsupportedSchemeException, com.google.android.exoplayer2.drm.UnsupportedDrmException
      Create a new MediaDrm instance
      Parameters:
      uuid - The UUID of the DRM scheme
      Returns:
      The new instance
      Throws:
      UnsupportedSchemeException - in case the scheme is not supported
      com.google.android.exoplayer2.drm.UnsupportedDrmException - in case the DRM is not supported