Search in sources :

Example 1 with TrackSelection

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

the class ExtractorMediaPeriod method selectTracks.

@Override
public long selectTracks(TrackSelection[] selections, boolean[] mayRetainStreamFlags, SampleStream[] streams, boolean[] streamResetFlags, long positionUs) {
    Assertions.checkState(prepared);
    // Disable 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;
            sampleQueues.valueAt(track).disable();
            streams[i] = null;
        }
    }
    // Enable new tracks.
    boolean selectedNewTracks = false;
    for (int i = 0; i < selections.length; i++) {
        if (streams[i] == null && selections[i] != null) {
            TrackSelection 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;
            selectedNewTracks = true;
        }
    }
    if (!seenFirstTrackSelection) {
        // At the time of the first track selection all queues will be enabled, so we need to disable
        // any that are no longer required.
        int trackCount = sampleQueues.size();
        for (int i = 0; i < trackCount; i++) {
            if (!trackEnabledStates[i]) {
                sampleQueues.valueAt(i).disable();
            }
        }
    }
    if (enabledTrackCount == 0) {
        notifyReset = false;
        if (loader.isLoading()) {
            loader.cancelLoading();
        }
    } else if (seenFirstTrackSelection ? selectedNewTracks : positionUs != 0) {
        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 : TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection)

Example 2 with TrackSelection

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

the class ExoPlayerImplInternal method setPlayingPeriodHolder.

private void setPlayingPeriodHolder(MediaPeriodHolder periodHolder) throws ExoPlaybackException {
    if (playingPeriodHolder == periodHolder) {
        return;
    }
    int enabledRendererCount = 0;
    boolean[] rendererWasEnabledFlags = new boolean[renderers.length];
    for (int i = 0; i < renderers.length; i++) {
        Renderer renderer = renderers[i];
        rendererWasEnabledFlags[i] = renderer.getState() != Renderer.STATE_DISABLED;
        TrackSelection newSelection = periodHolder.trackSelectorResult.selections.get(i);
        if (newSelection != null) {
            enabledRendererCount++;
        }
        if (rendererWasEnabledFlags[i] && (newSelection == null || (renderer.isCurrentStreamFinal() && renderer.getStream() == playingPeriodHolder.sampleStreams[i]))) {
            // is final and it's not reading ahead.
            if (renderer == rendererMediaClockSource) {
                // Sync standaloneMediaClock so that it can take over timing responsibilities.
                standaloneMediaClock.setPositionUs(rendererMediaClock.getPositionUs());
                rendererMediaClock = null;
                rendererMediaClockSource = null;
            }
            ensureStopped(renderer);
            renderer.disable();
        }
    }
    playingPeriodHolder = periodHolder;
    eventHandler.obtainMessage(MSG_TRACKS_CHANGED, periodHolder.trackSelectorResult).sendToTarget();
    enableRenderers(rendererWasEnabledFlags, enabledRendererCount);
}
Also used : TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection)

Example 3 with TrackSelection

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

the class ExoPlayerImplInternal method enableRenderers.

private void enableRenderers(boolean[] rendererWasEnabledFlags, int enabledRendererCount) throws ExoPlaybackException {
    enabledRenderers = new Renderer[enabledRendererCount];
    enabledRendererCount = 0;
    for (int i = 0; i < renderers.length; i++) {
        Renderer renderer = renderers[i];
        TrackSelection newSelection = playingPeriodHolder.trackSelectorResult.selections.get(i);
        if (newSelection != null) {
            enabledRenderers[enabledRendererCount++] = renderer;
            if (renderer.getState() == Renderer.STATE_DISABLED) {
                RendererConfiguration rendererConfiguration = playingPeriodHolder.trackSelectorResult.rendererConfigurations[i];
                // The renderer needs enabling with its new track selection.
                boolean playing = playWhenReady && state == ExoPlayer.STATE_READY;
                // Consider as joining only if the renderer was previously disabled.
                boolean joining = !rendererWasEnabledFlags[i] && playing;
                // Build an array of formats contained by the selection.
                Format[] formats = new Format[newSelection.length()];
                for (int j = 0; j < formats.length; j++) {
                    formats[j] = newSelection.getFormat(j);
                }
                // Enable the renderer.
                renderer.enable(rendererConfiguration, formats, playingPeriodHolder.sampleStreams[i], rendererPositionUs, joining, playingPeriodHolder.getRendererOffset());
                MediaClock mediaClock = renderer.getMediaClock();
                if (mediaClock != null) {
                    if (rendererMediaClock != null) {
                        throw ExoPlaybackException.createForUnexpected(new IllegalStateException("Multiple renderer media clocks enabled."));
                    }
                    rendererMediaClock = mediaClock;
                    rendererMediaClockSource = renderer;
                }
                // Start the renderer if playing.
                if (playing) {
                    renderer.start();
                }
            }
        }
    }
}
Also used : StandaloneMediaClock(com.google.android.exoplayer2.util.StandaloneMediaClock) MediaClock(com.google.android.exoplayer2.util.MediaClock) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection)

Example 4 with TrackSelection

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

the class ExoPlayerImplInternal method updatePeriods.

