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

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: changed to base::TimeDelta and addressed Joi'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 "base/time/time.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/renderer/media/rtc_media_constraints.h"
12 #include "content/renderer/media/webrtc_audio_processor.h"
13 #include "media/audio/audio_parameters.h"
14 #include "media/base/audio_bus.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
18
19 using ::testing::_;
20 using ::testing::AnyNumber;
21 using ::testing::AtLeast;
22 using ::testing::Return;
23
24 namespace content {
25
26 namespace {
27
28 #if defined(ANDROID)
29 const int kAudioProcessingSampleRate = 16000;
30 #else
31 const int kAudioProcessingSampleRate = 32000;
32 #endif
33 const int kAudioProcessingNumberOfChannel = 1;
34
35 // The number of packers used for testing.
36 const int kNumberOfPacketsForTest = 100;
37
38 void ReadDataFromSpeechFile(char* data, int length) {
39 base::FilePath file;
40 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file));
41 file = file.Append(FILE_PATH_LITERAL("media"))
42 .Append(FILE_PATH_LITERAL("test"))
43 .Append(FILE_PATH_LITERAL("data"))
44 .Append(FILE_PATH_LITERAL("speech_16b_stereo_48kHz.raw"));
45 DCHECK(base::PathExists(file));
46 int64 data_file_size64 = 0;
47 DCHECK(file_util::GetFileSize(file, &data_file_size64));
48 EXPECT_EQ(length, file_util::ReadFile(file, data, length));
49 DCHECK(data_file_size64 > length);
50 }
51
52 // Constant constraint keys which enables default audio constraints on
53 // mediastreams with audio.
54 struct {
55 const char* key;
56 const char* value;
57 } const kDefaultAudioConstraints[] = {
58 { webrtc::MediaConstraintsInterface::kEchoCancellation,
59 webrtc::MediaConstraintsInterface::kValueTrue },
60 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
61 // Enable the extended filter mode AEC on platforms with known echo issues.
62 { webrtc::MediaConstraintsInterface::kExperimentalEchoCancellation,
63 webrtc::MediaConstraintsInterface::kValueTrue },
64 #endif
65 { webrtc::MediaConstraintsInterface::kAutoGainControl,
66 webrtc::MediaConstraintsInterface::kValueTrue },
67 { webrtc::MediaConstraintsInterface::kExperimentalAutoGainControl,
68 webrtc::MediaConstraintsInterface::kValueTrue },
69 { webrtc::MediaConstraintsInterface::kNoiseSuppression,
70 webrtc::MediaConstraintsInterface::kValueTrue },
71 { webrtc::MediaConstraintsInterface::kHighpassFilter,
72 webrtc::MediaConstraintsInterface::kValueTrue },
73 };
74
75 void ApplyFixedAudioConstraints(RTCMediaConstraints* constraints) {
76 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDefaultAudioConstraints); ++i) {
77 constraints->AddMandatory(kDefaultAudioConstraints[i].key,
78 kDefaultAudioConstraints[i].value, false);
79 }
80 }
81
82 } // namespace
83
84 class WebRtcAudioProcessorTest : public ::testing::Test {
85 public:
86 WebRtcAudioProcessorTest()
87 : params_(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
88 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 512) {
89 CommandLine::ForCurrentProcess()->AppendSwitch(
90 switches::kEnableAudioTrackProcessing);
91 }
92
93 protected:
94 // Helper method to save duplicated code.
95 void ProcessDataAndVerifyFormat(WebRtcAudioProcessor* audio_processor,
96 int expected_output_sample_rate,
97 int expected_output_channels,
98 int expected_output_buffer_size) {
99 // Read the audio data from a file.
100 const int packet_size =
101 params_.frames_per_buffer() * 2 * params_.channels();
102 const size_t length = packet_size * kNumberOfPacketsForTest;
103 scoped_ptr<char[]> capture_data(new char[length]);
104 ReadDataFromSpeechFile(capture_data.get(), length);
105 const int16* data_ptr = reinterpret_cast<const int16*>(capture_data.get());
106 scoped_ptr<media::AudioBus> data_bus = media::AudioBus::Create(
107 params_.channels(), params_.frames_per_buffer());
108 for (int i = 0; i < kNumberOfPacketsForTest; ++i) {
109 data_bus->FromInterleaved(data_ptr, data_bus->frames(), 2);
110 audio_processor->PushCaptureData(data_bus.get());
111
112 // Feed data as render data to the processor, this does not cost anything
113 // when the audio processing is off in the processor.
114 audio_processor->PushRenderData(
115 data_ptr,
116 params_.sample_rate(), params_.channels(),
117 params_.frames_per_buffer(), base::TimeDelta::FromMilliseconds(10));
118
119 // Process and consume the data in the processor.
120 int16* output = NULL;
121 while(audio_processor->ProcessAndConsumeData(
122 base::TimeDelta::FromMilliseconds(10), 255, false, &output)) {
123 EXPECT_TRUE(output != NULL);
124 EXPECT_EQ(audio_processor->OutputFormat().sample_rate(),
125 expected_output_sample_rate);
126 EXPECT_EQ(audio_processor->OutputFormat().channels(),
127 expected_output_channels);
128 EXPECT_EQ(audio_processor->OutputFormat().frames_per_buffer(),
129 expected_output_buffer_size);
130 }
131
132 data_ptr += params_.frames_per_buffer() * params_.channels();
133 }
134 }
135
136 media::AudioParameters params_;
137 };
138
139 TEST_F(WebRtcAudioProcessorTest, WithoutAudioProcessing) {
140 // Setup the audio processor with empty constraint.
141 RTCMediaConstraints constraints;
142 WebRtcAudioProcessor audio_processor(&constraints);
143 audio_processor.SetCaptureFormat(params_);
144 EXPECT_FALSE(audio_processor.has_audio_processing());
145
146 ProcessDataAndVerifyFormat(&audio_processor,
147 params_.sample_rate(),
148 params_.channels(),
149 params_.sample_rate() / 100);
150 }
151
152 TEST_F(WebRtcAudioProcessorTest, WithAudioProcessing) {
153 // Setup the audio processor with default constraint.
154 RTCMediaConstraints constraints;
155 ApplyFixedAudioConstraints(&constraints);
156 WebRtcAudioProcessor audio_processor(&constraints);
157 audio_processor.SetCaptureFormat(params_);
158 EXPECT_TRUE(audio_processor.has_audio_processing());
159
160 ProcessDataAndVerifyFormat(&audio_processor,
161 kAudioProcessingSampleRate,
162 kAudioProcessingNumberOfChannel,
163 kAudioProcessingSampleRate / 100);
164 }
165
166 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698