Search in sources :

Example 1 with CompositeSequenceableLoader

use of com.google.android.exoplayer2.source.CompositeSequenceableLoader in project ExoPlayer by google.

the class SsMediaPeriod method selectTracks.

@Override
public long selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags, SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    ArrayList<ChunkSampleStream<SsChunkSource>> sampleStreamsList = new ArrayList<>();
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] != null) {
            @SuppressWarnings("unchecked") ChunkSampleStream<SsChunkSource> stream = (ChunkSampleStream<SsChunkSource>) streams[i];
            if (selections[i] == null || !mayRetainStreamFlags[i]) {
                stream.release();
                streams[i] = null;
            } else {
                sampleStreamsList.add(stream);
            }
        }
        if (streams[i] == null && selections[i] != null) {
            ChunkSampleStream<SsChunkSource> stream = buildSampleStream(selections[i], positionUs);
            sampleStreamsList.add(stream);
            streams[i] = stream;
            streamResetFlags[i] = true;
        }
    }
    sampleStreams = newSampleStreamArray(sampleStreamsList.size());
    sampleStreamsList.toArray(sampleStreams);
    sequenceableLoader = new CompositeSequenceableLoader(sampleStreams);
    return positionUs;
}
Also used : ChunkSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream) ArrayList(java.util.ArrayList) CompositeSequenceableLoader(com.google.android.exoplayer2.source.CompositeSequenceableLoader)

Example 2 with CompositeSequenceableLoader

use of com.google.android.exoplayer2.source.CompositeSequenceableLoader in project ExoPlayer by google.

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(com.google.android.exoplayer2.source.CompositeSequenceableLoader) SequenceableLoader(com.google.android.exoplayer2.source.SequenceableLoader) CompositeSequenceableLoader(com.google.android.exoplayer2.source.CompositeSequenceableLoader)

Example 3 with CompositeSequenceableLoader

use of com.google.android.exoplayer2.source.CompositeSequenceableLoader in project ExoPlayer by google.

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(com.google.android.exoplayer2.source.chunk.ChunkSampleStream) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) CompositeSequenceableLoader(com.google.android.exoplayer2.source.CompositeSequenceableLoader)

Example 4 with CompositeSequenceableLoader

use of com.google.android.exoplayer2.source.CompositeSequenceableLoader in project ExoPlayer by google.

the class DashMediaPeriod method selectTracks.

@Override
public long selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags, SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    int adaptationSetCount = adaptationSets.size();
    HashMap<Integer, ChunkSampleStream<DashChunkSource>> primarySampleStreams = new HashMap<>();
    // First pass for primary tracks.
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] instanceof ChunkSampleStream) {
            @SuppressWarnings("unchecked") ChunkSampleStream<DashChunkSource> stream = (ChunkSampleStream<DashChunkSource>) streams[i];
            if (selections[i] == null || !mayRetainStreamFlags[i]) {
                stream.release();
                streams[i] = null;
            } else {
                int adaptationSetIndex = trackGroups.indexOf(selections[i].getTrackGroup());
                primarySampleStreams.put(adaptationSetIndex, stream);
            }
        }
        if (streams[i] == null && selections[i] != null) {
            int trackGroupIndex = trackGroups.indexOf(selections[i].getTrackGroup());
            if (trackGroupIndex < adaptationSetCount) {
                ChunkSampleStream<DashChunkSource> stream = buildSampleStream(trackGroupIndex, selections[i], positionUs);
                primarySampleStreams.put(trackGroupIndex, stream);
                streams[i] = stream;
                streamResetFlags[i] = true;
            }
        }
    }
    // Second pass for embedded tracks.
    for (int i = 0; i < selections.length; i++) {
        if ((streams[i] instanceof EmbeddedSampleStream || streams[i] instanceof EmptySampleStream) && (selections[i] == null || !mayRetainStreamFlags[i])) {
            // The stream is for an embedded track and is either no longer selected or needs replacing.
            releaseIfEmbeddedSampleStream(streams[i]);
            streams[i] = null;
        }
        // may have been replaced, selected or deselected.
        if (selections[i] != null) {
            int trackGroupIndex = trackGroups.indexOf(selections[i].getTrackGroup());
            if (trackGroupIndex >= adaptationSetCount) {
                int embeddedTrackIndex = trackGroupIndex - adaptationSetCount;
                EmbeddedTrackInfo embeddedTrackInfo = embeddedTrackInfos[embeddedTrackIndex];
                int adaptationSetIndex = embeddedTrackInfo.adaptationSetIndex;
                ChunkSampleStream<?> primaryStream = primarySampleStreams.get(adaptationSetIndex);
                SampleStream stream = streams[i];
                boolean mayRetainStream = primaryStream == null ? stream instanceof EmptySampleStream : (stream instanceof EmbeddedSampleStream && ((EmbeddedSampleStream) stream).parent == primaryStream);
                if (!mayRetainStream) {
                    releaseIfEmbeddedSampleStream(stream);
                    streams[i] = primaryStream == null ? new EmptySampleStream() : primaryStream.selectEmbeddedTrack(positionUs, embeddedTrackInfo.trackType);
                    streamResetFlags[i] = true;
                }
            }
        }
    }
    sampleStreams = newSampleStreamArray(primarySampleStreams.size());
    primarySampleStreams.values().toArray(sampleStreams);
    sequenceableLoader = new CompositeSequenceableLoader(sampleStreams);
    return positionUs;
}
Also used : EmptySampleStream(com.google.android.exoplayer2.source.EmptySampleStream) ChunkSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream) HashMap(java.util.HashMap) EmbeddedSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream.EmbeddedSampleStream) ChunkSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream) EmptySampleStream(com.google.android.exoplayer2.source.EmptySampleStream) SampleStream(com.google.android.exoplayer2.source.SampleStream) CompositeSequenceableLoader(com.google.android.exoplayer2.source.CompositeSequenceableLoader) EmbeddedSampleStream(com.google.android.exoplayer2.source.chunk.ChunkSampleStream.EmbeddedSampleStream)

