From 782202963ef662aea5b51e182c4dcb3fd918c6d5 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 25 Oct 2023 03:59:10 +0200 Subject: [media] Use RefPtr instead of raw pointers --- dom/media/mediasource/MediaSourceDemuxer.cpp | 39 +++++++++++++++------------- dom/media/mediasource/MediaSourceDemuxer.h | 10 +++---- dom/media/mediasource/SourceBuffer.cpp | 4 +-- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/dom/media/mediasource/MediaSourceDemuxer.cpp b/dom/media/mediasource/MediaSourceDemuxer.cpp index cace222167..4c8f1d0c94 100644 --- a/dom/media/mediasource/MediaSourceDemuxer.cpp +++ b/dom/media/mediasource/MediaSourceDemuxer.cpp @@ -62,7 +62,7 @@ MediaSourceDemuxer::AddSizeOfResources(MediaSourceDecoder::ResourceSizes* aSizes RefPtr sizes = aSizes; nsCOMPtr task = NS_NewRunnableFunction([self, sizes] () { - for (TrackBuffersManager* manager : self->mSourceBuffers) { + for (const RefPtr& manager : self->mSourceBuffers) { manager->AddSizeOfResources(sizes); } }); @@ -171,42 +171,45 @@ MediaSourceDemuxer::GetCrypto() } void -MediaSourceDemuxer::AttachSourceBuffer(TrackBuffersManager* aSourceBuffer) +MediaSourceDemuxer::AttachSourceBuffer( + RefPtr& aSourceBuffer) { - nsCOMPtr task = - NewRunnableMethod( + nsCOMPtr task = NewRunnableMethod&&>( this, &MediaSourceDemuxer::DoAttachSourceBuffer, aSourceBuffer); GetTaskQueue()->Dispatch(task.forget()); } void -MediaSourceDemuxer::DoAttachSourceBuffer(mozilla::TrackBuffersManager* aSourceBuffer) +MediaSourceDemuxer::DoAttachSourceBuffer( + RefPtr&& aSourceBuffer) { MOZ_ASSERT(OnTaskQueue()); - mSourceBuffers.AppendElement(aSourceBuffer); + mSourceBuffers.AppendElement(Move(aSourceBuffer)); ScanSourceBuffersForContent(); } void -MediaSourceDemuxer::DetachSourceBuffer(TrackBuffersManager* aSourceBuffer) +MediaSourceDemuxer::DetachSourceBuffer( + RefPtr& aSourceBuffer) { - nsCOMPtr task = - NewRunnableMethod( - this, &MediaSourceDemuxer::DoDetachSourceBuffer, - aSourceBuffer); + RefPtr self = this; + nsCOMPtr task = NS_NewRunnableFunction( + [self, aSourceBuffer]() { + self->DoDetachSourceBuffer(aSourceBuffer); + }); GetTaskQueue()->Dispatch(task.forget()); } void -MediaSourceDemuxer::DoDetachSourceBuffer(TrackBuffersManager* aSourceBuffer) +MediaSourceDemuxer::DoDetachSourceBuffer( + const RefPtr& aSourceBuffer) { MOZ_ASSERT(OnTaskQueue()); - for (uint32_t i = 0; i < mSourceBuffers.Length(); i++) { - if (mSourceBuffers[i].get() == aSourceBuffer) { - mSourceBuffers.RemoveElementAt(i); - } - } + mSourceBuffers.RemoveElementsBy( + [&aSourceBuffer](const RefPtr aLinkedSourceBuffer) { + return aLinkedSourceBuffer == aSourceBuffer; + }); if (aSourceBuffer == mAudioTrack) { mAudioTrack = nullptr; } @@ -230,7 +233,7 @@ MediaSourceDemuxer::GetTrackInfo(TrackType aTrack) } } -TrackBuffersManager* +RefPtr MediaSourceDemuxer::GetManager(TrackType aTrack) { MonitorAutoLock mon(mMonitor); diff --git a/dom/media/mediasource/MediaSourceDemuxer.h b/dom/media/mediasource/MediaSourceDemuxer.h index e9db46196c..1518364c33 100644 --- a/dom/media/mediasource/MediaSourceDemuxer.h +++ b/dom/media/mediasource/MediaSourceDemuxer.h @@ -45,8 +45,8 @@ public: void NotifyDataArrived() override; /* interface for TrackBuffersManager */ - void AttachSourceBuffer(TrackBuffersManager* aSourceBuffer); - void DetachSourceBuffer(TrackBuffersManager* aSourceBuffer); + void AttachSourceBuffer(RefPtr& aSourceBuffer); + void DetachSourceBuffer(RefPtr& aSourceBuffer); AutoTaskQueue* GetTaskQueue() { return mTaskQueue; } // Returns a string describing the state of the MediaSource internal @@ -64,10 +64,10 @@ private: // Scan source buffers and update information. bool ScanSourceBuffersForContent(); RefPtr AttemptInit(); - TrackBuffersManager* GetManager(TrackInfo::TrackType aType); + RefPtr GetManager(TrackInfo::TrackType aType); TrackInfo* GetTrackInfo(TrackInfo::TrackType); - void DoAttachSourceBuffer(TrackBuffersManager* aSourceBuffer); - void DoDetachSourceBuffer(TrackBuffersManager* aSourceBuffer); + void DoAttachSourceBuffer(RefPtr&& aSourceBuffer); + void DoDetachSourceBuffer(const RefPtr& aSourceBuffer); bool OnTaskQueue() { return !GetTaskQueue() || GetTaskQueue()->IsCurrentThreadIn(); diff --git a/dom/media/mediasource/SourceBuffer.cpp b/dom/media/mediasource/SourceBuffer.cpp index 26ea937077..0bdfbfcf23 100644 --- a/dom/media/mediasource/SourceBuffer.cpp +++ b/dom/media/mediasource/SourceBuffer.cpp @@ -281,7 +281,7 @@ SourceBuffer::Detach() if (mTrackBuffersManager) { mTrackBuffersManager->Detach(); mMediaSource->GetDecoder()->GetDemuxer()->DetachSourceBuffer( - mTrackBuffersManager.get()); + mTrackBuffersManager); } mTrackBuffersManager = nullptr; mMediaSource = nullptr; @@ -321,7 +321,7 @@ SourceBuffer::SourceBuffer(MediaSource* aMediaSource, const nsACString& aType) SetMode(SourceBufferAppendMode::Segments, dummy); } mMediaSource->GetDecoder()->GetDemuxer()->AttachSourceBuffer( - mTrackBuffersManager.get()); + mTrackBuffersManager); } SourceBuffer::~SourceBuffer() -- cgit v1.2.3