OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "content/test/webrtc_audio_device_test.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/file_util.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/synchronization/waitable_event.h" | |
13 #include "base/test/test_timeouts.h" | |
14 #include "content/browser/media/capture/audio_mirroring_manager.h" | |
15 #include "content/browser/media/media_internals.h" | |
16 #include "content/browser/renderer_host/media/audio_input_renderer_host.h" | |
17 #include "content/browser/renderer_host/media/audio_renderer_host.h" | |
18 #include "content/browser/renderer_host/media/media_stream_manager.h" | |
19 #include "content/browser/renderer_host/media/mock_media_observer.h" | |
20 #include "content/common/media/media_param_traits.h" | |
21 #include "content/common/view_messages.h" | |
22 #include "content/public/browser/browser_thread.h" | |
23 #include "content/public/browser/resource_context.h" | |
24 #include "content/public/common/content_paths.h" | |
25 #include "content/public/test/test_browser_thread.h" | |
26 #include "content/renderer/media/audio_input_message_filter.h" | |
27 #include "content/renderer/media/audio_message_filter.h" | |
28 #include "content/renderer/media/webrtc_audio_device_impl.h" | |
29 #include "content/renderer/render_process.h" | |
30 #include "content/renderer/render_thread_impl.h" | |
31 #include "content/renderer/renderer_webkitplatformsupport_impl.h" | |
32 #include "media/audio/audio_parameters.h" | |
33 #include "media/base/audio_hardware_config.h" | |
34 #include "net/url_request/url_request_test_util.h" | |
35 #include "testing/gmock/include/gmock/gmock.h" | |
36 #include "testing/gtest/include/gtest/gtest.h" | |
37 #include "third_party/webrtc/voice_engine/include/voe_audio_processing.h" | |
38 #include "third_party/webrtc/voice_engine/include/voe_base.h" | |
39 #include "third_party/webrtc/voice_engine/include/voe_file.h" | |
40 #include "third_party/webrtc/voice_engine/include/voe_network.h" | |
41 | |
42 #if defined(OS_WIN) | |
43 #include "base/win/scoped_com_initializer.h" | |
44 #endif | |
45 | |
46 using media::AudioParameters; | |
47 using media::ChannelLayout; | |
48 using testing::_; | |
49 using testing::InvokeWithoutArgs; | |
50 using testing::Return; | |
51 using testing::StrEq; | |
52 | |
53 namespace content { | |
54 | |
55 // This class is a mock of the child process singleton which is needed | |
56 // to be able to create a RenderThread object. | |
57 class WebRTCMockRenderProcess : public RenderProcess { | |
58 public: | |
59 WebRTCMockRenderProcess() {} | |
60 virtual ~WebRTCMockRenderProcess() {} | |
61 | |
62 // RenderProcess implementation. | |
63 virtual skia::PlatformCanvas* GetDrawingCanvas( | |
64 TransportDIB** memory, const gfx::Rect& rect) OVERRIDE { | |
65 return NULL; | |
66 } | |
67 virtual void ReleaseTransportDIB(TransportDIB* memory) OVERRIDE {} | |
68 virtual void AddBindings(int bindings) OVERRIDE {} | |
69 virtual int GetEnabledBindings() const OVERRIDE { return 0; } | |
70 virtual TransportDIB* CreateTransportDIB(size_t size) OVERRIDE { | |
71 return NULL; | |
72 } | |
73 virtual void FreeTransportDIB(TransportDIB*) OVERRIDE {} | |
74 | |
75 private: | |
76 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess); | |
77 }; | |
78 | |
79 class TestAudioRendererHost : public AudioRendererHost { | |
80 public: | |
81 TestAudioRendererHost( | |
82 int render_process_id, | |
83 media::AudioManager* audio_manager, | |
84 AudioMirroringManager* mirroring_manager, | |
85 MediaInternals* media_internals, | |
86 MediaStreamManager* media_stream_manager, | |
87 IPC::Channel* channel) | |
88 : AudioRendererHost(render_process_id, audio_manager, mirroring_manager, | |
89 media_internals, media_stream_manager), | |
90 channel_(channel) {} | |
91 virtual bool Send(IPC::Message* message) OVERRIDE { | |
92 if (channel_) | |
93 return channel_->Send(message); | |
94 return false; | |
95 } | |
96 void ResetChannel() { | |
97 channel_ = NULL; | |
98 } | |
99 | |
100 protected: | |
101 virtual ~TestAudioRendererHost() {} | |
102 | |
103 private: | |
104 IPC::Channel* channel_; | |
105 }; | |
106 | |
107 class TestAudioInputRendererHost : public AudioInputRendererHost { | |
108 public: | |
109 TestAudioInputRendererHost( | |
110 media::AudioManager* audio_manager, | |
111 MediaStreamManager* media_stream_manager, | |
112 AudioMirroringManager* audio_mirroring_manager, | |
113 media::UserInputMonitor* user_input_monitor, | |
114 IPC::Channel* channel) | |
115 : AudioInputRendererHost(audio_manager, media_stream_manager, | |
116 audio_mirroring_manager, user_input_monitor), | |
117 channel_(channel) {} | |
118 virtual bool Send(IPC::Message* message) OVERRIDE { | |
119 if (channel_) | |
120 return channel_->Send(message); | |
121 return false; | |
122 } | |
123 void ResetChannel() { | |
124 channel_ = NULL; | |
125 } | |
126 | |
127 protected: | |
128 virtual ~TestAudioInputRendererHost() {} | |
129 | |
130 private: | |
131 IPC::Channel* channel_; | |
132 }; | |
133 | |
134 // Utility scoped class to replace the global content client's renderer for the | |
135 // duration of the test. | |
136 class ReplaceContentClientRenderer { | |
137 public: | |
138 explicit ReplaceContentClientRenderer(ContentRendererClient* new_renderer) { | |
139 saved_renderer_ = SetRendererClientForTesting(new_renderer); | |
140 } | |
141 ~ReplaceContentClientRenderer() { | |
142 // Restore the original renderer. | |
143 SetRendererClientForTesting(saved_renderer_); | |
144 } | |
145 private: | |
146 ContentRendererClient* saved_renderer_; | |
147 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer); | |
148 }; | |
149 | |
150 class MockRTCResourceContext : public ResourceContext { | |
151 public: | |
152 MockRTCResourceContext() : test_request_context_(NULL) {} | |
153 virtual ~MockRTCResourceContext() {} | |
154 | |
155 void set_request_context(net::URLRequestContext* request_context) { | |
156 test_request_context_ = request_context; | |
157 } | |
158 | |
159 // ResourceContext implementation: | |
160 virtual net::HostResolver* GetHostResolver() OVERRIDE { | |
161 return NULL; | |
162 } | |
163 virtual net::URLRequestContext* GetRequestContext() OVERRIDE { | |
164 return test_request_context_; | |
165 } | |
166 | |
167 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE { | |
168 return false; | |
169 } | |
170 | |
171 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE { | |
172 return false; | |
173 } | |
174 | |
175 private: | |
176 net::URLRequestContext* test_request_context_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(MockRTCResourceContext); | |
179 }; | |
180 | |
181 ACTION_P(QuitMessageLoop, loop_or_proxy) { | |
182 loop_or_proxy->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
183 } | |
184 | |
185 MAYBE_WebRTCAudioDeviceTest::MAYBE_WebRTCAudioDeviceTest() | |
186 : render_thread_(NULL), audio_hardware_config_(NULL), | |
187 has_input_devices_(false), has_output_devices_(false) { | |
188 } | |
189 | |
190 MAYBE_WebRTCAudioDeviceTest::~MAYBE_WebRTCAudioDeviceTest() {} | |
191 | |
192 void MAYBE_WebRTCAudioDeviceTest::SetUp() { | |
193 // This part sets up a RenderThread environment to ensure that | |
194 // RenderThread::current() (<=> TLS pointer) is valid. | |
195 // Main parts are inspired by the RenderViewFakeResourcesTest. | |
196 // Note that, the IPC part is not utilized in this test. | |
197 saved_content_renderer_.reset( | |
198 new ReplaceContentClientRenderer(&content_renderer_client_)); | |
199 mock_process_.reset(new WebRTCMockRenderProcess()); | |
200 ui_thread_.reset( | |
201 new TestBrowserThread(BrowserThread::UI, base::MessageLoop::current())); | |
202 | |
203 // Construct the resource context on the UI thread. | |
204 resource_context_.reset(new MockRTCResourceContext); | |
205 | |
206 static const char kThreadName[] = "RenderThread"; | |
207 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE, | |
208 base::Bind(&MAYBE_WebRTCAudioDeviceTest::InitializeIOThread, | |
209 base::Unretained(this), kThreadName)); | |
210 WaitForIOThreadCompletion(); | |
211 | |
212 sandbox_was_enabled_ = | |
213 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(false); | |
214 // TODO(tommi): RenderThreadImpl no longer supports being instantiated in | |
215 // tests like this. Right now it initializes DiscardableMemory which can | |
216 // only be initialized once. Since all WebRTCAudioDeviceTest have been | |
217 // disabled, they are starting to bit rot :-( | |
218 // We should use a mocked render thread. | |
219 render_thread_ = new RenderThreadImpl(kThreadName); | |
220 } | |
221 | |
222 void MAYBE_WebRTCAudioDeviceTest::TearDown() { | |
223 SetAudioHardwareConfig(NULL); | |
224 | |
225 // Run any pending cleanup tasks that may have been posted to the main thread. | |
226 base::RunLoop().RunUntilIdle(); | |
227 | |
228 // Kick of the cleanup process by closing the channel. This queues up | |
229 // OnStreamClosed calls to be executed on the audio thread. | |
230 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE, | |
231 base::Bind(&MAYBE_WebRTCAudioDeviceTest::DestroyChannel, | |
232 base::Unretained(this))); | |
233 WaitForIOThreadCompletion(); | |
234 | |
235 // When audio [input] render hosts are notified that the channel has | |
236 // been closed, they post tasks to the audio thread to close the | |
237 // AudioOutputController and once that's completed, a task is posted back to | |
238 // the IO thread to actually delete the AudioEntry for the audio stream. Only | |
239 // then is the reference to the audio manager released, so we wait for the | |
240 // whole thing to be torn down before we finally uninitialize the io thread. | |
241 WaitForAudioManagerCompletion(); | |
242 | |
243 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE, | |
244 base::Bind(&MAYBE_WebRTCAudioDeviceTest::UninitializeIOThread, | |
245 base::Unretained((this)))); | |
246 WaitForIOThreadCompletion(); | |
247 mock_process_.reset(); | |
248 media_stream_manager_.reset(); | |
249 mirroring_manager_.reset(); | |
250 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting( | |
251 sandbox_was_enabled_); | |
252 } | |
253 | |
254 bool MAYBE_WebRTCAudioDeviceTest::Send(IPC::Message* message) { | |
255 return channel_->Send(message); | |
256 } | |
257 | |
258 void MAYBE_WebRTCAudioDeviceTest::SetAudioHardwareConfig( | |
259 media::AudioHardwareConfig* hardware_config) { | |
260 audio_hardware_config_ = hardware_config; | |
261 } | |
262 | |
263 scoped_refptr<WebRtcAudioRenderer> | |
264 MAYBE_WebRTCAudioDeviceTest::CreateDefaultWebRtcAudioRenderer( | |
265 int render_view_id, | |
266 const scoped_refptr<webrtc::MediaStreamInterface>& media_stream) { | |
267 media::AudioHardwareConfig* hardware_config = | |
268 RenderThreadImpl::current()->GetAudioHardwareConfig(); | |
269 int sample_rate = hardware_config->GetOutputSampleRate(); | |
270 int frames_per_buffer = hardware_config->GetOutputBufferSize(); | |
271 return new WebRtcAudioRenderer(media_stream, render_view_id, MSG_ROUTING_NONE, | |
272 0, sample_rate, frames_per_buffer); | |
273 } | |
274 | |
275 void MAYBE_WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) { | |
276 #if defined(OS_WIN) | |
277 // We initialize COM (STA) on our IO thread as is done in Chrome. | |
278 // See BrowserProcessSubThread::Init. | |
279 initialize_com_.reset(new base::win::ScopedCOMInitializer()); | |
280 #endif | |
281 | |
282 // Set the current thread as the IO thread. | |
283 io_thread_.reset( | |
284 new TestBrowserThread(BrowserThread::IO, base::MessageLoop::current())); | |
285 | |
286 // Populate our resource context. | |
287 test_request_context_.reset(new net::TestURLRequestContext()); | |
288 MockRTCResourceContext* resource_context = | |
289 static_cast<MockRTCResourceContext*>(resource_context_.get()); | |
290 resource_context->set_request_context(test_request_context_.get()); | |
291 | |
292 // Create our own AudioManager, AudioMirroringManager and MediaStreamManager. | |
293 audio_manager_.reset(media::AudioManager::CreateForTesting()); | |
294 mirroring_manager_.reset(new AudioMirroringManager()); | |
295 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | |
296 | |
297 has_input_devices_ = audio_manager_->HasAudioInputDevices(); | |
298 has_output_devices_ = audio_manager_->HasAudioOutputDevices(); | |
299 | |
300 // Create an IPC channel that handles incoming messages on the IO thread. | |
301 CreateChannel(thread_name); | |
302 } | |
303 | |
304 void MAYBE_WebRTCAudioDeviceTest::UninitializeIOThread() { | |
305 resource_context_.reset(); | |
306 | |
307 test_request_context_.reset(); | |
308 | |
309 #if defined(OS_WIN) | |
310 initialize_com_.reset(); | |
311 #endif | |
312 | |
313 audio_manager_.reset(); | |
314 } | |
315 | |
316 void MAYBE_WebRTCAudioDeviceTest::CreateChannel(const char* name) { | |
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
318 | |
319 channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this)); | |
320 ASSERT_TRUE(channel_->Connect()); | |
321 | |
322 static const int kRenderProcessId = 1; | |
323 audio_render_host_ = new TestAudioRendererHost(kRenderProcessId, | |
324 audio_manager_.get(), | |
325 mirroring_manager_.get(), | |
326 MediaInternals::GetInstance(), | |
327 media_stream_manager_.get(), | |
328 channel_.get()); | |
329 audio_render_host_->set_peer_pid_for_testing(base::GetCurrentProcId()); | |
330 | |
331 audio_input_renderer_host_ = | |
332 new TestAudioInputRendererHost(audio_manager_.get(), | |
333 media_stream_manager_.get(), | |
334 mirroring_manager_.get(), | |
335 NULL, | |
336 channel_.get()); | |
337 audio_input_renderer_host_->set_peer_pid_for_testing( | |
338 base::GetCurrentProcId()); | |
339 } | |
340 | |
341 void MAYBE_WebRTCAudioDeviceTest::DestroyChannel() { | |
342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
343 audio_render_host_->OnChannelClosing(); | |
344 audio_render_host_->OnFilterRemoved(); | |
345 audio_input_renderer_host_->OnChannelClosing(); | |
346 audio_input_renderer_host_->OnFilterRemoved(); | |
347 audio_render_host_->ResetChannel(); | |
348 audio_input_renderer_host_->ResetChannel(); | |
349 channel_.reset(); | |
350 audio_render_host_ = NULL; | |
351 audio_input_renderer_host_ = NULL; | |
352 } | |
353 | |
354 void MAYBE_WebRTCAudioDeviceTest::OnGetAudioHardwareConfig( | |
355 AudioParameters* input_params, AudioParameters* output_params) { | |
356 ASSERT_TRUE(audio_hardware_config_); | |
357 *input_params = audio_hardware_config_->GetInputConfig(); | |
358 *output_params = audio_hardware_config_->GetOutputConfig(); | |
359 } | |
360 | |
361 // IPC::Listener implementation. | |
362 bool MAYBE_WebRTCAudioDeviceTest::OnMessageReceived( | |
363 const IPC::Message& message) { | |
364 if (render_thread_) { | |
365 IPC::ChannelProxy::MessageFilter* filter = | |
366 render_thread_->audio_input_message_filter(); | |
367 if (filter->OnMessageReceived(message)) | |
368 return true; | |
369 | |
370 filter = render_thread_->audio_message_filter(); | |
371 if (filter->OnMessageReceived(message)) | |
372 return true; | |
373 } | |
374 | |
375 if (audio_render_host_.get()) { | |
376 bool message_was_ok = false; | |
377 if (audio_render_host_->OnMessageReceived(message, &message_was_ok)) | |
378 return true; | |
379 } | |
380 | |
381 if (audio_input_renderer_host_.get()) { | |
382 bool message_was_ok = false; | |
383 if (audio_input_renderer_host_->OnMessageReceived(message, &message_was_ok)) | |
384 return true; | |
385 } | |
386 | |
387 bool handled ALLOW_UNUSED = true; | |
388 bool message_is_ok = true; | |
389 IPC_BEGIN_MESSAGE_MAP_EX(MAYBE_WebRTCAudioDeviceTest, message, message_is_ok) | |
390 IPC_MESSAGE_HANDLER(ViewHostMsg_GetAudioHardwareConfig, | |
391 OnGetAudioHardwareConfig) | |
392 IPC_MESSAGE_UNHANDLED(handled = false) | |
393 IPC_END_MESSAGE_MAP_EX() | |
394 | |
395 EXPECT_TRUE(message_is_ok); | |
396 | |
397 return true; | |
398 } | |
399 | |
400 // Posts a final task to the IO message loop and waits for completion. | |
401 void MAYBE_WebRTCAudioDeviceTest::WaitForIOThreadCompletion() { | |
402 WaitForTaskRunnerCompletion( | |
403 ChildProcess::current()->io_message_loop()->message_loop_proxy()); | |
404 } | |
405 | |
406 void MAYBE_WebRTCAudioDeviceTest::WaitForAudioManagerCompletion() { | |
407 if (audio_manager_) | |
408 WaitForTaskRunnerCompletion(audio_manager_->GetTaskRunner()); | |
409 } | |
410 | |
411 void MAYBE_WebRTCAudioDeviceTest::WaitForTaskRunnerCompletion( | |
412 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | |
413 base::WaitableEvent* event = new base::WaitableEvent(false, false); | |
414 task_runner->PostTask( | |
415 FROM_HERE, | |
416 base::Bind(&base::WaitableEvent::Signal, base::Unretained(event))); | |
417 if (event->TimedWait(TestTimeouts::action_max_timeout())) { | |
418 delete event; | |
419 } else { | |
420 // Don't delete the event object in case the message ever gets processed. | |
421 // If we do, we will crash the test process. | |
422 ADD_FAILURE() << "Failed to wait for message loop"; | |
423 } | |
424 } | |
425 | |
426 std::string MAYBE_WebRTCAudioDeviceTest::GetTestDataPath( | |
427 const base::FilePath::StringType& file_name) { | |
428 base::FilePath path; | |
429 EXPECT_TRUE(PathService::Get(DIR_TEST_DATA, &path)); | |
430 path = path.Append(file_name); | |
431 EXPECT_TRUE(base::PathExists(path)); | |
432 #if defined(OS_WIN) | |
433 return base::WideToUTF8(path.value()); | |
434 #else | |
435 return path.value(); | |
436 #endif | |
437 } | |
438 | |
439 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network) | |
440 : network_(network) { | |
441 } | |
442 | |
443 WebRTCTransportImpl::~WebRTCTransportImpl() {} | |
444 | |
445 int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) { | |
446 return network_->ReceivedRTPPacket(channel, data, len); | |
447 } | |
448 | |
449 int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data, | |
450 int len) { | |
451 return network_->ReceivedRTCPPacket(channel, data, len); | |
452 } | |
453 | |
454 } // namespace content | |
OLD | NEW |