Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: content/browser/renderer_host/media/video_capture_host_unittest.cc

Issue 10662049: Move the device enumerate/open/close work to device thread from IO thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: minor fix for the problem detected by the trybots Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/process_util.h" 12 #include "base/process_util.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/test/test_timeouts.h"
15 #include "content/browser/browser_thread_impl.h" 17 #include "content/browser/browser_thread_impl.h"
16 #include "content/browser/renderer_host/media/media_stream_manager.h" 18 #include "content/browser/renderer_host/media/media_stream_manager.h"
17 #include "content/browser/renderer_host/media/video_capture_host.h" 19 #include "content/browser/renderer_host/media/video_capture_host.h"
18 #include "content/browser/renderer_host/media/video_capture_manager.h" 20 #include "content/browser/renderer_host/media/video_capture_manager.h"
19 #include "content/common/media/video_capture_messages.h" 21 #include "content/common/media/video_capture_messages.h"
20 #include "content/public/test/mock_resource_context.h" 22 #include "content/public/test/mock_resource_context.h"
21 #include "media/audio/audio_manager.h"
22 #include "media/video/capture/video_capture_types.h" 23 #include "media/video/capture/video_capture_types.h"
23 #include "net/url_request/url_request_context.h" 24 #include "net/url_request/url_request_context.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 using ::testing::_; 28 using ::testing::_;
28 using ::testing::AtLeast; 29 using ::testing::AtLeast;
29 using ::testing::AnyNumber; 30 using ::testing::AnyNumber;
30 using ::testing::DoAll; 31 using ::testing::DoAll;
31 using ::testing::InSequence; 32 using ::testing::InSequence;
33 using ::testing::InvokeWithoutArgs;
32 using ::testing::Mock; 34 using ::testing::Mock;
33 using ::testing::Return; 35 using ::testing::Return;
34 using content::BrowserThread; 36 using content::BrowserThread;
35 using content::BrowserThreadImpl; 37 using content::BrowserThreadImpl;
36 38
37 // Id used to identify the capture session between renderer and 39 // Id used to identify the capture session between renderer and
38 // video_capture_host. 40 // video_capture_host.
39 static const int kDeviceId = 1; 41 static const int kDeviceId = 1;
40 // Id of a video capture device 42 // Id of a video capture device
41 static const media::VideoCaptureSessionId kTestFakeDeviceId = 43 static const media::VideoCaptureSessionId kTestFakeDeviceId =
(...skipping 22 matching lines...) Expand all
64 } 66 }
65 } 67 }
66 68
67 private: 69 private:
68 file_util::ScopedFILE file_; 70 file_util::ScopedFILE file_;
69 int expected_size_; 71 int expected_size_;
70 }; 72 };
71 73
72 class MockVideoCaptureHost : public VideoCaptureHost { 74 class MockVideoCaptureHost : public VideoCaptureHost {
73 public: 75 public:
74 MockVideoCaptureHost(content::ResourceContext* resource_context, 76 MockVideoCaptureHost(content::ResourceContext* resource_context)
mflodman_chromium_OOO 2012/06/29 14:46:04 Explicit.
no longer working on chromium 2012/07/02 08:40:49 Done.
75 media::AudioManager* audio_manager) 77 : VideoCaptureHost(resource_context),
76 : VideoCaptureHost(resource_context, audio_manager),
77 return_buffers_(false), 78 return_buffers_(false),
78 dump_video_(false) {} 79 dump_video_(false) {}
79 80
80 // A list of mock methods. 81 // A list of mock methods.
81 MOCK_METHOD4(OnNewBufferCreated, 82 MOCK_METHOD4(OnNewBufferCreated,
82 void(int device_id, base::SharedMemoryHandle handle, 83 void(int device_id, base::SharedMemoryHandle handle,
83 int length, int buffer_id)); 84 int length, int buffer_id));
84 MOCK_METHOD3(OnBufferFilled, 85 MOCK_METHOD3(OnBufferFilled,
85 void(int device_id, int buffer_id, base::Time timestamp)); 86 void(int device_id, int buffer_id, base::Time timestamp));
86 MOCK_METHOD2(OnStateChanged, 87 MOCK_METHOD2(OnStateChanged,
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); 202 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
202 203
203 // ResourceContext must be created on the UI thread. 204 // ResourceContext must be created on the UI thread.
204 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, 205 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
205 message_loop_.get())); 206 message_loop_.get()));
206 207
207 // MediaStreamManager must be created on the IO thread. 208 // MediaStreamManager must be created on the IO thread.
208 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 209 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
209 message_loop_.get())); 210 message_loop_.get()));
210 211
211 audio_manager_.reset(media::AudioManager::Create());
212
213 #ifndef TEST_REAL_CAPTURE_DEVICE 212 #ifndef TEST_REAL_CAPTURE_DEVICE
214 media_stream::MediaStreamManager::GetForResourceContext( 213 media_stream::MediaStreamManager::GetForResourceContext(
215 &resource_context_, audio_manager_.get())->UseFakeDevice(); 214 &resource_context_)->UseFakeDevice();
216 #endif 215 #endif
217 216
218 host_ = new MockVideoCaptureHost(&resource_context_, audio_manager_.get()); 217 host_ = new MockVideoCaptureHost(&resource_context_);
219 218
220 // Simulate IPC channel connected. 219 // Simulate IPC channel connected.
221 host_->OnChannelConnected(base::GetCurrentProcId()); 220 host_->OnChannelConnected(base::GetCurrentProcId());
222 } 221 }
223 222
224 virtual void TearDown() { 223 virtual void TearDown() {
225 // Verifies and removes the expectations on host_ and 224 // Verifies and removes the expectations on host_ and
226 // returns true iff successful. 225 // returns true iff successful.
227 Mock::VerifyAndClearExpectations(host_); 226 Mock::VerifyAndClearExpectations(host_);
228 227
229 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 228 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
230 video_capture::kStopped)) 229 video_capture::kStopped))
231 .Times(AnyNumber()); 230 .Times(AnyNumber());
232 231
233 // Simulate closing the IPC channel. 232 // Simulate closing the IPC channel.
234 host_->OnChannelClosing(); 233 host_->OnChannelClosing();
235 234
236 // Release the reference to the mock object. The object will be destructed 235 // Release the reference to the mock object. The object will be destructed
237 // on message_loop_. 236 // on message_loop_.
238 host_ = NULL; 237 host_ = NULL;
239 238
240 // We need to continue running message_loop_ to complete all destructions. 239 // We need to continue running message_loop_ to complete all destructions.
241 SyncWithVideoCaptureManagerThread(); 240 message_loop_->RunAllPending();
242 }
243
244 // Called on the VideoCaptureManager thread.
245 static void PostQuitMessageLoop(MessageLoop* message_loop) {
246 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
247 }
248
249 // Called on the main thread.
250 static void PostQuitOnVideoCaptureManagerThread(
251 MessageLoop* message_loop, content::ResourceContext* resource_context,
252 media:: AudioManager* audio_manager) {
253 media_stream::MediaStreamManager* manager =
254 media_stream::MediaStreamManager::GetForResourceContext(
255 resource_context, audio_manager);
256 manager->video_capture_manager()->GetMessageLoop()->PostTask(
257 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop));
258 }
259
260 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the
261 // video_capture_manager thread are executed while also processing pending
262 // task in message_loop_ on the current thread. It is used to synchronize
263 // with the video capture manager thread when we are stopping a video
264 // capture device.
265 void SyncWithVideoCaptureManagerThread() {
266 message_loop_->PostTask(
267 FROM_HERE,
268 base::Bind(&PostQuitOnVideoCaptureManagerThread, message_loop_.get(),
269 &resource_context_, audio_manager_.get()));
270 message_loop_->Run();
271 } 241 }
272 242
273 void StartCapture() { 243 void StartCapture() {
274 InSequence s; 244 InSequence s;
275 // 1. First - get info about the new resolution 245 // 1. First - get info about the new resolution
276 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); 246 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId));
277 247
278 // 2. Change state to started 248 // 2. Change state to started
279 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 249 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
280 video_capture::kStarted)); 250 video_capture::kStarted));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 params.width = width; 286 params.width = width;
317 params.height = heigt; 287 params.height = heigt;
318 params.frame_per_second = frame_rate; 288 params.frame_per_second = frame_rate;
319 params.session_id = kTestFakeDeviceId; 289 params.session_id = kTestFakeDeviceId;
320 host_->SetDumpVideo(true); 290 host_->SetDumpVideo(true);
321 host_->OnStartCapture(kDeviceId, params); 291 host_->OnStartCapture(kDeviceId, params);
322 message_loop_->Run(); 292 message_loop_->Run();
323 } 293 }
324 294
325 void StopCapture() { 295 void StopCapture() {
296 base::WaitableEvent event(false, false);
326 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 297 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
327 video_capture::kStopped)) 298 video_capture::kStopped))
328 .Times(AtLeast(1)); 299 .WillOnce(InvokeWithoutArgs(&event, &base::WaitableEvent::Signal));
329 300
330 host_->OnStopCapture(kDeviceId); 301 host_->OnStopCapture(kDeviceId);
331 host_->SetReturnReceviedDibs(true); 302 host_->SetReturnReceviedDibs(true);
332 host_->ReturnReceivedDibs(kDeviceId); 303 host_->ReturnReceivedDibs(kDeviceId);
333 304
334 SyncWithVideoCaptureManagerThread(); 305 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_timeout()));
mflodman_chromium_OOO 2012/06/29 14:46:04 Not sure if there is a prefered Chromium way for t
no longer working on chromium 2012/07/02 08:40:49 Done.
306
335 host_->SetReturnReceviedDibs(false); 307 host_->SetReturnReceviedDibs(false);
336 // Expect the VideoCaptureDevice has been stopped 308 // Expect the VideoCaptureDevice has been stopped
337 EXPECT_EQ(0u, host_->entries_.size()); 309 EXPECT_EQ(0u, host_->entries_.size());
338 } 310 }
339 311
340 void NotifyPacketReady() { 312 void NotifyPacketReady() {
341 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) 313 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _))
342 .Times(AnyNumber()) 314 .Times(AnyNumber())
343 .WillOnce(ExitMessageLoop(message_loop_.get())) 315 .WillOnce(ExitMessageLoop(message_loop_.get()))
344 .RetiresOnSaturation(); 316 .RetiresOnSaturation();
345 message_loop_->Run(); 317 message_loop_->Run();
346 } 318 }
347 319
348 void ReturnReceivedPackets() { 320 void ReturnReceivedPackets() {
349 host_->ReturnReceivedDibs(kDeviceId); 321 host_->ReturnReceivedDibs(kDeviceId);
350 } 322 }
351 323
352 void SimulateError() { 324 void SimulateError() {
353 // Expect a change state to error state sent through IPC. 325 // Expect a change state to error state sent through IPC.
354 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 326 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
355 video_capture::kError)) 327 video_capture::kError))
356 .Times(1); 328 .Times(1);
357 VideoCaptureControllerID id(kDeviceId); 329 VideoCaptureControllerID id(kDeviceId);
358 host_->OnError(id); 330 host_->OnError(id);
359 SyncWithVideoCaptureManagerThread(); 331 // Wait for the error callback.
332 message_loop_->RunAllPending();
360 } 333 }
361 334
362 scoped_refptr<MockVideoCaptureHost> host_; 335 scoped_refptr<MockVideoCaptureHost> host_;
363 336
364 private: 337 private:
365 scoped_ptr<MessageLoop> message_loop_; 338 scoped_ptr<MessageLoop> message_loop_;
366 scoped_ptr<BrowserThreadImpl> ui_thread_; 339 scoped_ptr<BrowserThreadImpl> ui_thread_;
367 scoped_ptr<BrowserThreadImpl> io_thread_; 340 scoped_ptr<BrowserThreadImpl> io_thread_;
368 scoped_ptr<media::AudioManager> audio_manager_;
369 content::MockResourceContext resource_context_; 341 content::MockResourceContext resource_context_;
370 342
371 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); 343 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest);
372 }; 344 };
373 345
374 TEST_F(VideoCaptureHostTest, StartCapture) { 346 TEST_F(VideoCaptureHostTest, StartCapture) {
375 StartCapture(); 347 StartCapture();
376 } 348 }
377 349
378 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { 350 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) {
(...skipping 21 matching lines...) Expand all
400 } 372 }
401 373
402 #ifdef DUMP_VIDEO 374 #ifdef DUMP_VIDEO
403 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { 375 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) {
404 CaptureAndDumpVideo(640, 480, 30); 376 CaptureAndDumpVideo(640, 480, 30);
405 } 377 }
406 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { 378 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) {
407 CaptureAndDumpVideo(1280, 720, 30); 379 CaptureAndDumpVideo(1280, 720, 30);
408 } 380 }
409 #endif 381 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698