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 "content/common/media/video_capture_messages.h" |
| 15 #include "mojo/public/cpp/system/platform_handle.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) | 19 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) |
19 : BrowserMessageFilter(VideoCaptureMsgStart), | 20 : BrowserMessageFilter(VideoCaptureMsgStart), |
20 BrowserAssociatedInterface(this, this), | 21 BrowserAssociatedInterface(this, this), |
21 media_stream_manager_(media_stream_manager) { | 22 media_stream_manager_(media_stream_manager) { |
22 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 23 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
23 } | 24 } |
24 | 25 |
(...skipping 28 matching lines...) Expand all Loading... |
53 | 54 |
54 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { | 55 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { |
55 DVLOG(1) << __func__; | 56 DVLOG(1) << __func__; |
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
57 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
58 BrowserThread::IO, FROM_HERE, | 59 BrowserThread::IO, FROM_HERE, |
59 base::Bind(&VideoCaptureHost::DoError, this, controller_id)); | 60 base::Bind(&VideoCaptureHost::DoError, this, controller_id)); |
60 } | 61 } |
61 | 62 |
62 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, | 63 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, |
63 base::SharedMemoryHandle handle, | 64 mojo::ScopedSharedBufferHandle handle, |
64 int length, | 65 int length, |
65 int buffer_id) { | 66 int buffer_id) { |
66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 67 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
67 if (controllers_.find(controller_id) == controllers_.end()) | 68 if (controllers_.find(controller_id) == controllers_.end()) |
68 return; | 69 return; |
69 | 70 |
70 Send(new VideoCaptureMsg_NewBuffer(controller_id, handle, length, buffer_id)); | 71 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 72 device_id_to_observer_map_[controller_id]->OnBufferCreated( |
| 73 buffer_id, std::move(handle)); |
| 74 } |
71 } | 75 } |
72 | 76 |
73 void VideoCaptureHost::OnBufferDestroyed(VideoCaptureControllerID controller_id, | 77 void VideoCaptureHost::OnBufferDestroyed(VideoCaptureControllerID controller_id, |
74 int buffer_id) { | 78 int buffer_id) { |
75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
76 if (controllers_.find(controller_id) == controllers_.end()) | 80 if (controllers_.find(controller_id) == controllers_.end()) |
77 return; | 81 return; |
78 | 82 |
79 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) | 83 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) |
80 device_id_to_observer_map_[controller_id]->OnBufferDestroyed(buffer_id); | 84 device_id_to_observer_map_[controller_id]->OnBufferDestroyed(buffer_id); |
(...skipping 25 matching lines...) Expand all Loading... |
106 } | 110 } |
107 | 111 |
108 void VideoCaptureHost::OnEnded(VideoCaptureControllerID controller_id) { | 112 void VideoCaptureHost::OnEnded(VideoCaptureControllerID controller_id) { |
109 DVLOG(1) << __func__; | 113 DVLOG(1) << __func__; |
110 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
111 BrowserThread::PostTask( | 115 BrowserThread::PostTask( |
112 BrowserThread::IO, FROM_HERE, | 116 BrowserThread::IO, FROM_HERE, |
113 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id)); | 117 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id)); |
114 } | 118 } |
115 | 119 |
116 void VideoCaptureHost::DoError(VideoCaptureControllerID controller_id) { | |
117 DVLOG(1) << __func__; | |
118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
119 if (controllers_.find(controller_id) == controllers_.end()) | |
120 return; | |
121 | |
122 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { | |
123 device_id_to_observer_map_[controller_id]->OnStateChanged( | |
124 mojom::VideoCaptureState::FAILED); | |
125 } | |
126 | |
127 DeleteVideoCaptureController(controller_id, true); | |
128 } | |
129 | |
130 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { | |
131 DVLOG(1) << __func__; | |
132 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
133 if (controllers_.find(controller_id) == controllers_.end()) | |
134 return; | |
135 | |
136 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { | |
137 device_id_to_observer_map_[controller_id]->OnStateChanged( | |
138 mojom::VideoCaptureState::ENDED); | |
139 } | |
140 | |
141 DeleteVideoCaptureController(controller_id, false); | |
142 } | |
143 | |
144 void VideoCaptureHost::Start(int32_t device_id, | 120 void VideoCaptureHost::Start(int32_t device_id, |
145 int32_t session_id, | 121 int32_t session_id, |
146 const media::VideoCaptureParams& params, | 122 const media::VideoCaptureParams& params, |
147 mojom::VideoCaptureObserverPtr observer) { | 123 mojom::VideoCaptureObserverPtr observer) { |
148 DVLOG(1) << __func__ << " session_id=" << session_id | 124 DVLOG(1) << __func__ << " session_id=" << session_id |
149 << ", device_id=" << device_id << ", format=" | 125 << ", device_id=" << device_id << ", format=" |
150 << media::VideoCaptureFormat::ToString(params.requested_format); | 126 << media::VideoCaptureFormat::ToString(params.requested_format); |
151 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
152 | 128 |
153 DCHECK(!base::ContainsKey(device_id_to_observer_map_, device_id)); | 129 DCHECK(!base::ContainsKey(device_id_to_observer_map_, device_id)); |
154 device_id_to_observer_map_[device_id] = std::move(observer); | 130 device_id_to_observer_map_[device_id] = std::move(observer); |
155 | 131 |
156 const VideoCaptureControllerID controller_id(device_id); | 132 const VideoCaptureControllerID controller_id(device_id); |
157 if (controllers_.find(controller_id) != controllers_.end()) { | 133 if (controllers_.find(controller_id) != controllers_.end()) { |
158 device_id_to_observer_map_[device_id]->OnStateChanged( | 134 device_id_to_observer_map_[device_id]->OnStateChanged( |
159 mojom::VideoCaptureState::STARTED); | 135 mojom::VideoCaptureState::STARTED); |
160 return; | 136 return; |
161 } | 137 } |
162 | 138 |
163 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); | 139 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); |
164 media_stream_manager_->video_capture_manager()->StartCaptureForClient( | 140 media_stream_manager_->video_capture_manager()->StartCaptureForClient( |
165 session_id, | 141 session_id, |
166 params, | 142 params, |
167 PeerHandle(), | |
168 controller_id, | 143 controller_id, |
169 this, | 144 this, |
170 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); | 145 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); |
171 } | 146 } |
172 | 147 |
173 void VideoCaptureHost::Stop(int32_t device_id) { | 148 void VideoCaptureHost::Stop(int32_t device_id) { |
174 DVLOG(1) << __func__ << " " << device_id; | 149 DVLOG(1) << __func__ << " " << device_id; |
175 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
176 | 151 |
177 VideoCaptureControllerID controller_id(device_id); | 152 VideoCaptureControllerID controller_id(device_id); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 DVLOG(1) << __func__ << " " << device_id; | 250 DVLOG(1) << __func__ << " " << device_id; |
276 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 251 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
277 media::VideoCaptureFormats formats_in_use; | 252 media::VideoCaptureFormats formats_in_use; |
278 if (!media_stream_manager_->video_capture_manager()->GetDeviceFormatsInUse( | 253 if (!media_stream_manager_->video_capture_manager()->GetDeviceFormatsInUse( |
279 session_id, &formats_in_use)) { | 254 session_id, &formats_in_use)) { |
280 DLOG(WARNING) << "Could not retrieve device format(s) in use"; | 255 DLOG(WARNING) << "Could not retrieve device format(s) in use"; |
281 } | 256 } |
282 callback.Run(formats_in_use); | 257 callback.Run(formats_in_use); |
283 } | 258 } |
284 | 259 |
| 260 void VideoCaptureHost::DoError(VideoCaptureControllerID controller_id) { |
| 261 DVLOG(1) << __func__; |
| 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 263 if (controllers_.find(controller_id) == controllers_.end()) |
| 264 return; |
| 265 |
| 266 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 267 device_id_to_observer_map_[controller_id]->OnStateChanged( |
| 268 mojom::VideoCaptureState::FAILED); |
| 269 } |
| 270 |
| 271 DeleteVideoCaptureController(controller_id, true); |
| 272 } |
| 273 |
| 274 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { |
| 275 DVLOG(1) << __func__; |
| 276 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 277 if (controllers_.find(controller_id) == controllers_.end()) |
| 278 return; |
| 279 |
| 280 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 281 device_id_to_observer_map_[controller_id]->OnStateChanged( |
| 282 mojom::VideoCaptureState::ENDED); |
| 283 } |
| 284 |
| 285 DeleteVideoCaptureController(controller_id, false); |
| 286 } |
| 287 |
285 void VideoCaptureHost::OnControllerAdded( | 288 void VideoCaptureHost::OnControllerAdded( |
286 int device_id, | 289 int device_id, |
287 const base::WeakPtr<VideoCaptureController>& controller) { | 290 const base::WeakPtr<VideoCaptureController>& controller) { |
288 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 291 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
289 VideoCaptureControllerID controller_id(device_id); | 292 VideoCaptureControllerID controller_id(device_id); |
290 auto it = controllers_.find(controller_id); | 293 auto it = controllers_.find(controller_id); |
291 if (it == controllers_.end()) { | 294 if (it == controllers_.end()) { |
292 if (controller) { | 295 if (controller) { |
293 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 296 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
294 controller.get(), controller_id, this, false); | 297 controller.get(), controller_id, this, false); |
(...skipping 30 matching lines...) Expand all Loading... |
325 const base::WeakPtr<VideoCaptureController> controller = it->second; | 328 const base::WeakPtr<VideoCaptureController> controller = it->second; |
326 controllers_.erase(it); | 329 controllers_.erase(it); |
327 if (!controller) | 330 if (!controller) |
328 return; | 331 return; |
329 | 332 |
330 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 333 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
331 controller.get(), controller_id, this, on_error); | 334 controller.get(), controller_id, this, on_error); |
332 } | 335 } |
333 | 336 |
334 } // namespace content | 337 } // namespace content |
OLD | NEW |