OLD | NEW |
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 // Unit test for VideoCaptureController. | 5 // Unit test for VideoCaptureController. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 235 |
236 // Start with two clients. | 236 // Start with two clients. |
237 controller_->AddClient(client_a_route_1, client_a_.get(), | 237 controller_->AddClient(client_a_route_1, client_a_.get(), |
238 base::kNullProcessHandle, session_100); | 238 base::kNullProcessHandle, session_100); |
239 controller_->AddClient(client_b_route_1, client_b_.get(), | 239 controller_->AddClient(client_b_route_1, client_b_.get(), |
240 base::kNullProcessHandle, session_300); | 240 base::kNullProcessHandle, session_300); |
241 controller_->AddClient(client_a_route_2, client_a_.get(), | 241 controller_->AddClient(client_a_route_2, client_a_.get(), |
242 base::kNullProcessHandle, session_200); | 242 base::kNullProcessHandle, session_200); |
243 ASSERT_EQ(3, controller_->GetClientCount()); | 243 ASSERT_EQ(3, controller_->GetClientCount()); |
244 | 244 |
245 // Now, simulate an incoming captured frame from the capture device. As a side | 245 // Now, simulate an incoming captured buffer from the capture device. As a |
246 // effect this will cause the first buffer to be shared with clients. | 246 // side effect this will cause the first buffer to be shared with clients. |
247 uint8 frame_no = 1; | 247 uint8 buffer_no = 1; |
248 scoped_refptr<media::VideoFrame> frame; | 248 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer; |
249 frame = device_->ReserveOutputBuffer(capture_resolution); | 249 buffer = |
250 ASSERT_TRUE(frame); | 250 device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution); |
251 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 251 ASSERT_TRUE(buffer); |
| 252 memset(buffer->data(), buffer_no++, buffer->size()); |
252 { | 253 { |
253 InSequence s; | 254 InSequence s; |
254 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); | 255 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)).Times(1); |
255 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); | 256 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); |
256 } | 257 } |
257 { | 258 { |
258 InSequence s; | 259 InSequence s; |
259 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)).Times(1); | 260 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)).Times(1); |
260 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); | 261 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); |
261 } | 262 } |
262 { | 263 { |
263 InSequence s; | 264 InSequence s; |
264 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); | 265 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)).Times(1); |
265 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); | 266 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); |
266 } | 267 } |
267 device_->OnIncomingCapturedVideoFrame( | 268 device_->OnIncomingCapturedBuffer(buffer, |
268 frame, base::Time(), device_format.frame_rate); | 269 media::VideoFrame::I420, |
269 frame = NULL; | 270 capture_resolution, |
| 271 base::Time(), |
| 272 device_format.frame_rate); |
| 273 buffer = NULL; |
270 | 274 |
271 base::RunLoop().RunUntilIdle(); | 275 base::RunLoop().RunUntilIdle(); |
272 Mock::VerifyAndClearExpectations(client_a_.get()); | 276 Mock::VerifyAndClearExpectations(client_a_.get()); |
273 Mock::VerifyAndClearExpectations(client_b_.get()); | 277 Mock::VerifyAndClearExpectations(client_b_.get()); |
274 | 278 |
275 // Second frame which ought to use the same shared memory buffer. In this case | 279 // Second buffer which ought to use the same shared memory buffer. In this |
276 // pretend that the VideoFrame pointer is held by the device for a long delay. | 280 // case pretend that the Buffer pointer is held by the device for a long |
277 // This shouldn't affect anything. | 281 // delay. This shouldn't affect anything. |
278 frame = device_->ReserveOutputBuffer(capture_resolution); | 282 buffer = |
279 ASSERT_TRUE(frame); | 283 device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution); |
280 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 284 ASSERT_TRUE(buffer); |
281 device_->OnIncomingCapturedVideoFrame( | 285 memset(buffer->data(), buffer_no++, buffer->size()); |
282 frame, base::Time(), device_format.frame_rate); | 286 device_->OnIncomingCapturedBuffer(buffer, |
| 287 media::VideoFrame::I420, |
| 288 capture_resolution, |
| 289 base::Time(), |
| 290 device_format.frame_rate); |
| 291 buffer = NULL; |
283 | 292 |
284 // The buffer should be delivered to the clients in any order. | 293 // The buffer should be delivered to the clients in any order. |
285 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); | 294 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); |
286 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); | 295 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); |
287 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); | 296 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); |
288 base::RunLoop().RunUntilIdle(); | 297 base::RunLoop().RunUntilIdle(); |
289 Mock::VerifyAndClearExpectations(client_a_.get()); | 298 Mock::VerifyAndClearExpectations(client_a_.get()); |
290 Mock::VerifyAndClearExpectations(client_b_.get()); | 299 Mock::VerifyAndClearExpectations(client_b_.get()); |
291 frame = NULL; | |
292 | 300 |
293 // Add a fourth client now that some frames have come through. | 301 // Add a fourth client now that some buffers have come through. |
294 controller_->AddClient(client_b_route_2, client_b_.get(), | 302 controller_->AddClient(client_b_route_2, client_b_.get(), |
295 base::kNullProcessHandle, session_1); | 303 base::kNullProcessHandle, session_1); |
296 Mock::VerifyAndClearExpectations(client_b_.get()); | 304 Mock::VerifyAndClearExpectations(client_b_.get()); |
297 | 305 |
298 // Third, fourth, and fifth frames. Pretend they all arrive at the same time. | 306 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
299 for (int i = 0; i < kPoolSize; i++) { | 307 for (int i = 0; i < kPoolSize; i++) { |
300 frame = device_->ReserveOutputBuffer(capture_resolution); | 308 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
301 ASSERT_TRUE(frame); | 309 capture_resolution); |
302 ASSERT_EQ(media::VideoFrame::I420, frame->format()); | 310 ASSERT_TRUE(buffer); |
303 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 311 memset(buffer->data(), buffer_no++, buffer->size()); |
304 device_->OnIncomingCapturedVideoFrame( | 312 device_->OnIncomingCapturedBuffer(buffer, |
305 frame, base::Time(), device_format.frame_rate); | 313 media::VideoFrame::I420, |
| 314 capture_resolution, |
| 315 base::Time(), |
| 316 device_format.frame_rate); |
| 317 buffer = NULL; |
306 } | 318 } |
307 // ReserveOutputBuffer ought to fail now, because the pool is depleted. | 319 // ReserveOutputBuffer ought to fail now, because the pool is depleted. |
308 ASSERT_FALSE(device_->ReserveOutputBuffer(capture_resolution)); | 320 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 321 capture_resolution)); |
309 | 322 |
310 // The new client needs to be told of 3 buffers; the old clients only 2. | 323 // The new client needs to be told of 3 buffers; the old clients only 2. |
311 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_2)).Times(kPoolSize); | 324 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_2)).Times(kPoolSize); |
312 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(kPoolSize); | 325 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(kPoolSize); |
313 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)) | 326 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)) |
314 .Times(kPoolSize - 1); | 327 .Times(kPoolSize - 1); |
315 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(kPoolSize); | 328 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(kPoolSize); |
316 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)) | 329 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)) |
317 .Times(kPoolSize - 1); | 330 .Times(kPoolSize - 1); |
318 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(kPoolSize); | 331 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(kPoolSize); |
319 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)) | 332 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)) |
320 .Times(kPoolSize - 1); | 333 .Times(kPoolSize - 1); |
321 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(kPoolSize); | 334 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(kPoolSize); |
322 base::RunLoop().RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
323 Mock::VerifyAndClearExpectations(client_a_.get()); | 336 Mock::VerifyAndClearExpectations(client_a_.get()); |
324 Mock::VerifyAndClearExpectations(client_b_.get()); | 337 Mock::VerifyAndClearExpectations(client_b_.get()); |
325 | 338 |
326 // Now test the interaction of client shutdown and frame delivery. | 339 // Now test the interaction of client shutdown and buffer delivery. |
327 // Kill A1 via renderer disconnect (synchronous). | 340 // Kill A1 via renderer disconnect (synchronous). |
328 controller_->RemoveClient(client_a_route_1, client_a_.get()); | 341 controller_->RemoveClient(client_a_route_1, client_a_.get()); |
329 // Kill B1 via session close (posts a task to disconnect). | 342 // Kill B1 via session close (posts a task to disconnect). |
330 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); | 343 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); |
331 controller_->StopSession(300); | 344 controller_->StopSession(300); |
332 // Queue up another frame. | 345 // Queue up another buffer. |
333 frame = device_->ReserveOutputBuffer(capture_resolution); | 346 buffer = |
334 ASSERT_TRUE(frame); | 347 device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution); |
335 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 348 ASSERT_TRUE(buffer); |
336 device_->OnIncomingCapturedVideoFrame( | 349 memset(buffer->data(), buffer_no++, buffer->size()); |
337 frame, base::Time(), device_format.frame_rate); | 350 device_->OnIncomingCapturedBuffer(buffer, |
338 frame = device_->ReserveOutputBuffer(capture_resolution); | 351 media::VideoFrame::I420, |
| 352 capture_resolution, |
| 353 base::Time(), |
| 354 device_format.frame_rate); |
| 355 buffer = NULL; |
| 356 buffer = |
| 357 device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution); |
339 { | 358 { |
340 // Kill A2 via session close (posts a task to disconnect, but A2 must not | 359 // Kill A2 via session close (posts a task to disconnect, but A2 must not |
341 // be sent either of these two frames).. | 360 // be sent either of these two buffers). |
342 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); | 361 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); |
343 controller_->StopSession(200); | 362 controller_->StopSession(200); |
344 } | 363 } |
345 ASSERT_TRUE(frame); | 364 ASSERT_TRUE(buffer); |
346 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 365 memset(buffer->data(), buffer_no++, buffer->size()); |
347 device_->OnIncomingCapturedVideoFrame( | 366 device_->OnIncomingCapturedBuffer(buffer, |
348 frame, base::Time(), device_format.frame_rate); | 367 media::VideoFrame::I420, |
| 368 capture_resolution, |
| 369 base::Time(), |
| 370 device_format.frame_rate); |
| 371 buffer = NULL; |
349 // B2 is the only client left, and is the only one that should | 372 // B2 is the only client left, and is the only one that should |
350 // get the frame. | 373 // get the buffer. |
351 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(2); | 374 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(2); |
352 base::RunLoop().RunUntilIdle(); | 375 base::RunLoop().RunUntilIdle(); |
353 Mock::VerifyAndClearExpectations(client_a_.get()); | 376 Mock::VerifyAndClearExpectations(client_a_.get()); |
354 Mock::VerifyAndClearExpectations(client_b_.get()); | 377 Mock::VerifyAndClearExpectations(client_b_.get()); |
355 } | 378 } |
356 | 379 |
357 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 380 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
358 // behavior of various operations after the error state has been signalled. | 381 // behavior of various operations after the error state has been signalled. |
359 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { | 382 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { |
360 media::VideoCaptureParams session_100; | 383 media::VideoCaptureParams session_100; |
361 session_100.session_id = 100; | 384 session_100.session_id = 100; |
362 session_100.requested_format = media::VideoCaptureFormat( | 385 session_100.requested_format = media::VideoCaptureFormat( |
363 320, 240, 30, media::ConstantResolutionVideoCaptureDevice); | 386 320, 240, 30, media::ConstantResolutionVideoCaptureDevice); |
364 | 387 |
365 media::VideoCaptureParams session_200 = session_100; | 388 media::VideoCaptureParams session_200 = session_100; |
366 session_200.session_id = 200; | 389 session_200.session_id = 200; |
367 | 390 |
| 391 const gfx::Size capture_resolution(320, 240); |
| 392 |
368 const VideoCaptureControllerID route_id(0x99); | 393 const VideoCaptureControllerID route_id(0x99); |
369 | 394 |
370 // Start with one client. | 395 // Start with one client. |
371 controller_->AddClient(route_id, client_a_.get(), | 396 controller_->AddClient(route_id, client_a_.get(), |
372 base::kNullProcessHandle, session_100); | 397 base::kNullProcessHandle, session_100); |
373 device_->OnError(); | 398 device_->OnError(); |
374 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); | 399 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); |
375 base::RunLoop().RunUntilIdle(); | 400 base::RunLoop().RunUntilIdle(); |
376 Mock::VerifyAndClearExpectations(client_a_.get()); | 401 Mock::VerifyAndClearExpectations(client_a_.get()); |
377 | 402 |
378 // Second client connects after the error state. It also should get told of | 403 // Second client connects after the error state. It also should get told of |
379 // the error. | 404 // the error. |
380 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 405 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
381 controller_->AddClient(route_id, client_b_.get(), | 406 controller_->AddClient(route_id, client_b_.get(), |
382 base::kNullProcessHandle, session_200); | 407 base::kNullProcessHandle, session_200); |
383 base::RunLoop().RunUntilIdle(); | 408 base::RunLoop().RunUntilIdle(); |
384 Mock::VerifyAndClearExpectations(client_b_.get()); | 409 Mock::VerifyAndClearExpectations(client_b_.get()); |
385 | 410 |
386 scoped_refptr<media::VideoFrame> frame = | 411 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer = |
387 device_->ReserveOutputBuffer(gfx::Size(320, 240)); | 412 device_->ReserveOutputBuffer(media::VideoFrame::I420, capture_resolution); |
388 ASSERT_TRUE(frame); | 413 ASSERT_TRUE(buffer); |
389 | 414 |
390 device_->OnIncomingCapturedVideoFrame(frame, base::Time(), 30); | 415 device_->OnIncomingCapturedBuffer( |
| 416 buffer, media::VideoFrame::I420, capture_resolution, base::Time(), 30); |
| 417 buffer = NULL; |
391 | 418 |
392 base::RunLoop().RunUntilIdle(); | 419 base::RunLoop().RunUntilIdle(); |
393 } | 420 } |
394 | 421 |
395 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 422 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
396 // behavior of various operations after the error state has been signalled. | 423 // behavior of various operations after the error state has been signalled. |
397 TEST_F(VideoCaptureControllerTest, ErrorAfterDeviceCreation) { | 424 TEST_F(VideoCaptureControllerTest, ErrorAfterDeviceCreation) { |
398 media::VideoCaptureParams session_100; | 425 media::VideoCaptureParams session_100; |
399 session_100.session_id = 100; | 426 session_100.session_id = 100; |
400 session_100.requested_format = media::VideoCaptureFormat( | 427 session_100.requested_format = media::VideoCaptureFormat( |
401 320, 240, 30, media::ConstantResolutionVideoCaptureDevice); | 428 320, 240, 30, media::ConstantResolutionVideoCaptureDevice); |
402 | 429 |
403 media::VideoCaptureParams session_200 = session_100; | 430 media::VideoCaptureParams session_200 = session_100; |
404 session_200.session_id = 200; | 431 session_200.session_id = 200; |
405 | 432 |
406 const VideoCaptureControllerID route_id(0x99); | 433 const VideoCaptureControllerID route_id(0x99); |
407 | 434 |
408 // Start with one client. | 435 // Start with one client. |
409 controller_->AddClient(route_id, client_a_.get(), | 436 controller_->AddClient(route_id, client_a_.get(), |
410 base::kNullProcessHandle, session_100); | 437 base::kNullProcessHandle, session_100); |
411 // OnFrameInfo from the VCD should become a no-op after the error occurs. | 438 // OnFrameInfo from the VCD should become a no-op after the error occurs. |
412 media::VideoCaptureCapability device_format( | 439 media::VideoCaptureCapability device_format( |
413 10, 10, 25, media::PIXEL_FORMAT_ARGB, | 440 10, 10, 25, media::PIXEL_FORMAT_ARGB, |
414 media::ConstantResolutionVideoCaptureDevice); | 441 media::ConstantResolutionVideoCaptureDevice); |
415 | 442 |
416 // Start the device. Then, before the first frame, signal an error and deliver | 443 // Start the device. Then, before the first buffer, signal an error and |
417 // the frame. The error should be propagated to clients; the frame should not | 444 // deliver the buffer. The error should be propagated to clients; the buffer |
418 // be. | 445 // should not be. |
419 base::RunLoop().RunUntilIdle(); | 446 base::RunLoop().RunUntilIdle(); |
420 Mock::VerifyAndClearExpectations(client_a_.get()); | 447 Mock::VerifyAndClearExpectations(client_a_.get()); |
421 | 448 |
422 scoped_refptr<media::VideoFrame> frame = | 449 const gfx::Size dims(320, 240); |
423 device_->ReserveOutputBuffer(gfx::Size(320, 240)); | 450 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> buffer = |
424 ASSERT_TRUE(frame); | 451 device_->ReserveOutputBuffer(media::VideoFrame::I420, dims); |
| 452 ASSERT_TRUE(buffer); |
425 | 453 |
426 device_->OnError(); | 454 device_->OnError(); |
427 device_->OnIncomingCapturedVideoFrame( | 455 device_->OnIncomingCapturedBuffer(buffer, |
428 frame, base::Time(), device_format.frame_rate); | 456 media::VideoFrame::I420, |
429 frame = NULL; | 457 dims, |
| 458 base::Time(), |
| 459 device_format.frame_rate); |
| 460 buffer = NULL; |
430 | 461 |
431 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); | 462 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); |
432 base::RunLoop().RunUntilIdle(); | 463 base::RunLoop().RunUntilIdle(); |
433 Mock::VerifyAndClearExpectations(client_a_.get()); | 464 Mock::VerifyAndClearExpectations(client_a_.get()); |
434 | 465 |
435 // Second client connects after the error state. It also should get told of | 466 // Second client connects after the error state. It also should get told of |
436 // the error. | 467 // the error. |
437 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 468 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
438 controller_->AddClient(route_id, client_b_.get(), | 469 controller_->AddClient(route_id, client_b_.get(), |
439 base::kNullProcessHandle, session_200); | 470 base::kNullProcessHandle, session_200); |
440 Mock::VerifyAndClearExpectations(client_b_.get()); | 471 Mock::VerifyAndClearExpectations(client_b_.get()); |
441 } | 472 } |
442 | 473 |
443 } // namespace content | 474 } // namespace content |
OLD | NEW |