Search in sources :

Example 16 with SampleStream

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

the class EventSampleStreamTest method readDataReturnDataAfterFormat.

/**
 * Tests that {@link EventSampleStream#readData(FormatHolder, DecoderInputBuffer, int)} will
 * return sample data after the first call.
 */
@Test
public void readDataReturnDataAfterFormat() {
    long presentationTimeUs = 1000000;
    EventMessage eventMessage = newEventMessageWithId(1);
    EventStream eventStream = new EventStream(SCHEME_ID, VALUE, TIME_SCALE, new long[] { presentationTimeUs }, new EventMessage[] { eventMessage });
    EventSampleStream sampleStream = new EventSampleStream(eventStream, FORMAT, false);
    // first read - read format
    readData(sampleStream);
    int result = readData(sampleStream);
    assertThat(result).isEqualTo(C.RESULT_BUFFER_READ);
    assertThat(inputBuffer.data.array()).isEqualTo(getEncodedMessage(eventMessage));
}
Also used : EventMessage(com.google.android.exoplayer2.metadata.emsg.EventMessage) EventStream(com.google.android.exoplayer2.source.dash.manifest.EventStream) Test(org.junit.Test)

Example 17 with SampleStream

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

the class HlsSampleStreamWrapper method selectTracks.

/**
 * Called by the parent {@link HlsMediaPeriod} when a track selection occurs.
 *
 * @param selections The renderer track selections.
 * @param mayRetainStreamFlags Flags indicating whether the existing sample stream can be retained
 *     for each selection. A {@code true} value indicates that the selection is unchanged, and
 *     that the caller does not require that the sample stream be recreated.
 * @param streams The existing sample streams, which will be updated to reflect the provided
 *     selections.
 * @param streamResetFlags Will be updated to indicate new sample streams, and sample streams that
 *     have been retained but with the requirement that the consuming renderer be reset.
 * @param positionUs The current playback position in microseconds.
 * @param forceReset If true then a reset is forced (i.e. a seek will be performed with in-buffer
 *     seeking disabled).
 * @return Whether this wrapper requires the parent {@link HlsMediaPeriod} to perform a seek as
 *     part of the track selection.
 */
public boolean selectTracks(@NullableType ExoTrackSelection[] selections, boolean[] mayRetainStreamFlags, @NullableType SampleStream[] streams, boolean[] streamResetFlags, long positionUs, boolean forceReset) {
    assertIsPrepared();
    int oldEnabledTrackGroupCount = enabledTrackGroupCount;
    // Deselect old tracks.
    for (int i = 0; i < selections.length; i++) {
        HlsSampleStream stream = (HlsSampleStream) streams[i];
        if (stream != null && (selections[i] == null || !mayRetainStreamFlags[i])) {
            enabledTrackGroupCount--;
            stream.unbindSampleQueue();
            streams[i] = null;
        }
    }
    // We'll always need to seek if we're being forced to reset, or if this is a first selection to
    // a position other than the one we started preparing with, or if we're making a selection
    // having previously disabled all tracks.
    boolean seekRequired = forceReset || (seenFirstTrackSelection ? oldEnabledTrackGroupCount == 0 : positionUs != lastSeekPositionUs);
    // Get the old (i.e. current before the loop below executes) primary track selection. The new
    // primary selection will equal the old one unless it's changed in the loop.
    ExoTrackSelection oldPrimaryTrackSelection = chunkSource.getTrackSelection();
    ExoTrackSelection primaryTrackSelection = oldPrimaryTrackSelection;
    // Select new tracks.
    for (int i = 0; i < selections.length; i++) {
        ExoTrackSelection selection = selections[i];
        if (selection == null) {
            continue;
        }
        int trackGroupIndex = trackGroups.indexOf(selection.getTrackGroup());
        if (trackGroupIndex == primaryTrackGroupIndex) {
            primaryTrackSelection = selection;
            chunkSource.setTrackSelection(selection);
        }
        if (streams[i] == null) {
            enabledTrackGroupCount++;
            streams[i] = new HlsSampleStream(this, trackGroupIndex);
            streamResetFlags[i] = true;
            if (trackGroupToSampleQueueIndex != null) {
                ((HlsSampleStream) streams[i]).bindSampleQueue();
                // If there's still a chance of avoiding a seek, try and seek within the sample queue.
                if (!seekRequired) {
                    SampleQueue sampleQueue = sampleQueues[trackGroupToSampleQueueIndex[trackGroupIndex]];
                    // 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 (enabledTrackGroupCount == 0) {
        chunkSource.reset();
        downstreamTrackFormat = null;
        pendingResetUpstreamFormats = true;
        mediaChunks.clear();
        if (loader.isLoading()) {
            if (sampleQueuesBuilt) {
                // Discard as much as we can synchronously.
                for (SampleQueue sampleQueue : sampleQueues) {
                    sampleQueue.discardToEnd();
                }
            }
            loader.cancelLoading();
        } else {
            resetSampleQueues();
        }
    } else {
        if (!mediaChunks.isEmpty() && !Util.areEqual(primaryTrackSelection, oldPrimaryTrackSelection)) {
            // The primary track selection has changed and we have buffered media. The buffered media
            // may need to be discarded.
            boolean primarySampleQueueDirty = false;
            if (!seenFirstTrackSelection) {
                long bufferedDurationUs = positionUs < 0 ? -positionUs : 0;
                HlsMediaChunk lastMediaChunk = getLastMediaChunk();
                MediaChunkIterator[] mediaChunkIterators = chunkSource.createMediaChunkIterators(lastMediaChunk, positionUs);
                primaryTrackSelection.updateSelectedTrack(positionUs, bufferedDurationUs, C.TIME_UNSET, readOnlyMediaChunks, mediaChunkIterators);
                int chunkIndex = chunkSource.getTrackGroup().indexOf(lastMediaChunk.trackFormat);
                if (primaryTrackSelection.getSelectedIndexInTrackGroup() != chunkIndex) {
                    // This is the first selection and the chunk loaded during preparation does not match
                    // the initially selected format.
                    primarySampleQueueDirty = true;
                }
            } else {
                // The primary sample queue contains media buffered for the old primary track selection.
                primarySampleQueueDirty = true;
            }
            if (primarySampleQueueDirty) {
                forceReset = true;
                seekRequired = true;
                pendingResetUpstreamFormats = true;
            }
        }
        if (seekRequired) {
            seekToUs(positionUs, forceReset);
            // 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;
                }
            }
        }
    }
    updateSampleStreams(streams);
    seenFirstTrackSelection = true;
    return seekRequired;
}
Also used : SampleQueue(com.google.android.exoplayer2.source.SampleQueue) MediaChunkIterator(com.google.android.exoplayer2.source.chunk.MediaChunkIterator) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection)

