Search in sources :

Example 1 with RobolectricUtil.runMainLooperUntil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil in project ExoPlayer by google.

the class RtspClientTest method connectServerAndClient_serverDoesNotSupportDescribe_doesNotUpdateTimeline.

@Test
public void connectServerAndClient_serverDoesNotSupportDescribe_doesNotUpdateTimeline() throws Exception {
    AtomicBoolean clientHasSentDescribeRequest = new AtomicBoolean();
    class ResponseProvider implements RtspServer.ResponseProvider {

        @Override
        public RtspResponse getOptionsResponse() {
            return new RtspResponse(/* status= */
            200, new RtspHeaders.Builder().add(RtspHeaders.PUBLIC, "OPTIONS").build());
        }

        @Override
        public RtspResponse getDescribeResponse(Uri requestedUri, RtspHeaders headers) {
            clientHasSentDescribeRequest.set(true);
            return RtspTestUtils.RTSP_ERROR_METHOD_NOT_ALLOWED;
        }
    }
    rtspServer = new RtspServer(new ResponseProvider());
    AtomicReference<String> failureMessage = new AtomicReference<>();
    rtspClient = new RtspClient(new SessionInfoListener() {

        @Override
        public void onSessionTimelineUpdated(RtspSessionTiming timing, ImmutableList<RtspMediaTrack> tracks) {
        }

        @Override
        public void onSessionTimelineRequestFailed(String message, @Nullable Throwable cause) {
            failureMessage.set(message);
        }
    }, EMPTY_PLAYBACK_LISTENER, /* userAgent= */
    "ExoPlayer:RtspClientTest", RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()), SocketFactory.getDefault(), /* debugLoggingEnabled= */
    false);
    rtspClient.start();
    RobolectricUtil.runMainLooperUntil(() -> failureMessage.get() != null);
    assertThat(failureMessage.get()).contains("DESCRIBE not supported.");
    assertThat(clientHasSentDescribeRequest.get()).isFalse();
    assertThat(rtspClient.getState()).isEqualTo(RtspClient.RTSP_STATE_UNINITIALIZED);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Uri(android.net.Uri) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SessionInfoListener(com.google.android.exoplayer2.source.rtsp.RtspClient.SessionInfoListener) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 2 with RobolectricUtil.runMainLooperUntil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil in project ExoPlayer by google.

the class RtspMediaPeriodTest method prepareMediaPeriod_withWwwAuthentication_refreshesSourceInfoAndCallsOnPrepared.

@Test
public void prepareMediaPeriod_withWwwAuthentication_refreshesSourceInfoAndCallsOnPrepared() throws Exception {
    RtpPacketStreamDump rtpPacketStreamDump = RtspTestUtils.readRtpPacketStreamDump("media/rtsp/aac-dump.json");
    rtspServer = new RtspServer(new RtspServer.ResponseProvider() {

        @Override
        public RtspResponse getOptionsResponse() {
            return new RtspResponse(/* status= */
            200, new RtspHeaders.Builder().add(RtspHeaders.PUBLIC, "OPTIONS, DESCRIBE").build());
        }

        @Override
        public RtspResponse getDescribeResponse(Uri requestedUri, RtspHeaders headers) {
            String authorizationHeader = headers.get(RtspHeaders.AUTHORIZATION);
            if (authorizationHeader == null) {
                return new RtspResponse(/* status= */
                401, new RtspHeaders.Builder().add(RtspHeaders.CSEQ, headers.get(RtspHeaders.CSEQ)).add(RtspHeaders.WWW_AUTHENTICATE, "Digest realm=\"RTSP server\"," + " nonce=\"0cdfe9719e7373b7d5bb2913e2115f3f\"," + " opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"").add(RtspHeaders.WWW_AUTHENTICATE, "BASIC realm=\"WallyWorld\"").build());
            }
            if (!authorizationHeader.contains("Digest")) {
                return new RtspResponse(401, new RtspHeaders.Builder().add(RtspHeaders.CSEQ, headers.get(RtspHeaders.CSEQ)).build());
            }
            return RtspTestUtils.newDescribeResponseWithSdpMessage("v=0\r\n" + "o=- 1606776316530225 1 IN IP4 127.0.0.1\r\n" + "s=Exoplayer test\r\n" + "t=0 0\r\n" + // The session is 50.46s long.
            "a=range:npt=0-50.46\r\n", ImmutableList.of(rtpPacketStreamDump), requestedUri);
        }
    });
    AtomicBoolean prepareCallbackCalled = new AtomicBoolean();
    AtomicLong refreshedSourceDurationMs = new AtomicLong();
    mediaPeriod = new RtspMediaPeriod(new DefaultAllocator(/* trimOnReset= */
    true, C.DEFAULT_BUFFER_SEGMENT_SIZE), new TransferRtpDataChannelFactory(DEFAULT_TIMEOUT_MS), RtspTestUtils.getTestUriWithUserInfo("username", "password", rtspServer.startAndGetPortNumber()), /* listener= */
    timing -> refreshedSourceDurationMs.set(timing.getDurationMs()), /* userAgent= */
    "ExoPlayer:RtspPeriodTest", /* socketFactory= */
    SocketFactory.getDefault(), /* debugLoggingEnabled= */
    false);
    mediaPeriod.prepare(new MediaPeriod.Callback() {

        @Override
        public void onPrepared(MediaPeriod mediaPeriod) {
            prepareCallbackCalled.set(true);
        }

        @Override
        public void onContinueLoadingRequested(MediaPeriod source) {
            source.continueLoading(/* positionUs= */
            0);
        }
    }, /* positionUs= */
    0);
    RobolectricUtil.runMainLooperUntil(prepareCallbackCalled::get);
    mediaPeriod.release();
    assertThat(refreshedSourceDurationMs.get()).isEqualTo(50_460);
}
Also used : Util(com.google.android.exoplayer2.util.Util) Uri(android.net.Uri) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) SocketFactory(javax.net.SocketFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) RobolectricUtil(com.google.android.exoplayer2.robolectric.RobolectricUtil) ImmutableList(com.google.common.collect.ImmutableList) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) After(org.junit.After) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) C(com.google.android.exoplayer2.C) Uri(android.net.Uri) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) MediaPeriod(com.google.android.exoplayer2.source.MediaPeriod) Test(org.junit.Test)

