Bug 853721 - Part 1: Refactor the code to convert GainNode's AudioParamTimeline to ticks into WebAudioUtils; r=roc
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 22 Mar 2013 00:01:28 -0400
changeset 125923 b42e344b20a9b6fec80ea0b36e65c0117b1b7800
parent 125922 89f25483717afe5184933a3b4604323adf678343
child 125924 d50dbae0b5388d40907ef5328668be697f34ad7e
push id25170
push usereakhgari@mozilla.com
push dateFri, 22 Mar 2013 12:33:51 +0000
treeherdermozilla-inbound@b42e344b20a9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs853721
milestone22.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 853721 - Part 1: Refactor the code to convert GainNode's AudioParamTimeline to ticks into WebAudioUtils; r=roc
content/media/webaudio/AudioParam.h
content/media/webaudio/AudioParamTimeline.h
content/media/webaudio/GainNode.cpp
content/media/webaudio/Makefile.in
content/media/webaudio/WebAudioUtils.cpp
content/media/webaudio/WebAudioUtils.h
--- a/content/media/webaudio/AudioParam.h
+++ b/content/media/webaudio/AudioParam.h
@@ -2,37 +2,34 @@
 /* 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 AudioParam_h_
 #define AudioParam_h_
 
-#include "AudioEventTimeline.h"
+#include "AudioParamTimeline.h"
 #include "nsWrapperCache.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCOMPtr.h"
 #include "EnableWebAudioCheck.h"
 #include "nsAutoPtr.h"
 #include "AudioNode.h"
 #include "mozilla/dom/TypedArray.h"
 #include "mozilla/Util.h"
-#include "mozilla/ErrorResult.h"
 #include "WebAudioUtils.h"
 
 struct JSContext;
 class nsIDOMWindow;
 
 namespace mozilla {
 
 namespace dom {
 
-typedef AudioEventTimeline<ErrorResult> AudioParamTimeline;
-
 class AudioParam MOZ_FINAL : public nsWrapperCache,
                              public EnableWebAudioCheck,
                              public AudioParamTimeline
 {
 public:
   typedef void (*CallbackType)(AudioNode*);
 
   AudioParam(AudioNode* aNode,
new file mode 100644
--- /dev/null
+++ b/content/media/webaudio/AudioParamTimeline.h
@@ -0,0 +1,26 @@
+/* -*- 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 AudioParamTimeline_h_
+#define AudioParamTimeline_h_
+
+// This header is intended to make it possible to use AudioParamTimeline
+// from multiple places without dealing with #include hell!
+
+#include "AudioEventTimeline.h"
+#include "mozilla/ErrorResult.h"
+
+namespace mozilla {
+
+namespace dom {
+
+typedef AudioEventTimeline<ErrorResult> AudioParamTimeline;
+
+}
+}
+
+#endif
+
--- a/content/media/webaudio/GainNode.cpp
+++ b/content/media/webaudio/GainNode.cpp
@@ -4,47 +4,30 @@
  * 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/. */
 
 #include "GainNode.h"
 #include "mozilla/dom/GainNodeBinding.h"
 #include "AudioNodeEngine.h"
 #include "AudioNodeStream.h"
 #include "AudioDestinationNode.h"
+#include "WebAudioUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED_1(GainNode, AudioNode,
                                      mGain)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(GainNode)
 NS_INTERFACE_MAP_END_INHERITING(AudioNode)
 
 NS_IMPL_ADDREF_INHERITED(GainNode, AudioNode)
 NS_IMPL_RELEASE_INHERITED(GainNode, AudioNode)
 
