Search in sources :

Example 1 with MediaPeriod

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

the class ConcatenatingMediaSource method createPeriod.

@Override
public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
    Object mediaSourceHolderUid = getMediaSourceHolderUid(id.periodUid);
    MediaPeriodId childMediaPeriodId = id.copyWithPeriodUid(getChildPeriodUid(id.periodUid));
    @Nullable MediaSourceHolder holder = mediaSourceByUid.get(mediaSourceHolderUid);
    if (holder == null) {
        // Stale event. The media source has already been removed.
        holder = new MediaSourceHolder(new FakeMediaSource(), useLazyPreparation);
        holder.isRemoved = true;
        prepareChildSource(holder, holder.mediaSource);
    }
    enableMediaSource(holder);
    holder.activeMediaPeriodIds.add(childMediaPeriodId);
    MediaPeriod mediaPeriod = holder.mediaSource.createPeriod(childMediaPeriodId, allocator, startPositionUs);
    mediaSourceByMediaPeriod.put(mediaPeriod, holder);
    disableUnusedMediaSources();
    return mediaPeriod;
}
Also used : MediaSourceHolder(com.google.android.exoplayer2.source.ConcatenatingMediaSource.MediaSourceHolder) Nullable(androidx.annotation.Nullable)

Example 2 with MediaPeriod

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

the class MediaSourceList method createPeriod.

/**
 * Returns a new {@link MediaPeriod} identified by {@code periodId}.
 *
 * @param id The identifier of the period.
 * @param allocator An {@link Allocator} from which to obtain media buffer allocations.
 * @param startPositionUs The expected start position, in microseconds.
 * @return A new {@link MediaPeriod}.
 */
public MediaPeriod createPeriod(MediaSource.MediaPeriodId id, Allocator allocator, long startPositionUs) {
    Object mediaSourceHolderUid = getMediaSourceHolderUid(id.periodUid);
    MediaSource.MediaPeriodId childMediaPeriodId = id.copyWithPeriodUid(getChildPeriodUid(id.periodUid));
    MediaSourceHolder holder = Assertions.checkNotNull(mediaSourceByUid.get(mediaSourceHolderUid));
    enableMediaSource(holder);
    holder.activeMediaPeriodIds.add(childMediaPeriodId);
    MediaPeriod mediaPeriod = holder.mediaSource.createPeriod(childMediaPeriodId, allocator, startPositionUs);
    mediaSourceByMediaPeriod.put(mediaPeriod, holder);
    disableUnusedMediaSources();
    return mediaPeriod;
}
Also used : MediaSource(com.google.android.exoplayer2.source.MediaSource) MaskingMediaSource(com.google.android.exoplayer2.source.MaskingMediaSource) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) MaskingMediaPeriod(com.google.android.exoplayer2.source.MaskingMediaPeriod)

Example 3 with MediaPeriod

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

the class AdsMediaSource method releasePeriod.

@Override
public void releasePeriod(MediaPeriod mediaPeriod) {
    MaskingMediaPeriod maskingMediaPeriod = (MaskingMediaPeriod) mediaPeriod;
    MediaPeriodId id = maskingMediaPeriod.id;
    if (id.isAd()) {
        AdMediaSourceHolder adMediaSourceHolder = checkNotNull(adMediaSourceHolders[id.adGroupIndex][id.adIndexInAdGroup]);
        adMediaSourceHolder.releaseMediaPeriod(maskingMediaPeriod);
        if (adMediaSourceHolder.isInactive()) {
            adMediaSourceHolder.release();
            adMediaSourceHolders[id.adGroupIndex][id.adIndexInAdGroup] = null;
        }
    } else {
        maskingMediaPeriod.releasePeriod();
    }
}
Also used : MaskingMediaPeriod(com.google.android.exoplayer2.source.MaskingMediaPeriod) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)

Example 4 with MediaPeriod

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

the class MergingMediaPeriodTest method prepareMergingPeriod.

