Skip to content

Commit

Permalink
Support dyanmically setting key request headers
Browse files Browse the repository at this point in the history
Issue: #1924

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146120465
  • Loading branch information
ojw28 committed Feb 15, 2017
1 parent af98ca6 commit 7c1b2be
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
Expand Down Expand Up @@ -239,19 +237,9 @@ private void initializePlayer() {
if (drmSchemeUuid != null) {
String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL);
String[] keyRequestPropertiesArray = intent.getStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES);
Map<String, String> keyRequestProperties;
if (keyRequestPropertiesArray == null || keyRequestPropertiesArray.length < 2) {
keyRequestProperties = null;
} else {
keyRequestProperties = new HashMap<>();
for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
keyRequestProperties.put(keyRequestPropertiesArray[i],
keyRequestPropertiesArray[i + 1]);
}
}
try {
drmSessionManager = buildDrmSessionManager(drmSchemeUuid, drmLicenseUrl,
keyRequestProperties);
keyRequestPropertiesArray);
} catch (UnsupportedDrmException e) {
int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported
: (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME
Expand Down Expand Up @@ -349,12 +337,18 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
}

private DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(UUID uuid,
String licenseUrl, Map<String, String> keyRequestProperties) throws UnsupportedDrmException {
String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
if (Util.SDK_INT < 18) {
return null;
}
HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl,
buildHttpDataSourceFactory(false), keyRequestProperties);
buildHttpDataSourceFactory(false));
if (keyRequestPropertiesArray != null) {
for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i],
keyRequestPropertiesArray[i + 1]);
}
}
return new DefaultDrmSessionManager<>(uuid,
FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, eventLogger);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource.Factory;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -57,21 +59,62 @@ public HttpMediaDrmCallback(String defaultUrl, HttpDataSource.Factory dataSource
}

/**
* @deprecated Use {@link HttpMediaDrmCallback#HttpMediaDrmCallback(String, Factory)}. Request
* properties can be set by calling {@link #setKeyRequestProperty(String, String)}.
* @param defaultUrl The default license URL.
* @param dataSourceFactory A factory from which to obtain {@link HttpDataSource} instances.
* @param keyRequestProperties Request properties to set when making key requests, or null.
*/
@Deprecated
public HttpMediaDrmCallback(String defaultUrl, HttpDataSource.Factory dataSourceFactory,
Map<String, String> keyRequestProperties) {
this.dataSourceFactory = dataSourceFactory;
this.defaultUrl = defaultUrl;
this.keyRequestProperties = keyRequestProperties;
this.keyRequestProperties = new HashMap<>();
if (keyRequestProperties != null) {
this.keyRequestProperties.putAll(keyRequestProperties);
}
}

/**
* Sets a header for key requests made by the callback.
*
* @param name The name of the header field.
* @param value The value of the field.
*/
public void setKeyRequestProperty(String name, String value) {
Assertions.checkNotNull(name);
Assertions.checkNotNull(value);
synchronized (keyRequestProperties) {
keyRequestProperties.put(name, value);
}
}

/**
* Clears a header for key requests made by the callback.
*
* @param name The name of the header field.
*/
public void clearKeyRequestProperty(String name) {
Assertions.checkNotNull(name);
synchronized (keyRequestProperties) {
keyRequestProperties.remove(name);
}
}

/**
* Clears all headers for key requests made by the callback.
*/
public void clearAllKeyRequestProperties() {
synchronized (keyRequestProperties) {
keyRequestProperties.clear();
}
}

@Override
public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException {
String url = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData());
return executePost(url, new byte[0], null);
return executePost(dataSourceFactory, url, new byte[0], null);
}

@Override
Expand All @@ -85,14 +128,14 @@ public byte[] executeKeyRequest(UUID uuid, KeyRequest request) throws Exception
if (C.PLAYREADY_UUID.equals(uuid)) {
requestProperties.putAll(PLAYREADY_KEY_REQUEST_PROPERTIES);
}
if (keyRequestProperties != null) {
synchronized (keyRequestProperties) {
requestProperties.putAll(keyRequestProperties);
}
return executePost(url, request.getData(), requestProperties);
return executePost(dataSourceFactory, url, request.getData(), requestProperties);
}

private byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
throws IOException {
private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
byte[] data, Map<String, String> requestProperties) throws IOException {
HttpDataSource dataSource = dataSourceFactory.createDataSource();
if (requestProperties != null) {
for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static DashManifest downloadManifest(HttpDataSource dataSource, String ma
public static OfflineLicenseHelper<FrameworkMediaCrypto> newWidevineInstance(
String licenseUrl, Factory httpDataSourceFactory) throws UnsupportedDrmException {
return newWidevineInstance(
new HttpMediaDrmCallback(licenseUrl, httpDataSourceFactory, null), null);
new HttpMediaDrmCallback(licenseUrl, httpDataSourceFactory), null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ interface Factory extends DataSource.Factory {
HttpDataSource createDataSource();

/**
* Sets a default request header field for {@link HttpDataSource} instances subsequently
* created by the factory. Previously created instances are not affected.
* Sets a default request header for {@link HttpDataSource} instances subsequently created by
* the factory. Previously created instances are not affected.
*
* @param name The name of the header field.
* @param value The value of the field.
*/
void setDefaultRequestProperty(String name, String value);

/**
* Clears a default request header field for {@link HttpDataSource} instances subsequently
* created by the factory. Previously created instances are not affected.
* Clears a default request header for {@link HttpDataSource} instances subsequently created by
* the factory. Previously created instances are not affected.
*
* @param name The name of the header field.
*/
void clearDefaultRequestProperty(String name);

/**
* Clears all default request header fields for all {@link HttpDataSource} instances
* subsequently created by the factory. Previously created instances are not affected.
* Clears all default request header for all {@link HttpDataSource} instances subsequently
* created by the factory. Previously created instances are not affected.
*/
void clearAllDefaultRequestProperties();

Expand Down Expand Up @@ -232,7 +232,7 @@ public InvalidResponseCodeException(int responseCode, Map<String, List<String>>
int read(byte[] buffer, int offset, int readLength) throws HttpDataSourceException;

/**
* Sets the value of a request header field. The value will be used for subsequent connections
* Sets the value of a request header. The value will be used for subsequent connections
* established by the source.
*
* @param name The name of the header field.
Expand All @@ -241,15 +241,15 @@ public InvalidResponseCodeException(int responseCode, Map<String, List<String>>
void setRequestProperty(String name, String value);

/**
* Clears the value of a request header field. The change will apply to subsequent connections
* Clears the value of a request header. The change will apply to subsequent connections
* established by the source.
*
* @param name The name of the header field.
*/
void clearRequestProperty(String name);

/**
* Clears all request header fields that were set by {@link #setRequestProperty(String, String)}.
* Clears all request headers that were set by {@link #setRequestProperty(String, String)}.
*/
void clearAllRequestProperties();

Expand Down

0 comments on commit 7c1b2be

Please sign in to comment.