use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.
the class ImaServerSideAdInsertionMediaSourceTest method adsLoaderStateToBundle_marshallAndUnmarshalling_resultIsEqual.
@Test
public void adsLoaderStateToBundle_marshallAndUnmarshalling_resultIsEqual() {
AdPlaybackState firstAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId1"), /* fromPositionUs= */
0, /* contentResumeOffsetUs= */
10, /* adDurationsUs...= */
5_000_000, 10_000_000, 20_000_000);
AdPlaybackState secondAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId2"), /* fromPositionUs= */
0, /* contentResumeOffsetUs= */
10, /* adDurationsUs...= */
10_000_000).withPlayedAd(/* adGroupIndex= */
0, /* adIndexInAdGroup= */
0);
AdPlaybackState thirdAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(new AdPlaybackState("adsId3"), /* fromPositionUs= */
C.TIME_END_OF_SOURCE, /* contentResumeOffsetUs= */
10, /* adDurationsUs...= */
10_000_000);
thirdAdPlaybackState = ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(thirdAdPlaybackState, /* fromPositionUs= */
0, /* contentResumeOffsetUs= */
10, /* adDurationsUs...= */
10_000_000).withRemovedAdGroupCount(1);
State state = new State(ImmutableMap.<String, AdPlaybackState>builder().put("adsId1", firstAdPlaybackState).put("adsId2", secondAdPlaybackState).put("adsId3", thirdAdPlaybackState).buildOrThrow());
assertThat(State.CREATOR.fromBundle(state.toBundle())).isEqualTo(state);
}
use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.
the class MediaSessionConnector method invalidateMediaSessionPlaybackState.
/**
* Updates the playback state of the media session.
*
* <p>Apps normally only need to call this method when the custom actions provided by a {@link
* CustomActionProvider} changed and the playback state needs to be updated immediately.
*/
public final void invalidateMediaSessionPlaybackState() {
PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
@Nullable Player player = this.player;
if (player == null) {
builder.setActions(buildPrepareActions()).setState(PlaybackStateCompat.STATE_NONE, /* position= */
0, /* playbackSpeed= */
0, /* updateTime= */
SystemClock.elapsedRealtime());
mediaSession.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
mediaSession.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
mediaSession.setPlaybackState(builder.build());
return;
}
Map<String, CustomActionProvider> currentActions = new HashMap<>();
for (CustomActionProvider customActionProvider : customActionProviders) {
@Nullable PlaybackStateCompat.CustomAction customAction = customActionProvider.getCustomAction(player);
if (customAction != null) {
currentActions.put(customAction.getAction(), customActionProvider);
builder.addCustomAction(customAction);
}
}
customActionMap = Collections.unmodifiableMap(currentActions);
Bundle extras = new Bundle();
@Nullable PlaybackException playbackError = player.getPlayerError();
boolean reportError = playbackError != null || customError != null;
int sessionPlaybackState = reportError ? PlaybackStateCompat.STATE_ERROR : getMediaSessionPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
if (customError != null) {
builder.setErrorMessage(customError.first, customError.second);
if (customErrorExtras != null) {
extras.putAll(customErrorExtras);
}
} else if (playbackError != null && errorMessageProvider != null) {
Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
builder.setErrorMessage(message.first, message.second);
}
long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player) : MediaSessionCompat.QueueItem.UNKNOWN_ID;
float playbackSpeed = player.getPlaybackParameters().speed;
extras.putFloat(EXTRAS_SPEED, playbackSpeed);
float sessionPlaybackSpeed = player.isPlaying() ? playbackSpeed : 0f;
@Nullable MediaItem currentMediaItem = player.getCurrentMediaItem();
if (currentMediaItem != null && !MediaItem.DEFAULT_MEDIA_ID.equals(currentMediaItem.mediaId)) {
extras.putString(PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID, currentMediaItem.mediaId);
}
builder.setActions(buildPrepareActions() | buildPlaybackActions(player)).setActiveQueueItemId(activeQueueItemId).setBufferedPosition(player.getBufferedPosition()).setState(sessionPlaybackState, player.getCurrentPosition(), sessionPlaybackSpeed, /* updateTime= */
SystemClock.elapsedRealtime()).setExtras(extras);
@Player.RepeatMode int repeatMode = player.getRepeatMode();
mediaSession.setRepeatMode(repeatMode == Player.REPEAT_MODE_ONE ? PlaybackStateCompat.REPEAT_MODE_ONE : repeatMode == Player.REPEAT_MODE_ALL ? PlaybackStateCompat.REPEAT_MODE_ALL : PlaybackStateCompat.REPEAT_MODE_NONE);
mediaSession.setShuffleMode(player.getShuffleModeEnabled() ? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE);
mediaSession.setPlaybackState(builder.build());
}
use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State 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();
}
use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.
the class MediaCodecVideoRenderer method setOutput.
private void setOutput(@Nullable Object output) throws ExoPlaybackException {
// Handle unsupported (i.e., non-Surface) outputs by clearing the surface.
@Nullable Surface surface = output instanceof Surface ? (Surface) output : null;
if (surface == null) {
// Use a dummy surface if possible.
if (dummySurface != null) {
surface = dummySurface;
} else {
MediaCodecInfo codecInfo = getCodecInfo();
if (codecInfo != null && shouldUseDummySurface(codecInfo)) {
dummySurface = DummySurface.newInstanceV17(context, codecInfo.secure);
surface = dummySurface;
}
}
}
// We only need to update the codec if the surface has changed.
if (this.surface != surface) {
this.surface = surface;
frameReleaseHelper.onSurfaceChanged(surface);
haveReportedFirstFrameRenderedForCurrentSurface = false;
@State int state = getState();
@Nullable MediaCodecAdapter codec = getCodec();
if (codec != null) {
if (Util.SDK_INT >= 23 && surface != null && !codecNeedsSetOutputSurfaceWorkaround) {
setOutputSurfaceV23(codec, surface);
} else {
releaseCodec();
maybeInitCodecOrBypass();
}
}
if (surface != null && surface != dummySurface) {
// If we know the video size, report it again immediately.
maybeRenotifyVideoSizeChanged();
// We haven't rendered to the new surface yet.
clearRenderedFirstFrame();
if (state == STATE_STARTED) {
setJoiningDeadlineMs();
}
} else {
// The surface has been removed.
clearReportedVideoSize();
clearRenderedFirstFrame();
}
} else if (surface != null && surface != dummySurface) {
// The surface is set and unchanged. If we know the video size and/or have already rendered to
// the surface, report these again immediately.
maybeRenotifyVideoSizeChanged();
maybeRenotifyRenderedFirstFrame();
}
}
use of com.google.android.exoplayer2.ext.ima.ImaServerSideAdInsertionMediaSource.AdsLoader.State in project ExoPlayer by google.
the class ServerSideAdInsertionUtilTest method addAdGroupToAdPlaybackState_insertsCorrectAdGroupData.
@Test
public void addAdGroupToAdPlaybackState_insertsCorrectAdGroupData() {
AdPlaybackState state = new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 1, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2);
// stream: 0-- content --4300-- ad1 --4500-- content
// content timeline: 0-4300 - [ad1] - 4700-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
4300, /* contentResumeOffsetUs= */
400, /* adDurationsUs...= */
200);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 4300, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
200));
// stream: 0-- content --2100-- ad1 --2400-- content --4300-- ad2 --4500-- content
// content timeline: 0-2100 - [ad1] - 2100-4000 - [ad2] - 4400-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
2100, /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
300);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 2100, 4000, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
3, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
200));
// stream: 0-- ad1 --100-- content --2100-- ad2 --2400-- content --4300-- ad3 --4500-- content
// content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-end
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
0, /* contentResumeOffsetUs= */
50, /* adDurationsUs...= */
100);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 0, 2050, 3950, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withAdCount(/* adGroupIndex= */
4, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
4, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
50).withContentResumeOffsetUs(/* adGroupIndex= */
4, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
100).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
4, /* adDurationsUs...= */
200));
// stream: 0-- ad1 --100-- c --2100-- ad2 --2400-- c --4300-- ad3 --4500-- c --5000-- ad4 --6000
// content timeline: 0 - [ad1] - 50-2050 -[ad2] - 2050-3950 - [ad3] - 4350-4850 - [ad4] - 4850
state = addAdGroupToAdPlaybackState(state, /* fromPositionUs= */
5000, /* contentResumeOffsetUs= */
0, /* adDurationsUs...= */
1000);
assertThat(state).isEqualTo(new AdPlaybackState(ADS_ID, /* adGroupTimesUs...= */
0, 0, 0, 2050, 3950, 4850, C.TIME_END_OF_SOURCE).withRemovedAdGroupCount(2).withAdCount(/* adGroupIndex= */
2, /* adCount= */
1).withAdCount(/* adGroupIndex= */
3, /* adCount= */
1).withAdCount(/* adGroupIndex= */
4, /* adCount= */
1).withAdCount(/* adGroupIndex= */
5, /* adCount= */
1).withIsServerSideInserted(/* adGroupIndex= */
2, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
3, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
4, /* isServerSideInserted= */
true).withIsServerSideInserted(/* adGroupIndex= */
5, /* isServerSideInserted= */
true).withContentResumeOffsetUs(/* adGroupIndex= */
2, /* contentResumeOffsetUs= */
50).withContentResumeOffsetUs(/* adGroupIndex= */
4, /* contentResumeOffsetUs= */
400).withAdDurationsUs(/* adGroupIndex= */
2, /* adDurationsUs...= */
100).withAdDurationsUs(/* adGroupIndex= */
3, /* adDurationsUs...= */
300).withAdDurationsUs(/* adGroupIndex= */
4, /* adDurationsUs...= */
200).withAdDurationsUs(/* adGroupIndex= */
5, /* adDurationsUs...= */
1000));
}
Aggregations