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
OTVConnectMediaDrmCallback
must be instantiated to be passed to thecreateInstance()
method ofOTVConnectManager
.JAVAprivate 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
OTVConnectMediaDrmCallback
which includes the stream token.JAVAmConnectDrmCallback.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).JAVAmConnectDrmCallback.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
OTVConnectProvisionListener
if provisioning succeeds.JAVAprivate static final String STREAM_URI = "https://cdnurl.com/mystream.m3u8"; private void startPlayback() { runOnUiThread(() -> { mOTVVideoView.setVideoPath(STREAM_URI); mOTVVideoView.start(); }); }
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.JAVAprivate 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.
JAVAprivate 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
OTVConnectManager
instance is created by passing the OpVault and theOTVMediaDrmCallback
object to thecreateInstance()
method. AnUnsupportedSchemeException
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 ofOTVLog.LOG_LEVEL_DEBUG
or higher (for exampleOTVLog.setLogLevel(OTVLog.LOG_LEVEL_DEBUG);
).Once the
OTVConnectManager
has been created, the device provisioning status can be checked by callingisProvisioned()
. If the device is already provisioned, playback can be started; if the device is not provisioned, this can be done by callingprovision()
and passing theOTVConnectProvisionListener
to handle the result.JAVAtry { 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.