From cfda4a58705362b0a2ae245044c12b7256819971 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 3 Feb 2020 16:05:52 +0000 Subject: [PATCH] Add some nullness annotations to SampleQueue PiperOrigin-RevId: 292921158 --- .../source/ProgressiveMediaPeriod.java | 2 +- .../exoplayer2/source/SampleQueue.java | 43 ++++++++++--------- .../exoplayer2/source/hls/HlsMediaPeriod.java | 2 +- .../source/hls/HlsSampleStreamWrapper.java | 9 ++-- 4 files changed, 31 insertions(+), 25 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 70d65b80a3..c811d52e56 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 @@ -719,7 +719,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; TrackGroup[] trackArray = new TrackGroup[trackCount]; boolean[] trackIsAudioVideoFlags = new boolean[trackCount]; for (int i = 0; i < trackCount; i++) { - Format trackFormat = sampleQueues[i].getUpstreamFormat(); + Format trackFormat = Assertions.checkNotNull(sampleQueues[i].getUpstreamFormat()); String mimeType = trackFormat.sampleMimeType; boolean isAudio = MimeTypes.isAudio(mimeType); boolean isAudioVideo = isAudio || MimeTypes.isVideo(mimeType); 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 d4e447bc61..ff0221a73a 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 @@ -34,6 +34,7 @@ import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.Util; import java.io.IOException; +import org.checkerframework.checker.nullness.compatqual.NullableType; /** A queue of media samples. */ public class SampleQueue implements TrackOutput { @@ -54,7 +55,7 @@ public class SampleQueue implements TrackOutput { private final SampleDataQueue sampleDataQueue; private final SampleExtrasHolder extrasHolder; private final DrmSessionManager drmSessionManager; - private UpstreamFormatChangedListener upstreamFormatChangeListener; + @Nullable private UpstreamFormatChangedListener upstreamFormatChangeListener; @Nullable private Format downstreamFormat; @Nullable private DrmSession currentDrmSession; @@ -65,7 +66,7 @@ public class SampleQueue implements TrackOutput { private int[] sizes; private int[] flags; private long[] timesUs; - private CryptoData[] cryptoDatas; + private @NullableType CryptoData[] cryptoDatas; private Format[] formats; private int length; @@ -78,12 +79,12 @@ public class SampleQueue implements TrackOutput { private boolean isLastSampleQueued; private boolean upstreamKeyframeRequired; private boolean upstreamFormatRequired; - private Format upstreamFormat; - private Format upstreamCommittedFormat; + private boolean upstreamFormatAdjustmentRequired; + @Nullable private Format unadjustedUpstreamFormat; + @Nullable private Format upstreamFormat; + @Nullable private Format upstreamCommittedFormat; private int upstreamSourceId; - private boolean pendingUpstreamFormatAdjustment; - private Format unadjustedUpstreamFormat; private long sampleOffsetUs; private boolean pendingSplice; @@ -226,6 +227,7 @@ public class SampleQueue implements TrackOutput { } /** Returns the upstream {@link Format} in which samples are being queued. */ + @Nullable public final synchronized Format getUpstreamFormat() { return upstreamFormatRequired ? null : upstreamFormat; } @@ -448,7 +450,8 @@ public class SampleQueue implements TrackOutput { * * @param listener The listener. */ - public final void setUpstreamFormatChangeListener(UpstreamFormatChangedListener listener) { + public final void setUpstreamFormatChangeListener( + @Nullable UpstreamFormatChangedListener listener) { upstreamFormatChangeListener = listener; } @@ -457,7 +460,7 @@ public class SampleQueue implements TrackOutput { @Override public final void format(Format unadjustedUpstreamFormat) { Format adjustedUpstreamFormat = getAdjustedUpstreamFormat(unadjustedUpstreamFormat); - pendingUpstreamFormatAdjustment = false; + upstreamFormatAdjustmentRequired = false; this.unadjustedUpstreamFormat = unadjustedUpstreamFormat; boolean upstreamFormatChanged = setUpstreamFormat(adjustedUpstreamFormat); if (upstreamFormatChangeListener != null && upstreamFormatChanged) { @@ -483,8 +486,8 @@ public class SampleQueue implements TrackOutput { int size, int offset, @Nullable CryptoData cryptoData) { - if (pendingUpstreamFormatAdjustment) { - format(unadjustedUpstreamFormat); + if (upstreamFormatAdjustmentRequired) { + format(Assertions.checkStateNotNull(unadjustedUpstreamFormat)); } timeUs += sampleOffsetUs; if (pendingSplice) { @@ -502,7 +505,7 @@ public class SampleQueue implements TrackOutput { * will be called to adjust the upstream {@link Format} again before the next sample is queued. */ protected final void invalidateUpstreamFormatAdjustment() { - pendingUpstreamFormatAdjustment = true; + upstreamFormatAdjustmentRequired = true; } /** @@ -593,10 +596,6 @@ public class SampleQueue implements TrackOutput { } private synchronized boolean setUpstreamFormat(Format format) { - if (format == null) { - upstreamFormatRequired = true; - return false; - } upstreamFormatRequired = false; if (Util.areEqual(format, upstreamFormat)) { // The format is unchanged. If format and upstreamFormat are different objects, we keep the @@ -653,7 +652,11 @@ public class SampleQueue implements TrackOutput { } private synchronized void commitSample( - long timeUs, @C.BufferFlags int sampleFlags, long offset, int size, CryptoData cryptoData) { + long timeUs, + @C.BufferFlags int sampleFlags, + long offset, + int size, + @Nullable CryptoData cryptoData) { if (upstreamKeyframeRequired) { if ((sampleFlags & C.BUFFER_FLAG_KEY_FRAME) == 0) { return; @@ -770,7 +773,7 @@ public class SampleQueue implements TrackOutput { private void onFormatResult(Format newFormat, FormatHolder outputFormatHolder) { outputFormatHolder.format = newFormat; boolean isFirstFormat = downstreamFormat == null; - DrmInitData oldDrmInitData = isFirstFormat ? null : downstreamFormat.drmInitData; + @Nullable DrmInitData oldDrmInitData = isFirstFormat ? null : downstreamFormat.drmInitData; downstreamFormat = newFormat; if (drmSessionManager == DrmSessionManager.DUMMY) { // Avoid attempting to acquire a session using the dummy DRM session manager. It's likely that @@ -779,7 +782,7 @@ public class SampleQueue implements TrackOutput { // TODO: Remove once renderers are migrated [Internal ref: b/122519809]. return; } - DrmInitData newDrmInitData = newFormat.drmInitData; + @Nullable DrmInitData newDrmInitData = newFormat.drmInitData; outputFormatHolder.includesDrmSession = true; outputFormatHolder.drmSession = currentDrmSession; if (!isFirstFormat && Util.areEqual(oldDrmInitData, newDrmInitData)) { @@ -788,7 +791,7 @@ public class SampleQueue implements TrackOutput { } // Ensure we acquire the new session before releasing the previous one in case the same session // is being used for both DrmInitData. - DrmSession previousSession = currentDrmSession; + @Nullable DrmSession previousSession = currentDrmSession; Looper playbackLooper = Assertions.checkNotNull(Looper.myLooper()); currentDrmSession = newDrmInitData != null @@ -920,6 +923,6 @@ public class SampleQueue implements TrackOutput { public int size; public long offset; - public CryptoData cryptoData; + @Nullable public CryptoData cryptoData; } } diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java index 7d6184b4df..b38cc4fefd 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java @@ -665,7 +665,7 @@ public final class HlsMediaPeriod implements MediaPeriod, HlsSampleStreamWrapper sampleStreamWrapper.prepareWithMasterPlaylistInfo( muxedTrackGroups.toArray(new TrackGroup[0]), /* primaryTrackGroupIndex= */ 0, - /* optionalTrackGroupsIndices= */ muxedTrackGroups.indexOf(id3TrackGroup)); + /* optionalTrackGroupsIndices...= */ muxedTrackGroups.indexOf(id3TrackGroup)); } } diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java index 3e2f477b0c..2b7e012770 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsSampleStreamWrapper.java @@ -1069,7 +1069,8 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; for (int i = 0; i < trackGroupCount; i++) { for (int queueIndex = 0; queueIndex < sampleQueues.length; queueIndex++) { SampleQueue sampleQueue = sampleQueues[queueIndex]; - if (formatsMatch(sampleQueue.getUpstreamFormat(), trackGroups.get(i).getFormat(0))) { + Format upstreamFormat = Assertions.checkStateNotNull(sampleQueue.getUpstreamFormat()); + if (formatsMatch(upstreamFormat, trackGroups.get(i).getFormat(0))) { trackGroupToSampleQueueIndex[i] = queueIndex; break; } @@ -1118,7 +1119,9 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; int primaryExtractorTrackIndex = C.INDEX_UNSET; int extractorTrackCount = sampleQueues.length; for (int i = 0; i < extractorTrackCount; i++) { - @Nullable String sampleMimeType = sampleQueues[i].getUpstreamFormat().sampleMimeType; + @Nullable + String sampleMimeType = + Assertions.checkStateNotNull(sampleQueues[i].getUpstreamFormat()).sampleMimeType; int trackType; if (MimeTypes.isVideo(sampleMimeType)) { trackType = C.TRACK_TYPE_VIDEO; @@ -1153,7 +1156,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull; // Construct the set of exposed track groups. TrackGroup[] trackGroups = new TrackGroup[extractorTrackCount]; for (int i = 0; i < extractorTrackCount; i++) { - Format sampleFormat = sampleQueues[i].getUpstreamFormat(); + Format sampleFormat = Assertions.checkStateNotNull(sampleQueues[i].getUpstreamFormat()); if (i == primaryExtractorTrackIndex) { Format[] formats = new Format[chunkSourceTrackCount]; if (chunkSourceTrackCount == 1) {