diff --git a/library/core/src/main/java/com/google/android/exoplayer2/BaseRenderer.java b/library/core/src/main/java/com/google/android/exoplayer2/BaseRenderer.java index fa161a35c9..902350e900 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/BaseRenderer.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/BaseRenderer.java @@ -30,11 +30,11 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { private final int trackType; private final FormatHolder formatHolder; - private RendererConfiguration configuration; + @Nullable private RendererConfiguration configuration; private int index; private int state; - private SampleStream stream; - private Format[] streamFormats; + @Nullable private SampleStream stream; + @Nullable private Format[] streamFormats; private long streamOffsetUs; private long lastResetPositionUs; private long readingPositionUs; @@ -144,7 +144,7 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { @Override public final void maybeThrowStreamError() throws IOException { - stream.maybeThrowError(); + Assertions.checkNotNull(stream).maybeThrowError(); } @Override @@ -303,16 +303,24 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { return formatHolder; } - /** Returns the formats of the currently enabled stream. */ + /** + * Returns the formats of the currently enabled stream. + * + *

This method may be called when the renderer is in the following states: {@link + * #STATE_ENABLED}, {@link #STATE_STARTED}. + */ protected final Format[] getStreamFormats() { - return streamFormats; + return Assertions.checkNotNull(streamFormats); } /** * Returns the configuration set when the renderer was most recently enabled. + * + *

This method may be called when the renderer is in the following states: {@link + * #STATE_ENABLED}, {@link #STATE_STARTED}. */ protected final RendererConfiguration getConfiguration() { - return configuration; + return Assertions.checkNotNull(configuration); } /** @@ -352,6 +360,9 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { * {@link C#RESULT_BUFFER_READ} is only returned if {@link #setCurrentStreamFinal()} has been * called. {@link C#RESULT_NOTHING_READ} is returned otherwise. * + *

This method may be called when the renderer is in the following states: {@link + * #STATE_ENABLED}, {@link #STATE_STARTED}. + * * @param formatHolder A {@link FormatHolder} to populate in the case of reading a format. * @param buffer A {@link DecoderInputBuffer} to populate in the case of reading a sample or the * end of the stream. If the end of the stream has been reached, the {@link @@ -364,7 +375,8 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { @SampleStream.ReadDataResult protected final int readSource( FormatHolder formatHolder, DecoderInputBuffer buffer, boolean formatRequired) { - @SampleStream.ReadDataResult int result = stream.readData(formatHolder, buffer, formatRequired); + @SampleStream.ReadDataResult + int result = Assertions.checkNotNull(stream).readData(formatHolder, buffer, formatRequired); if (result == C.RESULT_BUFFER_READ) { if (buffer.isEndOfStream()) { readingPositionUs = C.TIME_END_OF_SOURCE; @@ -373,7 +385,7 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { buffer.timeUs += streamOffsetUs; readingPositionUs = Math.max(readingPositionUs, buffer.timeUs); } else if (result == C.RESULT_FORMAT_READ) { - Format format = formatHolder.format; + Format format = Assertions.checkNotNull(formatHolder.format); if (format.subsampleOffsetUs != Format.OFFSET_SAMPLE_RELATIVE) { format = format @@ -390,17 +402,23 @@ public abstract class BaseRenderer implements Renderer, RendererCapabilities { * Attempts to skip to the keyframe before the specified position, or to the end of the stream if * {@code positionUs} is beyond it. * + *

This method may be called when the renderer is in the following states: {@link + * #STATE_ENABLED}, {@link #STATE_STARTED}. + * * @param positionUs The position in microseconds. * @return The number of samples that were skipped. */ protected int skipSource(long positionUs) { - return stream.skipData(positionUs - streamOffsetUs); + return Assertions.checkNotNull(stream).skipData(positionUs - streamOffsetUs); } /** * Returns whether the upstream source is ready. + * + *

This method may be called when the renderer is in the following states: {@link + * #STATE_ENABLED}, {@link #STATE_STARTED}. */ protected final boolean isSourceReady() { - return hasReadStreamToEnd() ? streamIsFinal : stream.isReady(); + return hasReadStreamToEnd() ? streamIsFinal : Assertions.checkNotNull(stream).isReady(); } }