Search in sources :

Example 1 with SampleQueue

use of androidx.media3.exoplayer.source.SampleQueue in project media by androidx.

the class ChunkSampleStream method seekToUs.

/**
 * Seeks to the specified position in microseconds.
 *
 * @param positionUs The seek position in microseconds.
 */
public void seekToUs(long positionUs) {
    lastSeekPositionUs = positionUs;
    if (isPendingReset()) {
        // A reset is already pending. We only need to update its position.
        pendingResetPositionUs = positionUs;
        return;
    }
    // Detect whether the seek is to the start of a chunk that's at least partially buffered.
    @Nullable BaseMediaChunk seekToMediaChunk = null;
    for (int i = 0; i < mediaChunks.size(); i++) {
        BaseMediaChunk mediaChunk = mediaChunks.get(i);
        long mediaChunkStartTimeUs = mediaChunk.startTimeUs;
        if (mediaChunkStartTimeUs == positionUs && mediaChunk.clippedStartTimeUs == C.TIME_UNSET) {
            seekToMediaChunk = mediaChunk;
            break;
        } else if (mediaChunkStartTimeUs > positionUs) {
            // We're not going to find a chunk with a matching start time.
            break;
        }
    }
    // See if we can seek inside the primary sample queue.
    boolean seekInsideBuffer;
    if (seekToMediaChunk != null) {
        // When seeking to the start of a chunk we use the index of the first sample in the chunk
        // rather than the seek position. This ensures we seek to the keyframe at the start of the
        // chunk even if its timestamp is slightly earlier than the advertised chunk start time.
        seekInsideBuffer = primarySampleQueue.seekTo(seekToMediaChunk.getFirstSampleIndex(0));
    } else {
        seekInsideBuffer = primarySampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
        positionUs < getNextLoadPositionUs());
    }
    if (seekInsideBuffer) {
        // We can seek inside the buffer.
        nextNotifyPrimaryFormatMediaChunkIndex = primarySampleIndexToMediaChunkIndex(primarySampleQueue.getReadIndex(), /* minChunkIndex= */
        0);
        // Seek the embedded sample queues.
        for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
            embeddedSampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
            true);
        }
    } else {
        // We can't seek inside the buffer, and so need to reset.
        pendingResetPositionUs = positionUs;
        loadingFinished = false;
        mediaChunks.clear();
        nextNotifyPrimaryFormatMediaChunkIndex = 0;
        if (loader.isLoading()) {
            // Discard as much as we can synchronously.
            primarySampleQueue.discardToEnd();
            for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
                embeddedSampleQueue.discardToEnd();
            }
            loader.cancelLoading();
        } else {
            loader.clearFatalError();
            resetSampleQueues();
        }
    }
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue) Nullable(androidx.annotation.Nullable)

Example 2 with SampleQueue

use of androidx.media3.exoplayer.source.SampleQueue in project media by androidx.

the class HlsSampleStreamWrapper method release.

public void release() {
    if (prepared) {
        // sampleQueues may still be being modified by the loading thread.
        for (SampleQueue sampleQueue : sampleQueues) {
            sampleQueue.preRelease();
        }
    }
    loader.release(this);
    handler.removeCallbacksAndMessages(null);
    released = true;
    hlsSampleStreams.clear();
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue)

Example 3 with SampleQueue

use of androidx.media3.exoplayer.source.SampleQueue in project media by androidx.

the class HlsSampleStreamWrapper method selectTracks.

/**
 * Called by the parent {@link HlsMediaPeriod} when a track selection occurs.
 *
 * @param selections The renderer track selections.
 * @param mayRetainStreamFlags Flags indicating whether the existing sample stream can be retained
 *     for each selection. A {@code true} value indicates that the selection is unchanged, and
 *     that the caller does not require that the sample stream be recreated.
 * @param streams The existing sample streams, which will be updated to reflect the provided
 *     selections.
 * @param streamResetFlags Will be updated to indicate new sample streams, and sample streams that
 *     have been retained but with the requirement that the consuming renderer be reset.
 * @param positionUs The current playback position in microseconds.
 * @param forceReset If true then a reset is forced (i.e. a seek will be performed with in-buffer
 *     seeking disabled).
 * @return Whether this wrapper requires the parent {@link HlsMediaPeriod} to perform a seek as
 *     part of the track selection.
 */
