Search in sources :

Example 11 with EventListener

use of com.google.android.exoplayer2.source.ads.AdsLoader.EventListener in project ExoPlayer by google.

the class DecoderVideoRendererTest method setUp.

@Before
public void setUp() {
    surface = new Surface(new SurfaceTexture(/* texName= */
    0));
    renderer = new DecoderVideoRenderer(/* allowedJoiningTimeMs= */
    0, new Handler(), eventListener, /* maxDroppedFramesToNotify= */
    -1) {

        private final Phaser inputBuffersInCodecPhaser = new Phaser();

        @C.VideoOutputMode
        private int outputMode;

        @Override
        public String getName() {
            return "TestVideoRenderer";
        }

        @Override
        @Capabilities
        public int supportsFormat(Format format) {
            return RendererCapabilities.create(C.FORMAT_HANDLED);
        }

        @Override
        protected void setDecoderOutputMode(@C.VideoOutputMode int outputMode) {
            this.outputMode = outputMode;
        }

        @Override
        protected void renderOutputBufferToSurface(VideoDecoderOutputBuffer outputBuffer, Surface surface) {
        // Do nothing.
        }

        @Override
        protected void onQueueInputBuffer(DecoderInputBuffer buffer) {
            // Decoding is done on a background thread we have no control about from the test.
            // Ensure the background calls are predictably serialized by waiting for them to finish:
            // 1. Register queued input buffers here.
            // 2. Deregister the input buffer when it's cleared. If an input buffer is cleared it
            // will have been fully handled by the decoder.
            // 3. Send a message on the test thread to wait for all currently pending input buffers
            // to be cleared.
            // 4. The tests need to call ShadowLooper.idleMainThread() to execute the wait message
            // sent in step (3).
            int currentPhase = inputBuffersInCodecPhaser.register();
            new Handler().post(() -> inputBuffersInCodecPhaser.awaitAdvance(currentPhase));
            super.onQueueInputBuffer(buffer);
        }

        @Override
        protected SimpleDecoder<DecoderInputBuffer, ? extends VideoDecoderOutputBuffer, ? extends DecoderException> createDecoder(Format format, @Nullable CryptoConfig cryptoConfig) {
            return new SimpleDecoder<DecoderInputBuffer, VideoDecoderOutputBuffer, DecoderException>(new DecoderInputBuffer[10], new VideoDecoderOutputBuffer[10]) {

                @Override
                protected DecoderInputBuffer createInputBuffer() {
                    return new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DIRECT) {

                        @Override
                        public void clear() {
                            super.clear();
                            inputBuffersInCodecPhaser.arriveAndDeregister();
                        }
                    };
                }

                @Override
                protected VideoDecoderOutputBuffer createOutputBuffer() {
                    return new VideoDecoderOutputBuffer(this::releaseOutputBuffer);
                }

                @Override
                protected DecoderException createUnexpectedDecodeException(Throwable error) {
                    return new DecoderException("error", error);
                }

                @Nullable
                @Override
                protected DecoderException decode(DecoderInputBuffer inputBuffer, VideoDecoderOutputBuffer outputBuffer, boolean reset) {
                    outputBuffer.init(inputBuffer.timeUs, outputMode, /* supplementalData= */
                    null);
                    return null;
                }

                @Override
                public String getName() {
                    return "TestDecoder";
                }
            };
        }
    };
    renderer.setOutput(surface);
}
Also used : SimpleDecoder(com.google.android.exoplayer2.decoder.SimpleDecoder) Handler(android.os.Handler) Surface(android.view.Surface) VideoDecoderOutputBuffer(com.google.android.exoplayer2.decoder.VideoDecoderOutputBuffer) DecoderException(com.google.android.exoplayer2.decoder.DecoderException) SurfaceTexture(android.graphics.SurfaceTexture) Format(com.google.android.exoplayer2.Format) DecoderInputBuffer(com.google.android.exoplayer2.decoder.DecoderInputBuffer) RendererCapabilities(com.google.android.exoplayer2.RendererCapabilities) Phaser(java.util.concurrent.Phaser) CryptoConfig(com.google.android.exoplayer2.decoder.CryptoConfig) Nullable(androidx.annotation.Nullable) Before(org.junit.Before)

