Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion webrtc-media/src/main/cpp/include/media/MediaPacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion webrtc-media/src/main/cpp/src/media/MediaPacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ namespace ffmpeg
{
std::lock_guard<std::mutex> 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
Expand Down Expand Up @@ -303,9 +306,11 @@ namespace ffmpeg
// capacity.
audio_space_.notify_one();

++delivering_;
lock.unlock();
DeliverVideo(std::move(item), due);
lock.lock();
--delivering_;

delivered = true;
}
Expand All @@ -325,9 +330,11 @@ namespace ffmpeg

audio_space_.notify_one();

++delivering_;
lock.unlock();
DeliverAudio(item, due);
lock.lock();
--delivering_;

delivered = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading