OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/environment.h" | 5 #include "base/environment.h" |
6 #include "base/test/test_timeouts.h" | 6 #include "base/test/test_timeouts.h" |
7 #include "content/renderer/media/webrtc_audio_device_impl.h" | 7 #include "content/renderer/media/webrtc_audio_device_impl.h" |
8 #include "content/test/webrtc_audio_device_test.h" | 8 #include "content/test/webrtc_audio_device_test.h" |
9 #include "media/audio/audio_util.h" | 9 #include "media/audio/audio_util.h" |
10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "third_party/webrtc/voice_engine/main/interface/voe_audio_processing.h" | 11 #include "third_party/webrtc/voice_engine/main/interface/voe_audio_processing.h" |
12 #include "third_party/webrtc/voice_engine/main/interface/voe_base.h" | 12 #include "third_party/webrtc/voice_engine/main/interface/voe_base.h" |
13 #include "third_party/webrtc/voice_engine/main/interface/voe_external_media.h" | |
13 #include "third_party/webrtc/voice_engine/main/interface/voe_file.h" | 14 #include "third_party/webrtc/voice_engine/main/interface/voe_file.h" |
14 #include "third_party/webrtc/voice_engine/main/interface/voe_network.h" | 15 #include "third_party/webrtc/voice_engine/main/interface/voe_network.h" |
15 | 16 |
16 using testing::_; | 17 using testing::_; |
17 using testing::InvokeWithoutArgs; | 18 using testing::InvokeWithoutArgs; |
18 using testing::Return; | 19 using testing::Return; |
19 using testing::StrEq; | 20 using testing::StrEq; |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
(...skipping 11 matching lines...) Expand all Loading... | |
34 } | 35 } |
35 }; | 36 }; |
36 | 37 |
37 bool IsRunningHeadless() { | 38 bool IsRunningHeadless() { |
38 scoped_ptr<base::Environment> env(base::Environment::Create()); | 39 scoped_ptr<base::Environment> env(base::Environment::Create()); |
39 if (env->HasVar("CHROME_HEADLESS")) | 40 if (env->HasVar("CHROME_HEADLESS")) |
40 return true; | 41 return true; |
41 return false; | 42 return false; |
42 } | 43 } |
43 | 44 |
45 class WebRTCMediaProcessImpl : public webrtc::VoEMediaProcess { | |
46 public: | |
47 explicit WebRTCMediaProcessImpl(base::WaitableEvent* event) | |
48 : event_(event), | |
49 channel_id_(-1), | |
50 type_(webrtc::kPlaybackPerChannel), | |
51 packet_size_(0), | |
52 sample_rate_(0), | |
53 channels_(0) {} | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
} on the next line since this is not a one liner
henrika (OOO until Aug 14)
2011/11/15 09:27:39
Done.
| |
54 virtual ~WebRTCMediaProcessImpl() {} | |
55 | |
56 // TODO(henrika): Refactor in WebRTC and convert to Chrome coding style. | |
57 virtual void Process(const int channel, | |
58 const webrtc::ProcessingTypes type, | |
59 WebRtc_Word16 audio_10ms[], | |
60 const int length, | |
61 const int sampling_freq, | |
62 const bool is_stereo) { | |
63 channel_id_ = channel; | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
too much indentation?
tommi (sloooow) - chröme
2011/11/14 17:25:35
Is it worth it doing something like:
if (channel_i
henrika (OOO until Aug 14)
2011/11/15 09:27:39
Done.
henrika (OOO until Aug 14)
2011/11/15 09:27:39
No. Because -1 is valid and means "all channels".
| |
64 type_ = type; | |
65 packet_size_ = length; | |
66 sample_rate_ = sampling_freq; | |
67 channels_ = (is_stereo ? 2 : 1); | |
68 if (event_) { | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
EXPECT_TRUE(event_)
| |
69 // Signal that a new callback has been received. | |
70 event_->Signal(); | |
71 } | |
72 } | |
73 | |
74 int channel_id() const { return channel_id_; } | |
75 int type() const { return type_; } | |
76 int packet_size() const { return packet_size_; } | |
77 int sample_rate() const { return sample_rate_; } | |
78 int channels() const { return channels_; } | |
79 | |
80 private: | |
81 base::WaitableEvent* event_; | |
82 int channel_id_; | |
83 webrtc::ProcessingTypes type_; | |
84 int packet_size_; | |
85 int sample_rate_; | |
86 int channels_; | |
87 DISALLOW_COPY_AND_ASSIGN(WebRTCMediaProcessImpl); | |
88 }; | |
89 | |
44 } // end namespace | 90 } // end namespace |
45 | 91 |
46 // Basic test that instantiates and initializes an instance of | 92 // Basic test that instantiates and initializes an instance of |
47 // WebRtcAudioDeviceImpl. | 93 // WebRtcAudioDeviceImpl. |
48 TEST_F(WebRTCAudioDeviceTest, Construct) { | 94 TEST_F(WebRTCAudioDeviceTest, Construct) { |
49 AudioUtil audio_util; | 95 AudioUtil audio_util; |
50 set_audio_util_callback(&audio_util); | 96 set_audio_util_callback(&audio_util); |
51 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( | 97 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( |
52 new WebRtcAudioDeviceImpl()); | 98 new WebRtcAudioDeviceImpl()); |
53 audio_device->SetSessionId(1); | 99 audio_device->SetSessionId(1); |
54 | 100 |
55 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | 101 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); |
56 ASSERT_TRUE(engine.valid()); | 102 ASSERT_TRUE(engine.valid()); |
57 | 103 |
58 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); | 104 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); |
59 int err = base->Init(audio_device); | 105 int err = base->Init(audio_device); |
60 EXPECT_EQ(0, err); | 106 EXPECT_EQ(0, err); |
61 EXPECT_EQ(0, base->Terminate()); | 107 EXPECT_EQ(0, base->Terminate()); |
62 } | 108 } |
63 | 109 |
110 // Verify that a call to webrtc::VoEBase::StartPlayout() starts audio output | |
111 // with the correct set of parameters. A WebRtcAudioDeviceImpl instance will | |
112 // be utilized to implement the actual audio path. The test registers a | |
113 // webrtc::VoEExternalMedia implementation to hijack the output audio and | |
114 // verify that streaming starts correctly. | |
115 // Disabled when running headless since the bots don't have the required config. | |
116 TEST_F(WebRTCAudioDeviceTest, StartPlayout) { | |
117 if (IsRunningHeadless()) | |
118 return; | |
119 | |
120 AudioUtil audio_util; | |
121 set_audio_util_callback(&audio_util); | |
122 | |
123 EXPECT_CALL(media_observer(), | |
124 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
fix indent on these lines
henrika (OOO until Aug 14)
2011/11/15 09:27:39
Done.
| |
125 EXPECT_CALL(media_observer(), | |
126 OnSetAudioStreamPlaying(_, 1, true)).Times(1); | |
127 EXPECT_CALL(media_observer(), | |
128 OnSetAudioStreamStatus(_, 1, StrEq("closed"))).Times(1); | |
129 EXPECT_CALL(media_observer(), | |
130 OnDeleteAudioStream(_, 1)).Times(1); | |
131 | |
132 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( | |
133 new WebRtcAudioDeviceImpl()); | |
134 audio_device->SetSessionId(1); | |
135 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | |
136 ASSERT_TRUE(engine.valid()); | |
137 | |
138 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); | |
139 ASSERT_TRUE(base.valid()); | |
140 int err = base->Init(audio_device); | |
141 ASSERT_EQ(0, err); | |
142 | |
143 int ch = base->CreateChannel(); | |
144 EXPECT_NE(-1, ch); | |
145 | |
146 ScopedWebRTCPtr<webrtc::VoEExternalMedia> external_media(engine.get()); | |
147 ASSERT_TRUE(external_media.valid()); | |
148 | |
149 base::WaitableEvent event(false, false); | |
150 scoped_ptr<WebRTCMediaProcessImpl> media_process( | |
151 new WebRTCMediaProcessImpl(&event)); | |
152 EXPECT_EQ(0, external_media->RegisterExternalMediaProcessing( | |
153 ch, webrtc::kPlaybackPerChannel, *media_process.get())); | |
154 | |
155 EXPECT_EQ(0, base->StartPlayout(ch)); | |
156 | |
157 EXPECT_TRUE(event.TimedWait( | |
158 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); | |
159 WaitForIOThreadCompletion(); | |
160 | |
161 EXPECT_TRUE(audio_device->playing()); | |
162 EXPECT_FALSE(audio_device->recording()); | |
163 EXPECT_EQ(ch, media_process->channel_id()); | |
164 EXPECT_EQ(webrtc::kPlaybackPerChannel, media_process->type()); | |
165 EXPECT_EQ(80, media_process->packet_size()); | |
166 EXPECT_EQ(8000, media_process->sample_rate()); | |
167 | |
168 EXPECT_EQ(0, external_media->DeRegisterExternalMediaProcessing( | |
169 ch, webrtc::kPlaybackPerChannel)); | |
170 EXPECT_EQ(0, base->StopPlayout(ch)); | |
171 | |
172 EXPECT_EQ(0, base->DeleteChannel(ch)); | |
173 EXPECT_EQ(0, base->Terminate()); | |
174 } | |
175 | |
176 // Verify that a call to webrtc::VoEBase::StartRecording() starts audio input | |
177 // with the correct set of parameters. A WebRtcAudioDeviceImpl instance will | |
178 // be utilized to implement the actual audio path. The test registers a | |
179 // webrtc::VoEExternalMedia implementation to hijack the input audio and | |
180 // verify that streaming starts correctly. An external transport implementation | |
181 // is also required to ensure that "sending" can start without actually trying | |
182 // to send encoded packets to the network. Our main interest here is to ensure | |
183 // that the audio capturing starts as it should. | |
184 // Disabled when running headless since the bots don't have the required config. | |
185 TEST_F(WebRTCAudioDeviceTest, StartRecording) { | |
186 if (IsRunningHeadless()) | |
187 return; | |
188 | |
189 AudioUtil audio_util; | |
190 set_audio_util_callback(&audio_util); | |
191 | |
192 // TODO(tommi): extend MediaObserver and MockMediaObserver with support | |
193 // for new interfaces, like OnSetAudioStreamRecording(). When done, add | |
194 // EXPECT_CALL() macros here. | |
195 | |
196 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( | |
197 new WebRtcAudioDeviceImpl()); | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
indent
henrika (OOO until Aug 14)
2011/11/15 09:27:39
Done.
| |
198 audio_device->SetSessionId(1); | |
199 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | |
200 ASSERT_TRUE(engine.valid()); | |
201 | |
202 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); | |
203 ASSERT_TRUE(base.valid()); | |
204 int err = base->Init(audio_device); | |
205 ASSERT_EQ(0, err); | |
206 | |
207 int ch = base->CreateChannel(); | |
208 EXPECT_NE(-1, ch); | |
209 | |
210 ScopedWebRTCPtr<webrtc::VoEExternalMedia> external_media(engine.get()); | |
211 ASSERT_TRUE(external_media.valid()); | |
212 | |
213 base::WaitableEvent event(false, false); | |
214 scoped_ptr<WebRTCMediaProcessImpl> media_process( | |
215 new WebRTCMediaProcessImpl(&event)); | |
tommi (sloooow) - chröme
2011/11/14 17:25:35
ditto. and below
henrika (OOO until Aug 14)
2011/11/15 09:27:39
Done.
| |
216 EXPECT_EQ(0, external_media->RegisterExternalMediaProcessing( | |
217 ch, webrtc::kRecordingPerChannel, *media_process.get())); | |
218 | |
219 // We must add an external transport implementation to be able to start | |
220 // recording without actually sending encoded packets to the network. All | |
221 // we want to do here is to verify that audio capturing starts as it should. | |
222 ScopedWebRTCPtr<webrtc::VoENetwork> network(engine.get()); | |
223 scoped_ptr<WebRTCTransportImpl> transport( | |
224 new WebRTCTransportImpl(network.get())); | |
225 EXPECT_EQ(0, network->RegisterExternalTransport(ch, *transport.get())); | |
226 EXPECT_EQ(0, base->StartSend(ch)); | |
227 | |
228 EXPECT_TRUE(event.TimedWait( | |
229 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); | |
230 WaitForIOThreadCompletion(); | |
231 | |
232 EXPECT_FALSE(audio_device->playing()); | |
233 EXPECT_TRUE(audio_device->recording()); | |
234 EXPECT_EQ(ch, media_process->channel_id()); | |
235 EXPECT_EQ(webrtc::kRecordingPerChannel, media_process->type()); | |
236 EXPECT_EQ(80, media_process->packet_size()); | |
237 EXPECT_EQ(8000, media_process->sample_rate()); | |
238 | |
239 EXPECT_EQ(0, external_media->DeRegisterExternalMediaProcessing( | |
240 ch, webrtc::kRecordingPerChannel)); | |
241 EXPECT_EQ(0, base->StopSend(ch)); | |
242 | |
243 EXPECT_EQ(0, base->DeleteChannel(ch)); | |
244 EXPECT_EQ(0, base->Terminate()); | |
245 } | |
246 | |
64 // Uses WebRtcAudioDeviceImpl to play a local wave file. | 247 // Uses WebRtcAudioDeviceImpl to play a local wave file. |
65 // Disabled when running headless since the bots don't have the required config. | 248 // Disabled when running headless since the bots don't have the required config. |
66 TEST_F(WebRTCAudioDeviceTest, PlayLocalFile) { | 249 TEST_F(WebRTCAudioDeviceTest, DISABLED_PlayLocalFile) { |
tommi (sloooow) - chröme
2011/11/14 17:25:35
any reason why?
henrika (OOO until Aug 14)
2011/11/15 09:27:39
If you think it is OK to play a file for 10 second
| |
67 if (IsRunningHeadless()) | 250 if (IsRunningHeadless()) |
68 return; | 251 return; |
69 | 252 |
70 std::string file_path( | 253 std::string file_path( |
71 GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); | 254 GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); |
72 | 255 |
73 AudioUtil audio_util; | 256 AudioUtil audio_util; |
74 set_audio_util_callback(&audio_util); | 257 set_audio_util_callback(&audio_util); |
75 | 258 |
76 EXPECT_CALL(media_observer(), | 259 EXPECT_CALL(media_observer(), |
(...skipping 30 matching lines...) Expand all Loading... | |
107 EXPECT_EQ(0, file->StartPlayingFileLocally(ch, file_path.c_str(), false, | 290 EXPECT_EQ(0, file->StartPlayingFileLocally(ch, file_path.c_str(), false, |
108 webrtc::kFileFormatPcm16kHzFile)); | 291 webrtc::kFileFormatPcm16kHzFile)); |
109 | 292 |
110 message_loop_.PostDelayedTask(FROM_HERE, | 293 message_loop_.PostDelayedTask(FROM_HERE, |
111 new MessageLoop::QuitTask(), | 294 new MessageLoop::QuitTask(), |
112 TestTimeouts::action_timeout_ms()); | 295 TestTimeouts::action_timeout_ms()); |
113 message_loop_.Run(); | 296 message_loop_.Run(); |
114 | 297 |
115 EXPECT_EQ(0, base->Terminate()); | 298 EXPECT_EQ(0, base->Terminate()); |
116 } | 299 } |
300 | |
301 // Uses WebRtcAudioDeviceImpl to play out recorded audio in loopback. | |
302 // An external transport implementation is utilized to feed back RTP packets | |
303 // which are recorded, encoded, packetized into RTP packets and finally | |
304 // "transmitted". The RTP packets are then fed back into the VoiceEngine | |
305 // where they are decoded and played out on the default audio output device. | |
306 // Disabled when running headless since the bots don't have the required config. | |
307 // TODO(henrika): improve quality by using a wideband codec, enabling noise- | |
308 // suppressions and perhaps also the digital AGC. | |
309 TEST_F(WebRTCAudioDeviceTest, DISABLED_FullDuplexAudio) { | |
310 if (IsRunningHeadless()) | |
311 return; | |
312 | |
313 AudioUtil audio_util; | |
314 set_audio_util_callback(&audio_util); | |
315 | |
316 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( | |
317 new WebRtcAudioDeviceImpl()); | |
318 audio_device->SetSessionId(1); | |
319 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | |
320 ASSERT_TRUE(engine.valid()); | |
321 | |
322 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); | |
323 ASSERT_TRUE(base.valid()); | |
324 int err = base->Init(audio_device); | |
325 ASSERT_EQ(0, err); | |
326 | |
327 int ch = base->CreateChannel(); | |
328 EXPECT_NE(-1, ch); | |
329 | |
330 ScopedWebRTCPtr<webrtc::VoENetwork> network(engine.get()); | |
331 scoped_ptr<WebRTCTransportImpl> transport( | |
332 new WebRTCTransportImpl(network.get())); | |
333 EXPECT_EQ(0, network->RegisterExternalTransport(ch, *transport.get())); | |
334 EXPECT_EQ(0, base->StartPlayout(ch)); | |
335 EXPECT_EQ(0, base->StartSend(ch)); | |
336 | |
337 LOG(INFO) << ">> You should now be able to hear yourself in loopback..."; | |
338 message_loop_.PostDelayedTask(FROM_HERE, | |
339 new MessageLoop::QuitTask(), | |
340 TestTimeouts::action_timeout_ms()); | |
341 message_loop_.Run(); | |
342 | |
343 EXPECT_EQ(0, base->StopSend(ch)); | |
344 EXPECT_EQ(0, base->StopPlayout(ch)); | |
345 | |
346 EXPECT_EQ(0, base->DeleteChannel(ch)); | |
347 EXPECT_EQ(0, base->Terminate()); | |
348 } | |
OLD | NEW |