Bug 1296531 - Break out SineWaveGenerator from MediaEngineDefault. r?jesup draft
authorAndreas Pehrson <pehrsons@gmail.com>
Fri, 17 Feb 2017 11:48:33 +0100
changeset 670306 5d553f6070f29584e3e1d76db856e136b0589a1e
parent 670305 781fe7fc1263359c80176b5561fa07e63aa5f0a6
child 670307 f4c75e4b5ec6cc15c00e4ee61f4d1a5ce6bfe371
push id81598
push userbmo:apehrson@mozilla.com
push dateTue, 26 Sep 2017 09:13:19 +0000
reviewersjesup
bugs1296531
milestone58.0a1
Bug 1296531 - Break out SineWaveGenerator from MediaEngineDefault. r?jesup For re-use in TestAudioTrackEncoder. MozReview-Commit-ID: AQvIiKkxkFH
dom/media/webrtc/MediaEngineDefault.cpp
dom/media/webrtc/MediaEngineDefault.h
dom/media/webrtc/SineWaveGenerator.h
dom/media/webrtc/moz.build
--- a/dom/media/webrtc/MediaEngineDefault.cpp
+++ b/dom/media/webrtc/MediaEngineDefault.cpp
@@ -318,64 +318,16 @@ MediaEngineDefaultVideoSource::NotifyPul
     IntSize size(image ? mOpts.mWidth : 0, image ? mOpts.mHeight : 0);
     segment.AppendFrame(image.forget(), delta, size, aPrincipalHandle);
     // This can fail if either a) we haven't added the track yet, or b)
     // we've removed or finished the track.
     aSource->AppendToTrack(aID, &segment);
   }
 }
 
-// generate 1k sine wave per second
-class SineWaveGenerator
-{
-public:
-  static const int bytesPerSample = 2;
-  static const int millisecondsPerSecond = PR_MSEC_PER_SEC;
-
-  explicit SineWaveGenerator(uint32_t aSampleRate, uint32_t aFrequency) :
-    mTotalLength(aSampleRate / aFrequency),
-    mReadLength(0) {
-    // If we allow arbitrary frequencies, there's no guarantee we won't get rounded here
-    // We could include an error term and adjust for it in generation; not worth the trouble
-    //MOZ_ASSERT(mTotalLength * aFrequency == aSampleRate);
-    mAudioBuffer = MakeUnique<int16_t[]>(mTotalLength);
-    for (int i = 0; i < mTotalLength; i++) {
-      // Set volume to -20db. It's from 32768.0 * 10^(-20/20) = 3276.8
-      mAudioBuffer[i] = (3276.8f * sin(2 * M_PI * i / mTotalLength));
-    }
-  }
-
-  // NOTE: only safely called from a single thread (MSG callback)
-  void generate(int16_t* aBuffer, int16_t aLengthInSamples) {
-    int16_t remaining = aLengthInSamples;
-
-    while (remaining) {
-      int16_t processSamples = 0;
-
-      if (mTotalLength - mReadLength >= remaining) {
-        processSamples = remaining;
-      } else {
-        processSamples = mTotalLength - mReadLength;
-      }
-      memcpy(aBuffer, &mAudioBuffer[mReadLength], processSamples * bytesPerSample);
-      aBuffer += processSamples;
-      mReadLength += processSamples;
-      remaining -= processSamples;
-      if (mReadLength == mTotalLength) {
-        mReadLength = 0;
-      }
-    }
-  }
-
-private:
-  UniquePtr<int16_t[]> mAudioBuffer;
-  int16_t mTotalLength;
-  int16_t mReadLength;
-};
-
 /**
  * Default audio source.
  */
 
 NS_IMPL_ISUPPORTS0(MediaEngineDefaultAudioSource)
 
 MediaEngineDefaultAudioSource::MediaEngineDefaultAudioSource()
   : MediaEngineAudioSource(kReleased)
--- a/dom/media/webrtc/MediaEngineDefault.h
+++ b/dom/media/webrtc/MediaEngineDefault.h
@@ -19,16 +19,17 @@
 #include "VideoSegment.h"
 #include "AudioSegment.h"
 #include "StreamTracks.h"
 #ifdef MOZ_WEBRTC
 #include "MediaEngineCameraVideoSource.h"
 #endif
 #include "MediaStreamGraph.h"
 #include "MediaTrackConstraints.h"
+#include "SineWaveGenerator.h"
 
 namespace mozilla {
 
 namespace layers {
 class ImageContainer;
 } // namespace layers
 
 class MediaEngineDefault;
new file mode 100644
--- /dev/null
+++ b/dom/media/webrtc/SineWaveGenerator.h
@@ -0,0 +1,60 @@
+/* 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 SINEWAVEGENERATOR_H_
+#define SINEWAVEGENERATOR_H_
+
+namespace mozilla {
+
+// generate 1k sine wave per second
+class SineWaveGenerator
+{
+public:
+  static const int bytesPerSample = 2;
+  static const int millisecondsPerSecond = PR_MSEC_PER_SEC;
+
+  explicit SineWaveGenerator(uint32_t aSampleRate, uint32_t aFrequency) :
+    mTotalLength(aSampleRate / aFrequency),
+    mReadLength(0) {
+    // If we allow arbitrary frequencies, there's no guarantee we won't get rounded here
+    // We could include an error term and adjust for it in generation; not worth the trouble
+    //MOZ_ASSERT(mTotalLength * aFrequency == aSampleRate);
+    mAudioBuffer = MakeUnique<int16_t[]>(mTotalLength);
+    for (int i = 0; i < mTotalLength; i++) {
+      // Set volume to -20db. It's from 32768.0 * 10^(-20/20) = 3276.8
+      mAudioBuffer[i] = (3276.8f * sin(2 * M_PI * i / mTotalLength));
+    }
+  }
+
+  // NOTE: only safely called from a single thread (MSG callback)
+  void generate(int16_t* aBuffer, int16_t aLengthInSamples) {
+    int16_t remaining = aLengthInSamples;
+
+    while (remaining) {
+      int16_t processSamples = 0;
+
+      if (mTotalLength - mReadLength >= remaining) {
+        processSamples = remaining;
+      } else {
+        processSamples = mTotalLength - mReadLength;
+      }
+      memcpy(aBuffer, &mAudioBuffer[mReadLength], processSamples * bytesPerSample);
+      aBuffer += processSamples;
+      mReadLength += processSamples;
+      remaining -= processSamples;
+      if (mReadLength == mTotalLength) {
+        mReadLength = 0;
+      }
+    }
+  }
+
+private:
+  UniquePtr<int16_t[]> mAudioBuffer;
+  int16_t mTotalLength;
+  int16_t mReadLength;
+};
+
+} // namespace mozilla
+
+#endif /* SINEWAVEGENERATOR_H_ */
--- a/dom/media/webrtc/moz.build
+++ b/dom/media/webrtc/moz.build
@@ -12,16 +12,17 @@ with Files('PeerIdentity.*'):
 
 XPIDL_MODULE = 'content_webrtc'
 
 EXPORTS += [
     'MediaEngine.h',
     'MediaEngineCameraVideoSource.h',
     'MediaEngineDefault.h',
     'MediaTrackConstraints.h',
+    'SineWaveGenerator.h',
 ]
 
 if CONFIG['MOZ_WEBRTC']:
     if CONFIG['OS_TARGET'] == 'WINNT':
         DEFINES['WEBRTC_WIN'] = True
     else:
         DEFINES['WEBRTC_POSIX'] = True
     EXPORTS += ['AudioOutputObserver.h',