use of com.google.android.exoplayer2.source.MediaPeriod.Callback in project ExoPlayer by google.
the class TestPlayerRunHelper method playUntilPosition.
/**
* Calls {@link Player#play()}, runs tasks of the main {@link Looper} until the {@code player}
* reaches the specified position or a playback error occurs, and then pauses the {@code player}.
*
* <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
*
* @param player The {@link Player}.
* @param mediaItemIndex The index of the media item.
* @param positionMs The position within the media item, in milliseconds.
* @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
* exceeded.
*/
public static void playUntilPosition(ExoPlayer player, int mediaItemIndex, long positionMs) throws TimeoutException {
verifyMainTestThread(player);
Looper applicationLooper = Util.getCurrentOrMainLooper();
AtomicBoolean messageHandled = new AtomicBoolean(false);
player.createMessage((messageType, payload) -> {
// Block playback thread until pause command has been sent from test thread.
ConditionVariable blockPlaybackThreadCondition = new ConditionVariable();
player.getClock().createHandler(applicationLooper, /* callback= */
null).post(() -> {
player.pause();
messageHandled.set(true);
blockPlaybackThreadCondition.open();
});
try {
player.getClock().onThreadBlocked();
blockPlaybackThreadCondition.block();
} catch (InterruptedException e) {
// Ignore.
}
}).setPosition(mediaItemIndex, positionMs).send();
player.play();
runMainLooperUntil(() -> messageHandled.get() || player.getPlayerError() != null);
if (player.getPlayerError() != null) {
throw new IllegalStateException(player.getPlayerError());
}
}
use of com.google.android.exoplayer2.source.MediaPeriod.Callback in project ExoPlayer by google.
the class FakeAdaptiveMediaPeriod method prepare.
@Override
public void prepare(Callback callback, long positionUs) {
mediaSourceEventDispatcher.loadStarted(new LoadEventInfo(fakePreparationLoadTaskId, FAKE_DATA_SPEC, SystemClock.elapsedRealtime()), C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, /* trackFormat= */
null, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
null, /* mediaStartTimeUs= */
0, /* mediaEndTimeUs = */
C.TIME_UNSET);
this.callback = callback;
prepared = true;
Util.castNonNull(this.callback).onPrepared(this);
mediaSourceEventDispatcher.loadCompleted(new LoadEventInfo(fakePreparationLoadTaskId, FAKE_DATA_SPEC, FAKE_DATA_SPEC.uri, /* responseHeaders= */
ImmutableMap.of(), SystemClock.elapsedRealtime(), /* loadDurationMs= */
0, /* bytesLoaded= */
100), C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, /* trackFormat= */
null, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
null, /* mediaStartTimeUs= */
0, /* mediaEndTimeUs = */
C.TIME_UNSET);
}
use of com.google.android.exoplayer2.source.MediaPeriod.Callback in project ExoPlayer by google.
the class DefaultAnalyticsCollector method onPositionDiscontinuity.
// Calling deprecated callback.
@SuppressWarnings("deprecation")
@Override
public final void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
if (reason == Player.DISCONTINUITY_REASON_SEEK) {
isSeeking = false;
}
mediaPeriodQueueTracker.onPositionDiscontinuity(checkNotNull(player));
EventTime eventTime = generateCurrentPlayerMediaPeriodEventTime();
sendEvent(eventTime, AnalyticsListener.EVENT_POSITION_DISCONTINUITY, listener -> {
listener.onPositionDiscontinuity(eventTime, reason);
listener.onPositionDiscontinuity(eventTime, oldPosition, newPosition, reason);
});
}
use of com.google.android.exoplayer2.source.MediaPeriod.Callback in project ExoPlayer by google.
the class DownloadHelperTest method prepareDownloadHelper.
private static void prepareDownloadHelper(DownloadHelper downloadHelper) throws Exception {
AtomicReference<Exception> prepareException = new AtomicReference<>(null);
CountDownLatch preparedLatch = new CountDownLatch(1);
downloadHelper.prepare(new Callback() {
@Override
public void onPrepared(DownloadHelper helper) {
preparedLatch.countDown();
}
@Override
public void onPrepareError(DownloadHelper helper, IOException e) {
prepareException.set(e);
preparedLatch.countDown();
}
});
while (!preparedLatch.await(0, MILLISECONDS)) {
shadowMainLooper().idleFor(shadowMainLooper().getNextScheduledTaskTime());
}
if (prepareException.get() != null) {
throw prepareException.get();
}
}
use of com.google.android.exoplayer2.source.MediaPeriod.Callback in project ExoPlayer by google.
the class ExoPlayerTest method infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException.
@Test
public void infiniteLoading_withSmallAllocations_oomIsPreventedByLoadControl_andThrowsStuckBufferingIllegalStateException() {
DefaultLoadControl loadControl = new DefaultLoadControl.Builder().setTargetBufferBytes(10 * C.DEFAULT_BUFFER_SEGMENT_SIZE).build();
// Return no end of stream signal to prevent playback from ending.
FakeMediaPeriod.TrackDataFactory trackDataWithoutEos = (format, periodId) -> ImmutableList.of();
MediaSource continuouslyAllocatingMediaSource = new FakeMediaSource(new FakeTimeline(), ExoPlayerTestRunner.VIDEO_FORMAT) {
@Override
protected MediaPeriod createMediaPeriod(MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, @Nullable TransferListener transferListener) {
return new FakeMediaPeriod(trackGroupArray, allocator, trackDataWithoutEos, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
false) {
private final List<Allocation> allocations = new ArrayList<>();
private Callback callback;
@Override
public synchronized void prepare(Callback callback, long positionUs) {
this.callback = callback;
super.prepare(callback, positionUs);
}
@Override
public long getBufferedPositionUs() {
// Pretend not to make loading progress, so that continueLoading keeps being called.
return 0;
}
@Override
public long getNextLoadPositionUs() {
// Pretend not to make loading progress, so that continueLoading keeps being called.
return 0;
}
@Override
public boolean continueLoading(long positionUs) {
allocations.add(allocator.allocate());
callback.onContinueLoadingRequested(this);
return true;
}
};
}
};
ExoPlayerTestRunner testRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(continuouslyAllocatingMediaSource).setLoadControl(loadControl).build();
ExoPlaybackException exception = assertThrows(ExoPlaybackException.class, () -> testRunner.start().blockUntilEnded(TIMEOUT_MS));
assertThat(exception.type).isEqualTo(ExoPlaybackException.TYPE_UNEXPECTED);
assertThat(exception.getUnexpectedException()).isInstanceOf(IllegalStateException.class);
}
Aggregations