Commit 5775a470 by aquilescanta Committed by Ian Baker

Remove maybe prefix for cast setting methods

Also move the masking internal state update before the message
dispatch, so as to ensure the result of the operation prevails
over the masking state.

PiperOrigin-RevId: 273740585
parent 1312a542
...@@ -134,7 +134,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -134,7 +134,7 @@ public final class CastPlayer extends BasePlayer {
currentTrackSelection = EMPTY_TRACK_SELECTION_ARRAY; currentTrackSelection = EMPTY_TRACK_SELECTION_ARRAY;
pendingSeekWindowIndex = C.INDEX_UNSET; pendingSeekWindowIndex = C.INDEX_UNSET;
pendingSeekPositionMs = C.TIME_UNSET; pendingSeekPositionMs = C.TIME_UNSET;
maybeUpdateInternalState(); updateInternalStateAndNotifyIfChanged();
} }
// Media Queue manipulation methods. // Media Queue manipulation methods.
...@@ -346,19 +346,20 @@ public final class CastPlayer extends BasePlayer { ...@@ -346,19 +346,20 @@ public final class CastPlayer extends BasePlayer {
if (remoteMediaClient == null) { if (remoteMediaClient == null) {
return; return;
} }
// We send the message to the receiver app and update the local state, which will cause the // We update the local state and send the message to the receiver app, which will cause the
// operation to be perceived as synchronous by the user. // operation to be perceived as synchronous by the user. When the operation reports a result,
// the local state will be updated to reflect the state reported by the Cast SDK.
setPlayerStateAndNotifyIfChanged(playWhenReady, playbackState);
flushNotifications();
PendingResult<MediaChannelResult> pendingResult = PendingResult<MediaChannelResult> pendingResult =
playWhenReady ? remoteMediaClient.play() : remoteMediaClient.pause(); playWhenReady ? remoteMediaClient.play() : remoteMediaClient.pause();
pendingResult.setResultCallback( pendingResult.setResultCallback(
mediaChannelResult -> { mediaChannelResult -> {
if (remoteMediaClient != null) { if (remoteMediaClient != null) {
maybeUpdatePlayerStateAndNotify(); updatePlayerStateAndNotifyIfChanged();
flushNotifications(); flushNotifications();
} }
}); });
maybeSetPlayerStateAndNotify(playWhenReady, playbackState);
flushNotifications();
} }
@Override @Override
...@@ -547,14 +548,14 @@ public final class CastPlayer extends BasePlayer { ...@@ -547,14 +548,14 @@ public final class CastPlayer extends BasePlayer {
// Internal methods. // Internal methods.
private void maybeUpdateInternalState() { private void updateInternalStateAndNotifyIfChanged() {
if (remoteMediaClient == null) { if (remoteMediaClient == null) {
// There is no session. We leave the state of the player as it is now. // There is no session. We leave the state of the player as it is now.
return; return;
} }
boolean wasPlaying = playbackState == Player.STATE_READY && playWhenReady; boolean wasPlaying = playbackState == Player.STATE_READY && playWhenReady;
maybeUpdatePlayerStateAndNotify(); updatePlayerStateAndNotifyIfChanged();
boolean isPlaying = playbackState == Player.STATE_READY && playWhenReady; boolean isPlaying = playbackState == Player.STATE_READY && playWhenReady;
if (wasPlaying != isPlaying) { if (wasPlaying != isPlaying) {
notificationsBatch.add( notificationsBatch.add(
...@@ -566,7 +567,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -566,7 +567,7 @@ public final class CastPlayer extends BasePlayer {
notificationsBatch.add( notificationsBatch.add(
new ListenerNotificationTask(listener -> listener.onRepeatModeChanged(this.repeatMode))); new ListenerNotificationTask(listener -> listener.onRepeatModeChanged(this.repeatMode)));
} }
maybeUpdateTimelineAndNotify(); updateTimelineAndNotifyIfChanged();
int currentWindowIndex = C.INDEX_UNSET; int currentWindowIndex = C.INDEX_UNSET;
MediaQueueItem currentItem = remoteMediaClient.getCurrentItem(); MediaQueueItem currentItem = remoteMediaClient.getCurrentItem();
...@@ -584,7 +585,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -584,7 +585,7 @@ public final class CastPlayer extends BasePlayer {
listener -> listener ->
listener.onPositionDiscontinuity(DISCONTINUITY_REASON_PERIOD_TRANSITION))); listener.onPositionDiscontinuity(DISCONTINUITY_REASON_PERIOD_TRANSITION)));
} }
if (updateTracksAndSelections()) { if (updateTracksAndSelectionsAndNotifyIfChanged()) {
notificationsBatch.add( notificationsBatch.add(
new ListenerNotificationTask( new ListenerNotificationTask(
listener -> listener.onTracksChanged(currentTrackGroups, currentTrackSelection))); listener -> listener.onTracksChanged(currentTrackGroups, currentTrackSelection)));
...@@ -593,12 +594,12 @@ public final class CastPlayer extends BasePlayer { ...@@ -593,12 +594,12 @@ public final class CastPlayer extends BasePlayer {
} }
@RequiresNonNull("remoteMediaClient") @RequiresNonNull("remoteMediaClient")
private void maybeUpdatePlayerStateAndNotify() { private void updatePlayerStateAndNotifyIfChanged() {
maybeSetPlayerStateAndNotify( setPlayerStateAndNotifyIfChanged(
!remoteMediaClient.isPaused(), fetchPlaybackState(remoteMediaClient)); !remoteMediaClient.isPaused(), fetchPlaybackState(remoteMediaClient));
} }
private void maybeUpdateTimelineAndNotify() { private void updateTimelineAndNotifyIfChanged() {
if (updateTimeline()) { if (updateTimeline()) {
@Player.TimelineChangeReason @Player.TimelineChangeReason
int reason = int reason =
...@@ -625,10 +626,8 @@ public final class CastPlayer extends BasePlayer { ...@@ -625,10 +626,8 @@ public final class CastPlayer extends BasePlayer {
return !oldTimeline.equals(currentTimeline); return !oldTimeline.equals(currentTimeline);
} }
/** /** Updates the internal tracks and selection and returns whether they have changed. */
* Updates the internal tracks and selection and returns whether they have changed. private boolean updateTracksAndSelectionsAndNotifyIfChanged() {
*/
private boolean updateTracksAndSelections() {
if (remoteMediaClient == null) { if (remoteMediaClient == null) {
// There is no session. We leave the state of the player as it is now. // There is no session. We leave the state of the player as it is now.
return false; return false;
...@@ -674,7 +673,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -674,7 +673,7 @@ public final class CastPlayer extends BasePlayer {
return false; return false;
} }
private void maybeSetPlayerStateAndNotify( private void setPlayerStateAndNotifyIfChanged(
boolean playWhenReady, @Player.State int playbackState) { boolean playWhenReady, @Player.State int playbackState) {
if (this.playWhenReady != playWhenReady || this.playbackState != playbackState) { if (this.playWhenReady != playWhenReady || this.playbackState != playbackState) {
this.playWhenReady = playWhenReady; this.playWhenReady = playWhenReady;
...@@ -701,7 +700,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -701,7 +700,7 @@ public final class CastPlayer extends BasePlayer {
} }
remoteMediaClient.addListener(statusListener); remoteMediaClient.addListener(statusListener);
remoteMediaClient.addProgressListener(statusListener, PROGRESS_REPORT_PERIOD_MS); remoteMediaClient.addProgressListener(statusListener, PROGRESS_REPORT_PERIOD_MS);
maybeUpdateInternalState(); updateInternalStateAndNotifyIfChanged();
} else { } else {
if (sessionAvailabilityListener != null) { if (sessionAvailabilityListener != null) {
sessionAvailabilityListener.onCastSessionUnavailable(); sessionAvailabilityListener.onCastSessionUnavailable();
...@@ -820,7 +819,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -820,7 +819,7 @@ public final class CastPlayer extends BasePlayer {
@Override @Override
public void onStatusUpdated() { public void onStatusUpdated() {
maybeUpdateInternalState(); updateInternalStateAndNotifyIfChanged();
} }
@Override @Override
...@@ -828,7 +827,7 @@ public final class CastPlayer extends BasePlayer { ...@@ -828,7 +827,7 @@ public final class CastPlayer extends BasePlayer {
@Override @Override
public void onQueueStatusUpdated() { public void onQueueStatusUpdated() {
maybeUpdateTimelineAndNotify(); updateTimelineAndNotifyIfChanged();
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment