| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stl_util.h" | |
| 10 #include "content/browser/browser_main_loop.h" | 9 #include "content/browser/browser_main_loop.h" |
| 11 #include "content/browser/renderer_host/media/media_stream_manager.h" | 10 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 12 #include "content/browser/renderer_host/media/video_capture_manager.h" | 11 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 13 #include "content/common/media/video_capture_messages.h" | 12 #include "content/common/media/video_capture_messages.h" |
| 14 | 13 |
| 15 namespace content { | 14 namespace content { |
| 16 | 15 |
| 17 struct VideoCaptureHost::Entry { | |
| 18 Entry(VideoCaptureController* controller) | |
| 19 : controller(controller) {} | |
| 20 | |
| 21 ~Entry() {} | |
| 22 | |
| 23 scoped_refptr<VideoCaptureController> controller; | |
| 24 }; | |
| 25 | |
| 26 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) | 16 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) |
| 27 : media_stream_manager_(media_stream_manager) { | 17 : media_stream_manager_(media_stream_manager) { |
| 28 } | 18 } |
| 29 | 19 |
| 30 VideoCaptureHost::~VideoCaptureHost() {} | 20 VideoCaptureHost::~VideoCaptureHost() {} |
| 31 | 21 |
| 32 void VideoCaptureHost::OnChannelClosing() { | 22 void VideoCaptureHost::OnChannelClosing() { |
| 33 BrowserMessageFilter::OnChannelClosing(); | 23 BrowserMessageFilter::OnChannelClosing(); |
| 34 | 24 |
| 35 // Since the IPC channel is gone, close all requested VideoCaptureDevices. | 25 // Since the IPC channel is gone, close all requested VideoCaptureDevices. |
| 36 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { | 26 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { |
| 37 VideoCaptureController* controller = it->second->controller.get(); | 27 const base::WeakPtr<VideoCaptureController>& controller = it->second; |
| 38 if (controller) { | 28 if (controller) { |
| 39 VideoCaptureControllerID controller_id(it->first); | 29 VideoCaptureControllerID controller_id(it->first); |
| 40 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 30 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 41 controller, controller_id, this); | 31 controller.get(), controller_id, this); |
| 42 } | 32 } |
| 43 } | 33 } |
| 44 STLDeleteValues(&entries_); | |
| 45 } | 34 } |
| 46 | 35 |
| 47 void VideoCaptureHost::OnDestruct() const { | 36 void VideoCaptureHost::OnDestruct() const { |
| 48 BrowserThread::DeleteOnIOThread::Destruct(this); | 37 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 49 } | 38 } |
| 50 | 39 |
| 51 /////////////////////////////////////////////////////////////////////////////// | 40 /////////////////////////////////////////////////////////////////////////////// |
| 52 | 41 |
| 53 // Implements VideoCaptureControllerEventHandler. | 42 // Implements VideoCaptureControllerEventHandler. |
| 54 void VideoCaptureHost::OnError(const VideoCaptureControllerID& controller_id) { | 43 void VideoCaptureHost::OnError(const VideoCaptureControllerID& controller_id) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 DVLOG(1) << "VideoCaptureHost::OnStartCapture, device_id " << device_id | 204 DVLOG(1) << "VideoCaptureHost::OnStartCapture, device_id " << device_id |
| 216 << ", (" << params.width << ", " << params.height << ", " | 205 << ", (" << params.width << ", " << params.height << ", " |
| 217 << params.frame_rate << ", " << params.session_id | 206 << params.frame_rate << ", " << params.session_id |
| 218 << ", variable resolution device:" | 207 << ", variable resolution device:" |
| 219 << ((params.frame_size_type == | 208 << ((params.frame_size_type == |
| 220 media::VariableResolutionVideoCaptureDevice) ? "yes" : "no") | 209 media::VariableResolutionVideoCaptureDevice) ? "yes" : "no") |
| 221 << ")"; | 210 << ")"; |
| 222 VideoCaptureControllerID controller_id(device_id); | 211 VideoCaptureControllerID controller_id(device_id); |
| 223 DCHECK(entries_.find(controller_id) == entries_.end()); | 212 DCHECK(entries_.find(controller_id) == entries_.end()); |
| 224 | 213 |
| 225 entries_[controller_id] = new Entry(NULL); | 214 entries_[controller_id] = base::WeakPtr<VideoCaptureController>(); |
| 226 media_stream_manager_->video_capture_manager()->StartCaptureForClient( | 215 media_stream_manager_->video_capture_manager()->StartCaptureForClient( |
| 227 params, PeerHandle(), controller_id, this, base::Bind( | 216 params, PeerHandle(), controller_id, this, base::Bind( |
| 228 &VideoCaptureHost::OnControllerAdded, this, device_id, params)); | 217 &VideoCaptureHost::OnControllerAdded, this, device_id, params)); |
| 229 } | 218 } |
| 230 | 219 |
| 231 void VideoCaptureHost::OnControllerAdded( | 220 void VideoCaptureHost::OnControllerAdded( |
| 232 int device_id, const media::VideoCaptureParams& params, | 221 int device_id, const media::VideoCaptureParams& params, |
| 233 VideoCaptureController* controller) { | 222 const base::WeakPtr<VideoCaptureController>& controller) { |
| 234 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
| 235 BrowserThread::IO, FROM_HERE, | 224 BrowserThread::IO, FROM_HERE, |
| 236 base::Bind(&VideoCaptureHost::DoControllerAddedOnIOThread, | 225 base::Bind(&VideoCaptureHost::DoControllerAddedOnIOThread, |
| 237 this, device_id, params, make_scoped_refptr(controller))); | 226 this, device_id, params, controller)); |
| 238 } | 227 } |
| 239 | 228 |
| 240 void VideoCaptureHost::DoControllerAddedOnIOThread( | 229 void VideoCaptureHost::DoControllerAddedOnIOThread( |
| 241 int device_id, const media::VideoCaptureParams params, | 230 int device_id, const media::VideoCaptureParams params, |
| 242 VideoCaptureController* controller) { | 231 const base::WeakPtr<VideoCaptureController>& controller) { |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 244 VideoCaptureControllerID controller_id(device_id); | 233 VideoCaptureControllerID controller_id(device_id); |
| 245 EntryMap::iterator it = entries_.find(controller_id); | 234 EntryMap::iterator it = entries_.find(controller_id); |
| 246 if (it == entries_.end()) { | 235 if (it == entries_.end()) { |
| 247 if (controller) { | 236 if (controller) { |
| 248 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 237 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 249 controller, controller_id, this); | 238 controller.get(), controller_id, this); |
| 250 } | 239 } |
| 251 return; | 240 return; |
| 252 } | 241 } |
| 253 | 242 |
| 254 if (controller == NULL) { | 243 if (!controller) { |
| 255 Send(new VideoCaptureMsg_StateChanged(device_id, | 244 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 256 VIDEO_CAPTURE_STATE_ERROR)); | 245 VIDEO_CAPTURE_STATE_ERROR)); |
| 257 delete it->second; | |
| 258 entries_.erase(controller_id); | 246 entries_.erase(controller_id); |
| 259 return; | 247 return; |
| 260 } | 248 } |
| 261 | 249 |
| 262 it->second->controller = controller; | 250 DCHECK(!it->second); |
| 251 it->second = controller; |
| 263 } | 252 } |
| 264 | 253 |
| 265 void VideoCaptureHost::OnStopCapture(int device_id) { | 254 void VideoCaptureHost::OnStopCapture(int device_id) { |
| 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 267 DVLOG(1) << "VideoCaptureHost::OnStopCapture, device_id " << device_id; | 256 DVLOG(1) << "VideoCaptureHost::OnStopCapture, device_id " << device_id; |
| 268 | 257 |
| 269 VideoCaptureControllerID controller_id(device_id); | 258 VideoCaptureControllerID controller_id(device_id); |
| 270 | 259 |
| 271 Send(new VideoCaptureMsg_StateChanged(device_id, | 260 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 272 VIDEO_CAPTURE_STATE_STOPPED)); | 261 VIDEO_CAPTURE_STATE_STOPPED)); |
| 273 DeleteVideoCaptureControllerOnIOThread(controller_id); | 262 DeleteVideoCaptureControllerOnIOThread(controller_id); |
| 274 } | 263 } |
| 275 | 264 |
| 276 void VideoCaptureHost::OnPauseCapture(int device_id) { | 265 void VideoCaptureHost::OnPauseCapture(int device_id) { |
| 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 278 DVLOG(1) << "VideoCaptureHost::OnPauseCapture, device_id " << device_id; | 267 DVLOG(1) << "VideoCaptureHost::OnPauseCapture, device_id " << device_id; |
| 279 // Not used. | 268 // Not used. |
| 280 Send(new VideoCaptureMsg_StateChanged(device_id, VIDEO_CAPTURE_STATE_ERROR)); | 269 Send(new VideoCaptureMsg_StateChanged(device_id, VIDEO_CAPTURE_STATE_ERROR)); |
| 281 } | 270 } |
| 282 | 271 |
| 283 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { | 272 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { |
| 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 285 | 274 |
| 286 VideoCaptureControllerID controller_id(device_id); | 275 VideoCaptureControllerID controller_id(device_id); |
| 287 EntryMap::iterator it = entries_.find(controller_id); | 276 EntryMap::iterator it = entries_.find(controller_id); |
| 288 if (it != entries_.end()) { | 277 if (it != entries_.end()) { |
| 289 scoped_refptr<VideoCaptureController> controller = it->second->controller; | 278 const base::WeakPtr<VideoCaptureController>& controller = it->second; |
| 290 if (controller.get()) | 279 if (controller) |
| 291 controller->ReturnBuffer(controller_id, this, buffer_id); | 280 controller->ReturnBuffer(controller_id, this, buffer_id); |
| 292 } | 281 } |
| 293 } | 282 } |
| 294 | 283 |
| 295 void VideoCaptureHost::DeleteVideoCaptureControllerOnIOThread( | 284 void VideoCaptureHost::DeleteVideoCaptureControllerOnIOThread( |
| 296 const VideoCaptureControllerID& controller_id) { | 285 const VideoCaptureControllerID& controller_id) { |
| 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 298 | 287 |
| 299 EntryMap::iterator it = entries_.find(controller_id); | 288 EntryMap::iterator it = entries_.find(controller_id); |
| 300 if (it == entries_.end()) | 289 if (it == entries_.end()) |
| 301 return; | 290 return; |
| 302 | 291 |
| 303 VideoCaptureController* controller = it->second->controller.get(); | 292 if (it->second) { |
| 304 if (controller) { | |
| 305 media_stream_manager_->video_capture_manager()->StopCaptureForClient( | 293 media_stream_manager_->video_capture_manager()->StopCaptureForClient( |
| 306 controller, controller_id, this); | 294 it->second.get(), controller_id, this); |
| 307 } | 295 } |
| 308 delete it->second; | 296 entries_.erase(it); |
| 309 entries_.erase(controller_id); | |
| 310 } | 297 } |
| 311 | 298 |
| 312 } // namespace content | 299 } // namespace content |
| OLD | NEW |