Ease the creation of SampleQueues without DRM management

This is useful in cases where the client is not interested in DRM.

PiperOrigin-RevId: 346313024
This commit is contained in:
aquilescanta 2020-12-08 14:51:09 +00:00 committed by Ian Baker
parent d148db5725
commit 9eef7b06f7
4 changed files with 43 additions and 22 deletions

View file

@ -715,7 +715,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
}
}
SampleQueue trackOutput =
new SampleQueue(
SampleQueue.createWithDrm(
allocator,
/* playbackLooper= */ handler.getLooper(),
drmSessionManager,

View file

@ -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.
*
* <p>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) {

View file

@ -145,7 +145,7 @@ public class ChunkSampleStream<T extends ChunkSource> 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<T extends ChunkSource> 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];

View file

@ -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();
}