Search in sources :

Example 1 with ExoTrackSelection

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

the class DownloadHelper method runTrackSelection.

/**
 * Runs the track selection for a given period index with the current parameters. The selected
 * tracks will be added to {@link #trackSelectionsByPeriodAndRenderer}.
 */
@RequiresNonNull({ "trackGroupArrays", "trackSelectionsByPeriodAndRenderer", "mediaPreparer", "mediaPreparer.timeline" })
private TrackSelectorResult runTrackSelection(int periodIndex) {
    try {
        TrackSelectorResult trackSelectorResult = trackSelector.selectTracks(rendererCapabilities, trackGroupArrays[periodIndex], new MediaPeriodId(mediaPreparer.timeline.getUidOfPeriod(periodIndex)), mediaPreparer.timeline);
        for (int i = 0; i < trackSelectorResult.length; i++) {
            @Nullable ExoTrackSelection newSelection = trackSelectorResult.selections[i];
            if (newSelection == null) {
                continue;
            }
            List<ExoTrackSelection> existingSelectionList = trackSelectionsByPeriodAndRenderer[periodIndex][i];
            boolean mergedWithExistingSelection = false;
            for (int j = 0; j < existingSelectionList.size(); j++) {
                ExoTrackSelection existingSelection = existingSelectionList.get(j);
                if (existingSelection.getTrackGroup().equals(newSelection.getTrackGroup())) {
                    // Merge with existing selection.
                    scratchSet.clear();
                    for (int k = 0; k < existingSelection.length(); k++) {
                        scratchSet.put(existingSelection.getIndexInTrackGroup(k), 0);
                    }
                    for (int k = 0; k < newSelection.length(); k++) {
                        scratchSet.put(newSelection.getIndexInTrackGroup(k), 0);
                    }
                    int[] mergedTracks = new int[scratchSet.size()];
                    for (int k = 0; k < scratchSet.size(); k++) {
                        mergedTracks[k] = scratchSet.keyAt(k);
                    }
                    existingSelectionList.set(j, new DownloadTrackSelection(existingSelection.getTrackGroup(), mergedTracks));
                    mergedWithExistingSelection = true;
                    break;
                }
            }
            if (!mergedWithExistingSelection) {
                existingSelectionList.add(newSelection);
            }
        }
        return trackSelectorResult;
    } catch (ExoPlaybackException e) {
        // DefaultTrackSelector does not throw exceptions during track selection.
        throw new UnsupportedOperationException(e);
    }
}
Also used : TrackSelectorResult(com.google.android.exoplayer2.trackselection.TrackSelectorResult) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) Nullable(androidx.annotation.Nullable) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 2 with ExoTrackSelection

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

the class DownloadHelper method onMediaPrepared.

// Initialization of array of Lists.
@SuppressWarnings("unchecked")
private void onMediaPrepared() {
    checkNotNull(mediaPreparer);
    checkNotNull(mediaPreparer.mediaPeriods);
    checkNotNull(mediaPreparer.timeline);
    int periodCount = mediaPreparer.mediaPeriods.length;
    int rendererCount = rendererCapabilities.length;
    trackSelectionsByPeriodAndRenderer = (List<ExoTrackSelection>[][]) new List<?>[periodCount][rendererCount];
    immutableTrackSelectionsByPeriodAndRenderer = (List<ExoTrackSelection>[][]) new List<?>[periodCount][rendererCount];
    for (int i = 0; i < periodCount; i++) {
        for (int j = 0; j < rendererCount; j++) {
            trackSelectionsByPeriodAndRenderer[i][j] = new ArrayList<>();
            immutableTrackSelectionsByPeriodAndRenderer[i][j] = Collections.unmodifiableList(trackSelectionsByPeriodAndRenderer[i][j]);
        }
    }
    trackGroupArrays = new TrackGroupArray[periodCount];
    mappedTrackInfos = new MappedTrackInfo[periodCount];
    for (int i = 0; i < periodCount; i++) {
        trackGroupArrays[i] = mediaPreparer.mediaPeriods[i].getTrackGroups();
        TrackSelectorResult trackSelectorResult = runTrackSelection(/* periodIndex= */
        i);
        trackSelector.onSelectionActivated(trackSelectorResult.info);
        mappedTrackInfos[i] = checkNotNull(trackSelector.getCurrentMappedTrackInfo());
    }
    setPreparedWithMedia();
    checkNotNull(callbackHandler).post(() -> checkNotNull(callback).onPrepared(this));
}
Also used : TrackSelectorResult(com.google.android.exoplayer2.trackselection.TrackSelectorResult) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with ExoTrackSelection

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

