Keep pending initial position when setting empty playlist

MediaControllerImplBase currently drops the pending initial seek
position when a user sets an empty playlist.

When seeking in empty playlists and setting new empty playlists,
the class also drops the the period index (and wrongly assigns zero
instead of the windowIndex).

#minor-release

PiperOrigin-RevId: 534035046
This commit is contained in:
tonihei 2023-05-22 13:32:46 +01:00
parent 6de6bd9c4f
commit caf1c77af1
2 changed files with 38 additions and 5 deletions

View File

@ -1804,7 +1804,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
} else if (startIndex == C.INDEX_UNSET) {
startIndex = playerInfo.sessionPositionInfo.positionInfo.mediaItemIndex;
startPositionMs = playerInfo.sessionPositionInfo.positionInfo.positionMs;
if (startIndex >= newTimeline.getWindowCount()) {
if (!newTimeline.isEmpty() && startIndex >= newTimeline.getWindowCount()) {
correctedStartIndex = true;
startIndex = newTimeline.getFirstWindowIndex(playerInfo.shuffleModeEnabled);
startPositionMs = C.TIME_UNSET;
@ -1821,7 +1821,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
startIndex,
/* mediaItem= */ null,
/* periodUid= */ null,
/* periodIndex= */ 0,
startIndex,
/* positionMs= */ startPositionMs == C.TIME_UNSET ? 0 : startPositionMs,
/* contentPositionMs= */ startPositionMs == C.TIME_UNSET ? 0 : startPositionMs,
/* adGroupIndex= */ C.INDEX_UNSET,
@ -1987,7 +1987,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
windowIndex,
/* mediaItem= */ null,
/* periodUid= */ null,
/* periodIndex= */ 0,
windowIndex,
/* positionMs= */ positionMs == C.TIME_UNSET ? 0 : positionMs,
/* contentPositionMs= */ positionMs == C.TIME_UNSET ? 0 : positionMs,
/* adGroupIndex= */ C.INDEX_UNSET,

View File

@ -1308,7 +1308,7 @@ public class MediaControllerStateMaskingTest {
int initialBufferedPosition = 0;
int initialTotalBufferedPosition = 0;
int testMediaItemIndex = 3;
int testPeriodIndex = initialPeriodIndex;
int testPeriodIndex = 3;
long testSeekPositionMs = 3_000;
long testPosition = testSeekPositionMs;
long testBufferedPosition = testSeekPositionMs;
@ -1576,6 +1576,39 @@ public class MediaControllerStateMaskingTest {
assertThat(totalBufferedDurationRef.get()).isEqualTo(testTotalBufferedDuration);
}
@Test
public void setMediaItems_toEmptyListAndResetPositionFalse_correctMasking() throws Exception {
Bundle playerConfig =
new RemoteMediaSession.MockPlayerConfigBuilder()
.setCurrentMediaItemIndex(2)
.setCurrentPeriodIndex(2)
.setCurrentPosition(8000)
.setContentPosition(8000)
.build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
AtomicReference<Timeline> currentTimelineRef = new AtomicReference<>();
AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
AtomicLong currentPositionRef = new AtomicLong();
AtomicInteger currentPeriodIndexRef = new AtomicInteger();
threadTestRule
.getHandler()
.postAndSync(
() -> {
controller.setMediaItems(ImmutableList.of(), /* resetPosition= */ false);
currentTimelineRef.set(controller.getCurrentTimeline());
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
currentPositionRef.set((int) controller.getCurrentPosition());
currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
});
assertThat(currentPositionRef.get()).isEqualTo(8000);
assertThat(currentTimelineRef.get().isEmpty()).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(2);
assertThat(currentPeriodIndexRef.get()).isEqualTo(2);
}
@Test
public void setMediaItems_withStartMediaItemIndexAndStartPosition() throws Exception {
int initialMediaItemIndex = 2;
@ -1687,7 +1720,7 @@ public class MediaControllerStateMaskingTest {
long initialTotalBufferedDuration = 1_200;
List<MediaItem> testMediaItemList = new ArrayList<>();
int testMediaItemIndex = 1;
int testPeriodIndex = 0;
int testPeriodIndex = 1;
long testPosition = 1_000;
long testBufferedPosition = 1_000;
long testTotalBufferedDuration = 0;