Search in sources :

Example 16 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class MediaCodecAudioRendererTest method render_throwsExoPlaybackExceptionJustOnce_whenSet.

@Test
public void render_throwsExoPlaybackExceptionJustOnce_whenSet() throws Exception {
    MediaCodecAudioRenderer exceptionThrowingRenderer = new MediaCodecAudioRenderer(ApplicationProvider.getApplicationContext(), mediaCodecSelector, /* eventHandler= */
    null, /* eventListener= */
    null) {

        @Override
        protected void onOutputFormatChanged(Format format, @Nullable MediaFormat mediaFormat) throws ExoPlaybackException {
            super.onOutputFormatChanged(format, mediaFormat);
            if (!format.equals(AUDIO_AAC)) {
                setPendingPlaybackException(ExoPlaybackException.createForRenderer(new AudioSink.ConfigurationException("Test", format), "rendererName", /* rendererIndex= */
                0, format, C.FORMAT_HANDLED, /* isRecoverable= */
                false, PlaybackException.ERROR_CODE_UNSPECIFIED));
            }
        }
    };
    Format changedFormat = AUDIO_AAC.buildUpon().setSampleRate(32_000).build();
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    AUDIO_AAC, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    exceptionThrowingRenderer.init(/* index= */
    0, PlayerId.UNSET);
    exceptionThrowingRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { AUDIO_AAC, changedFormat }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    exceptionThrowingRenderer.start();
    exceptionThrowingRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    exceptionThrowingRenderer.render(/* positionUs= */
    250, SystemClock.elapsedRealtime() * 1000);
    MediaFormat mediaFormat = new MediaFormat();
    mediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 2);
    mediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, 32_000);
    // Simulating the exception being thrown when not traceable back to render.
    exceptionThrowingRenderer.onOutputFormatChanged(changedFormat, mediaFormat);
    assertThrows(ExoPlaybackException.class, () -> exceptionThrowingRenderer.render(/* positionUs= */
    500, SystemClock.elapsedRealtime() * 1000));
    // Doesn't throw an exception because it's cleared after being thrown in the previous call to
    // render.
    exceptionThrowingRenderer.render(/* positionUs= */
    750, SystemClock.elapsedRealtime() * 1000);
}
Also used : MediaFormat(android.media.MediaFormat) FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) MediaFormat(android.media.MediaFormat) Format(androidx.media3.common.Format) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 17 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class AdsMediaSourceTest method setUp.

@Before
public void setUp() {
    // Set up content and ad media sources, passing a null timeline so tests can simulate setting it
    // later.
    contentMediaSource = new FakeMediaSource(/* timeline= */
    null);
    prerollAdMediaSource = new FakeMediaSource(/* timeline= */
    null);
    MediaSource.Factory adMediaSourceFactory = mock(MediaSource.Factory.class);
    when(adMediaSourceFactory.createMediaSource(any(MediaItem.class))).thenReturn(prerollAdMediaSource);
    // Prepare the AdsMediaSource and capture its ads loader listener.
    AdsLoader mockAdsLoader = mock(AdsLoader.class);
    AdViewProvider mockAdViewProvider = mock(AdViewProvider.class);
    ArgumentCaptor<EventListener> eventListenerArgumentCaptor = ArgumentCaptor.forClass(AdsLoader.EventListener.class);
    adsMediaSource = new AdsMediaSource(contentMediaSource, TEST_ADS_DATA_SPEC, TEST_ADS_ID, adMediaSourceFactory, mockAdsLoader, mockAdViewProvider);
    adsMediaSource.prepareSource(mockMediaSourceCaller, /* mediaTransferListener= */
    null, PlayerId.UNSET);
    shadowOf(Looper.getMainLooper()).idle();
    verify(mockAdsLoader).start(eq(adsMediaSource), eq(TEST_ADS_DATA_SPEC), eq(TEST_ADS_ID), eq(mockAdViewProvider), eventListenerArgumentCaptor.capture());
    // Simulate loading a preroll ad.
    AdsLoader.EventListener adsLoaderEventListener = eventListenerArgumentCaptor.getValue();
    adsLoaderEventListener.onAdPlaybackState(AD_PLAYBACK_STATE);
    shadowOf(Looper.getMainLooper()).idle();
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaItem(androidx.media3.common.MediaItem) AdViewProvider(androidx.media3.common.AdViewProvider) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) Before(org.junit.Before)

