Bug 890072 - Part 1: Factor out PlayingRefChanged from DelayNodeEngine; r=roc a=webaudio
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 03 Jul 2013 21:37:39 -0400
changeset 148713 f12e9f364c3a38f560e17c6d6577eca751df698e
parent 148712 554a3d3aeefe9d58073c5d0076df1913024d6503
child 148714 f6e678faa38c175bd3f4c1b63cba9eda6b7457b0
push id368
push userbbajaj@mozilla.com
push dateMon, 09 Sep 2013 22:57:58 +0000
treeherdermozilla-release@5a4f47ae1217 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, webaudio
bugs890072
milestone24.0a2
Bug 890072 - Part 1: Factor out PlayingRefChanged from DelayNodeEngine; r=roc a=webaudio
content/media/webaudio/DelayNode.cpp
content/media/webaudio/DelayNode.h
content/media/webaudio/PlayingRefChangeHandler.h
--- a/content/media/webaudio/DelayNode.cpp
+++ b/content/media/webaudio/DelayNode.cpp
@@ -20,52 +20,17 @@ NS_IMPL_CYCLE_COLLECTION_INHERITED_1(Del
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DelayNode)
 NS_INTERFACE_MAP_END_INHERITING(AudioNode)
 
 NS_IMPL_ADDREF_INHERITED(DelayNode, AudioNode)
 NS_IMPL_RELEASE_INHERITED(DelayNode, AudioNode)
 
 class DelayNodeEngine : public AudioNodeEngine
 {
-  class PlayingRefChanged : public nsRunnable
-  {
-  public:
-    enum ChangeType { ADDREF, RELEASE };
-    PlayingRefChanged(AudioNodeStream* aStream, ChangeType aChange)
-      : mStream(aStream)
-      , mChange(aChange)
-    {
-    }
-
-    NS_IMETHOD Run()
-    {
-      nsRefPtr<DelayNode> node;
-      {
-        // No need to keep holding the lock for the whole duration of this
-        // function, since we're holding a strong reference to it, so if
-        // we can obtain the reference, we will hold the node alive in
-        // this function.
-        MutexAutoLock lock(mStream->Engine()->NodeMutex());
-        node = static_cast<DelayNode*>(mStream->Engine()->Node());
-      }
-      if (node) {
-        if (mChange == ADDREF) {
-          node->mPlayingRef.Take(node);
-        } else if (mChange == RELEASE) {
-          node->mPlayingRef.Drop(node);
-        }
-      }
-      return NS_OK;
-    }
-
-  private:
-    nsRefPtr<AudioNodeStream> mStream;
-    ChangeType mChange;
-  };
-
+  typedef PlayingRefChangeHandler<DelayNode> PlayingRefChanged;
 public:
   DelayNodeEngine(AudioNode* aNode, AudioDestinationNode* aDestination)
     : AudioNodeEngine(aNode)
     , mSource(nullptr)
     , mDestination(static_cast<AudioNodeStream*> (aDestination->Stream()))
     // Keep the default value in sync with the default value in DelayNode::DelayNode.
     , mDelay(0.f)
     , mMaxDelay(0.)
--- a/content/media/webaudio/DelayNode.h
+++ b/content/media/webaudio/DelayNode.h
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef DelayNode_h_
 #define DelayNode_h_
 
 #include "AudioNode.h"
 #include "AudioParam.h"
+#include "PlayingRefChangeHandler.h"
 
 namespace mozilla {
 namespace dom {
 
 class AudioContext;
 
 class DelayNode : public AudioNode
 {
@@ -29,16 +30,17 @@ public:
   AudioParam* DelayTime() const
   {
     return mDelay;
   }
 
 private:
   static void SendDelayToStream(AudioNode* aNode);
   friend class DelayNodeEngine;
+  friend class PlayingRefChangeHandler<DelayNode>;
 
 private:
   nsRefPtr<AudioParam> mDelay;
   SelfReference<DelayNode> mPlayingRef;
 };
 
 }
 }
new file mode 100644
--- /dev/null
+++ b/content/media/webaudio/PlayingRefChangeHandler.h
@@ -0,0 +1,57 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef PlayingRefChangeHandler_h__
+#define PlayingRefChangeHandler_h__
+
+#include "nsThreadUtils.h"
+#include "AudioNodeStream.h"
+
+namespace mozilla {
+namespace dom {
+
+template<class NodeType>
+class PlayingRefChangeHandler : public nsRunnable
+{
+public:
+  enum ChangeType { ADDREF, RELEASE };
+  PlayingRefChangeHandler(AudioNodeStream* aStream, ChangeType aChange)
+    : mStream(aStream)
+    , mChange(aChange)
+  {
+  }
+
+  NS_IMETHOD Run()
+  {
+    nsRefPtr<NodeType> node;
+    {
+      // No need to keep holding the lock for the whole duration of this
+      // function, since we're holding a strong reference to it, so if
+      // we can obtain the reference, we will hold the node alive in
+      // this function.
+      MutexAutoLock lock(mStream->Engine()->NodeMutex());
+      node = static_cast<NodeType*>(mStream->Engine()->Node());
+    }
+    if (node) {
+      if (mChange == ADDREF) {
+        node->mPlayingRef.Take(node);
+      } else if (mChange == RELEASE) {
+        node->mPlayingRef.Drop(node);
+      }
+    }
+    return NS_OK;
+  }
+
+private:
+  nsRefPtr<AudioNodeStream> mStream;
+  ChangeType mChange;
+};
+
+}
+}
+
+#endif
+