diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java index 678822b7d2..14c1d6d1e7 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java @@ -231,7 +231,7 @@ public final class MediaItem { } /** - * Sets the optional DRM license server URI. If this URI is set, the {@link + * Sets the optional default DRM license server URI. If this URI is set, the {@link * DrmConfiguration#uuid} needs to be specified as well. * *

If {@link #setUri} is passed a non-null {@code uri}, the DRM license server URI is used to @@ -243,7 +243,7 @@ public final class MediaItem { } /** - * Sets the optional DRM license server URI. If this URI is set, the {@link + * Sets the optional default DRM license server URI. If this URI is set, the {@link * DrmConfiguration#uuid} needs to be specified as well. * *

If {@link #setUri} is passed a non-null {@code uri}, the DRM license server URI is used to @@ -294,8 +294,8 @@ public final class MediaItem { } /** - * Sets whether to use the DRM license server URI of the media item for key requests that - * include their own DRM license server URI. + * Sets whether to force use the default DRM license server URI even if the media specifies its + * own DRM license server URI. * *

If {@link #setUri} is passed a non-null {@code uri}, the DRM force default license flag is * used to create a {@link PlaybackProperties} object. Otherwise it will be ignored. @@ -568,8 +568,8 @@ public final class MediaItem { public final UUID uuid; /** - * Optional DRM license server {@link Uri}. If {@code null} then the DRM license server must be - * specified by the media. + * Optional default DRM license server {@link Uri}. If {@code null} then the DRM license server + * must be specified by the media. */ @Nullable public final Uri licenseUri; @@ -586,8 +586,8 @@ public final class MediaItem { public final boolean playClearContentWithoutKey; /** - * Sets whether to use the DRM license server URI of the media item for key requests that - * include their own DRM license server URI. + * Whether to force use of {@link #licenseUri} even if the media specifies its own DRM license + * server URI. */ public final boolean forceDefaultLicenseUri; @@ -605,6 +605,7 @@ public final class MediaItem { boolean playClearContentWithoutKey, List drmSessionForClearTypes, @Nullable byte[] keySetId) { + Assertions.checkArgument(!(forceDefaultLicenseUri && licenseUri == null)); this.uuid = uuid; this.licenseUri = licenseUri; this.requestHeaders = requestHeaders; diff --git a/library/core/src/main/java/com/google/android/exoplayer2/drm/HttpMediaDrmCallback.java b/library/core/src/main/java/com/google/android/exoplayer2/drm/HttpMediaDrmCallback.java index 7ab90b023e..6a20cf7bda 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/drm/HttpMediaDrmCallback.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/drm/HttpMediaDrmCallback.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.drm; +import android.net.Uri; import android.text.TextUtils; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; @@ -27,6 +28,7 @@ import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCode import com.google.android.exoplayer2.upstream.StatsDataSource; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import com.google.common.collect.ImmutableMap; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -39,29 +41,35 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback { private static final int MAX_MANUAL_REDIRECTS = 5; private final HttpDataSource.Factory dataSourceFactory; - private final String defaultLicenseUrl; + @Nullable private final String defaultLicenseUrl; private final boolean forceDefaultLicenseUrl; private final Map keyRequestProperties; /** * @param defaultLicenseUrl The default license URL. Used for key requests that do not specify - * their own license URL. + * their own license URL. May be {@code null} if it's known that all key requests will specify + * their own URLs. * @param dataSourceFactory A factory from which to obtain {@link HttpDataSource} instances. */ - public HttpMediaDrmCallback(String defaultLicenseUrl, HttpDataSource.Factory dataSourceFactory) { + public HttpMediaDrmCallback( + @Nullable String defaultLicenseUrl, HttpDataSource.Factory dataSourceFactory) { this(defaultLicenseUrl, /* forceDefaultLicenseUrl= */ false, dataSourceFactory); } /** * @param defaultLicenseUrl The default license URL. Used for key requests that do not specify - * their own license URL, or for all key requests if {@code forceDefaultLicenseUrl} is - * set to true. - * @param forceDefaultLicenseUrl Whether to use {@code defaultLicenseUrl} for key requests that - * include their own license URL. + * their own license URL, or for all key requests if {@code forceDefaultLicenseUrl} is set to + * true. May be {@code null} if {@code forceDefaultLicenseUrl} is {@code false} and if it's + * known that all key requests will specify their own URLs. + * @param forceDefaultLicenseUrl Whether to force use of {@code defaultLicenseUrl} for key + * requests that include their own license URL. * @param dataSourceFactory A factory from which to obtain {@link HttpDataSource} instances. */ - public HttpMediaDrmCallback(String defaultLicenseUrl, boolean forceDefaultLicenseUrl, + public HttpMediaDrmCallback( + @Nullable String defaultLicenseUrl, + boolean forceDefaultLicenseUrl, HttpDataSource.Factory dataSourceFactory) { + Assertions.checkArgument(!(forceDefaultLicenseUrl && TextUtils.isEmpty(defaultLicenseUrl))); this.dataSourceFactory = dataSourceFactory; this.defaultLicenseUrl = defaultLicenseUrl; this.forceDefaultLicenseUrl = forceDefaultLicenseUrl; @@ -121,6 +129,14 @@ public final class HttpMediaDrmCallback implements MediaDrmCallback { if (forceDefaultLicenseUrl || TextUtils.isEmpty(url)) { url = defaultLicenseUrl; } + if (TextUtils.isEmpty(url)) { + throw new MediaDrmCallbackException( + new DataSpec.Builder().setUri(Uri.EMPTY).build(), + Uri.EMPTY, + /* responseHeaders= */ ImmutableMap.of(), + /* bytesLoaded= */ 0, + /* cause= */ new IllegalStateException("No license URL")); + } Map requestProperties = new HashMap<>(); // Add standard request properties for supported schemes. String contentType = C.PLAYREADY_UUID.equals(uuid) ? "text/xml" diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceDrmHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceDrmHelper.java index 7859254401..f4a7b89fc7 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceDrmHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceDrmHelper.java @@ -17,7 +17,6 @@ package com.google.android.exoplayer2.source; import static com.google.android.exoplayer2.ExoPlayerLibraryInfo.DEFAULT_USER_AGENT; import static com.google.android.exoplayer2.drm.DefaultDrmSessionManager.MODE_PLAYBACK; -import static com.google.android.exoplayer2.util.Util.castNonNull; import androidx.annotation.Nullable; import com.google.android.exoplayer2.MediaItem; @@ -68,7 +67,7 @@ public final class MediaSourceDrmHelper { Assertions.checkNotNull(mediaItem.playbackProperties); @Nullable MediaItem.DrmConfiguration drmConfiguration = mediaItem.playbackProperties.drmConfiguration; - if (drmConfiguration == null || drmConfiguration.licenseUri == null || Util.SDK_INT < 18) { + if (drmConfiguration == null || Util.SDK_INT < 18) { return DrmSessionManager.getDummyDrmSessionManager(); } HttpDataSource.Factory dataSourceFactory = @@ -77,7 +76,7 @@ public final class MediaSourceDrmHelper { : new DefaultHttpDataSourceFactory(userAgent != null ? userAgent : DEFAULT_USER_AGENT); HttpMediaDrmCallback httpDrmCallback = new HttpMediaDrmCallback( - castNonNull(drmConfiguration.licenseUri).toString(), + drmConfiguration.licenseUri == null ? null : drmConfiguration.licenseUri.toString(), drmConfiguration.forceDefaultLicenseUri, dataSourceFactory); for (Map.Entry entry : drmConfiguration.requestHeaders.entrySet()) {