Search in sources :

Example 1 with CompositeSequenceableLoader

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

the class DashMediaPeriod method selectTracks.

@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    int[] streamIndexToTrackGroupIndex = getStreamIndexToTrackGroupIndex(selections);
    releaseDisabledStreams(selections, mayRetainStreamFlags, streams);
    releaseOrphanEmbeddedStreams(selections, streams, streamIndexToTrackGroupIndex);
    selectNewStreams(selections, streams, streamResetFlags, positionUs, streamIndexToTrackGroupIndex);
    ArrayList<ChunkSampleStream<DashChunkSource>> sampleStreamList = new ArrayList<>();
    ArrayList<EventSampleStream> eventSampleStreamList = new ArrayList<>();
    for (SampleStream sampleStream : streams) {
        if (sampleStream instanceof ChunkSampleStream) {
            @SuppressWarnings("unchecked") ChunkSampleStream<DashChunkSource> stream = (ChunkSampleStream<DashChunkSource>) sampleStream;
            sampleStreamList.add(stream);
        } else if (sampleStream instanceof EventSampleStream) {
            eventSampleStreamList.add((EventSampleStream) sampleStream);
        }
    }
    sampleStreams = newSampleStreamArray(sampleStreamList.size());
    sampleStreamList.toArray(sampleStreams);
    eventSampleStreams = new EventSampleStream[eventSampleStreamList.size()];
    eventSampleStreamList.toArray(eventSampleStreams);
    compositeSequenceableLoader = compositeSequenceableLoaderFactory.createCompositeSequenceableLoader(sampleStreams);
    return positionUs;
}
Also used : ChunkSampleStream(androidx.media3.exoplayer.source.chunk.ChunkSampleStream) ArrayList(java.util.ArrayList) ChunkSampleStream(androidx.media3.exoplayer.source.chunk.ChunkSampleStream) EmbeddedSampleStream(androidx.media3.exoplayer.source.chunk.ChunkSampleStream.EmbeddedSampleStream) EmptySampleStream(androidx.media3.exoplayer.source.EmptySampleStream) SampleStream(androidx.media3.exoplayer.source.SampleStream)

Example 2 with CompositeSequenceableLoader

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

the class FakeAdaptiveMediaPeriod method selectTracks.

// Casting sample streams created by this class.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    assertThat(prepared).isTrue();
    int rendererCount = selections.length;
    for (int i = 0; i < rendererCount; i++) {
        if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            ((ChunkSampleStream<FakeChunkSource>) streams[i]).release();
            sampleStreams.remove(streams[i]);
            streams[i] = null;
        }
        if (streams[i] == null && selections[i] != null) {
            ExoTrackSelection selection = selections[i];
            assertThat(selection.length()).isAtLeast(1);
            TrackGroup trackGroup = selection.getTrackGroup();
            assertThat(trackGroupArray.indexOf(trackGroup)).isNotEqualTo(C.INDEX_UNSET);
            int indexInTrackGroup = selection.getIndexInTrackGroup(selection.getSelectedIndex());
            assertThat(indexInTrackGroup).isAtLeast(0);
            assertThat(indexInTrackGroup).isLessThan(trackGroup.length);
            FakeChunkSource chunkSource = chunkSourceFactory.createChunkSource(selection, durationUs, transferListener);
            ChunkSampleStream<FakeChunkSource> sampleStream = new ChunkSampleStream<>(MimeTypes.getTrackType(selection.getSelectedFormat().sampleMimeType), /* embeddedTrackTypes= */
            null, /* embeddedTrackFormats= */
            null, chunkSource, /* callback= */
            this, allocator, positionUs, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), new DefaultLoadErrorHandlingPolicy(/* minimumLoadableRetryCount= */
            3), mediaSourceEventDispatcher);
            streams[i] = sampleStream;
            sampleStreams.add(sampleStream);
            streamResetFlags[i] = true;
        }
    }
    sequenceableLoader = new CompositeSequenceableLoader(sampleStreams.toArray(new ChunkSampleStream[0]));
    return seekToUs(positionUs);
}
Also used : ChunkSampleStream(androidx.media3.exoplayer.source.chunk.ChunkSampleStream) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) DefaultLoadErrorHandlingPolicy(androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy) TrackGroup(androidx.media3.common.TrackGroup) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) CompositeSequenceableLoader(androidx.media3.exoplayer.source.CompositeSequenceableLoader)

Example 3 with CompositeSequenceableLoader

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

the class FakeAdaptiveMediaPeriod method release.

/**
 * Releases the media period.
 */
