Skip to content

Commit

Permalink
Use NativePromise for WebCodecs encode/decode/flush methods
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=278909
rdar://135005637

Reviewed by Philippe Normand and Jean-Yves Avenard.

We move all these methods to NativePromise as it is easier to read and is easier for maintenance.
For instance, the NativePromises are wrapped via ScriptExecutionContext::enqueueTaskWhenSettled so that
it is guaranteed for the callback to be executed and destroyed in the context thread.
This will allow us to use ActiveDOMObject pending activities as part of these callbacks in a follow-up.
Another step will be to migrate the create encoder/decoder methods to NativePromise as well.

* Source/WebCore/Modules/webcodecs/WebCodecsAudioDecoder.cpp:
(WebCore::WebCodecsAudioDecoder::decode):
(WebCore::WebCodecsAudioDecoder::flush):
(WebCore::WebCore::WebCodecsAudioDecoder::suspend):
(WebCore::WebCodecsAudioDecoder::stop):
* Source/WebCore/Modules/webcodecs/WebCodecsAudioEncoder.cpp:
(WebCore::WebCodecsAudioEncoder::configure):
(WebCore::WebCodecsAudioEncoder::encode):
(WebCore::WebCodecsAudioEncoder::flush):
(WebCore::WebCore::WebCodecsAudioEncoder::suspend):
(WebCore::WebCodecsAudioEncoder::stop):
* Source/WebCore/Modules/webcodecs/WebCodecsVideoDecoder.cpp:
(WebCore::WebCodecsVideoDecoder::decode):
(WebCore::WebCodecsVideoDecoder::flush):
(WebCore::WebCore::WebCodecsVideoDecoder::suspend):
(WebCore::WebCodecsVideoDecoder::stop):
* Source/WebCore/Modules/webcodecs/WebCodecsVideoEncoder.cpp:
(WebCore::WebCodecsVideoEncoder::updateRates):
(WebCore::WebCodecsVideoEncoder::configure):
(WebCore::WebCodecsVideoEncoder::encode):
(WebCore::WebCodecsVideoEncoder::flush):
(WebCore::WebCore::WebCodecsVideoEncoder::suspend):
(WebCore::WebCodecsVideoEncoder::stop):
* Source/WebCore/Modules/webcodecs/WebCodecsVideoEncoder.h:
* Source/WebCore/platform/AudioDecoder.h:
* Source/WebCore/platform/AudioEncoder.h:
* Source/WebCore/platform/VideoDecoder.h:
* Source/WebCore/platform/VideoEncoder.h:
(WebCore::VideoEncoder::setRates): Deleted.
* Source/WebCore/platform/audio/gstreamer/AudioDecoderGStreamer.cpp:
(WebCore::GStreamerAudioDecoder::decode):
(WebCore::GStreamerAudioDecoder::flush):
(WebCore::GStreamerInternalAudioDecoder::GStreamerInternalAudioDecoder):
(WebCore::GStreamerInternalAudioDecoder::decode):
(WebCore::GStreamerInternalAudioDecoder::flush):
* Source/WebCore/platform/audio/gstreamer/AudioDecoderGStreamer.h:
* Source/WebCore/platform/audio/gstreamer/AudioEncoderGStreamer.cpp:
(WebCore::GStreamerAudioEncoder::encode):
(WebCore::GStreamerAudioEncoder::flush):
(WebCore::GStreamerInternalAudioEncoder::GStreamerInternalAudioEncoder):
(WebCore::GStreamerInternalAudioEncoder::flush):
* Source/WebCore/platform/audio/gstreamer/AudioEncoderGStreamer.h:
* Source/WebCore/platform/graphics/cocoa/WebCoreDecompressionSession.mm:
(WebCore::WebCoreDecompressionSession::decodeSample):
* Source/WebCore/platform/graphics/gstreamer/VideoDecoderGStreamer.cpp:
(WebCore::GStreamerVideoDecoder::decode):
(WebCore::GStreamerVideoDecoder::flush):
(WebCore::GStreamerInternalVideoDecoder::decode):
(WebCore::GStreamerInternalVideoDecoder::flush):
* Source/WebCore/platform/graphics/gstreamer/VideoDecoderGStreamer.h:
* Source/WebCore/platform/graphics/gstreamer/VideoEncoderGStreamer.cpp:
(WebCore::GStreamerInternalVideoEncoder::create):
(WebCore::GStreamerVideoEncoder::create):
(WebCore::GStreamerVideoEncoder::GStreamerVideoEncoder):
(WebCore::GStreamerVideoEncoder::encode):
(WebCore::GStreamerVideoEncoder::flush):
(WebCore::GStreamerVideoEncoder::setRates):
(WebCore::GStreamerInternalVideoEncoder::GStreamerInternalVideoEncoder):
(WebCore::GStreamerInternalVideoEncoder::initialize):
(WebCore::GStreamerInternalVideoEncoder::setRates):
(WebCore::GStreamerInternalVideoEncoder::applyRates):
(WebCore::GStreamerInternalVideoEncoder::flush):
* Source/WebCore/platform/graphics/gstreamer/VideoEncoderGStreamer.h:
* Source/WebCore/platform/libwebrtc/LibWebRTCVPXVideoDecoder.cpp:
(WebCore::LibWebRTCVPXVideoDecoder::decode):
(WebCore::LibWebRTCVPXVideoDecoder::flush):
(WebCore::LibWebRTCVPXInternalVideoDecoder::decode):
* Source/WebCore/platform/libwebrtc/LibWebRTCVPXVideoDecoder.h:
* Source/WebCore/platform/libwebrtc/LibWebRTCVPXVideoEncoder.cpp:
(WebCore::LibWebRTCVPXVideoEncoder::encode):
(WebCore::LibWebRTCVPXVideoEncoder::flush):
(WebCore::LibWebRTCVPXVideoEncoder::setRates):
(WebCore::LibWebRTCVPXInternalVideoEncoder::encode):
* Source/WebCore/platform/libwebrtc/LibWebRTCVPXVideoEncoder.h:
* Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.h:
* Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.messages.in:
* Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.mm:
(WebKit::LibWebRTCCodecsProxy::flushDecoder):
(WebKit::LibWebRTCCodecsProxy::setEncodeRates):
* Source/WebKit/WebProcess/GPU/media/RemoteVideoCodecFactory.cpp:
(WebKit::RemoteVideoDecoder::decode):
(WebKit::RemoteVideoDecoder::flush):
(WebKit::RemoteVideoEncoder::encode):
(WebKit::RemoteVideoEncoder::setRates):
(WebKit::RemoteVideoEncoder::flush):
* Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.cpp:
(WebKit::decodeVideoFrame):
(WebKit::LibWebRTCCodecs::flushDecoder):
(WebKit::LibWebRTCCodecs::sendFrameToDecode):
(WebKit::LibWebRTCCodecs::decodeWebRTCFrame):
(WebKit::LibWebRTCCodecs::decodeFrame):
(WebKit::LibWebRTCCodecs::decodeFrameInternal):
(WebKit::LibWebRTCCodecs::encodeFrameInternal):
(WebKit::LibWebRTCCodecs::encodeFrame):
(WebKit::LibWebRTCCodecs::flushEncoder):
(WebKit::LibWebRTCCodecs::setEncodeRates):
(WebKit::LibWebRTCCodecs::gpuProcessConnectionDidClose):
(WebKit::LibWebRTCCodecs::setDecoderConnection):
(WebKit::LibWebRTCCodecs::flushDecoderCompleted): Deleted.
* Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.h:
* Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.messages.in:

