diff --git a/webrtc-media/src/main/cpp/include/media/MediaPacer.h b/webrtc-media/src/main/cpp/include/media/MediaPacer.h index e3f443847..54732bab2 100644 --- a/webrtc-media/src/main/cpp/include/media/MediaPacer.h +++ b/webrtc-media/src/main/cpp/include/media/MediaPacer.h @@ -109,7 +109,8 @@ namespace ffmpeg // delivered, in the timestamps the items are pushed with. void Flush(int64_t position_us); - // True once everything handed over has been delivered. + // True once everything handed over has been delivered: the + // queues are empty and no item is being handed to a source. bool IsDrained() const; // The presentation time of the item delivered last, which is where @@ -171,6 +172,9 @@ namespace ffmpeg bool running_ = false; bool paused_ = false; + // Items taken from a queue whose delivery has not returned yet. + int delivering_ = 0; + // Presentation time plus this is a time on the WebRTC clock. int64_t base_us_ = 0; bool have_base_ = false; diff --git a/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp b/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp index 1cb9f9b41..d8d6ef99a 100644 --- a/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp +++ b/webrtc-media/src/main/cpp/src/media/MediaPacer.cpp @@ -242,7 +242,10 @@ namespace ffmpeg { std::lock_guard lock(mutex_); - return video_queue_.empty() && audio_queue_.empty(); + // An item leaves its queue before it is handed over, so empty queues + // alone would call the source played out while its last item is + // still on its way into WebRTC. + return video_queue_.empty() && audio_queue_.empty() && delivering_ == 0; } int64_t MediaPacer::GetPositionUs() const @@ -303,9 +306,11 @@ namespace ffmpeg // capacity. audio_space_.notify_one(); + ++delivering_; lock.unlock(); DeliverVideo(std::move(item), due); lock.lock(); + --delivering_; delivered = true; } @@ -325,9 +330,11 @@ namespace ffmpeg audio_space_.notify_one(); + ++delivering_; lock.unlock(); DeliverAudio(item, due); lock.lock(); + --delivering_; delivered = true; } diff --git a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaPlayerTest.java b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaPlayerTest.java index db4ba08c4..3eb17a55a 100644 --- a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaPlayerTest.java +++ b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaPlayerTest.java @@ -689,6 +689,59 @@ void closesFromAudioSink() throws Exception { } } + @Test + void endsOnlyOnceTheLastDeliveryHasReturned() throws Exception { + try (Sources sources = new Sources()) { + AudioTrack track = factory.createAudioTrack("audio", sources.audio); + MediaPlayer player = new MediaPlayer(new MediaReader(asset()), + sources.video, sources.audio); + CountDownLatch ended = new CountDownLatch(1); + AtomicInteger held = new AtomicInteger(); + AtomicInteger endedWhileHeld = new AtomicInteger(); + + player.setListener(new MediaPlayerListener() { + + @Override + public void onEndOfStream() { + ended.countDown(); + } + }); + + // Every chunk, the last one among them, is held on the pacing + // thread, by then out of its queue and on its way into WebRTC. An + // end of the stream reported while one is held comes before + // everything has been delivered, and an application closing a + // recorder or a receiver on it would cut that chunk off. + AudioTrackSink sink = (data, bits, rate, ch, count) -> { + held.incrementAndGet(); + + try { + if (ended.await(200, TimeUnit.MILLISECONDS)) { + endedWhileHeld.incrementAndGet(); + } + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }; + + track.addSink(sink); + + // Close to the end, so that only a few chunks are held. + player.seek(2_950_000); + player.play(); + + assertTrue(ended.await(10, TimeUnit.SECONDS), "not ended"); + assertTrue(held.get() > 0, "no audio"); + assertEquals(0, endedWhileHeld.get(), "ended while a chunk was being delivered"); + + player.close(); + + track.removeSink(sink); + track.dispose(); + } + } + @Test void outlivesDisposedSources() throws Exception { CustomVideoSource videoSource = new CustomVideoSource();