| 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 "content/browser/renderer_host/media/video_capture_manager.h" | 5 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 163 } |
| 164 | 164 |
| 165 } // namespace | 165 } // namespace |
| 166 | 166 |
| 167 namespace content { | 167 namespace content { |
| 168 | 168 |
| 169 // Instances of this struct go through several different phases during their | 169 // Instances of this struct go through several different phases during their |
| 170 // lifetime. | 170 // lifetime. |
| 171 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of | 171 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of |
| 172 // only the |video_capture_controller|. Clients can already connect to the | 172 // only the |video_capture_controller|. Clients can already connect to the |
| 173 // controller, but there is no |buffer_pool| or |video_capture_device| present. | 173 // controller, but there is no |video_capture_device| present. |
| 174 // Phase 2: When a request to "start" the entry comes in (via | 174 // Phase 2: When a request to "start" the entry comes in (via |
| 175 // HandleQueuedStartRequest()), |buffer_pool| is created and creation of | 175 // HandleQueuedStartRequest()), creation of |video_capture_device| is scheduled |
| 176 // |video_capture_device| is scheduled to run asynchronously on the Device | 176 // to run asynchronously on the Device Thread. |
| 177 // Thread. | |
| 178 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this | 177 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this |
| 179 // newly created VideoCaptureDevice instance is connected to the | 178 // newly created VideoCaptureDevice instance is connected to the |
| 180 // VideoCaptureController via SetConsumerFeedbackObserver(). Furthermore, the | 179 // VideoCaptureController via SetConsumerFeedbackObserver(). |
| 181 // |buffer_pool| is moved to the |video_capture_controller| as a | |
| 182 // FrameBufferPool via SetFrameBufferPool(). | |
| 183 // Phase 4: This phase can only be reached on Android. When the application goes | 180 // Phase 4: This phase can only be reached on Android. When the application goes |
| 184 // to the background, the |video_capture_device| is asynchronously stopped and | 181 // to the background, the |video_capture_device| is asynchronously stopped and |
| 185 // released on the Device Thread. When the application is resumed, we | 182 // released on the Device Thread. When the application is resumed, we |
| 186 // transition to Phase 2. | 183 // transition to Phase 2. |
| 187 struct VideoCaptureManager::DeviceEntry { | 184 struct VideoCaptureManager::DeviceEntry { |
| 188 public: | 185 public: |
| 189 DeviceEntry(MediaStreamType stream_type, | 186 DeviceEntry(MediaStreamType stream_type, |
| 190 const std::string& id, | 187 const std::string& id, |
| 191 const media::VideoCaptureParams& params); | 188 const media::VideoCaptureParams& params); |
| 192 ~DeviceEntry(); | 189 ~DeviceEntry(); |
| 193 std::unique_ptr<media::VideoCaptureDevice::Client> CreateDeviceClient(); | 190 std::unique_ptr<media::VideoCaptureDevice::Client> CreateDeviceClient(); |
| 194 std::unique_ptr<media::FrameBufferPool> CreateFrameBufferPool(); | |
| 195 | 191 |
| 196 const int serial_id; | 192 const int serial_id; |
| 197 const MediaStreamType stream_type; | 193 const MediaStreamType stream_type; |
| 198 const std::string id; | 194 const std::string id; |
| 199 const media::VideoCaptureParams parameters; | 195 const media::VideoCaptureParams parameters; |
| 200 VideoCaptureController video_capture_controller; | 196 VideoCaptureController video_capture_controller; |
| 201 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool; | |
| 202 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; | 197 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; |
| 203 }; | 198 }; |
| 204 | 199 |
| 205 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported | 200 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported |
| 206 // video formats. | 201 // video formats. |
| 207 struct VideoCaptureManager::DeviceInfo { | 202 struct VideoCaptureManager::DeviceInfo { |
| 208 DeviceInfo(); | 203 DeviceInfo(); |
| 209 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); | 204 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); |
| 210 DeviceInfo(const DeviceInfo& other); | 205 DeviceInfo(const DeviceInfo& other); |
| 211 ~DeviceInfo(); | 206 ~DeviceInfo(); |
| 212 DeviceInfo& operator=(const DeviceInfo& other); | 207 DeviceInfo& operator=(const DeviceInfo& other); |
| 213 | 208 |
| 214 media::VideoCaptureDeviceDescriptor descriptor; | 209 media::VideoCaptureDeviceDescriptor descriptor; |
| 215 media::VideoCaptureFormats supported_formats; | 210 media::VideoCaptureFormats supported_formats; |
| 216 }; | 211 }; |
| 217 | 212 |
| 218 class BufferPoolFrameBufferPool : public media::FrameBufferPool { | |
| 219 public: | |
| 220 explicit BufferPoolFrameBufferPool( | |
| 221 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool) | |
| 222 : buffer_pool_(std::move(buffer_pool)) {} | |
| 223 | |
| 224 void SetBufferHold(int buffer_id) override { | |
| 225 buffer_pool_->HoldForConsumers(buffer_id, 1); | |
| 226 } | |
| 227 | |
| 228 void ReleaseBufferHold(int buffer_id) override { | |
| 229 buffer_pool_->RelinquishConsumerHold(buffer_id, 1); | |
| 230 } | |
| 231 | |
| 232 private: | |
| 233 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool_; | |
| 234 }; | |
| 235 | |
| 236 // Class used for queuing request for starting a device. | 213 // Class used for queuing request for starting a device. |
| 237 class VideoCaptureManager::CaptureDeviceStartRequest { | 214 class VideoCaptureManager::CaptureDeviceStartRequest { |
| 238 public: | 215 public: |
| 239 CaptureDeviceStartRequest(int serial_id, | 216 CaptureDeviceStartRequest(int serial_id, |
| 240 media::VideoCaptureSessionId session_id, | 217 media::VideoCaptureSessionId session_id, |
| 241 const media::VideoCaptureParams& params); | 218 const media::VideoCaptureParams& params); |
| 242 int serial_id() const { return serial_id_; } | 219 int serial_id() const { return serial_id_; } |
| 243 media::VideoCaptureSessionId session_id() const { return session_id_; } | 220 media::VideoCaptureSessionId session_id() const { return session_id_; } |
| 244 media::VideoCaptureParams params() const { return params_; } | 221 media::VideoCaptureParams params() const { return params_; } |
| 245 | 222 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 274 DCHECK(video_capture_device == nullptr); | 251 DCHECK(video_capture_device == nullptr); |
| 275 } | 252 } |
| 276 | 253 |
| 277 std::unique_ptr<media::VideoCaptureDevice::Client> | 254 std::unique_ptr<media::VideoCaptureDevice::Client> |
| 278 VideoCaptureManager::DeviceEntry::CreateDeviceClient() { | 255 VideoCaptureManager::DeviceEntry::CreateDeviceClient() { |
| 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 256 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 280 | 257 |
| 281 const int max_buffers = stream_type == MEDIA_TAB_VIDEO_CAPTURE | 258 const int max_buffers = stream_type == MEDIA_TAB_VIDEO_CAPTURE |
| 282 ? kMaxNumberOfBuffersForTabCapture | 259 ? kMaxNumberOfBuffersForTabCapture |
| 283 : kMaxNumberOfBuffers; | 260 : kMaxNumberOfBuffers; |
| 284 buffer_pool = new media::VideoCaptureBufferPoolImpl( | 261 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool = |
| 285 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | 262 new media::VideoCaptureBufferPoolImpl( |
| 286 max_buffers); | 263 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| 264 max_buffers); |
| 287 | 265 |
| 288 return base::MakeUnique<media::VideoCaptureDeviceClient>( | 266 return base::MakeUnique<media::VideoCaptureDeviceClient>( |
| 289 base::MakeUnique<VideoFrameReceiverOnIOThread>( | 267 base::MakeUnique<VideoFrameReceiverOnIOThread>( |
| 290 video_capture_controller.GetWeakPtrForIOThread()), | 268 video_capture_controller.GetWeakPtrForIOThread()), |
| 291 buffer_pool, | 269 std::move(buffer_pool), |
| 292 base::Bind( | 270 base::Bind(&CreateGpuJpegDecoder, |
| 293 &CreateGpuJpegDecoder, | 271 base::Bind(&media::VideoFrameReceiver::OnFrameReadyInBuffer, |
| 294 base::Bind(&media::VideoFrameReceiver::OnIncomingCapturedVideoFrame, | 272 video_capture_controller.GetWeakPtrForIOThread()))); |
| 295 video_capture_controller.GetWeakPtrForIOThread()))); | |
| 296 } | |
| 297 | |
| 298 std::unique_ptr<media::FrameBufferPool> | |
| 299 VideoCaptureManager::DeviceEntry::CreateFrameBufferPool() { | |
| 300 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 301 DCHECK(buffer_pool); | |
| 302 return base::MakeUnique<BufferPoolFrameBufferPool>(std::move(buffer_pool)); | |
| 303 } | 273 } |
| 304 | 274 |
| 305 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; | 275 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; |
| 306 | 276 |
| 307 VideoCaptureManager::DeviceInfo::DeviceInfo( | 277 VideoCaptureManager::DeviceInfo::DeviceInfo( |
| 308 media::VideoCaptureDeviceDescriptor descriptor) | 278 media::VideoCaptureDeviceDescriptor descriptor) |
| 309 : descriptor(descriptor) {} | 279 : descriptor(descriptor) {} |
| 310 | 280 |
| 311 VideoCaptureManager::DeviceInfo::DeviceInfo( | 281 VideoCaptureManager::DeviceInfo::DeviceInfo( |
| 312 const VideoCaptureManager::DeviceInfo& other) = default; | 282 const VideoCaptureManager::DeviceInfo& other) = default; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 << entry->id << " serial_id = " << entry->serial_id; | 436 << entry->id << " serial_id = " << entry->serial_id; |
| 467 return; | 437 return; |
| 468 } | 438 } |
| 469 } | 439 } |
| 470 | 440 |
| 471 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id | 441 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id |
| 472 << " serial_id = " << entry->serial_id << "."; | 442 << " serial_id = " << entry->serial_id << "."; |
| 473 entry->video_capture_controller.OnLog( | 443 entry->video_capture_controller.OnLog( |
| 474 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); | 444 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); |
| 475 entry->video_capture_controller.SetConsumerFeedbackObserver(nullptr); | 445 entry->video_capture_controller.SetConsumerFeedbackObserver(nullptr); |
| 476 entry->video_capture_controller.SetFrameBufferPool(nullptr); | |
| 477 | 446 |
| 478 // |entry->video_capture_device| can be null if creating the device has | 447 // |entry->video_capture_device| can be null if creating the device has |
| 479 // failed. | 448 // failed. |
| 480 if (entry->video_capture_device) { | 449 if (entry->video_capture_device) { |
| 481 device_task_runner_->PostTask( | 450 device_task_runner_->PostTask( |
| 482 FROM_HERE, | 451 FROM_HERE, |
| 483 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 452 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
| 484 base::Passed(&entry->video_capture_device))); | 453 base::Passed(&entry->video_capture_device))); |
| 485 } | 454 } |
| 486 } | 455 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 498 | 467 |
| 499 const int serial_id = request->serial_id(); | 468 const int serial_id = request->serial_id(); |
| 500 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 469 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
| 501 DCHECK(entry); | 470 DCHECK(entry); |
| 502 | 471 |
| 503 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 472 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " |
| 504 << entry->id << " start id = " << entry->serial_id; | 473 << entry->id << " start id = " << entry->serial_id; |
| 505 | 474 |
| 506 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = | 475 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = |
| 507 entry->CreateDeviceClient(); | 476 entry->CreateDeviceClient(); |
| 508 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool = | |
| 509 entry->CreateFrameBufferPool(); | |
| 510 | 477 |
| 511 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> | 478 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> |
| 512 start_capture_function; | 479 start_capture_function; |
| 513 | 480 |
| 514 switch (entry->stream_type) { | 481 switch (entry->stream_type) { |
| 515 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 482 case MEDIA_DEVICE_VIDEO_CAPTURE: { |
| 516 // We look up the device id from the renderer in our local enumeration | 483 // We look up the device id from the renderer in our local enumeration |
| 517 // since the renderer does not have all the information that might be | 484 // since the renderer does not have all the information that might be |
| 518 // held in the browser-side VideoCaptureDevice::Name structure. | 485 // held in the browser-side VideoCaptureDevice::Name structure. |
| 519 const DeviceInfo* found = GetDeviceInfoById(entry->id); | 486 const DeviceInfo* found = GetDeviceInfoById(entry->id); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 break; | 527 break; |
| 561 | 528 |
| 562 default: { | 529 default: { |
| 563 NOTIMPLEMENTED(); | 530 NOTIMPLEMENTED(); |
| 564 return; | 531 return; |
| 565 } | 532 } |
| 566 } | 533 } |
| 567 base::PostTaskAndReplyWithResult( | 534 base::PostTaskAndReplyWithResult( |
| 568 device_task_runner_.get(), FROM_HERE, start_capture_function, | 535 device_task_runner_.get(), FROM_HERE, start_capture_function, |
| 569 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, | 536 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, |
| 570 request->serial_id(), base::Passed(&frame_buffer_pool))); | 537 request->serial_id())); |
| 571 } | 538 } |
| 572 | 539 |
| 573 void VideoCaptureManager::OnDeviceStarted( | 540 void VideoCaptureManager::OnDeviceStarted( |
| 574 int serial_id, | 541 int serial_id, |
| 575 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool, | |
| 576 std::unique_ptr<VideoCaptureDevice> device) { | 542 std::unique_ptr<VideoCaptureDevice> device) { |
| 577 DVLOG(3) << __func__; | 543 DVLOG(3) << __func__; |
| 578 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 544 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 579 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); | 545 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); |
| 580 // |device| can be null if creation failed in | 546 // |device| can be null if creation failed in |
| 581 // DoStartDeviceCaptureOnDeviceThread. | 547 // DoStartDeviceCaptureOnDeviceThread. |
| 582 if (device_start_queue_.front().abort_start()) { | 548 if (device_start_queue_.front().abort_start()) { |
| 583 // The device is no longer wanted. Stop the device again. | 549 // The device is no longer wanted. Stop the device again. |
| 584 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; | 550 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; |
| 585 media::VideoCaptureDevice* device_ptr = device.get(); | 551 media::VideoCaptureDevice* device_ptr = device.get(); |
| 586 base::Closure closure = | 552 base::Closure closure = |
| 587 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 553 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
| 588 base::Passed(&device)); | 554 base::Passed(&device)); |
| 589 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { | 555 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { |
| 590 // PostTask failed. The device must be stopped anyway. | 556 // PostTask failed. The device must be stopped anyway. |
| 591 device_ptr->StopAndDeAllocate(); | 557 device_ptr->StopAndDeAllocate(); |
| 592 } | 558 } |
| 593 } else { | 559 } else { |
| 594 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 560 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
| 595 DCHECK(entry); | 561 DCHECK(entry); |
| 596 DCHECK(!entry->video_capture_device); | 562 DCHECK(!entry->video_capture_device); |
| 597 if (device) { | 563 if (device) { |
| 598 entry->video_capture_controller.SetFrameBufferPool( | |
| 599 std::move(frame_buffer_pool)); | |
| 600 // Passing raw pointer |device.get()| to the controller is safe, | 564 // Passing raw pointer |device.get()| to the controller is safe, |
| 601 // because we transfer ownership of it to |entry|. We are calling | 565 // because we transfer ownership of it to |entry|. We are calling |
| 602 // SetConsumerFeedbackObserver(nullptr) before releasing | 566 // SetConsumerFeedbackObserver(nullptr) before releasing |
| 603 // |entry->video_capture_device_| on the |device_task_runner_|. | 567 // |entry->video_capture_device_| on the |device_task_runner_|. |
| 604 entry->video_capture_controller.SetConsumerFeedbackObserver( | 568 entry->video_capture_controller.SetConsumerFeedbackObserver( |
| 605 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | 569 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( |
| 606 device.get(), device_task_runner_)); | 570 device.get(), device_task_runner_)); |
| 607 } | 571 } |
| 608 entry->video_capture_device = std::move(device); | 572 entry->video_capture_device = std::move(device); |
| 609 | 573 |
| (...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1350 if (!device_in_queue) { | 1314 if (!device_in_queue) { |
| 1351 // Session ID is only valid for Screen capture. So we can fake it to | 1315 // Session ID is only valid for Screen capture. So we can fake it to |
| 1352 // resume video capture devices here. | 1316 // resume video capture devices here. |
| 1353 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); | 1317 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); |
| 1354 } | 1318 } |
| 1355 } | 1319 } |
| 1356 } | 1320 } |
| 1357 #endif // defined(OS_ANDROID) | 1321 #endif // defined(OS_ANDROID) |
| 1358 | 1322 |
| 1359 } // namespace content | 1323 } // namespace content |
| OLD | NEW |