the class ExoPlayerImplInternal method enableRenderer.

private void enableRenderer(int rendererIndex, boolean wasRendererEnabled) throws ExoPlaybackException {
    Renderer renderer = renderers[rendererIndex];
    if (isRendererEnabled(renderer)) {
        return;
    }
    MediaPeriodHolder periodHolder = queue.getReadingPeriod();
    boolean mayRenderStartOfStream = periodHolder == queue.getPlayingPeriod();
    TrackSelectorResult trackSelectorResult = periodHolder.getTrackSelectorResult();
    RendererConfiguration rendererConfiguration = trackSelectorResult.rendererConfigurations[rendererIndex];
    ExoTrackSelection newSelection = trackSelectorResult.selections[rendererIndex];
    Format[] formats = getFormats(newSelection);
    // The renderer needs enabling with its new track selection.
    boolean playing = shouldPlayWhenReady() && playbackInfo.playbackState == Player.STATE_READY;
    // Consider as joining only if the renderer was previously disabled.
    boolean joining = !wasRendererEnabled && playing;
    // Enable the renderer.
    enabledRendererCount++;
    renderersToReset.add(renderer);
    renderer.enable(rendererConfiguration, formats, periodHolder.sampleStreams[rendererIndex], rendererPositionUs, joining, mayRenderStartOfStream, periodHolder.getStartPositionRendererTime(), periodHolder.getRendererOffset());
    renderer.handleMessage(Renderer.MSG_SET_WAKEUP_LISTENER, new Renderer.WakeupListener() {

        @Override
        public void onSleep(long wakeupDeadlineMs) {
            // Do not sleep if the expected sleep time is not long enough to save significant power.
            if (wakeupDeadlineMs >= MIN_RENDERER_SLEEP_DURATION_MS) {
                requestForRendererSleep = true;
            }
        }

        @Override
        public void onWakeup() {
            handler.sendEmptyMessage(MSG_DO_SOME_WORK);
        }
    });
    mediaClock.onRendererEnabled(renderer);
    // Start the renderer if playing.
    if (playing) {
        renderer.start();
    }
}
Also used : TrackSelectorResult(com.google.android.exoplayer2.trackselection.TrackSelectorResult) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) TextRenderer(com.google.android.exoplayer2.text.TextRenderer)

Example 4 with ExoTrackSelection

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

the class ExoPlayerImplInternal method extractMetadataFromTrackSelectionArray.

private ImmutableList<Metadata> extractMetadataFromTrackSelectionArray(ExoTrackSelection[] trackSelections) {
    ImmutableList.Builder<Metadata> result = new ImmutableList.Builder<>();
    boolean seenNonEmptyMetadata = false;
    for (ExoTrackSelection trackSelection : trackSelections) {
        if (trackSelection != null) {
            Format format = trackSelection.getFormat(/* index= */
            0);
            if (format.metadata == null) {
                result.add(new Metadata());
            } else {
                result.add(format.metadata);
                seenNonEmptyMetadata = true;
            }
        }
    }
    return seenNonEmptyMetadata ? result.build() : ImmutableList.of();
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) ImmutableList(com.google.common.collect.ImmutableList) Metadata(com.google.android.exoplayer2.metadata.Metadata)

Example 5 with ExoTrackSelection

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

the class MediaPeriodHolder method enableTrackSelectionsInResult.

private void enableTrackSelectionsInResult() {
    if (!isLoadingMediaPeriod()) {
        return;
    }
    for (int i = 0; i < trackSelectorResult.length; i++) {
        boolean rendererEnabled = trackSelectorResult.isRendererEnabled(i);
        ExoTrackSelection trackSelection = trackSelectorResult.selections[i];
        if (rendererEnabled && trackSelection != null) {
            trackSelection.enable();
        }
    }
}
Also used : ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection)

Aggregations

ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)21 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)9 ArrayList (java.util.ArrayList)8 Format (com.google.android.exoplayer2.Format)5 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)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