Skip to content

Commit ff3e1e4

Browse files
committed
fix: report end of stream only after the last delivery has returned
The pacer takes an item out of its queue before handing it to WebRTC, so empty queues alone made IsDrained() true while the last audio or video delivery was still running. Count deliveries in flight and drain only once none are left.
1 parent 2dd25d2 commit ff3e1e4

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎webrtc-media/src/main/cpp/include/media/MediaPacer.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ namespace ffmpeg
109109
// delivered, in the timestamps the items are pushed with.
110110
void Flush(int64_t position_us);
111111

112-
// True once everything handed over has been delivered.
112+
// True once everything handed over has been delivered: the
113+
// queues are empty and no item is being handed to a source.
113114
bool IsDrained() const;
114115

115116
// The presentation time of the item delivered last, which is where
@@ -171,6 +172,9 @@ namespace ffmpeg
171172
bool running_ = false;
172173
bool paused_ = false;
173174

175+
// Items taken from a queue whose delivery has not returned yet.
176+
int delivering_ = 0;
177+
174178
// Presentation time plus this is a time on the WebRTC clock.
175179
int64_t base_us_ = 0;
176180
bool have_base_ = false;

‎webrtc-media/src/main/cpp/src/media/MediaPacer.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ namespace ffmpeg
242242
{
243243
std::lock_guard<std::mutex> lock(mutex_);
244244

245-
return video_queue_.empty() && audio_queue_.empty();
245+
// An item leaves its queue before it is handed over, so empty queues
246+
// alone would call the source played out while its last item is
247+
// still on its way into WebRTC.
248+
return video_queue_.empty() && audio_queue_.empty() && delivering_ == 0;
246249
}
247250

248251
int64_t MediaPacer::GetPositionUs() const
@@ -303,9 +306,11 @@ namespace ffmpeg
303306
// capacity.
304307
audio_space_.notify_one();
305308

309+
++delivering_;
306310
lock.unlock();
307311
DeliverVideo(std::move(item), due);
308312
lock.lock();
313+
--delivering_;
309314

310315
delivered = true;
311316
}
@@ -325,9 +330,11 @@ namespace ffmpeg
325330

326331
audio_space_.notify_one();
327332

333+
++delivering_;
328334
lock.unlock();
329335
DeliverAudio(item, due);
330336
lock.lock();
337+
--delivering_;
331338

332339
delivered = true;
333340
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,59 @@ void closesFromAudioSink() throws Exception {
689689
}
690690
}
691691

692+
@Test
693+
void endsOnlyOnceTheLastDeliveryHasReturned() throws Exception {
694+
try (Sources sources = new Sources()) {
695+
AudioTrack track = factory.createAudioTrack("audio", sources.audio);
696+
MediaPlayer player = new MediaPlayer(new MediaReader(asset()),
697+
sources.video, sources.audio);
698+
CountDownLatch ended = new CountDownLatch(1);
699+
AtomicInteger held = new AtomicInteger();
700+
AtomicInteger endedWhileHeld = new AtomicInteger();
701+
702+
player.setListener(new MediaPlayerListener() {
703+
704+
@Override
705+
public void onEndOfStream() {
706+
ended.countDown();
707+
}
708+
});
709+
710+
// Every chunk, the last one among them, is held on the pacing
711+
// thread, by then out of its queue and on its way into WebRTC. An
712+
// end of the stream reported while one is held comes before
713+
// everything has been delivered, and an application closing a
714+
// recorder or a receiver on it would cut that chunk off.
715+
AudioTrackSink sink = (data, bits, rate, ch, count) -> {
716+
held.incrementAndGet();
717+
718+
try {
719+
if (ended.await(200, TimeUnit.MILLISECONDS)) {
720+
endedWhileHeld.incrementAndGet();
721+
}
722+
}
723+
catch (InterruptedException e) {
724+
Thread.currentThread().interrupt();
725+
}
726+
};
727+
728+
track.addSink(sink);
729+
730+
// Close to the end, so that only a few chunks are held.
731+
player.seek(2_950_000);
732+
player.play();
733+
734+
assertTrue(ended.await(10, TimeUnit.SECONDS), "not ended");
735+
assertTrue(held.get() > 0, "no audio");
736+
assertEquals(0, endedWhileHeld.get(), "ended while a chunk was being delivered");
737+
738+
player.close();
739+
740+
track.removeSink(sink);
741+
track.dispose();
742+
}
743+
}
744+
692745
@Test
693746
void outlivesDisposedSources() throws Exception {
694747
CustomVideoSource videoSource = new CustomVideoSource();

0 commit comments

Comments
 (0)