Skip to content

Commit

Permalink
Fix NPE when reading from a SampleQueue from a loading thread
Browse files Browse the repository at this point in the history
Issue: #7273
PiperOrigin-RevId: 308238035
  • Loading branch information
AquilesCanta authored and icbaker committed Apr 27, 2020
1 parent 4df7470 commit 30c55d1
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
`http://dashif.org/guidelines/trickmode`) into the same `TrackGroup` as
the main adaptation sets to which they refer. Trick play tracks are
marked with the `C.ROLE_FLAG_TRICK_PLAY` flag.
* Fix assertion failure in `SampleQueue` when playing DASH streams with
EMSG tracks ([#7273](https://github.com/google/ExoPlayer/issues/7273)).
* MP3: Add `IndexSeeker` for accurate seeks in VBR streams
([#6787](https://github.com/google/ExoPlayer/issues/6787)). This seeker is
enabled by passing `FLAG_ENABLE_INDEX_SEEKING` to the `Mp3Extractor`. It may
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,12 @@ private TrackOutput prepareTrackOutput(TrackId id) {
return sampleQueues[i];
}
}
SampleQueue trackOutput = new SampleQueue(allocator, drmSessionManager, eventDispatcher);
SampleQueue trackOutput =
new SampleQueue(
allocator,
/* playbackLooper= */ handler.getLooper(),
drmSessionManager,
eventDispatcher);
trackOutput.setUpstreamFormatChangeListener(this);
@NullableType
TrackId[] sampleQueueTrackIds = Arrays.copyOf(this.sampleQueueTrackIds, trackCount + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public interface UpstreamFormatChangedListener {

private final SampleDataQueue sampleDataQueue;
private final SampleExtrasHolder extrasHolder;
private final Looper playbackLooper;
private final DrmSessionManager drmSessionManager;
private final MediaSourceEventDispatcher eventDispatcher;
@Nullable private UpstreamFormatChangedListener upstreamFormatChangeListener;
Expand Down Expand Up @@ -94,18 +95,21 @@ public interface UpstreamFormatChangedListener {
* Creates a sample queue.
*
* @param allocator An {@link Allocator} from which allocations for sample data can be obtained.
* @param playbackLooper The looper associated with the media playback thread.
* @param drmSessionManager The {@link DrmSessionManager} to obtain {@link DrmSession DrmSessions}
* from. The created instance does not take ownership of this {@link DrmSessionManager}.
* @param eventDispatcher A {@link MediaSourceEventDispatcher} to notify of events related to this
* SampleQueue.
*/
public SampleQueue(
Allocator allocator,
Looper playbackLooper,
DrmSessionManager drmSessionManager,
MediaSourceEventDispatcher eventDispatcher) {
sampleDataQueue = new SampleDataQueue(allocator);
this.playbackLooper = playbackLooper;
this.drmSessionManager = drmSessionManager;
this.eventDispatcher = eventDispatcher;
sampleDataQueue = new SampleDataQueue(allocator);
extrasHolder = new SampleExtrasHolder();
capacity = SAMPLE_CAPACITY_INCREMENT;
sourceIds = new int[capacity];
Expand Down Expand Up @@ -799,7 +803,6 @@ private void onFormatResult(Format newFormat, FormatHolder outputFormatHolder) {
// Ensure we acquire the new session before releasing the previous one in case the same session
// is being used for both DrmInitData.
@Nullable DrmSession previousSession = currentDrmSession;
Looper playbackLooper = Assertions.checkNotNull(Looper.myLooper());
currentDrmSession =
newDrmInitData != null
? drmSessionManager.acquireSession(playbackLooper, eventDispatcher, newDrmInitData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.source.chunk;

import android.os.Looper;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
Expand Down Expand Up @@ -132,14 +133,22 @@ public ChunkSampleStream(
int[] trackTypes = new int[1 + embeddedTrackCount];
SampleQueue[] sampleQueues = new SampleQueue[1 + embeddedTrackCount];

primarySampleQueue = new SampleQueue(allocator, drmSessionManager, eventDispatcher);
primarySampleQueue =
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
drmSessionManager,
eventDispatcher);
trackTypes[0] = primaryTrackType;
sampleQueues[0] = primarySampleQueue;

for (int i = 0; i < embeddedTrackCount; i++) {
SampleQueue sampleQueue =
new SampleQueue(
allocator, DrmSessionManager.getDummyDrmSessionManager(), eventDispatcher);
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
DrmSessionManager.getDummyDrmSessionManager(),
eventDispatcher);
embeddedSampleQueues[i] = sampleQueue;
sampleQueues[i + 1] = sampleQueue;
trackTypes[i + 1] = this.embeddedTrackTypes[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.when;

import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
Expand All @@ -39,6 +40,7 @@
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.DefaultAllocator;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MediaSourceEventDispatcher;
import com.google.android.exoplayer2.util.ParsableByteArray;
import java.io.IOException;
Expand Down Expand Up @@ -139,7 +141,12 @@ public void setUp() {
ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any()))
.thenReturn(mockDrmSession);
eventDispatcher = new MediaSourceEventDispatcher();
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager, eventDispatcher);
sampleQueue =
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
mockDrmSessionManager,
eventDispatcher);
formatHolder = new FormatHolder();
inputBuffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
}
Expand Down Expand Up @@ -356,7 +363,12 @@ public void isReadyReturnsTrueForValidDrmSession() {
public void isReadyReturnsTrueForClearSampleAndPlayClearSamplesWithoutKeysIsTrue() {
when(mockDrmSession.playClearSamplesWithoutKeys()).thenReturn(true);
// We recreate the queue to ensure the mock DRM session manager flags are taken into account.
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager, eventDispatcher);
sampleQueue =
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
mockDrmSessionManager,
eventDispatcher);
writeTestDataWithEncryptedSections();
assertThat(sampleQueue.isReady(/* loadingFinished= */ false)).isTrue();
}
Expand Down Expand Up @@ -534,7 +546,12 @@ public void readWithErrorSessionReadsNothingAndThrows() throws IOException {
public void allowPlayClearSamplesWithoutKeysReadsClearSamples() {
when(mockDrmSession.playClearSamplesWithoutKeys()).thenReturn(true);
// We recreate the queue to ensure the mock DRM session manager flags are taken into account.
sampleQueue = new SampleQueue(allocator, mockDrmSessionManager, eventDispatcher);
sampleQueue =
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
mockDrmSessionManager,
eventDispatcher);
when(mockDrmSession.getState()).thenReturn(DrmSession.STATE_OPENED);
writeTestDataWithEncryptedSections();

Expand Down Expand Up @@ -924,7 +941,11 @@ public void setSampleOffsetBetweenSamples() {
public void adjustUpstreamFormat() {
String label = "label";
sampleQueue =
new SampleQueue(allocator, mockDrmSessionManager, eventDispatcher) {
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
mockDrmSessionManager,
eventDispatcher) {
@Override
public Format getAdjustedUpstreamFormat(Format format) {
return super.getAdjustedUpstreamFormat(copyWithLabel(format, label));
Expand All @@ -940,7 +961,11 @@ public Format getAdjustedUpstreamFormat(Format format) {
public void invalidateUpstreamFormatAdjustment() {
AtomicReference<String> label = new AtomicReference<>("label1");
sampleQueue =
new SampleQueue(allocator, mockDrmSessionManager, eventDispatcher) {
new SampleQueue(
allocator,
/* playbackLooper= */ Assertions.checkNotNull(Looper.myLooper()),
mockDrmSessionManager,
eventDispatcher) {
@Override
public Format getAdjustedUpstreamFormat(Format format) {
return super.getAdjustedUpstreamFormat(copyWithLabel(format, label.get()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public final class PlayerTrackEmsgHandler implements TrackOutput {
this.sampleQueue =
new SampleQueue(
allocator,
/* playbackLooper= */ handler.getLooper(),
DrmSessionManager.getDummyDrmSessionManager(),
new MediaSourceEventDispatcher());
formatHolder = new FormatHolder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseIntArray;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
Expand Down Expand Up @@ -910,7 +911,12 @@ private SampleQueue createSampleQueue(int id, int type) {

boolean isAudioVideo = type == C.TRACK_TYPE_AUDIO || type == C.TRACK_TYPE_VIDEO;
HlsSampleQueue sampleQueue =
new HlsSampleQueue(allocator, drmSessionManager, eventDispatcher, overridingDrmInitData);
new HlsSampleQueue(
allocator,
/* playbackLooper= */ handler.getLooper(),
drmSessionManager,
eventDispatcher,
overridingDrmInitData);
if (isAudioVideo) {
sampleQueue.setDrmInitData(drmInitData);
}
Expand Down Expand Up @@ -1380,10 +1386,11 @@ private static final class HlsSampleQueue extends SampleQueue {

private HlsSampleQueue(
Allocator allocator,
Looper playbackLooper,
DrmSessionManager drmSessionManager,
MediaSourceEventDispatcher eventDispatcher,
Map<String, DrmInitData> overridingDrmInitData) {
super(allocator, drmSessionManager, eventDispatcher);
super(allocator, playbackLooper, drmSessionManager, eventDispatcher);
this.overridingDrmInitData = overridingDrmInitData;
}

Expand Down

0 comments on commit 30c55d1

Please sign in to comment.