-struct ConvertTimeToTickHelper
-{
-  AudioNodeStream* mSourceStream;
-  AudioNodeStream* mDestinationStream;
-
-  static int64_t Convert(double aTime, void* aClosure)
-  {
-    TrackRate sampleRate = IdealAudioRate();
-
-    ConvertTimeToTickHelper* This = static_cast<ConvertTimeToTickHelper*> (aClosure);
-    TrackTicks tick = This->mSourceStream->GetCurrentPosition();
-    StreamTime streamTime = TicksToTimeRoundDown(sampleRate, tick);
-    GraphTime graphTime = This->mSourceStream->StreamTimeToGraphTime(streamTime);
-    StreamTime destinationStreamTime = This->mDestinationStream->GraphTimeToStreamTime(graphTime);
-    return TimeToTicksRoundDown(sampleRate, destinationStreamTime + SecondsToMediaTime(aTime));
-  }
-};
-
 class GainNodeEngine : public AudioNodeEngine
 {
 public:
   explicit GainNodeEngine(AudioDestinationNode* aDestination)
     : mSource(nullptr)
     , mDestination(static_cast<AudioNodeStream*> (aDestination->Stream()))
     // Keep the default value in sync with the default value in GainNode::GainNode.
     , mGain(1.f)
@@ -60,20 +43,17 @@ public:
     GAIN
   };
   void SetTimelineParameter(uint32_t aIndex, const AudioParamTimeline& aValue) MOZ_OVERRIDE
   {
     switch (aIndex) {
     case GAIN:
       MOZ_ASSERT(mSource && mDestination);
       mGain = aValue;
-      ConvertTimeToTickHelper ctth;
-      ctth.mSourceStream = mSource;
-      ctth.mDestinationStream = mDestination;
-      mGain.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth);
+      WebAudioUtils::ConvertAudioParamToTicks(mGain, mSource, mDestination);
       break;
     default:
       NS_ERROR("Bad GainNodeEngine TimelineParameter");
     }
   }
 
   virtual void ProduceAudioBlock(AudioNodeStream* aStream,
                                  const AudioChunk& aInput,
--- a/content/media/webaudio/Makefile.in
+++ b/content/media/webaudio/Makefile.in
@@ -27,16 +27,17 @@ CPPSRCS := \
   BiquadFilterNode.cpp \
   DelayNode.cpp \
   DynamicsCompressorNode.cpp \
   EnableWebAudioCheck.cpp \
   GainNode.cpp \
   MediaBufferDecoder.cpp \
   PannerNode.cpp \
   ThreeDPoint.cpp \
+  WebAudioUtils.cpp \
   $(NULL)
 
 EXPORTS_NAMESPACES := mozilla/dom
 EXPORTS_mozilla/dom := \
   AudioBuffer.h \
   AudioBufferSourceNode.h \
   AudioContext.h \
   AudioDestinationNode.h \
@@ -48,16 +49,17 @@ EXPORTS_mozilla/dom := \
   DelayNode.h \
   DynamicsCompressorNode.h \
   EnableWebAudioCheck.h \
   GainNode.h \
   PannerNode.h \
   $(NULL)
 
 EXPORTS := \
+  AudioParamTimeline.h \
   MediaBufferDecoder.h \
   ThreeDPoint.h \
   WebAudioUtils.h \
   $(NULL)
 
 FORCE_STATIC_LIB := 1
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/content/media/webaudio/WebAudioUtils.cpp
@@ -0,0 +1,44 @@
+/* -*- 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/. */
+
+#include "WebAudioUtils.h"
+#include "AudioNodeStream.h"
+
+namespace mozilla {
+
+namespace dom {
+
+struct ConvertTimeToTickHelper
+{
+  AudioNodeStream* mSourceStream;
+  AudioNodeStream* mDestinationStream;
+
+  static int64_t Convert(double aTime, void* aClosure)
+  {
+    TrackRate sampleRate = IdealAudioRate();
+
+    ConvertTimeToTickHelper* This = static_cast<ConvertTimeToTickHelper*> (aClosure);
+    TrackTicks tick = This->mSourceStream->GetCurrentPosition();
+    StreamTime streamTime = TicksToTimeRoundDown(sampleRate, tick);
+    GraphTime graphTime = This->mSourceStream->StreamTimeToGraphTime(streamTime);
+    StreamTime destinationStreamTime = This->mDestinationStream->GraphTimeToStreamTime(graphTime);
+    return TimeToTicksRoundDown(sampleRate, destinationStreamTime + SecondsToMediaTime(aTime));
+  }
+};
+
+void
+WebAudioUtils::ConvertAudioParamToTicks(AudioParamTimeline& aParam,
+                                        AudioNodeStream* aSource,
+                                        AudioNodeStream* aDest)
+{
+  ConvertTimeToTickHelper ctth;
+  ctth.mSourceStream = aSource;
+  ctth.mDestinationStream = aDest;
+  aParam.ConvertEventTimesToTicks(ConvertTimeToTickHelper::Convert, &ctth);
+}
+
+}
+}
--- a/content/media/webaudio/WebAudioUtils.h
+++ b/content/media/webaudio/WebAudioUtils.h
@@ -3,31 +3,47 @@
 /* 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 WebAudioUtils_h_
 #define WebAudioUtils_h_
 
 #include <cmath>
+#include "AudioParamTimeline.h"
 
 namespace mozilla {
 
+class AudioNodeStream;
+
 namespace dom {
 
 struct WebAudioUtils {
   static bool FuzzyEqual(float v1, float v2)
   {
     using namespace std;
     return fabsf(v1 - v2) < 1e-7f;
   }
   static bool FuzzyEqual(double v1, double v2)
   {
     using namespace std;
     return fabs(v1 - v2) < 1e-7;
   }
+
+  /**
+   * Converts AudioParamTimeline floating point time values to tick values
+   * with respect to a source and a destination AudioNodeStream.
+   *
+   * This needs to be called for each AudioParamTimeline that gets sent to an
+   * AudioNodeEngine on the engine side where the AudioParamTimeline is
+   * received.  This means that such engines need to be aware of their source
+   * and destination streams as well.
+   */
+  static void ConvertAudioParamToTicks(AudioParamTimeline& aParam,
+                                       AudioNodeStream* aSource,
+                                       AudioNodeStream* aDest);
 };
 
 }
 }
 
 #endif