HLS stream playback example code
CONNECT-protected HLS playback is implemented in the connect-prm example code as follows.
- 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: JAVAOTVSDK.load(this);
- An - OTVConnectMediaDrmCallbackmust 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);
- Set the HTTP request properties of the - OTVConnectMediaDrmCallbackwhich 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 - OTVConnectMediaDrmCallbackwhich may include application data (clear and protected).JAVA- mConnectDrmCallback.setKeyRequestOption("protectedClientData", "thisIsProtectedClientData"); mConnectDrmCallback.setKeyRequestOption("clientData", "thisIsClientData");
- 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 - OTVConnectProvisionListenerif provisioning succeeds.JAVA- private static final String STREAM_URI = "https://cdnurl.com/mystream.m3u8"; private void startPlayback() { runOnUiThread(() -> { mOTVVideoView.setVideoPath(STREAM_URI); mOTVVideoView.start(); }); }
- Create an implementation of - OTVConnectProvisionListenerto 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()); } };
- 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]; }
- The - OTVConnectManagerinstance is created by passing the OpVault and the- OTVMediaDrmCallbackobject to the- createInstance()method. An- UnsupportedSchemeExceptionwill be thrown if the device does not support CONNECT.- When creating the - OTVConnectManagerinstance, 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_DEBUGor higher (for example- OTVLog.setLogLevel(OTVLog.LOG_LEVEL_DEBUG);).- Once the - OTVConnectManagerhas 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- OTVConnectProvisionListenerto 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. 
