Skip to content

Commit

Permalink
LibWebRTCCodecs's createRemoteEncoder is going through the main thread.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=270280
rdar://123806984

Reviewed by Youenn Fablet.

Make use of new sendWithAsyncReplyOnDispatcher to avoid the now unnecessary task re-dispatch.

No change in observable behaviour.

* Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.cpp:
(WebKit::createRemoteDecoder):
(WebKit::LibWebRTCCodecs::createDecoderInternal):
(WebKit::createRemoteEncoder):
(WebKit::LibWebRTCCodecs::createEncoderInternal):
(WebKit::LibWebRTCCodecs::flushEncoder):
* Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.h:
(WebKit::LibWebRTCCodecs::workQueue const):

Canonical link: https://commits.webkit.org/275545@main
  • Loading branch information
jyavenard committed Mar 1, 2024
1 parent a997251 commit c73ac9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
40 changes: 18 additions & 22 deletions Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static inline VideoFrame::Rotation toVideoRotation(webrtc::VideoRotation rotatio

static void createRemoteDecoder(LibWebRTCCodecs::Decoder& decoder, IPC::Connection& connection, bool useRemoteFrames, bool enableAdditionalLogging, Function<void(bool)>&& callback)
{
connection.sendWithAsyncReply(Messages::LibWebRTCCodecsProxy::CreateDecoder { decoder.identifier, decoder.type, decoder.codec, useRemoteFrames, enableAdditionalLogging }, WTFMove(callback), 0);
connection.sendWithAsyncReplyOnDispatcher(Messages::LibWebRTCCodecsProxy::CreateDecoder { decoder.identifier, decoder.type, decoder.codec, useRemoteFrames, enableAdditionalLogging }, WebProcess::singleton().libWebRTCCodecs().workQueue(), WTFMove(callback), 0);
}

static int32_t encodeVideoFrame(webrtc::WebKitVideoEncoder encoder, const webrtc::VideoFrame& frame, bool shouldEncodeAsKeyFrame)
Expand Down Expand Up @@ -349,16 +349,14 @@ LibWebRTCCodecs::Decoder* LibWebRTCCodecs::createDecoderInternal(VideoCodecType
{
Locker locker { m_connectionLock };
createRemoteDecoder(*decoder, *protectedConnection(), m_useRemoteFrames, m_enableAdditionalLogging, [identifier = decoder->identifier, callback = WTFMove(callback)](bool result) mutable {
WebProcess::singleton().libWebRTCCodecs().m_queue->dispatch([identifier, result, callback = WTFMove(callback)]() mutable {
if (!result) {
callback(nullptr);
return;
}

auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_decoders.get(identifier));
});
if (!result) {
callback(nullptr);
return;
}

auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_decoders.get(identifier));
});

setDecoderConnection(*decoder, m_connection.get());
Expand Down Expand Up @@ -576,7 +574,7 @@ void LibWebRTCCodecs::createEncoderAndWaitUntilInitialized(VideoCodecType type,

static void createRemoteEncoder(LibWebRTCCodecs::Encoder& encoder, IPC::Connection& connection, const Vector<std::pair<String, String>>& parameters, Function<void(bool)>&& callback)
{
connection.sendWithAsyncReply(Messages::LibWebRTCCodecsProxy::CreateEncoder { encoder.identifier, encoder.type, encoder.codec, parameters, encoder.isRealtime, encoder.useAnnexB, encoder.scalabilityMode }, WTFMove(callback), 0);
connection.sendWithAsyncReplyOnDispatcher(Messages::LibWebRTCCodecsProxy::CreateEncoder { encoder.identifier, encoder.type, encoder.codec, parameters, encoder.isRealtime, encoder.useAnnexB, encoder.scalabilityMode }, WebProcess::singleton().libWebRTCCodecs().workQueue(), WTFMove(callback), 0);
}

LibWebRTCCodecs::Encoder* LibWebRTCCodecs::createEncoderInternal(VideoCodecType type, const String& codec, const std::map<std::string, std::string>& formatParameters, bool isRealtime, bool useAnnexB, VideoEncoderScalabilityMode scalabilityMode, Function<void(Encoder*)>&& callback)
Expand Down Expand Up @@ -605,16 +603,14 @@ LibWebRTCCodecs::Encoder* LibWebRTCCodecs::createEncoderInternal(VideoCodecType
{
Locker locker { m_encodersConnectionLock };
createRemoteEncoder(*encoder, connection.get(), parameters, [identifier = encoder->identifier, callback = WTFMove(callback)](bool result) mutable {
WebProcess::singleton().libWebRTCCodecs().m_queue->dispatch([identifier, result, callback = WTFMove(callback)]() mutable {
if (!result) {
callback(nullptr);
return;
}

auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_encoders.get(identifier));
});
if (!result) {
callback(nullptr);
return;
}

auto& codecs = WebProcess::singleton().libWebRTCCodecs();
assertIsCurrent(codecs.workQueue());
callback(codecs.m_encoders.get(identifier));
});
setEncoderConnection(*encoder, connection.ptr());
}
Expand Down
4 changes: 2 additions & 2 deletions Source/WebKit/WebProcess/GPU/webrtc/LibWebRTCCodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class LibWebRTCCodecs : public IPC::WorkQueueMessageReceiver, public GPUProcessC
void deref() const final { return IPC::WorkQueueMessageReceiver::deref(); }
ThreadSafeWeakPtrControlBlock& controlBlock() const final { return IPC::WorkQueueMessageReceiver::controlBlock(); }

WorkQueue& workQueue() const { return m_queue; }

private:
LibWebRTCCodecs();
void ensureGPUProcessConnectionAndDispatchToThread(Function<void()>&&);
Expand All @@ -198,14 +200,12 @@ class LibWebRTCCodecs : public IPC::WorkQueueMessageReceiver, public GPUProcessC
void setDecoderConnection(Decoder&, RefPtr<IPC::Connection>&&) WTF_REQUIRES_LOCK(m_connectionLock);

template<typename Buffer> bool copySharedVideoFrame(LibWebRTCCodecs::Encoder&, IPC::Connection&, Buffer&&);
WorkQueue& workQueue() const { return m_queue; }

Decoder* createDecoderInternal(VideoCodecType, const String& codec, Function<void(Decoder(*))>&&);
Encoder* createEncoderInternal(VideoCodecType, const String& codec, const std::map<std::string, std::string>&, bool isRealtime, bool useAnnexB, WebCore::VideoEncoderScalabilityMode, Function<void(Encoder*)>&&);
template<typename Frame> int32_t encodeFrameInternal(Encoder&, const Frame&, bool shouldEncodeAsKeyFrame, WebCore::VideoFrameRotation, MediaTime, int64_t timestamp, std::optional<uint64_t> duration, Function<void(bool)>&&);
void initializeEncoderInternal(Encoder&, uint16_t width, uint16_t height, unsigned startBitrate, unsigned maxBitrate, unsigned minBitrate, uint32_t maxFramerate);

private:
RefPtr<IPC::Connection> protectedConnection() const WTF_REQUIRES_LOCK(m_connectionLock) { return m_connection; }
RefPtr<RemoteVideoFrameObjectHeapProxy> protectedVideoFrameObjectHeapProxy() const WTF_REQUIRES_LOCK(m_connectionLock);

Expand Down

0 comments on commit c73ac9c

Please sign in to comment.