Chromium Code Reviews
|
| 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) { return NULL; } | |
|
scherkus (not reviewing)
2011/11/08 22:24:46
want to align 2nd param w/ TransportDIB** then dro
tommi (sloooow) - chröme
2011/11/09 10:21:45
Done.
| |
| 46 virtual void ReleaseTransportDIB(TransportDIB* memory) {} | |
| 47 virtual bool UseInProcessPlugins() const { return false; } | |
| 48 virtual bool HasInitializedMediaLibrary() const { return false; } | |
| 49 | |
| 50 private: | |
| 51 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess); | |
| 52 }; | |
| 53 | |
| 54 // Utility scoped class to replace the global content client's renderer for the | |
| 55 // duration of the test. | |
| 56 class ReplaceContentClientRenderer { | |
| 57 public: | |
| 58 ReplaceContentClientRenderer(content::ContentRendererClient* new_renderer) { | |
| 59 saved_renderer_ = content::GetContentClient()->renderer(); | |
| 60 content::GetContentClient()->set_renderer(new_renderer); | |
| 61 } | |
| 62 ~ReplaceContentClientRenderer() { | |
| 63 // Restore the original renderer. | |
| 64 content::GetContentClient()->set_renderer(saved_renderer_); | |
| 65 } | |
| 66 private: | |
| 67 content::ContentRendererClient* saved_renderer_; | |
| 68 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer); | |
| 69 }; | |
| 70 | |
| 71 namespace { | |
| 72 | |
| 73 class WebRTCMockResourceContext : public content::ResourceContext { | |
| 74 public: | |
| 75 WebRTCMockResourceContext() {} | |
| 76 virtual ~WebRTCMockResourceContext() {} | |
| 77 virtual void EnsureInitialized() const OVERRIDE {} | |
| 78 }; | |
| 79 | |
| 80 ACTION_P(QuitMessageLoop, loop_or_proxy) { | |
| 81 loop_or_proxy->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 82 } | |
| 83 | |
| 84 } // end namespace | |
| 85 | |
| 86 WebRTCAudioDeviceTest::WebRTCAudioDeviceTest() | |
| 87 : render_thread_(NULL), event_(false, false), audio_util_callback_(NULL) {} | |
|
scherkus (not reviewing)
2011/11/08 22:24:46
nit: not a one-liner, drop } to next line
tommi (sloooow) - chröme
2011/11/09 10:21:45
Done.
| |
| 88 WebRTCAudioDeviceTest::~WebRTCAudioDeviceTest() {} | |
| 89 | |
| 90 void WebRTCAudioDeviceTest::SetUp() { | |
| 91 // This part sets up a RenderThread environment to ensure that | |
| 92 // RenderThread::current() (<=> TLS pointer) is valid. | |
| 93 // Main parts are inspired by the RenderViewFakeResourcesTest. | |
| 94 // Note that, the IPC part is not utilized in this test. | |
| 95 saved_content_renderer_.reset( | |
| 96 new ReplaceContentClientRenderer(&mock_content_renderer_client_)); | |
| 97 mock_process_.reset(new WebRTCMockRenderProcess()); | |
| 98 ui_thread_.reset(new content::TestBrowserThread(content::BrowserThread::UI, | |
| 99 MessageLoop::current())); | |
| 100 | |
| 101 // Construct the resource context on the UI thread. | |
| 102 resource_context_.reset(new WebRTCMockResourceContext()); | |
| 103 | |
| 104 static const char kThreadName[] = "RenderThread"; | |
| 105 ChildProcess::current()->io_message_loop()->PostTask( | |
| 106 FROM_HERE, | |
| 107 base::Bind(&SetupTask::InitializeIOThread, new SetupTask(this), | |
| 108 kThreadName)); | |
| 109 WaitForIOThreadCompletion(); | |
| 110 | |
| 111 render_thread_ = new RenderThreadImpl(kThreadName); | |
| 112 mock_process_->set_main_thread(render_thread_); | |
| 113 } | |
| 114 | |
| 115 void WebRTCAudioDeviceTest::TearDown() { | |
| 116 ChildProcess::current()->io_message_loop()->PostTask( | |
| 117 FROM_HERE, | |
| 118 base::Bind(&SetupTask::UninitializeIOThread, new SetupTask(this))); | |
| 119 WaitForIOThreadCompletion(); | |
| 120 mock_process_.reset(); | |
| 121 } | |
| 122 | |
| 123 bool WebRTCAudioDeviceTest::Send(IPC::Message* message) { | |
| 124 return channel_->Send(message); | |
| 125 } | |
| 126 | |
| 127 void WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) { | |
| 128 // Set the current thread as the IO thread. | |
| 129 io_thread_.reset(new content::TestBrowserThread(content::BrowserThread::IO, | |
| 130 MessageLoop::current())); | |
| 131 test_request_context_ = new TestURLRequestContext(); | |
| 132 resource_context_->set_request_context(test_request_context_.get()); | |
| 133 media_observer_.reset(new MockMediaObserver()); | |
| 134 resource_context_->set_media_observer(media_observer_.get()); | |
| 135 | |
| 136 CreateChannel(thread_name, resource_context_.get()); | |
| 137 } | |
| 138 | |
| 139 void WebRTCAudioDeviceTest::UninitializeIOThread() { | |
| 140 DestroyChannel(); | |
| 141 resource_context_.reset(); | |
| 142 test_request_context_ = NULL; | |
| 143 } | |
| 144 | |
| 145 void WebRTCAudioDeviceTest::CreateChannel(const char* name, | |
|
scherkus (not reviewing)
2011/11/08 22:24:46
param indentation is unusual: want to drop 1st par
tommi (sloooow) - chröme
2011/11/09 10:21:45
Done.
| |
| 146 content::ResourceContext* resource_context) { | |
| 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 148 audio_render_host_ = new AudioRendererHost(resource_context); | |
| 149 audio_render_host_->OnChannelConnected(base::GetCurrentProcId()); | |
| 150 | |
| 151 channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this)); | |
| 152 ASSERT_TRUE(channel_->Connect()); | |
| 153 | |
| 154 audio_render_host_->OnFilterAdded(channel_.get()); | |
| 155 } | |
| 156 | |
| 157 void WebRTCAudioDeviceTest::DestroyChannel() { | |
| 158 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 159 channel_.reset(); | |
| 160 audio_render_host_ = NULL; | |
| 161 } | |
| 162 | |
| 163 void WebRTCAudioDeviceTest::OnGetHardwareSampleRate(double* sample_rate) { | |
| 164 EXPECT_TRUE(audio_util_callback_); | |
| 165 *sample_rate = audio_util_callback_ ? | |
| 166 audio_util_callback_->GetAudioHardwareSampleRate() : 0.0; | |
| 167 } | |
| 168 | |
| 169 void WebRTCAudioDeviceTest::OnGetHardwareInputSampleRate(double* sample_rate) { | |
| 170 EXPECT_TRUE(audio_util_callback_); | |
| 171 *sample_rate = audio_util_callback_ ? | |
| 172 audio_util_callback_->GetAudioInputHardwareSampleRate() : 0.0; | |
| 173 } | |
| 174 | |
| 175 // IPC::Channel::Listener implementation. | |
| 176 bool WebRTCAudioDeviceTest::OnMessageReceived(const IPC::Message& message) { | |
| 177 if (render_thread_) { | |
| 178 IPC::ChannelProxy::MessageFilter* filter = | |
| 179 render_thread_->audio_input_message_filter(); | |
| 180 if (filter->OnMessageReceived(message)) | |
| 181 return true; | |
| 182 | |
| 183 filter = render_thread_->audio_message_filter(); | |
| 184 if (filter->OnMessageReceived(message)) | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 if (audio_render_host_.get()) { | |
| 189 bool message_was_ok = false; | |
| 190 if (audio_render_host_->OnMessageReceived(message, &message_was_ok)) | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 bool handled = true; | |
| 195 bool message_is_ok = true; | |
| 196 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCAudioDeviceTest, message, message_is_ok) | |
| 197 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareSampleRate, | |
| 198 OnGetHardwareSampleRate) | |
| 199 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareInputSampleRate, | |
| 200 OnGetHardwareInputSampleRate) | |
| 201 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 202 IPC_END_MESSAGE_MAP_EX() | |
| 203 | |
| 204 EXPECT_TRUE(message_is_ok); | |
| 205 | |
| 206 // We leave a DLOG as a hint to the developer in case important IPC messages | |
| 207 // are being dropped. | |
| 208 DLOG_IF(WARNING, !handled) << "Unhandled IPC message"; | |
| 209 | |
| 210 return true; | |
| 211 } | |
| 212 | |
| 213 // Posts a final task to the IO message loop and waits for completion. | |
| 214 void WebRTCAudioDeviceTest::WaitForIOThreadCompletion() { | |
| 215 ChildProcess::current()->io_message_loop()->PostTask( | |
| 216 FROM_HERE, new base::SignalingTask(&event_)); | |
| 217 EXPECT_TRUE(event_.TimedWait( | |
| 218 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); | |
| 219 } | |
| 220 | |
| 221 std::string WebRTCAudioDeviceTest::GetTestDataPath( | |
| 222 const FilePath::StringType& file_name) { | |
| 223 FilePath path; | |
| 224 EXPECT_TRUE(PathService::Get(content::DIR_TEST_DATA, &path)); | |
| 225 path = path.Append(file_name); | |
| 226 EXPECT_TRUE(file_util::PathExists(path)); | |
| 227 #ifdef OS_WIN | |
| 228 return WideToUTF8(path.value()); | |
| 229 #else | |
| 230 return path.value(); | |
| 231 #endif | |
| 232 } | |
| 233 | |
| 234 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network) | |
| 235 : network_(network) { | |
| 236 } | |
| 237 | |
| 238 WebRTCTransportImpl::~WebRTCTransportImpl() { | |
|
scherkus (not reviewing)
2011/11/08 22:24:46
nit: close one-liners to {}
tommi (sloooow) - chröme
2011/11/09 10:21:45
Done.
| |
| 239 } | |
| 240 | |
| 241 int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) { | |
| 242 ADD_FAILURE(); // We don't expect a call to this method in our tests. | |
| 243 return network_->ReceivedRTPPacket(channel, data, len); | |
| 244 } | |
| 245 | |
| 246 int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data, | |
| 247 int len) { | |
| 248 return network_->ReceivedRTCPPacket(channel, data, len); | |
| 249 } | |
| OLD | NEW |