Canonical link: https://commits.webkit.org/283145@main
  • Loading branch information
youennf committed Sep 4, 2024
1 parent 667de7f commit 39a1102
Show file tree
Hide file tree
Showing 29 changed files with 370 additions and 383 deletions.
22 changes: 15 additions & 7 deletions Source/WebCore/Modules/webcodecs/WebCodecsAudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ ExceptionOr<void> WebCodecsAudioDecoder::decode(Ref<WebCodecsEncodedAudioChunk>&
--m_decodeQueueSize;
scheduleDequeueEvent();

m_internalDecoder->decode({ chunk->span(), chunk->type() == WebCodecsEncodedAudioChunkType::Key, chunk->timestamp(), chunk->duration() }, [this, pendingActivity = takePendingWebCodecActivity()](String&& result) {
if (!result.isNull())
closeDecoder(Exception { ExceptionCode::EncodingError, WTFMove(result) });
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalDecoder->decode({ chunk->span(), chunk->type() == WebCodecsEncodedAudioChunkType::Key, chunk->timestamp(), chunk->duration() }), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, pendingActivity = takePendingWebCodecActivity()] (auto&& result) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !!result)
return;

protectedThis->closeDecoder(Exception { ExceptionCode::EncodingError, WTFMove(result.error()) });
});
} });
return { };
Expand All @@ -177,11 +180,15 @@ ExceptionOr<void> WebCodecsAudioDecoder::flush(Ref<DeferredPromise>&& promise)
m_isKeyChunkRequired = true;
m_pendingFlushPromises.append(WTFMove(promise));
queueControlMessageAndProcess({ *this, [this, clearFlushPromiseCount = m_clearFlushPromiseCount] {
m_internalDecoder->flush([this, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] {
if (clearFlushPromiseCount != m_clearFlushPromiseCount)
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalDecoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] (auto&&) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;

m_pendingFlushPromises.takeFirst()->resolve();
if (clearFlushPromiseCount != protectedThis->m_clearFlushPromiseCount)
return;

protectedThis->m_pendingFlushPromises.takeFirst()->resolve();
});
} });
return { };
Expand Down Expand Up @@ -302,13 +309,14 @@ void WebCodecsAudioDecoder::processControlMessageQueue()

