Bug 1387454 - Set sample rate in AudioContext constructor. r=padenot
authorAlex Chronopoulos <achronop@gmail.com>
Tue, 03 Apr 2018 20:02:15 +0300
changeset 411951 e6ab07a07c3b6b965328536dafe4b9779aca9a99
parent 411950 b3a05d5d62168ff0f927139c7bd916f8b4f380ae
child 411952 6e227df35207d4eac605804c925ae2c916c79134
push id33778
push userapavel@mozilla.com
push dateFri, 06 Apr 2018 09:57:38 +0000
treeherdermozilla-central@46a5fc19bd7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1387454
milestone61.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 1387454 - Set sample rate in AudioContext constructor. r=padenot MozReview-Commit-ID: 9uQLotrF86k
dom/media/webaudio/AudioContext.cpp
dom/media/webaudio/AudioContext.h
--- a/dom/media/webaudio/AudioContext.cpp
+++ b/dom/media/webaudio/AudioContext.cpp
@@ -115,17 +115,17 @@ NS_IMPL_ADDREF_INHERITED(AudioContext, D
 NS_IMPL_RELEASE_INHERITED(AudioContext, DOMEventTargetHelper)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioContext)
   NS_INTERFACE_MAP_ENTRY(nsIMemoryReporter)
 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
 
 static float GetSampleRateForAudioContext(bool aIsOffline, float aSampleRate)
 {
-  if (aIsOffline) {
+  if (aIsOffline || aSampleRate != 0.0) {
     return aSampleRate;
   } else {
     return static_cast<float>(CubebUtils::PreferredSampleRate());
   }
 }
 
 AudioContext::AudioContext(nsPIDOMWindowInner* aWindow,
                            bool aIsOffline,
@@ -192,28 +192,37 @@ AudioContext::WrapObject(JSContext* aCx,
     return OfflineAudioContextBinding::Wrap(aCx, this, aGivenProto);
   } else {
     return AudioContextBinding::Wrap(aCx, this, aGivenProto);
   }
 }
 
 /* static */ already_AddRefed<AudioContext>
 AudioContext::Constructor(const GlobalObject& aGlobal,
+                          const AudioContextOptions& aOptions,
                           ErrorResult& aRv)
 {
   nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
   if (!window) {
     aRv.Throw(NS_ERROR_FAILURE);
     return nullptr;
   }
 
+  if (aOptions.mSampleRate > 0 &&
+      (aOptions.mSampleRate - WebAudioUtils::MinSampleRate < 0.0 ||
+      WebAudioUtils::MaxSampleRate - aOptions.mSampleRate < 0.0)) {
+    aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
+    return nullptr;
+  }
+
   uint32_t maxChannelCount = std::min<uint32_t>(WebAudioUtils::MaxChannelCount,
       CubebUtils::MaxNumberOfChannels());
   RefPtr<AudioContext> object =
-    new AudioContext(window, false,maxChannelCount);
+    new AudioContext(window, false, maxChannelCount,
+                     0, aOptions.mSampleRate);
   aRv = object->Init();
   if (NS_WARN_IF(aRv.Failed())) {
      return nullptr;
   }
 
   RegisterWeakMemoryReporter(object);
 
   return object.forget();
--- a/dom/media/webaudio/AudioContext.h
+++ b/dom/media/webaudio/AudioContext.h
@@ -111,16 +111,17 @@ public:
 private:
   RefPtr<AudioContext> mAudioContext;
   void* mPromise;
   RefPtr<AudioNodeStream> mAudioNodeStream;
   AudioContextState mNewState;
 };
 
 enum class AudioContextOperation { Suspend, Resume, Close };
+struct AudioContextOptions;
 
 class AudioContext final : public DOMEventTargetHelper,
                            public nsIMemoryReporter,
                            public RelativeTimeline
 {
   AudioContext(nsPIDOMWindowInner* aParentWindow,
                bool aIsOffline,
                uint32_t aNumberOfChannels = 0,
@@ -148,17 +149,19 @@ public:
   void Shutdown(); // idempotent
 
   JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   using DOMEventTargetHelper::DispatchTrustedEvent;
 
   // Constructor for regular AudioContext
   static already_AddRefed<AudioContext>
-  Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
+  Constructor(const GlobalObject& aGlobal,
+              const AudioContextOptions& aOptions,
+              ErrorResult& aRv);
 
   // Constructor for offline AudioContext with options object
   static already_AddRefed<AudioContext>
   Constructor(const GlobalObject& aGlobal,
               const OfflineAudioContextOptions& aOptions,
               ErrorResult& aRv);
 
   // Constructor for offline AudioContext