add a use_alsa gyp setting
[chromium-blink-merge.git] / content / test / webrtc_audio_device_test.cc
blobe9a3c480ef5f86a758b7fdc5ad8959b33c9f8be0
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"
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/message_loop.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/test/test_timeouts.h"
14 #include "content/browser/renderer_host/media/audio_input_renderer_host.h"
15 #include "content/browser/renderer_host/media/audio_mirroring_manager.h"
16 #include "content/browser/renderer_host/media/audio_renderer_host.h"
17 #include "content/browser/renderer_host/media/media_stream_manager.h"
18 #include "content/browser/renderer_host/media/mock_media_observer.h"
19 #include "content/common/view_messages.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/common/content_paths.h"
22 #include "content/public/test/mock_resource_context.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "content/renderer/media/audio_input_message_filter.h"
25 #include "content/renderer/media/audio_message_filter.h"
26 #include "content/renderer/media/webrtc_audio_device_impl.h"
27 #include "content/renderer/render_process.h"
28 #include "content/renderer/render_thread_impl.h"
29 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
30 #include "media/base/audio_hardware_config.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "third_party/webrtc/voice_engine/include/voe_audio_processing.h"
35 #include "third_party/webrtc/voice_engine/include/voe_base.h"
36 #include "third_party/webrtc/voice_engine/include/voe_file.h"
37 #include "third_party/webrtc/voice_engine/include/voe_network.h"
39 #if defined(OS_WIN)
40 #include "base/win/scoped_com_initializer.h"
41 #endif
43 using testing::_;
44 using testing::InvokeWithoutArgs;
45 using testing::Return;
46 using testing::StrEq;
48 namespace content {
50 // This class is a mock of the child process singleton which is needed
51 // to be able to create a RenderThread object.
52 class WebRTCMockRenderProcess : public RenderProcess {
53 public:
54 WebRTCMockRenderProcess() {}
55 virtual ~WebRTCMockRenderProcess() {}
57 // RenderProcess implementation.
58 virtual skia::PlatformCanvas* GetDrawingCanvas(
59 TransportDIB** memory, const gfx::Rect& rect) OVERRIDE {
60 return NULL;
62 virtual void ReleaseTransportDIB(TransportDIB* memory) OVERRIDE {}
63 virtual bool UseInProcessPlugins() const OVERRIDE { return false; }
64 virtual void AddBindings(int bindings) OVERRIDE {}
65 virtual int GetEnabledBindings() const OVERRIDE { return 0; }
66 virtual TransportDIB* CreateTransportDIB(size_t size) OVERRIDE {
67 return NULL;
69 virtual void FreeTransportDIB(TransportDIB*) OVERRIDE {}
71 private:
72 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess);
75 // Utility scoped class to replace the global content client's renderer for the
76 // duration of the test.
77 class ReplaceContentClientRenderer {
78 public:
79 explicit ReplaceContentClientRenderer(ContentRendererClient* new_renderer) {
80 saved_renderer_ = GetContentClient()->renderer();
81 GetContentClient()->set_renderer_for_testing(new_renderer);
83 ~ReplaceContentClientRenderer() {
84 // Restore the original renderer.
85 GetContentClient()->set_renderer_for_testing(saved_renderer_);
87 private:
88 ContentRendererClient* saved_renderer_;
89 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer);
92 class MockRTCResourceContext : public ResourceContext {
93 public:
94 MockRTCResourceContext() : test_request_context_(NULL) {}
95 virtual ~MockRTCResourceContext() {}
97 void set_request_context(net::URLRequestContext* request_context) {
98 test_request_context_ = request_context;
101 // ResourceContext implementation:
102 virtual net::HostResolver* GetHostResolver() OVERRIDE {
103 return NULL;
105 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
106 return test_request_context_;
109 private:
110 net::URLRequestContext* test_request_context_;
112 DISALLOW_COPY_AND_ASSIGN(MockRTCResourceContext);
115 ACTION_P(QuitMessageLoop, loop_or_proxy) {
116 loop_or_proxy->PostTask(FROM_HERE, MessageLoop::QuitClosure());
119 WebRTCAudioDeviceTest::WebRTCAudioDeviceTest()
120 : render_thread_(NULL), audio_hardware_config_(NULL),
121 has_input_devices_(false), has_output_devices_(false) {
124 WebRTCAudioDeviceTest::~WebRTCAudioDeviceTest() {}
126 void WebRTCAudioDeviceTest::SetUp() {
127 // This part sets up a RenderThread environment to ensure that
128 // RenderThread::current() (<=> TLS pointer) is valid.
129 // Main parts are inspired by the RenderViewFakeResourcesTest.
130 // Note that, the IPC part is not utilized in this test.
131 saved_content_renderer_.reset(
132 new ReplaceContentClientRenderer(&content_renderer_client_));
133 mock_process_.reset(new WebRTCMockRenderProcess());
134 ui_thread_.reset(new TestBrowserThread(BrowserThread::UI,
135 MessageLoop::current()));
137 // Construct the resource context on the UI thread.
138 resource_context_.reset(new MockRTCResourceContext);
140 static const char kThreadName[] = "RenderThread";
141 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE,
142 base::Bind(&WebRTCAudioDeviceTest::InitializeIOThread,
143 base::Unretained(this), kThreadName));
144 WaitForIOThreadCompletion();
146 sandbox_was_enabled_ =
147 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(false);
148 render_thread_ = new RenderThreadImpl(kThreadName);
151 void WebRTCAudioDeviceTest::TearDown() {
152 SetAudioHardwareConfig(NULL);
154 // Run any pending cleanup tasks that may have been posted to the main thread.
155 ChildProcess::current()->main_thread()->message_loop()->RunUntilIdle();
157 // Kick of the cleanup process by closing the channel. This queues up
158 // OnStreamClosed calls to be executed on the audio thread.
159 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE,
160 base::Bind(&WebRTCAudioDeviceTest::DestroyChannel,
161 base::Unretained(this)));
162 WaitForIOThreadCompletion();
164 // When audio [input] render hosts are notified that the channel has
165 // been closed, they post tasks to the audio thread to close the
166 // AudioOutputController and once that's completed, a task is posted back to
167 // the IO thread to actually delete the AudioEntry for the audio stream. Only
168 // then is the reference to the audio manager released, so we wait for the
169 // whole thing to be torn down before we finally uninitialize the io thread.
170 WaitForAudioManagerCompletion();
172 ChildProcess::current()->io_message_loop()->PostTask(FROM_HERE,
173 base::Bind(&WebRTCAudioDeviceTest::UninitializeIOThread,
174 base::Unretained((this))));
175 WaitForIOThreadCompletion();
176 mock_process_.reset();
177 media_stream_manager_.reset();
178 mirroring_manager_.reset();
179 audio_manager_.reset();
180 RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
181 sandbox_was_enabled_);
184 bool WebRTCAudioDeviceTest::Send(IPC::Message* message) {
185 return channel_->Send(message);
188 void WebRTCAudioDeviceTest::SetAudioHardwareConfig(
189 media::AudioHardwareConfig* hardware_config) {
190 audio_hardware_config_ = hardware_config;
193 void WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) {
194 #if defined(OS_WIN)
195 // We initialize COM (STA) on our IO thread as is done in Chrome.
196 // See BrowserProcessSubThread::Init.
197 initialize_com_.reset(new base::win::ScopedCOMInitializer());
198 #endif
200 // Set the current thread as the IO thread.
201 io_thread_.reset(new TestBrowserThread(BrowserThread::IO,
202 MessageLoop::current()));
204 // Populate our resource context.
205 test_request_context_.reset(new net::TestURLRequestContext());
206 MockRTCResourceContext* resource_context =
207 static_cast<MockRTCResourceContext*>(resource_context_.get());
208 resource_context->set_request_context(test_request_context_.get());
209 media_internals_.reset(new MockMediaInternals());
211 // Create our own AudioManager, AudioMirroringManager and MediaStreamManager.
212 audio_manager_.reset(media::AudioManager::Create());
213 mirroring_manager_.reset(new AudioMirroringManager());
214 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
216 has_input_devices_ = audio_manager_->HasAudioInputDevices();
217 has_output_devices_ = audio_manager_->HasAudioOutputDevices();
219 // Create an IPC channel that handles incoming messages on the IO thread.
220 CreateChannel(thread_name);
223 void WebRTCAudioDeviceTest::UninitializeIOThread() {
224 resource_context_.reset();
226 test_request_context_.reset();
228 #if defined(OS_WIN)
229 initialize_com_.reset();
230 #endif
233 void WebRTCAudioDeviceTest::CreateChannel(const char* name) {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
236 static const int kRenderProcessId = 1;
237 audio_render_host_ = new AudioRendererHost(
238 kRenderProcessId, audio_manager_.get(), mirroring_manager_.get(),
239 media_internals_.get());
240 audio_render_host_->OnChannelConnected(base::GetCurrentProcId());
242 audio_input_renderer_host_ = new AudioInputRendererHost(
243 audio_manager_.get(), media_stream_manager_.get());
244 audio_input_renderer_host_->OnChannelConnected(base::GetCurrentProcId());
246 channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this));
247 ASSERT_TRUE(channel_->Connect());
249 audio_render_host_->OnFilterAdded(channel_.get());
250 audio_input_renderer_host_->OnFilterAdded(channel_.get());
253 void WebRTCAudioDeviceTest::DestroyChannel() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
255 audio_render_host_->OnChannelClosing();
256 audio_render_host_->OnFilterRemoved();
257 audio_input_renderer_host_->OnChannelClosing();
258 audio_input_renderer_host_->OnFilterRemoved();
259 channel_.reset();
260 audio_render_host_ = NULL;
261 audio_input_renderer_host_ = NULL;
264 void WebRTCAudioDeviceTest::OnGetAudioHardwareConfig(
265 int* output_buffer_size, int* output_sample_rate, int* input_sample_rate,
266 media::ChannelLayout* input_channel_layout) {
267 ASSERT_TRUE(audio_hardware_config_);
269 *output_buffer_size = audio_hardware_config_->GetOutputBufferSize();
270 *output_sample_rate = audio_hardware_config_->GetOutputSampleRate();
272 // TODO(henrika): add support for all available input devices.
273 *input_sample_rate = audio_hardware_config_->GetInputSampleRate();
274 *input_channel_layout = audio_hardware_config_->GetInputChannelLayout();
277 // IPC::Listener implementation.
278 bool WebRTCAudioDeviceTest::OnMessageReceived(const IPC::Message& message) {
279 if (render_thread_) {
280 IPC::ChannelProxy::MessageFilter* filter =
281 render_thread_->audio_input_message_filter();
282 if (filter->OnMessageReceived(message))
283 return true;
285 filter = render_thread_->audio_message_filter();
286 if (filter->OnMessageReceived(message))
287 return true;
290 if (audio_render_host_.get()) {
291 bool message_was_ok = false;
292 if (audio_render_host_->OnMessageReceived(message, &message_was_ok))
293 return true;
296 if (audio_input_renderer_host_.get()) {
297 bool message_was_ok = false;
298 if (audio_input_renderer_host_->OnMessageReceived(message, &message_was_ok))
299 return true;
302 bool handled ALLOW_UNUSED = true;
303 bool message_is_ok = true;
304 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCAudioDeviceTest, message, message_is_ok)
305 IPC_MESSAGE_HANDLER(ViewHostMsg_GetAudioHardwareConfig,
306 OnGetAudioHardwareConfig)
307 IPC_MESSAGE_UNHANDLED(handled = false)
308 IPC_END_MESSAGE_MAP_EX()
310 EXPECT_TRUE(message_is_ok);
312 return true;
315 // Posts a final task to the IO message loop and waits for completion.
316 void WebRTCAudioDeviceTest::WaitForIOThreadCompletion() {
317 WaitForMessageLoopCompletion(
318 ChildProcess::current()->io_message_loop()->message_loop_proxy());
321 void WebRTCAudioDeviceTest::WaitForAudioManagerCompletion() {
322 if (audio_manager_.get())
323 WaitForMessageLoopCompletion(audio_manager_->GetMessageLoop());
326 void WebRTCAudioDeviceTest::WaitForMessageLoopCompletion(
327 base::MessageLoopProxy* loop) {
328 base::WaitableEvent* event = new base::WaitableEvent(false, false);
329 loop->PostTask(FROM_HERE, base::Bind(&base::WaitableEvent::Signal,
330 base::Unretained(event)));
331 if (event->TimedWait(TestTimeouts::action_max_timeout())) {
332 delete event;
333 } else {
334 // Don't delete the event object in case the message ever gets processed.
335 // If we do, we will crash the test process.
336 ADD_FAILURE() << "Failed to wait for message loop";
340 std::string WebRTCAudioDeviceTest::GetTestDataPath(
341 const base::FilePath::StringType& file_name) {
342 base::FilePath path;
343 EXPECT_TRUE(PathService::Get(DIR_TEST_DATA, &path));
344 path = path.Append(file_name);
345 EXPECT_TRUE(file_util::PathExists(path));
346 #ifdef OS_WIN
347 return WideToUTF8(path.value());
348 #else
349 return path.value();
350 #endif
353 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network)
354 : network_(network) {
357 WebRTCTransportImpl::~WebRTCTransportImpl() {}
359 int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) {
360 return network_->ReceivedRTPPacket(channel, data, len);
363 int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data,
364 int len) {
365 return network_->ReceivedRTCPPacket(channel, data, len);
368 } // namespace content