Search in sources :

Example 1 with Chunk

use of androidx.media3.exoplayer.source.chunk.Chunk in project media by androidx.

the class AdaptiveTrackSelection method determineIdealSelectedIndex.

/**
 * Computes the ideal selected index ignoring buffer health.
 *
 * @param nowMs The current time in the timebase of {@link Clock#elapsedRealtime()}, or {@link
 *     Long#MIN_VALUE} to ignore track exclusion.
 * @param chunkDurationUs The duration of a media chunk in microseconds, or {@link C#TIME_UNSET}
 *     if unknown.
 */
private int determineIdealSelectedIndex(long nowMs, long chunkDurationUs) {
    long effectiveBitrate = getAllocatedBandwidth(chunkDurationUs);
    int lowestBitrateAllowedIndex = 0;
    for (int i = 0; i < length; i++) {
        if (nowMs == Long.MIN_VALUE || !isBlacklisted(i, nowMs)) {
            Format format = getFormat(i);
            if (canSelectFormat(format, format.bitrate, effectiveBitrate)) {
                return i;
            } else {
                lowestBitrateAllowedIndex = i;
            }
        }
    }
    return lowestBitrateAllowedIndex;
}
Also used : Format(androidx.media3.common.Format)

Example 2 with Chunk

use of androidx.media3.exoplayer.source.chunk.Chunk in project media by androidx.

the class AdaptiveTrackSelection method evaluateQueueSize.

@Override
public int evaluateQueueSize(long playbackPositionUs, List<? extends MediaChunk> queue) {
    long nowMs = clock.elapsedRealtime();
    if (!shouldEvaluateQueueSize(nowMs, queue)) {
        return queue.size();
    }
    lastBufferEvaluationMs = nowMs;
    lastBufferEvaluationMediaChunk = queue.isEmpty() ? null : Iterables.getLast(queue);
    if (queue.isEmpty()) {
        return 0;
    }
    int queueSize = queue.size();
    MediaChunk lastChunk = queue.get(queueSize - 1);
    long playoutBufferedDurationBeforeLastChunkUs = Util.getPlayoutDurationForMediaDuration(lastChunk.startTimeUs - playbackPositionUs, playbackSpeed);
    long minDurationToRetainAfterDiscardUs = getMinDurationToRetainAfterDiscardUs();
    if (playoutBufferedDurationBeforeLastChunkUs < minDurationToRetainAfterDiscardUs) {
        return queueSize;
    }
    int idealSelectedIndex = determineIdealSelectedIndex(nowMs, getLastChunkDurationUs(queue));
    Format idealFormat = getFormat(idealSelectedIndex);
    // are less than or equal to maxWidthToDiscard and maxHeightToDiscard respectively.
    for (int i = 0; i < queueSize; i++) {
        MediaChunk chunk = queue.get(i);
        Format format = chunk.trackFormat;
        long mediaDurationBeforeThisChunkUs = chunk.startTimeUs - playbackPositionUs;
        long playoutDurationBeforeThisChunkUs = Util.getPlayoutDurationForMediaDuration(mediaDurationBeforeThisChunkUs, playbackSpeed);
        if (playoutDurationBeforeThisChunkUs >= minDurationToRetainAfterDiscardUs && format.bitrate < idealFormat.bitrate && format.height != Format.NO_VALUE && format.height <= maxHeightToDiscard && format.width != Format.NO_VALUE && format.width <= maxWidthToDiscard && format.height < idealFormat.height) {
            return i;
        }
    }
    return queueSize;
}
Also used : Format(androidx.media3.common.Format) MediaChunk(androidx.media3.exoplayer.source.chunk.MediaChunk)

Example 3 with Chunk

use of androidx.media3.exoplayer.source.chunk.Chunk in project media by androidx.

the class ChunkSampleStream method seekToUs.

/**
 * Seeks to the specified position in microseconds.
 *
 * @param positionUs The seek position in microseconds.
 */