Example 18 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class DecoderVideoRendererTest method enable_withoutMayRenderStartOfStream_rendersFirstFrameAfterStart.

@Test
public void enable_withoutMayRenderStartOfStream_rendersFirstFrameAfterStart() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME)));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    renderer.start();
    for (int i = 0; i < 10; i++) {
        renderer.render(/* positionUs= */
        0, SystemClock.elapsedRealtime() * 1000);
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 19 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class DecoderVideoRendererTest method enable_withoutMayRenderStartOfStream_doesNotRenderFirstFrameBeforeStart.

@Test
public void enable_withoutMayRenderStartOfStream_doesNotRenderFirstFrameBeforeStart() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME)));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    for (int i = 0; i < 10; i++) {
        renderer.render(/* positionUs= */
        0, SystemClock.elapsedRealtime() * 1000);
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener, never()).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 20 with EventListener

use of androidx.media3.exoplayer.source.ads.AdsLoader.EventListener in project media by androidx.

the class DecoderVideoRendererTest method replaceStream_whenStarted_rendersFirstFrameOfNewStream.

// TODO: Fix rendering of first frame at stream transition.
@Ignore
@Test
public void replaceStream_whenStarted_rendersFirstFrameOfNewStream() throws Exception {
    FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream1.writeData(/* startPositionUs= */
    0);
    FakeSampleStream fakeSampleStream2 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    H264_FORMAT, ImmutableList.of(oneByteSample(/* timeUs= */
    0), END_OF_STREAM_ITEM));
    fakeSampleStream2.writeData(/* startPositionUs= */
    0);
    renderer.enable(RendererConfiguration.DEFAULT, new Format[] { H264_FORMAT }, fakeSampleStream1, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    renderer.start();
    boolean replacedStream = false;
    for (int i = 0; i <= 10; i++) {
        renderer.render(/* positionUs= */
        i * 10, SystemClock.elapsedRealtime() * 1000);
        if (!replacedStream && renderer.hasReadStreamToEnd()) {
            renderer.replaceStream(new Format[] { H264_FORMAT }, fakeSampleStream2, /* startPositionUs= */
            100, /* offsetUs= */
            100);
            replacedStream = true;
        }
        // Ensure pending messages are delivered.
        ShadowLooper.idleMainLooper();
    }
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(androidx.media3.test.utils.FakeSampleStream) DefaultAllocator(androidx.media3.exoplayer.upstream.DefaultAllocator) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DrmSessionEventListener (androidx.media3.exoplayer.drm.DrmSessionEventListener)18 Test (org.junit.Test)18 FakeSampleStream (androidx.media3.test.utils.FakeSampleStream)17 DefaultAllocator (androidx.media3.exoplayer.upstream.DefaultAllocator)16 Nullable (androidx.annotation.Nullable)8 Format (androidx.media3.common.Format)6 Before (org.junit.Before)6 ShadowLooper (org.robolectric.shadows.ShadowLooper)6 SurfaceTexture (android.graphics.SurfaceTexture)5 Handler (android.os.Handler)5 Surface (android.view.Surface)5 MediaFormat (android.media.MediaFormat)4 Looper (android.os.Looper)4 C (androidx.media3.common.C)4 MimeTypes (androidx.media3.common.MimeTypes)4 VideoSize (androidx.media3.common.VideoSize)4 RendererCapabilities (androidx.media3.exoplayer.RendererCapabilities)4 PlayerId (androidx.media3.exoplayer.analytics.PlayerId)4 CodecCapabilities (android.media.MediaCodecInfo.CodecCapabilities)3 CodecProfileLevel (android.media.MediaCodecInfo.CodecProfileLevel)3