Example 18 with SampleStream

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

the class HlsSampleStreamWrapper method mapSampleQueuesToMatchTrackGroups.

@RequiresNonNull("trackGroups")
@EnsuresNonNull("trackGroupToSampleQueueIndex")
private void mapSampleQueuesToMatchTrackGroups() {
    int trackGroupCount = trackGroups.length;
    trackGroupToSampleQueueIndex = new int[trackGroupCount];
    Arrays.fill(trackGroupToSampleQueueIndex, C.INDEX_UNSET);
    for (int i = 0; i < trackGroupCount; i++) {
        for (int queueIndex = 0; queueIndex < sampleQueues.length; queueIndex++) {
            SampleQueue sampleQueue = sampleQueues[queueIndex];
            Format upstreamFormat = Assertions.checkStateNotNull(sampleQueue.getUpstreamFormat());
            if (formatsMatch(upstreamFormat, trackGroups.get(i).getFormat(0))) {
                trackGroupToSampleQueueIndex[i] = queueIndex;
                break;
            }
        }
    }
    for (HlsSampleStream sampleStream : hlsSampleStreams) {
        sampleStream.bindSampleQueue();
    }
}
Also used : SampleQueue(com.google.android.exoplayer2.source.SampleQueue) Format(com.google.android.exoplayer2.Format) EnsuresNonNull(org.checkerframework.checker.nullness.qual.EnsuresNonNull) RequiresNonNull(org.checkerframework.checker.nullness.qual.RequiresNonNull)

Example 19 with SampleStream

use of com.google.android.exoplayer2.source.SampleStream 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 20 with SampleStream

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

Aggregations

EventStream (com.google.android.exoplayer2.source.dash.manifest.EventStream)15 Test (org.junit.Test)15 EventMessage (com.google.android.exoplayer2.metadata.emsg.EventMessage)10 ExoTrackSelection (com.google.android.exoplayer2.trackselection.ExoTrackSelection)10 SampleStream (com.google.android.exoplayer2.source.SampleStream)8 TrackGroup (com.google.android.exoplayer2.source.TrackGroup)6 CompositeSequenceableLoader (com.google.android.exoplayer2.source.CompositeSequenceableLoader)5 ChunkSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream)5 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)5 ArrayList (java.util.ArrayList)5 Format (com.google.android.exoplayer2.Format)3 FormatHolder (com.google.android.exoplayer2.FormatHolder)3 EmptySampleStream (com.google.android.exoplayer2.source.EmptySampleStream)3 TextRenderer (com.google.android.exoplayer2.text.TextRenderer)3 TrackSelectorResult (com.google.android.exoplayer2.trackselection.TrackSelectorResult)3 DecoderInputBuffer (com.google.android.exoplayer2.decoder.DecoderInputBuffer)2 SampleQueue (com.google.android.exoplayer2.source.SampleQueue)2 EmbeddedSampleStream (com.google.android.exoplayer2.source.chunk.ChunkSampleStream.EmbeddedSampleStream)2 FixedTrackSelection (com.google.android.exoplayer2.trackselection.FixedTrackSelection)2 NullableType (org.checkerframework.checker.nullness.compatqual.NullableType)2