Search in sources :

Example 1 with PlaybackParameters

use of androidx.media3.common.PlaybackParameters in project media by androidx.

the class CastPlayer method setPlaybackParameters.

@Override
public void setPlaybackParameters(PlaybackParameters playbackParameters) {
    if (remoteMediaClient == null) {
        return;
    }
    PlaybackParameters actualPlaybackParameters = new PlaybackParameters(Util.constrainValue(playbackParameters.speed, MIN_SPEED_SUPPORTED, MAX_SPEED_SUPPORTED));
    setPlaybackParametersAndNotifyIfChanged(actualPlaybackParameters);
    listeners.flushEvents();
    PendingResult<MediaChannelResult> pendingResult = remoteMediaClient.setPlaybackRate(actualPlaybackParameters.speed, /* customData= */
    null);
    this.playbackParameters.pendingResultCallback = new ResultCallback<MediaChannelResult>() {

        @Override
        public void onResult(MediaChannelResult mediaChannelResult) {
            if (remoteMediaClient != null) {
                updatePlaybackRateAndNotifyIfChanged(this);
                listeners.flushEvents();
            }
        }
    };
    pendingResult.setResultCallback(this.playbackParameters.pendingResultCallback);
}
Also used : MediaChannelResult(com.google.android.gms.cast.framework.media.RemoteMediaClient.MediaChannelResult) PlaybackParameters(androidx.media3.common.PlaybackParameters)

Example 2 with PlaybackParameters

use of androidx.media3.common.PlaybackParameters in project media by androidx.

the class CastPlayerTest method setPlaybackParameters_speedChangeNotSupported_resetOnResultCallback.

@Test
public void setPlaybackParameters_speedChangeNotSupported_resetOnResultCallback() {
    when(mockRemoteMediaClient.setPlaybackRate(eq((double) 1.234f), any())).thenReturn(mockPendingResult);
    PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
    1.234f);
    // Change the playback speed and and capture the result callback.
    castPlayer.setPlaybackParameters(playbackParameters);
    verify(mockPendingResult).setResultCallback(setResultCallbackArgumentCaptor.capture());
    verify(mockListener).onPlaybackParametersChanged(new PlaybackParameters(/* speed= */
    1.234f));
    // The device does not support speed changes and returns unit speed to the result callback.
    when(mockMediaStatus.getPlaybackRate()).thenReturn(1.0d);
    setResultCallbackArgumentCaptor.getValue().onResult(mock(RemoteMediaClient.MediaChannelResult.class));
    assertThat(castPlayer.getPlaybackParameters()).isEqualTo(PlaybackParameters.DEFAULT);
    verify(mockListener).onPlaybackParametersChanged(PlaybackParameters.DEFAULT);
    verifyNoMoreInteractions(mockListener);
}
Also used : PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 3 with PlaybackParameters

use of androidx.media3.common.PlaybackParameters in project media by androidx.

the class ExoPlayerTest method setUnsupportedPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndResetsOnceHandled.

@Test
public void setUnsupportedPlaybackSpeedConsecutivelyNotifiesListenerForEveryChangeOnceAndResetsOnceHandled() throws Exception {
    Renderer renderer = new FakeMediaClockRenderer(C.TRACK_TYPE_AUDIO) {

        @Override
        public long getPositionUs() {
            return 0;
        }

        @Override
        public void setPlaybackParameters(PlaybackParameters playbackParameters) {
        }

        @Override
        public PlaybackParameters getPlaybackParameters() {
            return PlaybackParameters.DEFAULT;
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).pause().waitForPlaybackState(Player.STATE_READY).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.1f)).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.2f)).setPlaybackParameters(new PlaybackParameters(/* speed= */
    1.3f)).play().build();
    List<PlaybackParameters> reportedPlaybackParameters = new ArrayList<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
            reportedPlaybackParameters.add(playbackParameters);
        }
    };
    new ExoPlayerTestRunner.Builder(context).setSupportedFormats(ExoPlayerTestRunner.AUDIO_FORMAT).setRenderers(renderer).setActionSchedule(actionSchedule).setPlayerListener(listener).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(reportedPlaybackParameters).containsExactly(new PlaybackParameters(/* speed= */
    1.1f), new PlaybackParameters(/* speed= */
    1.2f), new PlaybackParameters(/* speed= */
    1.3f), PlaybackParameters.DEFAULT).inOrder();
}
Also used : Listener(androidx.media3.common.Player.Listener) Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) ActionSchedule(androidx.media3.test.utils.ActionSchedule) FakeRenderer(androidx.media3.test.utils.FakeRenderer) FakeVideoRenderer(androidx.media3.test.utils.FakeVideoRenderer) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) ArrayList(java.util.ArrayList) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) FakeMediaClockRenderer(androidx.media3.test.utils.FakeMediaClockRenderer) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 4 with PlaybackParameters

