Bug 1296531 - Add AudioGenerator to TestAudioTrackEncoder for simple generation of audio. r?jesup draft
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 16 May 2017 12:39:39 +0200
changeset 670307 f4c75e4b5ec6cc15c00e4ee61f4d1a5ce6bfe371
parent 670306 5d553f6070f29584e3e1d76db856e136b0589a1e
child 670308 420c79e05e860eea28244d78d31bfc059afe53ec
push id81598
push userbmo:apehrson@mozilla.com
push dateTue, 26 Sep 2017 09:13:19 +0000
reviewersjesup
bugs1296531
milestone58.0a1
Bug 1296531 - Add AudioGenerator to TestAudioTrackEncoder for simple generation of audio. r?jesup MozReview-Commit-ID: Cj9pyLZ3RR6
dom/media/gtest/TestAudioTrackEncoder.cpp
--- a/dom/media/gtest/TestAudioTrackEncoder.cpp
+++ b/dom/media/gtest/TestAudioTrackEncoder.cpp
@@ -1,18 +1,44 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 "gtest/gtest.h"
 #include "OpusTrackEncoder.h"
+#include "SineWaveGenerator.h"
 
 using namespace mozilla;
 
+class AudioGenerator
+{
+public:
+  AudioGenerator(int32_t aChannels, int32_t aSampleRate)
+    : mGenerator(aSampleRate, 1000)
+    , mChannels(aChannels)
+  {}
+
+  void Generate(AudioSegment& aSegment, const int32_t& aSamples)
+  {
+    RefPtr<SharedBuffer> buffer = SharedBuffer::Create(aSamples * sizeof(int16_t));
+    int16_t* dest = static_cast<int16_t*>(buffer->Data());
+    mGenerator.generate(dest, aSamples);
+    AutoTArray<const int16_t*, 1> channels;
+    for (int32_t i = 0; i < mChannels; i++) {
+      channels.AppendElement(dest);
+    }
+    aSegment.AppendFrames(buffer.forget(), channels, aSamples, PRINCIPAL_HANDLE_NONE);
+  }
+
+private:
+  SineWaveGenerator mGenerator;
+  const int32_t mChannels;
+};
+
 class TestOpusTrackEncoder : public OpusTrackEncoder
 {
 public:
   // Return true if it has successfully initialized the Opus encoder.
   bool TestOpusCreation(int aChannels, int aSamplingRate)
   {
     if (Init(aChannels, aSamplingRate) == NS_OK) {
       if (GetPacketDuration()) {