package com.kaltura.playersdk.players;
import android.annotation.TargetApi;
import android.content.Context;
import android.media.MediaDrm;
import android.os.Build;
import android.util.Base64;
import android.util.Log;
import com.google.android.exoplayer.drm.DrmSessionManager;
import com.google.android.libraries.mediaframework.exoplayerextensions.ExoplayerUtil;
import com.google.android.libraries.mediaframework.exoplayerextensions.ExtendedMediaDrmCallback;
import com.kaltura.playersdk.BuildConfig;
import com.kaltura.playersdk.drm.OfflineDrmManager;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Created by noamt on 01/05/2016.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
class KPlayerExoDrmCallback implements ExtendedMediaDrmCallback {
private static final String TAG = "KPlayerDrmCallback";
private static final long MAX_LICENCE_URI_WAIT = 8000;
private String mLicenseUri;
private final Object mLicenseLock = new Object();
private final Context mContext;
private boolean mOffline;
KPlayerExoDrmCallback(Context context, boolean offline) {
mContext = context;
mOffline = offline;
Log.d(TAG, "KPlayerDrmCallback created");
}
@Override
public byte[] executeProvisionRequest(UUID uuid, MediaDrm.ProvisionRequest request) throws IOException {
String url = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData());
return ExoplayerUtil.executePost(url, null, null);
}
@Override
public byte[] executeKeyRequest(UUID uuid, MediaDrm.KeyRequest request) throws IOException {
if (mOffline) {
if (BuildConfig.DEBUG) {
throw new AssertionError("executeKeyRequest is not allowed in offline mode");
} else {
return null;
}
}
Map<String, String> headers = new HashMap<>(1);
headers.put("Content-Type", "application/octet-stream");
// The license uri arrives on a different thread (typically the main thread).
// If this method is called before the uri has arrived, we have to wait for it.
// mLicenseLock is the wait lock.
synchronized (mLicenseLock) {
// No uri? wait.
if (mLicenseUri == null) {
try {
mLicenseLock.wait(MAX_LICENCE_URI_WAIT);
} catch (InterruptedException e) {
Log.d(TAG, "Interrupted", e);
}
}
// Still no uri? throw.
if (mLicenseUri == null) {
throw new IllegalStateException("licenseUri cannot be null");
}
// Execute request.
byte[] response = ExoplayerUtil.executePost(mLicenseUri, request.getData(), headers);
Log.d(TAG, "response data (b64): " + Base64.encodeToString(response, 0));
return response;
}
}
public void setLicenseUri(String licenseUri) {
synchronized (mLicenseLock) {
mLicenseUri = licenseUri;
// notify executeKeyRequest() that we have the license uri.
mLicenseLock.notify();
}
}
@Override
public DrmSessionManager getSessionManager() {
return mOffline ? OfflineDrmManager.getSessionManager(mContext) : null;
}
}