Search in sources :

Example 6 with ExoTrackSelection

use of com.google.android.exoplayer2.trackselection.ExoTrackSelection in project ExoPlayer by google.

the class ProgressiveMediaPeriod method selectTracks.

@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    assertPrepared();
    TrackGroupArray tracks = trackState.tracks;
    boolean[] trackEnabledStates = trackState.trackEnabledStates;
    int oldEnabledTrackCount = enabledTrackCount;
    // Deselect old tracks.
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            int track = ((SampleStreamImpl) streams[i]).track;
            Assertions.checkState(trackEnabledStates[track]);
            enabledTrackCount--;
            trackEnabledStates[track] = false;
            streams[i] = null;
        }
    }
    // We'll always need to seek if this is a first selection to a non-zero position, or if we're
    // making a selection having previously disabled all tracks.
    boolean seekRequired = seenFirstTrackSelection ? oldEnabledTrackCount == 0 : positionUs != 0;
    // Select new tracks.
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] == null && selections[i] != null) {
            ExoTrackSelection selection = selections[i];
            Assertions.checkState(selection.length() == 1);
            Assertions.checkState(selection.getIndexInTrackGroup(0) == 0);
            int track = tracks.indexOf(selection.getTrackGroup());
            Assertions.checkState(!trackEnabledStates[track]);
            enabledTrackCount++;
            trackEnabledStates[track] = true;
            streams[i] = new SampleStreamImpl(track);
            streamResetFlags[i] = true;
            // If there's still a chance of avoiding a seek, try and seek within the sample queue.
            if (!seekRequired) {
                SampleQueue sampleQueue = sampleQueues[track];
                // 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 (enabledTrackCount == 0) {
        pendingDeferredRetry = false;
        notifyDiscontinuity = false;
        if (loader.isLoading()) {
            // Discard as much as we can synchronously.
            for (SampleQueue sampleQueue : sampleQueues) {
                sampleQueue.discardToEnd();
            }
            loader.cancelLoading();
        } else {
            for (SampleQueue sampleQueue : sampleQueues) {
                sampleQueue.reset();
            }
        }
    } else if (seekRequired) {
        positionUs = seekToUs(positionUs);
        // 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;
            }
        }
    }
    seenFirstTrackSelection = true;
    return positionUs;
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection)

Example 7 with ExoTrackSelection

use of com.google.android.exoplayer2.trackselection.ExoTrackSelection in project ExoPlayer by google.

the class DefaultTrackSelector method getLegacyRendererOverride.

// Calling deprecated getSelectionOverride.
@SuppressWarnings("deprecation")
private ExoTrackSelection.@NullableType Definition getLegacyRendererOverride(MappedTrackInfo mappedTrackInfo, Parameters params, int rendererIndex) {
    TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
    @Nullable SelectionOverride override = params.getSelectionOverride(rendererIndex, rendererTrackGroups);
    if (override == null) {
        return null;
    }
    return new ExoTrackSelection.Definition(rendererTrackGroups.get(override.groupIndex), override.tracks, override.type);
}
Also used : TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride) TrackGroupArray(com.google.android.exoplayer2.source.TrackGroupArray) Nullable(androidx.annotation.Nullable)

Example 8 with ExoTrackSelection

use of com.google.android.exoplayer2.trackselection.ExoTrackSelection in project ExoPlayer by google.

the class TrackSelectionUtil method createTrackSelectionsForDefinitions.

/**
 * Creates track selections for an array of track selection definitions, with at most one
 * multi-track adaptive selection.
 *
 * @param definitions The list of track selection {@link Definition definitions}. May include null
 *     values.
 * @param adaptiveTrackSelectionFactory A factory for the multi-track adaptive track selection.
 * @return The array of created track selection. For null entries in {@code definitions} returns
 *     null values.
 */
@NullableType
public static ExoTrackSelection[] createTrackSelectionsForDefinitions(@NullableType Definition[] definitions, AdaptiveTrackSelectionFactory adaptiveTrackSelectionFactory) {
    ExoTrackSelection[] selections = new ExoTrackSelection[definitions.length];
    boolean createdAdaptiveTrackSelection = false;
    for (int i = 0; i < definitions.length; i++) {
        Definition definition = definitions[i];
        if (definition == null) {
            continue;
        }
        if (definition.tracks.length > 1 && !createdAdaptiveTrackSelection) {
            createdAdaptiveTrackSelection = true;
            selections[i] = adaptiveTrackSelectionFactory.createAdaptiveTrackSelection(definition);
        } else {
            selections[i] = new FixedTrackSelection(definition.group, definition.tracks[0], /* type= */
            definition.type);
        }
    }
    return selections;
}
Also used : Definition(com.google.android.exoplayer2.trackselection.ExoTrackSelection.Definition) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType)

