diff --git a/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataEncoderCodec.cpp b/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataEncoderCodec.cpp index b5dd2d90ba12..ed98718e1773 100644 --- a/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataEncoderCodec.cpp +++ b/media/webrtc/signaling/src/media-conduit/WebrtcMediaDataEncoderCodec.cpp @@ -157,17 +157,25 @@ bool WebrtcMediaDataEncoder::InitEncoder() { int32_t WebrtcMediaDataEncoder::RegisterEncodeCompleteCallback( webrtc::EncodedImageCallback* aCallback) { - mCallback = aCallback; + OwnerThread()->Dispatch(NS_NewRunnableFunction( + "WebrtcMediaDataEncoder::RegisterEncodeCompleteCallback", + [self = RefPtr(this), aCallback]() { + self->mCallback = aCallback; + })); return WEBRTC_VIDEO_CODEC_OK; } int32_t WebrtcMediaDataEncoder::Shutdown() { LOG("Release encoder"); - auto rv = media::Await(do_AddRef(mThreadPool), mEncoder->Shutdown()); - mEncoder = nullptr; - mCallback = nullptr; - mError = NS_OK; - return rv.IsResolve() ? WEBRTC_VIDEO_CODEC_OK : WEBRTC_VIDEO_CODEC_ERROR; + OwnerThread()->Dispatch(NS_NewRunnableFunction( + "WebrtcMediaDataEncoder::Shutdown", + [self = RefPtr(this), + encoder = RefPtr(mEncoder.forget())]() { + self->mCallback = nullptr; + self->mError = NS_OK; + encoder->Shutdown(); + })); + return WEBRTC_VIDEO_CODEC_OK; } RefPtr WebrtcMediaDataEncoder::CreateVideoDataFromWebrtcVideoFrame( @@ -242,6 +250,10 @@ void WebrtcMediaDataEncoder::ProcessEncode( [display, self = RefPtr(this), // capture this for printing address in LOG. this](const MediaDataEncoder::EncodedData& aData) { + // Callback has been unregistered. + if (!mCallback) { + return; + } // The encoder haven't finished encoding yet. if (aData.IsEmpty()) { return;