Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Side by Side Diff: content/renderer/media/webrtc_audio_processor_unittest.cc

Issue 54383003: Added an "enable-audio-processor" flag and WebRtcAudioProcessor class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed Per's comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/command_line.h"
6 #include "base/file_util.h"
7 #include "base/files/file_path.h"
8 #include "base/path_service.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/renderer/media/rtc_media_constraints.h"
11 #include "content/renderer/media/webrtc_audio_processor.h"
12 #include "media/audio/audio_parameters.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
17
18 using ::testing::_;
19 using ::testing::AnyNumber;
20 using ::testing::AtLeast;
21 using ::testing::Return;
22
23 namespace content {
24
25 namespace {
26
27 #if defined(ANDROID)
28 const int kAudioProcessingSampleRate = 16000;
29 #else
30 const int kAudioProcessingSampleRate = 32000;
31 #endif
32 const int kAudioProcessingNumberOfChannel = 1;
33
34 // The number of packers used for testing.
35 const int kNumberOfPacketsForTest = 100;
36
37 void ReadDataFromSpeechFile(char* data, int length) {
38 base::FilePath file;
39 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file));
40 file = file.Append(FILE_PATH_LITERAL("media"))
41 .Append(FILE_PATH_LITERAL("test"))
42 .Append(FILE_PATH_LITERAL("data"))
43 .Append(FILE_PATH_LITERAL("speech_16b_stereo_48kHz.raw"));
44 DCHECK(base::PathExists(file));
45 int64 data_file_size64 = 0;
46 DCHECK(file_util::GetFileSize(file, &data_file_size64));
47 EXPECT_EQ(length, file_util::ReadFile(file, data, length));
48 DCHECK(data_file_size64 > length);
49 }
50
51 // Constant constraint keys which enables default audio constraints on
52 // mediastreams with audio.
53 struct {
54 const char* key;
55 const char* value;
56 } const kDefaultAudioConstraints[] = {
57 { webrtc::MediaConstraintsInterface::kEchoCancellation,
58 webrtc::MediaConstraintsInterface::kValueTrue },
59 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
60 // Enable the extended filter mode AEC on platforms with known echo issues.
61 { webrtc::MediaConstraintsInterface::kExperimentalEchoCancellation,
62 webrtc::MediaConstraintsInterface::kValueTrue },
63 #endif
64 { webrtc::MediaConstraintsInterface::kAutoGainControl,
65 webrtc::MediaConstraintsInterface::kValueTrue },
66 { webrtc::MediaConstraintsInterface::kExperimentalAutoGainControl,
67 webrtc::MediaConstraintsInterface::kValueTrue },
68 { webrtc::MediaConstraintsInterface::kNoiseSuppression,
69 webrtc::MediaConstraintsInterface::kValueTrue },
70 { webrtc::MediaConstraintsInterface::kHighpassFilter,
71 webrtc::MediaConstraintsInterface::kValueTrue },
72 };
73
74 void ApplyFixedAudioConstraints(RTCMediaConstraints* constraints) {
75 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDefaultAudioConstraints); ++i) {
DaleCurtis 2013/11/06 00:21:18 just arraysize() should be fine here since it's a
no longer working on chromium 2013/11/06 16:45:14 The compiler complains about: no matching function
76 constraints->AddMandatory(kDefaultAudioConstraints[i].key,
77 kDefaultAudioConstraints[i].value, false);
78 }
79 }
80
81 } // namespace
82
83 class WebRtcAudioProcessorTest : public ::testing::Test {
84 protected:
85 virtual void SetUp() OVERRIDE {
DaleCurtis 2013/11/06 00:21:18 Why SetUp instead of constructor?
no longer working on chromium 2013/11/06 16:45:14 Fine with using the constructor.
86 CommandLine::ForCurrentProcess()->AppendSwitch(
DaleCurtis 2013/11/06 00:21:18 Is this the right way to do this? Will this affec
no longer working on chromium 2013/11/06 16:45:14 I am going to see if this will affect other conten
no longer working on chromium 2013/11/07 14:43:12 It proves to be the correct approach, and setting
87 switches::kEnableAudioTrackProcessing);
88 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
89 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 512);
90
91 }
92
93 media::AudioParameters params_;
94 };
95
96 TEST_F(WebRtcAudioProcessorTest, WithoutAudioProcessing) {
97 // Setup the audio processor with empty constraint.
98 RTCMediaConstraints constraints;
99 scoped_ptr<WebRtcAudioProcessor> audio_processor(
100 new WebRtcAudioProcessor(&constraints));
101 audio_processor->SetCaptureFormat(params_);
102 EXPECT_FALSE(audio_processor->has_audio_processing());
103
104 // Read the audio data from a file.
DaleCurtis 2013/11/06 00:21:18 There's a lot of common setup here, move to a clas
no longer working on chromium 2013/11/06 16:45:14 OK, I will address this comment in a later version
no longer working on chromium 2013/11/07 14:43:12 Done
105 const int packet_size = params_.frames_per_buffer() * 2 * params_.channels();
106 const size_t length = packet_size * kNumberOfPacketsForTest;
107 scoped_ptr<char[]> capture_data(new char[length]);
108 ReadDataFromSpeechFile(capture_data.get(), length);
109 const int16* data_ptr = reinterpret_cast<const int16*>(capture_data.get());
110 scoped_ptr<media::AudioBus> data_bus = media::AudioBus::Create(
111 params_.channels(), params_.frames_per_buffer());
112 for (int i = 0; i < kNumberOfPacketsForTest; ++i) {
113 data_bus->FromInterleaved(data_ptr, data_bus->frames(), 2);
114 audio_processor->PushCaptureData(data_bus.get());
115
116 // Feed data as render data to the processor, this does not cost anything
117 // when the audio processing is off in the processor.
118 audio_processor->FeedRenderDataToAudioProcessing(
DaleCurtis 2013/11/06 00:21:18 Not sure how this compiles??
no longer working on chromium 2013/11/06 16:45:14 Done.
119 data_ptr,
120 params_.sample_rate(), params_.channels(),
121 params_.frames_per_buffer(), 10);
122
123 // Process and consume the data in the processor.
DaleCurtis 2013/11/06 00:21:18 Ditto for all this.
no longer working on chromium 2013/11/07 14:43:12 Done.
124 int16* output = NULL;
125 while(audio_processor->ProcessAndConsume10MsData(10, 255, false, &output)) {
126 EXPECT_TRUE(output != NULL);
127 EXPECT_EQ(audio_processor->OutputFormat().sample_rate(),
128 params_.sample_rate());
129 EXPECT_EQ(audio_processor->OutputFormat().channels(),
130 params_.channels());
131 EXPECT_EQ(audio_processor->OutputFormat().frames_per_buffer(),
132 params_.sample_rate() / 100);
133 }
134
135 data_ptr += params_.frames_per_buffer() * params_.channels();
136 }
137 }
138
139 TEST_F(WebRtcAudioProcessorTest, WithAudioProcessing) {
140 // Setup the audio processor with default constraint.
141 RTCMediaConstraints constraints;
142 ApplyFixedAudioConstraints(&constraints);
143 scoped_ptr<WebRtcAudioProcessor> audio_processor(
144 new WebRtcAudioProcessor(&constraints));
145 audio_processor->SetCaptureFormat(params_);
146 EXPECT_TRUE(audio_processor->has_audio_processing());
147
148 // Read the audio data from a file.
149 const int packet_size = params_.frames_per_buffer() * 2 * params_.channels();
150 const size_t length = packet_size * kNumberOfPacketsForTest;
151 scoped_ptr<char[]> capture_data(new char[length]);
152 ReadDataFromSpeechFile(capture_data.get(), length);
153 const int16* data_ptr = reinterpret_cast<const int16*>(capture_data.get());
154 scoped_ptr<media::AudioBus> data_bus = media::AudioBus::Create(
155 params_.channels(), params_.frames_per_buffer());
156 for (int i = 0; i < kNumberOfPacketsForTest; ++i) {
157 data_bus->FromInterleaved(data_ptr, data_bus->frames(), 2);
158 audio_processor->PushCaptureData(data_bus.get());
159
160 // Feed data as render data to the processor.
161 audio_processor->FeedRenderDataToAudioProcessing(
162 data_ptr,
163 params_.sample_rate(), params_.channels(),
164 params_.frames_per_buffer(), 10);
165
166 // Process and consume the data in the processor.
167 int16* output = NULL;
168 while(audio_processor->ProcessAndConsume10MsData(10, 255, false, &output)) {
169 EXPECT_TRUE(output != NULL);
170 EXPECT_EQ(audio_processor->OutputFormat().sample_rate(),
171 kAudioProcessingSampleRate);
172 EXPECT_EQ(audio_processor->OutputFormat().channels(),
173 kAudioProcessingNumberOfChannel);
174 EXPECT_EQ(audio_processor->OutputFormat().frames_per_buffer(),
175 kAudioProcessingSampleRate / 100);
176 }
177
178 data_ptr += params_.frames_per_buffer() * params_.channels();
179 }
180 }
181
182 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698