Skip to content

Commit 1c6aaac

Browse files
icbakerojw28
authored andcommitted
Merge playback speed into PlaybackInfo and update EPI and EPII
PiperOrigin-RevId: 322539001
1 parent 78260e2 commit 1c6aaac

File tree

5 files changed

+98
-91
lines changed

5 files changed

+98
-91
lines changed

library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java

Lines changed: 33 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.annotation.SuppressLint;
2323
import android.os.Handler;
2424
import android.os.Looper;
25-
import android.os.Message;
2625
import android.util.Pair;
2726
import androidx.annotation.Nullable;
2827
import com.google.android.exoplayer2.PlayerMessage.Target;
@@ -67,7 +66,8 @@
6766

6867
private final Renderer[] renderers;
6968
private final TrackSelector trackSelector;
70-
private final PlaybackUpdateListenerImpl playbackUpdateListener;
69+
private final Handler playbackInfoUpdateHandler;
70+
private final ExoPlayerImplInternal.PlaybackInfoUpdateListener playbackInfoUpdateListener;
7171
private final ExoPlayerImplInternal internalPlayer;
7272
private final Handler internalPlayerHandler;
7373
private final CopyOnWriteArrayList<ListenerHolder> listeners;
@@ -87,8 +87,6 @@
8787
@DiscontinuityReason private int pendingDiscontinuityReason;
8888
@PlayWhenReadyChangeReason private int pendingPlayWhenReadyChangeReason;
8989
private boolean foregroundMode;
90-
private int pendingSetPlaybackSpeedAcks;
91-
private float playbackSpeed;
9290
private SeekParameters seekParameters;
9391
private ShuffleOrder shuffleOrder;
9492
private boolean pauseAtEndOfMediaItems;
@@ -155,9 +153,11 @@ public ExoPlayerImpl(
155153
new TrackSelection[renderers.length],
156154
null);
157155
period = new Timeline.Period();
158-
playbackSpeed = Player.DEFAULT_PLAYBACK_SPEED;
159156
maskingWindowIndex = C.INDEX_UNSET;
160-
playbackUpdateListener = new PlaybackUpdateListenerImpl(applicationLooper);
157+
playbackInfoUpdateHandler = new Handler(applicationLooper);
158+
playbackInfoUpdateListener =
159+
playbackInfoUpdate ->
160+
playbackInfoUpdateHandler.post(() -> handlePlaybackInfo(playbackInfoUpdate));
161161
playbackInfo = PlaybackInfo.createDummy(emptyTrackSelectorResult);
162162
pendingListenerNotifications = new ArrayDeque<>();
163163
if (analyticsCollector != null) {
@@ -179,7 +179,7 @@ public ExoPlayerImpl(
179179
pauseAtEndOfMediaItems,
180180
applicationLooper,
181181
clock,
182-
playbackUpdateListener);
182+
playbackInfoUpdateListener);
183183
internalPlayerHandler = new Handler(internalPlayer.getPlaybackLooper());
184184
}
185185