void WebCore::WebCodecsAudioDecoder::suspend(ReasonForSuspension)
{
// FIXME: Implement.
}

void WebCodecsAudioDecoder::stop()
{
m_state = WebCodecsCodecState::Closed;
m_internalDecoder = nullptr;
m_controlMessageQueue.clear();
m_pendingFlushPromises.clear();
}

bool WebCodecsAudioDecoder::virtualHasPendingActivity() const
Expand Down
29 changes: 17 additions & 12 deletions Source/WebCore/Modules/webcodecs/WebCodecsAudioEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ ExceptionOr<void> WebCodecsAudioEncoder::configure(ScriptExecutionContext&, WebC
if (m_internalEncoder) {
queueControlMessageAndProcess({ *this, [this, config]() mutable {
m_isMessageQueueBlocked = true;
m_internalEncoder->flush([weakThis = ThreadSafeWeakPtr { *this }, config = WTFMove(config).isolatedCopy()]() mutable {

protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, config = WTFMove(config)] (auto&&) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
Expand Down Expand Up @@ -270,13 +271,15 @@ ExceptionOr<void> WebCodecsAudioEncoder::encode(Ref<WebCodecsAudioData>&& frame)
queueControlMessageAndProcess({ *this, [this, audioData = WTFMove(audioData), timestamp = frame->timestamp(), duration = frame->duration()]() mutable {
--m_encodeQueueSize;
scheduleDequeueEvent();
m_internalEncoder->encode({ WTFMove(audioData), timestamp, duration }, [this, pendingActivity = takePendingWebCodecActivity()](auto&& result) {
if (!result.isNull()) {
if (auto* context = scriptExecutionContext())
context->addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("AudioEncoder encode failed: "_s, result));
closeEncoder(Exception { ExceptionCode::EncodingError, WTFMove(result) });

protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->encode({ WTFMove(audioData), timestamp, duration }), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, pendingActivity = takePendingWebCodecActivity()] (auto&& result) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !!result)
return;
}

if (auto context = protectedThis->protectedScriptExecutionContext())
context->addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("AudioEncoder encode failed: "_s, result.error()));
protectedThis->closeEncoder(Exception { ExceptionCode::EncodingError, WTFMove(result.error()) });
});
} });
return { };
Expand All @@ -291,12 +294,12 @@ void WebCodecsAudioEncoder::flush(Ref<DeferredPromise>&& promise)

m_pendingFlushPromises.append(WTFMove(promise));
queueControlMessageAndProcess({ *this, [this, clearFlushPromiseCount = m_clearFlushPromiseCount]() mutable {
m_internalEncoder->flush([this, weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] {
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] (auto&&) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || clearFlushPromiseCount != m_clearFlushPromiseCount)
if (!protectedThis || clearFlushPromiseCount != protectedThis->m_clearFlushPromiseCount)
return;

m_pendingFlushPromises.takeFirst()->resolve();
protectedThis->m_pendingFlushPromises.takeFirst()->resolve();
});
} });
}
Expand Down Expand Up @@ -417,12 +420,14 @@ void WebCodecsAudioEncoder::processControlMessageQueue()

void WebCore::WebCodecsAudioEncoder::suspend(ReasonForSuspension)
{
// FIXME: Implement.
}

