use of com.google.android.exoplayer2.PlaybackParameters in project ExoPlayer by google.
the class DefaultAudioSinkTest method handlesBufferAfterReset_withFormatChangeAndPlaybackSpeed.
@Test
public void handlesBufferAfterReset_withFormatChangeAndPlaybackSpeed() throws Exception {
defaultAudioSink.setPlaybackParameters(new PlaybackParameters(/* speed= */
1.5f));
configureDefaultAudioSink(CHANNEL_COUNT_STEREO);
defaultAudioSink.handleBuffer(createDefaultSilenceBuffer(), /* presentationTimeUs= */
0, /* encodedAccessUnitCount= */
1);
// After reset and re-configure we can successfully queue more input.
defaultAudioSink.reset();
configureDefaultAudioSink(CHANNEL_COUNT_MONO);
defaultAudioSink.handleBuffer(createDefaultSilenceBuffer(), /* presentationTimeUs= */
0, /* encodedAccessUnitCount= */
1);
assertThat(defaultAudioSink.getPlaybackParameters()).isEqualTo(new PlaybackParameters(/* speed= */
1.5f));
}
use of com.google.android.exoplayer2.PlaybackParameters in project ExoPlayer by google.
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);
}
use of com.google.android.exoplayer2.PlaybackParameters in project ExoPlayer by google.
the class CastPlayerTest method playbackParameters_onStatusUpdated_setsRemotePlaybackSpeed.
@Test
public void playbackParameters_onStatusUpdated_setsRemotePlaybackSpeed() {
PlaybackParameters expectedPlaybackParameters = new PlaybackParameters(/* speed= */
1.234f);
when(mockMediaStatus.getPlaybackRate()).thenReturn(1.234d);
remoteMediaClientCallback.onStatusUpdated();
assertThat(castPlayer.getPlaybackParameters()).isEqualTo(expectedPlaybackParameters);
verify(mockListener).onPlaybackParametersChanged(expectedPlaybackParameters);
}
use of com.google.android.exoplayer2.PlaybackParameters in project ExoPlayer by google.
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();
}
use of com.google.android.exoplayer2.PlaybackParameters in project ExoPlayer by google.
the class ExoPlayerTest method setPlaybackSpeed_withAdPlayback_onlyAppliesToContent.
@Test
public void setPlaybackSpeed_withAdPlayback_onlyAppliesToContent() throws Exception {
// Create renderer with media clock to listen to playback parameter changes.
ArrayList<PlaybackParameters> playbackParameters = new ArrayList<>();
FakeMediaClockRenderer audioRenderer = new FakeMediaClockRenderer(C.TRACK_TYPE_AUDIO) {
private long positionUs;
@Override
protected void onStreamChanged(Format[] formats, long startPositionUs, long offsetUs) {
this.positionUs = offsetUs;
}
@Override
public long getPositionUs() {
// Continuously increase position to let playback progress.
positionUs += 10_000;
return positionUs;
}
@Override
public void setPlaybackParameters(PlaybackParameters parameters) {
playbackParameters.add(parameters);
}
@Override
public PlaybackParameters getPlaybackParameters() {
return playbackParameters.isEmpty() ? PlaybackParameters.DEFAULT : Iterables.getLast(playbackParameters);
}
};
ExoPlayer player = new TestExoPlayerBuilder(context).setRenderers(audioRenderer).build();
AdPlaybackState adPlaybackState = FakeTimeline.createAdPlaybackState(/* adsPerAdGroup= */
1, /* adGroupTimesUs...= */
0, 7 * C.MICROS_PER_SECOND, C.TIME_END_OF_SOURCE);
TimelineWindowDefinition adTimelineDefinition = new TimelineWindowDefinition(/* periodCount= */
1, /* id= */
0, /* isSeekable= */
true, /* isDynamic= */
false, /* isLive= */
false, /* isPlaceholder= */
false, /* durationUs= */
10 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
0, /* windowOffsetInFirstPeriodUs= */
0, adPlaybackState);
player.setMediaSource(new FakeMediaSource(new FakeTimeline(adTimelineDefinition), ExoPlayerTestRunner.AUDIO_FORMAT));
Player.Listener mockListener = mock(Player.Listener.class);
player.addListener(mockListener);
player.setPlaybackSpeed(5f);
player.prepare();
player.play();
runUntilPlaybackState(player, Player.STATE_ENDED);
player.release();
// Assert that the renderer received the playback speed updates at each ad/content boundary.
assertThat(playbackParameters).containsExactly(/* preroll ad */
new PlaybackParameters(1f), /* content after preroll */
new PlaybackParameters(5f), /* midroll ad */
new PlaybackParameters(1f), /* content after midroll */
new PlaybackParameters(5f), /* postroll ad */
new PlaybackParameters(1f), /* content after postroll */
new PlaybackParameters(5f)).inOrder();
// Assert that user-set speed was reported, but none of the ad overrides.
verify(mockListener).onPlaybackParametersChanged(any());
verify(mockListener).onPlaybackParametersChanged(new PlaybackParameters(5.0f));
}
Aggregations