@@ -595,7 +595,7 @@ public void seekTo(int windowIndex, long positionMs) {
595595
// general because the midroll ad preceding the seek destination must be played before the
596596
// content position can be played, if a different ad is playing at the moment.
597597
Log.w(TAG, "seekTo ignored because an ad is playing");
598-
playbackUpdateListener.onPlaybackInfoUpdate(
598+
playbackInfoUpdateListener.onPlaybackInfoUpdate(
599599
new ExoPlayerImplInternal.PlaybackInfoUpdate(playbackInfo));
600600
return;
601601
}
@@ -632,30 +632,30 @@ public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameter
632632
@Deprecated
633633
@Override
634634
public PlaybackParameters getPlaybackParameters() {
635-
return new PlaybackParameters(playbackSpeed);
635+
return new PlaybackParameters(playbackInfo.playbackSpeed);
636636
}
637637

638-
@SuppressWarnings("deprecation")
639638
@Override
640639
public void setPlaybackSpeed(float playbackSpeed) {
641640
checkState(playbackSpeed > 0);
642-
if (this.playbackSpeed == playbackSpeed) {
641+
if (playbackInfo.playbackSpeed == playbackSpeed) {
643642
return;
644643
}
645-
pendingSetPlaybackSpeedAcks++;
646-
this.playbackSpeed = playbackSpeed;
647-
PlaybackParameters playbackParameters = new PlaybackParameters(playbackSpeed);
644+
PlaybackInfo newPlaybackInfo = playbackInfo.copyWithPlaybackSpeed(playbackSpeed);
645+
pendingOperationAcks++;
648646
internalPlayer.setPlaybackSpeed(playbackSpeed);
649-
notifyListeners(
650-
listener -> {
651-
listener.onPlaybackParametersChanged(playbackParameters);
652-
listener.onPlaybackSpeedChanged(playbackSpeed);
653-
});
647+
updatePlaybackInfo(
648+
newPlaybackInfo,
649+
/* positionDiscontinuity= */ false,
650+
/* ignored */ DISCONTINUITY_REASON_INTERNAL,
651+
/* ignored */ TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED,
652+
/* ignored */ PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST,
653+
/* seekProcessed= */ false);
654654
}
655655

656656
@Override
657657
public float getPlaybackSpeed() {
658-
return playbackSpeed;
658+
return playbackInfo.playbackSpeed;
659659
}
660660

661661
@Override
@@ -726,7 +726,7 @@ public void release() {
726726
ExoPlaybackException.createForUnexpected(
727727
new RuntimeException(new TimeoutException("Player release timed out.")))));
728728
}
729-
playbackUpdateListener.handler.removeCallbacksAndMessages(null);
729+
playbackInfoUpdateHandler.removeCallbacksAndMessages(null);
730730
if (analyticsCollector != null) {
731731
bandwidthMeter.removeEventListener(analyticsCollector);
732732
}
@@ -896,23 +896,6 @@ private List<MediaSource> createMediaSources(List<MediaItem> mediaItems) {
896896
return mediaSources;
897897
}
898898

