Search in sources :

Example 21 with MediaPeriodId

use of com.google.android.exoplayer2.source.MediaSource.MediaPeriodId 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 22 with MediaPeriodId

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

Example 23 with MediaPeriodId

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

the class AdsMediaSourceTest method createPeriod_createsChildContentMediaPeriod.

@Test
public void createPeriod_createsChildContentMediaPeriod() {
    contentMediaSource.setNewSourceInfo(CONTENT_TIMELINE);
    shadowOf(Looper.getMainLooper()).idle();
    adsMediaSource.createPeriod(new MediaPeriodId(CONTENT_PERIOD_UID, /* windowSequenceNumber= */
    0), mock(Allocator.class), /* startPositionUs= */
    0);
    contentMediaSource.assertMediaPeriodCreated(new MediaPeriodId(CONTENT_PERIOD_UID, /* windowSequenceNumber= */
    0));
}
Also used : Allocator(com.google.android.exoplayer2.upstream.Allocator) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 24 with MediaPeriodId

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

the class AdsMediaSourceTest method createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfo.

@Test
public void createPeriod_preparesChildAdMediaSourceAndRefreshesSourceInfo() {
    contentMediaSource.setNewSourceInfo(CONTENT_TIMELINE);
    adsMediaSource.createPeriod(new MediaPeriodId(CONTENT_PERIOD_UID, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    0), mock(Allocator.class), /* startPositionUs= */
    0);
    shadowOf(Looper.getMainLooper()).idle();
    assertThat(prerollAdMediaSource.isPrepared()).isTrue();
    verify(mockMediaSourceCaller).onSourceInfoRefreshed(adsMediaSource, new SinglePeriodAdTimeline(CONTENT_TIMELINE, AD_PLAYBACK_STATE));
}
Also used : Allocator(com.google.android.exoplayer2.upstream.Allocator) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Example 25 with MediaPeriodId

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

the class ConcatenatingMediaSourceTest method periodCreationWithAds.

@Test
public void periodCreationWithAds() throws IOException, InterruptedException {
    // Create concatenated media source with ad child source.
    Timeline timelineContentOnly = new FakeTimeline(new TimelineWindowDefinition(2, 111, true, false, 10 * C.MICROS_PER_SECOND));
    Timeline timelineWithAds = new FakeTimeline(new TimelineWindowDefinition(2, 222, true, false, 10 * C.MICROS_PER_SECOND, FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
    1, /* adGroupTimesUs=... */
    0)));
    FakeMediaSource mediaSourceContentOnly = new FakeMediaSource(timelineContentOnly);
    FakeMediaSource mediaSourceWithAds = new FakeMediaSource(timelineWithAds);
    mediaSource.addMediaSource(mediaSourceContentOnly);
    mediaSource.addMediaSource(mediaSourceWithAds);
    Timeline timeline = testRunner.prepareSource();
    // Assert the timeline contains ad groups.
    TimelineAsserts.assertAdGroupCounts(timeline, 0, 0, 1, 1);
    // Create all periods and assert period creation of child media sources has been called.
    testRunner.assertPrepareAndReleaseAllPeriods();
    Object timelineContentOnlyPeriodUid0 = timelineContentOnly.getUidOfPeriod(/* periodIndex= */
    0);
    Object timelineContentOnlyPeriodUid1 = timelineContentOnly.getUidOfPeriod(/* periodIndex= */
    1);
    Object timelineWithAdsPeriodUid0 = timelineWithAds.getUidOfPeriod(/* periodIndex= */
    0);
    Object timelineWithAdsPeriodUid1 = timelineWithAds.getUidOfPeriod(/* periodIndex= */
    1);
    mediaSourceContentOnly.assertMediaPeriodCreated(new MediaPeriodId(timelineContentOnlyPeriodUid0, /* windowSequenceNumber= */
    0));
    mediaSourceContentOnly.assertMediaPeriodCreated(new MediaPeriodId(timelineContentOnlyPeriodUid1, /* windowSequenceNumber= */
    0));
    mediaSourceWithAds.assertMediaPeriodCreated(new MediaPeriodId(timelineWithAdsPeriodUid0, /* windowSequenceNumber= */
    1));
    mediaSourceWithAds.assertMediaPeriodCreated(new MediaPeriodId(timelineWithAdsPeriodUid1, /* windowSequenceNumber= */
    1));
    mediaSourceWithAds.assertMediaPeriodCreated(new MediaPeriodId(timelineWithAdsPeriodUid0, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    1));
    mediaSourceWithAds.assertMediaPeriodCreated(new MediaPeriodId(timelineWithAdsPeriodUid1, /* adGroupIndex= */
    0, /* adIndexInAdGroup= */
    0, /* windowSequenceNumber= */
    1));
    testRunner.assertCompletedManifestLoads(0, 1);
    assertCompletedAllMediaPeriodLoads(timeline);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) MediaPeriodId(com.google.android.exoplayer2.source.MediaSource.MediaPeriodId) Test(org.junit.Test)

Aggregations

MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)106 Test (org.junit.Test)85 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)75 Timeline (com.google.android.exoplayer2.Timeline)50 EventTime (com.google.android.exoplayer2.analytics.AnalyticsListener.EventTime)48 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)28 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)28 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)27 Nullable (androidx.annotation.Nullable)26 Allocator (com.google.android.exoplayer2.upstream.Allocator)22 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)19 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)17 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)16 TrackGroupArray (com.google.android.exoplayer2.source.TrackGroupArray)15 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)15 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)15 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)15 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)13 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)12 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)11