Skip to content

Truly independent web browser

LibMedia+Tests: Expose whether video decoders have pending decodes

Zaggy102407ee7b3d1c70
diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
index bef8e85e170..8b928830571 100644
--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
@@ -223,7 +223,9 @@ DecoderErrorOr<void> DecodedVideoProducer::ThreadData::create_decoder_for_frame(
wait_state->condition.broadcast();
});
// Output that arrives while the decode thread reads coded data is taken as soon as the read returns.
- m_decoder->set_output_arrived_callback([wait_state = m_wait_state, demuxer = m_demuxer, track = m_track] {
+ m_decoder->set_decode_completed_callback([wait_state = m_wait_state, demuxer = m_demuxer, track = m_track](DecodeCompletion completion) {
+ if (completion != DecodeCompletion::ProducedOutput)
+ return;
if (wait_state->output_should_interrupt_demuxer_reads.load(MemoryOrder::memory_order_relaxed))
demuxer->set_blocking_reads_aborted_for_track(track);
});
diff --git a/Libraries/LibMedia/RemoteVideoDecoder.cpp b/Libraries/LibMedia/RemoteVideoDecoder.cpp
index a1b06baf12f..fe80e7f2d57 100644
--- a/Libraries/LibMedia/RemoteVideoDecoder.cpp
+++ b/Libraries/LibMedia/RemoteVideoDecoder.cpp
@@ -72,6 +72,7 @@ struct RemoteVideoDecoder::State : public AtomicRefCounted<State> {
auto forgotten_outputs = move(outputs);
server_is_waiting_for_storage = false;
submissions_in_flight.clear();
+ server_decoder_has_any_decode_in_progress = false;
failure.clear();
server_decoder_state_changed.broadcast();
drained = false;
@@ -89,12 +90,13 @@ struct RemoteVideoDecoder::State : public AtomicRefCounted<State> {
Vector<Output> outputs;
Vector<SubmissionInFlight> submissions_in_flight;
+ bool server_decoder_has_any_decode_in_progress { false };
Optional<DecoderError> failure;
bool drained { false };
// Called under the mutex when the server reports something the decoder's caller is not already waiting for, so
// that destroying the decoder, which clears it, ends its calls.
- Function<void()> output_arrived_callback;
+ Function<void(DecodeCompletion)> decode_completed_callback;
bool caller_is_waiting_for_output { false };
// Set once before any frame is submitted, and called after the mutex is released.
@@ -118,10 +120,17 @@ struct RemoteVideoDecoder::State : public AtomicRefCounted<State> {
return clear_server_wait_for_storage_while_locked();
}
- void tell_caller_of_new_state_while_locked()
+ bool has_any_decode_in_progress_while_locked() const
{
- if (!caller_is_waiting_for_output && output_arrived_callback)
- output_arrived_callback();
+ if (submissions_in_flight_of_current_generation() > 0 || server_decoder_has_any_decode_in_progress)
+ return true;
+ return !outputs.is_empty() || failure.has_value();
+ }
+
+ void tell_caller_of_new_state_while_locked(DecodeCompletion completion)
+ {
+ if (!caller_is_waiting_for_output && decode_completed_callback)
+ decode_completed_callback(completion);
}
};
@@ -345,7 +354,7 @@ private:
if (state->decoder_id != decoder_id)
continue;
forgotten_outputs = state->forget_server_while_locked();
- state->tell_caller_of_new_state_while_locked();
+ state->tell_caller_of_new_state_while_locked(DecodeCompletion::ProducedOutput);
}
}
@@ -357,7 +366,7 @@ private:
return nullptr;
}
- // What the decoder's caller is told of once the mutex is released: new state to take, through the output arrived
+ // What the decoder's caller is told of once the mutex is released: new state to take, through the decode completed
// callback unless it is already waiting, and the end of a wait for storage it was answered TryAgain for, through
// the storage freed callback.
struct CallerWakeup {
@@ -377,10 +386,13 @@ private:
MutexLocker locker { state->mutex };
if (state->decoder_id != decoder_id || state->decoder_was_destroyed)
return false;
+ auto any_decode_was_in_progress = state->has_any_decode_in_progress_while_locked();
wakeup = callback(*state);
state->server_decoder_state_changed.broadcast();
if (wakeup.has_something_to_take)
- state->tell_caller_of_new_state_while_locked();
+ state->tell_caller_of_new_state_while_locked(DecodeCompletion::ProducedOutput);
+ else if (any_decode_was_in_progress && !state->has_any_decode_in_progress_while_locked())
+ state->tell_caller_of_new_state_while_locked(DecodeCompletion::ProducedNothing);
}
if (wakeup.storage_was_freed && state->storage_freed_callback)
state->storage_freed_callback();
@@ -449,6 +461,14 @@ private:
});
}
+ virtual void any_decode_in_progress_changed(u64 decoder_id, bool any_decode_is_in_progress) override
+ {
+ with_decoder(decoder_id, [&](RemoteVideoDecoder::State& state) -> CallerWakeup {
+ state.server_decoder_has_any_decode_in_progress = any_decode_is_in_progress;
+ return {};
+ });
+ }
+
virtual void decoder_failed(u64 decoder_id, u32 generation, DecoderError error) override
{
with_decoder(decoder_id, [&](RemoteVideoDecoder::State& state) -> CallerWakeup {
@@ -538,7 +558,7 @@ RemoteVideoDecoder::~RemoteVideoDecoder()
decoder_id = m_state->decoder_id;
outputs = move(m_state->outputs);
m_state->decoder_was_destroyed = true;
- m_state->output_arrived_callback = nullptr;
+ m_state->decode_completed_callback = nullptr;
}
connection.with_target([&](auto& connection) {
connection.async_destroy_decoder(decoder_id);
@@ -592,10 +612,16 @@ void RemoteVideoDecoder::set_storage_freed_callback(Function<void()> callback)
m_state->storage_freed_callback = move(callback);
}
-void RemoteVideoDecoder::set_output_arrived_callback(Function<void()> callback)
+void RemoteVideoDecoder::set_decode_completed_callback(Function<void(DecodeCompletion)> callback)
+{
+ MutexLocker locker { m_state->mutex };
+ m_state->decode_completed_callback = move(callback);
+}
+
+bool RemoteVideoDecoder::has_any_decode_in_progress() const
{
MutexLocker locker { m_state->mutex };
- m_state->output_arrived_callback = move(callback);
+ return m_state->has_any_decode_in_progress_while_locked();
}
DecoderError RemoteVideoDecoder::report_server_loss()
diff --git a/Libraries/LibMedia/RemoteVideoDecoder.h b/Libraries/LibMedia/RemoteVideoDecoder.h
index 0b57e11cd17..6fb6e30ffac 100644
--- a/Libraries/LibMedia/RemoteVideoDecoder.h
+++ b/Libraries/LibMedia/RemoteVideoDecoder.h
@@ -50,7 +50,8 @@ public:
virtual ~RemoteVideoDecoder() override;
virtual void set_storage_freed_callback(Function<void()>) override;
- virtual void set_output_arrived_callback(Function<void()>) override;
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)>) override;
+ virtual bool has_any_decode_in_progress() const override;
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) override;
virtual void signal_end_of_stream() override;
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.h
index 327a2aa1ddf..6942ffdb1de 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.h
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.h
@@ -30,7 +30,8 @@ public:
virtual ~FFmpegVideoDecoder() override;
virtual void set_storage_freed_callback(Function<void()> callback) override { m_frame_pool->set_slot_freed_callback(move(callback)); }
- virtual void set_output_arrived_callback(Function<void()>) override { }
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)>) override { }
+ virtual bool has_any_decode_in_progress() const override { return false; }
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) override;
virtual void signal_end_of_stream() override;
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
index 09541768f40..83a335fe8cb 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.cpp
@@ -682,6 +682,7 @@ void FFmpegVulkanVideoDecoder::complete_frames_until_exit()
if (m_completion_thread_should_exit)
return;
decoding_frame = m_decoding_frames.dequeue();
+ m_frame_is_completing = true;
}
Optional<DecoderErrorOr<DecodedOutput>> output;
@@ -689,6 +690,8 @@ void FFmpegVulkanVideoDecoder::complete_frames_until_exit()
output = complete_frame(decoding_frame);
MutexLocker locker { m_mutex };
+ m_frame_is_completing = false;
+ auto completion = DecodeCompletion::ProducedNothing;
if (decoding_frame.generation == m_generation) {
if (!output.has_value())
m_completed_end_of_stream = true;
@@ -696,9 +699,11 @@ void FFmpegVulkanVideoDecoder::complete_frames_until_exit()
m_decode_failure = output->release_error();
else
m_outputs.append(output->release_value());
- if (!m_caller_is_waiting_for_output && m_output_arrived_callback)
- m_output_arrived_callback();
+ completion = DecodeCompletion::ProducedOutput;
}
+ auto caller_needs_waking = completion == DecodeCompletion::ProducedOutput || m_decoding_frames.is_empty();
+ if (caller_needs_waking && !m_caller_is_waiting_for_output && m_decode_completed_callback)
+ m_decode_completed_callback(completion);
m_condition.broadcast();
}
}
diff --git a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
index 5400390e82c..0cad77816b9 100644
--- a/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
+++ b/Libraries/LibMediaCodecs/FFmpeg/FFmpegVulkanVideoDecoder.h
@@ -38,10 +38,17 @@ public:
virtual ~FFmpegVulkanVideoDecoder() override;
virtual void set_storage_freed_callback(Function<void()> callback) override { m_surface_budget->set_storage_freed_callback(move(callback)); }
- virtual void set_output_arrived_callback(Function<void()> callback) override
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)> callback) override
{
MutexLocker locker { m_mutex };
- m_output_arrived_callback = move(callback);
+ m_decode_completed_callback = move(callback);
+ }
+ virtual bool has_any_decode_in_progress() const override
+ {
+ MutexLocker locker { m_mutex };
+ if (!m_decoding_frames.is_empty() || m_frame_is_completing)
+ return true;
+ return !m_outputs.is_empty() || m_decode_failure.has_value();
}
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) override;
@@ -107,13 +114,14 @@ private:
mutable Mutex m_mutex;
ConditionVariable m_condition { m_mutex };
Queue<DecodingFrame> m_decoding_frames;
+ bool m_frame_is_completing { false };
Vector<DecodedOutput> m_outputs;
Optional<DecoderError> m_decode_failure;
bool m_completed_end_of_stream { false };
bool m_caller_is_waiting_for_output { false };
bool m_completion_thread_should_exit { false };
u64 m_generation { 0 };
- Function<void()> m_output_arrived_callback;
+ Function<void(DecodeCompletion)> m_decode_completed_callback;
RefPtr<Threading::Thread> m_completion_thread;
// Only the completion thread uses these. A frames context recycles its images, so each one's surface is kept and
diff --git a/Libraries/LibMediaCodecs/VideoDecoder.h b/Libraries/LibMediaCodecs/VideoDecoder.h
index 6d9b824572a..45f9e4d79ab 100644
--- a/Libraries/LibMediaCodecs/VideoDecoder.h
+++ b/Libraries/LibMediaCodecs/VideoDecoder.h
@@ -20,18 +20,27 @@ enum class DecodeIntent : u8 {
Output,
};
+enum class DecodeCompletion : u8 {
+ // A frame or a failure is ready for take_next_output().
+ ProducedOutput,
+ ProducedNothing,
+};
+
class VideoDecoder {
public:
virtual ~VideoDecoder() { }
// Both callbacks are installed before any coded data is received, and run on whichever thread frees the storage
- // or finishes the output.
+ // or finishes the decode.
// Called without the decoder's locks held.
virtual void set_storage_freed_callback(Function<void()>) = 0;
- // Called when output becomes available while no take_next_output() call is waiting for it, so that a caller
- // blocked elsewhere knows to come back. Decoders whose output is complete by the time receive_coded_data()
- // returns never call it. It may be called with the decoder's locks held, so it must not call into the decoder.
- virtual void set_output_arrived_callback(Function<void()>) = 0;
+ // Called when a decode finishes while no take_next_output() call is waiting for it, so that a caller blocked
+ // elsewhere knows to come back. Decoders whose decodes are complete by the time receive_coded_data() returns
+ // never call it. It may be called with the decoder's locks held, so it must not call into the decoder.
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)>) = 0;
+ // Returns whether output is ready to be taken or may still arrive without more coded data. The decode completed
+ // callback is called when a decode finishing makes this stop returning true.
+ virtual bool has_any_decode_in_progress() const = 0;
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) = 0;
virtual void signal_end_of_stream() = 0;
diff --git a/Libraries/LibMediaCodecs/VideoDecoderClient.ipc b/Libraries/LibMediaCodecs/VideoDecoderClient.ipc
index f0fc99bd938..8bd836daeba 100644
--- a/Libraries/LibMediaCodecs/VideoDecoderClient.ipc
+++ b/Libraries/LibMediaCodecs/VideoDecoderClient.ipc
@@ -7,6 +7,8 @@ endpoint VideoDecoderClient
{
frame_decoded(u64 decoder_id, u32 generation, u64 frame_id, Media::VideoFrameMetadata metadata, Media::VideoFramePixels pixels) =|
coded_frame_consumed(u64 decoder_id) =|
+ // A decode that a coded frame starts is reported in progress before the frame is reported consumed.
+ any_decode_in_progress_changed(u64 decoder_id, bool any_decode_is_in_progress) =|
decoder_failed(u64 decoder_id, u32 generation, Media::DecoderError error) =|
decoder_drained(u64 decoder_id, u32 generation) =|
// Every slot the decoder can output into is held by the client, so it waits for one to be released. Anything the
diff --git a/Libraries/LibMediaCodecs/VideoDecoderServerConnection.cpp b/Libraries/LibMediaCodecs/VideoDecoderServerConnection.cpp
index ddb950d1311..c19a7f05674 100644
--- a/Libraries/LibMediaCodecs/VideoDecoderServerConnection.cpp
+++ b/Libraries/LibMediaCodecs/VideoDecoderServerConnection.cpp
@@ -91,7 +91,7 @@ private:
Mutex mutex;
ConditionVariable condition { mutex };
bool storage_was_freed { false };
- bool output_arrived { false };
+ bool decode_completed { false };
};
DecoderSession(ThreadSafeWeakRef<VideoDecoderServerConnection> connection, Core::EventLoop& connection_event_loop, ReadonlySpan<VideoDecoderRegistration> hosted_decoders, u64 decoder_id, u32 generation, CodecID codec_id, ByteBuffer codec_initialization_data)
@@ -111,19 +111,19 @@ private:
create_decoder();
while (true) {
Optional<Command> command;
- auto output_arrived = false;
+ auto decode_completed = false;
{
MutexLocker locker { m_wait_state->mutex };
- while (m_commands.is_empty() && !m_wait_state->output_arrived && !m_exit_requested)
+ while (m_commands.is_empty() && !m_wait_state->decode_completed && !m_exit_requested)
m_wait_state->condition.wait();
if (m_exit_requested)
break;
- output_arrived = exchange(m_wait_state->output_arrived, false);
+ decode_completed = exchange(m_wait_state->decode_completed, false);
if (!m_commands.is_empty())
command = m_commands.dequeue();
}
// Output the decoder finished on its own goes out before the next command, which it does not depend on.
- if (output_arrived && !m_failure.has_value())
+ if (decode_completed && !m_failure.has_value())
take_outputs();
if (command.has_value()) {
command->visit(
@@ -132,6 +132,7 @@ private:
[&](Flush const& flush) { apply_flush(flush.generation); },
[&](ReplaceRing& replace_ring) { m_ring = move(replace_ring.ring); });
}
+ report_whether_any_decode_is_in_progress();
}
m_decoder.clear();
}
@@ -156,9 +157,9 @@ private:
wait_state->storage_was_freed = true;
wait_state->condition.broadcast();
});
- m_decoder->set_output_arrived_callback([wait_state = m_wait_state] {
+ m_decoder->set_decode_completed_callback([wait_state = m_wait_state](DecodeCompletion) {
MutexLocker locker { wait_state->mutex };
- wait_state->output_arrived = true;
+ wait_state->decode_completed = true;
wait_state->condition.broadcast();
});
}
@@ -183,8 +184,20 @@ private:
});
}
+ void report_whether_any_decode_is_in_progress()
+ {
+ auto any_decode_is_in_progress = m_decoder && m_decoder->has_any_decode_in_progress();
+ if (any_decode_is_in_progress == m_any_decode_was_reported_in_progress)
+ return;
+ m_any_decode_was_reported_in_progress = any_decode_is_in_progress;
+ m_connection.with_target([&](auto& connection) {
+ connection.async_any_decode_in_progress_changed(m_decoder_id, any_decode_is_in_progress);
+ });
+ }
+
void acknowledge_submission()
{
+ report_whether_any_decode_is_in_progress();
m_connection.with_target([&](auto& connection) {
connection.async_coded_frame_consumed(m_decoder_id);
});
@@ -332,6 +345,7 @@ private:
// Only the decoder thread uses these.
OwnPtr<VideoDecoder> m_decoder;
Optional<DecoderError> m_failure;
+ bool m_any_decode_was_reported_in_progress { false };
u32 m_generation { 0 };
// The latest submission's, which output the decoder finishes later is taken with as well.
Optional<AK::Duration> m_output_target;
diff --git a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.h b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.h
index 77c68bfc6d1..2a22fed9cc8 100644
--- a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.h
+++ b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.h
@@ -39,7 +39,8 @@ public:
virtual ~VideoToolboxVideoDecoder() override;
virtual void set_storage_freed_callback(Function<void()> callback) override { m_surface_budget->set_storage_freed_callback(move(callback)); }
- virtual void set_output_arrived_callback(Function<void()> callback) override { m_output_arrived_callback = move(callback); }
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)> callback) override { m_decode_completed_callback = move(callback); }
+ virtual bool has_any_decode_in_progress() const override;
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) override;
virtual void signal_end_of_stream() override;
@@ -89,7 +90,7 @@ private:
mutable Mutex m_output_mutex;
ConditionVariable m_output_arrived { m_output_mutex };
bool m_caller_is_waiting_for_output { false };
- Function<void()> m_output_arrived_callback;
+ Function<void(DecodeCompletion)> m_decode_completed_callback;
Vector<DecodedOutput> m_outputs;
u8 m_reorder_frame_count { 0 };
Optional<DecoderError> m_decode_failure;
diff --git a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
index 306861ba04c..2f81d334d0e 100644
--- a/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
+++ b/Libraries/LibMediaCodecs/VideoToolbox/VideoToolboxVideoDecoder.mm
@@ -758,6 +758,7 @@ DecoderErrorOr<void> VideoToolboxVideoDecoder::ensure_session_for_frame(CodedFra
void VideoToolboxVideoDecoder::note_decode_completed(CodingIndependentCodePoints const& cicp, u64 generation, i32 status, void* image_buffer, AK::Duration timestamp, AK::Duration duration)
{
+ auto completion = DecodeCompletion::ProducedNothing;
auto caller_needs_waking = false;
{
MutexLocker locker { m_output_mutex };
@@ -768,14 +769,17 @@ void VideoToolboxVideoDecoder::note_decode_completed(CodingIndependentCodePoints
else if (image_buffer != nullptr)
enqueue_decoded_output_while_locked(cicp, image_buffer, timestamp, duration);
auto frame_was_decoded_for_reference_only = status == noErr && image_buffer == nullptr;
- caller_needs_waking = !frame_was_decoded_for_reference_only && !m_caller_is_waiting_for_output;
+ if (!frame_was_decoded_for_reference_only)
+ completion = DecodeCompletion::ProducedOutput;
}
note_frame_left_the_media_engine_while_locked();
+ auto decodes_finished = m_frames_in_flight.load(AK::MemoryOrder::memory_order_relaxed) == 0;
+ caller_needs_waking = (completion == DecodeCompletion::ProducedOutput || decodes_finished) && !m_caller_is_waiting_for_output;
}
- if (caller_needs_waking && m_output_arrived_callback)
- m_output_arrived_callback();
+ if (caller_needs_waking && m_decode_completed_callback)
+ m_decode_completed_callback(completion);
}
void VideoToolboxVideoDecoder::note_frame_left_the_media_engine_while_locked()
@@ -834,6 +838,15 @@ void VideoToolboxVideoDecoder::enqueue_decoded_output_while_locked(CodingIndepen
insert_output_in_presentation_order_while_locked(move(output));
}
+bool VideoToolboxVideoDecoder::has_any_decode_in_progress() const
+{
+ MutexLocker locker { m_output_mutex };
+ if (m_frames_in_flight.load(AK::MemoryOrder::memory_order_relaxed) > 0)
+ return true;
+ // Output held back for reordering waits for more coded data, so only output that can be taken now counts.
+ return may_pull_frame_from_reorder_queue_while_locked() || m_decode_failure.has_value();
+}
+
DecoderErrorOr<void> VideoToolboxVideoDecoder::receive_coded_data(CodedFrame const& coded_frame, DecodeIntent intent)
{
if (m_reached_end_of_stream)
diff --git a/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.cpp b/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.cpp
index 5ff77098214..9afa3dabfb9 100644
--- a/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.cpp
+++ b/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.cpp
@@ -56,7 +56,8 @@ struct WindowsMediaFoundationVideoDecoder::Transform {
class WindowsMediaFoundationVideoDecoder::ThreadData final : public AtomicRefCounted<ThreadData> {
public:
- void set_output_arrived_callback(Function<void()>);
+ void set_decode_completed_callback(Function<void(DecodeCompletion)>);
+ bool has_any_decode_in_progress() const;
void enqueue(DecodedOutput);
void complete_outputs_until_exit();
@@ -70,17 +71,18 @@ public:
private:
Vector<DecodedOutput> take_all_outputs_while_locked();
- Mutex m_mutex;
+ mutable Mutex m_mutex;
ConditionVariable m_condition { m_mutex };
// Media Foundation hands frames back in presentation order, once nothing decoded later could precede them.
Queue<DecodedOutput> m_awaiting_completion;
+ bool m_output_is_completing { false };
Vector<DecodedOutput> m_completed;
Optional<DecoderError> m_decode_failure;
bool m_completed_end_of_stream { false };
bool m_caller_is_waiting_for_output { false };
bool m_should_exit { false };
u64 m_generation { 0 };
- Function<void()> m_output_arrived_callback;
+ Function<void(DecodeCompletion)> m_decode_completed_callback;
};
namespace {
@@ -446,15 +448,28 @@ DecoderErrorOr<void> WindowsMediaFoundationVideoDecoder::collect_available_outpu
}
}
-void WindowsMediaFoundationVideoDecoder::set_output_arrived_callback(Function<void()> callback)
+void WindowsMediaFoundationVideoDecoder::set_decode_completed_callback(Function<void(DecodeCompletion)> callback)
{
- m_thread_data->set_output_arrived_callback(move(callback));
+ m_thread_data->set_decode_completed_callback(move(callback));
}
-void WindowsMediaFoundationVideoDecoder::ThreadData::set_output_arrived_callback(Function<void()> callback)
+bool WindowsMediaFoundationVideoDecoder::has_any_decode_in_progress() const
+{
+ return m_thread_data->has_any_decode_in_progress();
+}
+
+void WindowsMediaFoundationVideoDecoder::ThreadData::set_decode_completed_callback(Function<void(DecodeCompletion)> callback)
+{
+ MutexLocker locker { m_mutex };
+ m_decode_completed_callback = move(callback);
+}
+
+bool WindowsMediaFoundationVideoDecoder::ThreadData::has_any_decode_in_progress() const
{
MutexLocker locker { m_mutex };
- m_output_arrived_callback = move(callback);
+ if (!m_awaiting_completion.is_empty() || m_output_is_completing)
+ return true;
+ return !m_completed.is_empty() || m_decode_failure.has_value();
}
void WindowsMediaFoundationVideoDecoder::ThreadData::enqueue(DecodedOutput output)
@@ -476,6 +491,7 @@ void WindowsMediaFoundationVideoDecoder::ThreadData::complete_outputs_until_exit
if (m_should_exit)
return;
output = m_awaiting_completion.dequeue();
+ m_output_is_completing = true;
}
DecoderErrorOr<void> decoded = {};
@@ -485,6 +501,8 @@ void WindowsMediaFoundationVideoDecoder::ThreadData::complete_outputs_until_exit
MutexLocker locker { m_mutex };
if (m_should_exit)
return;
+ m_output_is_completing = false;
+ auto completion = DecodeCompletion::ProducedNothing;
if (output.generation == m_generation) {
if (!output.sample)
m_completed_end_of_stream = true;
@@ -492,9 +510,11 @@ void WindowsMediaFoundationVideoDecoder::ThreadData::complete_outputs_until_exit
m_decode_failure = decoded.release_error();
else
m_completed.append(move(output));
- if (!m_caller_is_waiting_for_output && m_output_arrived_callback)
- m_output_arrived_callback();
+ completion = DecodeCompletion::ProducedOutput;
}
+ auto caller_needs_waking = completion == DecodeCompletion::ProducedOutput || m_awaiting_completion.is_empty();
+ if (caller_needs_waking && !m_caller_is_waiting_for_output && m_decode_completed_callback)
+ m_decode_completed_callback(completion);
m_condition.broadcast();
}
}
diff --git a/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.h b/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.h
index 8ad5ff01afa..2d7920d272a 100644
--- a/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.h
+++ b/Libraries/LibMediaCodecs/WindowsMediaFoundation/WindowsMediaFoundationVideoDecoder.h
@@ -33,7 +33,8 @@ public:
virtual ~WindowsMediaFoundationVideoDecoder() override;
virtual void set_storage_freed_callback(Function<void()> callback) override { m_frame_pool->set_slot_freed_callback(move(callback)); }
- virtual void set_output_arrived_callback(Function<void()>) override;
+ virtual void set_decode_completed_callback(Function<void(DecodeCompletion)>) override;
+ virtual bool has_any_decode_in_progress() const override;
virtual DecoderErrorOr<void> receive_coded_data(CodedFrame const&, DecodeIntent) override;
virtual void signal_end_of_stream() override;
diff --git a/Tests/LibMedia/TestRemoteVideoDecoder.cpp b/Tests/LibMedia/TestRemoteVideoDecoder.cpp
index 024e3acb15b..75795ce43d4 100644
--- a/Tests/LibMedia/TestRemoteVideoDecoder.cpp
+++ b/Tests/LibMedia/TestRemoteVideoDecoder.cpp
@@ -326,7 +326,7 @@ StalledDecoding submit_until_the_server_stops_answering(size_t unanswered_submis
auto first_coded_frame = MUST(demuxer->get_next_sample_for_track(track));
auto decoder = MUST(Media::RemoteVideoDecoder::try_create(first_coded_frame.codec_id(), first_coded_frame.new_codec_configuration().value()));
- decoder->set_output_arrived_callback([] { });
+ decoder->set_decode_completed_callback([](Media::DecodeCompletion) { });
MUST(decoder->receive_coded_data(first_coded_frame, Media::DecodeIntent::Output));
for (size_t index = 1; index < WithholdingVideoDecoder::MAXIMUM_WITHHELD_OUTPUTS + unanswered_submission_count; index++)
@@ -407,7 +407,9 @@ TEST_CASE(output_the_server_decoder_finishes_later_wakes_the_client)
auto first_coded_frame = TRY_OR_FAIL(demuxer->get_next_sample_for_track(track));
auto decoder = TRY_OR_FAIL(Media::RemoteVideoDecoder::try_create(first_coded_frame.codec_id(), first_coded_frame.new_codec_configuration().value()));
- decoder->set_output_arrived_callback([&] {
+ decoder->set_decode_completed_callback([&](Media::DecodeCompletion completion) {
+ if (completion != Media::DecodeCompletion::ProducedOutput)
+ return;
MutexLocker locker { wake_mutex };
wake_count++;
wake_condition.broadcast();
@@ -442,6 +444,61 @@ TEST_CASE(output_the_server_decoder_finishes_later_wakes_the_client)
EXPECT_EQ(frame.value()->timestamp(), first_coded_frame.presentation_timestamp());
}
+// The server reports its decoder idle once it has sent the frame that the decoder finished, but that frame is still output
+// for the client's caller to take, so the client must keep reporting a decode in progress until the frame is taken.
+TEST_CASE(a_frame_waiting_to_be_taken_counts_as_a_decode_in_progress)
+{
+ auto withheld_outputs = make_ref_counted<WithheldOutputs>();
+ WithholdingVideoDecoder::set_withheld_outputs_for_new_decoders(withheld_outputs);
+ ScopeGuard stop_withholding = [] { WithholdingVideoDecoder::set_withheld_outputs_for_new_decoders(nullptr); };
+
+ auto file = MUST(Core::File::open(test_video_path, Core::File::OpenMode::Read));
+ auto stream = Media::IncrementallyPopulatedStream::create_from_buffer(MUST(file->read_until_eof()));
+ NonnullRefPtr<Media::Demuxer> demuxer = MUST(Media::Matroska::MatroskaDemuxer::from_stream(stream));
+ auto track = MUST(demuxer->get_preferred_track_for_type(Media::TrackType::Video)).release_value();
+ TRY_OR_FAIL(demuxer->create_context_for_track(track));
+
+ Mutex mutex;
+ ConditionVariable condition { mutex };
+ bool output_arrived = false;
+
+ auto coded_frame = TRY_OR_FAIL(demuxer->get_next_sample_for_track(track));
+ auto decoder = TRY_OR_FAIL(Media::RemoteVideoDecoder::try_create(coded_frame.codec_id(), coded_frame.new_codec_configuration().value()));
+ decoder->set_decode_completed_callback([&](Media::DecodeCompletion completion) {
+ if (completion != Media::DecodeCompletion::ProducedOutput)
+ return;
+ MutexLocker locker { mutex };
+ output_arrived = true;
+ condition.broadcast();
+ });
+
+ TRY_OR_FAIL(decoder->receive_coded_data(coded_frame, Media::DecodeIntent::Output));
+ EXPECT(withheld_outputs->wait_until_withheld_count_reaches(1, AK::Duration::from_seconds(5)));
+ EXPECT(decoder->has_any_decode_in_progress());
+
+ EXPECT(withheld_outputs->release_next());
+ {
+ auto deadline = MonotonicTime::now() + AK::Duration::from_seconds(5);
+ MutexLocker locker { mutex };
+ while (!output_arrived && MonotonicTime::now() < deadline)
+ condition.wait_for(deadline - MonotonicTime::now());
+ if (!output_arrived) {
+ FAIL("The frame the server's decoder finished did not reach the client");
+ return;
+ }
+
+ // Give the server's report that its decoder is idle time to arrive.
+ auto settle_deadline = MonotonicTime::now() + AK::Duration::from_milliseconds(300);
+ while (decoder->has_any_decode_in_progress() && MonotonicTime::now() < settle_deadline)
+ condition.wait_for(AK::Duration::from_milliseconds(10));
+ }
+ EXPECT(decoder->has_any_decode_in_progress());
+
+ auto frame = decoder->take_next_output({});
+ EXPECT(!frame.is_error());
+ EXPECT(!decoder->has_any_decode_in_progress());
+}
+
// Once the client holds every slot the server can decode into, the server waits for one to be released, and the
// client answers TryAgain until a release of its own lets the server decode again.
TEST_CASE(a_server_out_of_slots_is_waited_for_until_a_frame_is_released)
@@ -456,7 +513,7 @@ TEST_CASE(a_server_out_of_slots_is_waited_for_until_a_frame_is_released)
auto decoder = TRY_OR_FAIL(Media::RemoteVideoDecoder::try_create(coded_frame.codec_id(), coded_frame.new_codec_configuration().value()));
size_t storage_freed_count = 0;
decoder->set_storage_freed_callback([&] { storage_freed_count++; });
- decoder->set_output_arrived_callback([] { });
+ decoder->set_decode_completed_callback([](Media::DecodeCompletion) { });
Vector<NonnullRefPtr<Media::VideoFrame>> held_frames;
auto server_ran_out_of_slots = false;
diff --git a/Tests/LibMedia/WithholdingVideoDecoder.h b/Tests/LibMedia/WithholdingVideoDecoder.h
index e8cd40fd14e..e759d4511de 100644
--- a/Tests/LibMedia/WithholdingVideoDecoder.h
+++ b/Tests/LibMedia/WithholdingVideoDecoder.h
@@ -35,8 +35,8 @@ public:
return false;
m_released.append(m_withheld.take_first());
m_released_changed.broadcast();
- if (m_output_arrived_callback && !m_caller_is_waiting_for_output)
- m_output_arrived_callback();
+ if (m_decode_completed_callback && !m_caller_is_waiting_for_output)
+ m_decode_completed_callback(Media::DecodeCompletion::ProducedOutput);
return true;
}
@@ -101,7 +101,7 @@ private:
ConditionVariable m_caller_wait_changed { m_mutex };
Vector<Output> m_withheld;
Vector<Output> m_released;
- Function<void()> m_output_arrived_callback;
+ Function<void(Media::DecodeCompletion)> m_decode_completed_callback;
bool m_caller_is_waiting_for_output { false };
size_t m_handed_out_count { 0 };
Optional<size_t> m_output_index_that_loses_state;
@@ -135,14 +135,19 @@ public:
virtual ~WithholdingVideoDecoder() override
{
MutexLocker locker { m_withheld_outputs->m_mutex };
- m_withheld_outputs->m_output_arrived_callback = nullptr;
+ m_withheld_outputs->m_decode_completed_callback = nullptr;
}
virtual void set_storage_freed_callback(Function<void()> callback) override { m_frame_pool->set_slot_freed_callback(move(callback)); }
- virtual void set_output_arrived_callback(Function<void()> callback) override
+ virtual void set_decode_completed_callback(Function<void(Media::DecodeCompletion)> callback) override
{
MutexLocker locker { m_withheld_outputs->m_mutex };
- m_withheld_outputs->m_output_arrived_callback = move(callback);
+ m_withheld_outputs->m_decode_completed_callback = move(callback);
+ }
+ virtual bool has_any_decode_in_progress() const override
+ {
+ MutexLocker locker { m_withheld_outputs->m_mutex };
+ return !m_withheld_outputs->m_withheld.is_empty() || !m_withheld_outputs->m_released.is_empty();
}
virtual Media::DecoderErrorOr<void> receive_coded_data(Media::CodedFrame const& coded_frame, Media::DecodeIntent intent) override