Bug 1208371 - Count the users of a MediaStream to ease Destroy() responsibility. r=roc

MozReview-Commit-ID: FdcR4ChTND4

--HG--
extra : rebase_source : c0dfccffb686b483203b2906b734ae8b9459b924
This commit is contained in:
Andreas Pehrson 2016-01-05 10:16:22 +08:00
parent 98f7b22b8a
commit 1bffbe178b
2 changed files with 36 additions and 1 deletions

View File

@ -1839,6 +1839,7 @@ MediaStream::MediaStream(DOMMediaStream* aWrapper)
, mMainThreadFinished(false)
, mFinishedNotificationSent(false)
, mMainThreadDestroyed(false)
, mNrOfMainThreadUsers(0)
, mGraph(nullptr)
, mAudioChannelType(dom::AudioChannel::Normal)
{
@ -1982,6 +1983,8 @@ MediaStream::DestroyImpl()
void
MediaStream::Destroy()
{
NS_ASSERTION(mNrOfMainThreadUsers == 0,
"Do not mix Destroy() and RegisterUser()/UnregisterUser()");
// Keep this stream alive until we leave this method
RefPtr<MediaStream> kungFuDeathGrip = this;
@ -2006,6 +2009,26 @@ MediaStream::Destroy()
mMainThreadDestroyed = true;
}
void
MediaStream::RegisterUser()
{
MOZ_ASSERT(NS_IsMainThread());
++mNrOfMainThreadUsers;
}
void
MediaStream::UnregisterUser()
{
MOZ_ASSERT(NS_IsMainThread());
--mNrOfMainThreadUsers;
NS_ASSERTION(mNrOfMainThreadUsers >= 0, "Double-removal of main thread user");
NS_ASSERTION(!IsDestroyed(), "Do not mix Destroy() and RegisterUser()/UnregisterUser()");
if (mNrOfMainThreadUsers == 0) {
Destroy();
}
}
void
MediaStream::AddAudioOutput(void* aKey)
{

View File

@ -431,8 +431,19 @@ public:
*/
void RunAfterPendingUpdates(already_AddRefed<nsIRunnable> aRunnable);
// Signal that the client is done with this MediaStream. It will be deleted later.
// Signal that the client is done with this MediaStream. It will be deleted
// later. Do not mix usage of Destroy() with RegisterUser()/UnregisterUser().
// That will cause the MediaStream to be destroyed twice, which will cause
// some assertions to fail.
virtual void Destroy();
// Signal that a client is using this MediaStream. Useful to not have to
// explicitly manage ownership (responsibility to Destroy()) when there are
// multiple clients using a MediaStream.
void RegisterUser();
// Signal that a client no longer needs this MediaStream. When the number of
// clients using this MediaStream reaches 0, it will be destroyed.
void UnregisterUser();
// Returns the main-thread's view of how much data has been processed by
// this stream.
StreamTime GetCurrentTime()
@ -706,6 +717,7 @@ protected:
bool mMainThreadFinished;
bool mFinishedNotificationSent;
bool mMainThreadDestroyed;
int mNrOfMainThreadUsers;
// Our media stream graph. null if destroyed on the graph thread.
MediaStreamGraphImpl* mGraph;