public void release() {
    prepared = false;
    for (ChunkSampleStream<FakeChunkSource> sampleStream : sampleStreams) {
        sampleStream.release();
    }
    sampleStreams.clear();
    sequenceableLoader = new CompositeSequenceableLoader(new SequenceableLoader[0]);
}
Also used : CompositeSequenceableLoader(androidx.media3.exoplayer.source.CompositeSequenceableLoader) SequenceableLoader(androidx.media3.exoplayer.source.SequenceableLoader) CompositeSequenceableLoader(androidx.media3.exoplayer.source.CompositeSequenceableLoader)

Example 4 with CompositeSequenceableLoader

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

the class HlsMediaPeriod method selectTracks.

@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    // Map each selection and stream onto a child period index.
    int[] streamChildIndices = new int[selections.length];
    int[] selectionChildIndices = new int[selections.length];
    for (int i = 0; i < selections.length; i++) {
        streamChildIndices[i] = streams[i] == null ? C.INDEX_UNSET : streamWrapperIndices.get(streams[i]);
        selectionChildIndices[i] = C.INDEX_UNSET;
        if (selections[i] != null) {
            TrackGroup trackGroup = selections[i].getTrackGroup();
            for (int j = 0; j < sampleStreamWrappers.length; j++) {
                if (sampleStreamWrappers[j].getTrackGroups().indexOf(trackGroup) != C.INDEX_UNSET) {
                    selectionChildIndices[i] = j;
                    break;
                }
            }
        }
    }
    boolean forceReset = false;
    streamWrapperIndices.clear();
    // Select tracks for each child, copying the resulting streams back into a new streams array.
    SampleStream[] newStreams = new SampleStream[selections.length];
    @NullableType SampleStream[] childStreams = new SampleStream[selections.length];
    @NullableType ExoTrackSelection[] childSelections = new ExoTrackSelection[selections.length];
    int newEnabledSampleStreamWrapperCount = 0;
    HlsSampleStreamWrapper[] newEnabledSampleStreamWrappers = new HlsSampleStreamWrapper[sampleStreamWrappers.length];
    for (int i = 0; i < sampleStreamWrappers.length; i++) {
        for (int j = 0; j < selections.length; j++) {
            childStreams[j] = streamChildIndices[j] == i ? streams[j] : null;
            childSelections[j] = selectionChildIndices[j] == i ? selections[j] : null;
        }
        HlsSampleStreamWrapper sampleStreamWrapper = sampleStreamWrappers[i];
        boolean wasReset = sampleStreamWrapper.selectTracks(childSelections, mayRetainStreamFlags, childStreams, streamResetFlags, positionUs, forceReset);
        boolean wrapperEnabled = false;
        for (int j = 0; j < selections.length; j++) {
            SampleStream childStream = childStreams[j];
            if (selectionChildIndices[j] == i) {
                // Assert that the child provided a stream for the selection.
                Assertions.checkNotNull(childStream);
                newStreams[j] = childStream;
                wrapperEnabled = true;
                streamWrapperIndices.put(childStream, i);
            } else if (streamChildIndices[j] == i) {
                // Assert that the child cleared any previous stream.
                Assertions.checkState(childStream == null);
            }
        }
        if (wrapperEnabled) {
            newEnabledSampleStreamWrappers[newEnabledSampleStreamWrapperCount] = sampleStreamWrapper;
            if (newEnabledSampleStreamWrapperCount++ == 0) {
                // The first enabled wrapper is always allowed to initialize timestamp adjusters. Note
                // that the first wrapper will correspond to a variant, or else an audio rendition, or
                // else a text rendition, in that order.
                sampleStreamWrapper.setIsTimestampMaster(true);
                if (wasReset || enabledSampleStreamWrappers.length == 0 || sampleStreamWrapper != enabledSampleStreamWrappers[0]) {
                    // The wrapper responsible for initializing the timestamp adjusters was reset or
                    // changed. We need to reset the timestamp adjuster provider and all other wrappers.
                    timestampAdjusterProvider.reset();
                    forceReset = true;
                }
            } else {
                // Additional wrappers are also allowed to initialize timestamp adjusters if they contain
                // audio or video, since they are expected to contain dense samples. Text wrappers are not
                // permitted except in the case above in which no variant or audio rendition wrappers are
                // enabled.
                sampleStreamWrapper.setIsTimestampMaster(i < audioVideoSampleStreamWrapperCount);
            }
        }
    }
    // Copy the new streams back into the streams array.
    System.arraycopy(newStreams, 0, streams, 0, newStreams.length);
    // Update the local state.
    enabledSampleStreamWrappers = Util.nullSafeArrayCopy(newEnabledSampleStreamWrappers, newEnabledSampleStreamWrapperCount);
    compositeSequenceableLoader = compositeSequenceableLoaderFactory.createCompositeSequenceableLoader(enabledSampleStreamWrappers);
    return positionUs;
}
Also used : ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) TrackGroup(androidx.media3.common.TrackGroup) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType) SampleStream(androidx.media3.exoplayer.source.SampleStream)

Example 5 with CompositeSequenceableLoader

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

