Search in sources :

Example 11 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

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(com.google.android.exoplayer2.testutil.FakeSampleStream) MediaFormat(android.media.MediaFormat) Format(com.google.android.exoplayer2.Format) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 12 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class MediaCodecAudioRendererTest method render_configuresAudioSink_afterGaplessFormatChange.

@Test
public void render_configuresAudioSink_afterGaplessFormatChange() throws Exception {
    Format changedFormat = AUDIO_AAC.buildUpon().setEncoderDelay(400).setEncoderPadding(232).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), oneByteSample(/* timeUs= */
    50, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    100, C.BUFFER_FLAG_KEY_FRAME), format(changedFormat), oneByteSample(/* timeUs= */
    150, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    200, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    250, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecAudioRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { AUDIO_AAC, changedFormat }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecAudioRenderer.start();
    mediaCodecAudioRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecAudioRenderer.render(/* positionUs= */
    250, SystemClock.elapsedRealtime() * 1000);
    mediaCodecAudioRenderer.setCurrentStreamFinal();
    int positionUs = 500;
    do {
        mediaCodecAudioRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
        positionUs += 250;
    } while (!mediaCodecAudioRenderer.isEnded());
    verify(audioSink).configure(getAudioSinkFormat(AUDIO_AAC), /* specifiedBufferSize= */
    0, /* outputChannels= */
    null);
    verify(audioSink).configure(getAudioSinkFormat(changedFormat), /* specifiedBufferSize= */
    0, /* outputChannels= */
    null);
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) MediaFormat(android.media.MediaFormat) Format(com.google.android.exoplayer2.Format) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 13 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class MediaCodecAudioRendererTest method render_configuresAudioSink_afterFormatChange.

@Test
public void render_configuresAudioSink_afterFormatChange() throws Exception {
    Format changedFormat = AUDIO_AAC.buildUpon().setSampleRate(48_000).setEncoderDelay(400).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), oneByteSample(/* timeUs= */
    50, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    100, C.BUFFER_FLAG_KEY_FRAME), format(changedFormat), oneByteSample(/* timeUs= */
    150, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    200, C.BUFFER_FLAG_KEY_FRAME), oneByteSample(/* timeUs= */
    250, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecAudioRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { AUDIO_AAC, changedFormat }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    false, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecAudioRenderer.start();
    mediaCodecAudioRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecAudioRenderer.render(/* positionUs= */
    250, SystemClock.elapsedRealtime() * 1000);
    mediaCodecAudioRenderer.setCurrentStreamFinal();
    int positionUs = 500;
    do {
        mediaCodecAudioRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
        positionUs += 250;
    } while (!mediaCodecAudioRenderer.isEnded());
    verify(audioSink).configure(getAudioSinkFormat(AUDIO_AAC), /* specifiedBufferSize= */
    0, /* outputChannels= */
    null);
    verify(audioSink).configure(getAudioSinkFormat(changedFormat), /* specifiedBufferSize= */
    0, /* outputChannels= */
    null);
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) MediaFormat(android.media.MediaFormat) Format(com.google.android.exoplayer2.Format) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 14 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class MediaCodecVideoRendererTest method render_includingResetPosition_keepsOutputFormatInVideoFrameMetadataListener.

@Test
public void render_includingResetPosition_keepsOutputFormatInVideoFrameMetadataListener() throws Exception {
    FakeSampleStream fakeSampleStream = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME)));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.start();
    mediaCodecVideoRenderer.render(/* positionUs= */
    0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.resetPosition(0);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    fakeSampleStream.append(ImmutableList.of(oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    int positionUs = 10;
    do {
        mediaCodecVideoRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
        positionUs += 10;
    } while (!mediaCodecVideoRenderer.isEnded());
    shadowOf(testMainLooper).idle();
    assertThat(currentOutputFormat).isEqualTo(VIDEO_H264);
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 15 with END_OF_STREAM_ITEM

use of com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM in project ExoPlayer by google.

the class MediaCodecVideoRendererTest method replaceStream_rendersFirstFrameOnlyAfterStartPosition.

@Test
public void replaceStream_rendersFirstFrameOnlyAfterStartPosition() throws Exception {
    ShadowLooper shadowLooper = shadowOf(testMainLooper);
    FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
    true, /* individualAllocationSize= */
    1024), /* mediaSourceEventDispatcher= */
    null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
    VIDEO_H264, 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= */
    VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
    1_000_000, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
    fakeSampleStream2.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream1, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.start();
    boolean replacedStream = false;
    for (int i = 0; i <= 10; i++) {
        mediaCodecVideoRenderer.render(/* positionUs= */
        i * 10, SystemClock.elapsedRealtime() * 1000);
        if (!replacedStream && mediaCodecVideoRenderer.hasReadStreamToEnd()) {
            mediaCodecVideoRenderer.replaceStream(new Format[] { VIDEO_H264 }, fakeSampleStream2, /* startPositionUs= */
            100, /* offsetUs= */
            100);
            replacedStream = true;
        }
    }
    // Expect only the first frame of the first stream to have been rendered.
    shadowLooper.idle();
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) ShadowLooper(org.robolectric.shadows.ShadowLooper) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)14 DefaultAllocator (com.google.android.exoplayer2.upstream.DefaultAllocator)14 FakeSampleStream (com.google.android.exoplayer2.testutil.FakeSampleStream)13 Format (com.google.android.exoplayer2.Format)5 MediaFormat (android.media.MediaFormat)4 MediaPeriodId (com.google.android.exoplayer2.source.MediaSource.MediaPeriodId)3 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)3 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)3 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)3 ShadowLooper (org.robolectric.shadows.ShadowLooper)3 Nullable (androidx.annotation.Nullable)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 C (com.google.android.exoplayer2.C)2 Timeline (com.google.android.exoplayer2.Timeline)2 DrmSessionManager (com.google.android.exoplayer2.drm.DrmSessionManager)2 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)2 FakeMediaPeriod (com.google.android.exoplayer2.testutil.FakeMediaPeriod)2 END_OF_STREAM_ITEM (com.google.android.exoplayer2.testutil.FakeSampleStream.FakeSampleStreamItem.END_OF_STREAM_ITEM)2 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)2