Commit 7c1b2beb by olly Committed by Oliver Woodman

Support dyanmically setting key request headers

Issue: #1924

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=146120465
parent af98ca66
...@@ -70,8 +70,6 @@ import com.google.android.exoplayer2.util.Util; ...@@ -70,8 +70,6 @@ import com.google.android.exoplayer2.util.Util;
import java.net.CookieHandler; import java.net.CookieHandler;
import java.net.CookieManager; import java.net.CookieManager;
import java.net.CookiePolicy; import java.net.CookiePolicy;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -239,19 +237,9 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay ...@@ -239,19 +237,9 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay
if (drmSchemeUuid != null) { if (drmSchemeUuid != null) {
String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL); String drmLicenseUrl = intent.getStringExtra(DRM_LICENSE_URL);
String[] keyRequestPropertiesArray = intent.getStringArrayExtra(DRM_KEY_REQUEST_PROPERTIES); 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 { try {
drmSessionManager = buildDrmSessionManager(drmSchemeUuid, drmLicenseUrl, drmSessionManager = buildDrmSessionManager(drmSchemeUuid, drmLicenseUrl,
keyRequestProperties); keyRequestPropertiesArray);
} catch (UnsupportedDrmException e) { } catch (UnsupportedDrmException e) {
int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported
: (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME : (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME
...@@ -349,12 +337,18 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay ...@@ -349,12 +337,18 @@ public class PlayerActivity extends Activity implements OnClickListener, ExoPlay
} }
private DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(UUID uuid, 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) { if (Util.SDK_INT < 18) {
return null; return null;
} }
HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl, 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, return new DefaultDrmSessionManager<>(uuid,
FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, eventLogger); FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, eventLogger);
} }
......
...@@ -24,6 +24,8 @@ import com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest; ...@@ -24,6 +24,8 @@ import com.google.android.exoplayer2.drm.ExoMediaDrm.ProvisionRequest;
import com.google.android.exoplayer2.upstream.DataSourceInputStream; import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.HttpDataSource; 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 com.google.android.exoplayer2.util.Util;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
...@@ -57,21 +59,62 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback { ...@@ -57,21 +59,62 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback {
} }
/** /**
* @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 defaultUrl The default license URL.
* @param dataSourceFactory A factory from which to obtain {@link HttpDataSource} instances. * @param dataSourceFactory A factory from which to obtain {@link HttpDataSource} instances.
* @param keyRequestProperties Request properties to set when making key requests, or null. * @param keyRequestProperties Request properties to set when making key requests, or null.
*/ */
@Deprecated
public HttpMediaDrmCallback(String defaultUrl, HttpDataSource.Factory dataSourceFactory, public HttpMediaDrmCallback(String defaultUrl, HttpDataSource.Factory dataSourceFactory,
Map<String, String> keyRequestProperties) { Map<String, String> keyRequestProperties) {
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.defaultUrl = defaultUrl; 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 @Override
public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException { public byte[] executeProvisionRequest(UUID uuid, ProvisionRequest request) throws IOException {
String url = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData()); 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 @Override
...@@ -85,14 +128,14 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback { ...@@ -85,14 +128,14 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback {
if (C.PLAYREADY_UUID.equals(uuid)) { if (C.PLAYREADY_UUID.equals(uuid)) {
requestProperties.putAll(PLAYREADY_KEY_REQUEST_PROPERTIES); requestProperties.putAll(PLAYREADY_KEY_REQUEST_PROPERTIES);
} }
if (keyRequestProperties != null) { synchronized (keyRequestProperties) {
requestProperties.putAll(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) private static byte[] executePost(HttpDataSource.Factory dataSourceFactory, String url,
throws IOException { byte[] data, Map<String, String> requestProperties) throws IOException {
HttpDataSource dataSource = dataSourceFactory.createDataSource(); HttpDataSource dataSource = dataSourceFactory.createDataSource();
if (requestProperties != null) { if (requestProperties != null) {
for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) { for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
......
...@@ -93,7 +93,7 @@ public final class OfflineLicenseHelper<T extends ExoMediaCrypto> { ...@@ -93,7 +93,7 @@ public final class OfflineLicenseHelper<T extends ExoMediaCrypto> {
public static OfflineLicenseHelper<FrameworkMediaCrypto> newWidevineInstance( public static OfflineLicenseHelper<FrameworkMediaCrypto> newWidevineInstance(
String licenseUrl, Factory httpDataSourceFactory) throws UnsupportedDrmException { String licenseUrl, Factory httpDataSourceFactory) throws UnsupportedDrmException {
return newWidevineInstance( return newWidevineInstance(
new HttpMediaDrmCallback(licenseUrl, httpDataSourceFactory, null), null); new HttpMediaDrmCallback(licenseUrl, httpDataSourceFactory), null);
} }
/** /**
......
...@@ -41,8 +41,8 @@ public interface HttpDataSource extends DataSource { ...@@ -41,8 +41,8 @@ public interface HttpDataSource extends DataSource {
HttpDataSource createDataSource(); HttpDataSource createDataSource();
/** /**
* Sets a default request header field for {@link HttpDataSource} instances subsequently * Sets a default request header for {@link HttpDataSource} instances subsequently created by
* created by the factory. Previously created instances are not affected. * the factory. Previously created instances are not affected.
* *
* @param name The name of the header field. * @param name The name of the header field.
* @param value The value of the field. * @param value The value of the field.
...@@ -50,16 +50,16 @@ public interface HttpDataSource extends DataSource { ...@@ -50,16 +50,16 @@ public interface HttpDataSource extends DataSource {
void setDefaultRequestProperty(String name, String value); void setDefaultRequestProperty(String name, String value);
/** /**
* Clears a default request header field for {@link HttpDataSource} instances subsequently * Clears a default request header for {@link HttpDataSource} instances subsequently created by
* created by the factory. Previously created instances are not affected. * the factory. Previously created instances are not affected.
* *
* @param name The name of the header field. * @param name The name of the header field.
*/ */
void clearDefaultRequestProperty(String name); void clearDefaultRequestProperty(String name);
/** /**
* Clears all default request header fields for all {@link HttpDataSource} instances * Clears all default request header for all {@link HttpDataSource} instances subsequently
* subsequently created by the factory. Previously created instances are not affected. * created by the factory. Previously created instances are not affected.
*/ */
void clearAllDefaultRequestProperties(); void clearAllDefaultRequestProperties();
...@@ -232,7 +232,7 @@ public interface HttpDataSource extends DataSource { ...@@ -232,7 +232,7 @@ public interface HttpDataSource extends DataSource {
int read(byte[] buffer, int offset, int readLength) throws HttpDataSourceException; 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. * established by the source.
* *
* @param name The name of the header field. * @param name The name of the header field.
...@@ -241,7 +241,7 @@ public interface HttpDataSource extends DataSource { ...@@ -241,7 +241,7 @@ public interface HttpDataSource extends DataSource {
void setRequestProperty(String name, String value); 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. * established by the source.
* *
* @param name The name of the header field. * @param name The name of the header field.
...@@ -249,7 +249,7 @@ public interface HttpDataSource extends DataSource { ...@@ -249,7 +249,7 @@ public interface HttpDataSource extends DataSource {
void clearRequestProperty(String name); 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(); void clearAllRequestProperties();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment