Skip to content

Commit dc4148d

Browse files
marcbaechingerojw28
authored andcommitted
Add positions and new reasons to onPositionDiscontinuity
PiperOrigin-RevId: 364861539
1 parent f19ab4a commit dc4148d

File tree

26 files changed

+1586
-192
lines changed

26 files changed

+1586
-192
lines changed

RELEASENOTES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
### dev-v2 (not yet released)
44

5+
* Core Library:
6+
* Add position info of the old and the new position as arguments to
7+
`EventListener.onPositionDiscontinuity`. Add the new reasons
8+
`DISCONTINUITY_REASON_SKIP` and `DISCONTINUITY_REASON_REMOVE` and rename
9+
`DISCONTINUITY_REASON_PERIOD_TRANSITION` to
10+
`DISCONTINUITY_REASON_AUTO_TRANSITION`. Remove
11+
`DISCONTINUITY_REASON_AD_INSERTION` for which
12+
`DISCONTINUITY_REASON_AUTO_TRANSITION` is used instead. Deprecate the
13+
`onPositionDiscontinuity(int)` callback
14+
([#6163](https://github.com/google/ExoPlayer/issues/6163),
15+
[#4768](https://github.com/google/ExoPlayer/issues/4768)).
516
* UI:
617
* Add builder for `PlayerNotificationManager`.
718
* Add group setting to `PlayerNotificationManager`.

extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ public void seekTo(int windowIndex, long positionMs) {
460460
pendingSeekCount++;
461461
pendingSeekWindowIndex = windowIndex;
462462
pendingSeekPositionMs = positionMs;
463+
// TODO(b/181262841): call new onPositionDiscontinuity callback
463464
listeners.queueEvent(
464465
Player.EVENT_POSITION_DISCONTINUITY,
465466
listener -> listener.onPositionDiscontinuity(DISCONTINUITY_REASON_SEEK));
@@ -630,6 +631,8 @@ public long getContentBufferedPosition() {
630631

631632
// Internal methods.
632633

634+
// Call deprecated callbacks.
635+
@SuppressWarnings("deprecation")
633636
private void updateInternalStateAndNotifyIfChanged() {
634637
if (remoteMediaClient == null) {
635638
// There is no session. We leave the state of the player as it is now.
@@ -648,9 +651,10 @@ private void updateInternalStateAndNotifyIfChanged() {
648651
int currentWindowIndex = fetchCurrentWindowIndex(remoteMediaClient, currentTimeline);
649652
if (this.currentWindowIndex != currentWindowIndex && pendingSeekCount == 0) {
650653
this.currentWindowIndex = currentWindowIndex;
654+
// TODO(b/181262841): call new onPositionDiscontinuity callback
651655
listeners.queueEvent(
652656
Player.EVENT_POSITION_DISCONTINUITY,
653-
listener -> listener.onPositionDiscontinuity(DISCONTINUITY_REASON_PERIOD_TRANSITION));
657+
listener -> listener.onPositionDiscontinuity(DISCONTINUITY_REASON_AUTO_TRANSITION));
654658
listeners.queueEvent(
655659
Player.EVENT_MEDIA_ITEM_TRANSITION,
656660
listener ->

extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/AdTagLoader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,10 @@ public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason in
468468
}
469469

470470
@Override
471-
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
471+
public void onPositionDiscontinuity(
472+
Player.PositionInfo oldPosition,
473+
Player.PositionInfo newPosition,
474+
@Player.DiscontinuityReason int reason) {
472475
handleTimelineOrPositionChanged();
473476
}
474477

extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,10 @@ public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason in
613613
}
614614

615615
@Override
616-
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
616+
public void onPositionDiscontinuity(
617+
Player.PositionInfo oldPosition,
618+
Player.PositionInfo newPosition,
619+
@Player.DiscontinuityReason int reason) {
617620
maybeUpdateCurrentAdTagLoader();
618621
maybePreloadNextPeriodAds();
619622
}

extensions/ima/src/test/java/com/google/android/exoplayer2/ext/ima/FakePlayer.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
private final ListenerSet<EventListener> listeners;
3131
private final Timeline.Period period;
32+
private final Object windowUid = new Object();
33+
private final Object periodUid = new Object();
3234

3335
private Timeline timeline;
3436
@Player.State private int state;
@@ -65,16 +67,38 @@ public void updateTimeline(Timeline timeline, @TimelineChangeReason int reason)
6567
*/
6668
public void setPlayingContentPosition(int periodIndex, long positionMs) {
6769
boolean notify = isPlayingAd;
70+
PositionInfo oldPosition =
71+
new PositionInfo(
72+
windowUid,
73+
/* windowIndex= */ 0,
74+
periodUid,
75+
/* periodIndex= */ 0,
76+
this.positionMs,
77+
this.contentPositionMs,
78+
this.adGroupIndex,
79+
this.adIndexInAdGroup);
6880
isPlayingAd = false;
6981
adGroupIndex = C.INDEX_UNSET;
7082
adIndexInAdGroup = C.INDEX_UNSET;
7183
this.periodIndex = periodIndex;
7284
this.positionMs = positionMs;
7385
contentPositionMs = positionMs;
7486
if (notify) {
87+
PositionInfo newPosition =
88+
new PositionInfo(
89+
windowUid,
90+
/* windowIndex= */ 0,
91+
periodUid,
92+
/* periodIndex= */ 0,
93+
positionMs,
94+
this.contentPositionMs,
95+
this.adGroupIndex,
96+
this.adIndexInAdGroup);
7597
listeners.sendEvent(
7698
Player.EVENT_POSITION_DISCONTINUITY,
77-
listener -> listener.onPositionDiscontinuity(DISCONTINUITY_REASON_AD_INSERTION));
99+
listener ->
100+
listener.onPositionDiscontinuity(
101+
oldPosition, newPosition, DISCONTINUITY_REASON_AUTO_TRANSITION));
78102
}
79103
}
80104

@@ -90,16 +114,38 @@ public void setPlayingAdPosition(
90114
long positionMs,
91115
long contentPositionMs) {
92116
boolean notify = !isPlayingAd || this.adIndexInAdGroup != adIndexInAdGroup;
117+
PositionInfo oldPosition =
118+
new PositionInfo(
119+
windowUid,
120+
/* windowIndex= */ 0,
121+
periodUid,
122+
/* periodIndex= */ 0,
123+
this.positionMs,
124+
this.contentPositionMs,
125+
this.adGroupIndex,
126+
this.adIndexInAdGroup);
93127
isPlayingAd = true;
94128
this.periodIndex = periodIndex;
95129
this.adGroupIndex = adGroupIndex;
96130
this.adIndexInAdGroup = adIndexInAdGroup;
97131
this.positionMs = positionMs;
98132
this.contentPositionMs = contentPositionMs;
99133
if (notify) {
134+
PositionInfo newPosition =
135+
new PositionInfo(
136+
windowUid,
137+
/* windowIndex= */ 0,
138+
periodUid,
139+
/* periodIndex= */ 0,
140+
positionMs,
141+
contentPositionMs,
142+
adGroupIndex,
143+
adIndexInAdGroup);
100144
listeners.sendEvent(
101145
EVENT_POSITION_DISCONTINUITY,
102-
listener -> listener.onPositionDiscontinuity(DISCONTINUITY_REASON_AD_INSERTION));
146+
listener ->
147+
listener.onPositionDiscontinuity(
148+
oldPosition, newPosition, DISCONTINUITY_REASON_AUTO_TRANSITION));
103149
}
104150
}
105151

extensions/ima/src/test/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoaderTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,26 @@ public void startAndCallbacksAfterRelease() {
281281
videoAdPlayer.pauseAd(TEST_AD_MEDIA_INFO);
282282
videoAdPlayer.stopAd(TEST_AD_MEDIA_INFO);
283283
imaAdsLoader.onPlayerError(ExoPlaybackException.createForSource(new IOException()));
284-
imaAdsLoader.onPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK);
284+
imaAdsLoader.onPositionDiscontinuity(
285+
new Player.PositionInfo(
286+
/* windowUid= */ new Object(),
287+
/* windowIndex= */ 0,
288+
/* periodUid= */ new Object(),
289+
/* periodIndex= */ 0,
290+
/* positionMs= */ 10_000,
291+
/* contentPositionMs= */ 0,
292+
/* adGroupIndex= */ -1,
293+
/* adIndexInAdGroup= */ -1),
294+
new Player.PositionInfo(
295+
/* windowUid= */ new Object(),
296+
/* windowIndex= */ 1,
297+
/* periodUid= */ new Object(),
298+
/* periodIndex= */ 0,
299+
/* positionMs= */ 20_000,
300+
/* contentPositionMs= */ 0,
301+
/* adGroupIndex= */ -1,
302+
/* adIndexInAdGroup= */ -1),
303+
Player.DISCONTINUITY_REASON_SEEK);
285304
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */ null));
286305
imaAdsLoader.handlePrepareError(
287306
adsMediaSource, /* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());

extensions/leanback/src/main/java/com/google/android/exoplayer2/ext/leanback/LeanbackPlayerAdapter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ public void onTimelineChanged(Timeline timeline, @TimelineChangeReason int reaso
306306
}
307307

308308
@Override
309-
public void onPositionDiscontinuity(@DiscontinuityReason int reason) {
309+
public void onPositionDiscontinuity(
310+
Player.PositionInfo oldPosition,
311+
Player.PositionInfo newPosition,
312+
@DiscontinuityReason int reason) {
310313
Callback callback = getCallback();
311314
callback.onCurrentPositionChanged(LeanbackPlayerAdapter.this);
312315
callback.onBufferedPositionChanged(LeanbackPlayerAdapter.this);

extensions/media2/src/main/java/com/google/android/exoplayer2/ext/media2/PlayerWrapper.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ private void handlePlayerStateChanged() {
437437
case Player.STATE_READY:
438438
if (!prepared) {
439439
prepared = true;
440-
handlePositionDiscontinuity(Player.DISCONTINUITY_REASON_PERIOD_TRANSITION);
440+
handlePositionDiscontinuity(Player.DISCONTINUITY_REASON_AUTO_TRANSITION);
441441
listener.onPrepared(
442442
Assertions.checkNotNull(getCurrentMediaItem()), player.getBufferedPercentage());
443443
}
@@ -517,9 +517,11 @@ private void handlePositionDiscontinuity(@Player.DiscontinuityReason int reason)
517517
int currentWindowIndex = getCurrentMediaItemIndex();
518518
if (this.currentWindowIndex != currentWindowIndex) {
519519
this.currentWindowIndex = currentWindowIndex;
520-
androidx.media2.common.MediaItem currentMediaItem =
521-
Assertions.checkNotNull(getCurrentMediaItem());
522-
listener.onCurrentMediaItemChanged(currentMediaItem);
520+
if (currentWindowIndex != C.INDEX_UNSET) {
521+
androidx.media2.common.MediaItem currentMediaItem =
522+
Assertions.checkNotNull(getCurrentMediaItem());
523+
listener.onCurrentMediaItemChanged(currentMediaItem);
524+
}
523525
} else {
524526
listener.onSeekCompleted();
525527
}
@@ -597,7 +599,10 @@ public void onPlaybackStateChanged(@Player.State int state) {
597599
}
598600

599601
@Override
600-
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
602+
public void onPositionDiscontinuity(
603+
Player.PositionInfo oldPosition,
604+
Player.PositionInfo newPosition,
605+
@Player.DiscontinuityReason int reason) {
601606
handlePositionDiscontinuity(reason);
602607
}
603608

0 commit comments

Comments
 (0)