private MergingMediaPeriod prepareMergingPeriod(MergingPeriodDefinition... definitions) throws Exception {
    MediaPeriod[] mediaPeriods = new MediaPeriod[definitions.length];
    long[] timeOffsetsUs = new long[definitions.length];
    for (int i = 0; i < definitions.length; i++) {
        MergingPeriodDefinition definition = definitions[i];
        timeOffsetsUs[i] = definition.timeOffsetUs;
        TrackGroup[] trackGroups = new TrackGroup[definition.formats.length];
        for (int j = 0; j < definition.formats.length; j++) {
            trackGroups[j] = new TrackGroup(definition.formats[j]);
        }
        mediaPeriods[i] = new FakeMediaPeriodWithSelectTracksPosition(new TrackGroupArray(trackGroups), new EventDispatcher().withParameters(/* windowIndex= */
        i, new MediaPeriodId(/* periodUid= */
        i), /* mediaTimeOffsetMs= */
        0), /* trackDataFactory= */
        (unusedFormat, unusedMediaPeriodId) -> ImmutableList.of(oneByteSample(definition.singleSampleTimeUs, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    }
    MergingMediaPeriod mergingMediaPeriod = new MergingMediaPeriod(new DefaultCompositeSequenceableLoaderFactory(), timeOffsetsUs, mediaPeriods);
    CountDownLatch prepareCountDown = new CountDownLatch(1);
    mergingMediaPeriod.prepare(new MediaPeriod.Callback() {

        @Override
        public void onPrepared(MediaPeriod mediaPeriod) {
            prepareCountDown.countDown();
        }

        @Override
        public void onContinueLoadingRequested(MediaPeriod source) {
            mergingMediaPeriod.continueLoading(/* positionUs= */
            0);
        }
    }, /* positionUs= */
    0);
    prepareCountDown.await();
    return mergingMediaPeriod;
}
Also used : FLAG_REQUIRE_FORMAT(com.google.android.exoplayer2.source.SampleStream.FLAG_REQUIRE_FORMAT) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) END_OF_STREAM_ITEM(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM) EventDispatcher(com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod) Format(com.google.android.exoplayer2.Format) FixedTrackSelection(com.google.android.exoplayer2.trackselection.FixedTrackSelection) CountDownLatch(java.util.concurrent.CountDownLatch) FakeSampleStreamItem.oneByteSample(com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.oneByteSample) FormatHolder(com.google.android.exoplayer2.FormatHolder) ExoTrackSelection(com.google.android.exoplayer2.trackselection.ExoTrackSelection) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) ImmutableList(com.google.common.collect.ImmutableList) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DecoderInputBuffer(com.google.android.exoplayer2.decoder.DecoderInputBuffer) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) C(com.google.android.exoplayer2.C) NullableType(org.checkerframework.checker.nullness.compatqual.NullableType) CountDownLatch(java.util.concurrent.CountDownLatch) EventDispatcher(com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) FakeMediaPeriod(com.google.android.exoplayer2.testutil.FakeMediaPeriod)

Example 5 with MediaPeriod

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

the class ProgressiveMediaPeriodTest method testExtractorsUpdatesSourceInfoBeforeOnPreparedCallback.

private static void testExtractorsUpdatesSourceInfoBeforeOnPreparedCallback(ProgressiveMediaExtractor extractor) throws TimeoutException {
    AtomicBoolean sourceInfoRefreshCalled = new AtomicBoolean(false);
    ProgressiveMediaPeriod.Listener sourceInfoRefreshListener = (durationUs, isSeekable, isLive) -> sourceInfoRefreshCalled.set(true);
    MediaPeriodId mediaPeriodId = new MediaPeriodId(/* periodUid= */
    new Object());
    ProgressiveMediaPeriod mediaPeriod = new ProgressiveMediaPeriod(Uri.parse("asset://android_asset/media/mp4/sample.mp4"), new AssetDataSource(ApplicationProvider.getApplicationContext()), extractor, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher().withParameters(/* windowIndex= */
    0, mediaPeriodId), new DefaultLoadErrorHandlingPolicy(), new MediaSourceEventListener.EventDispatcher().withParameters(/* windowIndex= */
    0, mediaPeriodId, /* mediaTimeOffsetMs= */
    0), sourceInfoRefreshListener, new DefaultAllocator(/* trimOnReset= */
    true, C.DEFAULT_BUFFER_SEGMENT_SIZE), /* customCacheKey= */
    null, ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES);
    AtomicBoolean prepareCallbackCalled = new AtomicBoolean(false);
    AtomicBoolean sourceInfoRefreshCalledBeforeOnPrepared = new AtomicBoolean(false);
    mediaPeriod.prepare(new MediaPeriod.Callback() {

        @Override
        public void onPrepared(MediaPeriod mediaPeriod) {
            sourceInfoRefreshCalledBeforeOnPrepared.set(sourceInfoRefreshCalled.get());
            prepareCallbackCalled.set(true);
        }

        @Override
        public void onContinueLoadingRequested(MediaPeriod source) {
            source.continueLoading(/* positionUs= */
            0);
        }
    }, /* positionUs= */
    0);
    runMainLooperUntil(prepareCallbackCalled::get);
    mediaPeriod.release();
    assertThat(sourceInfoRefreshCalledBeforeOnPrepared.get()).isTrue();
}
Also used : RobolectricUtil.runMainLooperUntil(com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) AssetDataSource(com.google.android.exoplayer2.upstream.AssetDataSource) DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) Uri(android.net.Uri) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) ApplicationProvider(androidx.test.core.app.ApplicationProvider) Mp4Extractor(com.google.android.exoplayer2.extractor.mp4.Mp4Extractor) PlayerId(com.google.android.exoplayer2.analytics.PlayerId) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) C(com.google.android.exoplayer2.C) AssetDataSource(com.google.android.exoplayer2.upstream.AssetDataSource) DefaultLoadErrorHandlingPolicy(com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator)

Aggregations

MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)23 Test (org.junit.Test)21 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)18 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)17 Nullable (androidx.annotation.Nullable)15 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)15 Allocator (com.google.android.exoplayer2.upstream.Allocator)15 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)14 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)14 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)14 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)13 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)11 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)8 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)7 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)7 MediaSource (com.google.android.exoplayer2.source.MediaSource)7 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)7 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)7