void WebCodecsAudioEncoder::stop()
{
// FIXME: Implement.
m_state = WebCodecsCodecState::Closed;
m_internalEncoder = nullptr;
m_controlMessageQueue.clear();
m_pendingFlushPromises.clear();
}

bool WebCodecsAudioEncoder::virtualHasPendingActivity() const
Expand Down
22 changes: 15 additions & 7 deletions Source/WebCore/Modules/webcodecs/WebCodecsVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ ExceptionOr<void> WebCodecsVideoDecoder::decode(Ref<WebCodecsEncodedVideoChunk>&
--m_decodeQueueSize;
scheduleDequeueEvent();

m_internalDecoder->decode({ chunk->span(), chunk->type() == WebCodecsEncodedVideoChunkType::Key, chunk->timestamp(), chunk->duration() }, [this, pendingActivity = takePendingWebCodecActivity()](auto&& result) {
if (!result.isNull())
closeDecoder(Exception { ExceptionCode::EncodingError, WTFMove(result) });
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalDecoder->decode({ chunk->span(), chunk->type() == WebCodecsEncodedVideoChunkType::Key, chunk->timestamp(), chunk->duration() }), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { * this }, pendingActivity = takePendingWebCodecActivity()] (auto&& result) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !!result)
return;

protectedThis->closeDecoder(Exception { ExceptionCode::EncodingError, WTFMove(result.error()) });
});
} });
return { };
Expand All @@ -213,11 +216,15 @@ ExceptionOr<void> WebCodecsVideoDecoder::flush(Ref<DeferredPromise>&& promise)
m_isKeyChunkRequired = true;
m_pendingFlushPromises.append(WTFMove(promise));
queueControlMessageAndProcess({ *this, [this, clearFlushPromiseCount = m_clearFlushPromiseCount] {
m_internalDecoder->flush([this, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] {
if (clearFlushPromiseCount != m_clearFlushPromiseCount)
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalDecoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] (auto&&) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;

m_pendingFlushPromises.takeFirst()->resolve();
if (clearFlushPromiseCount != protectedThis->m_clearFlushPromiseCount)
return;

protectedThis->m_pendingFlushPromises.takeFirst()->resolve();
});
} });
return { };
Expand Down Expand Up @@ -338,13 +345,14 @@ void WebCodecsVideoDecoder::processControlMessageQueue()

void WebCore::WebCodecsVideoDecoder::suspend(ReasonForSuspension)
{
// FIXME: Implement.
}

void WebCodecsVideoDecoder::stop()
{
m_state = WebCodecsCodecState::Closed;
m_internalDecoder = nullptr;
m_controlMessageQueue.clear();
m_pendingFlushPromises.clear();
}

bool WebCodecsVideoDecoder::virtualHasPendingActivity() const
Expand Down
50 changes: 25 additions & 25 deletions Source/WebCore/Modules/webcodecs/WebCodecsVideoEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,27 @@ static ExceptionOr<VideoEncoder::Config> createVideoEncoderConfig(const WebCodec
return VideoEncoder::Config { config.width, config.height, useAnnexB, config.bitrate.value_or(0), config.framerate.value_or(0), config.latencyMode == LatencyMode::Realtime, scalabilityMode };
}

bool WebCodecsVideoEncoder::updateRates(const WebCodecsVideoEncoderConfig& config)
void WebCodecsVideoEncoder::updateRates(const WebCodecsVideoEncoderConfig& config)
{
auto bitrate = config.bitrate.value_or(0);
auto framerate = config.framerate.value_or(0);

m_isMessageQueueBlocked = true;
bool isChangingRatesSupported = m_internalEncoder->setRates(bitrate, framerate, [this, weakThis = WeakPtr { *this }, bitrate, framerate]() mutable {
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->setRates(bitrate, framerate), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, bitrate, framerate] (auto&&) mutable {
auto protectedThis = weakThis.get();
if (!protectedThis)
return;

if (m_state == WebCodecsCodecState::Closed || !scriptExecutionContext())
if (protectedThis->m_state == WebCodecsCodecState::Closed || !protectedThis->scriptExecutionContext())
return;

if (bitrate)
m_baseConfiguration.bitrate = bitrate;
protectedThis->m_baseConfiguration.bitrate = bitrate;
if (framerate)
m_baseConfiguration.framerate = framerate;
m_isMessageQueueBlocked = false;
processControlMessageQueue();
protectedThis->m_baseConfiguration.framerate = framerate;
protectedThis->m_isMessageQueueBlocked = false;
protectedThis->processControlMessageQueue();
});
if (!isChangingRatesSupported)
m_isMessageQueueBlocked = false;
return isChangingRatesSupported;
}

