Search in sources :

Example 41 with MediaPeriod

use of androidx.media3.exoplayer.source.MediaPeriod in project media by androidx.

the class ServerSideAdInsertionMediaSource method createPeriod.

@Override
public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
    @Nullable SharedMediaPeriod sharedPeriod = null;
    Pair<Long, Object> sharedMediaPeriodKey = new Pair<>(id.windowSequenceNumber, id.periodUid);
    boolean reusedSharedPeriod = false;
    if (lastUsedMediaPeriod != null) {
        if (lastUsedMediaPeriod.periodUid.equals(id.periodUid)) {
            sharedPeriod = lastUsedMediaPeriod;
            mediaPeriods.put(sharedMediaPeriodKey, sharedPeriod);
            reusedSharedPeriod = true;
        } else {
            lastUsedMediaPeriod.release(mediaSource);
        }
        lastUsedMediaPeriod = null;
    }
    if (sharedPeriod == null) {
        @Nullable SharedMediaPeriod lastExistingPeriod = Iterables.getLast(mediaPeriods.get(sharedMediaPeriodKey), /* defaultValue= */
        null);
        if (lastExistingPeriod != null && lastExistingPeriod.canReuseMediaPeriod(id, startPositionUs)) {
            sharedPeriod = lastExistingPeriod;
        } else {
            AdPlaybackState adPlaybackState = checkNotNull(adPlaybackStates.get(id.periodUid));
            long streamPositionUs = getStreamPositionUs(startPositionUs, id, adPlaybackState);
            sharedPeriod = new SharedMediaPeriod(mediaSource.createPeriod(new MediaPeriodId(id.periodUid, id.windowSequenceNumber), allocator, streamPositionUs), id.periodUid, adPlaybackState);
            mediaPeriods.put(sharedMediaPeriodKey, sharedPeriod);
        }
    }
    MediaPeriodImpl mediaPeriod = new MediaPeriodImpl(sharedPeriod, id, createEventDispatcher(id), createDrmEventDispatcher(id));
    sharedPeriod.add(mediaPeriod);
    if (reusedSharedPeriod && sharedPeriod.trackSelections.length > 0) {
        mediaPeriod.seekToUs(startPositionUs);
    }
    return mediaPeriod;
}
Also used : AdPlaybackState(androidx.media3.common.AdPlaybackState) Nullable(androidx.annotation.Nullable) Pair(android.util.Pair)

Aggregations

MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)23 Test (org.junit.Test)21 FakeTimeline (androidx.media3.test.utils.FakeTimeline)18 Nullable (androidx.annotation.Nullable)17 TrackGroupArray (androidx.media3.common.TrackGroupArray)17 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)17 Allocator (androidx.media3.exoplayer.upstream.Allocator)15 DrmSessionManager (androidx.media3.exoplayer.drm.DrmSessionManager)14 MediaPeriod (androidx.media3.exoplayer.source.MediaPeriod)14 FakeMediaPeriod (androidx.media3.test.utils.FakeMediaPeriod)14 TransferListener (androidx.media3.datasource.TransferListener)13 Timeline (androidx.media3.common.Timeline)11 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)11 DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)9 AdPlaybackState (androidx.media3.common.AdPlaybackState)8 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)8 C (androidx.media3.common.C)7 TrackGroup (androidx.media3.common.TrackGroup)7 MediaSourceEventListener (androidx.media3.exoplayer.source.MediaSourceEventListener)7 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)7