Skip to content

Commit 5e293ce

Browse files
committed
fix: keep what a seek decoded at the new position
MediaPlayer::Seek flushed the pacer after releasing its lock. When the decode thread performed the seek in between and decoded at the new position, that flush threw it away: an audio source, decoded seconds ahead, skipped ahead or ended at once. The flush now happens under the lock the decode thread takes the request with. This made MediaPlayerTest.positionFollowsABackwardSeekOfAudio fail now and then, which now also checks that playback goes on.
1 parent 704a166 commit 5e293ce

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎webrtc-java-media/src/main/cpp/src/media/MediaPlayer.cpp‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,28 @@ namespace ffmpeg
156156

157157
void MediaPlayer::Seek(int64_t position_us)
158158
{
159-
{
160-
std::lock_guard<std::mutex> lock(mutex_);
161-
162-
if (closing_) {
163-
return;
164-
}
165-
166-
seek_pending_ = true;
167-
seek_position_us_ = position_us;
168-
ended_ = false;
159+
std::lock_guard<std::mutex> lock(mutex_);
169160

170-
command_.notify_all();
161+
if (closing_) {
162+
return;
171163
}
172164

165+
seek_pending_ = true;
166+
seek_position_us_ = position_us;
167+
ended_ = false;
168+
173169
// The decode thread may be held up by a full queue, and it cannot act
174170
// on the request until it gets out of that. Dropping what is queued
175171
// both frees it and throws away what belongs to the old position; the
176172
// thread flushes again once the seek has actually happened.
173+
//
174+
// Under the lock, as the thread takes the request only under it: a
175+
// flush after that would throw away what it has decoded at the new
176+
// position, and an audio source, decoded seconds ahead, would skip
177+
// ahead or end at once.
177178
pacer_->Flush(position_us + loop_offset_us_.load());
179+
180+
command_.notify_all();
178181
}
179182

180183
void MediaPlayer::SetLooping(bool looping)

‎webrtc-java-media/src/test/java/dev/onvoid/webrtc/media/player/MediaPlayerTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ void positionFollowsABackwardSeekOfAudio() throws Exception {
335335
// About 0.4 s into the source again, not held at where it was.
336336
assertTrue(after < before / 2, "position after seeking back: " + after
337337
+ " us, " + before + " us before it");
338+
// And still playing: the source is decoded to its end long before
339+
// it plays out, and a flush that came after the seek had happened
340+
// threw away all of it, so that the player ended at once.
341+
assertEquals(MediaPlayerState.PLAYING, playback.player.getState());
342+
assertTrue(after > 0, "position after seeking back: " + after + " us");
338343
}
339344
}
340345

0 commit comments

Comments
 (0)