Initialise the DRM module

Your application must instantiate the DRM module at startup before it can decode Widevine-encrypted streams.

Prerequisites

Procedure

The main steps are as follows:

  1. Instantiate the following objects to initialise the multi-DRM interface:
    • MediaKeySystemConfiguration
    • MediaKeySystemMediaCapability objects for audio and video
  2. Specify the DRM scheme (Widevine) by calling the MediaKeySystemConfiguration object's setInitDataTypes() method.
  3. Register the MediaKeySystemMediaCapability objects with the MediaKeySystemConfiguration object by calling its setVideoCapabilities() and setAudioCapabilities() methods.
  4. Request access to the key system by calling the MediaKeySystemAccess object's requestMediaKeySystemAccess() method.
  5. Create a MediaKeys object by calling the MediaKeySystemAccess object's createMediaKeys() object. 
    You will use this object to acquire a license.

Code sample

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import nagra.nmp.sdk.NMPLog;
import nagra.nmp.sdk.drm.MediaKeyDeliverySession;
import nagra.nmp.sdk.drm.MediaKeyMessage;
import nagra.nmp.sdk.drm.MediaKeySystemAccess;
import nagra.nmp.sdk.drm.MediaKeySystemConfiguration;
import nagra.nmp.sdk.drm.MediaKeySystemMediaCapability;
import nagra.nmp.sdk.drm.MediaKeys;


//Example of function to prepare Media Keys

private MediaKeySystemAccess             mDrmAccess      = null;
private MediaKeys                        mMediaKeys      = null;

private boolean prepareMediaKeys() {

    // Create a MediaKeySystemConfiguration object.
    MediaKeySystemConfiguration config = new MediaKeySystemConfiguration();

    // Create MediaKeySystemCapability objects for video and audio. The constructor takes a single string as its parameter.
    // This consists of the MIME type and a codec specifier.
    // Note that the Player only supports the codecs specified here.
    MediaKeySystemMediaCapability supportedVideo = new MediaKeySystemMediaCapability("video/mp4; codecs='avc1'");
    MediaKeySystemMediaCapability supportedAudio = new MediaKeySystemMediaCapability("audio/mp4; codecs='mp4a'");

    // Tell the MediaKeySystemConfiguration object which DRM scheme you are using (i.e., Widevine).
    config.setInitDataTypes(Collections.singletonList("com.widevine.cenc::pssh"));

    // Pass the MediaKeySystemMediaCapability objects that you created to the MediaKeySystemConfiguration object. 
    config.setVideoCapabilities(Collections.singletonList(supportedVideo));
    config.setAudioCapabilities(Collections.singletonList(supportedAudio));

    // Request access to the key system.This returns a MediaKeySystemAccess object.
    mDrmAccess = MediaKeySystemAccess.requestMediaKeySystemAccess(MediaKeySystemAccess.WIDEVINE_KEY_SYSTEM, Collections.singletonList(config));

    // Call createMediaKeys(). This returns a MediaKeys object that you will use this later when acquiring a licence.
    if(mDrmAccess != null) {
      mMediaKeys = mDrmAccess.createMediaKeys(); 
      return true;
    }
    return false;
}

Note that only one MediaKeySystemAccess object can be created at a time. If you need to switch DRM schemes, you must delete this object and instantiate a new one.