public boolean selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs, boolean forceReset) {
    assertIsPrepared();
    int oldEnabledTrackGroupCount = enabledTrackGroupCount;
    // Deselect old tracks.
    for (int i = 0; i < selections.length; i++) {
        HlsSampleStream stream = (HlsSampleStream) streams[i];
        if (stream != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            enabledTrackGroupCount--;
            stream.unbindSampleQueue();
            streams[i] = null;
        }
    }
    // We'll always need to seek if we're being forced to reset, or if this is a first selection to
    // a position other than the one we started preparing with, or if we're making a selection
    // having previously disabled all tracks.
    boolean seekRequired = forceReset || (seenFirstTrackSelection ? oldEnabledTrackGroupCount == 0 : positionUs != lastSeekPositionUs);
    // Get the old (i.e. current before the loop below executes) primary track selection. The new
    // primary selection will equal the old one unless it's changed in the loop.
    ExoTrackSelection oldPrimaryTrackSelection = chunkSource.getTrackSelection();
    ExoTrackSelection primaryTrackSelection = oldPrimaryTrackSelection;
    // Select new tracks.
    for (int i = 0; i < selections.length; i++) {
        ExoTrackSelection selection = selections[i];
        if (selection == null) {
            continue;
        }
        int trackGroupIndex = trackGroups.indexOf(selection.getTrackGroup());
        if (trackGroupIndex == primaryTrackGroupIndex) {
            primaryTrackSelection = selection;
            chunkSource.setTrackSelection(selection);
        }
        if (streams[i] == null) {
            enabledTrackGroupCount++;
            streams[i] = new HlsSampleStream(this, trackGroupIndex);
            streamResetFlags[i] = true;
            if (trackGroupToSampleQueueIndex != null) {
                ((HlsSampleStream) streams[i]).bindSampleQueue();
                // If there's still a chance of avoiding a seek, try and seek within the sample queue.
                if (!seekRequired) {
                    SampleQueue sampleQueue = sampleQueues[trackGroupToSampleQueueIndex[trackGroupIndex]];
                    // A seek can be avoided if we're able to seek to the current playback position in
                    // the sample queue, or if we haven't read anything from the queue since the previous
                    // seek (this case is common for sparse tracks such as metadata tracks). In all other
                    // cases a seek is required.
                    seekRequired = !sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
                    true) && sampleQueue.getReadIndex() != 0;
                }
            }
        }
    }
    if (enabledTrackGroupCount == 0) {
        chunkSource.reset();
        downstreamTrackFormat = null;
        pendingResetUpstreamFormats = true;
        mediaChunks.clear();
        if (loader.isLoading()) {
            if (sampleQueuesBuilt) {
                // Discard as much as we can synchronously.
                for (SampleQueue sampleQueue : sampleQueues) {
                    sampleQueue.discardToEnd();
                }
            }
            loader.cancelLoading();
        } else {
            resetSampleQueues();
        }
    } else {
        if (!mediaChunks.isEmpty() && !Util.areEqual(primaryTrackSelection, oldPrimaryTrackSelection)) {
            // The primary track selection has changed and we have buffered media. The buffered media
            // may need to be discarded.
            boolean primarySampleQueueDirty = false;
            if (!seenFirstTrackSelection) {
                long bufferedDurationUs = positionUs < 0 ? -positionUs : 0;
                HlsMediaChunk lastMediaChunk = getLastMediaChunk();
                MediaChunkIterator[] mediaChunkIterators = chunkSource.createMediaChunkIterators(lastMediaChunk, positionUs);
                primaryTrackSelection.updateSelectedTrack(positionUs, bufferedDurationUs, C.TIME_UNSET, readOnlyMediaChunks, mediaChunkIterators);
                int chunkIndex = chunkSource.getTrackGroup().indexOf(lastMediaChunk.trackFormat);
                if (primaryTrackSelection.getSelectedIndexInTrackGroup() != chunkIndex) {
                    // This is the first selection and the chunk loaded during preparation does not match
                    // the initially selected format.
                    primarySampleQueueDirty = true;
                }
            } else {
                // The primary sample queue contains media buffered for the old primary track selection.
                primarySampleQueueDirty = true;
            }
            if (primarySampleQueueDirty) {
                forceReset = true;
                seekRequired = true;
                pendingResetUpstreamFormats = true;
            }
        }
        if (seekRequired) {
            seekToUs(positionUs, forceReset);
            // We'll need to reset renderers consuming from all streams due to the seek.
            for (int i = 0; i < streams.length; i++) {
                if (streams[i] != null) {
                    streamResetFlags[i] = true;
                }
            }
        }
    }
    updateSampleStreams(streams);
    seenFirstTrackSelection = true;
    return seekRequired;
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue) MediaChunkIterator(androidx.media3.exoplayer.source.chunk.MediaChunkIterator) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection)

Example 4 with SampleQueue

use of androidx.media3.exoplayer.source.SampleQueue in project media by androidx.

the class HlsSampleStreamWrapper method seekToUs.