use of androidx.media3.common.PlaybackParameters in project media by androidx.

the class ExoPlayerTest method setPlaybackSpeedBeforePreparationCompletesSucceeds.

@Test
public void setPlaybackSpeedBeforePreparationCompletesSucceeds() throws Exception {
    // Test that no exception is thrown when playback parameters are updated between creating a
    // period and preparation of the period completing.
    final CountDownLatch createPeriodCalledCountDownLatch = new CountDownLatch(1);
    final FakeMediaPeriod[] fakeMediaPeriodHolder = new FakeMediaPeriod[1];
    MediaSource mediaSource = 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) {
            // Defer completing preparation of the period until playback parameters have been set.
            fakeMediaPeriodHolder[0] = new FakeMediaPeriod(trackGroupArray, allocator, TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US, mediaSourceEventDispatcher, drmSessionManager, drmEventDispatcher, /* deferOnPrepared= */
            true);
            createPeriodCalledCountDownLatch.countDown();
            return fakeMediaPeriodHolder[0];
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).waitForPlaybackState(Player.STATE_BUFFERING).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            try {
                player.getClock().onThreadBlocked();
                createPeriodCalledCountDownLatch.await();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        }
    }).setPlaybackParameters(new PlaybackParameters(/* speed= */
    2f)).executeRunnable(() -> fakeMediaPeriodHolder[0].setPreparationComplete()).build();
    new ExoPlayerTestRunner.Builder(context).setMediaSources(mediaSource).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
}
Also used : TransferListener(androidx.media3.datasource.TransferListener) Allocator(androidx.media3.exoplayer.upstream.Allocator) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) DrmSessionManager(androidx.media3.exoplayer.drm.DrmSessionManager) TrackGroupArray(androidx.media3.common.TrackGroupArray) CountDownLatch(java.util.concurrent.CountDownLatch) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) Nullable(androidx.annotation.Nullable) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Example 5 with PlaybackParameters

use of androidx.media3.common.PlaybackParameters in project media by androidx.

the class ExoPlayerTest method onEvents_correspondToListenerCalls.