Example 9 with ExoTrackSelection

use of com.google.android.exoplayer2.trackselection.ExoTrackSelection in project ExoPlayer by google.

the class MergingMediaPeriodTest method selectTracks_createsSampleStreamsFromChildPeriods.

@Test
public void selectTracks_createsSampleStreamsFromChildPeriods() throws Exception {
    MergingMediaPeriod mergingMediaPeriod = prepareMergingPeriod(new MergingPeriodDefinition(/* timeOffsetUs= */
    0, /* singleSampleTimeUs= */
    0, childFormat11, childFormat12), new MergingPeriodDefinition(/* timeOffsetUs= */
    0, /* singleSampleTimeUs= */
    0, childFormat21, childFormat22));
    ExoTrackSelection selectionForChild1 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(1), /* track= */
    0);
    ExoTrackSelection selectionForChild2 = new FixedTrackSelection(mergingMediaPeriod.getTrackGroups().get(2), /* track= */
    0);
    SampleStream[] streams = new SampleStream[4];
    mergingMediaPeriod.selectTracks(/* selections= */
    new ExoTrackSelection[] { null, selectionForChild1, selectionForChild2, null }, /* mayRetainStreamFlags= */
    new boolean[] { false, false, false, false }, streams, /* streamResetFlags= */
    new boolean[] { false, false, false, false }, /* positionUs= */
    0);
    mergingMediaPeriod.continueLoading(/* positionUs= */
    0);
    assertThat(streams[0]).isNull();
    assertThat(streams[3]).isNull();
    FormatHolder formatHolder = new FormatHolder();
    DecoderInputBuffer inputBuffer = new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
    assertThat(streams[1].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT)).isEqualTo(C.RESULT_FORMAT_READ);
    assertThat(formatHolder.format).isEqualTo(childFormat12);
    assertThat(streams[2].readData(formatHolder, inputBuffer, FLAG_REQUIRE_FORMAT)).isEqualTo(C.RESULT_FORMAT_READ);
    assertThat(formatHolder.format).isEqualTo(childFormat21);
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) DecoderInputBuffer(com.google.android.exoplayer2.decoder.DecoderInputBuffer) FormatHolder(com.google.android.exoplayer2.FormatHolder) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) Test(org.junit.Test)

Example 10 with ExoTrackSelection

use of com.google.android.exoplayer2.trackselection.ExoTrackSelection in project ExoPlayer by google.

the class SsMediaPeriod method getStreamKeys.

@Override
public List<StreamKey> getStreamKeys(List<ExoTrackSelection> trackSelections) {
    List<StreamKey> streamKeys = new ArrayList<>();
    for (int selectionIndex = 0; selectionIndex < trackSelections.size(); selectionIndex++) {
        ExoTrackSelection trackSelection = trackSelections.get(selectionIndex);
        int streamElementIndex = trackGroups.indexOf(trackSelection.getTrackGroup());
        for (int i = 0; i < trackSelection.length(); i++) {
            streamKeys.add(new StreamKey(streamElementIndex, trackSelection.getIndexInTrackGroup(i)));
        }
    }
    return streamKeys;
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) ArrayList(java.util.ArrayList) StreamKey(com.google.android.exoplayer2.offline.StreamKey)

Aggregations

ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)21 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)9 ArrayList (java.util.ArrayList)8 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)6 Format (com.google.android.exoplayer2.Format)5 NullableType (org.checkerframework.checker.nullness.compatqual.NullableType)5 StreamKey (com.google.android.exoplayer2.offline.StreamKey)4 ChunkSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream)4 SuppressLint (android.annotation.SuppressLint)3 Point (android.graphics.Point)3 Nullable (androidx.annotation.Nullable)3 TrackSelectorResult (com.google.android.exoplayer2.trackselection.TrackSelectorResult)3 FormatHolder (com.google.android.exoplayer2.FormatHolder)2 RendererCapabilities (com.google.android.exoplayer2.RendererCapabilities)2 Capabilities (com.google.android.exoplayer2.RendererCapabilities.Capabilities)2 RendererConfiguration (com.google.android.exoplayer2.RendererConfiguration)2 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)2 EmptySampleStream (com.google.android.exoplayer2.source.EmptySampleStream)2 SampleStream (com.google.android.exoplayer2.source.SampleStream)2 FixedTrackSelection (com.google.android.exoplayer2.trackselection.FixedTrackSelection)2