Example 3 with RobolectricUtil.runMainLooperUntil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil in project ExoPlayer by google.

the class RtspPlaybackTest method prepare_noSupportedTrack_throwsPreparationError.

@Test
public void prepare_noSupportedTrack_throwsPreparationError() throws Exception {
    try (RtspServer rtspServer = new RtspServer(new ResponseProvider(clock, ImmutableList.of(mp4aLatmRtpPacketStreamDump), fakeRtpDataChannel))) {
        ExoPlayer player = createExoPlayer(rtspServer.startAndGetPortNumber(), rtpDataChannelFactory);
        AtomicReference<Throwable> playbackError = new AtomicReference<>();
        player.prepare();
        player.addListener(new Listener() {

            @Override
            public void onPlayerError(PlaybackException error) {
                playbackError.set(error);
            }
        });
        RobolectricUtil.runMainLooperUntil(() -> playbackError.get() != null);
        player.release();
        assertThat(playbackError.get()).hasCauseThat().hasMessageThat().contains("No playable track.");
    }
}
Also used : PlaybackException(com.google.android.exoplayer2.PlaybackException) Listener(com.google.android.exoplayer2.Player.Listener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Test(org.junit.Test)

Example 4 with RobolectricUtil.runMainLooperUntil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil in project ExoPlayer by google.

the class RobolectricUtilTest method runMainLooperUntil_withConditionAlreadyTrue_returnsImmediately.

@Test
public void runMainLooperUntil_withConditionAlreadyTrue_returnsImmediately() throws Exception {
    Clock mockClock = mock(Clock.class);
    RobolectricUtil.runMainLooperUntil(() -> true, /* timeoutMs= */
    0, mockClock);
    verify(mockClock, atMost(1)).currentTimeMillis();
}
Also used : Clock(com.google.android.exoplayer2.util.Clock) Test(org.junit.Test)

Example 5 with RobolectricUtil.runMainLooperUntil

use of com.google.android.exoplayer2.robolectric.RobolectricUtil.runMainLooperUntil in project ExoPlayer by google.

the class RobolectricUtilTest method runMainLooperUntil_withConditionThatNeverBecomesTrue_timesOut.

@Test
public void runMainLooperUntil_withConditionThatNeverBecomesTrue_timesOut() {
    Clock mockClock = mock(Clock.class);
    when(mockClock.currentTimeMillis()).thenReturn(0L, 41L, 42L);
    assertThrows(TimeoutException.class, () -> RobolectricUtil.runMainLooperUntil(() -> false, /* timeoutMs= */
    42, mockClock));
    verify(mockClock, times(3)).currentTimeMillis();
}
Also used : Clock(com.google.android.exoplayer2.util.Clock) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Uri (android.net.Uri)4 ImmutableList (com.google.common.collect.ImmutableList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SocketFactory (javax.net.SocketFactory)3 Nullable (androidx.annotation.Nullable)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 C (com.google.android.exoplayer2.C)2 RobolectricUtil (com.google.android.exoplayer2.robolectric.RobolectricUtil)2 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)2 SessionInfoListener (com.google.android.exoplayer2.source.rtsp.RtspClient.SessionInfoListener)2 DefaultAllocator (com.google.android.exoplayer2.upstream.DefaultAllocator)2 Clock (com.google.android.exoplayer2.util.Clock)2 Util (com.google.android.exoplayer2.util.Util)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 After (org.junit.After)2 RunWith (org.junit.runner.RunWith)2 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1