| 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 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/shared_memory.h" | 8 #include "base/memory/shared_memory.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 private: | 35 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); | 36 DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureMessageFilter); |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 class VideoCaptureImplTest : public ::testing::Test { | 39 class VideoCaptureImplTest : public ::testing::Test { |
| 40 public: | 40 public: |
| 41 class MockVideoCaptureImpl : public VideoCaptureImpl { | 41 class MockVideoCaptureImpl : public VideoCaptureImpl { |
| 42 public: | 42 public: |
| 43 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, | 43 MockVideoCaptureImpl(const media::VideoCaptureSessionId id, |
| 44 VideoCaptureMessageFilter* filter) | 44 VideoCaptureMessageFilter* filter) |
| 45 : VideoCaptureImpl(id, filter), received_buffer_count_(0) {} | 45 : VideoCaptureImpl(id, filter, base::ThreadTaskRunnerHandle::Get()), |
| 46 received_buffer_count_(0) {} |
| 46 ~MockVideoCaptureImpl() override {} | 47 ~MockVideoCaptureImpl() override {} |
| 47 | 48 |
| 48 // Override Send() to mimic device to send events. | 49 // Override Send() to mimic device to send events. |
| 49 void Send(IPC::Message* message) override { | 50 void Send(IPC::Message* message) override { |
| 50 CHECK(message); | 51 CHECK(message); |
| 51 | 52 |
| 52 // In this method, messages are sent to the according handlers as if | 53 // In this method, messages are sent to the according handlers as if |
| 53 // we are the device. | 54 // we are the device. |
| 54 bool handled = true; | 55 bool handled = true; |
| 55 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) | 56 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureImpl, *message) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 139 |
| 139 protected: | 140 protected: |
| 140 MOCK_METHOD2(OnFrameReady, | 141 MOCK_METHOD2(OnFrameReady, |
| 141 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); | 142 void(const scoped_refptr<media::VideoFrame>&, base::TimeTicks)); |
| 142 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); | 143 MOCK_METHOD1(OnStateUpdate, void(VideoCaptureState)); |
| 143 MOCK_METHOD1(OnDeviceFormatsInUse, | 144 MOCK_METHOD1(OnDeviceFormatsInUse, |
| 144 void(const media::VideoCaptureFormats&)); | 145 void(const media::VideoCaptureFormats&)); |
| 145 MOCK_METHOD1(OnDeviceSupportedFormats, | 146 MOCK_METHOD1(OnDeviceSupportedFormats, |
| 146 void(const media::VideoCaptureFormats&)); | 147 void(const media::VideoCaptureFormats&)); |
| 147 | 148 |
| 148 void Init() { | |
| 149 video_capture_impl_->Init(); | |
| 150 } | |
| 151 | |
| 152 void StartCapture(int client_id, const media::VideoCaptureParams& params) { | 149 void StartCapture(int client_id, const media::VideoCaptureParams& params) { |
| 153 video_capture_impl_->StartCapture( | 150 video_capture_impl_->StartCapture( |
| 154 client_id, params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, | 151 client_id, params, base::Bind(&VideoCaptureImplTest::OnStateUpdate, |
| 155 base::Unretained(this)), | 152 base::Unretained(this)), |
| 156 base::Bind(&VideoCaptureImplTest::OnFrameReady, | 153 base::Bind(&VideoCaptureImplTest::OnFrameReady, |
| 157 base::Unretained(this))); | 154 base::Unretained(this))); |
| 158 } | 155 } |
| 159 | 156 |
| 160 void StopCapture(int client_id) { | 157 void StopCapture(int client_id) { |
| 161 video_capture_impl_->StopCapture(client_id); | 158 video_capture_impl_->StopCapture(client_id); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 179 | 176 |
| 180 video_capture_impl_->OnBufferReceived( | 177 video_capture_impl_->OnBufferReceived( |
| 181 buffer_id, timestamp, metadata, media::PIXEL_FORMAT_I420, | 178 buffer_id, timestamp, metadata, media::PIXEL_FORMAT_I420, |
| 182 media::VideoFrame::STORAGE_SHMEM, size, gfx::Rect(size)); | 179 media::VideoFrame::STORAGE_SHMEM, size, gfx::Rect(size)); |
| 183 } | 180 } |
| 184 | 181 |
| 185 void BufferDestroyed(int buffer_id) { | 182 void BufferDestroyed(int buffer_id) { |
| 186 video_capture_impl_->OnBufferDestroyed(buffer_id); | 183 video_capture_impl_->OnBufferDestroyed(buffer_id); |
| 187 } | 184 } |
| 188 | 185 |
| 189 void DeInit() { | |
| 190 video_capture_impl_->DeInit(); | |
| 191 } | |
| 192 | |
| 193 void GetDeviceSupportedFormats() { | 186 void GetDeviceSupportedFormats() { |
| 194 const base::Callback<void(const media::VideoCaptureFormats&)> | 187 const base::Callback<void(const media::VideoCaptureFormats&)> |
| 195 callback = base::Bind( | 188 callback = base::Bind( |
| 196 &VideoCaptureImplTest::OnDeviceSupportedFormats, | 189 &VideoCaptureImplTest::OnDeviceSupportedFormats, |
| 197 base::Unretained(this)); | 190 base::Unretained(this)); |
| 198 video_capture_impl_->GetDeviceSupportedFormats(callback); | 191 video_capture_impl_->GetDeviceSupportedFormats(callback); |
| 199 } | 192 } |
| 200 | 193 |
| 201 void GetDeviceFormatsInUse() { | 194 void GetDeviceFormatsInUse() { |
| 202 const base::Callback<void(const media::VideoCaptureFormats&)> | 195 const base::Callback<void(const media::VideoCaptureFormats&)> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 216 | 209 |
| 217 private: | 210 private: |
| 218 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); | 211 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplTest); |
| 219 }; | 212 }; |
| 220 | 213 |
| 221 TEST_F(VideoCaptureImplTest, Simple) { | 214 TEST_F(VideoCaptureImplTest, Simple) { |
| 222 // Execute SetCapture() and StopCapture() for one client. | 215 // Execute SetCapture() and StopCapture() for one client. |
| 223 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 216 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 224 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 217 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
| 225 | 218 |
| 226 Init(); | |
| 227 StartCapture(0, params_small_); | 219 StartCapture(0, params_small_); |
| 228 StopCapture(0); | 220 StopCapture(0); |
| 229 DeInit(); | |
| 230 } | 221 } |
| 231 | 222 |
| 232 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { | 223 TEST_F(VideoCaptureImplTest, TwoClientsInSequence) { |
| 233 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 224 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
| 234 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 225 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 235 | 226 |
| 236 Init(); | |
| 237 StartCapture(0, params_small_); | 227 StartCapture(0, params_small_); |
| 238 StopCapture(0); | 228 StopCapture(0); |
| 239 StartCapture(1, params_small_); | 229 StartCapture(1, params_small_); |
| 240 StopCapture(1); | 230 StopCapture(1); |
| 241 DeInit(); | |
| 242 } | 231 } |
| 243 | 232 |
| 244 TEST_F(VideoCaptureImplTest, LargeAndSmall) { | 233 TEST_F(VideoCaptureImplTest, LargeAndSmall) { |
| 245 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 234 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
| 246 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 235 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 247 | 236 |
| 248 Init(); | |
| 249 StartCapture(0, params_large_); | 237 StartCapture(0, params_large_); |
| 250 StopCapture(0); | 238 StopCapture(0); |
| 251 StartCapture(1, params_small_); | 239 StartCapture(1, params_small_); |
| 252 StopCapture(1); | 240 StopCapture(1); |
| 253 DeInit(); | |
| 254 } | 241 } |
| 255 | 242 |
| 256 TEST_F(VideoCaptureImplTest, SmallAndLarge) { | 243 TEST_F(VideoCaptureImplTest, SmallAndLarge) { |
| 257 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 244 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
| 258 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 245 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 259 | 246 |
| 260 Init(); | |
| 261 StartCapture(0, params_small_); | 247 StartCapture(0, params_small_); |
| 262 StopCapture(0); | 248 StopCapture(0); |
| 263 StartCapture(1, params_large_); | 249 StartCapture(1, params_large_); |
| 264 StopCapture(1); | 250 StopCapture(1); |
| 265 DeInit(); | |
| 266 } | 251 } |
| 267 | 252 |
| 268 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the | 253 // Check that a request to GetDeviceSupportedFormats() ends up eventually in the |
| 269 // provided callback. | 254 // provided callback. |
| 270 TEST_F(VideoCaptureImplTest, GetDeviceFormats) { | 255 TEST_F(VideoCaptureImplTest, GetDeviceFormats) { |
| 271 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)); | 256 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)); |
| 272 | 257 |
| 273 Init(); | |
| 274 GetDeviceSupportedFormats(); | 258 GetDeviceSupportedFormats(); |
| 275 DeInit(); | |
| 276 } | 259 } |
| 277 | 260 |
| 278 // Check that two requests to GetDeviceSupportedFormats() end up eventually | 261 // Check that two requests to GetDeviceSupportedFormats() end up eventually |
| 279 // calling the provided callbacks. | 262 // calling the provided callbacks. |
| 280 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) { | 263 TEST_F(VideoCaptureImplTest, TwoClientsGetDeviceFormats) { |
| 281 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2); | 264 EXPECT_CALL(*this, OnDeviceSupportedFormats(_)).Times(2); |
| 282 | 265 |
| 283 Init(); | |
| 284 GetDeviceSupportedFormats(); | 266 GetDeviceSupportedFormats(); |
| 285 GetDeviceSupportedFormats(); | 267 GetDeviceSupportedFormats(); |
| 286 DeInit(); | |
| 287 } | 268 } |
| 288 | 269 |
| 289 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the | 270 // Check that a request to GetDeviceFormatsInUse() ends up eventually in the |
| 290 // provided callback. | 271 // provided callback. |
| 291 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { | 272 TEST_F(VideoCaptureImplTest, GetDeviceFormatsInUse) { |
| 292 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); | 273 EXPECT_CALL(*this, OnDeviceFormatsInUse(_)); |
| 293 | 274 |
| 294 Init(); | |
| 295 GetDeviceFormatsInUse(); | 275 GetDeviceFormatsInUse(); |
| 296 DeInit(); | |
| 297 } | 276 } |
| 298 | 277 |
| 299 TEST_F(VideoCaptureImplTest, BufferReceived) { | 278 TEST_F(VideoCaptureImplTest, BufferReceived) { |
| 300 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | 279 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); |
| 301 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | 280 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); |
| 302 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1); | 281 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(1); |
| 303 | 282 |
| 304 const gfx::Size size(1280, 720); | 283 const gfx::Size size(1280, 720); |
| 305 | 284 |
| 306 // Create a fake shared memory for buffer. | 285 // Create a fake shared memory for buffer. |
| 307 base::SharedMemory shm; | 286 base::SharedMemory shm; |
| 308 const size_t frame_size = media::VideoFrame::AllocationSize( | 287 const size_t frame_size = media::VideoFrame::AllocationSize( |
| 309 media::PIXEL_FORMAT_I420, size); | 288 media::PIXEL_FORMAT_I420, size); |
| 310 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); | 289 ASSERT_TRUE(shm.CreateAndMapAnonymous(frame_size)); |
| 311 | 290 |
| 312 media::VideoCaptureParams params; | 291 media::VideoCaptureParams params; |
| 313 params.requested_format = media::VideoCaptureFormat( | 292 params.requested_format = media::VideoCaptureFormat( |
| 314 size, 30, media::PIXEL_FORMAT_I420); | 293 size, 30, media::PIXEL_FORMAT_I420); |
| 315 | 294 |
| 316 Init(); | |
| 317 StartCapture(0, params); | 295 StartCapture(0, params); |
| 318 NewBuffer(0, shm); | 296 NewBuffer(0, shm); |
| 319 BufferReceived(0, size); | 297 BufferReceived(0, size); |
| 320 StopCapture(0); | 298 StopCapture(0); |
| 321 BufferDestroyed(0); | 299 BufferDestroyed(0); |
| 322 DeInit(); | |
| 323 } | 300 } |
| 324 | 301 |
| 325 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { | 302 TEST_F(VideoCaptureImplTest, BufferReceivedAfterStop) { |
| 326 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); | 303 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(1); |
| 327 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); | 304 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(1); |
| 328 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); | 305 EXPECT_CALL(*this, OnFrameReady(_, _)).Times(0); |
| 329 | 306 |
| 330 // Create a fake shared memory for buffer. | 307 // Create a fake shared memory for buffer. |
| 331 base::SharedMemory shm; | 308 base::SharedMemory shm; |
| 332 const size_t i420_frame_size = media::VideoFrame::AllocationSize( | 309 const size_t i420_frame_size = media::VideoFrame::AllocationSize( |
| 333 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); | 310 media::PIXEL_FORMAT_I420, params_large_.requested_format.frame_size); |
| 334 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); | 311 ASSERT_TRUE(shm.CreateAndMapAnonymous(i420_frame_size)); |
| 335 | 312 |
| 336 Init(); | |
| 337 StartCapture(0, params_large_); | 313 StartCapture(0, params_large_); |
| 338 NewBuffer(0, shm); | 314 NewBuffer(0, shm); |
| 339 StopCapture(0); | 315 StopCapture(0); |
| 340 BufferReceived(0, params_large_.requested_format.frame_size); | 316 BufferReceived(0, params_large_.requested_format.frame_size); |
| 341 BufferDestroyed(0); | 317 BufferDestroyed(0); |
| 342 DeInit(); | |
| 343 | 318 |
| 344 EXPECT_EQ(this->video_capture_impl_->received_buffer_count(), 1); | 319 EXPECT_EQ(this->video_capture_impl_->received_buffer_count(), 1); |
| 345 } | 320 } |
| 346 | 321 |
| 347 TEST_F(VideoCaptureImplTest, AlreadyStarted) { | 322 TEST_F(VideoCaptureImplTest, AlreadyStarted) { |
| 348 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); | 323 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)).Times(2); |
| 349 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); | 324 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)).Times(2); |
| 350 | 325 |
| 351 Init(); | |
| 352 StartCapture(0, params_small_); | 326 StartCapture(0, params_small_); |
| 353 StartCapture(1, params_large_); | 327 StartCapture(1, params_large_); |
| 354 StopCapture(0); | 328 StopCapture(0); |
| 355 StopCapture(1); | 329 StopCapture(1); |
| 356 DeInit(); | |
| 357 DCHECK(video_capture_impl_->capture_params().requested_format.frame_size == | 330 DCHECK(video_capture_impl_->capture_params().requested_format.frame_size == |
| 358 params_small_.requested_format.frame_size); | 331 params_small_.requested_format.frame_size); |
| 359 } | 332 } |
| 360 | 333 |
| 361 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { | 334 TEST_F(VideoCaptureImplTest, EndedBeforeStop) { |
| 362 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 335 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 363 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); | 336 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STOPPED)); |
| 364 | 337 |
| 365 Init(); | |
| 366 StartCapture(0, params_small_); | 338 StartCapture(0, params_small_); |
| 367 | 339 |
| 368 // Receive state change message from browser. | 340 // Receive state change message from browser. |
| 369 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); | 341 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ENDED); |
| 370 | 342 |
| 371 StopCapture(0); | 343 StopCapture(0); |
| 372 DeInit(); | |
| 373 } | 344 } |
| 374 | 345 |
| 375 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { | 346 TEST_F(VideoCaptureImplTest, ErrorBeforeStop) { |
| 376 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); | 347 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_STARTED)); |
| 377 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); | 348 EXPECT_CALL(*this, OnStateUpdate(VIDEO_CAPTURE_STATE_ERROR)); |
| 378 | 349 |
| 379 Init(); | |
| 380 StartCapture(0, params_small_); | 350 StartCapture(0, params_small_); |
| 381 | 351 |
| 382 // Receive state change message from browser. | 352 // Receive state change message from browser. |
| 383 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); | 353 video_capture_impl_->ReceiveStateChangeMessage(VIDEO_CAPTURE_STATE_ERROR); |
| 384 | 354 |
| 385 StopCapture(0); | 355 StopCapture(0); |
| 386 DeInit(); | |
| 387 } | 356 } |
| 388 | 357 |
| 389 } // namespace content | 358 } // namespace content |
| OLD | NEW |