| 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 (observers_.find(controller_id) != observers_.end()) |
| 113 VIDEO_CAPTURE_STATE_ERROR)); | 118 observers_[controller_id]->OnStateChanged(mojom::VideoCaptureState::FAILED); |
| 119 |
| 114 DeleteVideoCaptureController(controller_id, true); | 120 DeleteVideoCaptureController(controller_id, true); |
| 115 } | 121 } |
| 116 | 122 |
| 117 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { | 123 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) { |
| 118 DVLOG(1) << __func__; | 124 DVLOG(1) << __func__; |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 if (controllers_.find(controller_id) == controllers_.end()) | 126 if (controllers_.find(controller_id) == controllers_.end()) |
| 121 return; | 127 return; |
| 122 | 128 |
| 123 Send(new VideoCaptureMsg_StateChanged(controller_id, | 129 if (observers_.find(controller_id) != observers_.end()) |
| 124 VIDEO_CAPTURE_STATE_ENDED)); | 130 observers_[controller_id]->OnStateChanged(mojom::VideoCaptureState::ENDED); |
| 131 |
| 125 DeleteVideoCaptureController(controller_id, false); | 132 DeleteVideoCaptureController(controller_id, false); |
| 126 } | 133 } |
| 127 | 134 |
| 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, | 135 void VideoCaptureHost::Start(int32_t device_id, |
| 158 int32_t session_id, | 136 int32_t session_id, |
| 159 const media::VideoCaptureParams& params) { | 137 const media::VideoCaptureParams& params, |
| 138 mojom::VideoCaptureObserverPtr observer) { |
| 160 DVLOG(1) << __func__ << " session_id=" << session_id | 139 DVLOG(1) << __func__ << " session_id=" << session_id |
| 161 << ", device_id=" << device_id << ", format=" | 140 << ", device_id=" << device_id << ", format=" |
| 162 << media::VideoCaptureFormat::ToString(params.requested_format); | 141 << media::VideoCaptureFormat::ToString(params.requested_format); |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 |
| 144 DCHECK(observers_.find(device_id) == observers_.end()); |
| 145 observers_[device_id] = std::move(observer); |
| 146 |
| 164 const VideoCaptureControllerID controller_id(device_id); | 147 const VideoCaptureControllerID controller_id(device_id); |
| 165 if (controllers_.find(controller_id) != controllers_.end()) { | 148 if (controllers_.find(controller_id) != controllers_.end()) { |
| 166 Send(new VideoCaptureMsg_StateChanged(device_id, | 149 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::STARTED); |
| 167 VIDEO_CAPTURE_STATE_ERROR)); | |
| 168 return; | 150 return; |
| 169 } | 151 } |
| 170 | 152 |
| 171 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); | 153 controllers_[controller_id] = base::WeakPtr<VideoCaptureController>(); |
| 172 media_stream_manager_->video_capture_manager()->StartCaptureForClient( | 154 media_stream_manager_->video_capture_manager()->StartCaptureForClient( |
| 173 session_id, | 155 session_id, |
| 174 params, | 156 params, |
| 175 PeerHandle(), | 157 PeerHandle(), |
| 176 controller_id, | 158 controller_id, |
| 177 this, | 159 this, |
| 178 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); | 160 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); |
| 179 } | 161 } |
| 180 | 162 |
| 181 void VideoCaptureHost::Stop(int32_t device_id) { | 163 void VideoCaptureHost::Stop(int32_t device_id) { |
| 182 DVLOG(1) << __func__ << " " << device_id; | 164 DVLOG(1) << __func__ << " " << device_id; |
| 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 184 | 166 |
| 185 VideoCaptureControllerID controller_id(device_id); | 167 VideoCaptureControllerID controller_id(device_id); |
| 186 | 168 |
| 187 Send(new VideoCaptureMsg_StateChanged(device_id, | 169 if (observers_.find(device_id) != observers_.end()) |
| 188 VIDEO_CAPTURE_STATE_STOPPED)); | 170 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::STOPPED); |
| 171 observers_.erase(controller_id); |
| 172 |
| 189 DeleteVideoCaptureController(controller_id, false); | 173 DeleteVideoCaptureController(controller_id, false); |
| 190 } | 174 } |
| 191 | 175 |
| 192 void VideoCaptureHost::Pause(int32_t device_id) { | 176 void VideoCaptureHost::Pause(int32_t device_id) { |
| 193 DVLOG(1) << __func__ << " " << device_id; | 177 DVLOG(1) << __func__ << " " << device_id; |
| 194 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 195 | 179 |
| 196 VideoCaptureControllerID controller_id(device_id); | 180 VideoCaptureControllerID controller_id(device_id); |
| 197 auto it = controllers_.find(controller_id); | 181 auto it = controllers_.find(controller_id); |
| 198 if (it == controllers_.end() || !it->second) | 182 if (it == controllers_.end() || !it->second) |
| 199 return; | 183 return; |
| 200 | 184 |
| 201 media_stream_manager_->video_capture_manager()->PauseCaptureForClient( | 185 media_stream_manager_->video_capture_manager()->PauseCaptureForClient( |
| 202 it->second.get(), controller_id, this); | 186 it->second.get(), controller_id, this); |
| 203 Send(new VideoCaptureMsg_StateChanged(device_id, VIDEO_CAPTURE_STATE_PAUSED)); | 187 if (observers_.find(device_id) != observers_.end()) |
| 188 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::PAUSED); |
| 204 } | 189 } |
| 205 | 190 |
| 206 void VideoCaptureHost::Resume(int32_t device_id, | 191 void VideoCaptureHost::Resume(int32_t device_id, |
| 207 int32_t session_id, | 192 int32_t session_id, |
| 208 const media::VideoCaptureParams& params) { | 193 const media::VideoCaptureParams& params) { |
| 209 DVLOG(1) << __func__ << " " << device_id; | 194 DVLOG(1) << __func__ << " " << device_id; |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 211 | 196 |
| 212 VideoCaptureControllerID controller_id(device_id); | 197 VideoCaptureControllerID controller_id(device_id); |
| 213 auto it = controllers_.find(controller_id); | 198 auto it = controllers_.find(controller_id); |
| 214 if (it == controllers_.end() || !it->second) | 199 if (it == controllers_.end() || !it->second) |
| 215 return; | 200 return; |
| 216 | 201 |
| 217 media_stream_manager_->video_capture_manager()->ResumeCaptureForClient( | 202 media_stream_manager_->video_capture_manager()->ResumeCaptureForClient( |
| 218 session_id, params, it->second.get(), controller_id, this); | 203 session_id, params, it->second.get(), controller_id, this); |
| 219 Send(new VideoCaptureMsg_StateChanged(device_id, | 204 if (observers_.find(device_id) != observers_.end()) |
| 220 VIDEO_CAPTURE_STATE_RESUMED)); | 205 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::RESUMED); |
| 221 } | 206 } |
| 222 | 207 |
| 223 void VideoCaptureHost::RequestRefreshFrame(int32_t device_id) { | 208 void VideoCaptureHost::RequestRefreshFrame(int32_t device_id) { |
| 224 DVLOG(1) << __func__ << " " << device_id; | 209 DVLOG(1) << __func__ << " " << device_id; |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 226 | 211 |
| 227 VideoCaptureControllerID controller_id(device_id); | 212 VideoCaptureControllerID controller_id(device_id); |
| 228 auto it = controllers_.find(controller_id); | 213 auto it = controllers_.find(controller_id); |
| 229 if (it == controllers_.end()) | 214 if (it == controllers_.end()) |
| 230 return; | 215 return; |
| 231 | 216 |
| 232 if (VideoCaptureController* controller = it->second.get()) { | 217 if (VideoCaptureController* controller = it->second.get()) { |
| 233 media_stream_manager_->video_capture_manager() | 218 media_stream_manager_->video_capture_manager() |
| 234 ->RequestRefreshFrameForClient(controller); | 219 ->RequestRefreshFrameForClient(controller); |
| 235 } | 220 } |
| 236 } | 221 } |
| 237 | 222 |
| 223 void VideoCaptureHost::ReleaseBuffer(int32_t device_id, |
| 224 int32_t buffer_id, |
| 225 const gpu::SyncToken& sync_token, |
| 226 double consumer_resource_utilization) { |
| 227 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 228 |
| 229 VideoCaptureControllerID controller_id(device_id); |
| 230 auto it = controllers_.find(controller_id); |
| 231 if (it == controllers_.end()) |
| 232 return; |
| 233 |
| 234 const base::WeakPtr<VideoCaptureController>& controller = it->second; |
| 235 if (controller) { |
| 236 controller->ReturnBuffer(controller_id, this, buffer_id, sync_token, |
| 237 consumer_resource_utilization); |
| 238 } |
| 239 } |
| 240 |
| 238 void VideoCaptureHost::GetDeviceSupportedFormats( | 241 void VideoCaptureHost::GetDeviceSupportedFormats( |
| 239 int32_t device_id, | 242 int32_t device_id, |
| 240 int32_t session_id, | 243 int32_t session_id, |
| 241 const GetDeviceSupportedFormatsCallback& callback) { | 244 const GetDeviceSupportedFormatsCallback& callback) { |
| 242 DVLOG(1) << __func__ << " " << device_id; | 245 DVLOG(1) << __func__ << " " << device_id; |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 244 media::VideoCaptureFormats supported_formats; | 247 media::VideoCaptureFormats supported_formats; |
| 245 if (!media_stream_manager_->video_capture_manager() | 248 if (!media_stream_manager_->video_capture_manager() |
| 246 ->GetDeviceSupportedFormats(session_id, &supported_formats)) { | 249 ->GetDeviceSupportedFormats(session_id, &supported_formats)) { |
| 247 DLOG(WARNING) << "Could not retrieve device supported formats"; | 250 DLOG(WARNING) << "Could not retrieve device supported formats"; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 271 auto it = controllers_.find(controller_id); | 274 auto it = controllers_.find(controller_id); |
| 272 if (it == controllers_.end()) { | 275 if (it == controllers_.end()) { |
| 273 if (controller) { | 276 if (controller) { |
| 274 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 277 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 275 controller.get(), controller_id, this, false); | 278 controller.get(), controller_id, this, false); |
| 276 } | 279 } |
| 277 return; | 280 return; |
| 278 } | 281 } |
| 279 | 282 |
| 280 if (!controller) { | 283 if (!controller) { |
| 281 Send(new VideoCaptureMsg_StateChanged(device_id, | 284 if (observers_.find(device_id) != observers_.end()) |
| 282 VIDEO_CAPTURE_STATE_ERROR)); | 285 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::FAILED); |
| 283 controllers_.erase(controller_id); | 286 controllers_.erase(controller_id); |
| 284 return; | 287 return; |
| 285 } | 288 } |
| 286 | 289 |
| 290 if (observers_.find(device_id) != observers_.end()) |
| 291 observers_[device_id]->OnStateChanged(mojom::VideoCaptureState::STARTED); |
| 292 |
| 287 DCHECK(!it->second); | 293 DCHECK(!it->second); |
| 288 it->second = controller; | 294 it->second = controller; |
| 289 } | 295 } |
| 290 | 296 |
| 291 void VideoCaptureHost::DeleteVideoCaptureController( | 297 void VideoCaptureHost::DeleteVideoCaptureController( |
| 292 VideoCaptureControllerID controller_id, bool on_error) { | 298 VideoCaptureControllerID controller_id, bool on_error) { |
| 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 294 | 300 |
| 295 auto it = controllers_.find(controller_id); | 301 auto it = controllers_.find(controller_id); |
| 296 if (it == controllers_.end()) | 302 if (it == controllers_.end()) |
| 297 return; | 303 return; |
| 298 | 304 |
| 299 if (it->second) { | 305 if (it->second) { |
| 300 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 306 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 301 it->second.get(), controller_id, this, on_error); | 307 it->second.get(), controller_id, this, on_error); |
| 302 } | 308 } |
| 303 controllers_.erase(it); | 309 controllers_.erase(it); |
| 304 } | 310 } |
| 305 | 311 |
| 306 } // namespace content | 312 } // namespace content |
| OLD | NEW |