| 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_ptr<media::VideoCaptureDevice::Client::Buffer> buffer; |
| 249 frame = device_->ReserveOutputBuffer(capture_resolution); | 249 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 250 ASSERT_TRUE(frame); | 250 capture_resolution).Pass(); |
| 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(frame, base::Time()); | 268 device_->OnIncomingCapturedBuffer( |
| 268 frame = NULL; | 269 buffer.Pass(), media::VideoFrame::I420, capture_resolution, base::Time()); |
| 269 | 270 |
| 270 base::RunLoop().RunUntilIdle(); | 271 base::RunLoop().RunUntilIdle(); |
| 271 Mock::VerifyAndClearExpectations(client_a_.get()); | 272 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 272 Mock::VerifyAndClearExpectations(client_b_.get()); | 273 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 273 | 274 |
| 274 // Second frame which ought to use the same shared memory buffer. In this case | 275 // Second buffer which ought to use the same shared memory buffer. In this |
| 275 // pretend that the VideoFrame pointer is held by the device for a long delay. | 276 // case pretend that the Buffer pointer is held by the device for a long |
| 276 // This shouldn't affect anything. | 277 // delay. This shouldn't affect anything. |
| 277 frame = device_->ReserveOutputBuffer(capture_resolution); | 278 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 278 ASSERT_TRUE(frame); | 279 capture_resolution).Pass(); |
| 279 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 280 ASSERT_TRUE(buffer); |
| 280 device_->OnIncomingCapturedVideoFrame(frame, base::Time()); | 281 memset(buffer->data(), buffer_no++, buffer->size()); |
| 282 device_->OnIncomingCapturedBuffer( |
| 283 buffer.Pass(), media::VideoFrame::I420, capture_resolution, base::Time()); |
| 281 | 284 |
| 282 // The buffer should be delivered to the clients in any order. | 285 // The buffer should be delivered to the clients in any order. |
| 283 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); | 286 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(1); |
| 284 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); | 287 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(1); |
| 285 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); | 288 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(1); |
| 286 base::RunLoop().RunUntilIdle(); | 289 base::RunLoop().RunUntilIdle(); |
| 287 Mock::VerifyAndClearExpectations(client_a_.get()); | 290 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 288 Mock::VerifyAndClearExpectations(client_b_.get()); | 291 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 289 frame = NULL; | |
| 290 | 292 |
| 291 // Add a fourth client now that some frames have come through. | 293 // Add a fourth client now that some buffers have come through. |
| 292 controller_->AddClient(client_b_route_2, client_b_.get(), | 294 controller_->AddClient(client_b_route_2, client_b_.get(), |
| 293 base::kNullProcessHandle, session_1); | 295 base::kNullProcessHandle, session_1); |
| 294 Mock::VerifyAndClearExpectations(client_b_.get()); | 296 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 295 | 297 |
| 296 // Third, fourth, and fifth frames. Pretend they all arrive at the same time. | 298 // Third, fourth, and fifth buffers. Pretend they all arrive at the same time. |
| 297 for (int i = 0; i < kPoolSize; i++) { | 299 for (int i = 0; i < kPoolSize; i++) { |
| 298 frame = device_->ReserveOutputBuffer(capture_resolution); | 300 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 299 ASSERT_TRUE(frame); | 301 capture_resolution).Pass(); |
| 300 ASSERT_EQ(media::VideoFrame::I420, frame->format()); | 302 ASSERT_TRUE(buffer); |
| 301 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 303 memset(buffer->data(), buffer_no++, buffer->size()); |
| 302 device_->OnIncomingCapturedVideoFrame(frame, base::Time()); | 304 device_->OnIncomingCapturedBuffer(buffer.Pass(), |
| 303 | 305 media::VideoFrame::I420, |
| 306 capture_resolution, |
| 307 base::Time()); |
| 304 } | 308 } |
| 305 // ReserveOutputBuffer ought to fail now, because the pool is depleted. | 309 // ReserveOutputBuffer ought to fail now, because the pool is depleted. |
| 306 ASSERT_FALSE(device_->ReserveOutputBuffer(capture_resolution)); | 310 ASSERT_FALSE(device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 311 capture_resolution)); |
| 307 | 312 |
| 308 // The new client needs to be told of 3 buffers; the old clients only 2. | 313 // The new client needs to be told of 3 buffers; the old clients only 2. |
| 309 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_2)).Times(kPoolSize); | 314 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_2)).Times(kPoolSize); |
| 310 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(kPoolSize); | 315 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(kPoolSize); |
| 311 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)) | 316 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_1)) |
| 312 .Times(kPoolSize - 1); | 317 .Times(kPoolSize - 1); |
| 313 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(kPoolSize); | 318 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_1)).Times(kPoolSize); |
| 314 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)) | 319 EXPECT_CALL(*client_a_, DoBufferCreated(client_a_route_2)) |
| 315 .Times(kPoolSize - 1); | 320 .Times(kPoolSize - 1); |
| 316 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(kPoolSize); | 321 EXPECT_CALL(*client_a_, DoBufferReady(client_a_route_2)).Times(kPoolSize); |
| 317 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)) | 322 EXPECT_CALL(*client_b_, DoBufferCreated(client_b_route_1)) |
| 318 .Times(kPoolSize - 1); | 323 .Times(kPoolSize - 1); |
| 319 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(kPoolSize); | 324 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_1)).Times(kPoolSize); |
| 320 base::RunLoop().RunUntilIdle(); | 325 base::RunLoop().RunUntilIdle(); |
| 321 Mock::VerifyAndClearExpectations(client_a_.get()); | 326 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 322 Mock::VerifyAndClearExpectations(client_b_.get()); | 327 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 323 | 328 |
| 324 // Now test the interaction of client shutdown and frame delivery. | 329 // Now test the interaction of client shutdown and buffer delivery. |
| 325 // Kill A1 via renderer disconnect (synchronous). | 330 // Kill A1 via renderer disconnect (synchronous). |
| 326 controller_->RemoveClient(client_a_route_1, client_a_.get()); | 331 controller_->RemoveClient(client_a_route_1, client_a_.get()); |
| 327 // Kill B1 via session close (posts a task to disconnect). | 332 // Kill B1 via session close (posts a task to disconnect). |
| 328 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); | 333 EXPECT_CALL(*client_b_, DoEnded(client_b_route_1)).Times(1); |
| 329 controller_->StopSession(300); | 334 controller_->StopSession(300); |
| 330 // Queue up another frame. | 335 // Queue up another buffer. |
| 331 frame = device_->ReserveOutputBuffer(capture_resolution); | 336 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 332 ASSERT_TRUE(frame); | 337 capture_resolution).Pass(); |
| 333 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 338 ASSERT_TRUE(buffer); |
| 334 device_->OnIncomingCapturedVideoFrame(frame, base::Time()); | 339 memset(buffer->data(), buffer_no++, buffer->size()); |
| 335 frame = device_->ReserveOutputBuffer(capture_resolution); | 340 device_->OnIncomingCapturedBuffer( |
| 341 buffer.Pass(), media::VideoFrame::I420, capture_resolution, base::Time()); |
| 342 buffer = device_->ReserveOutputBuffer(media::VideoFrame::I420, |
| 343 capture_resolution).Pass(); |
| 336 { | 344 { |
| 337 // Kill A2 via session close (posts a task to disconnect, but A2 must not | 345 // Kill A2 via session close (posts a task to disconnect, but A2 must not |
| 338 // be sent either of these two frames).. | 346 // be sent either of these two buffers). |
| 339 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); | 347 EXPECT_CALL(*client_a_, DoEnded(client_a_route_2)).Times(1); |
| 340 controller_->StopSession(200); | 348 controller_->StopSession(200); |
| 341 } | 349 } |
| 342 ASSERT_TRUE(frame); | 350 ASSERT_TRUE(buffer); |
| 343 media::FillYUV(frame, frame_no++, 0x22, 0x44); | 351 memset(buffer->data(), buffer_no++, buffer->size()); |
| 344 device_->OnIncomingCapturedVideoFrame(frame, base::Time()); | 352 device_->OnIncomingCapturedBuffer( |
| 353 buffer.Pass(), media::VideoFrame::I420, capture_resolution, base::Time()); |
| 345 // B2 is the only client left, and is the only one that should | 354 // B2 is the only client left, and is the only one that should |
| 346 // get the frame. | 355 // get the buffer. |
| 347 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(2); | 356 EXPECT_CALL(*client_b_, DoBufferReady(client_b_route_2)).Times(2); |
| 348 base::RunLoop().RunUntilIdle(); | 357 base::RunLoop().RunUntilIdle(); |
| 349 Mock::VerifyAndClearExpectations(client_a_.get()); | 358 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 350 Mock::VerifyAndClearExpectations(client_b_.get()); | 359 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 351 } | 360 } |
| 352 | 361 |
| 353 // Exercises the OnError() codepath of VideoCaptureController, and tests the | 362 // Exercises the OnError() codepath of VideoCaptureController, and tests the |
| 354 // behavior of various operations after the error state has been signalled. | 363 // behavior of various operations after the error state has been signalled. |
| 355 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { | 364 TEST_F(VideoCaptureControllerTest, ErrorBeforeDeviceCreation) { |
| 356 media::VideoCaptureParams session_100; | 365 media::VideoCaptureParams session_100; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 const VideoCaptureControllerID route_id(0x99); | 411 const VideoCaptureControllerID route_id(0x99); |
| 403 | 412 |
| 404 // Start with one client. | 413 // Start with one client. |
| 405 controller_->AddClient(route_id, client_a_.get(), | 414 controller_->AddClient(route_id, client_a_.get(), |
| 406 base::kNullProcessHandle, session_100); | 415 base::kNullProcessHandle, session_100); |
| 407 // OnFrameInfo from the VCD should become a no-op after the error occurs. | 416 // OnFrameInfo from the VCD should become a no-op after the error occurs. |
| 408 media::VideoCaptureCapability device_format( | 417 media::VideoCaptureCapability device_format( |
| 409 10, 10, 25, media::PIXEL_FORMAT_ARGB, | 418 10, 10, 25, media::PIXEL_FORMAT_ARGB, |
| 410 media::ConstantResolutionVideoCaptureDevice); | 419 media::ConstantResolutionVideoCaptureDevice); |
| 411 | 420 |
| 412 // Start the device. Then, before the first frame, signal an error and deliver | 421 // Start the device. Then, before the first buffer, signal an error and |
| 413 // the frame. The error should be propagated to clients; the frame should not | 422 // deliver the buffer. The error should be propagated to clients; the buffer |
| 414 // be. | 423 // should not be. |
| 415 base::RunLoop().RunUntilIdle(); | 424 base::RunLoop().RunUntilIdle(); |
| 416 Mock::VerifyAndClearExpectations(client_a_.get()); | 425 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 417 | 426 |
| 418 scoped_refptr<media::VideoFrame> frame = | 427 const gfx::Size dims(320, 240); |
| 419 device_->ReserveOutputBuffer(gfx::Size(320, 240)); | 428 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer( |
| 420 ASSERT_TRUE(frame); | 429 device_->ReserveOutputBuffer(media::VideoFrame::I420, dims)); |
| 430 ASSERT_TRUE(buffer); |
| 421 | 431 |
| 422 device_->OnError(); | 432 device_->OnError(); |
| 423 device_->OnIncomingCapturedVideoFrame(frame, base::Time()); | 433 device_->OnIncomingCapturedBuffer( |
| 424 frame = NULL; | 434 buffer.Pass(), media::VideoFrame::I420, dims, base::Time()); |
| 435 buffer.reset(); |
| 425 | 436 |
| 426 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); | 437 EXPECT_CALL(*client_a_, DoError(route_id)).Times(1); |
| 427 base::RunLoop().RunUntilIdle(); | 438 base::RunLoop().RunUntilIdle(); |
| 428 Mock::VerifyAndClearExpectations(client_a_.get()); | 439 Mock::VerifyAndClearExpectations(client_a_.get()); |
| 429 | 440 |
| 430 // Second client connects after the error state. It also should get told of | 441 // Second client connects after the error state. It also should get told of |
| 431 // the error. | 442 // the error. |
| 432 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); | 443 EXPECT_CALL(*client_b_, DoError(route_id)).Times(1); |
| 433 controller_->AddClient(route_id, client_b_.get(), | 444 controller_->AddClient(route_id, client_b_.get(), |
| 434 base::kNullProcessHandle, session_200); | 445 base::kNullProcessHandle, session_200); |
| 435 Mock::VerifyAndClearExpectations(client_b_.get()); | 446 Mock::VerifyAndClearExpectations(client_b_.get()); |
| 436 } | 447 } |
| 437 | 448 |
| 438 } // namespace content | 449 } // namespace content |
| OLD | NEW |