Search in sources :

Example 26 with ExoTrackSelection

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

the class DefaultTrackSelector method selectTracks.

// MappingTrackSelector implementation.
@Override
protected final Pair<@NullableType RendererConfiguration[], @NullableType ExoTrackSelection[]> selectTracks(MappedTrackInfo mappedTrackInfo, @Capabilities int[][][] rendererFormatSupports, @AdaptiveSupport int[] rendererMixedMimeTypeAdaptationSupports, MediaPeriodId mediaPeriodId, Timeline timeline) throws ExoPlaybackException {
    Parameters params = parametersReference.get();
    int rendererCount = mappedTrackInfo.getRendererCount();
    ExoTrackSelection.@NullableType Definition[] definitions = selectAllTracks(mappedTrackInfo, rendererFormatSupports, rendererMixedMimeTypeAdaptationSupports, params);
    // Apply per track type overrides.
    SparseArray<Pair<TrackSelectionOverride, Integer>> applicableOverridesByTrackType = getApplicableOverrides(mappedTrackInfo, params);
    for (int i = 0; i < applicableOverridesByTrackType.size(); i++) {
        Pair<TrackSelectionOverride, Integer> overrideAndRendererIndex = applicableOverridesByTrackType.valueAt(i);
        applyTrackTypeOverride(mappedTrackInfo, definitions, /* trackType= */
        applicableOverridesByTrackType.keyAt(i), /* override= */
        overrideAndRendererIndex.first, /* overrideRendererIndex= */
        overrideAndRendererIndex.second);
    }
    // Apply legacy per renderer overrides.
    for (int i = 0; i < rendererCount; i++) {
        if (hasLegacyRendererOverride(mappedTrackInfo, params, /* rendererIndex= */
        i)) {
            definitions[i] = getLegacyRendererOverride(mappedTrackInfo, params, /* rendererIndex= */
            i);
        }
    }
    // Disable renderers if needed.
    for (int i = 0; i < rendererCount; i++) {
        if (isRendererDisabled(mappedTrackInfo, params, /* rendererIndex= */
        i)) {
            definitions[i] = null;
        }
    }
    @NullableType ExoTrackSelection[] rendererTrackSelections = trackSelectionFactory.createTrackSelections(definitions, getBandwidthMeter(), mediaPeriodId, timeline);
    // Initialize the renderer configurations to the default configuration for all renderers with
    // selections, and null otherwise.
    @NullableType RendererConfiguration[] rendererConfigurations = new RendererConfiguration[rendererCount];
    for (int i = 0; i < rendererCount; i++) {
        @C.TrackType int rendererType = mappedTrackInfo.getRendererType(i);
        boolean forceRendererDisabled = params.getRendererDisabled(i) || params.disabledTrackTypes.contains(rendererType);
        boolean rendererEnabled = !forceRendererDisabled && (mappedTrackInfo.getRendererType(i) == C.TRACK_TYPE_NONE || rendererTrackSelections[i] != null);
        rendererConfigurations[i] = rendererEnabled ? RendererConfiguration.DEFAULT : null;
    }
    // Configure audio and video renderers to use tunneling if appropriate.
    if (params.tunnelingEnabled) {
        maybeConfigureRenderersForTunneling(mappedTrackInfo, rendererFormatSupports, rendererConfigurations, rendererTrackSelections);
    }
    return Pair.create(rendererConfigurations, rendererTrackSelections);
}
Also used : TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) RendererConfiguration(com.google.android.exoplayer2.RendererConfiguration) Pair(android.util.Pair) TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride)

Example 27 with ExoTrackSelection

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

the class DefaultTrackSelector method selectOtherTrack.

// Generic track selection methods.
/**
 * Called by {@link #selectAllTracks(MappedTrackInfo, int[][][], int[], Parameters)} to create a
 * {@link ExoTrackSelection} for a renderer whose type is neither video, audio or text.
 *
 * @param trackType The type of the renderer.
 * @param groups The {@link TrackGroupArray} mapped to the renderer.
 * @param formatSupport The {@link Capabilities} for each mapped track, indexed by track group and
 *     track (in that order).
 * @param params The selector's current constraint parameters.
 * @return The {@link ExoTrackSelection} for the renderer, or null if no selection was made.
 * @throws ExoPlaybackException If an error occurs while selecting the tracks.
 */
