Class SampleQueue

    • Method Detail

      • createWithoutDrm

        public static SampleQueue createWithoutDrm​(Allocator allocator)
        Creates a sample queue without DRM resource management.
        Parameters:
        allocator - An Allocator from which allocations for sample data can be obtained.
      • release

        @CallSuper
        public void release()
        Calls reset(true) and releases any resources owned by the queue.
      • reset

        public final void reset()
        Convenience method for reset(false).
      • reset

        @CallSuper
        public void reset​(boolean resetUpstreamFormat)
        Clears all samples from the queue.
        Parameters:
        resetUpstreamFormat - Whether the upstream format should be cleared. If set to false, samples queued after the reset (and before a subsequent call to format(Format)) are assumed to have the current upstream format. If set to true, format(Format) must be called after the reset before any more samples can be queued.
      • setStartTimeUs

        public final void setStartTimeUs​(long startTimeUs)
        Sets the start time for the queue. Samples with earlier timestamps will be discarded or have the C.BUFFER_FLAG_DECODE_ONLY flag set when read.
        Parameters:
        startTimeUs - The start time, in microseconds.
      • sourceId

        public final void sourceId​(int sourceId)
        Sets a source identifier for subsequent samples.
        Parameters:
        sourceId - The source identifier.
      • splice

        public final void splice()
        Indicates samples that are subsequently queued should be spliced into those already queued.
      • getWriteIndex

        public final int getWriteIndex()
        Returns the current absolute write index.
      • discardUpstreamSamples

        public final void discardUpstreamSamples​(int discardFromIndex)
        Discards samples from the write side of the queue.
        Parameters:
        discardFromIndex - The absolute index of the first sample to be discarded. Must be in the range [getReadIndex(), getWriteIndex()].
      • discardUpstreamFrom

        public final void discardUpstreamFrom​(long timeUs)
        Discards samples from the write side of the queue.
        Parameters:
        timeUs - Samples will be discarded from the write end of the queue until a sample with a timestamp smaller than timeUs is encountered (this sample is not discarded). Must be larger than getLargestReadTimestampUs().
      • preRelease

        @CallSuper
        public void preRelease()
        Calls discardToEnd() and releases any resources owned by the queue.
      • maybeThrowError

        @CallSuper
        public void maybeThrowError()
                             throws IOException
        Throws an error that's preventing data from being read. Does nothing if no such error exists.
        Throws:
        IOException - The underlying error.
      • getFirstIndex

        public final int getFirstIndex()
        Returns the current absolute start index.
      • getReadIndex

        public final int getReadIndex()
        Returns the current absolute read index.
      • peekSourceId

        public final int peekSourceId()
        Peeks the source id of the next sample to be read, or the current upstream source id if the queue is empty or if the read position is at the end of the queue.
        Returns:
        The source id.
      • getUpstreamFormat

        @Nullable
        public final Format getUpstreamFormat()
        Returns the upstream Format in which samples are being queued.
      • getLargestQueuedTimestampUs

        public final long getLargestQueuedTimestampUs()
        Returns the largest sample timestamp that has been queued since the last reset().

        Samples that were discarded by calling discardUpstreamSamples(int) are not considered as having been queued. Samples that were dequeued from the front of the queue are considered as having been queued.

        Returns:
        The largest sample timestamp that has been queued, or Long.MIN_VALUE if no samples have been queued.
      • getLargestReadTimestampUs

        public final long getLargestReadTimestampUs()
        Returns the largest sample timestamp that has been read since the last reset().
        Returns:
        The largest sample timestamp that has been read, or Long.MIN_VALUE if no samples have been read.
      • isLastSampleQueued

        public final boolean isLastSampleQueued()
        Returns whether the last sample of the stream has knowingly been queued. A return value of false means that the last sample had not been queued or that it's unknown whether the last sample has been queued.

        Samples that were discarded by calling discardUpstreamSamples(int) are not considered as having been queued. Samples that were dequeued from the front of the queue are considered as having been queued.

      • getFirstTimestampUs

        public final long getFirstTimestampUs()
        Returns the timestamp of the first sample, or Long.MIN_VALUE if the queue is empty.
      • isReady

        @CallSuper
        public boolean isReady​(boolean loadingFinished)
        Returns whether there is data available for reading.

        Note: If the stream has ended then a buffer with the end of stream flag can always be read from read(com.google.android.exoplayer2.FormatHolder, com.google.android.exoplayer2.decoder.DecoderInputBuffer, int, boolean). Hence an ended stream is always ready.

        Parameters:
        loadingFinished - Whether no more samples will be written to the sample queue. When true, this method returns true if the sample queue is empty, because an empty sample queue means the end of stream has been reached. When false, this method returns false if the sample queue is empty.
      • seekTo

        public final boolean seekTo​(int sampleIndex)
        Attempts to seek the read position to the specified sample index.
        Parameters:
        sampleIndex - The sample index.
        Returns:
        Whether the seek was successful.
      • seekTo

        public final boolean seekTo​(long timeUs,
                                    boolean allowTimeBeyondBuffer)
        Attempts to seek the read position to the keyframe before or at the specified time.
        Parameters:
        timeUs - The time to seek to.
        allowTimeBeyondBuffer - Whether the operation can succeed if timeUs is beyond the end of the queue, by seeking to the last sample (or keyframe).
        Returns:
        Whether the seek was successful.
      • getSkipCount

        public final int getSkipCount​(long timeUs,
                                      boolean allowEndOfQueue)
        Returns the number of samples that need to be skipped to advance the read position to the keyframe before or at the specified time.
        Parameters:
        timeUs - The time to advance to.
        allowEndOfQueue - Whether the end of the queue is considered a keyframe when timeUs is larger than the largest queued timestamp.
        Returns:
        The number of samples that need to be skipped, which may be equal to 0.
      • skip

        public final void skip​(int count)
        Advances the read position by the specified number of samples.
        Parameters:
        count - The number of samples to advance the read position by. Must be at least 0 and at most getWriteIndex() - getReadIndex().
      • discardTo

        public final void discardTo​(long timeUs,
                                    boolean toKeyframe,
                                    boolean stopAtReadPosition)
        Discards up to but not including the sample immediately before or at the specified time.
        Parameters:
        timeUs - The time to discard up to.
        toKeyframe - If true then discards samples up to the keyframe before or at the specified time, rather than any sample before or at that time.
        stopAtReadPosition - If true then samples are only discarded if they're before the read position. If false then samples at and beyond the read position may be discarded, in which case the read position is advanced to the first remaining sample.
      • discardToRead

        public final void discardToRead()
        Discards up to but not including the read position.
      • discardToEnd

        public final void discardToEnd()
        Discards all samples in the queue and advances the read position.
      • setSampleOffsetUs

        public final void setSampleOffsetUs​(long sampleOffsetUs)
        Sets an offset that will be added to the timestamps (and sub-sample timestamps) of samples that are subsequently queued.
        Parameters:
        sampleOffsetUs - The timestamp offset in microseconds.
      • setUpstreamFormatChangeListener

        public final void setUpstreamFormatChangeListener​(@Nullable
                                                          SampleQueue.UpstreamFormatChangedListener listener)
        Sets a listener to be notified of changes to the upstream format.
        Parameters:
        listener - The listener.
      • format

        public final void format​(Format unadjustedUpstreamFormat)
        Description copied from interface: TrackOutput
        Called when the Format of the track has been extracted from the stream.
        Specified by:
        format in interface TrackOutput
        Parameters:
        unadjustedUpstreamFormat - The extracted Format.
      • sampleData

        public final int sampleData​(DataReader input,
                                    int length,
                                    boolean allowEndOfInput,
                                    @SampleDataPart
                                    int sampleDataPart)
                             throws IOException
        Description copied from interface: TrackOutput
        Called to write sample data to the output.
        Specified by:
        sampleData in interface TrackOutput
        Parameters:
        input - A DataReader from which to read the sample data.
        length - The maximum length to read from the input.
        allowEndOfInput - True if encountering the end of the input having read no data is allowed, and should result in C.RESULT_END_OF_INPUT being returned. False if it should be considered an error, causing an EOFException to be thrown.
        sampleDataPart - The part of the sample data to which this call corresponds.
        Returns:
        The number of bytes appended.
        Throws:
        IOException - If an error occurred reading from the input.
      • sampleData

        public final void sampleData​(ParsableByteArray buffer,
                                     int length,
                                     @SampleDataPart
                                     int sampleDataPart)
        Description copied from interface: TrackOutput
        Called to write sample data to the output.
        Specified by:
        sampleData in interface TrackOutput
        Parameters:
        buffer - A ParsableByteArray from which to read the sample data.
        length - The number of bytes to read, starting from data.getPosition().
        sampleDataPart - The part of the sample data to which this call corresponds.
      • invalidateUpstreamFormatAdjustment

        protected final void invalidateUpstreamFormatAdjustment()
        Invalidates the last upstream format adjustment. getAdjustedUpstreamFormat(Format) will be called to adjust the upstream Format again before the next sample is queued.
      • discardSampleMetadataToRead

        public long discardSampleMetadataToRead()