| 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_manager.h" | 5 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 VideoCaptureManager::VideoCaptureManager() | 41 VideoCaptureManager::VideoCaptureManager() |
| 42 : vc_device_thread_("VideoCaptureManagerThread"), | 42 : vc_device_thread_("VideoCaptureManagerThread"), |
| 43 listener_(NULL), | 43 listener_(NULL), |
| 44 new_capture_session_id_(kFirstSessionId), | 44 new_capture_session_id_(kFirstSessionId), |
| 45 use_fake_device_(false) { | 45 use_fake_device_(false) { |
| 46 vc_device_thread_.Start(); | 46 vc_device_thread_.Start(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 VideoCaptureManager::~VideoCaptureManager() { | 49 VideoCaptureManager::~VideoCaptureManager() { |
| 50 // TODO(mflodman) Remove this temporary solution when shut-down issue is | |
| 51 // resolved, i.e. all code below this comment. | |
| 52 // Temporary solution: close all open devices and delete them, after the | |
| 53 // thread is stopped. | |
| 54 DLOG_IF(ERROR, !devices_.empty()) << "VideoCaptureManager: Open devices!"; | |
| 55 listener_ = NULL; | |
| 56 // The devices must be stopped on the device thread to avoid threading issues | |
| 57 // in native device code. | |
| 58 vc_device_thread_.message_loop()->PostTask( | |
| 59 FROM_HERE, | |
| 60 base::Bind(&VideoCaptureManager::TerminateOnDeviceThread, | |
| 61 base::Unretained(this))); | |
| 62 vc_device_thread_.Stop(); | 50 vc_device_thread_.Stop(); |
| 51 DCHECK(devices_.empty()); |
| 52 DCHECK(controllers_.empty()); |
| 63 } | 53 } |
| 64 | 54 |
| 65 void VideoCaptureManager::Register(MediaStreamProviderListener* listener) { | 55 void VideoCaptureManager::Register(MediaStreamProviderListener* listener) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 67 DCHECK(!listener_); | 57 DCHECK(!listener_); |
| 68 listener_ = listener; | 58 listener_ = listener; |
| 69 } | 59 } |
| 70 | 60 |
| 71 void VideoCaptureManager::Unregister() { | 61 void VideoCaptureManager::Unregister() { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 } | 182 } |
| 193 | 183 |
| 194 devices_[capture_session_id] = video_capture_device; | 184 devices_[capture_session_id] = video_capture_device; |
| 195 PostOnOpened(capture_session_id); | 185 PostOnOpened(capture_session_id); |
| 196 } | 186 } |
| 197 | 187 |
| 198 void VideoCaptureManager::OnClose(int capture_session_id) { | 188 void VideoCaptureManager::OnClose(int capture_session_id) { |
| 199 DCHECK(IsOnCaptureDeviceThread()); | 189 DCHECK(IsOnCaptureDeviceThread()); |
| 200 | 190 |
| 201 media::VideoCaptureDevice* video_capture_device = NULL; | 191 media::VideoCaptureDevice* video_capture_device = NULL; |
| 202 VideoCaptureDevices::iterator it = devices_.find(capture_session_id); | 192 VideoCaptureDevices::iterator device_it = devices_.find(capture_session_id); |
| 203 if (it != devices_.end()) { | 193 if (device_it != devices_.end()) { |
| 204 video_capture_device = it->second; | 194 video_capture_device = device_it->second; |
| 205 devices_.erase(it); | 195 devices_.erase(device_it); |
| 196 if (!DeviceInUse(video_capture_device)) { |
| 197 // No other users of this device, deallocate (if not done already) and |
| 198 // delete the device. No need to take care of the controller, that is done |
| 199 // by |OnStop|. |
| 200 video_capture_device->DeAllocate(); |
| 201 Controllers::iterator cit = controllers_.find(video_capture_device); |
| 202 if (cit != controllers_.end()) { |
| 203 delete cit->second; |
| 204 controllers_.erase(cit); |
| 205 } |
| 206 delete video_capture_device; |
| 207 } |
| 206 } | 208 } |
| 207 if (video_capture_device && !DeviceInUse(video_capture_device)) { | |
| 208 // Deallocate (if not done already) and delete the device. | |
| 209 video_capture_device->DeAllocate(); | |
| 210 Controllers::iterator cit = controllers_.find(video_capture_device); | |
| 211 if (cit != controllers_.end()) { | |
| 212 delete cit->second; | |
| 213 controllers_.erase(cit); | |
| 214 } | |
| 215 delete video_capture_device; | |
| 216 } | |
| 217 | |
| 218 PostOnClosed(capture_session_id); | 209 PostOnClosed(capture_session_id); |
| 219 } | 210 } |
| 220 | 211 |
| 221 void VideoCaptureManager::OnStart( | 212 void VideoCaptureManager::OnStart( |
| 222 const media::VideoCaptureParams capture_params, | 213 const media::VideoCaptureParams capture_params, |
| 223 media::VideoCaptureDevice::EventHandler* video_capture_receiver) { | 214 media::VideoCaptureDevice::EventHandler* video_capture_receiver) { |
| 224 DCHECK(IsOnCaptureDeviceThread()); | 215 DCHECK(IsOnCaptureDeviceThread()); |
| 225 DCHECK(video_capture_receiver != NULL); | 216 DCHECK(video_capture_receiver != NULL); |
| 226 | 217 |
| 227 media::VideoCaptureDevice* video_capture_device = | 218 media::VideoCaptureDevice* video_capture_device = |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 OnOpen(capture_session_id, device); | 493 OnOpen(capture_session_id, device); |
| 503 | 494 |
| 504 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id); | 495 VideoCaptureDevices::iterator dit = devices_.find(capture_session_id); |
| 505 if (dit != devices_.end()) { | 496 if (dit != devices_.end()) { |
| 506 return dit->second; | 497 return dit->second; |
| 507 } | 498 } |
| 508 } | 499 } |
| 509 return NULL; | 500 return NULL; |
| 510 } | 501 } |
| 511 | 502 |
| 512 void VideoCaptureManager::TerminateOnDeviceThread() { | |
| 513 DCHECK(IsOnCaptureDeviceThread()); | |
| 514 | |
| 515 std::set<media::VideoCaptureDevice*> devices_to_delete; | |
| 516 for (VideoCaptureDevices::iterator it = devices_.begin(); | |
| 517 it != devices_.end(); ++it) { | |
| 518 it->second->DeAllocate(); | |
| 519 devices_to_delete.insert(it->second); | |
| 520 } | |
| 521 STLDeleteElements(&devices_to_delete); | |
| 522 STLDeleteValues(&controllers_); | |
| 523 } | |
| 524 | |
| 525 } // namespace media_stream | 503 } // namespace media_stream |
| OLD | NEW |