Skip to main content
Skip table of contents

HLS stream playback example code

CONNECT-protected HLS playback is implemented in the connect-prm example code as follows.

  1. In Android Studio, add the OpVault to the application build project as a project resource. 

    The usual SDK initialisation precedes any other SDK access. In the main activity:

    JAVA
    OTVSDK.load(this);
  2. An OTVConnectMediaDrmCallback must be instantiated to be passed to the createInstance() method of OTVConnectManager.

    JAVA
    private static final String CONNECT_LICENSE_SERVER = "https://licenseserverurl.com/TENANT_ID/prmls/contentlicenseservice/v1/licenses/";
    
    private OTVConnectMediaDrmCallback mConnectDrmCallback = new OTVConnectMediaDrmCallback(CONNECT_LICENSE_SERVER);
  3. Set the HTTP request properties of the OTVConnectMediaDrmCallback which includes the stream token.

    JAVA
    mConnectDrmCallback.setKeyRequestProperty("Accept", "application/json");
    mConnectDrmCallback.setKeyRequestProperty("Content-Type", "application/json");
    mConnectDrmCallback.setKeyRequestProperty("nv-authorizations", STREAM_TOKEN); // Stream specific
    mConnectDrmCallback.setKeyRequestProperty("nv-tenant-id", TENANT_ID); // Server specific

    Optionally, depending on the license server set-up, set the request options of the OTVConnectMediaDrmCallback which may include application data (clear and protected).

    JAVA
       mConnectDrmCallback.setKeyRequestOption("protectedClientData", "thisIsProtectedClientData");
       mConnectDrmCallback.setKeyRequestOption("clientData", "thisIsClientData");
  4. Provide a method for starting playback on the UI thread. In this example, it will be called immediately if the device is already provisioned, or if provisioning is required, it will be called by the OTVConnectProvisionListener if provisioning succeeds.

    JAVA
       private static final String STREAM_URI = "https://cdnurl.com/mystream.m3u8";
    
       private void startPlayback() {
        runOnUiThread(() -> {
          mOTVVideoView.setVideoPath(STREAM_URI);
          mOTVVideoView.start();
        });
       }
  5. Create an implementation of OTVConnectProvisionListener to handle the result of the provisioning operation. This displays the result in a toast message and will also start playback if successful.

    JAVA
    private OTVConnectProvisionListener mProvisionListener = new OTVConnectProvisionListener() {
         @Override
         public void onProvisionCompleted() {
           Toast.makeText(getBaseContext(), "Provision successful", Toast.LENGTH_LONG).show();
           startPlayback();
         }
    
         @Override
         public void onProvisionError(Exception error) {
           Toast.makeText(getBaseContext(), "Provision failed", Toast.LENGTH_LONG).show();
           OTVLog.w(TAG, error.getMessage());
         }
       };
  6. The OpVault must be read from its file, so the following method is provided.

    JAVA
       private byte[] loadOpVault() {
         // Loading connect_opvault in src/main/res/raw
         try {
           byte[] buffer = new byte[1024 * 4];
           ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
           int bytesRead;
           InputStream opvaultStream = getResources().openRawResource(R.raw.connect_opvault);
           while ((bytesRead = opvaultStream.read(buffer)) != -1) {
             outputStream.write(buffer, 0, bytesRead);
           }
           return outputStream.toByteArray();
         } catch (IOException e) {
           OTVLog.e(TAG, e.getMessage());
           finish();
         }
        return new byte[0];
       }
  7. The OTVConnectManager instance is created by passing the OpVault and the OTVMediaDrmCallback object to the createInstance() method. An UnsupportedSchemeException will be thrown if the device does not support CONNECT.

    When creating the OTVConnectManager instance, the log levels of the NAGRA CONNECT libraries are set based on the SDK log levels. If you require more NAGRA CONNECT logs, make sure log levels are set with a value of OTVLog.LOG_LEVEL_DEBUG or higher (for example OTVLog.setLogLevel(OTVLog.LOG_LEVEL_DEBUG);).

    Once the OTVConnectManager has been created, the device provisioning status can be checked by calling isProvisioned(). If the device is already provisioned, playback can be started; if the device is not provisioned, this can be done by calling provision(), passing the OTVConnectProvisionListener to handle the result.

    JAVA
       try {
         byte[] opVault = loadOpVault();
         OTVConnectManager.createInstance(opVault, mConnectDrmCallback);
         mConnectManager = OTVConnectManager.getInstance();
         //Check and Provision device
         if (!mConnectManager.isProvisioned()) {
           mConnectManager.provision(mProvisionListener);
         } else {
           Toast.makeText(getBaseContext(), "The device has been provisioned.", Toast.LENGTH_LONG).show();
           startPlayback();
         }
       } catch (UnsupportedSchemeException e) {
         OTVLog.w(TAG, e.getMessage());
         Toast.makeText(getBaseContext(), "The device doesn't support Connect PRM.", Toast.LENGTH_LONG).show();
       }

    If the device supports CONNECT, a toast message will provide information on the device's provisioning status, and the stream will start playing.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.