ExceptionOr<void> WebCodecsVideoEncoder::configure(ScriptExecutionContext& context, WebCodecsVideoEncoderConfig&& config)
Expand All @@ -152,11 +149,13 @@ ExceptionOr<void> WebCodecsVideoEncoder::configure(ScriptExecutionContext& conte

if (m_internalEncoder) {
queueControlMessageAndProcess({ *this, [this, config]() mutable {
if (isSameConfigurationExceptBitrateAndFramerate(m_baseConfiguration, config) && updateRates(config))
if (isSameConfigurationExceptBitrateAndFramerate(m_baseConfiguration, config)) {
updateRates(config);
return;
}

m_isMessageQueueBlocked = true;
m_internalEncoder->flush([weakThis = ThreadSafeWeakPtr { *this }, config = WTFMove(config).isolatedCopy(), pendingActivity = takePendingWebCodecActivity()]() mutable {
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, config = WTFMove(config), pendingActivity = takePendingWebCodecActivity()] (auto&&) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
Expand All @@ -172,8 +171,10 @@ ExceptionOr<void> WebCodecsVideoEncoder::configure(ScriptExecutionContext& conte

bool isSupportedCodec = isSupportedEncoderCodec(config.codec, context.settingsValues());
queueControlMessageAndProcess({ *this, [this, config = WTFMove(config), isSupportedCodec, identifier = scriptExecutionContext()->identifier()]() mutable {
if (isSupportedCodec && isSameConfigurationExceptBitrateAndFramerate(m_baseConfiguration, config) && updateRates(config))
if (isSupportedCodec && isSameConfigurationExceptBitrateAndFramerate(m_baseConfiguration, config)) {
updateRates(config);
return;
}

m_isMessageQueueBlocked = true;
VideoEncoder::PostTaskCallback postTaskCallback = [weakThis = ThreadSafeWeakPtr { *this }, identifier](auto&& task) {
Expand Down Expand Up @@ -283,18 +284,15 @@ ExceptionOr<void> WebCodecsVideoEncoder::encode(Ref<WebCodecsVideoFrame>&& frame
queueControlMessageAndProcess({ *this, [this, internalFrame = internalFrame.releaseNonNull(), timestamp = frame->timestamp(), duration = frame->duration(), options = WTFMove(options)]() mutable {
--m_encodeQueueSize;
scheduleDequeueEvent();
m_internalEncoder->encode({ WTFMove(internalFrame), timestamp, duration }, options.keyFrame, [weakThis = ThreadSafeWeakPtr { *this }, pendingActivity = takePendingWebCodecActivity()](String&& result) {

protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->encode({ WTFMove(internalFrame), timestamp, duration }, options.keyFrame), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, pendingActivity = takePendingWebCodecActivity()] (auto&& result) {
RefPtr protectedThis = weakThis.get();
ASSERT(protectedThis);
if (!protectedThis)
if (!protectedThis || !!result)
return;

if (!result.isNull()) {
if (RefPtr context = protectedThis->scriptExecutionContext())
context->addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("VideoEncoder encode failed: "_s, result));
protectedThis->closeEncoder(Exception { ExceptionCode::EncodingError, WTFMove(result) });
return;
}
if (RefPtr context = protectedThis->scriptExecutionContext())
context->addConsoleMessage(MessageSource::JS, MessageLevel::Error, makeString("VideoEncoder encode failed: "_s, result.error()));
protectedThis->closeEncoder(Exception { ExceptionCode::EncodingError, WTFMove(result.error()) });
});
} });
return { };
Expand All @@ -309,7 +307,7 @@ void WebCodecsVideoEncoder::flush(Ref<DeferredPromise>&& promise)

m_pendingFlushPromises.append(WTFMove(promise));
queueControlMessageAndProcess({ *this, [this, clearFlushPromiseCount = m_clearFlushPromiseCount]() mutable {
m_internalEncoder->flush([weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] {
protectedScriptExecutionContext()->enqueueTaskWhenSettled(m_internalEncoder->flush(), TaskSource::MediaElement, [weakThis = ThreadSafeWeakPtr { *this }, clearFlushPromiseCount, pendingActivity = takePendingWebCodecActivity()] (auto&&) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || clearFlushPromiseCount != protectedThis->m_clearFlushPromiseCount)
return;
Expand Down Expand Up @@ -435,12 +433,14 @@ void WebCodecsVideoEncoder::processControlMessageQueue()

void WebCore::WebCodecsVideoEncoder::suspend(ReasonForSuspension)
{
// FIXME: Implement.
}

void WebCodecsVideoEncoder::stop()
{
// FIXME: Implement.
m_state = WebCodecsCodecState::Closed;
m_internalEncoder = nullptr;
m_controlMessageQueue.clear();
m_pendingFlushPromises.clear();
}

bool WebCodecsVideoEncoder::virtualHasPendingActivity() const
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/webcodecs/WebCodecsVideoEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class WebCodecsVideoEncoder
void queueControlMessageAndProcess(WebCodecsControlMessage<WebCodecsVideoEncoder>&&);
void processControlMessageQueue();
WebCodecsEncodedVideoChunkMetadata createEncodedChunkMetadata(std::optional<unsigned>);
bool updateRates(const WebCodecsVideoEncoderConfig&);
void updateRates(const WebCodecsVideoEncoderConfig&);

WebCodecsCodecState m_state { WebCodecsCodecState::Unconfigured };
size_t m_encodeQueueSize { 0 };
Expand Down
9 changes: 4 additions & 5 deletions Source/WebCore/platform/AudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

#include <span>
#include <wtf/CompletionHandler.h>
#include <wtf/Expected.h>
#include <wtf/Ref.h>
#include <wtf/NativePromise.h>

namespace WebCore {

Expand Down Expand Up @@ -70,10 +69,10 @@ class AudioDecoder {

static void create(const String&, const Config&, CreateCallback&&, OutputCallback&&, PostTaskCallback&&);

using DecodeCallback = Function<void(String&&)>;
virtual void decode(EncodedData&&, DecodeCallback&&) = 0;
using DecodePromise = NativePromise<void, String>;
virtual Ref<DecodePromise> decode(EncodedData&&) = 0;

virtual void flush(Function<void()>&&) = 0;
virtual Ref<GenericPromise> flush() = 0;
virtual void reset() = 0;
virtual void close() = 0;
};
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/platform/AudioEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "WebCodecsAudioInternalData.h"
#include <span>
#include <wtf/CompletionHandler.h>
#include <wtf/Expected.h>
#include <wtf/NativePromise.h>
#include <wtf/Vector.h>

namespace WebCore {
Expand Down Expand Up @@ -86,10 +86,10 @@ class AudioEncoder {

static void create(const String&, const Config&, CreateCallback&&, DescriptionCallback&&, OutputCallback&&, PostTaskCallback&&);

using EncodeCallback = Function<void(String&&)>;
virtual void encode(RawFrame&&, EncodeCallback&&) = 0;
using EncodePromise = NativePromise<void, String>;
virtual Ref<EncodePromise> encode(RawFrame&&) = 0;

virtual void flush(Function<void()>&&) = 0;
virtual Ref<GenericPromise> flush() = 0;
virtual void reset() = 0;
virtual void close() = 0;
};
Expand Down
9 changes: 4 additions & 5 deletions Source/WebCore/platform/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include "ProcessIdentity.h"
#include <span>
#include <wtf/CompletionHandler.h>
#include <wtf/Expected.h>
#include <wtf/Ref.h>
#include <wtf/NativePromise.h>

namespace WebCore {

Expand Down Expand Up @@ -74,10 +73,10 @@ class VideoDecoder {
static void create(const String&, const Config&, CreateCallback&&, OutputCallback&&, PostTaskCallback&&);
WEBCORE_EXPORT static void createLocalDecoder(const String&, const Config&, CreateCallback&&, OutputCallback&&, PostTaskCallback&&);

using DecodeCallback = Function<void(String&&)>;
virtual void decode(EncodedFrame&&, DecodeCallback&&) = 0;
using DecodePromise = NativePromise<void, String>;
virtual Ref<DecodePromise> decode(EncodedFrame&&) = 0;

virtual void flush(Function<void()>&&) = 0;
virtual Ref<GenericPromise> flush() = 0;
virtual void reset() = 0;
virtual void close() = 0;

Expand Down
Loading

0 comments on commit 39a1102

Please sign in to comment.