/**
 * Attempts to seek to the specified position in microseconds.
 *
 * @param positionUs The seek position in microseconds.
 * @param forceReset If true then a reset is forced (i.e. in-buffer seeking is disabled).
 * @return Whether the wrapper was reset, meaning the wrapped sample queues were reset. If false,
 *     an in-buffer seek was performed.
 */
public boolean seekToUs(long positionUs, boolean forceReset) {
    lastSeekPositionUs = positionUs;
    if (isPendingReset()) {
        // A reset is already pending. We only need to update its position.
        pendingResetPositionUs = positionUs;
        return true;
    }
    // If we're not forced to reset, try and seek within the buffer.
    if (sampleQueuesBuilt && !forceReset && seekInsideBufferUs(positionUs)) {
        return false;
    }
    // We can't seek inside the buffer, and so need to reset.
    pendingResetPositionUs = positionUs;
    loadingFinished = false;
    mediaChunks.clear();
    if (loader.isLoading()) {
        if (sampleQueuesBuilt) {
            // Discard as much as we can synchronously.
            for (SampleQueue sampleQueue : sampleQueues) {
                sampleQueue.discardToEnd();
            }
        }
        loader.cancelLoading();
    } else {
        loader.clearFatalError();
        resetSampleQueues();
    }
    return true;
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue)

Example 5 with SampleQueue

use of androidx.media3.exoplayer.source.SampleQueue in project media by androidx.

the class SampleQueueTest method assertReadSample.

/**
 * Asserts {@link SampleQueue#read} returns {@link C#RESULT_BUFFER_READ} and that the buffer is
 * filled with the specified sample data.
 *
 * @param timeUs The expected buffer timestamp.
 * @param isKeyFrame The expected keyframe flag.
 * @param isDecodeOnly The expected decodeOnly flag.
 * @param isEncrypted The expected encrypted flag.
 * @param sampleData An array containing the expected sample data.
 * @param offset The offset in {@code sampleData} of the expected sample data.
 * @param length The length of the expected sample data.
 */
private void assertReadSample(long timeUs, boolean isKeyFrame, boolean isDecodeOnly, boolean isEncrypted, byte[] sampleData, int offset, int length) {
    // Check that peek whilst omitting data yields the expected values.
    formatHolder.format = null;
    DecoderInputBuffer flagsOnlyBuffer = DecoderInputBuffer.newNoDataInstance();
    int result = sampleQueue.read(formatHolder, flagsOnlyBuffer, FLAG_OMIT_SAMPLE_DATA | FLAG_PEEK, /* loadingFinished= */
    false);
    assertSampleBufferReadResult(flagsOnlyBuffer, result, timeUs, isKeyFrame, isDecodeOnly, isEncrypted);
    // Check that peek yields the expected values.
    clearFormatHolderAndInputBuffer();
    result = sampleQueue.read(formatHolder, inputBuffer, FLAG_PEEK, /* loadingFinished= */
    false);
    assertSampleBufferReadResult(result, timeUs, isKeyFrame, isDecodeOnly, isEncrypted, sampleData, offset, length);
    // Check that read yields the expected values.
    clearFormatHolderAndInputBuffer();
    result = sampleQueue.read(formatHolder, inputBuffer, /* readFlags= */
    0, /* loadingFinished= */
    false);
    assertSampleBufferReadResult(result, timeUs, isKeyFrame, isDecodeOnly, isEncrypted, sampleData, offset, length);
}
Also used : DecoderInputBuffer(androidx.media3.decoder.DecoderInputBuffer)

Aggregations

SampleQueue (androidx.media3.exoplayer.source.SampleQueue)14 Nullable (androidx.annotation.Nullable)5 Format (androidx.media3.common.Format)4 TrackGroup (androidx.media3.common.TrackGroup)2 TrackGroupArray (androidx.media3.common.TrackGroupArray)2 ParsableByteArray (androidx.media3.common.util.ParsableByteArray)2 DecoderInputBuffer (androidx.media3.decoder.DecoderInputBuffer)2 LoadEventInfo (androidx.media3.exoplayer.source.LoadEventInfo)2 ExoTrackSelection (androidx.media3.exoplayer.trackselection.ExoTrackSelection)2 ImmutableList (com.google.common.collect.ImmutableList)2 Uri (android.net.Uri)1 Metadata (androidx.media3.common.Metadata)1 DataSpec (androidx.media3.datasource.DataSpec)1 StatsDataSource (androidx.media3.datasource.StatsDataSource)1 FormatHolder (androidx.media3.exoplayer.FormatHolder)1 DrmSession (androidx.media3.exoplayer.drm.DrmSession)1 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)1 Chunk (androidx.media3.exoplayer.source.chunk.Chunk)1 MediaChunkIterator (androidx.media3.exoplayer.source.chunk.MediaChunkIterator)1 DefaultAllocator (androidx.media3.exoplayer.upstream.DefaultAllocator)1