| 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_host.h" | 5 #include "content/browser/renderer_host/media/video_capture_host.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "content/browser/browser_main_loop.h" | 11 #include "content/browser/browser_main_loop.h" |
| 12 #include "content/browser/renderer_host/media/media_stream_manager.h" | 12 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 13 #include "content/browser/renderer_host/media/video_capture_manager.h" | 13 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 14 #include "content/common/media/video_capture_messages.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 15 #include "mojo/public/cpp/system/platform_handle.h" | |
| 16 | 15 |
| 17 namespace content { | 16 namespace content { |
| 18 | 17 |
| 19 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) | 18 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) |
| 20 : BrowserMessageFilter(VideoCaptureMsgStart), | 19 : media_stream_manager_(media_stream_manager), |
| 21 BrowserAssociatedInterface(this, this), | 20 weak_factory_(this) { |
| 22 media_stream_manager_(media_stream_manager) { | 21 DVLOG(1) << __func__; |
| 23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 22 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 24 } | 23 } |
| 25 | 24 |
| 26 VideoCaptureHost::~VideoCaptureHost() {} | 25 // static |
| 26 void VideoCaptureHost::Create(MediaStreamManager* media_stream_manager, |
| 27 mojom::VideoCaptureHostRequest request) { |
| 28 DVLOG(1) << __func__; |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 30 mojo::MakeStrongBinding( |
| 31 base::MakeUnique<VideoCaptureHost>(media_stream_manager), |
| 32 std::move(request)); |
| 33 } |
| 27 | 34 |
| 28 void VideoCaptureHost::OnChannelClosing() { | 35 VideoCaptureHost::~VideoCaptureHost() { |
| 29 // Since the IPC sender is gone, close all requested VideoCaptureDevices. | |
| 30 for (auto it = controllers_.begin(); it != controllers_.end(); ) { | 36 for (auto it = controllers_.begin(); it != controllers_.end(); ) { |
| 31 const base::WeakPtr<VideoCaptureController>& controller = it->second; | 37 const base::WeakPtr<VideoCaptureController>& controller = it->second; |
| 32 if (controller) { | 38 if (controller) { |
| 33 const VideoCaptureControllerID controller_id(it->first); | 39 const VideoCaptureControllerID controller_id(it->first); |
| 34 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 40 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 35 controller.get(), controller_id, this, false); | 41 controller.get(), controller_id, this, false); |
| 36 ++it; | 42 ++it; |
| 37 } else { | 43 } else { |
| 38 // Remove the entry for this controller_id so that when the controller | 44 // Remove the entry for this controller_id so that when the controller |
| 39 // is added, the controller will be notified to stop for this client | 45 // is added, the controller will be notified to stop for this client |
| 40 // in DoControllerAdded. | 46 // in DoControllerAdded. |
| 41 controllers_.erase(it++); | 47 controllers_.erase(it++); |
| 42 } | 48 } |
| 43 } | 49 } |
| 44 } | 50 } |
| 45 | 51 |
| 46 void VideoCaptureHost::OnDestruct() const { | |
| 47 BrowserThread::DeleteOnIOThread::Destruct(this); | |
| 48 } | |
| 49 | |
| 50 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message) { | |
| 51 NOTREACHED() << __func__ << " should not be receiving messages"; | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { | 52 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { |
| 56 DVLOG(1) << __func__; | 53 DVLOG(1) << __func__; |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 58 BrowserThread::PostTask( | 55 BrowserThread::PostTask( |
| 59 BrowserThread::IO, FROM_HERE, | 56 BrowserThread::IO, FROM_HERE, |
| 60 base::Bind(&VideoCaptureHost::DoError, this, controller_id)); | 57 base::Bind(&VideoCaptureHost::DoError, weak_factory_.GetWeakPtr(), |
| 58 controller_id)); |
| 61 } | 59 } |
| 62 | 60 |
| 63 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, | 61 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, |
| 64 mojo::ScopedSharedBufferHandle handle, | 62 mojo::ScopedSharedBufferHandle handle, |
| 65 int length, | 63 int length, |
| 66 int buffer_id) { | 64 int buffer_id) { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 68 if (controllers_.find(controller_id) == controllers_.end()) | 66 if (controllers_.find(controller_id) == controllers_.end()) |
| 69 return; | 67 return; |
| 70 | 68 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 105 |
| 108 device_id_to_observer_map_[controller_id]->OnBufferReady(buffer_id, | 106 device_id_to_observer_map_[controller_id]->OnBufferReady(buffer_id, |
| 109 std::move(info)); | 107 std::move(info)); |
| 110 } | 108 } |
| 111 | 109 |
| 112 void VideoCaptureHost::OnEnded(VideoCaptureControllerID controller_id) { | 110 void VideoCaptureHost::OnEnded(VideoCaptureControllerID controller_id) { |
| 113 DVLOG(1) << __func__; | 111 DVLOG(1) << __func__; |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 115 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
| 116 BrowserThread::IO, FROM_HERE, | 114 BrowserThread::IO, FROM_HERE, |
| 117 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id)); | 115 base::Bind(&VideoCaptureHost::DoEnded, weak_factory_.GetWeakPtr(), |
| 116 controller_id)); |
| 118 } | 117 } |
| 119 | 118 |
| 120 void VideoCaptureHost::Start(int32_t device_id, | 119 void VideoCaptureHost::Start(int32_t device_id, |
| 121 int32_t session_id, | 120 int32_t session_id, |
| 122 const media::VideoCaptureParams& params, | 121 const media::VideoCaptureParams& params, |
| 123 mojom::VideoCaptureObserverPtr observer) { | 122 mojom::VideoCaptureObserverPtr observer) { |
| 124 DVLOG(1) << __func__ << " session_id=" << session_id | 123 DVLOG(1) << __func__ << " session_id=" << session_id |
| 125 << ", device_id=" << device_id << ", format=" | 124 << ", device_id=" << device_id << ", format=" |
| 126 << media::VideoCaptureFormat::ToString(params.requested_format); | 125 << media::VideoCaptureFormat::ToString(params.requested_format); |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 128 | 127 |
| 129 DCHECK(!base::ContainsKey(device_id_to_observer_map_, device_id)); | 128 DCHECK(!base::ContainsKey(device_id_to_observer_map_, device_id)); |
| 130 device_id_to_observer_map_[device_id] = std::move(observer); | 129 device_id_to_observer_map_[device_id] = std::move(observer); |
| 131 | 130 |
| 132 const VideoCaptureControllerID controller_id(device_id); | 131 const VideoCaptureControllerID controller_id(device_id); |
| 133 if (controllers_.find(controller_id) != controllers_.end()) { | 132 if (controllers_.find(controller_id) != controllers_.end()) { |
| 134 device_id_to_observer_map_[device_id]->OnStateChanged( | 133 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 135 mojom::VideoCaptureState::STARTED); | 134 mojom::VideoCaptureState::STARTED); |
| 136 return; | 135 return; |
| 137 } | 136 } |
| 138 | 137 |
| 139 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); | 138 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); |
| 140 media_stream_manager_->video_capture_manager()->StartCaptureForClient( | 139 media_stream_manager_->video_capture_manager()->StartCaptureForClient( |
| 141 session_id, | 140 session_id, params, controller_id, this, |
| 142 params, | 141 base::Bind(&VideoCaptureHost::OnControllerAdded, |
| 143 controller_id, | 142 weak_factory_.GetWeakPtr(), device_id)); |
| 144 this, | |
| 145 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); | |
| 146 } | 143 } |
| 147 | 144 |
| 148 void VideoCaptureHost::Stop(int32_t device_id) { | 145 void VideoCaptureHost::Stop(int32_t device_id) { |
| 149 DVLOG(1) << __func__ << " " << device_id; | 146 DVLOG(1) << __func__ << " " << device_id; |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 | 148 |
| 152 VideoCaptureControllerID controller_id(device_id); | 149 VideoCaptureControllerID controller_id(device_id); |
| 153 | 150 |
| 154 if (base::ContainsKey(device_id_to_observer_map_, device_id)) { | 151 if (base::ContainsKey(device_id_to_observer_map_, device_id)) { |
| 155 device_id_to_observer_map_[device_id]->OnStateChanged( | 152 device_id_to_observer_map_[device_id]->OnStateChanged( |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 const base::WeakPtr<VideoCaptureController> controller = it->second; | 325 const base::WeakPtr<VideoCaptureController> controller = it->second; |
| 329 controllers_.erase(it); | 326 controllers_.erase(it); |
| 330 if (!controller) | 327 if (!controller) |
| 331 return; | 328 return; |
| 332 | 329 |
| 333 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 330 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 334 controller.get(), controller_id, this, on_error); | 331 controller.get(), controller_id, this, on_error); |
| 335 } | 332 } |
| 336 | 333 |
| 337 } // namespace content | 334 } // namespace content |
| OLD | NEW |