Bug 1742959 - Do not ref-count MediaEventForwarder. r=mjf

Differential Revision: https://phabricator.services.mozilla.com/D132216
This commit is contained in:
Andreas Pehrson 2021-11-30 17:47:20 +00:00
parent b989c39bda
commit 6ed647aa59
4 changed files with 24 additions and 28 deletions

View File

@ -540,8 +540,6 @@ class MediaEventForwarder : public MediaEventSource<Es...> {
template <typename T>
using ArgType = typename detail::EventTypeTraits<T>::ArgType;
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaEventForwarder);
explicit MediaEventForwarder(nsCOMPtr<nsISerialEventTarget> aEventTarget)
: mEventTarget(std::move(aEventTarget)) {}
@ -549,6 +547,8 @@ class MediaEventForwarder : public MediaEventSource<Es...> {
: mEventTarget(aOther.mEventTarget),
mListeners(std::move(aOther.mListeners)) {}
~MediaEventForwarder() { MOZ_ASSERT(mListeners.IsEmpty()); }
MediaEventForwarder& operator=(MediaEventForwarder&& aOther) {
MOZ_RELEASE_ASSERT(mEventTarget == aOther.mEventTarget);
MOZ_ASSERT(mListeners.IsEmpty());
@ -556,10 +556,11 @@ class MediaEventForwarder : public MediaEventSource<Es...> {
}
void Forward(MediaEventSource<Es...>& aSource) {
mListeners.AppendElement(aSource.Connect(
mEventTarget,
[self = RefPtr<MediaEventForwarder>(this)](ArgType<Es>&&... aEvents) {
self->NotifyInternal(std::forward<ArgType<Es>...>(aEvents)...);
// Forwarding a rawptr `this` here is fine, since DisconnectAll disconnect
// all mListeners synchronously and prevents this handler from running.
mListeners.AppendElement(
aSource.Connect(mEventTarget, [this](ArgType<Es>&&... aEvents) {
this->NotifyInternal(std::forward<ArgType<Es>...>(aEvents)...);
}));
}
@ -571,8 +572,6 @@ class MediaEventForwarder : public MediaEventSource<Es...> {
}
private:
~MediaEventForwarder() { MOZ_ASSERT(mListeners.IsEmpty()); }
const nsCOMPtr<nsISerialEventTarget> mEventTarget;
nsTArray<MediaEventListener> mListeners;
};

View File

@ -56,32 +56,31 @@ class MediaEventBlocker : public ShutdownBlocker {
class RefCountedTicket {
RefPtr<MediaEventBlocker> mBlocker;
RefPtr<MediaEventForwarder<void>> mShutdownEventForwarder;
MediaEventForwarder<void> mShutdownEventForwarder;
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedTicket)
RefCountedTicket()
: mShutdownEventForwarder(
new MediaEventForwarder<void>(GetMainThreadSerialEventTarget())) {}
: mShutdownEventForwarder(GetMainThreadSerialEventTarget()) {}
void AddBlocker(const nsString& aName, const nsString& aFileName,
int32_t aLineNr) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mBlocker);
mBlocker = MakeAndAddRef<MediaEventBlocker>(aName);
mShutdownEventForwarder->Forward(mBlocker->ShutdownEvent());
mShutdownEventForwarder.Forward(mBlocker->ShutdownEvent());
GetShutdownBarrier()->AddBlocker(mBlocker.get(), aFileName, aLineNr, aName);
}
MediaEventSource<void>& ShutdownEvent() { return *mShutdownEventForwarder; }
MediaEventSource<void>& ShutdownEvent() { return mShutdownEventForwarder; }
protected:
virtual ~RefCountedTicket() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mBlocker);
GetShutdownBarrier()->RemoveBlocker(mBlocker.get());
mShutdownEventForwarder->DisconnectAll();
mShutdownEventForwarder.DisconnectAll();
}
};

View File

@ -35,8 +35,8 @@ WebrtcVideoDecoderFactory::CreateVideoDecoder(
case webrtc::VideoCodecType::kVideoCodecH264: {
// Get an external decoder
auto gmpDecoder = WrapUnique(GmpVideoCodec::CreateDecoder(mPCHandle));
mCreatedGmpPluginEvent->Forward(*gmpDecoder->InitPluginEvent());
mReleasedGmpPluginEvent->Forward(*gmpDecoder->ReleasePluginEvent());
mCreatedGmpPluginEvent.Forward(*gmpDecoder->InitPluginEvent());
mReleasedGmpPluginEvent.Forward(*gmpDecoder->ReleasePluginEvent());
decoder.reset(gmpDecoder.release());
break;
}
@ -103,8 +103,8 @@ WebrtcVideoEncoderFactory::InternalFactory::CreateVideoEncoder(
case webrtc::VideoCodecType::kVideoCodecH264: {
// get an external encoder
auto gmpEncoder = WrapUnique(GmpVideoCodec::CreateEncoder(mPCHandle));
mCreatedGmpPluginEvent->Forward(*gmpEncoder->InitPluginEvent());
mReleasedGmpPluginEvent->Forward(*gmpEncoder->ReleasePluginEvent());
mCreatedGmpPluginEvent.Forward(*gmpEncoder->InitPluginEvent());
mReleasedGmpPluginEvent.Forward(*gmpEncoder->ReleasePluginEvent());
encoder.reset(gmpEncoder.release());
break;
}

View File

@ -20,30 +20,28 @@ class GmpPluginNotifier : public GmpPluginNotifierInterface {
public:
explicit GmpPluginNotifier(nsCOMPtr<nsISerialEventTarget> aOwningThread)
: mOwningThread(std::move(aOwningThread)),
mCreatedGmpPluginEvent(
new MediaEventForwarder<uint64_t>(mOwningThread)),
mReleasedGmpPluginEvent(
new MediaEventForwarder<uint64_t>(mOwningThread)) {}
mCreatedGmpPluginEvent(mOwningThread),
mReleasedGmpPluginEvent(mOwningThread) {}
~GmpPluginNotifier() {
mCreatedGmpPluginEvent->DisconnectAll();
mReleasedGmpPluginEvent->DisconnectAll();
mCreatedGmpPluginEvent.DisconnectAll();
mReleasedGmpPluginEvent.DisconnectAll();
}
MediaEventSource<uint64_t>& CreatedGmpPluginEvent() override {
MOZ_ASSERT(mOwningThread->IsOnCurrentThread());
return *mCreatedGmpPluginEvent;
return mCreatedGmpPluginEvent;
}
MediaEventSource<uint64_t>& ReleasedGmpPluginEvent() override {
MOZ_ASSERT(mOwningThread->IsOnCurrentThread());
return *mReleasedGmpPluginEvent;
return mReleasedGmpPluginEvent;
}
protected:
const nsCOMPtr<nsISerialEventTarget> mOwningThread;
RefPtr<MediaEventForwarder<uint64_t>> mCreatedGmpPluginEvent;
RefPtr<MediaEventForwarder<uint64_t>> mReleasedGmpPluginEvent;
MediaEventForwarder<uint64_t> mCreatedGmpPluginEvent;
MediaEventForwarder<uint64_t> mReleasedGmpPluginEvent;
};
class WebrtcVideoDecoderFactory : public GmpPluginNotifier,