Bug 1388591 - Implement OfflineAudioCanvas dict constructor; r=padenot draft
authorThomas Wisniewski <wisniewskit@gmail.com>
Tue, 08 Aug 2017 22:55:43 -0400
changeset 642998 dc5ac2c298a2f70cb7ba8c9f683a2e7ae27b4678
parent 642916 1d042bcb2632ea6a38fa08dbe21a6e8a0ee46961
child 725169 678831217b8c76bad6f4f9b512b067d7518fd182
push id72947
push userbmo:wisniewskit@gmail.com
push dateWed, 09 Aug 2017 04:07:34 +0000
reviewerspadenot
bugs1388591
milestone57.0a1
Bug 1388591 - Implement OfflineAudioCanvas dict constructor; r=padenot MozReview-Commit-ID: F9h9JO5tYeU
dom/media/webaudio/AudioContext.cpp
dom/media/webaudio/AudioContext.h
dom/media/webaudio/test/test_OfflineAudioContext.html
dom/webidl/OfflineAudioContext.webidl
--- a/dom/media/webaudio/AudioContext.cpp
+++ b/dom/media/webaudio/AudioContext.cpp
@@ -215,16 +215,28 @@ AudioContext::Constructor(const GlobalOb
 
   RegisterWeakMemoryReporter(object);
 
   return object.forget();
 }
 
 /* static */ already_AddRefed<AudioContext>
 AudioContext::Constructor(const GlobalObject& aGlobal,
+                          const OfflineAudioContextOptions& aOptions,
+                          ErrorResult& aRv)
+{
+  return Constructor(aGlobal,
+                     aOptions.mNumberOfChannels,
+                     aOptions.mLength,
+                     aOptions.mSampleRate,
+                     aRv);
+}
+
+/* static */ already_AddRefed<AudioContext>
+AudioContext::Constructor(const GlobalObject& aGlobal,
                           uint32_t aNumberOfChannels,
                           uint32_t aLength,
                           float aSampleRate,
                           ErrorResult& aRv)
 {
   nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
   if (!window) {
     aRv.Throw(NS_ERROR_FAILURE);
--- a/dom/media/webaudio/AudioContext.h
+++ b/dom/media/webaudio/AudioContext.h
@@ -3,16 +3,17 @@
 /* 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 AudioContext_h_
 #define AudioContext_h_
 
 #include "mozilla/dom/AudioChannelBinding.h"
+#include "mozilla/dom/OfflineAudioContextBinding.h"
 #include "MediaBufferDecoder.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/dom/TypedArray.h"
 #include "mozilla/UniquePtr.h"
 #include "nsCOMPtr.h"
 #include "nsCycleCollectionParticipant.h"
@@ -149,16 +150,22 @@ public:
   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 for offline AudioContext with options object
+  static already_AddRefed<AudioContext>
+  Constructor(const GlobalObject& aGlobal,
+              const OfflineAudioContextOptions& aOptions,
+              ErrorResult& aRv);
+
   // Constructor for offline AudioContext
   static already_AddRefed<AudioContext>
   Constructor(const GlobalObject& aGlobal,
               uint32_t aNumberOfChannels,
               uint32_t aLength,
               float aSampleRate,
               ErrorResult& aRv);
 
--- a/dom/media/webaudio/test/test_OfflineAudioContext.html
+++ b/dom/media/webaudio/test/test_OfflineAudioContext.html
@@ -26,43 +26,63 @@ function setOrCompareRenderedBuffer(aRen
     finish();
   } else {
     renderedBuffer = aRenderedBuffer;
   }
 }
 
 SimpleTest.waitForExplicitFinish();
 addLoadEvent(function() {
-  var ctx = new OfflineAudioContext(2, 100, 22050);
-  ok(ctx instanceof EventTarget, "OfflineAudioContexts must be EventTargets");
-  is(ctx.length, 100, "OfflineAudioContext.length is equal to the value passed to the ctor.");
+  let ctxs = [
+    new OfflineAudioContext(2, 100, 22050),
+    new OfflineAudioContext({length: 100, sampleRate: 22050}),
+    new OfflineAudioContext({channels: 2, length: 100, sampleRate: 22050}),
+  ];
 
-  var buf = ctx.createBuffer(2, 100, ctx.sampleRate);
-  for (var i = 0; i < 2; ++i) {
-    for (var j = 0; j < 100; ++j) {
-      buf.getChannelData(i)[j] = Math.sin(2 * Math.PI * 200 * j / ctx.sampleRate);
+  for (let ctx of ctxs) {
+    ok(ctx instanceof EventTarget, "OfflineAudioContexts must be EventTargets");
+    is(ctx.length, 100, "OfflineAudioContext.length is equal to the value passed to the ctor.");
+
+    var buf = ctx.createBuffer(2, 100, ctx.sampleRate);
+    for (var i = 0; i < 2; ++i) {
+      for (var j = 0; j < 100; ++j) {
+        buf.getChannelData(i)[j] = Math.sin(2 * Math.PI * 200 * j / ctx.sampleRate);
+      }
     }
   }
 
+  is(ctxs[1].destination.channelCount, 1, "OfflineAudioContext defaults to to correct channelCount.");
+
+  let ctx = ctxs[0];
+
   expectException(function() {
     new OfflineAudioContext(2, 100, 0);
   }, DOMException.NOT_SUPPORTED_ERR);
   expectException(function() {
     new OfflineAudioContext(2, 100, -1);
   }, DOMException.NOT_SUPPORTED_ERR);
   expectException(function() {
     new OfflineAudioContext(0, 100, 44100);
   }, DOMException.NOT_SUPPORTED_ERR);
   new OfflineAudioContext(32, 100, 44100);
   expectException(function() {
     new OfflineAudioContext(33, 100, 44100);
   }, DOMException.NOT_SUPPORTED_ERR);
   expectException(function() {
     new OfflineAudioContext(2, 0, 44100);
   }, DOMException.NOT_SUPPORTED_ERR);
+  expectTypeError(function() {
+    new OfflineAudioContext({});
+  });
+  expectTypeError(function() {
+    new OfflineAudioContext({sampleRate: 44100});
+  });
+  expectTypeError(function() {
+    new OfflineAudioContext({length: 44100*40});
+  });
 
   var src = ctx.createBufferSource();
   src.buffer = buf;
   src.start(0);
   src.connect(ctx.destination);
 
   ctx.addEventListener("complete", function(e) {
     ok(e instanceof OfflineAudioCompletionEvent, "Correct event received");
--- a/dom/webidl/OfflineAudioContext.webidl
+++ b/dom/webidl/OfflineAudioContext.webidl
@@ -5,17 +5,24 @@
  *
  * The origin of this IDL file is
  * https://webaudio.github.io/web-audio-api/
  *
  * Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C
  * liability, trademark and document use rules apply.
  */
 
-[Constructor(unsigned long numberOfChannels, unsigned long length, float sampleRate),
+dictionary OfflineAudioContextOptions {
+             unsigned long numberOfChannels = 1;
+    required unsigned long length;
+    required float         sampleRate;
+};
+
+[Constructor (OfflineAudioContextOptions contextOptions),
+Constructor(unsigned long numberOfChannels, unsigned long length, float sampleRate),
 Pref="dom.webaudio.enabled"]
 interface OfflineAudioContext : BaseAudioContext {
 
     [Throws]
     Promise<AudioBuffer> startRendering();
 
     // TODO: Promise<void>        suspend (double suspendTime);