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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 << entry->id << " serial_id = " << entry->serial_id; | 435 << entry->id << " serial_id = " << entry->serial_id; |
466 return; | 436 return; |
467 } | 437 } |
468 } | 438 } |
469 | 439 |
470 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id | 440 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id |
471 << " serial_id = " << entry->serial_id << "."; | 441 << " serial_id = " << entry->serial_id << "."; |
472 entry->video_capture_controller.OnLog( | 442 entry->video_capture_controller.OnLog( |
473 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); | 443 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); |
474 entry->video_capture_controller.SetConsumerFeedbackObserver(nullptr); | 444 entry->video_capture_controller.SetConsumerFeedbackObserver(nullptr); |
475 entry->video_capture_controller.SetFrameBufferPool(nullptr); | |
476 | 445 |
477 // |entry->video_capture_device| can be null if creating the device has | 446 // |entry->video_capture_device| can be null if creating the device has |
478 // failed. | 447 // failed. |
479 if (entry->video_capture_device) { | 448 if (entry->video_capture_device) { |
480 device_task_runner_->PostTask( | 449 device_task_runner_->PostTask( |
481 FROM_HERE, | 450 FROM_HERE, |
482 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 451 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
483 base::Passed(&entry->video_capture_device))); | 452 base::Passed(&entry->video_capture_device))); |
484 } | 453 } |
485 } | 454 } |
(...skipping 11 matching lines...) Expand all Loading... |
497 | 466 |
498 const int serial_id = request->serial_id(); | 467 const int serial_id = request->serial_id(); |
499 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 468 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
500 DCHECK(entry); | 469 DCHECK(entry); |
501 | 470 |
502 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 471 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " |
503 << entry->id << " start id = " << entry->serial_id; | 472 << entry->id << " start id = " << entry->serial_id; |
504 | 473 |
505 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = | 474 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = |
506 entry->CreateDeviceClient(); | 475 entry->CreateDeviceClient(); |
507 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool = | |
508 entry->CreateFrameBufferPool(); | |
509 | 476 |
510 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> | 477 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> |
511 start_capture_function; | 478 start_capture_function; |
512 | 479 |
513 switch (entry->stream_type) { | 480 switch (entry->stream_type) { |
514 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 481 case MEDIA_DEVICE_VIDEO_CAPTURE: { |
515 // We look up the device id from the renderer in our local enumeration | 482 // We look up the device id from the renderer in our local enumeration |
516 // since the renderer does not have all the information that might be | 483 // since the renderer does not have all the information that might be |
517 // held in the browser-side VideoCaptureDevice::Name structure. | 484 // held in the browser-side VideoCaptureDevice::Name structure. |
518 const DeviceInfo* found = GetDeviceInfoById(entry->id); | 485 const DeviceInfo* found = GetDeviceInfoById(entry->id); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 break; | 526 break; |
560 | 527 |
561 default: { | 528 default: { |
562 NOTIMPLEMENTED(); | 529 NOTIMPLEMENTED(); |
563 return; | 530 return; |
564 } | 531 } |
565 } | 532 } |
566 base::PostTaskAndReplyWithResult( | 533 base::PostTaskAndReplyWithResult( |
567 device_task_runner_.get(), FROM_HERE, start_capture_function, | 534 device_task_runner_.get(), FROM_HERE, start_capture_function, |
568 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, | 535 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, |
569 request->serial_id(), base::Passed(&frame_buffer_pool))); | 536 request->serial_id())); |
570 } | 537 } |
571 | 538 |
572 void VideoCaptureManager::OnDeviceStarted( | 539 void VideoCaptureManager::OnDeviceStarted( |
573 int serial_id, | 540 int serial_id, |
574 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool, | |
575 std::unique_ptr<VideoCaptureDevice> device) { | 541 std::unique_ptr<VideoCaptureDevice> device) { |
576 DVLOG(3) << __func__; | 542 DVLOG(3) << __func__; |
577 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 543 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
578 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); | 544 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); |
579 // |device| can be null if creation failed in | 545 // |device| can be null if creation failed in |
580 // DoStartDeviceCaptureOnDeviceThread. | 546 // DoStartDeviceCaptureOnDeviceThread. |
581 if (device_start_queue_.front().abort_start()) { | 547 if (device_start_queue_.front().abort_start()) { |
582 // The device is no longer wanted. Stop the device again. | 548 // The device is no longer wanted. Stop the device again. |
583 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; | 549 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; |
584 media::VideoCaptureDevice* device_ptr = device.get(); | 550 media::VideoCaptureDevice* device_ptr = device.get(); |
585 base::Closure closure = | 551 base::Closure closure = |
586 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 552 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
587 base::Passed(&device)); | 553 base::Passed(&device)); |
588 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { | 554 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { |
589 // PostTask failed. The device must be stopped anyway. | 555 // PostTask failed. The device must be stopped anyway. |
590 device_ptr->StopAndDeAllocate(); | 556 device_ptr->StopAndDeAllocate(); |
591 } | 557 } |
592 } else { | 558 } else { |
593 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 559 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
594 DCHECK(entry); | 560 DCHECK(entry); |
595 DCHECK(!entry->video_capture_device); | 561 DCHECK(!entry->video_capture_device); |
596 if (device) { | 562 if (device) { |
597 entry->video_capture_controller.SetFrameBufferPool( | |
598 std::move(frame_buffer_pool)); | |
599 // Passing raw pointer |device.get()| to the controller is safe, | 563 // Passing raw pointer |device.get()| to the controller is safe, |
600 // because we transfer ownership of it to |entry|. We are calling | 564 // because we transfer ownership of it to |entry|. We are calling |
601 // SetConsumerFeedbackObserver(nullptr) before releasing | 565 // SetConsumerFeedbackObserver(nullptr) before releasing |
602 // |entry->video_capture_device_| on the |device_task_runner_|. | 566 // |entry->video_capture_device_| on the |device_task_runner_|. |
603 entry->video_capture_controller.SetConsumerFeedbackObserver( | 567 entry->video_capture_controller.SetConsumerFeedbackObserver( |
604 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | 568 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( |
605 device.get(), device_task_runner_)); | 569 device.get(), device_task_runner_)); |
606 } | 570 } |
607 entry->video_capture_device = std::move(device); | 571 entry->video_capture_device = std::move(device); |
608 | 572 |
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1349 if (!device_in_queue) { | 1313 if (!device_in_queue) { |
1350 // Session ID is only valid for Screen capture. So we can fake it to | 1314 // Session ID is only valid for Screen capture. So we can fake it to |
1351 // resume video capture devices here. | 1315 // resume video capture devices here. |
1352 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); | 1316 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); |
1353 } | 1317 } |
1354 } | 1318 } |
1355 } | 1319 } |
1356 #endif // defined(OS_ANDROID) | 1320 #endif // defined(OS_ANDROID) |
1357 | 1321 |
1358 } // namespace content | 1322 } // namespace content |
OLD | NEW |