Fix message indexing bug.

We keep an index hint for the next pending player message. This hint
wasn't updated correctly when messages are removed due to a timeline
update.

This change makes sure to only use the hint locally in one method so
that it doesn't need to be updated anywhere else and also adds the "hint"
suffix to the variable name to make it clearer that it's just a hint and
there are no guarantees this index actually exists anymore.

issue:#7278
PiperOrigin-RevId: 309217614
This commit is contained in:
tonihei 2020-04-30 15:01:44 +01:00 committed by Oliver Woodman
parent 1950905990
commit 85bc1d6e14
3 changed files with 48 additions and 8 deletions

View file

@ -6,6 +6,9 @@
* Add opt-in to verify correct thread usage with
`SimpleExoPlayer.setThrowsWhenUsingWrongThread(true)`
([#4463](https://github.com/google/ExoPlayer/issues/4463)).
* Fix bug where `PlayerMessages` throw an exception after `MediaSources`
are removed from the playlist
([#7278](https://github.com/google/ExoPlayer/issues/7278)).
* Add playbackPositionUs parameter to 'LoadControl.shouldContinueLoading'.
* The `DefaultLoadControl` default minimum buffer is set to 50 seconds,
equal to the default maximum buffer. `DefaultLoadControl` applies the
@ -107,10 +110,6 @@
* Implement timing-out of stuck CEA-608 captions (as permitted by
ANSI/CTA-608-E R-2014 Annex C.9) and set the default timeout to 16
seconds ([#7181](https://github.com/google/ExoPlayer/issues/7181)).
* Add special-case positioning behaviour for vertical cues being rendered
horizontally.
* Implement steps 4-10 of the
[WebVTT line computation algorithm](https://www.w3.org/TR/webvtt1/#cue-computed-line).
* DRM:
* Add support for attaching DRM sessions to clear content in the demo app.
* Remove `DrmSessionManager` references from all renderers.
@ -151,8 +150,6 @@
* Upgrade Truth dependency from 0.44 to 1.0.
* Upgrade to JUnit 4.13-rc-2.
* UI
* Remove deperecated `exo_simpe_player_view.xml` and
`exo_playback_control_view.xml` from resource.
* Add `showScrubber` and `hideScrubber` methods to DefaultTimeBar.
* Move logic of prev, next, fast forward and rewind to ControlDispatcher
([#6926](https://github.com/google/ExoPlayer/issues/6926)).

View file

@ -131,7 +131,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
private int enabledRendererCount;
@Nullable private SeekPosition pendingInitialSeekPosition;
private long rendererPositionUs;
private int nextPendingMessageIndex;
private int nextPendingMessageIndexHint;
private boolean deliverPendingMessageAtStartPositionRequired;
private long releaseTimeoutMs;
@ -1191,7 +1191,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
pendingMessageInfo.message.markAsProcessed(/* isDelivered= */ false);
}
pendingMessages.clear();
nextPendingMessageIndex = 0;
resetPosition = true;
}
MediaPeriodId mediaPeriodId = playbackInfo.periodId;
@ -1365,6 +1364,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
// Correct next index if necessary (e.g. after seeking, timeline changes, or new messages)
int currentPeriodIndex =
playbackInfo.timeline.getIndexOfPeriod(playbackInfo.periodId.periodUid);
int nextPendingMessageIndex = Math.min(nextPendingMessageIndexHint, pendingMessages.size());
PendingMessageInfo previousInfo =
nextPendingMessageIndex > 0 ? pendingMessages.get(nextPendingMessageIndex - 1) : null;
while (previousInfo != null
@ -1410,6 +1410,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
? pendingMessages.get(nextPendingMessageIndex)
: null;
}
nextPendingMessageIndexHint = nextPendingMessageIndex;
}
private void ensureStopped(Renderer renderer) throws ExoPlaybackException {

View file

@ -19,6 +19,11 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;
import android.content.Context;
@ -2313,6 +2318,43 @@ public final class ExoPlayerTest {
assertThat(target.messageCount).isEqualTo(1);
}
@Test
public void sendMessages_withMediaRemoval_triggersCorrectMessagesAndDoesNotThrow()
throws Exception {
ExoPlayer player = new TestExoPlayer.Builder(context).build();
MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(/* windowCount= */ 1));
player.addMediaSources(Arrays.asList(mediaSource, mediaSource));
player
.createMessage((messageType, payload) -> {})
.setPosition(/* windowIndex= */ 0, /* positionMs= */ 0)
.setDeleteAfterDelivery(false)
.send();
PlayerMessage.Target secondMediaItemTarget = mock(PlayerMessage.Target.class);
player
.createMessage(secondMediaItemTarget)
.setPosition(/* windowIndex= */ 1, /* positionMs= */ 0)
.setDeleteAfterDelivery(false)
.send();
// Play through media once to trigger all messages. This ensures any internally saved message
// indices are non-zero.
player.prepare();
player.play();
TestExoPlayer.runUntilPlaybackState(player, Player.STATE_ENDED);
verify(secondMediaItemTarget).handleMessage(anyInt(), any());
// Remove first item and play second item again to check if message is triggered again.
// After removal, any internally saved message indices are invalid and will throw
// IndexOutOfBoundsException if used without updating.
// See https://github.com/google/ExoPlayer/issues/7278.
player.removeMediaItem(/* index= */ 0);
player.seekTo(/* positionMs= */ 0);
TestExoPlayer.runUntilPlaybackState(player, Player.STATE_ENDED);
assertThat(player.getPlayerError()).isNull();
verify(secondMediaItemTarget, times(2)).handleMessage(anyInt(), any());
}
@Test
public void setAndSwitchSurface() throws Exception {
final List<Integer> rendererMessages = new ArrayList<>();