@Test
public void onEvents_correspondToListenerCalls() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    Format formatWithStaticMetadata = new Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).setMetadata(new Metadata(new BinaryFrame(/* id= */
    "", /* data= */
    new byte[0]), new TextInformationFrame(/* id= */
    "TT2", /* description= */
    null, /* value= */
    "title"))).build();
    // Set multiple values together.
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(), formatWithStaticMetadata));
    player.seekTo(2_000);
    player.setPlaybackParameters(new PlaybackParameters(/* speed= */
    2.0f));
    runUntilPendingCommandsAreFullyHandled(player);
    verify(listener).onTimelineChanged(any(), anyInt());
    verify(listener).onMediaItemTransition(any(), anyInt());
    verify(listener).onPositionDiscontinuity(any(), any(), anyInt());
    verify(listener).onPlaybackParametersChanged(any());
    ArgumentCaptor<Player.Events> eventCaptor = ArgumentCaptor.forClass(Player.Events.class);
    verify(listener).onEvents(eq(player), eventCaptor.capture());
    Player.Events events = eventCaptor.getValue();
    assertThat(events.contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(events.contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(events.contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(events.contains(Player.EVENT_PLAYBACK_PARAMETERS_CHANGED)).isTrue();
    // Set values recursively.
    player.addListener(new Player.Listener() {

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            player.setShuffleModeEnabled(true);
        }
    });
    player.setRepeatMode(Player.REPEAT_MODE_ONE);
    runUntilPendingCommandsAreFullyHandled(player);
    verify(listener).onRepeatModeChanged(anyInt());
    verify(listener).onShuffleModeEnabledChanged(anyBoolean());
    verify(listener, times(2)).onEvents(eq(player), eventCaptor.capture());
    events = Iterables.getLast(eventCaptor.getAllValues());
    assertThat(events.contains(Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
    assertThat(events.contains(Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED)).isTrue();
    // Ensure all other events are called (even though we can't control how exactly they are
    // combined together in onEvents calls).
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    player.play();
    player.setMediaItem(MediaItem.fromUri("http://this-will-throw-an-exception.mp4"));
    TestPlayerRunHelper.runUntilError(player);
    runUntilPendingCommandsAreFullyHandled(player);
    player.release();
    // Verify that all callbacks have been called at least once.
    verify(listener, atLeastOnce()).onTimelineChanged(any(), anyInt());
    verify(listener, atLeastOnce()).onMediaItemTransition(any(), anyInt());
    verify(listener, atLeastOnce()).onPositionDiscontinuity(any(), any(), anyInt());
    verify(listener, atLeastOnce()).onPlaybackParametersChanged(any());
    verify(listener, atLeastOnce()).onRepeatModeChanged(anyInt());
    verify(listener, atLeastOnce()).onShuffleModeEnabledChanged(anyBoolean());
    verify(listener, atLeastOnce()).onPlaybackStateChanged(anyInt());
    verify(listener, atLeastOnce()).onIsLoadingChanged(anyBoolean());
    verify(listener, atLeastOnce()).onTracksChanged(any(), any());
    verify(listener, atLeastOnce()).onMediaMetadataChanged(any());
    verify(listener, atLeastOnce()).onPlayWhenReadyChanged(anyBoolean(), anyInt());
    verify(listener, atLeastOnce()).onIsPlayingChanged(anyBoolean());
    verify(listener, atLeastOnce()).onPlayerErrorChanged(any());
    verify(listener, atLeastOnce()).onPlayerError(any());
    // Verify all the same events have been recorded with onEvents.
    verify(listener, atLeastOnce()).onEvents(eq(player), eventCaptor.capture());
    List<Player.Events> allEvents = eventCaptor.getAllValues();
    assertThat(containsEvent(allEvents, Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYBACK_PARAMETERS_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_IS_LOADING_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_TRACKS_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_MEDIA_METADATA_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAY_WHEN_READY_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_IS_PLAYING_CHANGED)).isTrue();
    assertThat(containsEvent(allEvents, Player.EVENT_PLAYER_ERROR)).isTrue();
}
Also used : Player(androidx.media3.common.Player) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaMetadata(androidx.media3.common.MediaMetadata) Metadata(androidx.media3.common.Metadata) TextInformationFrame(androidx.media3.extractor.metadata.id3.TextInformationFrame) Listener(androidx.media3.common.Player.Listener) Format(androidx.media3.common.Format) BinaryFrame(androidx.media3.extractor.metadata.id3.BinaryFrame) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) PlaybackParameters(androidx.media3.common.PlaybackParameters) Test(org.junit.Test)

Aggregations

PlaybackParameters (androidx.media3.common.PlaybackParameters)45 Test (org.junit.Test)32 Player (androidx.media3.common.Player)12 LargeTest (androidx.test.filters.LargeTest)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Bundle (android.os.Bundle)8 Listener (androidx.media3.common.Player.Listener)7 Timeline (androidx.media3.common.Timeline)7 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)7 FakeTimeline (androidx.media3.test.utils.FakeTimeline)7 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)7 Nullable (androidx.annotation.Nullable)6 MediaItem (androidx.media3.common.MediaItem)6 MediaMetadata (androidx.media3.common.MediaMetadata)5 RepeatMode (androidx.media3.common.Player.RepeatMode)4 State (androidx.media3.common.Player.State)4 ArrayList (java.util.ArrayList)4 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)3