public void seekToUs(long positionUs) {
    lastSeekPositionUs = positionUs;
    if (isPendingReset()) {
        // A reset is already pending. We only need to update its position.
        pendingResetPositionUs = positionUs;
        return;
    }
    // Detect whether the seek is to the start of a chunk that's at least partially buffered.
    @Nullable BaseMediaChunk seekToMediaChunk = null;
    for (int i = 0; i < mediaChunks.size(); i++) {
        BaseMediaChunk mediaChunk = mediaChunks.get(i);
        long mediaChunkStartTimeUs = mediaChunk.startTimeUs;
        if (mediaChunkStartTimeUs == positionUs && mediaChunk.clippedStartTimeUs == C.TIME_UNSET) {
            seekToMediaChunk = mediaChunk;
            break;
        } else if (mediaChunkStartTimeUs > positionUs) {
            // We're not going to find a chunk with a matching start time.
            break;
        }
    }
    // See if we can seek inside the primary sample queue.
    boolean seekInsideBuffer;
    if (seekToMediaChunk != null) {
        // When seeking to the start of a chunk we use the index of the first sample in the chunk
        // rather than the seek position. This ensures we seek to the keyframe at the start of the
        // chunk even if its timestamp is slightly earlier than the advertised chunk start time.
        seekInsideBuffer = primarySampleQueue.seekTo(seekToMediaChunk.getFirstSampleIndex(0));
    } else {
        seekInsideBuffer = primarySampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
        positionUs < getNextLoadPositionUs());
    }
    if (seekInsideBuffer) {
        // We can seek inside the buffer.
        nextNotifyPrimaryFormatMediaChunkIndex = primarySampleIndexToMediaChunkIndex(primarySampleQueue.getReadIndex(), /* minChunkIndex= */
        0);
        // Seek the embedded sample queues.
        for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
            embeddedSampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */
            true);
        }
    } else {
        // We can't seek inside the buffer, and so need to reset.
        pendingResetPositionUs = positionUs;
        loadingFinished = false;
        mediaChunks.clear();
        nextNotifyPrimaryFormatMediaChunkIndex = 0;
        if (loader.isLoading()) {
            // Discard as much as we can synchronously.
            primarySampleQueue.discardToEnd();
            for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
                embeddedSampleQueue.discardToEnd();
            }
            loader.cancelLoading();
        } else {
            loader.clearFatalError();
            resetSampleQueues();
        }
    }
}
Also used : SampleQueue(androidx.media3.exoplayer.source.SampleQueue) Nullable(androidx.annotation.Nullable)

Example 4 with Chunk

use of androidx.media3.exoplayer.source.chunk.Chunk in project media by androidx.

the class ChunkSampleStream method onLoadCanceled.

@Override
public void onLoadCanceled(Chunk loadable, long elapsedRealtimeMs, long loadDurationMs, boolean released) {
    loadingChunk = null;
    canceledMediaChunk = null;
    LoadEventInfo loadEventInfo = new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, loadable.getUri(), loadable.getResponseHeaders(), elapsedRealtimeMs, loadDurationMs, loadable.bytesLoaded());
    loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
    mediaSourceEventDispatcher.loadCanceled(loadEventInfo, loadable.type, primaryTrackType, loadable.trackFormat, loadable.trackSelectionReason, loadable.trackSelectionData, loadable.startTimeUs, loadable.endTimeUs);
    if (!released) {
        if (isPendingReset()) {
            resetSampleQueues();
        } else if (isMediaChunk(loadable)) {
            // TODO: Support splicing to keep data from canceled chunk. See [internal b/161130873].
            discardUpstreamMediaChunksFromIndex(mediaChunks.size() - 1);
            if (mediaChunks.isEmpty()) {
                pendingResetPositionUs = lastSeekPositionUs;
            }
        }
        callback.onContinueLoadingRequested(this);
    }
}
Also used : LoadEventInfo(androidx.media3.exoplayer.source.LoadEventInfo)

Example 5 with Chunk

use of androidx.media3.exoplayer.source.chunk.Chunk in project media by androidx.

the class ChunkSampleStream method onLoadCompleted.

// Loader.Callback implementation.
@Override
public void onLoadCompleted(Chunk loadable, long elapsedRealtimeMs, long loadDurationMs) {
    loadingChunk = null;
    chunkSource.onChunkLoadCompleted(loadable);
    LoadEventInfo loadEventInfo = new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, loadable.getUri(), loadable.getResponseHeaders(), elapsedRealtimeMs, loadDurationMs, loadable.bytesLoaded());
    loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
    mediaSourceEventDispatcher.loadCompleted(loadEventInfo, loadable.type, primaryTrackType, loadable.trackFormat, loadable.trackSelectionReason, loadable.trackSelectionData, loadable.startTimeUs, loadable.endTimeUs);
    callback.onContinueLoadingRequested(this);
}
Also used : LoadEventInfo(androidx.media3.exoplayer.source.LoadEventInfo)

Aggregations

Nullable (androidx.annotation.Nullable)22 Format (androidx.media3.common.Format)10 LoadEventInfo (androidx.media3.exoplayer.source.LoadEventInfo)10 Uri (android.net.Uri)9 DataSpec (androidx.media3.datasource.DataSpec)8 TrackGroup (androidx.media3.common.TrackGroup)6 ParsableByteArray (androidx.media3.common.util.ParsableByteArray)6 SampleQueue (androidx.media3.exoplayer.source.SampleQueue)5 RangedUri (androidx.media3.exoplayer.dash.manifest.RangedUri)4 HlsMediaPlaylist (androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist)4 MediaLoadData (androidx.media3.exoplayer.source.MediaLoadData)4 ContainerMediaChunk (androidx.media3.exoplayer.source.chunk.ContainerMediaChunk)4 MediaChunkIterator (androidx.media3.exoplayer.source.chunk.MediaChunkIterator)4 LoadErrorHandlingPolicy (androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy)4 Test (org.junit.Test)4 HttpDataSource (androidx.media3.datasource.HttpDataSource)3 BaseMediaChunkIterator (androidx.media3.exoplayer.source.chunk.BaseMediaChunkIterator)3 BundledChunkExtractor (androidx.media3.exoplayer.source.chunk.BundledChunkExtractor)3 Chunk (androidx.media3.exoplayer.source.chunk.Chunk)3 MediaChunk (androidx.media3.exoplayer.source.chunk.MediaChunk)3