899-
@SuppressWarnings("deprecation")
900-
private void handlePlaybackSpeed(float playbackSpeed, boolean operationAck) {
901-
if (operationAck) {
902-
pendingSetPlaybackSpeedAcks--;
903-
}
904-
if (pendingSetPlaybackSpeedAcks == 0) {
905-
if (this.playbackSpeed != playbackSpeed) {
906-
this.playbackSpeed = playbackSpeed;
907-
notifyListeners(
908-
listener -> {
909-
listener.onPlaybackParametersChanged(new PlaybackParameters(playbackSpeed));
910-
listener.onPlaybackSpeedChanged(playbackSpeed);
911-
});
912-
}
913-
}
914-
}
915-
916899
private void handlePlaybackInfo(ExoPlayerImplInternal.PlaybackInfoUpdate playbackInfoUpdate) {
917900
pendingOperationAcks -= playbackInfoUpdate.operationAcks;
918901
if (playbackInfoUpdate.positionDiscontinuity) {
@@ -996,7 +979,7 @@ private Pair<Boolean, Integer> evaluateMediaItemTransitionReason(
996979
PlaybackInfo playbackInfo,
997980
PlaybackInfo oldPlaybackInfo,
998981
boolean positionDiscontinuity,
999-
int positionDiscontinuityReason,
982+
@DiscontinuityReason int positionDiscontinuityReason,
1000983
boolean timelineChanged) {
1001984

1002985
Timeline oldTimeline = oldPlaybackInfo.timeline;
@@ -1360,49 +1343,6 @@ private long periodPositionUsToWindowPositionMs(MediaPeriodId periodId, long pos
13601343
return positionMs;
13611344
}
13621345

1363-
private final class PlaybackUpdateListenerImpl
1364-
implements ExoPlayerImplInternal.PlaybackUpdateListener, Handler.Callback {
1365-
private static final int MSG_PLAYBACK_INFO_CHANGED = 0;
1366-
private static final int MSG_PLAYBACK_SPEED_CHANGED = 1;
1367-
1368-
private final Handler handler;
1369-
1370-
private PlaybackUpdateListenerImpl(Looper applicationLooper) {
1371-
handler = Util.createHandler(applicationLooper, /* callback= */ this);
1372-
}
1373-
1374-
@Override
1375-
public void onPlaybackInfoUpdate(ExoPlayerImplInternal.PlaybackInfoUpdate playbackInfo) {
1376-
handler.obtainMessage(MSG_PLAYBACK_INFO_CHANGED, playbackInfo).sendToTarget();
1377-
}
1378-
1379-
@Override
1380-
public void onPlaybackSpeedChange(float playbackSpeed, boolean acknowledgeCommand) {
1381-
handler
1382-
.obtainMessage(
1383-
MSG_PLAYBACK_SPEED_CHANGED,
1384-
/* arg1= */ acknowledgeCommand ? 1 : 0,
1385-
/* arg2= */ 0,
1386-
/* obj= */ playbackSpeed)
1387-
.sendToTarget();
1388-
}
1389-
1390-
@Override
1391-
public boolean handleMessage(Message msg) {
1392-
switch (msg.what) {
1393-
case MSG_PLAYBACK_INFO_CHANGED:
1394-
handlePlaybackInfo((ExoPlayerImplInternal.PlaybackInfoUpdate) msg.obj);
1395-
break;
1396-
case MSG_PLAYBACK_SPEED_CHANGED:
1397-
handlePlaybackSpeed((Float) msg.obj, /* operationAck= */ msg.arg1 != 0);
1398-
break;
1399-
default:
1400-
throw new IllegalStateException();
1401-
}
1402-
return true;
1403-
}
1404-
}
1405-
14061346
private static final class PlaybackInfoUpdate implements Runnable {
14071347

14081348
private final PlaybackInfo playbackInfo;
@@ -1424,6 +1364,7 @@ private static final class PlaybackInfoUpdate implements Runnable {
14241364
private final boolean playWhenReadyChanged;
14251365
private final boolean playbackSuppressionReasonChanged;
14261366
private final boolean isPlayingChanged;
1367+
private final boolean playbackSpeedChanged;
14271368

14281369
public PlaybackInfoUpdate(
14291370
PlaybackInfo playbackInfo,
@@ -1461,6 +1402,7 @@ public PlaybackInfoUpdate(
14611402
playbackSuppressionReasonChanged =
14621403
previousPlaybackInfo.playbackSuppressionReason != playbackInfo.playbackSuppressionReason;
14631404
isPlayingChanged = isPlaying(previousPlaybackInfo) != isPlaying(playbackInfo);
1405+
playbackSpeedChanged = previousPlaybackInfo.playbackSpeed != playbackInfo.playbackSpeed;
14641406
}
14651407

14661408
@SuppressWarnings("deprecation")
@@ -1526,6 +1468,15 @@ public void run() {
15261468
invokeAll(
15271469
listenerSnapshot, listener -> listener.onIsPlayingChanged(isPlaying(playbackInfo)));
15281470
}
1471+
if (playbackSpeedChanged) {
1472+
PlaybackParameters playbackParameters = new PlaybackParameters(playbackInfo.playbackSpeed);
1473+
invokeAll(
1474+
listenerSnapshot,
1475+
listener -> {
1476+
listener.onPlaybackSpeedChanged(playbackInfo.playbackSpeed);
1477+
listener.onPlaybackParametersChanged(playbackParameters);
1478+
});
1479+
}
15291480
if (seekProcessed) {
15301481
invokeAll(listenerSnapshot, EventListener::onSeekProcessed);
15311482
}

library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ public void setPlayWhenReadyChangeReason(
109109
}
110110
}
111111

112-
public interface PlaybackUpdateListener {
112+
public interface PlaybackInfoUpdateListener {
113113
void onPlaybackInfoUpdate(ExoPlayerImplInternal.PlaybackInfoUpdate playbackInfo);
114-
115-
void onPlaybackSpeedChange(float playbackSpeed, boolean acknowledgeCommand);
116114
}
117115

118116
// Internal messages
@@ -168,7 +166,7 @@ public interface PlaybackUpdateListener {
168166
private final DefaultMediaClock mediaClock;
169167
private final ArrayList<PendingMessageInfo> pendingMessages;
170168
private final Clock clock;
171-
private final PlaybackUpdateListener playbackUpdateListener;
169+
private final PlaybackInfoUpdateListener playbackInfoUpdateListener;
172170
private final MediaPeriodQueue queue;
173171
private final MediaSourceList mediaSourceList;
174172

@@ -210,8 +208,8 @@ public ExoPlayerImplInternal(
210208
boolean pauseAtEndOfWindow,
211209
Looper applicationLooper,
212210
Clock clock,
213-
PlaybackUpdateListener playbackUpdateListener) {
214-
this.playbackUpdateListener = playbackUpdateListener;
211+
PlaybackInfoUpdateListener playbackInfoUpdateListener) {
212+
this.playbackInfoUpdateListener = playbackInfoUpdateListener;
215213
this.renderers = renderers;
216214
this.trackSelector = trackSelector;
217215
this.emptyTrackSelectorResult = emptyTrackSelectorResult;
@@ -624,7 +622,7 @@ private void setState(int state) {
624622
private void maybeNotifyPlaybackInfoChanged() {
625623
playbackInfoUpdate.setPlaybackInfo(playbackInfo);
626624
if (playbackInfoUpdate.hasPendingChange) {
627-
playbackUpdateListener.onPlaybackInfoUpdate(playbackInfoUpdate);
625+
playbackInfoUpdateListener.onPlaybackInfoUpdate(playbackInfoUpdate);
628626
playbackInfoUpdate = new PlaybackInfoUpdate(playbackInfo);
629627
}
630628
}
@@ -1279,6 +1277,7 @@ private void resetInternal(
12791277
mediaPeriodId,
12801278
playbackInfo.playWhenReady,
12811279
playbackInfo.playbackSuppressionReason,
1280+
playbackInfo.playbackSpeed,
12821281
startPositionUs,
12831282
/* totalBufferedDurationUs= */ 0,
12841283
startPositionUs);
@@ -1964,7 +1963,8 @@ private void handleContinueLoadingRequested(MediaPeriod mediaPeriod) {
19641963

19651964
private void handlePlaybackSpeed(float playbackSpeed, boolean acknowledgeCommand)
19661965
throws ExoPlaybackException {
1967-
playbackUpdateListener.onPlaybackSpeedChange(playbackSpeed, acknowledgeCommand);
1966+
playbackInfoUpdate.incrementPendingOperationAcks(acknowledgeCommand ? 1 : 0);
1967+
playbackInfo = playbackInfo.copyWithPlaybackSpeed(playbackSpeed);
19681968
updateTrackSelectionPlaybackSpeed(playbackSpeed);
19691969
for (Renderer renderer : renderers) {
19701970
if (renderer != null) {

library/core/src/main/java/com/google/android/exoplayer2/PlaybackInfo.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
public final boolean playWhenReady;
6464
/** Reason why playback is suppressed even though {@link #playWhenReady} is {@code true}. */
6565
@PlaybackSuppressionReason public final int playbackSuppressionReason;
66+
/** The playback speed. */
67+
public final float playbackSpeed;
6668

6769
/**
6870
* Position up to which media is buffered in {@link #loadingMediaPeriodId) relative to the start
@@ -101,6 +103,7 @@ public static PlaybackInfo createDummy(TrackSelectorResult emptyTrackSelectorRes
101103
PLACEHOLDER_MEDIA_PERIOD_ID,
102104
/* playWhenReady= */ false,
103105
Player.PLAYBACK_SUPPRESSION_REASON_NONE,
106+
Player.DEFAULT_PLAYBACK_SPEED,
104107
/* bufferedPositionUs= */ 0,
105108
/* totalBufferedDurationUs= */ 0,
106109
/* positionUs= */ 0);
@@ -133,6 +136,7 @@ public PlaybackInfo(
133136
MediaPeriodId loadingMediaPeriodId,
134137
boolean playWhenReady,
135138
@PlaybackSuppressionReason int playbackSuppressionReason,
139+
float playbackSpeed,
136140
long bufferedPositionUs,
137141
long totalBufferedDurationUs,
138142
long positionUs) {
@@ -147,6 +151,7 @@ public PlaybackInfo(
147151
this.loadingMediaPeriodId = loadingMediaPeriodId;
148152
this.playWhenReady = playWhenReady;
149153
this.playbackSuppressionReason = playbackSuppressionReason;
154+
this.playbackSpeed = playbackSpeed;
150155
this.bufferedPositionUs = bufferedPositionUs;
151156
this.totalBufferedDurationUs = totalBufferedDurationUs;
152157
this.positionUs = positionUs;
@@ -190,6 +195,7 @@ public PlaybackInfo copyWithNewPosition(
190195
loadingMediaPeriodId,
191196
playWhenReady,
192197
playbackSuppressionReason,
198+
playbackSpeed,
193199
bufferedPositionUs,
194200
totalBufferedDurationUs,
195201
positionUs);
@@ -215,6 +221,7 @@ public PlaybackInfo copyWithTimeline(Timeline timeline) {
215221
loadingMediaPeriodId,
216222
playWhenReady,
217223
playbackSuppressionReason,
224+
playbackSpeed,
218225
bufferedPositionUs,
219226
totalBufferedDurationUs,
220227
positionUs);
@@ -240,6 +247,7 @@ public PlaybackInfo copyWithPlaybackState(int playbackState) {
240247
loadingMediaPeriodId,
241248
playWhenReady,
242249
playbackSuppressionReason,
250+
playbackSpeed,
243251
bufferedPositionUs,
244252
totalBufferedDurationUs,
245253
positionUs);
@@ -265,6 +273,7 @@ public PlaybackInfo copyWithPlaybackError(@Nullable ExoPlaybackException playbac
265273
loadingMediaPeriodId,
266274
playWhenReady,
267275
playbackSuppressionReason,
276+
playbackSpeed,
268277
bufferedPositionUs,
269278
totalBufferedDurationUs,
270279
positionUs);
@@ -290,6 +299,7 @@ public PlaybackInfo copyWithIsLoading(boolean isLoading) {
290299
loadingMediaPeriodId,
291300
playWhenReady,
292301
playbackSuppressionReason,
302+
playbackSpeed,
293303
bufferedPositionUs,
294304
totalBufferedDurationUs,
295305
positionUs);
@@ -315,6 +325,7 @@ public PlaybackInfo copyWithLoadingMediaPeriodId(MediaPeriodId loadingMediaPerio
315325
loadingMediaPeriodId,
316326
playWhenReady,
317327
playbackSuppressionReason,
328+
playbackSpeed,
318329
bufferedPositionUs,
319330
totalBufferedDurationUs,
320331
positionUs);
@@ -344,6 +355,33 @@ public PlaybackInfo copyWithPlayWhenReady(
344355
loadingMediaPeriodId,
345356
playWhenReady,
346357
playbackSuppressionReason,
358+
playbackSpeed,
359+
bufferedPositionUs,
360+
totalBufferedDurationUs,
361+
positionUs);
362+
}
363+
364+
/**
365+
* Copies playback info with new playback speed.
366+
*
367+
* @param playbackSpeed New playback speed. See {@link #playbackSpeed}.
368+
* @return Copied playback info with new playback speed.
369+
*/
370+
@CheckResult
371+
public PlaybackInfo copyWithPlaybackSpeed(float playbackSpeed) {
372+
return new PlaybackInfo(
373+
timeline,
374+
periodId,
375+
requestedContentPositionUs,
376+
playbackState,
377+
playbackError,
378+
isLoading,
379+
trackGroups,
380+
trackSelectorResult,
381+
loadingMediaPeriodId,
382+
playWhenReady,
383+
playbackSuppressionReason,
384+
playbackSpeed,
347385
bufferedPositionUs,
348386
totalBufferedDurationUs,
349387
positionUs);

0 commit comments

Comments
 (0)