From 9974670cc7ed0c908d3a5507424a0af6d04c7eb5 Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Wed, 4 Dec 2019 19:18:56 +0000 Subject: [PATCH] Make DownloadHelper pass DrmSessionManager to MediaSources PiperOrigin-RevId: 283795201 --- .../exoplayer2/demo/PlayerActivity.java | 13 ++++- .../exoplayer2/offline/DownloadHelper.java | 49 ++++++++++++++++--- .../source/ExtractorMediaSource.java | 7 +++ .../exoplayer2/source/MediaSourceFactory.java | 11 +++++ .../source/ProgressiveMediaSource.java | 29 +++++------ .../source/dash/DashMediaSource.java | 29 +++++------ .../exoplayer2/source/hls/HlsMediaSource.java | 29 +++++------ .../source/smoothstreaming/SsMediaSource.java | 29 +++++------ 8 files changed, 131 insertions(+), 65 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index 2f8d0045d3..11a4b7216b 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -615,10 +615,21 @@ public class PlayerActivity extends AppCompatActivity } MediaSourceFactory adMediaSourceFactory = new MediaSourceFactory() { + + private DrmSessionManager drmSessionManager = + DrmSessionManager.getDummyDrmSessionManager(); + + @Override + @SuppressWarnings("unchecked") // Safe upcasting. + public MediaSourceFactory setDrmSessionManager(DrmSessionManager drmSessionManager) { + this.drmSessionManager = (DrmSessionManager) drmSessionManager; + return this; + } + @Override public MediaSource createMediaSource(Uri uri) { return PlayerActivity.this.createLeafMediaSource( - uri, /* extension=*/ null, DrmSessionManager.getDummyDrmSessionManager()); + uri, /* extension=*/ null, drmSessionManager); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java index c585c79b76..b2641552c0 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java @@ -263,9 +263,13 @@ public final class DownloadHelper { uri, /* cacheKey= */ null, createMediaSourceInternal( - DASH_FACTORY_CONSTRUCTOR, uri, dataSourceFactory, /* streamKeys= */ null), + DASH_FACTORY_CONSTRUCTOR, + uri, + dataSourceFactory, + drmSessionManager, + /* streamKeys= */ null), trackSelectorParameters, - Util.getRendererCapabilities(renderersFactory, drmSessionManager)); + Util.getRendererCapabilities(renderersFactory, /* drmSessionManager= */ null)); } /** @deprecated Use {@link #forHls(Context, Uri, Factory, RenderersFactory)} */ @@ -329,9 +333,13 @@ public final class DownloadHelper { uri, /* cacheKey= */ null, createMediaSourceInternal( - HLS_FACTORY_CONSTRUCTOR, uri, dataSourceFactory, /* streamKeys= */ null), + HLS_FACTORY_CONSTRUCTOR, + uri, + dataSourceFactory, + drmSessionManager, + /* streamKeys= */ null), trackSelectorParameters, - Util.getRendererCapabilities(renderersFactory, drmSessionManager)); + Util.getRendererCapabilities(renderersFactory, /* drmSessionManager= */ null)); } /** @deprecated Use {@link #forSmoothStreaming(Context, Uri, Factory, RenderersFactory)} */ @@ -395,9 +403,24 @@ public final class DownloadHelper { uri, /* cacheKey= */ null, createMediaSourceInternal( - SS_FACTORY_CONSTRUCTOR, uri, dataSourceFactory, /* streamKeys= */ null), + SS_FACTORY_CONSTRUCTOR, + uri, + dataSourceFactory, + drmSessionManager, + /* streamKeys= */ null), trackSelectorParameters, - Util.getRendererCapabilities(renderersFactory, drmSessionManager)); + Util.getRendererCapabilities(renderersFactory, /* drmSessionManager= */ null)); + } + + /** + * Equivalent to {@link #createMediaSource(DownloadRequest, Factory, DrmSessionManager) + * createMediaSource(downloadRequest, dataSourceFactory, + * DrmSessionManager.getDummyDrmSessionManager())}. + */ + public static MediaSource createMediaSource( + DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory) { + return createMediaSource( + downloadRequest, dataSourceFactory, DrmSessionManager.getDummyDrmSessionManager()); } /** @@ -409,7 +432,9 @@ public final class DownloadHelper { * @return A MediaSource which only contains the tracks defined in {@code downloadRequest}. */ public static MediaSource createMediaSource( - DownloadRequest downloadRequest, DataSource.Factory dataSourceFactory) { + DownloadRequest downloadRequest, + DataSource.Factory dataSourceFactory, + DrmSessionManager drmSessionManager) { @Nullable Constructor constructor; switch (downloadRequest.type) { case DownloadRequest.TYPE_DASH: @@ -428,7 +453,11 @@ public final class DownloadHelper { throw new IllegalStateException("Unsupported type: " + downloadRequest.type); } return createMediaSourceInternal( - constructor, downloadRequest.uri, dataSourceFactory, downloadRequest.streamKeys); + constructor, + downloadRequest.uri, + dataSourceFactory, + drmSessionManager, + downloadRequest.streamKeys); } private final String downloadType; @@ -888,12 +917,16 @@ public final class DownloadHelper { @Nullable Constructor constructor, Uri uri, Factory dataSourceFactory, + @Nullable DrmSessionManager drmSessionManager, @Nullable List streamKeys) { if (constructor == null) { throw new IllegalStateException("Module missing to create media source."); } try { MediaSourceFactory factory = constructor.newInstance(dataSourceFactory); + if (drmSessionManager != null) { + factory.setDrmSessionManager(drmSessionManager); + } if (streamKeys != null) { factory.setStreamKeys(streamKeys); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorMediaSource.java index 060027fee7..830a62a884 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ExtractorMediaSource.java @@ -179,6 +179,13 @@ public final class ExtractorMediaSource extends CompositeMediaSource { return this; } + /** @deprecated Use {@link ProgressiveMediaSource.Factory#setDrmSessionManager} instead. */ + @Override + @Deprecated + public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { + throw new UnsupportedOperationException(); + } + /** * Returns a new {@link ExtractorMediaSource} using the current parameters. * diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceFactory.java index c53abd1235..201f241d59 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceFactory.java @@ -17,6 +17,8 @@ package com.google.android.exoplayer2.source; import android.net.Uri; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.drm.DrmSession; +import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.offline.StreamKey; import java.util.List; @@ -34,6 +36,15 @@ public interface MediaSourceFactory { return this; } + /** + * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. + * + * @param drmSessionManager The {@link DrmSessionManager}. + * @return This factory, for convenience. + * @throws IllegalStateException If one of the {@code create} methods has already been called. + */ + MediaSourceFactory setDrmSessionManager(DrmSessionManager drmSessionManager); + /** * Creates a new {@link MediaSource} with the specified {@code uri}. * diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaSource.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaSource.java index c88972da62..f05b576acb 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaSource.java @@ -132,20 +132,6 @@ public final class ProgressiveMediaSource extends BaseMediaSource return this; } - /** - * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The - * default value is {@link DrmSessionManager#DUMMY}. - * - * @param drmSessionManager The {@link DrmSessionManager}. - * @return This factory, for convenience. - * @throws IllegalStateException If one of the {@code create} methods has already been called. - */ - public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { - Assertions.checkState(!isCreateCalled); - this.drmSessionManager = drmSessionManager; - return this; - } - /** * Sets the {@link LoadErrorHandlingPolicy}. The default value is created by calling {@link * DefaultLoadErrorHandlingPolicy#DefaultLoadErrorHandlingPolicy()}. @@ -177,6 +163,21 @@ public final class ProgressiveMediaSource extends BaseMediaSource return this; } + /** + * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The + * default value is {@link DrmSessionManager#DUMMY}. + * + * @param drmSessionManager The {@link DrmSessionManager}. + * @return This factory, for convenience. + * @throws IllegalStateException If one of the {@code create} methods has already been called. + */ + @Override + public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { + Assertions.checkState(!isCreateCalled); + this.drmSessionManager = drmSessionManager; + return this; + } + /** * Returns a new {@link ProgressiveMediaSource} using the current parameters. * diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaSource.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaSource.java index 3f179d0e7e..dfcd62b8b1 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaSource.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaSource.java @@ -136,20 +136,6 @@ public final class DashMediaSource extends BaseMediaSource { return this; } - /** - * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The - * default value is {@link DrmSessionManager#DUMMY}. - * - * @param drmSessionManager The {@link DrmSessionManager}. - * @return This factory, for convenience. - * @throws IllegalStateException If one of the {@code create} methods has already been called. - */ - public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { - Assertions.checkState(!isCreateCalled); - this.drmSessionManager = drmSessionManager; - return this; - } - /** * Sets the minimum number of times to retry if a loading error occurs. See {@link * #setLoadErrorHandlingPolicy} for the default value. @@ -310,6 +296,21 @@ public final class DashMediaSource extends BaseMediaSource { return mediaSource; } + /** + * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The + * default value is {@link DrmSessionManager#DUMMY}. + * + * @param drmSessionManager The {@link DrmSessionManager}. + * @return This factory, for convenience. + * @throws IllegalStateException If one of the {@code create} methods has already been called. + */ + @Override + public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { + Assertions.checkState(!isCreateCalled); + this.drmSessionManager = drmSessionManager; + return this; + } + /** * Returns a new {@link DashMediaSource} using the current parameters. * diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java index db52fa1c02..16dedb6c21 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaSource.java @@ -160,20 +160,6 @@ public final class HlsMediaSource extends BaseMediaSource return this; } - /** - * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The - * default value is {@link DrmSessionManager#DUMMY}. - * - * @param drmSessionManager The {@link DrmSessionManager}. - * @return This factory, for convenience. - * @throws IllegalStateException If one of the {@code create} methods has already been called. - */ - public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { - Assertions.checkState(!isCreateCalled); - this.drmSessionManager = drmSessionManager; - return this; - } - /** * Sets the {@link LoadErrorHandlingPolicy}. The default value is created by calling {@link * DefaultLoadErrorHandlingPolicy#DefaultLoadErrorHandlingPolicy()}. @@ -326,6 +312,21 @@ public final class HlsMediaSource extends BaseMediaSource return mediaSource; } + /** + * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The + * default value is {@link DrmSessionManager#DUMMY}. + * + * @param drmSessionManager The {@link DrmSessionManager}. + * @return This factory, for convenience. + * @throws IllegalStateException If one of the {@code create} methods has already been called. + */ + @Override + public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { + Assertions.checkState(!isCreateCalled); + this.drmSessionManager = drmSessionManager; + return this; + } + /** * Returns a new {@link HlsMediaSource} using the current parameters. * diff --git a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/SsMediaSource.java b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/SsMediaSource.java index 4c05353186..8cc848dfa4 100644 --- a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/SsMediaSource.java +++ b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/SsMediaSource.java @@ -121,20 +121,6 @@ public final class SsMediaSource extends BaseMediaSource return this; } - /** - * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The - * default value is {@link DrmSessionManager#DUMMY}. - * - * @param drmSessionManager The {@link DrmSessionManager}. - * @return This factory, for convenience. - * @throws IllegalStateException If one of the {@code create} methods has already been called. - */ - public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { - Assertions.checkState(!isCreateCalled); - this.drmSessionManager = drmSessionManager; - return this; - } - /** * Sets the minimum number of times to retry if a loading error occurs. See {@link * #setLoadErrorHandlingPolicy} for the default value. @@ -276,6 +262,21 @@ public final class SsMediaSource extends BaseMediaSource return mediaSource; } + /** + * Sets the {@link DrmSessionManager} to use for acquiring {@link DrmSession DrmSessions}. The + * default value is {@link DrmSessionManager#DUMMY}. + * + * @param drmSessionManager The {@link DrmSessionManager}. + * @return This factory, for convenience. + * @throws IllegalStateException If one of the {@code create} methods has already been called. + */ + @Override + public Factory setDrmSessionManager(DrmSessionManager drmSessionManager) { + Assertions.checkState(!isCreateCalled); + this.drmSessionManager = drmSessionManager; + return this; + } + /** * Returns a new {@link SsMediaSource} using the current parameters. *