From a67d260e649bb1ffd4e314783736040aebc89f9e Mon Sep 17 00:00:00 2001 From: kimvde Date: Fri, 5 Mar 2021 08:59:34 +0000 Subject: [PATCH] Fix onPositionDiscontinuity event in CastPlayer - Avoid having two onPositionDiscontinuity events (seek and transition) sent after a seek to another media item. - Avoid triggering an onPositionDiscontinuity event after a timeline change. #minor-release PiperOrigin-RevId: 361092914 --- RELEASENOTES.md | 4 ++ .../exoplayer2/ext/cast/CastPlayer.java | 39 +++++++++++++------ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 32b4cb8efa..6e8d205fc5 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -45,6 +45,10 @@ * MediaSession extension: Remove dependency to core module and rely on common only. The `TimelineQueueEditor` uses a new `MediaDescriptionConverter` for this purpose and does not rely on the `ConcatenatingMediaSource` anymore. +* Cast extension: + * Fix `onPositionDiscontinuity` event so that it is not triggered with + reason `DISCONTINUITY_REASON_PERIOD_TRANSITION` after a seek to another + media item and so that it is not triggered after a timeline change. ### 2.13.2 (2021-02-25) diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java index d20b84cbc3..4f75ba9d6e 100644 --- a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java +++ b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/CastPlayer.java @@ -653,15 +653,7 @@ public final class CastPlayer extends BasePlayer { updateRepeatModeAndNotifyIfChanged(/* resultCallback= */ null); updateTimelineAndNotifyIfChanged(); - int currentWindowIndex = C.INDEX_UNSET; - MediaQueueItem currentItem = remoteMediaClient.getCurrentItem(); - if (currentItem != null) { - currentWindowIndex = currentTimeline.getIndexOfPeriod(currentItem.getItemId()); - } - if (currentWindowIndex == C.INDEX_UNSET) { - // The timeline is empty. Fall back to index 0, which is what ExoPlayer would do. - currentWindowIndex = 0; - } + int currentWindowIndex = fetchCurrentWindowIndex(remoteMediaClient, currentTimeline); if (this.currentWindowIndex != currentWindowIndex && pendingSeekCount == 0) { this.currentWindowIndex = currentWindowIndex; listeners.queueEvent( @@ -721,7 +713,9 @@ public final class CastPlayer extends BasePlayer { } /** - * Updates the current timeline and returns whether it has changed. + * Updates the current timeline. The current window index may change as a result. + * + * @return Whether the current timeline has changed. */ private boolean updateTimeline() { CastTimeline oldTimeline = currentTimeline; @@ -730,7 +724,11 @@ public final class CastPlayer extends BasePlayer { status != null ? timelineTracker.getCastTimeline(remoteMediaClient) : CastTimeline.EMPTY_CAST_TIMELINE; - return !oldTimeline.equals(currentTimeline); + boolean timelineChanged = !oldTimeline.equals(currentTimeline); + if (timelineChanged) { + currentWindowIndex = fetchCurrentWindowIndex(remoteMediaClient, currentTimeline); + } + return timelineChanged; } /** Updates the internal tracks and selection and returns whether they have changed. */ @@ -940,6 +938,24 @@ public final class CastPlayer extends BasePlayer { } } + private static int fetchCurrentWindowIndex( + @Nullable RemoteMediaClient remoteMediaClient, Timeline timeline) { + if (remoteMediaClient == null) { + return 0; + } + + int currentWindowIndex = C.INDEX_UNSET; + @Nullable MediaQueueItem currentItem = remoteMediaClient.getCurrentItem(); + if (currentItem != null) { + currentWindowIndex = timeline.getIndexOfPeriod(currentItem.getItemId()); + } + if (currentWindowIndex == C.INDEX_UNSET) { + // The timeline is empty. Fall back to index 0, which is what ExoPlayer would do. + currentWindowIndex = 0; + } + return currentWindowIndex; + } + private static boolean isTrackActive(long id, long[] activeTrackIds) { for (long activeTrackId : activeTrackIds) { if (activeTrackId == id) { @@ -1078,6 +1094,7 @@ public final class CastPlayer extends BasePlayer { + CastUtils.getLogString(statusCode)); } if (--pendingSeekCount == 0) { + currentWindowIndex = pendingSeekWindowIndex; pendingSeekWindowIndex = C.INDEX_UNSET; pendingSeekPositionMs = C.TIME_UNSET; listeners.sendEvent(/* eventFlag= */ C.INDEX_UNSET, EventListener::onSeekProcessed);