Search in sources :

Example 6 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class DrmPlaybackTest method clearkeyPlayback.

@Test
public void clearkeyPlayback() throws Exception {
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(CLEARKEY_RESPONSE));
    mockWebServer.start();
    MediaItem mediaItem = new MediaItem.Builder().setUri("asset:///media/drm/sample_fragmented_clearkey.mp4").setDrmConfiguration(new MediaItem.DrmConfiguration.Builder(C.CLEARKEY_UUID).setLicenseUri(mockWebServer.url("license").toString()).build()).build();
    AtomicReference<ExoPlayer> player = new AtomicReference<>();
    ConditionVariable playbackComplete = new ConditionVariable();
    AtomicReference<PlaybackException> playbackException = new AtomicReference<>();
    getInstrumentation().runOnMainSync(() -> {
        player.set(new ExoPlayer.Builder(getInstrumentation().getContext()).build());
        player.get().addListener(new Player.Listener() {

            @Override
            public void onPlaybackStateChanged(@Player.State int playbackState) {
                if (playbackState == Player.STATE_ENDED) {
                    playbackComplete.open();
                }
            }

            @Override
            public void onPlayerError(PlaybackException error) {
                playbackException.set(error);
                playbackComplete.open();
            }
        });
        player.get().setMediaItem(mediaItem);
        player.get().prepare();
        player.get().play();
    });
    playbackComplete.block();
    getInstrumentation().runOnMainSync(() -> player.get().release());
    getInstrumentation().waitForIdleSync();
    assertThat(playbackException.get()).isNull();
}
Also used : PlaybackException(com.google.android.exoplayer2.PlaybackException) MockResponse(okhttp3.mockwebserver.MockResponse) Player(com.google.android.exoplayer2.Player) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) MediaItem(com.google.android.exoplayer2.MediaItem) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.junit.Test)

Example 7 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class CronetDataSourceTest method connectTimeout.

@Test
public void connectTimeout() throws InterruptedException {
    long startTimeMs = SystemClock.elapsedRealtime();
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final CountDownLatch timedOutLatch = new CountDownLatch(1);
    new Thread() {

        @Override
        public void run() {
            try {
                dataSourceUnderTest.open(testDataSpec);
                fail();
            } catch (HttpDataSourceException e) {
                // Expected.
                assertThat(e).isInstanceOf(CronetDataSource.OpenException.class);
                assertThat(e).hasCauseThat().isInstanceOf(SocketTimeoutException.class);
                assertThat(((CronetDataSource.OpenException) e).cronetConnectionStatus).isEqualTo(TEST_CONNECTION_STATUS);
                timedOutLatch.countDown();
            }
        }
    }.start();
    startCondition.block();
    // We should still be trying to open.
    assertNotCountedDown(timedOutLatch);
    // We should still be trying to open as we approach the timeout.
    setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
    startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
    assertNotCountedDown(timedOutLatch);
    // Now we timeout.
    setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
    startTimeMs + TEST_CONNECT_TIMEOUT_MS + 10);
    timedOutLatch.await();
    verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true);
}
Also used : ConditionVariable(android.os.ConditionVariable) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class CronetDataSourceTest method connectInterrupted.

@Test
public void connectInterrupted() throws InterruptedException {
    long startTimeMs = SystemClock.elapsedRealtime();
    final ConditionVariable startCondition = buildUrlRequestStartedCondition();
    final CountDownLatch timedOutLatch = new CountDownLatch(1);
    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                dataSourceUnderTest.open(testDataSpec);
                fail();
            } catch (HttpDataSourceException e) {
                // Expected.
                assertThat(e).isInstanceOf(CronetDataSource.OpenException.class);
                assertThat(e).hasCauseThat().isInstanceOf(InterruptedIOException.class);
                assertThat(((CronetDataSource.OpenException) e).cronetConnectionStatus).isEqualTo(TEST_INVALID_CONNECTION_STATUS);
                timedOutLatch.countDown();
            }
        }
    };
    thread.start();
    startCondition.block();
    // We should still be trying to open.
    assertNotCountedDown(timedOutLatch);
    // We should still be trying to open as we approach the timeout.
    setSystemClockInMsAndTriggerPendingMessages(/* nowMs= */
    startTimeMs + TEST_CONNECT_TIMEOUT_MS - 1);
    assertNotCountedDown(timedOutLatch);
    // Now we interrupt.
    thread.interrupt();
    timedOutLatch.await();
    verify(mockTransferListener, never()).onTransferStart(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true);
}
Also used : ConditionVariable(android.os.ConditionVariable) InterruptedIOException(java.io.InterruptedIOException) HttpDataSourceException(com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class RobolectricUtilTest method createRobolectricConditionVariable_blockWithTimeout_blocksForAtLeastTimeout.

@Test
public void createRobolectricConditionVariable_blockWithTimeout_blocksForAtLeastTimeout() throws InterruptedException {
    ConditionVariable conditionVariable = RobolectricUtil.createRobolectricConditionVariable();
    long startTimeMs = System.currentTimeMillis();
    assertThat(conditionVariable.block(/* timeoutMs= */
    500)).isFalse();
    long endTimeMs = System.currentTimeMillis();
    assertThat(endTimeMs - startTimeMs).isAtLeast(500);
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Test(org.junit.Test)

Example 10 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class RobolectricUtilTest method createRobolectricConditionVariable_blockWithTimeout_timesOut.

@Test
public void createRobolectricConditionVariable_blockWithTimeout_timesOut() throws InterruptedException {
    ConditionVariable conditionVariable = RobolectricUtil.createRobolectricConditionVariable();
    assertThat(conditionVariable.block(/* timeoutMs= */
    1)).isFalse();
    assertThat(conditionVariable.isOpen()).isFalse();
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 ConditionVariable (android.os.ConditionVariable)12 HttpDataSourceException (com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException)9 ConditionVariable (com.google.android.exoplayer2.util.ConditionVariable)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 InterruptedIOException (java.io.InterruptedIOException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HandlerThread (android.os.HandlerThread)2 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)2 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Handler (android.os.Handler)1 Looper (android.os.Looper)1 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1 MediaItem (com.google.android.exoplayer2.MediaItem)1 PlaybackException (com.google.android.exoplayer2.PlaybackException)1 Player (com.google.android.exoplayer2.Player)1 RobolectricUtil.createRobolectricConditionVariable (com.google.android.exoplayer2.robolectric.RobolectricUtil.createRobolectricConditionVariable)1