Search in sources :

Example 36 with Callback

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

the class CastPlayerTest method setMediaItems_replaceExistingPlaylist_notifiesPositionDiscontinuity.

// Verifies deprecated callback being called correctly.
@SuppressWarnings("deprecation")
@Test
public void setMediaItems_replaceExistingPlaylist_notifiesPositionDiscontinuity() {
    List<MediaItem> firstPlaylist = new ArrayList<>();
    String uri1 = "http://www.google.com/video1";
    String uri2 = "http://www.google.com/video2";
    firstPlaylist.add(new MediaItem.Builder().setUri(uri1).setMimeType(MimeTypes.APPLICATION_MPD).build());
    firstPlaylist.add(new MediaItem.Builder().setUri(uri2).setMimeType(MimeTypes.APPLICATION_MP4).build());
    ImmutableList<MediaItem> secondPlaylist = ImmutableList.of(new MediaItem.Builder().setUri(Uri.EMPTY).setMimeType(MimeTypes.APPLICATION_MPD).build());
    castPlayer.setMediaItems(firstPlaylist, /* startWindowIndex= */
    1, /* startPositionMs= */
    2000L);
    updateTimeLine(firstPlaylist, /* mediaQueueItemIds= */
    new int[] { 1, 2 }, /* currentItemId= */
    2, /* streamTypes= */
    new int[] { MediaInfo.STREAM_TYPE_BUFFERED, MediaInfo.STREAM_TYPE_BUFFERED }, /* durationsMs= */
    new long[] { 20_000, 20_000 }, /* positionMs= */
    2000L);
    // Replacing existing playlist.
    castPlayer.setMediaItems(secondPlaylist, /* startWindowIndex= */
    0, /* startPositionMs= */
    1000L);
    updateTimeLine(secondPlaylist, /* mediaQueueItemIds= */
    new int[] { 3 }, /* currentItemId= */
    3, /* streamTypes= */
    new int[] { MediaInfo.STREAM_TYPE_BUFFERED }, /* durationsMs= */
    new long[] { 20_000 }, /* positionMs= */
    1000L);
    Player.PositionInfo oldPosition = new Player.PositionInfo(/* windowUid= */
    2, /* windowIndex= */
    1, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(2).build(), /* periodUid= */
    2, /* periodIndex= */
    1, /* positionMs= */
    2000, /* contentPositionMs= */
    2000, /* adGroupIndex= */
    C.INDEX_UNSET, /* adIndexInAdGroup= */
    C.INDEX_UNSET);
    Player.PositionInfo newPosition = new Player.PositionInfo(/* windowUid= */
    3, /* windowIndex= */
    0, new MediaItem.Builder().setUri(Uri.EMPTY).setTag(3).build(), /* periodUid= */
    3, /* periodIndex= */
    0, /* positionMs= */
    1000, /* contentPositionMs= */
    1000, /* adGroupIndex= */
    C.INDEX_UNSET, /* adIndexInAdGroup= */
    C.INDEX_UNSET);
    InOrder inOrder = Mockito.inOrder(mockListener);
    inOrder.verify(mockListener).onPositionDiscontinuity(eq(DISCONTINUITY_REASON_REMOVE));
    inOrder.verify(mockListener).onPositionDiscontinuity(eq(oldPosition), eq(newPosition), eq(DISCONTINUITY_REASON_REMOVE));
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(anyInt());
    inOrder.verify(mockListener, never()).onPositionDiscontinuity(any(), any(), anyInt());
}
Also used : Player(com.google.android.exoplayer2.Player) InOrder(org.mockito.InOrder) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 37 with Callback

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

the class DashMediaSource method startLoading.

private <T> void startLoading(ParsingLoadable<T> loadable, Loader.Callback<ParsingLoadable<T>> callback, int minRetryCount) {
    long elapsedRealtimeMs = loader.startLoading(loadable, callback, minRetryCount);
    manifestEventDispatcher.loadStarted(new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, elapsedRealtimeMs), loadable.type);
}
Also used : LoadEventInfo(com.google.android.exoplayer2.source.LoadEventInfo)

Example 38 with Callback

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

the class FakeClockTest method createHandler_withIsAutoAdvancing_advancesTimeToNextMessages.