Example 12 with EventListener

use of com.google.android.exoplayer2.source.ads.AdsLoader.EventListener in project ExoPlayer by google.

the class DecoderVideoRendererTest method replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream.

// TODO: Fix rendering of first frame at stream transition.
@Ignore
@Test
public void replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream() 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);
    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).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
    // Render to streamOffsetUs and verify the new first frame gets rendered.
    renderer.render(/* positionUs= */
    100, SystemClock.elapsedRealtime() * 1000);
    verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with EventListener

use of com.google.android.exoplayer2.source.ads.AdsLoader.EventListener in project ExoPlayer by google.

the class DecoderVideoRendererTest method enable_withMayRenderStartOfStream_rendersFirstFrameBeforeStart.

@Test
public void enable_withMayRenderStartOfStream_rendersFirstFrameBeforeStart() 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= */
    true, /* startPositionUs= */
    0L, /* 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).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
    anyLong());
}
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 14 with EventListener

use of com.google.android.exoplayer2.source.ads.AdsLoader.EventListener in project ExoPlayer by google.

the class MediaCodecVideoRendererTest method render_dropsLateBuffer.

@Test
public void render_dropsLateBuffer() 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(// First buffer.
    oneByteSample(/* timeUs= */
    0, C.BUFFER_FLAG_KEY_FRAME), // Late buffer.
    oneByteSample(/* timeUs= */
    50_000), // Last buffer.
    oneByteSample(/* timeUs= */
    100_000), END_OF_STREAM_ITEM));
    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(0, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.render(40_000, SystemClock.elapsedRealtime() * 1000);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    // Ensures buffer will be 30_001us late.
    int posUs = 80_001;
    while (!mediaCodecVideoRenderer.isEnded()) {
        mediaCodecVideoRenderer.render(posUs, SystemClock.elapsedRealtime() * 1000);
        posUs += 40_000;
    }
    shadowOf(testMainLooper).idle();
    verify(eventListener).onDroppedFrames(eq(1), anyLong());
}
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 EventListener

use of com.google.android.exoplayer2.source.ads.AdsLoader.EventListener in project ExoPlayer by google.

the class MediaCodecVideoRendererTest method render_sendsVideoSizeChangeWithCurrentFormatValues.

@Test
public void render_sendsVideoSizeChangeWithCurrentFormatValues() 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), END_OF_STREAM_ITEM));
    fakeSampleStream.writeData(/* startPositionUs= */
    0);
    mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream, /* positionUs= */
    0, /* joining= */
    false, /* mayRenderStartOfStream= */
    true, /* startPositionUs= */
    0, /* offsetUs */
    0);
    mediaCodecVideoRenderer.setCurrentStreamFinal();
    mediaCodecVideoRenderer.start();
    int positionUs = 0;
    do {
        mediaCodecVideoRenderer.render(positionUs, SystemClock.elapsedRealtime() * 1000);
        positionUs += 10;
    } while (!mediaCodecVideoRenderer.isEnded());
    shadowOf(testMainLooper).idle();
    verify(eventListener).onVideoSizeChanged(new VideoSize(VIDEO_H264.width, VIDEO_H264.height, VIDEO_H264.rotationDegrees, VIDEO_H264.pixelWidthHeightRatio));
}
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)

Aggregations

DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)18 Test (org.junit.Test)18 FakeSampleStream (com.google.android.exoplayer2.testutil.FakeSampleStream)17 DefaultAllocator (com.google.android.exoplayer2.upstream.DefaultAllocator)16 Nullable (androidx.annotation.Nullable)12 Collections (java.util.Collections)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Before (org.junit.Before)6 Context (android.content.Context)5 Intent (android.content.Intent)5 SurfaceTexture (android.graphics.SurfaceTexture)5 Uri (android.net.Uri)5 ShadowLooper (org.robolectric.shadows.ShadowLooper)5 ValueAnimator (android.animation.ValueAnimator)4 SuppressLint (android.annotation.SuppressLint)4 ActivityNotFoundException (android.content.ActivityNotFoundException)4 Color (android.graphics.Color)4 PorterDuff (android.graphics.PorterDuff)4