the class MergingMediaPeriod method selectTracks.

@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    // Map each selection and stream onto a child period index.
    int[] streamChildIndices = new int[selections.length];
    int[] selectionChildIndices = new int[selections.length];
    for (int i = 0; i < selections.length; i++) {
        Integer streamChildIndex = streams[i] == null ? null : streamPeriodIndices.get(streams[i]);
        streamChildIndices[i] = streamChildIndex == null ? C.INDEX_UNSET : streamChildIndex;
        selectionChildIndices[i] = C.INDEX_UNSET;
        if (selections[i] != null) {
            TrackGroup mergedTrackGroup = selections[i].getTrackGroup();
            TrackGroup childTrackGroup = checkNotNull(childTrackGroupByMergedTrackGroup.get(mergedTrackGroup));
            for (int j = 0; j < periods.length; j++) {
                if (periods[j].getTrackGroups().indexOf(childTrackGroup) != C.INDEX_UNSET) {
                    selectionChildIndices[i] = j;
                    break;
                }
            }
        }
    }
    streamPeriodIndices.clear();
    // Select tracks for each child, copying the resulting streams back into a new streams array.
    @NullableType SampleStream[] newStreams = new SampleStream[selections.length];
    @NullableType SampleStream[] childStreams = new SampleStream[selections.length];
    @NullableType ExoTrackSelection[] childSelections = new ExoTrackSelection[selections.length];
    ArrayList<MediaPeriod> enabledPeriodsList = new ArrayList<>(periods.length);
    for (int i = 0; i < periods.length; i++) {
        for (int j = 0; j < selections.length; j++) {
            childStreams[j] = streamChildIndices[j] == i ? streams[j] : null;
            if (selectionChildIndices[j] == i) {
                ExoTrackSelection mergedTrackSelection = checkNotNull(selections[j]);
                TrackGroup mergedTrackGroup = mergedTrackSelection.getTrackGroup();
                TrackGroup childTrackGroup = checkNotNull(childTrackGroupByMergedTrackGroup.get(mergedTrackGroup));
                childSelections[j] = new ForwardingTrackSelection(mergedTrackSelection, childTrackGroup);
            } else {
                childSelections[j] = null;
            }
        }
        long selectPositionUs = periods[i].selectTracks(childSelections, mayRetainStreamFlags, childStreams, streamResetFlags, positionUs);
        if (i == 0) {
            positionUs = selectPositionUs;
        } else if (selectPositionUs != positionUs) {
            throw new IllegalStateException("Children enabled at different positions.");
        }
        boolean periodEnabled = false;
        for (int j = 0; j < selections.length; j++) {
            if (selectionChildIndices[j] == i) {
                // Assert that the child provided a stream for the selection.
                SampleStream childStream = Assertions.checkNotNull(childStreams[j]);
                newStreams[j] = childStreams[j];
                periodEnabled = true;
                streamPeriodIndices.put(childStream, i);
            } else if (streamChildIndices[j] == i) {
                // Assert that the child cleared any previous stream.
                Assertions.checkState(childStreams[j] == null);
            }
        }
        if (periodEnabled) {
            enabledPeriodsList.add(periods[i]);
        }
    }
    // Copy the new streams back into the streams array.
    System.arraycopy(newStreams, 0, streams, 0, newStreams.length);
    // Update the local state.
    enabledPeriods = enabledPeriodsList.toArray(new MediaPeriod[0]);
    compositeSequenceableLoader = compositeSequenceableLoaderFactory.createCompositeSequenceableLoader(enabledPeriods);
    return positionUs;
}
Also used : ArrayList(java.util.ArrayList) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType) ExoTrackSelection(androidx.media3.exoplayer.trackselection.ExoTrackSelection) TrackGroup(androidx.media3.common.TrackGroup)

Aggregations

TrackGroup (androidx.media3.common.TrackGroup)3 ExoTrackSelection (androidx.media3.exoplayer.trackselection.ExoTrackSelection)3 CompositeSequenceableLoader (androidx.media3.exoplayer.source.CompositeSequenceableLoader)2 SampleStream (androidx.media3.exoplayer.source.SampleStream)2 ChunkSampleStream (androidx.media3.exoplayer.source.chunk.ChunkSampleStream)2 ArrayList (java.util.ArrayList)2 NullableType (org.checkerframework.checker.nullness.compatqual.NullableType)2 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)1 EmptySampleStream (androidx.media3.exoplayer.source.EmptySampleStream)1 SequenceableLoader (androidx.media3.exoplayer.source.SequenceableLoader)1 EmbeddedSampleStream (androidx.media3.exoplayer.source.chunk.ChunkSampleStream.EmbeddedSampleStream)1 DefaultLoadErrorHandlingPolicy (androidx.media3.exoplayer.upstream.DefaultLoadErrorHandlingPolicy)1