Example 5 with CompositeSequenceableLoader

use of com.google.android.exoplayer2.source.CompositeSequenceableLoader in project ExoPlayer by google.

the class HlsMediaPeriod method selectTracks.

@Override
public long selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags, 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 selectedNewTracks = false;
    streamWrapperIndices.clear();
    // Select tracks for each child, copying the resulting streams back into a new streams array.
    SampleStream[] newStreams = new SampleStream[selections.length];
    SampleStream[] childStreams = new SampleStream[selections.length];
    TrackSelection[] childSelections = new TrackSelection[selections.length];
    ArrayList<HlsSampleStreamWrapper> enabledSampleStreamWrapperList = new ArrayList<>(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;
        }
        selectedNewTracks |= sampleStreamWrappers[i].selectTracks(childSelections, mayRetainStreamFlags, childStreams, streamResetFlags, !seenFirstTrackSelection);
        boolean wrapperEnabled = false;
        for (int j = 0; j < selections.length; j++) {
            if (selectionChildIndices[j] == i) {
                // Assert that the child provided a stream for the selection.
                Assertions.checkState(childStreams[j] != null);
                newStreams[j] = childStreams[j];
                wrapperEnabled = true;
                streamWrapperIndices.put(childStreams[j], i);
            } else if (streamChildIndices[j] == i) {
                // Assert that the child cleared any previous stream.
                Assertions.checkState(childStreams[j] == null);
            }
        }
        if (wrapperEnabled) {
            enabledSampleStreamWrapperList.add(sampleStreamWrappers[i]);
        }
    }
    // Copy the new streams back into the streams array.
    System.arraycopy(newStreams, 0, streams, 0, newStreams.length);
    // Update the local state.
    enabledSampleStreamWrappers = new HlsSampleStreamWrapper[enabledSampleStreamWrapperList.size()];
    enabledSampleStreamWrapperList.toArray(enabledSampleStreamWrappers);
    // initialization.
    if (enabledSampleStreamWrappers.length > 0) {
        enabledSampleStreamWrappers[0].setIsTimestampMaster(true);
        for (int i = 1; i < enabledSampleStreamWrappers.length; i++) {
            enabledSampleStreamWrappers[i].setIsTimestampMaster(false);
        }
    }
    sequenceableLoader = new CompositeSequenceableLoader(enabledSampleStreamWrappers);
    if (seenFirstTrackSelection && selectedNewTracks) {
        seekToUs(positionUs);
        // We'll need to reset renderers consuming from all streams due to the seek.
        for (int i = 0; i < selections.length; i++) {
            if (streams[i] != null) {
                streamResetFlags[i] = true;
            }
        }
    }
    seenFirstTrackSelection = true;
    return positionUs;
}
Also used : TrackGroup(com.google.android.exoplayer2.source.TrackGroup) ArrayList(java.util.ArrayList) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) SampleStream(com.google.android.exoplayer2.source.SampleStream) CompositeSequenceableLoader(com.google.android.exoplayer2.source.CompositeSequenceableLoader)

Aggregations

CompositeSequenceableLoader (com.google.android.exoplayer2.source.CompositeSequenceableLoader)5 SampleStream (com.google.android.exoplayer2.source.SampleStream)4 ChunkSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream)4 ArrayList (java.util.ArrayList)4 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)3 ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)3 EmptySampleStream (com.google.android.exoplayer2.source.EmptySampleStream)2 EmbeddedSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream.EmbeddedSampleStream)2 NullableType (org.checkerframework.checker.nullness.compatqual.NullableType)2 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)1 SequenceableLoader (com.google.android.exoplayer2.source.SequenceableLoader)1 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)1 DefaultLoadErrorHandlingPolicy (com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy)1 HashMap (java.util.HashMap)1