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.
5 #include "content/test/webrtc_audio_device_test.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/file_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/test/test_timeouts.h"
15 #include "content/browser/renderer_host/media/audio_input_renderer_host.h"
16 #include "content/browser/renderer_host/media/audio_mirroring_manager.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"
43 #include "base/win/scoped_com_initializer.h"
46 using media::AudioParameters
;
47 using media::ChannelLayout
;
49 using testing::InvokeWithoutArgs
;
50 using testing::Return
;
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
{
59 WebRTCMockRenderProcess() {}
60 virtual ~WebRTCMockRenderProcess() {}
62 // RenderProcess implementation.
63 virtual skia::PlatformCanvas
* GetDrawingCanvas(
64 TransportDIB
** memory
, const gfx::Rect
& rect
) OVERRIDE
{
67 virtual void ReleaseTransportDIB(TransportDIB
* memory
) OVERRIDE
{}
68 virtual bool UseInProcessPlugins() const OVERRIDE
{ return false; }
69 virtual void AddBindings(int bindings
) OVERRIDE
{}
70 virtual int GetEnabledBindings() const OVERRIDE
{ return 0; }
71 virtual TransportDIB
* CreateTransportDIB(size_t size
) OVERRIDE
{
74 virtual void FreeTransportDIB(TransportDIB
*) OVERRIDE
{}
77 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess
);
80 // Utility scoped class to replace the global content client's renderer for the
81 // duration of the test.
82 class ReplaceContentClientRenderer
{
84 explicit ReplaceContentClientRenderer(ContentRendererClient
* new_renderer
) {
85 saved_renderer_
= SetRendererClientForTesting(new_renderer
);
87 ~ReplaceContentClientRenderer() {
88 // Restore the original renderer.
89 SetRendererClientForTesting(saved_renderer_
);
92 ContentRendererClient
* saved_renderer_
;
93 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer
);
96 class MockRTCResourceContext
: public ResourceContext
{
98 MockRTCResourceContext() : test_request_context_(NULL
) {}
99 virtual ~MockRTCResourceContext() {}
101 void set_request_context(net::URLRequestContext
* request_context
) {
102 test_request_context_
= request_context
;
105 // ResourceContext implementation:
106 virtual net::HostResolver
* GetHostResolver() OVERRIDE
{
109 virtual net::URLRequestContext
* GetRequestContext() OVERRIDE
{
110 return test_request_context_
;
113 virtual bool AllowMicAccess(const GURL
& origin
) OVERRIDE
{
117 virtual bool AllowCameraAccess(const GURL
& origin
) OVERRIDE
{
122 net::URLRequestContext
* test_request_context_
;
124 DISALLOW_COPY_AND_ASSIGN(MockRTCResourceContext
);
127 ACTION_P(QuitMessageLoop
, loop_or_proxy
) {
128 loop_or_proxy
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
131 MAYBE_WebRTCAudioDeviceTest::MAYBE_WebRTCAudioDeviceTest()
132 : render_thread_(NULL
), audio_hardware_config_(NULL
),
133 has_input_devices_(false), has_output_devices_(false) {
136 MAYBE_WebRTCAudioDeviceTest::~MAYBE_WebRTCAudioDeviceTest() {}
138 void MAYBE_WebRTCAudioDeviceTest::SetUp() {
139 // This part sets up a RenderThread environment to ensure that
140 // RenderThread::current() (<=> TLS pointer) is valid.
141 // Main parts are inspired by the RenderViewFakeResourcesTest.
142 // Note that, the IPC part is not utilized in this test.
143 saved_content_renderer_
.reset(
144 new ReplaceContentClientRenderer(&content_renderer_client_
));
145 mock_process_
.reset(new WebRTCMockRenderProcess());
147 new TestBrowserThread(BrowserThread::UI
, base::MessageLoop::current()));
149 // Construct the resource context on the UI thread.
150 resource_context_
.reset(new MockRTCResourceContext
);
152 static const char kThreadName
[] = "RenderThread";
153 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE
,
154 base::Bind(&MAYBE_WebRTCAudioDeviceTest::InitializeIOThread
,
155 base::Unretained(this), kThreadName
));
156 WaitForIOThreadCompletion();
158 sandbox_was_enabled_
=
159 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(false);
160 render_thread_
= new RenderThreadImpl(kThreadName
);
163 void MAYBE_WebRTCAudioDeviceTest::TearDown() {
164 SetAudioHardwareConfig(NULL
);
166 // Run any pending cleanup tasks that may have been posted to the main thread.
167 base::RunLoop().RunUntilIdle();
169 // Kick of the cleanup process by closing the channel. This queues up
170 // OnStreamClosed calls to be executed on the audio thread.
171 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE
,
172 base::Bind(&MAYBE_WebRTCAudioDeviceTest::DestroyChannel
,
173 base::Unretained(this)));
174 WaitForIOThreadCompletion();
176 // When audio [input] render hosts are notified that the channel has
177 // been closed, they post tasks to the audio thread to close the
178 // AudioOutputController and once that's completed, a task is posted back to
179 // the IO thread to actually delete the AudioEntry for the audio stream. Only
180 // then is the reference to the audio manager released, so we wait for the
181 // whole thing to be torn down before we finally uninitialize the io thread.
182 WaitForAudioManagerCompletion();
184 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE
,
185 base::Bind(&MAYBE_WebRTCAudioDeviceTest::UninitializeIOThread
,
186 base::Unretained((this))));
187 WaitForIOThreadCompletion();
188 mock_process_
.reset();
189 media_stream_manager_
.reset();
190 mirroring_manager_
.reset();
191 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
192 sandbox_was_enabled_
);
195 bool MAYBE_WebRTCAudioDeviceTest::Send(IPC::Message
* message
) {
196 return channel_
->Send(message
);
199 void MAYBE_WebRTCAudioDeviceTest::SetAudioHardwareConfig(
200 media::AudioHardwareConfig
* hardware_config
) {
201 audio_hardware_config_
= hardware_config
;
204 scoped_refptr
<WebRtcAudioRenderer
>
205 MAYBE_WebRTCAudioDeviceTest::CreateDefaultWebRtcAudioRenderer(
206 int render_view_id
) {
207 media::AudioHardwareConfig
* hardware_config
=
208 RenderThreadImpl::current()->GetAudioHardwareConfig();
209 int sample_rate
= hardware_config
->GetOutputSampleRate();
210 int frames_per_buffer
= hardware_config
->GetOutputBufferSize();
212 return new WebRtcAudioRenderer(render_view_id
, 0, sample_rate
,
216 void MAYBE_WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name
) {
218 // We initialize COM (STA) on our IO thread as is done in Chrome.
219 // See BrowserProcessSubThread::Init.
220 initialize_com_
.reset(new base::win::ScopedCOMInitializer());
223 // Set the current thread as the IO thread.
225 new TestBrowserThread(BrowserThread::IO
, base::MessageLoop::current()));
227 // Populate our resource context.
228 test_request_context_
.reset(new net::TestURLRequestContext());
229 MockRTCResourceContext
* resource_context
=
230 static_cast<MockRTCResourceContext
*>(resource_context_
.get());
231 resource_context
->set_request_context(test_request_context_
.get());
232 media_internals_
.reset(new MockMediaInternals());
234 // Create our own AudioManager, AudioMirroringManager and MediaStreamManager.
235 audio_manager_
.reset(media::AudioManager::Create());
236 mirroring_manager_
.reset(new AudioMirroringManager());
237 media_stream_manager_
.reset(new MediaStreamManager(audio_manager_
.get()));
239 has_input_devices_
= audio_manager_
->HasAudioInputDevices();
240 has_output_devices_
= audio_manager_
->HasAudioOutputDevices();
242 // Create an IPC channel that handles incoming messages on the IO thread.
243 CreateChannel(thread_name
);
246 void MAYBE_WebRTCAudioDeviceTest::UninitializeIOThread() {
247 resource_context_
.reset();
249 test_request_context_
.reset();
252 initialize_com_
.reset();
255 audio_manager_
.reset();
258 void MAYBE_WebRTCAudioDeviceTest::CreateChannel(const char* name
) {
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
261 static const int kRenderProcessId
= 1;
262 audio_render_host_
= new AudioRendererHost(
263 kRenderProcessId
, audio_manager_
.get(), mirroring_manager_
.get(),
264 media_internals_
.get(), media_stream_manager_
.get());
265 audio_render_host_
->OnChannelConnected(base::GetCurrentProcId());
267 audio_input_renderer_host_
=
268 new AudioInputRendererHost(audio_manager_
.get(),
269 media_stream_manager_
.get(),
270 mirroring_manager_
.get(),
272 audio_input_renderer_host_
->OnChannelConnected(base::GetCurrentProcId());
274 channel_
.reset(new IPC::Channel(name
, IPC::Channel::MODE_SERVER
, this));
275 ASSERT_TRUE(channel_
->Connect());
277 audio_render_host_
->OnFilterAdded(channel_
.get());
278 audio_input_renderer_host_
->OnFilterAdded(channel_
.get());
281 void MAYBE_WebRTCAudioDeviceTest::DestroyChannel() {
282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
283 audio_render_host_
->OnChannelClosing();
284 audio_render_host_
->OnFilterRemoved();
285 audio_input_renderer_host_
->OnChannelClosing();
286 audio_input_renderer_host_
->OnFilterRemoved();
288 audio_render_host_
= NULL
;
289 audio_input_renderer_host_
= NULL
;
292 void MAYBE_WebRTCAudioDeviceTest::OnGetAudioHardwareConfig(
293 AudioParameters
* input_params
, AudioParameters
* output_params
) {
294 ASSERT_TRUE(audio_hardware_config_
);
295 *input_params
= audio_hardware_config_
->GetInputConfig();
296 *output_params
= audio_hardware_config_
->GetOutputConfig();
299 // IPC::Listener implementation.
300 bool MAYBE_WebRTCAudioDeviceTest::OnMessageReceived(
301 const IPC::Message
& message
) {
302 if (render_thread_
) {
303 IPC::ChannelProxy::MessageFilter
* filter
=
304 render_thread_
->audio_input_message_filter();
305 if (filter
->OnMessageReceived(message
))
308 filter
= render_thread_
->audio_message_filter();
309 if (filter
->OnMessageReceived(message
))
313 if (audio_render_host_
.get()) {
314 bool message_was_ok
= false;
315 if (audio_render_host_
->OnMessageReceived(message
, &message_was_ok
))
319 if (audio_input_renderer_host_
.get()) {
320 bool message_was_ok
= false;
321 if (audio_input_renderer_host_
->OnMessageReceived(message
, &message_was_ok
))
325 bool handled ALLOW_UNUSED
= true;
326 bool message_is_ok
= true;
327 IPC_BEGIN_MESSAGE_MAP_EX(MAYBE_WebRTCAudioDeviceTest
, message
, message_is_ok
)
328 IPC_MESSAGE_HANDLER(ViewHostMsg_GetAudioHardwareConfig
,
329 OnGetAudioHardwareConfig
)
330 IPC_MESSAGE_UNHANDLED(handled
= false)
331 IPC_END_MESSAGE_MAP_EX()
333 EXPECT_TRUE(message_is_ok
);
338 // Posts a final task to the IO message loop and waits for completion.
339 void MAYBE_WebRTCAudioDeviceTest::WaitForIOThreadCompletion() {
340 WaitForMessageLoopCompletion(
341 ChildProcess::current()->io_message_loop()->message_loop_proxy().get());
344 void MAYBE_WebRTCAudioDeviceTest::WaitForAudioManagerCompletion() {
346 WaitForMessageLoopCompletion(audio_manager_
->GetMessageLoop().get());
349 void MAYBE_WebRTCAudioDeviceTest::WaitForMessageLoopCompletion(
350 base::MessageLoopProxy
* loop
) {
351 base::WaitableEvent
* event
= new base::WaitableEvent(false, false);
352 loop
->PostTask(FROM_HERE
, base::Bind(&base::WaitableEvent::Signal
,
353 base::Unretained(event
)));
354 if (event
->TimedWait(TestTimeouts::action_max_timeout())) {
357 // Don't delete the event object in case the message ever gets processed.
358 // If we do, we will crash the test process.
359 ADD_FAILURE() << "Failed to wait for message loop";
363 std::string
MAYBE_WebRTCAudioDeviceTest::GetTestDataPath(
364 const base::FilePath::StringType
& file_name
) {
366 EXPECT_TRUE(PathService::Get(DIR_TEST_DATA
, &path
));
367 path
= path
.Append(file_name
);
368 EXPECT_TRUE(base::PathExists(path
));
370 return WideToUTF8(path
.value());
376 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork
* network
)
377 : network_(network
) {
380 WebRTCTransportImpl::~WebRTCTransportImpl() {}
382 int WebRTCTransportImpl::SendPacket(int channel
, const void* data
, int len
) {
383 return network_
->ReceivedRTPPacket(channel
, data
, len
);
386 int WebRTCTransportImpl::SendRTCPPacket(int channel
, const void* data
,
388 return network_
->ReceivedRTCPPacket(channel
, data
, len
);
391 } // namespace content