| 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" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // in DoControllerAdded. | 39 // in DoControllerAdded. |
| 40 controllers_.erase(it++); | 40 controllers_.erase(it++); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 | 44 |
| 45 void VideoCaptureHost::OnDestruct() const { | 45 void VideoCaptureHost::OnDestruct() const { |
| 46 BrowserThread::DeleteOnIOThread::Destruct(this); | 46 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message) { |
| 50 NOTREACHED() << __func__ << " should not be receiving messages"; |
| 51 return true; |
| 52 } |
| 53 |
| 49 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { | 54 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) { |
| 50 DVLOG(1) << __func__; | 55 DVLOG(1) << __func__; |
| 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 52 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
| 53 BrowserThread::IO, FROM_HERE, | 58 BrowserThread::IO, FROM_HERE, |
| 54 base::Bind(&VideoCaptureHost::DoError, this, controller_id)); | 59 base::Bind(&VideoCaptureHost::DoError, this, controller_id)); |
| 55 } | 60 } |
| 56 | 61 |
| 57 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, | 62 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id, |
| 58 base::SharedMemoryHandle handle, | 63 base::SharedMemoryHandle handle, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 BrowserThread::IO, FROM_HERE, | 107 BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id)); | 108 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id)); |
| 104 } | 109 } |
| 105 | 110 |
| 106 void VideoCaptureHost::DoError(VideoCaptureControllerID controller_id) { | 111 void VideoCaptureHost::DoError(VideoCaptureControllerID controller_id) { |
| 107 DVLOG(1) << __func__; | 112 DVLOG(1) << __func__; |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 if (controllers_.find(controller_id) == controllers_.end()) | 114 if (controllers_.find(controller_id) == controllers_.end()) |
| 110 return; | 115 return; |
| 111 | 116 |
| 112 Send(new VideoCaptureMsg_StateChanged(controller_id, | 117 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 113 VIDEO_CAPTURE_STATE_ERROR)); | 118 device_id_to_observer_map_[controller_id]->OnStateChanged( |
| 119 mojom::VideoCaptureState::FAILED); |
| 120 } |
| 121 |
| 114 DeleteVideoCaptureController(controller_id, true); | 122 DeleteVideoCaptureController(controller_id, true); |
| 115 } | 123 } |
| 116 | 124 |
| 117 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { | 125 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { |
| 118 DVLOG(1) << __func__; | 126 DVLOG(1) << __func__; |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 if (controllers_.find(controller_id) == controllers_.end()) | 128 if (controllers_.find(controller_id) == controllers_.end()) |
| 121 return; | 129 return; |
| 122 | 130 |
| 123 Send(new VideoCaptureMsg_StateChanged(controller_id, | 131 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 124 VIDEO_CAPTURE_STATE_ENDED)); | 132 device_id_to_observer_map_[controller_id]->OnStateChanged( |
| 133 mojom::VideoCaptureState::ENDED); |
| 134 } |
| 135 |
| 125 DeleteVideoCaptureController(controller_id, false); | 136 DeleteVideoCaptureController(controller_id, false); |
| 126 } | 137 } |
| 127 | 138 |
| 128 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message) { | |
| 129 bool handled = true; | |
| 130 IPC_BEGIN_MESSAGE_MAP(VideoCaptureHost, message) | |
| 131 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, | |
| 132 OnRendererFinishedWithBuffer) | |
| 133 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 134 IPC_END_MESSAGE_MAP() | |
| 135 | |
| 136 return handled; | |
| 137 } | |
| 138 | |
| 139 void VideoCaptureHost::OnRendererFinishedWithBuffer( | |
| 140 int device_id, | |
| 141 int buffer_id, | |
| 142 const gpu::SyncToken& sync_token, | |
| 143 double consumer_resource_utilization) { | |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 145 | |
| 146 VideoCaptureControllerID controller_id(device_id); | |
| 147 auto it = controllers_.find(controller_id); | |
| 148 if (it != controllers_.end()) { | |
| 149 const base::WeakPtr<VideoCaptureController>& controller = it->second; | |
| 150 if (controller) { | |
| 151 controller->ReturnBuffer(controller_id, this, buffer_id, sync_token, | |
| 152 consumer_resource_utilization); | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void VideoCaptureHost::Start(int32_t device_id, | 139 void VideoCaptureHost::Start(int32_t device_id, |
| 158 int32_t session_id, | 140 int32_t session_id, |
| 159 const media::VideoCaptureParams& params) { | 141 const media::VideoCaptureParams& params, |
| 142 mojom::VideoCaptureObserverPtr observer) { |
| 160 DVLOG(1) << __func__ << " session_id=" << session_id | 143 DVLOG(1) << __func__ << " session_id=" << session_id |
| 161 << ", device_id=" << device_id << ", format=" | 144 << ", device_id=" << device_id << ", format=" |
| 162 << media::VideoCaptureFormat::ToString(params.requested_format); | 145 << media::VideoCaptureFormat::ToString(params.requested_format); |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 146 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 147 |
| 148 DCHECK(!base::ContainsKey(device_id_to_observer_map_, device_id)); |
| 149 device_id_to_observer_map_[device_id] = std::move(observer); |
| 150 |
| 164 const VideoCaptureControllerID controller_id(device_id); | 151 const VideoCaptureControllerID controller_id(device_id); |
| 165 if (controllers_.find(controller_id) != controllers_.end()) { | 152 if (controllers_.find(controller_id) != controllers_.end()) { |
| 166 Send(new VideoCaptureMsg_StateChanged(device_id, | 153 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 167 VIDEO_CAPTURE_STATE_ERROR)); | 154 mojom::VideoCaptureState::STARTED); |
| 168 return; | 155 return; |
| 169 } | 156 } |
| 170 | 157 |
| 171 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); | 158 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); |
| 172 media_stream_manager_->video_capture_manager()->StartCaptureForClient( | 159 media_stream_manager_->video_capture_manager()->StartCaptureForClient( |
| 173 session_id, | 160 session_id, |
| 174 params, | 161 params, |
| 175 PeerHandle(), | 162 PeerHandle(), |
| 176 controller_id, | 163 controller_id, |
| 177 this, | 164 this, |
| 178 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); | 165 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); |
| 179 } | 166 } |
| 180 | 167 |
| 181 void VideoCaptureHost::Stop(int32_t device_id) { | 168 void VideoCaptureHost::Stop(int32_t device_id) { |
| 182 DVLOG(1) << __func__ << " " << device_id; | 169 DVLOG(1) << __func__ << " " << device_id; |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 170 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 | 171 |
| 185 VideoCaptureControllerID controller_id(device_id); | 172 VideoCaptureControllerID controller_id(device_id); |
| 186 | 173 |
| 187 Send(new VideoCaptureMsg_StateChanged(device_id, | 174 if (base::ContainsKey(device_id_to_observer_map_, device_id)) { |
| 188 VIDEO_CAPTURE_STATE_STOPPED)); | 175 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 176 mojom::VideoCaptureState::STOPPED); |
| 177 } |
| 178 device_id_to_observer_map_.erase(controller_id); |
| 179 |
| 189 DeleteVideoCaptureController(controller_id, false); | 180 DeleteVideoCaptureController(controller_id, false); |
| 190 } | 181 } |
| 191 | 182 |
| 192 void VideoCaptureHost::Pause(int32_t device_id) { | 183 void VideoCaptureHost::Pause(int32_t device_id) { |
| 193 DVLOG(1) << __func__ << " " << device_id; | 184 DVLOG(1) << __func__ << " " << device_id; |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 195 | 186 |
| 196 VideoCaptureControllerID controller_id(device_id); | 187 VideoCaptureControllerID controller_id(device_id); |
| 197 auto it = controllers_.find(controller_id); | 188 auto it = controllers_.find(controller_id); |
| 198 if (it == controllers_.end() || !it->second) | 189 if (it == controllers_.end() || !it->second) |
| 199 return; | 190 return; |
| 200 | 191 |
| 201 media_stream_manager_->video_capture_manager()->PauseCaptureForClient( | 192 media_stream_manager_->video_capture_manager()->PauseCaptureForClient( |
| 202 it->second.get(), controller_id, this); | 193 it->second.get(), controller_id, this); |
| 203 Send(new VideoCaptureMsg_StateChanged(device_id, VIDEO_CAPTURE_STATE_PAUSED)); | 194 if (base::ContainsKey(device_id_to_observer_map_, device_id)) { |
| 195 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 196 mojom::VideoCaptureState::PAUSED); |
| 197 } |
| 204 } | 198 } |
| 205 | 199 |
| 206 void VideoCaptureHost::Resume(int32_t device_id, | 200 void VideoCaptureHost::Resume(int32_t device_id, |
| 207 int32_t session_id, | 201 int32_t session_id, |
| 208 const media::VideoCaptureParams& params) { | 202 const media::VideoCaptureParams& params) { |
| 209 DVLOG(1) << __func__ << " " << device_id; | 203 DVLOG(1) << __func__ << " " << device_id; |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 204 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 211 | 205 |
| 212 VideoCaptureControllerID controller_id(device_id); | 206 VideoCaptureControllerID controller_id(device_id); |
| 213 auto it = controllers_.find(controller_id); | 207 auto it = controllers_.find(controller_id); |
| 214 if (it == controllers_.end() || !it->second) | 208 if (it == controllers_.end() || !it->second) |
| 215 return; | 209 return; |
| 216 | 210 |
| 217 media_stream_manager_->video_capture_manager()->ResumeCaptureForClient( | 211 media_stream_manager_->video_capture_manager()->ResumeCaptureForClient( |
| 218 session_id, params, it->second.get(), controller_id, this); | 212 session_id, params, it->second.get(), controller_id, this); |
| 219 Send(new VideoCaptureMsg_StateChanged(device_id, | 213 if (base::ContainsKey(device_id_to_observer_map_, device_id)) { |
| 220 VIDEO_CAPTURE_STATE_RESUMED)); | 214 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 215 mojom::VideoCaptureState::RESUMED); |
| 216 } |
| 221 } | 217 } |
| 222 | 218 |
| 223 void VideoCaptureHost::RequestRefreshFrame(int32_t device_id) { | 219 void VideoCaptureHost::RequestRefreshFrame(int32_t device_id) { |
| 224 DVLOG(1) << __func__ << " " << device_id; | 220 DVLOG(1) << __func__ << " " << device_id; |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 226 | 222 |
| 227 VideoCaptureControllerID controller_id(device_id); | 223 VideoCaptureControllerID controller_id(device_id); |
| 228 auto it = controllers_.find(controller_id); | 224 auto it = controllers_.find(controller_id); |
| 229 if (it == controllers_.end()) | 225 if (it == controllers_.end()) |
| 230 return; | 226 return; |
| 231 | 227 |
| 232 if (VideoCaptureController* controller = it->second.get()) { | 228 if (VideoCaptureController* controller = it->second.get()) { |
| 233 media_stream_manager_->video_capture_manager() | 229 media_stream_manager_->video_capture_manager() |
| 234 ->RequestRefreshFrameForClient(controller); | 230 ->RequestRefreshFrameForClient(controller); |
| 235 } | 231 } |
| 236 } | 232 } |
| 237 | 233 |
| 234 void VideoCaptureHost::ReleaseBuffer(int32_t device_id, |
| 235 int32_t buffer_id, |
| 236 const gpu::SyncToken& sync_token, |
| 237 double consumer_resource_utilization) { |
| 238 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 239 |
| 240 VideoCaptureControllerID controller_id(device_id); |
| 241 auto it = controllers_.find(controller_id); |
| 242 if (it == controllers_.end()) |
| 243 return; |
| 244 |
| 245 const base::WeakPtr<VideoCaptureController>& controller = it->second; |
| 246 if (controller) { |
| 247 controller->ReturnBuffer(controller_id, this, buffer_id, sync_token, |
| 248 consumer_resource_utilization); |
| 249 } |
| 250 } |
| 251 |
| 238 void VideoCaptureHost::GetDeviceSupportedFormats( | 252 void VideoCaptureHost::GetDeviceSupportedFormats( |
| 239 int32_t device_id, | 253 int32_t device_id, |
| 240 int32_t session_id, | 254 int32_t session_id, |
| 241 const GetDeviceSupportedFormatsCallback& callback) { | 255 const GetDeviceSupportedFormatsCallback& callback) { |
| 242 DVLOG(1) << __func__ << " " << device_id; | 256 DVLOG(1) << __func__ << " " << device_id; |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 257 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 244 media::VideoCaptureFormats supported_formats; | 258 media::VideoCaptureFormats supported_formats; |
| 245 if (!media_stream_manager_->video_capture_manager() | 259 if (!media_stream_manager_->video_capture_manager() |
| 246 ->GetDeviceSupportedFormats(session_id, &supported_formats)) { | 260 ->GetDeviceSupportedFormats(session_id, &supported_formats)) { |
| 247 DLOG(WARNING) << "Could not retrieve device supported formats"; | 261 DLOG(WARNING) << "Could not retrieve device supported formats"; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 271 auto it = controllers_.find(controller_id); | 285 auto it = controllers_.find(controller_id); |
| 272 if (it == controllers_.end()) { | 286 if (it == controllers_.end()) { |
| 273 if (controller) { | 287 if (controller) { |
| 274 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 288 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 275 controller.get(), controller_id, this, false); | 289 controller.get(), controller_id, this, false); |
| 276 } | 290 } |
| 277 return; | 291 return; |
| 278 } | 292 } |
| 279 | 293 |
| 280 if (!controller) { | 294 if (!controller) { |
| 281 Send(new VideoCaptureMsg_StateChanged(device_id, | 295 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 282 VIDEO_CAPTURE_STATE_ERROR)); | 296 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 297 mojom::VideoCaptureState::FAILED); |
| 298 } |
| 283 controllers_.erase(controller_id); | 299 controllers_.erase(controller_id); |
| 284 return; | 300 return; |
| 285 } | 301 } |
| 286 | 302 |
| 303 if (base::ContainsKey(device_id_to_observer_map_, controller_id)) { |
| 304 device_id_to_observer_map_[device_id]->OnStateChanged( |
| 305 mojom::VideoCaptureState::STARTED); |
| 306 } |
| 307 |
| 287 DCHECK(!it->second); | 308 DCHECK(!it->second); |
| 288 it->second = controller; | 309 it->second = controller; |
| 289 } | 310 } |
| 290 | 311 |
| 291 void VideoCaptureHost::DeleteVideoCaptureController( | 312 void VideoCaptureHost::DeleteVideoCaptureController( |
| 292 VideoCaptureControllerID controller_id, bool on_error) { | 313 VideoCaptureControllerID controller_id, bool on_error) { |
| 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 314 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 294 | 315 |
| 295 auto it = controllers_.find(controller_id); | 316 auto it = controllers_.find(controller_id); |
| 296 if (it == controllers_.end()) | 317 if (it == controllers_.end()) |
| 297 return; | 318 return; |
| 298 | 319 |
| 299 if (it->second) { | 320 if (it->second) { |
| 300 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 321 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 301 it->second.get(), controller_id, this, on_error); | 322 it->second.get(), controller_id, this, on_error); |
| 302 } | 323 } |
| 303 controllers_.erase(it); | 324 controllers_.erase(it); |
| 304 } | 325 } |
| 305 | 326 |
| 306 } // namespace content | 327 } // namespace content |
| OLD | NEW |