OLD | NEW |
(Empty) | |
| 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 |
| 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/file_util.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/test/signaling_task.h" |
| 12 #include "base/test/test_timeouts.h" |
| 13 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 14 #include "content/browser/renderer_host/media/mock_media_observer.h" |
| 15 #include "content/browser/resource_context.h" |
| 16 #include "content/common/view_messages.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/common/content_paths.h" |
| 19 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 20 #include "content/renderer/render_process.h" |
| 21 #include "content/renderer/render_thread_impl.h" |
| 22 #include "content/test/test_browser_thread.h" |
| 23 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/webrtc/voice_engine/main/interface/voe_audio_processing.h" |
| 27 #include "third_party/webrtc/voice_engine/main/interface/voe_base.h" |
| 28 #include "third_party/webrtc/voice_engine/main/interface/voe_file.h" |
| 29 #include "third_party/webrtc/voice_engine/main/interface/voe_network.h" |
| 30 |
| 31 using testing::_; |
| 32 using testing::InvokeWithoutArgs; |
| 33 using testing::Return; |
| 34 using testing::StrEq; |
| 35 |
| 36 // This class is a mock of the child process singleton which is needed |
| 37 // to be able to create a RenderThread object. |
| 38 class WebRTCMockRenderProcess : public RenderProcess { |
| 39 public: |
| 40 WebRTCMockRenderProcess() {} |
| 41 virtual ~WebRTCMockRenderProcess() {} |
| 42 |
| 43 // RenderProcess implementation. |
| 44 virtual skia::PlatformCanvas* GetDrawingCanvas(TransportDIB** memory, |
| 45 const gfx::Rect& rect) { |
| 46 return NULL; |
| 47 } |
| 48 virtual void ReleaseTransportDIB(TransportDIB* memory) {} |
| 49 virtual bool UseInProcessPlugins() const { return false; } |
| 50 virtual bool HasInitializedMediaLibrary() const { return false; } |
| 51 |
| 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess); |
| 54 }; |
| 55 |
| 56 // Utility scoped class to replace the global content client's renderer for the |
| 57 // duration of the test. |
| 58 class ReplaceContentClientRenderer { |
| 59 public: |
| 60 ReplaceContentClientRenderer(content::ContentRendererClient* new_renderer) { |
| 61 saved_renderer_ = content::GetContentClient()->renderer(); |
| 62 content::GetContentClient()->set_renderer(new_renderer); |
| 63 } |
| 64 ~ReplaceContentClientRenderer() { |
| 65 // Restore the original renderer. |
| 66 content::GetContentClient()->set_renderer(saved_renderer_); |
| 67 } |
| 68 private: |
| 69 content::ContentRendererClient* saved_renderer_; |
| 70 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer); |
| 71 }; |
| 72 |
| 73 namespace { |
| 74 |
| 75 class WebRTCMockResourceContext : public content::ResourceContext { |
| 76 public: |
| 77 WebRTCMockResourceContext() {} |
| 78 virtual ~WebRTCMockResourceContext() {} |
| 79 virtual void EnsureInitialized() const OVERRIDE {} |
| 80 }; |
| 81 |
| 82 ACTION_P(QuitMessageLoop, loop_or_proxy) { |
| 83 loop_or_proxy->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 84 } |
| 85 |
| 86 } // end namespace |
| 87 |
| 88 WebRTCAudioDeviceTest::WebRTCAudioDeviceTest() |
| 89 : render_thread_(NULL), event_(false, false), audio_util_callback_(NULL) { |
| 90 } |
| 91 |
| 92 WebRTCAudioDeviceTest::~WebRTCAudioDeviceTest() {} |
| 93 |
| 94 void WebRTCAudioDeviceTest::SetUp() { |
| 95 // This part sets up a RenderThread environment to ensure that |
| 96 // RenderThread::current() (<=> TLS pointer) is valid. |
| 97 // Main parts are inspired by the RenderViewFakeResourcesTest. |
| 98 // Note that, the IPC part is not utilized in this test. |
| 99 saved_content_renderer_.reset( |
| 100 new ReplaceContentClientRenderer(&mock_content_renderer_client_)); |
| 101 mock_process_.reset(new WebRTCMockRenderProcess()); |
| 102 ui_thread_.reset(new content::TestBrowserThread(content::BrowserThread::UI, |
| 103 MessageLoop::current())); |
| 104 |
| 105 // Construct the resource context on the UI thread. |
| 106 resource_context_.reset(new WebRTCMockResourceContext()); |
| 107 |
| 108 static const char kThreadName[] = "RenderThread"; |
| 109 ChildProcess::current()->io_message_loop()->PostTask( |
| 110 FROM_HERE, |
| 111 base::Bind(&SetupTask::InitializeIOThread, new SetupTask(this), |
| 112 kThreadName)); |
| 113 WaitForIOThreadCompletion(); |
| 114 |
| 115 render_thread_ = new RenderThreadImpl(kThreadName); |
| 116 mock_process_->set_main_thread(render_thread_); |
| 117 } |
| 118 |
| 119 void WebRTCAudioDeviceTest::TearDown() { |
| 120 ChildProcess::current()->io_message_loop()->PostTask( |
| 121 FROM_HERE, |
| 122 base::Bind(&SetupTask::UninitializeIOThread, new SetupTask(this))); |
| 123 WaitForIOThreadCompletion(); |
| 124 mock_process_.reset(); |
| 125 } |
| 126 |
| 127 bool WebRTCAudioDeviceTest::Send(IPC::Message* message) { |
| 128 return channel_->Send(message); |
| 129 } |
| 130 |
| 131 void WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) { |
| 132 // Set the current thread as the IO thread. |
| 133 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, |
| 134 MessageLoop::current())); |
| 135 test_request_context_ = new TestURLRequestContext(); |
| 136 resource_context_->set_request_context(test_request_context_.get()); |
| 137 media_observer_.reset(new MockMediaObserver()); |
| 138 resource_context_->set_media_observer(media_observer_.get()); |
| 139 |
| 140 CreateChannel(thread_name, resource_context_.get()); |
| 141 } |
| 142 |
| 143 void WebRTCAudioDeviceTest::UninitializeIOThread() { |
| 144 DestroyChannel(); |
| 145 resource_context_.reset(); |
| 146 test_request_context_ = NULL; |
| 147 } |
| 148 |
| 149 void WebRTCAudioDeviceTest::CreateChannel( |
| 150 const char* name, |
| 151 content::ResourceContext* resource_context) { |
| 152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 153 audio_render_host_ = new AudioRendererHost(resource_context); |
| 154 audio_render_host_->OnChannelConnected(base::GetCurrentProcId()); |
| 155 |
| 156 channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this)); |
| 157 ASSERT_TRUE(channel_->Connect()); |
| 158 |
| 159 audio_render_host_->OnFilterAdded(channel_.get()); |
| 160 } |
| 161 |
| 162 void WebRTCAudioDeviceTest::DestroyChannel() { |
| 163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 164 channel_.reset(); |
| 165 audio_render_host_ = NULL; |
| 166 } |
| 167 |
| 168 void WebRTCAudioDeviceTest::OnGetHardwareSampleRate(double* sample_rate) { |
| 169 EXPECT_TRUE(audio_util_callback_); |
| 170 *sample_rate = audio_util_callback_ ? |
| 171 audio_util_callback_->GetAudioHardwareSampleRate() : 0.0; |
| 172 } |
| 173 |
| 174 void WebRTCAudioDeviceTest::OnGetHardwareInputSampleRate(double* sample_rate) { |
| 175 EXPECT_TRUE(audio_util_callback_); |
| 176 *sample_rate = audio_util_callback_ ? |
| 177 audio_util_callback_->GetAudioInputHardwareSampleRate() : 0.0; |
| 178 } |
| 179 |
| 180 // IPC::Channel::Listener implementation. |
| 181 bool WebRTCAudioDeviceTest::OnMessageReceived(const IPC::Message& message) { |
| 182 if (render_thread_) { |
| 183 IPC::ChannelProxy::MessageFilter* filter = |
| 184 render_thread_->audio_input_message_filter(); |
| 185 if (filter->OnMessageReceived(message)) |
| 186 return true; |
| 187 |
| 188 filter = render_thread_->audio_message_filter(); |
| 189 if (filter->OnMessageReceived(message)) |
| 190 return true; |
| 191 } |
| 192 |
| 193 if (audio_render_host_.get()) { |
| 194 bool message_was_ok = false; |
| 195 if (audio_render_host_->OnMessageReceived(message, &message_was_ok)) |
| 196 return true; |
| 197 } |
| 198 |
| 199 bool handled = true; |
| 200 bool message_is_ok = true; |
| 201 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCAudioDeviceTest, message, message_is_ok) |
| 202 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareSampleRate, |
| 203 OnGetHardwareSampleRate) |
| 204 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareInputSampleRate, |
| 205 OnGetHardwareInputSampleRate) |
| 206 IPC_MESSAGE_UNHANDLED(handled = false) |
| 207 IPC_END_MESSAGE_MAP_EX() |
| 208 |
| 209 EXPECT_TRUE(message_is_ok); |
| 210 |
| 211 // We leave a DLOG as a hint to the developer in case important IPC messages |
| 212 // are being dropped. |
| 213 DLOG_IF(WARNING, !handled) << "Unhandled IPC message"; |
| 214 |
| 215 return true; |
| 216 } |
| 217 |
| 218 // Posts a final task to the IO message loop and waits for completion. |
| 219 void WebRTCAudioDeviceTest::WaitForIOThreadCompletion() { |
| 220 ChildProcess::current()->io_message_loop()->PostTask( |
| 221 FROM_HERE, new base::SignalingTask(&event_)); |
| 222 EXPECT_TRUE(event_.TimedWait( |
| 223 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); |
| 224 } |
| 225 |
| 226 std::string WebRTCAudioDeviceTest::GetTestDataPath( |
| 227 const FilePath::StringType& file_name) { |
| 228 FilePath path; |
| 229 EXPECT_TRUE(PathService::Get(content::DIR_TEST_DATA, &path)); |
| 230 path = path.Append(file_name); |
| 231 EXPECT_TRUE(file_util::PathExists(path)); |
| 232 #ifdef OS_WIN |
| 233 return WideToUTF8(path.value()); |
| 234 #else |
| 235 return path.value(); |
| 236 #endif |
| 237 } |
| 238 |
| 239 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network) |
| 240 : network_(network) { |
| 241 } |
| 242 |
| 243 WebRTCTransportImpl::~WebRTCTransportImpl() {} |
| 244 |
| 245 int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) { |
| 246 ADD_FAILURE(); // We don't expect a call to this method in our tests. |
| 247 return network_->ReceivedRTPPacket(channel, data, len); |
| 248 } |
| 249 |
| 250 int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data, |
| 251 int len) { |
| 252 return network_->ReceivedRTCPPacket(channel, data, len); |
| 253 } |
OLD | NEW |