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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 323 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
324 DCHECK(listener_); | 324 DCHECK(listener_); |
325 DVLOG(1) << "VideoCaptureManager::Close, id " << capture_session_id; | 325 DVLOG(1) << "VideoCaptureManager::Close, id " << capture_session_id; |
326 | 326 |
327 SessionMap::iterator session_it = sessions_.find(capture_session_id); | 327 SessionMap::iterator session_it = sessions_.find(capture_session_id); |
328 if (session_it == sessions_.end()) { | 328 if (session_it == sessions_.end()) { |
329 NOTREACHED(); | 329 NOTREACHED(); |
330 return; | 330 return; |
331 } | 331 } |
332 | 332 |
333 DeviceEntry* const existing_device = GetDeviceEntryForMediaStreamDevice( | 333 DeviceEntry* const existing_device = |
334 session_it->second); | 334 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
335 if (existing_device) { | 335 if (existing_device) { |
336 // Remove any client that is still using the session. This is safe to call | 336 // Remove any client that is still using the session. This is safe to call |
337 // even if there are no clients using the session. | 337 // even if there are no clients using the session. |
338 existing_device->video_capture_controller() | 338 existing_device->video_capture_controller() |
339 ->StopSession(capture_session_id); | 339 ->StopSession(capture_session_id); |
340 | 340 |
341 // StopSession() may have removed the last client, so we might need to | 341 // StopSession() may have removed the last client, so we might need to |
342 // close the device. | 342 // close the device. |
343 DestroyDeviceEntryIfNoClients(existing_device); | 343 DestroyDeviceEntryIfNoClients(existing_device); |
344 } | 344 } |
(...skipping 11 matching lines...) Expand all Loading... | |
356 const media::VideoCaptureParams& params) { | 356 const media::VideoCaptureParams& params) { |
357 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 357 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
358 device_start_queue_.push_back( | 358 device_start_queue_.push_back( |
359 CaptureDeviceStartRequest(entry->serial_id, session_id, params)); | 359 CaptureDeviceStartRequest(entry->serial_id, session_id, params)); |
360 if (device_start_queue_.size() == 1) | 360 if (device_start_queue_.size() == 1) |
361 HandleQueuedStartRequest(); | 361 HandleQueuedStartRequest(); |
362 } | 362 } |
363 | 363 |
364 void VideoCaptureManager::DoStopDevice(DeviceEntry* entry) { | 364 void VideoCaptureManager::DoStopDevice(DeviceEntry* entry) { |
365 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 365 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
366 DCHECK(std::find(devices_.begin(), devices_.end(), entry) != devices_.end()); | 366 // TODO(mcasas): use a helper function https://crbug.com/624854. |
367 DCHECK( | |
368 std::find_if(devices_.begin(), devices_.end(), | |
369 [entry](const std::unique_ptr<DeviceEntry>& device_entry) { | |
370 return device_entry.get() == entry; | |
371 }) != devices_.end()); | |
367 | 372 |
368 // Find the matching start request. | 373 // Find the matching start request. |
369 for (DeviceStartQueue::reverse_iterator request = | 374 for (DeviceStartQueue::reverse_iterator request = |
370 device_start_queue_.rbegin(); | 375 device_start_queue_.rbegin(); |
371 request != device_start_queue_.rend(); ++request) { | 376 request != device_start_queue_.rend(); ++request) { |
372 if (request->serial_id() == entry->serial_id) { | 377 if (request->serial_id() == entry->serial_id) { |
373 request->set_abort_start(); | 378 request->set_abort_start(); |
374 DVLOG(3) << "DoStopDevice, aborting start request for device " | 379 DVLOG(3) << "DoStopDevice, aborting start request for device " |
375 << entry->id << " serial_id = " << entry->serial_id; | 380 << entry->id << " serial_id = " << entry->serial_id; |
376 return; | 381 return; |
(...skipping 19 matching lines...) Expand all Loading... | |
396 // Remove all start requests that have been aborted. | 401 // Remove all start requests that have been aborted. |
397 while (device_start_queue_.begin() != device_start_queue_.end() && | 402 while (device_start_queue_.begin() != device_start_queue_.end() && |
398 device_start_queue_.begin()->abort_start()) { | 403 device_start_queue_.begin()->abort_start()) { |
399 device_start_queue_.pop_front(); | 404 device_start_queue_.pop_front(); |
400 } | 405 } |
401 DeviceStartQueue::iterator request = device_start_queue_.begin(); | 406 DeviceStartQueue::iterator request = device_start_queue_.begin(); |
402 if (request == device_start_queue_.end()) | 407 if (request == device_start_queue_.end()) |
403 return; | 408 return; |
404 | 409 |
405 const int serial_id = request->serial_id(); | 410 const int serial_id = request->serial_id(); |
406 DeviceEntries::iterator entry_it = std::find_if( | 411 DeviceEntry* entry = GetDeviceEntryBySerialId(serial_id); |
emircan
2016/06/30 18:24:04
DeviceEntry* const entry
mcasas
2016/06/30 21:51:24
Done.
| |
407 devices_.begin(), devices_.end(), | 412 DCHECK(entry); |
408 [serial_id] (const DeviceEntry* e) { | |
409 return e->serial_id == serial_id; | |
410 }); | |
411 DCHECK(entry_it != devices_.end()); | |
412 DeviceEntry* entry = (*entry_it); | |
413 | 413 |
414 #if defined(OS_MACOSX) | 414 #if defined(OS_MACOSX) |
415 if (NeedToInitializeCaptureDeviceApi(entry->stream_type)) { | 415 if (NeedToInitializeCaptureDeviceApi(entry->stream_type)) { |
416 InitializeCaptureDeviceApiOnUIThread( | 416 InitializeCaptureDeviceApiOnUIThread( |
417 base::Bind(&VideoCaptureManager::HandleQueuedStartRequest, this)); | 417 base::Bind(&VideoCaptureManager::HandleQueuedStartRequest, this)); |
418 return; | 418 return; |
419 } | 419 } |
420 #endif | 420 #endif |
421 | 421 |
422 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 422 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " |
423 << entry->id << " start id = " << entry->serial_id; | 423 << entry->id << " start id = " << entry->serial_id; |
424 | 424 |
425 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> | 425 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> |
426 start_capture_function; | 426 start_capture_function; |
427 | 427 |
428 switch (entry->stream_type) { | 428 switch (entry->stream_type) { |
429 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 429 case MEDIA_DEVICE_VIDEO_CAPTURE: { |
430 // We look up the device id from the renderer in our local enumeration | 430 // We look up the device id from the renderer in our local enumeration |
431 // since the renderer does not have all the information that might be | 431 // since the renderer does not have all the information that might be |
432 // held in the browser-side VideoCaptureDevice::Name structure. | 432 // held in the browser-side VideoCaptureDevice::Name structure. |
433 const media::VideoCaptureDeviceInfo* found = | 433 const media::VideoCaptureDeviceInfo* found = GetDeviceInfoById(entry->id); |
434 FindDeviceInfoById(entry->id, devices_info_cache_); | |
435 if (found) { | 434 if (found) { |
436 entry->video_capture_controller()->DoLogOnIOThread(base::StringPrintf( | 435 entry->video_capture_controller()->DoLogOnIOThread(base::StringPrintf( |
437 "Starting device: id: %s, name: %s, api: %s", | 436 "Starting device: id: %s, name: %s, api: %s", |
438 found->name.id().c_str(), found->name.GetNameAndModel().c_str(), | 437 found->name.id().c_str(), found->name.GetNameAndModel().c_str(), |
439 found->name.GetCaptureApiTypeString())); | 438 found->name.GetCaptureApiTypeString())); |
440 | 439 |
441 start_capture_function = base::Bind( | 440 start_capture_function = base::Bind( |
442 &VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, this, | 441 &VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, this, |
443 found->name, request->params(), | 442 found->name, request->params(), |
444 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | 443 base::Passed(entry->video_capture_controller()->NewDeviceClient())); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; | 498 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; |
500 media::VideoCaptureDevice* device_ptr = device.get(); | 499 media::VideoCaptureDevice* device_ptr = device.get(); |
501 base::Closure closure = | 500 base::Closure closure = |
502 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 501 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
503 base::Passed(&device)); | 502 base::Passed(&device)); |
504 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { | 503 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { |
505 // PostTask failed. The device must be stopped anyway. | 504 // PostTask failed. The device must be stopped anyway. |
506 device_ptr->StopAndDeAllocate(); | 505 device_ptr->StopAndDeAllocate(); |
507 } | 506 } |
508 } else { | 507 } else { |
509 DeviceEntries::iterator entry_it = std::find_if( | 508 DeviceEntry* entry = GetDeviceEntryBySerialId(serial_id); |
emircan
2016/06/30 18:24:04
Ditto
mcasas
2016/06/30 21:51:24
Done.
| |
510 devices_.begin(), devices_.end(), | 509 DCHECK(entry); |
511 [serial_id] (const DeviceEntry* e) { | |
512 return e->serial_id == serial_id; | |
513 }); | |
514 DCHECK(entry_it != devices_.end()); | |
515 DeviceEntry* entry = *entry_it; | |
516 DCHECK(!entry->video_capture_device()); | 510 DCHECK(!entry->video_capture_device()); |
517 entry->SetVideoCaptureDevice(std::move(device)); | 511 entry->SetVideoCaptureDevice(std::move(device)); |
518 | 512 |
519 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { | 513 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { |
520 const media::VideoCaptureSessionId session_id = | 514 const media::VideoCaptureSessionId session_id = |
521 device_start_queue_.front().session_id(); | 515 device_start_queue_.front().session_id(); |
522 DCHECK(session_id != kFakeSessionId); | 516 DCHECK(session_id != kFakeSessionId); |
523 MaybePostDesktopCaptureWindowId(session_id); | 517 MaybePostDesktopCaptureWindowId(session_id); |
524 } | 518 } |
525 } | 519 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
642 | 636 |
643 void VideoCaptureManager::StopCaptureForClient( | 637 void VideoCaptureManager::StopCaptureForClient( |
644 VideoCaptureController* controller, | 638 VideoCaptureController* controller, |
645 VideoCaptureControllerID client_id, | 639 VideoCaptureControllerID client_id, |
646 VideoCaptureControllerEventHandler* client_handler, | 640 VideoCaptureControllerEventHandler* client_handler, |
647 bool aborted_due_to_error) { | 641 bool aborted_due_to_error) { |
648 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 642 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
649 DCHECK(controller); | 643 DCHECK(controller); |
650 DCHECK(client_handler); | 644 DCHECK(client_handler); |
651 | 645 |
652 DeviceEntry* entry = GetDeviceEntryForController(controller); | 646 DeviceEntry* entry = GetDeviceEntryByController(controller); |
653 if (!entry) { | 647 if (!entry) { |
654 NOTREACHED(); | 648 NOTREACHED(); |
655 return; | 649 return; |
656 } | 650 } |
657 if (!aborted_due_to_error) { | 651 if (!aborted_due_to_error) { |
658 if (controller->has_received_frames()) { | 652 if (controller->has_received_frames()) { |
659 LogVideoCaptureEvent(VIDEO_CAPTURE_STOP_CAPTURE_OK); | 653 LogVideoCaptureEvent(VIDEO_CAPTURE_STOP_CAPTURE_OK); |
660 } else if (entry->stream_type == MEDIA_DEVICE_VIDEO_CAPTURE) { | 654 } else if (entry->stream_type == MEDIA_DEVICE_VIDEO_CAPTURE) { |
661 LogVideoCaptureEvent( | 655 LogVideoCaptureEvent( |
662 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE); | 656 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE); |
(...skipping 23 matching lines...) Expand all Loading... | |
686 DestroyDeviceEntryIfNoClients(entry); | 680 DestroyDeviceEntryIfNoClients(entry); |
687 } | 681 } |
688 | 682 |
689 void VideoCaptureManager::PauseCaptureForClient( | 683 void VideoCaptureManager::PauseCaptureForClient( |
690 VideoCaptureController* controller, | 684 VideoCaptureController* controller, |
691 VideoCaptureControllerID client_id, | 685 VideoCaptureControllerID client_id, |
692 VideoCaptureControllerEventHandler* client_handler) { | 686 VideoCaptureControllerEventHandler* client_handler) { |
693 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 687 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
694 DCHECK(controller); | 688 DCHECK(controller); |
695 DCHECK(client_handler); | 689 DCHECK(client_handler); |
696 DeviceEntry* entry = GetDeviceEntryForController(controller); | 690 DeviceEntry* entry = GetDeviceEntryByController(controller); |
697 if (!entry) { | 691 if (!entry) |
698 NOTREACHED(); | 692 NOTREACHED() << "Got Null entry while pausing capture"; |
699 DVLOG(1) << "Got Null entry while pausing capture"; | |
700 } | |
701 | 693 |
702 // Do not pause Content Video Capture devices, e.g. Tab or Screen capture. | 694 // Do not pause Content Video Capture devices, e.g. Tab or Screen capture. |
703 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) | 695 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) |
704 return; | 696 return; |
705 | 697 |
706 controller->PauseClient(client_id, client_handler); | 698 controller->PauseClient(client_id, client_handler); |
707 } | 699 } |
708 | 700 |
709 void VideoCaptureManager::ResumeCaptureForClient( | 701 void VideoCaptureManager::ResumeCaptureForClient( |
710 media::VideoCaptureSessionId session_id, | 702 media::VideoCaptureSessionId session_id, |
711 const media::VideoCaptureParams& params, | 703 const media::VideoCaptureParams& params, |
712 VideoCaptureController* controller, | 704 VideoCaptureController* controller, |
713 VideoCaptureControllerID client_id, | 705 VideoCaptureControllerID client_id, |
714 VideoCaptureControllerEventHandler* client_handler) { | 706 VideoCaptureControllerEventHandler* client_handler) { |
715 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 707 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
716 DCHECK(controller); | 708 DCHECK(controller); |
717 DCHECK(client_handler); | 709 DCHECK(client_handler); |
718 | 710 |
719 DeviceEntry* entry = GetDeviceEntryForController(controller); | 711 DeviceEntry* entry = GetDeviceEntryByController(controller); |
720 if (!entry) { | 712 if (!entry) |
721 NOTREACHED(); | 713 NOTREACHED() << "Got Null entry while resuming capture"; |
722 DVLOG(1) << "Got Null entry while resuming capture"; | |
723 } | |
724 | 714 |
725 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 715 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. |
726 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) | 716 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) |
727 return; | 717 return; |
728 | 718 |
729 controller->ResumeClient(client_id, client_handler); | 719 controller->ResumeClient(client_id, client_handler); |
730 } | 720 } |
731 | 721 |
732 void VideoCaptureManager::RequestRefreshFrameForClient( | 722 void VideoCaptureManager::RequestRefreshFrameForClient( |
733 VideoCaptureController* controller) { | 723 VideoCaptureController* controller) { |
734 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 724 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
735 | 725 |
736 if (DeviceEntry* entry = GetDeviceEntryForController(controller)) { | 726 if (DeviceEntry* entry = GetDeviceEntryByController(controller)) { |
737 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 727 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { |
738 device_task_runner_->PostTask( | 728 device_task_runner_->PostTask( |
739 FROM_HERE, | 729 FROM_HERE, |
740 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, | 730 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, |
741 // Unretained is safe to use here because |device| would be | 731 // Unretained is safe to use here because |device| would be |
742 // null if it was scheduled for shutdown and destruction, | 732 // null if it was scheduled for shutdown and destruction, |
743 // and because this task is guaranteed to run before the | 733 // and because this task is guaranteed to run before the |
744 // task that destroys the |device|. | 734 // task that destroys the |device|. |
745 base::Unretained(device))); | 735 base::Unretained(device))); |
746 } | 736 } |
747 } | 737 } |
748 } | 738 } |
749 | 739 |
750 bool VideoCaptureManager::GetDeviceSupportedFormats( | 740 bool VideoCaptureManager::GetDeviceSupportedFormats( |
751 media::VideoCaptureSessionId capture_session_id, | 741 media::VideoCaptureSessionId capture_session_id, |
752 media::VideoCaptureFormats* supported_formats) { | 742 media::VideoCaptureFormats* supported_formats) { |
753 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 743 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
754 DCHECK(supported_formats->empty()); | 744 DCHECK(supported_formats->empty()); |
755 | 745 |
756 SessionMap::iterator it = sessions_.find(capture_session_id); | 746 SessionMap::iterator it = sessions_.find(capture_session_id); |
757 if (it == sessions_.end()) | 747 if (it == sessions_.end()) |
758 return false; | 748 return false; |
759 DVLOG(1) << "GetDeviceSupportedFormats for device: " << it->second.name; | 749 DVLOG(1) << "GetDeviceSupportedFormats for device: " << it->second.name; |
760 | 750 |
761 // Return all available formats of the device, regardless its started state. | 751 // Return all available formats of the device, regardless its started state. |
762 media::VideoCaptureDeviceInfo* existing_device = | 752 media::VideoCaptureDeviceInfo* existing_device = |
763 FindDeviceInfoById(it->second.id, devices_info_cache_); | 753 GetDeviceInfoById(it->second.id); |
764 if (existing_device) | 754 if (existing_device) |
765 *supported_formats = existing_device->supported_formats; | 755 *supported_formats = existing_device->supported_formats; |
766 return true; | 756 return true; |
767 } | 757 } |
768 | 758 |
769 bool VideoCaptureManager::GetDeviceFormatsInUse( | 759 bool VideoCaptureManager::GetDeviceFormatsInUse( |
770 media::VideoCaptureSessionId capture_session_id, | 760 media::VideoCaptureSessionId capture_session_id, |
771 media::VideoCaptureFormats* formats_in_use) { | 761 media::VideoCaptureFormats* formats_in_use) { |
772 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 762 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
773 DCHECK(formats_in_use->empty()); | 763 DCHECK(formats_in_use->empty()); |
774 | 764 |
775 SessionMap::iterator it = sessions_.find(capture_session_id); | 765 SessionMap::iterator it = sessions_.find(capture_session_id); |
776 if (it == sessions_.end()) | 766 if (it == sessions_.end()) |
777 return false; | 767 return false; |
778 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; | 768 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; |
779 | 769 |
780 // Return the currently in-use format(s) of the device, if it's started. | 770 // Return the currently in-use format(s) of the device, if it's started. |
781 DeviceEntry* device_in_use = | 771 DeviceEntry* device_in_use = |
782 GetDeviceEntryForMediaStreamDevice(it->second); | 772 GetDeviceEntryByTypeAndId(it->second.type, it->second.id); |
783 if (device_in_use) { | 773 if (device_in_use) { |
784 // Currently only one format-in-use is supported at the VCC level. | 774 // Currently only one format-in-use is supported at the VCC level. |
785 formats_in_use->push_back( | 775 formats_in_use->push_back( |
786 device_in_use->video_capture_controller()->GetVideoCaptureFormat()); | 776 device_in_use->video_capture_controller()->GetVideoCaptureFormat()); |
787 } | 777 } |
788 return true; | 778 return true; |
789 } | 779 } |
790 | 780 |
791 void VideoCaptureManager::SetDesktopCaptureWindowId( | 781 void VideoCaptureManager::SetDesktopCaptureWindowId( |
792 media::VideoCaptureSessionId session_id, | 782 media::VideoCaptureSessionId session_id, |
793 gfx::NativeViewId window_id) { | 783 gfx::NativeViewId window_id) { |
794 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 784 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
795 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; | 785 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; |
796 | 786 |
797 notification_window_ids_[session_id] = window_id; | 787 notification_window_ids_[session_id] = window_id; |
798 MaybePostDesktopCaptureWindowId(session_id); | 788 MaybePostDesktopCaptureWindowId(session_id); |
799 } | 789 } |
800 | 790 |
801 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( | 791 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( |
802 media::VideoCaptureSessionId session_id) { | 792 media::VideoCaptureSessionId session_id) { |
803 SessionMap::iterator session_it = sessions_.find(session_id); | 793 SessionMap::iterator session_it = sessions_.find(session_id); |
804 if (session_it == sessions_.end()) { | 794 if (session_it == sessions_.end()) |
805 return; | 795 return; |
806 } | |
807 | 796 |
808 DeviceEntry* const existing_device = | 797 DeviceEntry* const existing_device = |
809 GetDeviceEntryForMediaStreamDevice(session_it->second); | 798 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
810 if (!existing_device) { | 799 if (!existing_device) { |
811 DVLOG(2) << "Failed to find an existing screen capture device."; | 800 DVLOG(2) << "Failed to find an existing screen capture device."; |
812 return; | 801 return; |
813 } | 802 } |
814 | 803 |
815 if (!existing_device->video_capture_device()) { | 804 if (!existing_device->video_capture_device()) { |
816 DVLOG(2) << "Screen capture device not yet started."; | 805 DVLOG(2) << "Screen capture device not yet started."; |
817 return; | 806 return; |
818 } | 807 } |
819 | 808 |
(...skipping 19 matching lines...) Expand all Loading... | |
839 window_id_it->second)); | 828 window_id_it->second)); |
840 | 829 |
841 notification_window_ids_.erase(window_id_it); | 830 notification_window_ids_.erase(window_id_it); |
842 } | 831 } |
843 | 832 |
844 void VideoCaptureManager::GetPhotoCapabilities( | 833 void VideoCaptureManager::GetPhotoCapabilities( |
845 int session_id, | 834 int session_id, |
846 media::ScopedResultCallback< | 835 media::ScopedResultCallback< |
847 VideoCaptureDevice::GetPhotoCapabilitiesCallback> callback) { | 836 VideoCaptureDevice::GetPhotoCapabilitiesCallback> callback) { |
848 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 837 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
849 VideoCaptureDevice* device = GetVideoCaptureDeviceFromSessionId(session_id); | 838 VideoCaptureDevice* device = GetVideoCaptureDeviceBySessionId(session_id); |
850 if (!device) | 839 if (!device) |
851 return; | 840 return; |
852 // Unretained(device) is safe to use here because |device| would be null if it | 841 // Unretained(device) is safe to use here because |device| would be null if it |
853 // was scheduled for shutdown and destruction, and because this task is | 842 // was scheduled for shutdown and destruction, and because this task is |
854 // guaranteed to run before the task that destroys the |device|. | 843 // guaranteed to run before the task that destroys the |device|. |
855 device_task_runner_->PostTask( | 844 device_task_runner_->PostTask( |
856 FROM_HERE, base::Bind(&VideoCaptureDevice::GetPhotoCapabilities, | 845 FROM_HERE, base::Bind(&VideoCaptureDevice::GetPhotoCapabilities, |
857 base::Unretained(device), base::Passed(&callback))); | 846 base::Unretained(device), base::Passed(&callback))); |
858 } | 847 } |
859 | 848 |
860 void VideoCaptureManager::TakePhoto( | 849 void VideoCaptureManager::TakePhoto( |
861 int session_id, | 850 int session_id, |
862 media::ScopedResultCallback<VideoCaptureDevice::TakePhotoCallback> | 851 media::ScopedResultCallback<VideoCaptureDevice::TakePhotoCallback> |
863 callback) { | 852 callback) { |
864 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 853 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
865 VideoCaptureDevice* device = GetVideoCaptureDeviceFromSessionId(session_id); | 854 VideoCaptureDevice* device = GetVideoCaptureDeviceBySessionId(session_id); |
866 if (!device) | 855 if (!device) |
867 return; | 856 return; |
868 // Unretained(device) is safe to use here because |device| would be null if it | 857 // Unretained(device) is safe to use here because |device| would be null if it |
869 // was scheduled for shutdown and destruction, and because this task is | 858 // was scheduled for shutdown and destruction, and because this task is |
870 // guaranteed to run before the task that destroys the |device|. | 859 // guaranteed to run before the task that destroys the |device|. |
871 device_task_runner_->PostTask( | 860 device_task_runner_->PostTask( |
872 FROM_HERE, base::Bind(&VideoCaptureDevice::TakePhoto, | 861 FROM_HERE, base::Bind(&VideoCaptureDevice::TakePhoto, |
873 base::Unretained(device), base::Passed(&callback))); | 862 base::Unretained(device), base::Passed(&callback))); |
874 } | 863 } |
875 | 864 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
961 media::VideoCaptureDeviceInfo device_info(it, media::VideoCaptureFormats()); | 950 media::VideoCaptureDeviceInfo device_info(it, media::VideoCaptureFormats()); |
962 video_capture_device_factory_->GetDeviceSupportedFormats( | 951 video_capture_device_factory_->GetDeviceSupportedFormats( |
963 it, &(device_info.supported_formats)); | 952 it, &(device_info.supported_formats)); |
964 ConsolidateCaptureFormats(&device_info.supported_formats); | 953 ConsolidateCaptureFormats(&device_info.supported_formats); |
965 new_devices_info_cache.push_back(device_info); | 954 new_devices_info_cache.push_back(device_info); |
966 } | 955 } |
967 | 956 |
968 on_devices_enumerated_callback.Run(new_devices_info_cache); | 957 on_devices_enumerated_callback.Run(new_devices_info_cache); |
969 } | 958 } |
970 | 959 |
971 VideoCaptureManager::DeviceEntry* | |
972 VideoCaptureManager::GetDeviceEntryForMediaStreamDevice( | |
973 const MediaStreamDevice& device_info) { | |
974 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
975 | |
976 for (DeviceEntry* device : devices_) { | |
977 if (device_info.type == device->stream_type && device_info.id == device->id) | |
978 return device; | |
979 } | |
980 return nullptr; | |
981 } | |
982 | |
983 media::VideoCaptureDevice* | |
984 VideoCaptureManager::GetVideoCaptureDeviceFromSessionId(int session_id) { | |
985 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
986 SessionMap::const_iterator session_it = sessions_.find(session_id); | |
987 if (session_it == sessions_.end()) | |
988 return nullptr; | |
989 | |
990 DeviceEntry* const device_info = | |
991 GetDeviceEntryForMediaStreamDevice(session_it->second); | |
992 if (!device_info) | |
993 return nullptr; | |
994 return device_info->video_capture_device(); | |
995 } | |
996 | |
997 VideoCaptureManager::DeviceEntry* | |
998 VideoCaptureManager::GetDeviceEntryForController( | |
999 const VideoCaptureController* controller) const { | |
1000 // Look up |controller| in |devices_|. | |
1001 for (DeviceEntry* device : devices_) { | |
1002 if (device->video_capture_controller() == controller) | |
1003 return device; | |
1004 } | |
1005 return nullptr; | |
1006 } | |
1007 | |
1008 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { | 960 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { |
1009 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 961 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1010 // Removal of the last client stops the device. | 962 // Removal of the last client stops the device. |
1011 if (!entry->video_capture_controller()->HasActiveClient() && | 963 if (!entry->video_capture_controller()->HasActiveClient() && |
1012 !entry->video_capture_controller()->HasPausedClient()) { | 964 !entry->video_capture_controller()->HasPausedClient()) { |
1013 DVLOG(1) << "VideoCaptureManager stopping device (type = " | 965 DVLOG(1) << "VideoCaptureManager stopping device (type = " |
1014 << entry->stream_type << ", id = " << entry->id << ")"; | 966 << entry->stream_type << ", id = " << entry->id << ")"; |
1015 | 967 |
1016 // The DeviceEntry is removed from |devices_| immediately. The controller is | 968 // The DeviceEntry is removed from |devices_| immediately. The controller is |
1017 // deleted immediately, and the device is freed asynchronously. After this | 969 // deleted immediately, and the device is freed asynchronously. After this |
1018 // point, subsequent requests to open this same device ID will create a new | 970 // point, subsequent requests to open this same device ID will create a new |
1019 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. | 971 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. |
1020 DoStopDevice(entry); | 972 DoStopDevice(entry); |
1021 DeviceEntries::iterator device_it = std::find(devices_.begin(), | 973 // TODO(mcasas): use a helper function https://crbug.com/624854. |
1022 devices_.end(), | 974 DeviceEntries::iterator device_it = |
1023 entry); | 975 std::find_if(devices_.begin(), devices_.end(), |
976 [entry](const std::unique_ptr<DeviceEntry>& device_entry) { | |
977 return device_entry.get() == entry; | |
978 }); | |
1024 devices_.erase(device_it); | 979 devices_.erase(device_it); |
1025 } | 980 } |
1026 } | 981 } |
1027 | 982 |
983 media::VideoCaptureDevice* | |
984 VideoCaptureManager::GetVideoCaptureDeviceBySessionId(int session_id) { | |
985 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
986 SessionMap::const_iterator session_it = sessions_.find(session_id); | |
987 if (session_it == sessions_.end()) | |
988 return nullptr; | |
989 | |
990 DeviceEntry* const device_info = | |
991 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); | |
992 return device_info ? device_info->video_capture_device() : nullptr; | |
993 } | |
994 | |
995 VideoCaptureManager::DeviceEntry* | |
996 VideoCaptureManager::GetDeviceEntryByTypeAndId(MediaStreamType type, | |
997 const std::string& id) const { | |
998 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
999 | |
1000 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | |
1001 if (type == device->stream_type && id == device->id) | |
1002 return device.get(); | |
1003 } | |
1004 return nullptr; | |
1005 } | |
1006 | |
1007 VideoCaptureManager::DeviceEntry* | |
1008 VideoCaptureManager::GetDeviceEntryByController( | |
1009 const VideoCaptureController* controller) const { | |
1010 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1011 | |
1012 // Look up |controller| in |devices_|. | |
1013 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | |
1014 if (device->video_capture_controller() == controller) | |
1015 return device.get(); | |
1016 } | |
1017 return nullptr; | |
1018 } | |
1019 | |
1020 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetDeviceEntryBySerialId( | |
1021 int serial_id) const { | |
1022 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1023 | |
1024 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | |
1025 if (device->serial_id == serial_id) | |
1026 return device.get(); | |
1027 } | |
1028 return nullptr; | |
1029 } | |
1030 | |
1031 media::VideoCaptureDeviceInfo* VideoCaptureManager::GetDeviceInfoById( | |
1032 const std::string& id) { | |
1033 for (auto& it : devices_info_cache_) { | |
1034 if (it.name.id() == id) | |
1035 return &(it); | |
emircan
2016/06/30 18:24:04
Remove extra ()
mcasas
2016/06/30 21:51:24
Done.
| |
1036 } | |
1037 return nullptr; | |
1038 } | |
1039 | |
1028 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetOrCreateDeviceEntry( | 1040 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetOrCreateDeviceEntry( |
1029 media::VideoCaptureSessionId capture_session_id, | 1041 media::VideoCaptureSessionId capture_session_id, |
1030 const media::VideoCaptureParams& params) { | 1042 const media::VideoCaptureParams& params) { |
1031 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1043 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1032 | 1044 |
1033 SessionMap::iterator session_it = sessions_.find(capture_session_id); | 1045 SessionMap::iterator session_it = sessions_.find(capture_session_id); |
1034 if (session_it == sessions_.end()) | 1046 if (session_it == sessions_.end()) |
1035 return nullptr; | 1047 return nullptr; |
1036 const MediaStreamDevice& device_info = session_it->second; | 1048 const MediaStreamDevice& device_info = session_it->second; |
1037 | 1049 |
1038 // Check if another session has already opened this device. If so, just | 1050 // Check if another session has already opened this device. If so, just |
1039 // use that opened device. | 1051 // use that opened device. |
1040 DeviceEntry* const existing_device = | 1052 DeviceEntry* const existing_device = |
1041 GetDeviceEntryForMediaStreamDevice(device_info); | 1053 GetDeviceEntryByTypeAndId(device_info.type, device_info.id); |
1042 if (existing_device) { | 1054 if (existing_device) { |
1043 DCHECK_EQ(device_info.type, existing_device->stream_type); | 1055 DCHECK_EQ(device_info.type, existing_device->stream_type); |
1044 return existing_device; | 1056 return existing_device; |
1045 } | 1057 } |
1046 | 1058 |
1047 const int max_buffers = device_info.type == MEDIA_TAB_VIDEO_CAPTURE ? | 1059 const int max_buffers = device_info.type == MEDIA_TAB_VIDEO_CAPTURE ? |
1048 kMaxNumberOfBuffersForTabCapture : kMaxNumberOfBuffers; | 1060 kMaxNumberOfBuffersForTabCapture : kMaxNumberOfBuffers; |
1049 std::unique_ptr<VideoCaptureController> video_capture_controller( | 1061 std::unique_ptr<VideoCaptureController> video_capture_controller( |
1050 new VideoCaptureController(max_buffers)); | 1062 new VideoCaptureController(max_buffers)); |
1051 DeviceEntry* new_device = | 1063 DeviceEntry* new_device = |
1052 new DeviceEntry(device_info.type, device_info.id, | 1064 new DeviceEntry(device_info.type, device_info.id, |
1053 std::move(video_capture_controller), params); | 1065 std::move(video_capture_controller), params); |
1054 devices_.push_back(new_device); | 1066 devices_.emplace_back(new_device); |
1055 return new_device; | 1067 return new_device; |
1056 } | 1068 } |
1057 | 1069 |
1058 media::VideoCaptureDeviceInfo* VideoCaptureManager::FindDeviceInfoById( | |
1059 const std::string& id, | |
1060 media::VideoCaptureDeviceInfos& device_vector) { | |
1061 for (auto& it : device_vector) { | |
1062 if (it.name.id() == id) | |
1063 return &(it); | |
1064 } | |
1065 return nullptr; | |
1066 } | |
1067 | |
1068 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( | 1070 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( |
1069 media::VideoCaptureDevice* device, | 1071 media::VideoCaptureDevice* device, |
1070 gfx::NativeViewId window_id) { | 1072 gfx::NativeViewId window_id) { |
1071 DCHECK(IsOnDeviceThread()); | 1073 DCHECK(IsOnDeviceThread()); |
1072 #if defined(ENABLE_SCREEN_CAPTURE) | 1074 #if defined(ENABLE_SCREEN_CAPTURE) |
1073 DesktopCaptureDevice* desktop_device = | 1075 DesktopCaptureDevice* desktop_device = |
1074 static_cast<DesktopCaptureDevice*>(device); | 1076 static_cast<DesktopCaptureDevice*>(device); |
1075 desktop_device->SetNotificationWindowId(window_id); | 1077 desktop_device->SetNotificationWindowId(window_id); |
1076 VLOG(2) << "Screen capture notification window passed on device thread."; | 1078 VLOG(2) << "Screen capture notification window passed on device thread."; |
1077 #endif | 1079 #endif |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1121 } | 1123 } |
1122 | 1124 |
1123 void VideoCaptureManager::ReleaseDevices() { | 1125 void VideoCaptureManager::ReleaseDevices() { |
1124 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1126 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1125 | 1127 |
1126 for (auto& entry : devices_) { | 1128 for (auto& entry : devices_) { |
1127 // Do not stop Content Video Capture devices, e.g. Tab or Screen capture. | 1129 // Do not stop Content Video Capture devices, e.g. Tab or Screen capture. |
1128 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) | 1130 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE) |
1129 continue; | 1131 continue; |
1130 | 1132 |
1131 DoStopDevice(entry); | 1133 DoStopDevice(entry.get()); |
1132 } | 1134 } |
1133 } | 1135 } |
1134 | 1136 |
1135 void VideoCaptureManager::ResumeDevices() { | 1137 void VideoCaptureManager::ResumeDevices() { |
1136 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1137 | 1139 |
1138 for (auto& entry : devices_) { | 1140 for (auto& entry : devices_) { |
1139 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 1141 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. |
1140 // Do not try to restart already running devices. | 1142 // Do not try to restart already running devices. |
1141 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || | 1143 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || |
1142 entry->video_capture_device()) | 1144 entry->video_capture_device()) |
1143 continue; | 1145 continue; |
1144 | 1146 |
1145 // Check if the device is already in the start queue. | 1147 // Check if the device is already in the start queue. |
1146 bool device_in_queue = false; | 1148 bool device_in_queue = false; |
1147 for (auto& request : device_start_queue_) { | 1149 for (auto& request : device_start_queue_) { |
1148 if (request.serial_id() == entry->serial_id) { | 1150 if (request.serial_id() == entry->serial_id) { |
1149 device_in_queue = true; | 1151 device_in_queue = true; |
1150 break; | 1152 break; |
1151 } | 1153 } |
1152 } | 1154 } |
1153 | 1155 |
1154 if (!device_in_queue) { | 1156 if (!device_in_queue) { |
1155 // Session ID is only valid for Screen capture. So we can fake it to | 1157 // Session ID is only valid for Screen capture. So we can fake it to |
1156 // resume video capture devices here. | 1158 // resume video capture devices here. |
1157 QueueStartDevice(kFakeSessionId, entry, entry->parameters); | 1159 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); |
1158 } | 1160 } |
1159 } | 1161 } |
1160 } | 1162 } |
1161 #endif // defined(OS_ANDROID) | 1163 #endif // defined(OS_ANDROID) |
1162 | 1164 |
1163 } // namespace content | 1165 } // namespace content |
OLD | NEW |