|
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/message_loop.h" | |
9 #include "base/synchronization/waitable_event.h" | |
10 #include "base/test/signaling_task.h" | |
11 #include "base/test/test_timeouts.h" | |
12 #include "content/browser/renderer_host/media/audio_renderer_host.h" | |
13 #include "content/browser/renderer_host/media/mock_media_observer.h" | |
14 #include "content/browser/resource_context.h" | |
15 #include "content/common/view_messages.h" | |
16 #include "content/public/common/content_paths.h" | |
17 #include "content/renderer/media/webrtc_audio_device_impl.h" | |
18 #include "content/renderer/render_process.h" | |
19 #include "content/renderer/render_thread_impl.h" | |
20 #include "content/test/test_browser_thread.h" | |
21 #include "net/url_request/url_request_test_util.h" | |
22 #include "testing/gmock/include/gmock/gmock.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 #include "third_party/webrtc/voice_engine/main/interface/voe_audio_processing.h" | |
25 #include "third_party/webrtc/voice_engine/main/interface/voe_base.h" | |
26 #include "third_party/webrtc/voice_engine/main/interface/voe_file.h" | |
27 #include "third_party/webrtc/voice_engine/main/interface/voe_network.h" | |
28 | |
29 using testing::_; | |
30 using testing::InvokeWithoutArgs; | |
31 using testing::Return; | |
32 using testing::StrEq; | |
33 | |
34 // This class is a mock of the child process singleton which is needed | |
35 // to be able to create a RenderThread object. | |
36 class WebRTCMockRenderProcess : public RenderProcess { | |
37 public: | |
38 WebRTCMockRenderProcess() {} | |
39 virtual ~WebRTCMockRenderProcess() {} | |
40 | |
41 // RenderProcess implementation. | |
42 virtual skia::PlatformCanvas* GetDrawingCanvas(TransportDIB** memory, | |
43 const gfx::Rect& rect) { return NULL; } | |
44 virtual void ReleaseTransportDIB(TransportDIB* memory) {} | |
45 virtual bool UseInProcessPlugins() const { return false; } | |
46 virtual bool HasInitializedMediaLibrary() const { return false; } | |
47 | |
48 private: | |
49 DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess); | |
50 }; | |
51 | |
52 class ReplaceContentClientRenderer { | |
henrika (OOO until Aug 14)
2011/11/04 10:35:12
Comment?
tommi (sloooow) - chröme
2011/11/07 10:27:28
The comment was in the header file but I moved it
| |
53 public: | |
54 ReplaceContentClientRenderer(content::ContentRendererClient* new_renderer) { | |
55 saved_renderer_ = content::GetContentClient()->renderer(); | |
56 content::GetContentClient()->set_renderer(new_renderer); | |
57 } | |
58 ~ReplaceContentClientRenderer() { | |
59 // Restore the original renderer. | |
60 content::GetContentClient()->set_renderer(saved_renderer_); | |
61 } | |
62 private: | |
63 content::ContentRendererClient* saved_renderer_; | |
64 DISALLOW_COPY_AND_ASSIGN(ReplaceContentClientRenderer); | |
65 }; | |
66 | |
67 namespace { | |
68 | |
69 class WebRTCMockResourceContext : public content::ResourceContext { | |
70 public: | |
71 WebRTCMockResourceContext() {} | |
72 virtual ~WebRTCMockResourceContext() {} | |
73 virtual void EnsureInitialized() const OVERRIDE {} | |
74 }; | |
75 | |
76 ACTION_P(QuitMessageLoop, loop_or_proxy) { | |
77 loop_or_proxy->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
78 } | |
79 | |
80 } // end namespace | |
81 | |
82 WebRTCAudioDeviceTest::WebRTCAudioDeviceTest() | |
83 : render_thread_(NULL), event_(false, false), audio_util_callback_(NULL) {} | |
84 WebRTCAudioDeviceTest::~WebRTCAudioDeviceTest() {} | |
85 | |
86 void WebRTCAudioDeviceTest::SetUp() { | |
87 // Set low latency mode, as it soon would be on by default. | |
henrika (OOO until Aug 14)
2011/11/04 10:35:12
Don't think you need this part at all, right?
tommi (sloooow) - chröme
2011/11/07 10:27:28
Right - and thanks for catching. Removed.
| |
88 if (AudioRendererImpl::latency_type() != AudioRendererImpl::kLowLatency) | |
89 AudioRendererImpl::set_latency_type(AudioRendererImpl::kLowLatency); | |
90 | |
91 ASSERT_EQ(AudioRendererImpl::kLowLatency, | |
92 AudioRendererImpl::latency_type()); | |
93 | |
94 // This part sets up a RenderThread environment to ensure that | |
95 // RenderThread::current() (<=> TLS pointer) is valid. | |
96 // Main parts are inspired by the RenderViewFakeResourcesTest. | |
97 // Note that, the IPC part is not utilized in this test. | |
98 saved_content_renderer_.reset( | |
99 new ReplaceContentClientRenderer(&mock_content_renderer_client_)); | |
100 mock_process_.reset(new WebRTCMockRenderProcess()); | |
101 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
102 MessageLoop::current())); | |
103 | |
104 // Construct the resource context on the UI thread. | |
105 resource_context_.reset(new WebRTCMockResourceContext()); | |
106 | |
107 static const char kThreadName[] = "RenderThread"; | |
108 ChildProcess::current()->io_message_loop()->PostTask( | |
109 FROM_HERE, | |
110 base::Bind(&SetupTask::InitializeIOThread, new SetupTask(this), | |
111 kThreadName)); | |
112 WaitForIOThreadCompletion(); | |
113 | |
114 render_thread_ = new RenderThreadImpl(kThreadName); | |
115 mock_process_->set_main_thread(render_thread_); | |
116 } | |
117 | |
118 void WebRTCAudioDeviceTest::TearDown() { | |
119 ChildProcess::current()->io_message_loop()->PostTask( | |
120 FROM_HERE, | |
121 base::Bind(&SetupTask::UninitializeIOThread, new SetupTask(this))); | |
122 WaitForIOThreadCompletion(); | |
123 mock_process_.reset(); | |
124 } | |
125 | |
126 bool WebRTCAudioDeviceTest::Send(IPC::Message* message) { | |
127 return channel_->Send(message); | |
128 } | |
129 | |
130 void WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) { | |
131 // Set the current thread as the IO thread. | |
132 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, | |
133 MessageLoop::current())); | |
134 test_request_context_ = new TestURLRequestContext(); | |
135 resource_context_->set_request_context(test_request_context_.get()); | |
136 media_observer_.reset(new MockMediaObserver()); | |
137 resource_context_->set_media_observer(media_observer_.get()); | |
138 | |
139 CreateChannel(thread_name, resource_context_.get()); | |
140 } | |
141 | |
142 void WebRTCAudioDeviceTest::UninitializeIOThread() { | |
143 DestroyChannel(); | |
144 resource_context_.reset(); | |
145 test_request_context_ = NULL; | |
146 } | |
147 | |
148 void WebRTCAudioDeviceTest::CreateChannel(const char* name, | |
149 content::ResourceContext* resource_context) { | |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
151 audio_render_host_ = new AudioRendererHost(resource_context); | |
152 audio_render_host_->OnChannelConnected(base::GetCurrentProcId()); | |
153 | |
154 channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this)); | |
155 ASSERT_TRUE(channel_->Connect()); | |
156 | |
157 audio_render_host_->OnFilterAdded(channel_.get()); | |
158 } | |
159 | |
160 void WebRTCAudioDeviceTest::DestroyChannel() { | |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
162 channel_.reset(); | |
163 audio_render_host_ = NULL; | |
164 } | |
165 | |
166 void WebRTCAudioDeviceTest::OnGetHardwareSampleRate(double* sample_rate) { | |
167 EXPECT_TRUE(audio_util_callback_); | |
168 *sample_rate = audio_util_callback_ ? | |
169 audio_util_callback_->GetAudioHardwareSampleRate() : 0.0; | |
170 } | |
171 | |
172 void WebRTCAudioDeviceTest::OnGetHardwareInputSampleRate(double* sample_rate) { | |
173 EXPECT_TRUE(audio_util_callback_); | |
174 *sample_rate = audio_util_callback_ ? | |
175 audio_util_callback_->GetAudioInputHardwareSampleRate() : 0.0; | |
176 } | |
177 | |
178 // IPC::Channel::Listener implementation. | |
179 bool WebRTCAudioDeviceTest::OnMessageReceived(const IPC::Message& message) { | |
180 if (render_thread_) { | |
181 IPC::ChannelProxy::MessageFilter* filter = | |
182 render_thread_->audio_input_message_filter(); | |
183 if (filter->OnMessageReceived(message)) | |
184 return true; | |
185 | |
186 filter = render_thread_->audio_message_filter(); | |
187 if (filter->OnMessageReceived(message)) | |
188 return true; | |
189 } | |
190 | |
191 if (audio_render_host_.get()) { | |
192 bool message_was_ok = false; | |
193 if (audio_render_host_->OnMessageReceived(message, &message_was_ok)) | |
194 return true; | |
195 } | |
196 | |
197 bool handled = true; | |
198 bool message_is_ok = true; | |
199 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCAudioDeviceTest, message, message_is_ok) | |
200 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareSampleRate, | |
201 OnGetHardwareSampleRate) | |
202 IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareInputSampleRate, | |
203 OnGetHardwareInputSampleRate) | |
204 IPC_MESSAGE_UNHANDLED(handled = false) | |
205 IPC_END_MESSAGE_MAP_EX() | |
206 | |
207 EXPECT_TRUE(message_is_ok); | |
208 | |
209 // In case tests stop working, we leave a DLOG as a hint to the developer | |
210 // in case important IPC messages are being dropped. | |
211 DLOG_IF(WARNING, !handled) << "Unhandled IPC message"; | |
212 | |
213 return true; | |
214 } | |
215 | |
216 // Posts a final task to the IO message loop and waits for completion. | |
217 void WebRTCAudioDeviceTest::WaitForIOThreadCompletion() { | |
218 ChildProcess::current()->io_message_loop()->PostTask( | |
219 FROM_HERE, new base::SignalingTask(&event_)); | |
220 EXPECT_TRUE(event_.TimedWait( | |
221 base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); | |
222 } | |
223 | |
224 std::string WebRTCAudioDeviceTest::GetTestDataPath( | |
225 const FilePath::StringType& file_name) { | |
226 FilePath path; | |
227 EXPECT_TRUE(PathService::Get(content::DIR_TEST_DATA, &path)); | |
228 path = path.Append(file_name); | |
229 #ifdef OS_WIN | |
230 return WideToUTF8(path.value()); | |
231 #else | |
232 return path.value(); | |
233 #endif | |
234 } | |
235 | |
236 void WebRTCAudioDeviceTest::PlayLocalFile(int duration) { | |
237 EXPECT_GE(duration, 0); | |
238 EXPECT_CALL(media_observer(), | |
239 OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); | |
240 | |
241 EXPECT_CALL(media_observer(), | |
242 OnSetAudioStreamPlaying(_, 1, true)).Times(1); | |
243 | |
244 // When the "closed" event is triggered, we end the test. | |
245 EXPECT_CALL(media_observer(), | |
246 OnSetAudioStreamStatus(_, 1, StrEq("closed"))) | |
247 .WillOnce(QuitMessageLoop(message_loop_.message_loop_proxy())); | |
248 | |
249 EXPECT_CALL(media_observer(), | |
250 OnDeleteAudioStream(_, 1)).Times(1); | |
251 | |
252 scoped_refptr<WebRtcAudioDeviceImpl> audio_device( | |
253 new WebRtcAudioDeviceImpl()); | |
254 audio_device->SetSessionId(1); | |
255 | |
256 WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); | |
257 | |
258 ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); | |
259 int err = base->Init(audio_device); | |
260 EXPECT_EQ(0, err); | |
261 if (err == 0) { | |
262 ScopedWebRTCPtr<webrtc::VoEAudioProcessing> audio_processing(engine.get()); | |
henrika (OOO until Aug 14)
2011/11/04 10:35:12
An AGC is not needed to play out a file. I underst
tommi (sloooow) - chröme
2011/11/07 10:27:28
Done.
| |
263 EXPECT_EQ(0, audio_processing->SetAgcStatus(true, | |
264 webrtc::kAgcAdaptiveDigital)); | |
265 | |
266 int ch = base->CreateChannel(); | |
267 EXPECT_NE(-1, ch); | |
268 | |
269 ScopedWebRTCPtr<webrtc::VoENetwork> network(engine.get()); | |
henrika (OOO until Aug 14)
2011/11/04 10:35:12
webrtc::VoENetwork is not needed either since we a
tommi (sloooow) - chröme
2011/11/07 10:27:28
Done.
| |
270 scoped_ptr<WebRTCTransportImpl> transport( | |
271 new WebRTCTransportImpl(network.get())); | |
272 EXPECT_EQ(0, network->RegisterExternalTransport(ch, *transport.get())); | |
273 EXPECT_EQ(0, base->StartReceive(ch)); | |
274 EXPECT_EQ(0, base->StartPlayout(ch)); | |
275 EXPECT_EQ(0, base->StartSend(ch)); | |
276 | |
277 std::string file_path( | |
278 GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); | |
279 | |
280 ScopedWebRTCPtr<webrtc::VoEFile> file(engine.get()); | |
281 if (duration == 0) { | |
282 EXPECT_EQ(0, file->GetFileDuration(file_path.c_str(), duration, | |
283 webrtc::kFileFormatPcm16kHzFile)); | |
284 EXPECT_NE(0, duration); | |
285 } | |
286 | |
287 EXPECT_EQ(0, file->StartPlayingFileLocally(ch, file_path.c_str(), false, | |
288 webrtc::kFileFormatPcm16kHzFile)); | |
289 | |
290 message_loop_.PostDelayedTask(FROM_HERE, | |
291 new MessageLoop::QuitTask(), duration); | |
292 message_loop_.Run(); | |
293 | |
294 EXPECT_EQ(0, network->DeRegisterExternalTransport(ch)); | |
295 } | |
296 } | |
297 | |
298 WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network) | |
299 : network_(network) { | |
300 } | |
301 | |
302 WebRTCTransportImpl::~WebRTCTransportImpl() { | |
303 } | |
304 | |
305 int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) { | |
henrika (OOO until Aug 14)
2011/11/04 10:35:12
Can be removed.
tommi (sloooow) - chröme
2011/11/07 10:27:28
It's a pure virtual method that must be implemente
henrika (OOO until Aug 14)
2011/11/07 16:40:16
Fine with me.
| |
306 return network_->ReceivedRTPPacket(channel, data, len); | |
307 } | |
308 | |
309 int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data, | |
310 int len) { | |
311 return network_->ReceivedRTCPPacket(channel, data, len); | |
312 } | |
OLD | NEW |