use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class CacheWriterTest method cache_afterFailureOnClose_succeeds.
@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
true);
FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
0, /* eventListener= */
null);
CachingCounters counters = new CachingCounters();
CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
null, counters);
// DataSink.close failing must cause the operation to fail rather than succeed.
assertThrows(IOException.class, cacheWriter::cache);
// Since all of the bytes were read through the DataSource chain successfully before the sink
// was closed, the progress listener will have seen all of the bytes being cached, even though
// this may not really be the case.
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
failOnClose.set(false);
// The bytes will be downloaded again, but cached successfully this time.
cacheWriter.cache();
counters.assertValues(/* bytesAlreadyCached= */
0, /* bytesNewlyCached= */
100, /* contentLength= */
100);
assertCachedData(cache, fakeDataSet);
}
use of com.google.android.exoplayer2.Player.Listener 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);
}
use of com.google.android.exoplayer2.Player.Listener 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.");
}
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class TestPlayerRunHelper method runUntilSleepingForOffload.
/**
* Runs tasks of the main {@link Looper} until {@link
* ExoPlayer.AudioOffloadListener#onExperimentalSleepingForOffloadChanged(boolean)} is called or a
* playback error occurs.
*
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
*
* @param player The {@link Player}.
* @param expectedSleepForOffload The expected sleep of offload state.
* @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
* exceeded.
*/
public static void runUntilSleepingForOffload(ExoPlayer player, boolean expectedSleepForOffload) throws TimeoutException {
verifyMainTestThread(player);
AtomicBoolean receiverCallback = new AtomicBoolean(false);
ExoPlayer.AudioOffloadListener listener = new ExoPlayer.AudioOffloadListener() {
@Override
public void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload) {
if (sleepingForOffload == expectedSleepForOffload) {
receiverCallback.set(true);
}
}
};
player.addAudioOffloadListener(listener);
runMainLooperUntil(() -> receiverCallback.get() || player.getPlayerError() != null);
if (player.getPlayerError() != null) {
throw new IllegalStateException(player.getPlayerError());
}
}
use of com.google.android.exoplayer2.Player.Listener in project ExoPlayer by google.
the class TestPlayerRunHelper method runUntilRenderedFirstFrame.
/**
* Runs tasks of the main {@link Looper} until the {@link Player.Listener#onRenderedFirstFrame}
* callback is called or a playback error occurs.
*
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}..
*
* @param player The {@link Player}.
* @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
* exceeded.
*/
public static void runUntilRenderedFirstFrame(ExoPlayer player) throws TimeoutException {
verifyMainTestThread(player);
AtomicBoolean receivedCallback = new AtomicBoolean(false);
Player.Listener listener = new Player.Listener() {
@Override
public void onRenderedFirstFrame() {
receivedCallback.set(true);
}
};
player.addListener(listener);
runMainLooperUntil(() -> receivedCallback.get() || player.getPlayerError() != null);
player.removeListener(listener);
if (player.getPlayerError() != null) {
throw new IllegalStateException(player.getPlayerError());
}
}
Aggregations