@Nullable
protected ExoTrackSelection.Definition selectOtherTrack(int trackType, TrackGroupArray groups, @Capabilities int[][] formatSupport, Parameters params) throws ExoPlaybackException {
    @Nullable TrackGroup selectedGroup = null;
    int selectedTrackIndex = 0;
    @Nullable OtherTrackScore selectedTrackScore = null;
    for (int groupIndex = 0; groupIndex < groups.length; groupIndex++) {
        TrackGroup trackGroup = groups.get(groupIndex);
        @Capabilities int[] trackFormatSupport = formatSupport[groupIndex];
        for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++) {
            if (isSupported(trackFormatSupport[trackIndex], params.exceedRendererCapabilitiesIfNecessary)) {
                Format format = trackGroup.getFormat(trackIndex);
                OtherTrackScore trackScore = new OtherTrackScore(format, trackFormatSupport[trackIndex]);
                if (selectedTrackScore == null || trackScore.compareTo(selectedTrackScore) > 0) {
                    selectedGroup = trackGroup;
                    selectedTrackIndex = trackIndex;
                    selectedTrackScore = trackScore;
                }
            }
        }
    }
    return selectedGroup == null ? null : new ExoTrackSelection.Definition(selectedGroup, selectedTrackIndex);
}
Also used : Format(com.google.android.exoplayer2.Format) TrackGroup(com.google.android.exoplayer2.source.TrackGroup) Capabilities(com.google.android.exoplayer2.RendererCapabilities.Capabilities) RendererCapabilities(com.google.android.exoplayer2.RendererCapabilities) Nullable(androidx.annotation.Nullable) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) Nullable(androidx.annotation.Nullable)

Example 28 with ExoTrackSelection

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

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(com.google.android.exoplayer2.trackselection.ExoTrackSelection)

Example 29 with ExoTrackSelection

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

the class RtspMediaPeriod method selectTracks.

@Override
public long selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    // Input array streams contains the streams selected in the previous track selection.
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            streams[i] = null;
        }
    }
    // Select new tracks.
    selectedLoadInfos.clear();
    for (int i = 0; i < selections.length; i++) {
        TrackSelection selection = selections[i];
        if (selection == null) {
            continue;
        }
        TrackGroup trackGroup = selection.getTrackGroup();
        int trackGroupIndex = checkNotNull(trackGroups).indexOf(trackGroup);
        selectedLoadInfos.add(checkNotNull(rtspLoaderWrappers.get(trackGroupIndex)).loadInfo);
        // Find the sampleStreamWrapper that contains this track group.
        if (trackGroups.contains(trackGroup)) {
            if (streams[i] == null) {
                streams[i] = new SampleStreamImpl(trackGroupIndex);
                // Update flag for newly created SampleStream.
                streamResetFlags[i] = true;
            }
        }
    }
    // Cancel non-selected loadables.
    for (int i = 0; i < rtspLoaderWrappers.size(); i++) {
        RtspLoaderWrapper loadControl = rtspLoaderWrappers.get(i);
        if (!selectedLoadInfos.contains(loadControl.loadInfo)) {
            loadControl.cancelLoad();
        }
    }
    trackSelected = true;
    maybeSetupTracks();
    return positionUs;
}
Also used : TrackGroup(com.google.android.exoplayer2.source.TrackGroup) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection)

Example 30 with ExoTrackSelection

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

the class ExoPlayerWrapper method getSelectedAudioTrack.

@Override
public int getSelectedAudioTrack() {
    TrackSelectionArray trackSelections = exoPlayer.getCurrentTrackSelections();
    List<Format> availableFormats = getFormats();
    for (int i = 0; i < trackSelections.length; i++) {
        ExoTrackSelection track = (ExoTrackSelection) trackSelections.get(i);
        if (track == null) {
            continue;
        }
        if (availableFormats.contains(track.getSelectedFormat())) {
            return availableFormats.indexOf(track.getSelectedFormat());
        }
    }
    return -1;
}
Also used : Format(com.google.android.exoplayer2.Format) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) TrackSelectionArray(com.google.android.exoplayer2.trackselection.TrackSelectionArray)

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