Search in sources :

Example 6 with Callback

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

the class ProgressiveMediaPeriod method onLoadCanceled.

@Override
public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, boolean released) {
    StatsDataSource dataSource = loadable.dataSource;
    LoadEventInfo loadEventInfo = new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, dataSource.getLastOpenedUri(), dataSource.getLastResponseHeaders(), elapsedRealtimeMs, loadDurationMs, dataSource.getBytesRead());
    loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
    mediaSourceEventDispatcher.loadCanceled(loadEventInfo, C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, /* trackFormat= */
    null, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
    null, /* mediaStartTimeUs= */
    loadable.seekTimeUs, durationUs);
    if (!released) {
        copyLengthFromLoader(loadable);
        for (SampleQueue sampleQueue : sampleQueues) {
            sampleQueue.reset();
        }
        if (enabledTrackCount > 0) {
            Assertions.checkNotNull(callback).onContinueLoadingRequested(this);
        }
    }
}
Also used : StatsDataSource(com.google.android.exoplayer2.upstream.StatsDataSource)

Example 7 with Callback

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

the class ProgressiveMediaPeriod method onLoadCompleted.

// Loader.Callback implementation.
@Override
public void onLoadCompleted(ExtractingLoadable loadable, long elapsedRealtimeMs, long loadDurationMs) {
    if (durationUs == C.TIME_UNSET && seekMap != null) {
        boolean isSeekable = seekMap.isSeekable();
        long largestQueuedTimestampUs = getLargestQueuedTimestampUs();
        durationUs = largestQueuedTimestampUs == Long.MIN_VALUE ? 0 : largestQueuedTimestampUs + DEFAULT_LAST_SAMPLE_DURATION_US;
        listener.onSourceInfoRefreshed(durationUs, isSeekable, isLive);
    }
    StatsDataSource dataSource = loadable.dataSource;
    LoadEventInfo loadEventInfo = new LoadEventInfo(loadable.loadTaskId, loadable.dataSpec, dataSource.getLastOpenedUri(), dataSource.getLastResponseHeaders(), elapsedRealtimeMs, loadDurationMs, dataSource.getBytesRead());
    loadErrorHandlingPolicy.onLoadTaskConcluded(loadable.loadTaskId);
    mediaSourceEventDispatcher.loadCompleted(loadEventInfo, C.DATA_TYPE_MEDIA, C.TRACK_TYPE_UNKNOWN, /* trackFormat= */
    null, C.SELECTION_REASON_UNKNOWN, /* trackSelectionData= */
    null, /* mediaStartTimeUs= */
    loadable.seekTimeUs, durationUs);
    copyLengthFromLoader(loadable);
    loadingFinished = true;
    Assertions.checkNotNull(callback).onContinueLoadingRequested(this);
}
Also used : StatsDataSource(com.google.android.exoplayer2.upstream.StatsDataSource)

Example 8 with Callback

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

the class ChunkSampleStream method release.

/**
 * Releases the stream.
 *
 * <p>This method should be called when the stream is no longer required. Either this method or
 * {@link #release()} can be used to release this stream.
 *
 * @param callback An optional callback to be called on the loading thread once the loader has
 *     been released.
 */
public void release(@Nullable ReleaseCallback<T> callback) {
    this.releaseCallback = callback;
    // Discard as much as we can synchronously.
    primarySampleQueue.preRelease();
    for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
        embeddedSampleQueue.preRelease();
    }
    loader.release(this);
}
Also used : SampleQueue(com.google.android.exoplayer2.source.SampleQueue)

Example 9 with Callback

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

the class ExoPlayerTest method recursivePlayerChangesAreReportedInCorrectOrder.

@Test
public void recursivePlayerChangesAreReportedInCorrectOrder() throws Exception {
    // The listener stops the player as soon as it's ready (which should report a timeline and state
    // change) and sets playWhenReady to false when the timeline callback is received.
    final AtomicReference<Player> playerReference = new AtomicReference<>();
    final List<Boolean> playerListenerPlayWhenReady = new ArrayList<>();
    final List<Integer> playerListenerStates = new ArrayList<>();
    List<Integer> sequence = new ArrayList<>();
    final Player.Listener playerListener = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListenerStates.add(playbackState);
            if (playbackState == Player.STATE_READY) {
                playerReference.get().stop(/* reset= */
                true);
                sequence.add(0);
            }
        }

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            if (timeline.isEmpty()) {
                playerReference.get().pause();
                sequence.add(1);
            }
        }

        @Override
        public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
            playerListenerPlayWhenReady.add(playWhenReady);
            sequence.add(2);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener);
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(playerListenerStates).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
    assertThat(playerListenerPlayWhenReady).containsExactly(false).inOrder();
    assertThat(sequence).containsExactly(0, 1, 2).inOrder();
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) Listener(com.google.android.exoplayer2.Player.Listener) MediaSourceEventListener(com.google.android.exoplayer2.source.MediaSourceEventListener) AnalyticsListener(com.google.android.exoplayer2.analytics.AnalyticsListener) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) ActionSchedule(com.google.android.exoplayer2.testutil.ActionSchedule) PlayerRunnable(com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Listener(com.google.android.exoplayer2.Player.Listener) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 10 with Callback

use of com.google.android.exoplayer2.source.MediaPeriod.Callback 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());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Player(com.google.android.exoplayer2.Player) ExoPlayer(com.google.android.exoplayer2.ExoPlayer)

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