From 9eef7b06f71bffcd597e1f8c27aef6c3ff8fd9d8 Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Tue, 8 Dec 2020 14:51:09 +0000 Subject: [PATCH] Ease the creation of SampleQueues without DRM management This is useful in cases where the client is not interested in DRM. PiperOrigin-RevId: 346313024 --- .../source/ProgressiveMediaPeriod.java | 2 +- .../exoplayer2/source/SampleQueue.java | 45 ++++++++++++++++--- .../source/chunk/ChunkSampleStream.java | 9 +--- .../source/dash/PlayerEmsgHandler.java | 9 +--- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java index f3f46d553b..5d7636be6a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/ProgressiveMediaPeriod.java @@ -715,7 +715,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; } } SampleQueue trackOutput = - new SampleQueue( + SampleQueue.createWithDrm( allocator, /* playbackLooper= */ handler.getLooper(), drmSessionManager, diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/SampleQueue.java b/library/core/src/main/java/com/google/android/exoplayer2/source/SampleQueue.java index 824b8dedaf..df2ca4847f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/SampleQueue.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/SampleQueue.java @@ -60,9 +60,9 @@ public class SampleQueue implements TrackOutput { private final SampleDataQueue sampleDataQueue; private final SampleExtrasHolder extrasHolder; - private final Looper playbackLooper; - private final DrmSessionManager drmSessionManager; - private final DrmSessionEventListener.EventDispatcher drmEventDispatcher; + @Nullable private final DrmSessionManager drmSessionManager; + @Nullable private final DrmSessionEventListener.EventDispatcher drmEventDispatcher; + @Nullable private final Looper playbackLooper; @Nullable private UpstreamFormatChangedListener upstreamFormatChangeListener; @Nullable private Format downstreamFormat; @@ -100,7 +100,23 @@ public class SampleQueue implements TrackOutput { private boolean pendingSplice; /** - * Creates a sample queue. + * Creates a sample queue without DRM resource management. + * + * @param allocator An {@link Allocator} from which allocations for sample data can be obtained. + */ + public static SampleQueue createWithoutDrm(Allocator allocator) { + return new SampleQueue( + allocator, + /* playbackLooper= */ null, + /* drmSessionManager= */ null, + /* drmEventDispatcher= */ null); + } + + /** + * Creates a sample queue with DRM resource management. + * + *

For each sample added to the queue, a {@link DrmSession} will be attached containing the + * keys needed to decrypt it. * * @param allocator An {@link Allocator} from which allocations for sample data can be obtained. * @param playbackLooper The looper associated with the media playback thread. @@ -109,11 +125,23 @@ public class SampleQueue implements TrackOutput { * @param drmEventDispatcher A {@link DrmSessionEventListener.EventDispatcher} to notify of events * related to this SampleQueue. */ - public SampleQueue( + public static SampleQueue createWithDrm( Allocator allocator, Looper playbackLooper, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher) { + return new SampleQueue( + allocator, + Assertions.checkNotNull(playbackLooper), + Assertions.checkNotNull(drmSessionManager), + Assertions.checkNotNull(drmEventDispatcher)); + } + + protected SampleQueue( + Allocator allocator, + @Nullable Looper playbackLooper, + @Nullable DrmSessionManager drmSessionManager, + @Nullable DrmSessionEventListener.EventDispatcher drmEventDispatcher) { this.playbackLooper = playbackLooper; this.drmSessionManager = drmSessionManager; this.drmEventDispatcher = drmEventDispatcher; @@ -844,6 +872,10 @@ public class SampleQueue implements TrackOutput { outputFormatHolder.format = newFormat.copyWithExoMediaCryptoType(drmSessionManager.getExoMediaCryptoType(newFormat)); outputFormatHolder.drmSession = currentDrmSession; + if (drmSessionManager == null) { + // This sample queue is not expected to handle DRM. Nothing to do. + return; + } if (!isFirstFormat && Util.areEqual(oldDrmInitData, newDrmInitData)) { // Nothing to do. return; @@ -852,7 +884,8 @@ public class SampleQueue implements TrackOutput { // is being used for both DrmInitData. @Nullable DrmSession previousSession = currentDrmSession; currentDrmSession = - drmSessionManager.acquireSession(playbackLooper, drmEventDispatcher, newFormat); + drmSessionManager.acquireSession( + Assertions.checkNotNull(playbackLooper), drmEventDispatcher, newFormat); outputFormatHolder.drmSession = currentDrmSession; if (previousSession != null) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java index 1a5371c83f..88a8a0d1ea 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java @@ -145,7 +145,7 @@ public class ChunkSampleStream implements SampleStream, S SampleQueue[] sampleQueues = new SampleQueue[1 + embeddedTrackCount]; primarySampleQueue = - new SampleQueue( + SampleQueue.createWithDrm( allocator, /* playbackLooper= */ checkNotNull(Looper.myLooper()), drmSessionManager, @@ -154,12 +154,7 @@ public class ChunkSampleStream implements SampleStream, S sampleQueues[0] = primarySampleQueue; for (int i = 0; i < embeddedTrackCount; i++) { - SampleQueue sampleQueue = - new SampleQueue( - allocator, - /* playbackLooper= */ checkNotNull(Looper.myLooper()), - DrmSessionManager.getDummyDrmSessionManager(), - drmEventDispatcher); + SampleQueue sampleQueue = SampleQueue.createWithoutDrm(allocator); embeddedSampleQueues[i] = sampleQueue; sampleQueues[i + 1] = sampleQueue; trackTypes[i + 1] = this.embeddedTrackTypes[i]; diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/PlayerEmsgHandler.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/PlayerEmsgHandler.java index 2185b52f93..725d0b6456 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/PlayerEmsgHandler.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/PlayerEmsgHandler.java @@ -24,8 +24,6 @@ import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.ParserException; -import com.google.android.exoplayer2.drm.DrmSessionEventListener; -import com.google.android.exoplayer2.drm.DrmSessionManager; import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.metadata.MetadataInputBuffer; @@ -285,12 +283,7 @@ public final class PlayerEmsgHandler implements Handler.Callback { private final MetadataInputBuffer buffer; /* package */ PlayerTrackEmsgHandler(Allocator allocator) { - this.sampleQueue = - new SampleQueue( - allocator, - /* playbackLooper= */ handler.getLooper(), - DrmSessionManager.getDummyDrmSessionManager(), - new DrmSessionEventListener.EventDispatcher()); + this.sampleQueue = SampleQueue.createWithoutDrm(allocator); formatHolder = new FormatHolder(); buffer = new MetadataInputBuffer(); }