Package com.castlabs.android.drm

# Class CastlabsMediaDrm

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

public class CastlabsMediaDrm extends Object
A wrapper around `FrameworkMediaDrm` that provides asynchronous session management capabilities. This class handles DRM session operations with proper resource management and thread safety.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `CastlabsMediaDrm.CastlabsMediaDrmException`
  Exception thrown when operations on the MediaDrm instance fail.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `closeSession(byte[] sessionId)`
  Asynchronously closes the specified DRM session.
  `void`
  `closeSessionSync(byte[] sessionId)`
  Synchronously closes the specified DRM session.
  `com.google.android.exoplayer2.drm.FrameworkMediaCrypto`
  `createMediaCrypto(byte[] initData)`
  Creates a MediaCrypto instance for the specified initialization data.
  `com.google.android.exoplayer2.drm.ExoMediaDrm.KeyRequest`
  `getKeyRequest(byte[] scope, List<com.google.android.exoplayer2.drm.DrmInitData.SchemeData> schemeDatas, int keyType, HashMap<String,String> optionalParameters)`
  Gets a key request for the specified session and key type.
  `byte[]`
  `getPropertyByteArray(String propertyName)`
  Gets a byte array property from the MediaDrm instance.
  `String`
  `getPropertyString(String propertyName)`
  Gets a string property from the MediaDrm instance.
  `com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest`
  `getProvisionRequest()`
  Gets a provision request for the device.
  `static CastlabsMediaDrm`
  `newInstance(UUID uuid)`
  Creates a new instance of CastlabsMediaDrm for the specified DRM scheme UUID.
  `byte[]`
  `openSession()`
  Opens a new DRM session.
  `byte[]`
  `provideKeyResponse(byte[] scope, byte[] response)`
  Provides a key response for the specified session.
  `void`
  `provideProvisionResponse(byte[] response)`
  Provides a provision response for the device.
  `Map<String,String>`
  `queryKeyStatus(byte[] sessionId)`
  Queries the key status for the specified session.
  `void`
  `release()`
  Releases all resources associated with this MediaDrm instance.
  `void`
  `releaseAsync()`
  Asynchronously releases all resources associated with this MediaDrm instance.
  `void`
  `restoreKeys(byte[] sessionId, byte[] keySetId)`
  Restores keys for the specified session.
  `void`
  `setOnEventListener(com.google.android.exoplayer2.drm.ExoMediaDrm.OnEventListener<? super com.google.android.exoplayer2.drm.FrameworkMediaCrypto> listener)`
  Sets the event listener for DRM events.
  `void`
  `setOnKeyStatusChangeListener(com.google.android.exoplayer2.drm.ExoMediaDrm.OnKeyStatusChangeListener<? super com.google.android.exoplayer2.drm.FrameworkMediaCrypto> listener)`
  Sets the key status change listener for DRM events.
  `void`
  `setPropertyByteArray(String propertyName, byte[] value)`
  Sets a byte array property on the MediaDrm instance.
  `void`
  `setPropertyString(String propertyName, String value)`
  Sets a string property on the MediaDrm instance.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### newInstance

    public static CastlabsMediaDrm newInstance(UUID uuid) throws com.google.android.exoplayer2.drm.UnsupportedDrmException
    Creates a new instance of CastlabsMediaDrm for the specified DRM scheme UUID.
    Parameters:
      `uuid` - The UUID of the DRM scheme to use
    Returns:
      A new CastlabsMediaDrm instance
    Throws:
      `com.google.android.exoplayer2.drm.UnsupportedDrmException` - If the DRM scheme is not supported

  - ### setOnEventListener

    public void setOnEventListener(com.google.android.exoplayer2.drm.ExoMediaDrm.OnEventListener<? super com.google.android.exoplayer2.drm.FrameworkMediaCrypto> listener)
    Sets the event listener for DRM events.
    Parameters:
      `listener` - The listener to be notified of DRM events

  - ### setOnKeyStatusChangeListener

    public void setOnKeyStatusChangeListener(com.google.android.exoplayer2.drm.ExoMediaDrm.OnKeyStatusChangeListener<? super com.google.android.exoplayer2.drm.FrameworkMediaCrypto> listener)
    Sets the key status change listener for DRM events.
    Parameters:
      `listener` - The listener to be notified of key status changes

  - ### openSession

    public byte[] openSession() throws NotProvisionedException, ResourceBusyException, MediaDrmException
    Opens a new DRM session.
    Returns:
      The session ID for the newly opened session
    Throws:
      `NotProvisionedException` - If the device is not provisioned
      `ResourceBusyException` - If the DRM system is busy
      `MediaDrmException` - If there was an error opening the session

  - ### closeSession

    public void closeSession(byte[] sessionId)
    Asynchronously closes the specified DRM session.
    Parameters:
      `sessionId` - The ID of the session to close

  - ### closeSessionSync

    public void closeSessionSync(byte[] sessionId)
    Synchronously closes the specified DRM session.
    Parameters:
      `sessionId` - The ID of the session to close

  - ### getKeyRequest

    public com.google.android.exoplayer2.drm.ExoMediaDrm.KeyRequest getKeyRequest(byte[] scope, @Nullable List<com.google.android.exoplayer2.drm.DrmInitData.SchemeData> schemeDatas, int keyType, HashMap<String,String> optionalParameters) throws NotProvisionedException
    Gets a key request for the specified session and key type.
    Parameters:
      `scope` - The session ID
      `schemeDatas` - The scheme data for the key request
      `keyType` - The type of key request
      `optionalParameters` - Optional parameters for the key request
    Returns:
      The key request
    Throws:
      `NotProvisionedException` - If the device is not provisioned

  - ### provideKeyResponse

    public byte[] provideKeyResponse(byte[] scope, byte[] response) throws NotProvisionedException, DeniedByServerException
    Provides a key response for the specified session.
    Parameters:
      `scope` - The session ID
      `response` - The key response
    Returns:
      The key response data
    Throws:
      `NotProvisionedException` - If the device is not provisioned
      `DeniedByServerException` - If the server denied the request

  - ### getProvisionRequest

    public com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest getProvisionRequest()
    Gets a provision request for the device.
    Returns:
      The provision request

  - ### provideProvisionResponse

    public void provideProvisionResponse(byte[] response) throws DeniedByServerException
    Provides a provision response for the device.
    Parameters:
      `response` - The provision response
    Throws:
      `DeniedByServerException` - If the server denied the request

  - ### queryKeyStatus

    public Map<String,String> queryKeyStatus(byte[] sessionId)
    Queries the key status for the specified session.
    Parameters:
      `sessionId` - The session ID
    Returns:
      A map containing the key status information

  - ### release

    public void release()
    Releases all resources associated with this MediaDrm instance.

    This method guards against exceptions that may occur during release, particularly on some devices where MediaDrm.release() can throw NullPointerException due to framework issues with service unbinding.

  - ### releaseAsync

    public void releaseAsync()
    Asynchronously releases all resources associated with this MediaDrm instance.

  - ### restoreKeys

    public void restoreKeys(byte[] sessionId, byte[] keySetId)
    Restores keys for the specified session.
    Parameters:
      `sessionId` - The session ID
      `keySetId` - The key set ID to restore

  - ### getPropertyString

    public String getPropertyString(String propertyName)
    Gets a string property from the MediaDrm instance.
    Parameters:
      `propertyName` - The name of the property
    Returns:
      The property value

  - ### getPropertyByteArray

    public byte[] getPropertyByteArray(String propertyName)
    Gets a byte array property from the MediaDrm instance.
    Parameters:
      `propertyName` - The name of the property
    Returns:
      The property value

  - ### setPropertyString

    public void setPropertyString(String propertyName, String value)
    Sets a string property on the MediaDrm instance.
    Parameters:
      `propertyName` - The name of the property
      `value` - The property value

  - ### setPropertyByteArray

    public void setPropertyByteArray(String propertyName, byte[] value)
    Sets a byte array property on the MediaDrm instance.
    Parameters:
      `propertyName` - The name of the property
      `value` - The property value

  - ### createMediaCrypto

    public com.google.android.exoplayer2.drm.FrameworkMediaCrypto createMediaCrypto(byte[] initData) throws MediaCryptoException
    Creates a MediaCrypto instance for the specified initialization data.
    Parameters:
      `initData` - The initialization data
    Returns:
      A new MediaCrypto instance
    Throws:
      `MediaCryptoException` - If there was an error creating the MediaCrypto instance