@Test
public void createHandler_withIsAutoAdvancing_advancesTimeToNextMessages() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), /* callback= */
    null);
    // Post a series of immediate and delayed messages.
    ArrayList<Long> clockTimes = new ArrayList<>();
    handler.post(() -> {
        handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
        100);
        handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
        50);
        handler.post(() -> clockTimes.add(fakeClock.elapsedRealtime()));
        handler.postDelayed(() -> {
            clockTimes.add(fakeClock.elapsedRealtime());
            handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
            50);
        }, /* delayMs= */
        20);
    });
    ShadowLooper.idleMainLooper();
    shadowOf(handler.getLooper()).idle();
    assertThat(clockTimes).containsExactly(0L, 20L, 50L, 70L, 100L).inOrder();
}
Also used : HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 39 with Callback

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

the class FakeClockTest method createHandler_blockingThreadWithOnBusyWaiting_canBeUnblockedByOtherThread.

@Test
public void createHandler_blockingThreadWithOnBusyWaiting_canBeUnblockedByOtherThread() {
    HandlerThread handlerThread1 = new HandlerThread("FakeClockTest");
    handlerThread1.start();
    HandlerThread handlerThread2 = new HandlerThread("FakeClockTest");
    handlerThread2.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    HandlerWrapper handler1 = fakeClock.createHandler(handlerThread1.getLooper(), /* callback= */
    null);
    HandlerWrapper handler2 = fakeClock.createHandler(handlerThread2.getLooper(), /* callback= */
    null);
    ArrayList<Integer> executionOrder = new ArrayList<>();
    handler1.post(() -> {
        executionOrder.add(1);
        ConditionVariable blockingCondition = new ConditionVariable();
        handler2.postDelayed(() -> {
            executionOrder.add(2);
            blockingCondition.open();
        }, /* delayMs= */
        50);
        handler1.post(() -> executionOrder.add(4));
        fakeClock.onThreadBlocked();
        blockingCondition.block();
        executionOrder.add(3);
    });
    ShadowLooper.idleMainLooper();
    shadowOf(handler1.getLooper()).idle();
    shadowOf(handler2.getLooper()).idle();
    assertThat(executionOrder).containsExactly(1, 2, 3, 4).inOrder();
}
Also used : ConditionVariable(android.os.ConditionVariable) HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 40 with Callback

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

the class FakeClockTest method createHandler_removeAllMessages_removesAllMessages.

@Test
public void createHandler_removeAllMessages_removesAllMessages() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0);
    TestCallback callback = new TestCallback();
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), callback);
    TestCallback otherCallback = new TestCallback();
    HandlerWrapper otherHandler = fakeClock.createHandler(handlerThread.getLooper(), otherCallback);
    TestRunnable testRunnable1 = new TestRunnable();
    TestRunnable testRunnable2 = new TestRunnable();
    Object messageToken = new Object();
    handler.obtainMessage(/* what= */
    1, /* obj= */
    messageToken).sendToTarget();
    handler.sendEmptyMessageDelayed(/* what= */
    2, /* delayMs= */
    50);
    handler.post(testRunnable1);
    handler.postDelayed(testRunnable2, /* delayMs= */
    25);
    handler.sendEmptyMessage(/* what= */
    3);
    otherHandler.sendEmptyMessage(/* what= */
    1);
    handler.removeCallbacksAndMessages(/* token= */
    null);
    fakeClock.advanceTime(50);
    ShadowLooper.idleMainLooper();
    shadowOf(handlerThread.getLooper()).idle();
    assertThat(callback.messages).isEmpty();
    assertThat(testRunnable1.hasRun).isFalse();
    assertThat(testRunnable2.hasRun).isFalse();
    // Assert that message on other handler wasn't removed.
    assertThat(otherCallback.messages).containsExactly(new MessageData(/* what= */
    1, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null));
}
Also used : HandlerThread(android.os.HandlerThread) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 ArrayList (java.util.ArrayList)10 HandlerThread (android.os.HandlerThread)9 Nullable (androidx.annotation.Nullable)9 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)9 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)9 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)8 PlaybackParameters (com.google.android.exoplayer2.PlaybackParameters)7 Player (com.google.android.exoplayer2.Player)6 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)6 ApplicationProvider (androidx.test.core.app.ApplicationProvider)5 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)5 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)5 MediaSource (com.google.android.exoplayer2.source.MediaSource)5 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 RunWith (org.junit.runner.RunWith)5 LoadEventInfo (com.google.android.exoplayer2.source.LoadEventInfo)4 ImmutableList (com.google.common.collect.ImmutableList)4 SurfaceTexture (android.graphics.SurfaceTexture)3