private void updatePeriods() throws ExoPlaybackException, IOException {
    if (timeline == null) {
        // We're waiting to get information about periods.
        mediaSource.maybeThrowSourceInfoRefreshError();
        return;
    }
    // Update the loading period if required.
    maybeUpdateLoadingPeriod();
    if (loadingPeriodHolder == null || loadingPeriodHolder.isFullyBuffered()) {
        setIsLoading(false);
    } else if (loadingPeriodHolder != null && loadingPeriodHolder.needsContinueLoading) {
        maybeContinueLoading();
    }
    if (playingPeriodHolder == null) {
        // We're waiting for the first period to be prepared.
        return;
    }
    // Update the playing and reading periods.
    while (playingPeriodHolder != readingPeriodHolder && rendererPositionUs >= playingPeriodHolder.next.rendererPositionOffsetUs) {
        // All enabled renderers' streams have been read to the end, and the playback position reached
        // the end of the playing period, so advance playback to the next period.
        playingPeriodHolder.release();
        setPlayingPeriodHolder(playingPeriodHolder.next);
        playbackInfo = new PlaybackInfo(playingPeriodHolder.index, playingPeriodHolder.startPositionUs);
        updatePlaybackPositions();
        eventHandler.obtainMessage(MSG_POSITION_DISCONTINUITY, playbackInfo).sendToTarget();
    }
    if (readingPeriodHolder.isLast) {
        for (int i = 0; i < renderers.length; i++) {
            Renderer renderer = renderers[i];
            SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
            // stream in case of playlist changes that cause the stream to be no longer final.
            if (sampleStream != null && renderer.getStream() == sampleStream && renderer.hasReadStreamToEnd()) {
                renderer.setCurrentStreamFinal();
            }
        }
        return;
    }
    for (int i = 0; i < renderers.length; i++) {
        Renderer renderer = renderers[i];
        SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
        if (renderer.getStream() != sampleStream || (sampleStream != null && !renderer.hasReadStreamToEnd())) {
            return;
        }
    }
    if (readingPeriodHolder.next != null && readingPeriodHolder.next.prepared) {
        TrackSelectorResult oldTrackSelectorResult = readingPeriodHolder.trackSelectorResult;
        readingPeriodHolder = readingPeriodHolder.next;
        TrackSelectorResult newTrackSelectorResult = readingPeriodHolder.trackSelectorResult;
        boolean initialDiscontinuity = readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET;
        for (int i = 0; i < renderers.length; i++) {
            Renderer renderer = renderers[i];
            TrackSelection oldSelection = oldTrackSelectorResult.selections.get(i);
            if (oldSelection == null) {
            // The renderer has no current stream and will be enabled when we play the next period.
            } else if (initialDiscontinuity) {
                // The new period starts with a discontinuity, so the renderer will play out all data then
                // be disabled and re-enabled when it starts playing the next period.
                renderer.setCurrentStreamFinal();
            } else if (!renderer.isCurrentStreamFinal()) {
                TrackSelection newSelection = newTrackSelectorResult.selections.get(i);
                RendererConfiguration oldConfig = oldTrackSelectorResult.rendererConfigurations[i];
                RendererConfiguration newConfig = newTrackSelectorResult.rendererConfigurations[i];
                if (newSelection != null && newConfig.equals(oldConfig)) {
                    // Replace the renderer's SampleStream so the transition to playing the next period can
                    // be seamless.
                    Format[] formats = new Format[newSelection.length()];
                    for (int j = 0; j < formats.length; j++) {
                        formats[j] = newSelection.getFormat(j);
                    }
                    renderer.replaceStream(formats, readingPeriodHolder.sampleStreams[i], readingPeriodHolder.getRendererOffset());
                } else {
                    // The renderer will be disabled when transitioning to playing the next period, either
                    // because there's no new selection or because a configuration change is required. Mark
                    // the SampleStream as final to play out any remaining data.
                    renderer.setCurrentStreamFinal();
                }
            }
        }
    }
}
Also used : TrackSelectorResult(com.google.android.exoplayer2.trackselection.TrackSelectorResult) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) SampleStream(com.google.android.exoplayer2.source.SampleStream)

Example 5 with TrackSelection

use of com.google.android.exoplayer2.trackselection.TrackSelection 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)

Aggregations

TrackGroup (com.google.android.exoplayer2.source.TrackGroup)8 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)8 Point (android.graphics.Point)5 Format (com.google.android.exoplayer2.Format)4 CompositeSequenceableLoader (com.google.android.exoplayer2.source.CompositeSequenceableLoader)3 SampleStream (com.google.android.exoplayer2.source.SampleStream)3 ChunkSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream)3 RendererConfiguration (com.google.android.exoplayer2.RendererConfiguration)2 Metadata (com.google.android.exoplayer2.metadata.Metadata)2 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)2 ArrayList (java.util.ArrayList)2 EmptySampleStream (com.google.android.exoplayer2.source.EmptySampleStream)1 EmbeddedSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream.EmbeddedSampleStream)1 AdaptationSet (com.google.android.exoplayer2.source.dash.manifest.AdaptationSet)1 MappedTrackInfo (com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo)1 TrackSelectionArray (com.google.android.exoplayer2.trackselection.TrackSelectionArray)1 TrackSelectorResult (com.google.android.exoplayer2.trackselection.TrackSelectorResult)1 MediaClock (com.google.android.exoplayer2.util.MediaClock)1 StandaloneMediaClock (com.google.android.exoplayer2.util.StandaloneMediaClock)1 HashMap (java.util.HashMap)1