Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/renderer/media/video_capture_impl_manager.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/stl_util.h" |
| 9 | #include "content/renderer/media/video_capture_impl.h" |
| 10 | #include "content/renderer/media/video_capture_message_filter.h" |
| 11 | |
| 12 | namespace content { |
| 13 | |
| 14 | VideoCaptureImplManager::VideoCaptureImplManager() |
| 15 | : thread_("VC manager") { |
| 16 | thread_.Start(); |
| 17 | message_loop_proxy_ = thread_.message_loop_proxy(); |
| 18 | filter_ = new VideoCaptureMessageFilter(); |
| 19 | } |
| 20 | |
| 21 | media::VideoCapture* VideoCaptureImplManager::AddDevice( |
| 22 | media::VideoCaptureSessionId id, |
| 23 | media::VideoCapture::EventHandler* handler) { |
| 24 | DCHECK(handler); |
| 25 | |
| 26 | base::AutoLock auto_lock(lock_); |
| 27 | Devices::iterator it = devices_.find(id); |
| 28 | if (it == devices_.end()) { |
| 29 | VideoCaptureImpl* vc = |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame^] | 30 | new VideoCaptureImpl(id, message_loop_proxy_.get(), filter_.get()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | devices_[id] = new Device(vc, handler); |
| 32 | vc->Init(); |
| 33 | return vc; |
| 34 | } |
| 35 | |
| 36 | devices_[id]->clients.push_front(handler); |
| 37 | return it->second->vc; |
| 38 | } |
| 39 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 40 | void VideoCaptureImplManager::SuspendDevices(bool suspend) { |
| 41 | base::AutoLock auto_lock(lock_); |
| 42 | for (Devices::iterator it = devices_.begin(); it != devices_.end(); ++it) |
| 43 | it->second->vc->SuspendCapture(suspend); |
| 44 | } |
| 45 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 46 | void VideoCaptureImplManager::RemoveDevice( |
| 47 | media::VideoCaptureSessionId id, |
| 48 | media::VideoCapture::EventHandler* handler) { |
| 49 | DCHECK(handler); |
| 50 | |
| 51 | base::AutoLock auto_lock(lock_); |
| 52 | Devices::iterator it = devices_.find(id); |
| 53 | if (it == devices_.end()) |
| 54 | return; |
| 55 | |
| 56 | size_t size = it->second->clients.size(); |
| 57 | it->second->clients.remove(handler); |
| 58 | |
| 59 | if (size == it->second->clients.size() || size > 1) |
| 60 | return; |
| 61 | |
| 62 | devices_[id]->vc->DeInit(base::Bind(&VideoCaptureImplManager::FreeDevice, |
| 63 | this, devices_[id]->vc)); |
| 64 | delete devices_[id]; |
| 65 | devices_.erase(id); |
| 66 | } |
| 67 | |
| 68 | void VideoCaptureImplManager::FreeDevice(VideoCaptureImpl* vc) { |
| 69 | delete vc; |
| 70 | } |
| 71 | |
| 72 | VideoCaptureImplManager::~VideoCaptureImplManager() { |
| 73 | thread_.Stop(); |
| 74 | // TODO(wjia): uncomment the line below after collecting enough info for |
| 75 | // crbug.com/152418. |
| 76 | // STLDeleteContainerPairSecondPointers(devices_.begin(), devices_.end()); |
| 77 | } |
| 78 | |
| 79 | VideoCaptureImplManager::Device::Device( |
| 80 | VideoCaptureImpl* device, |
| 81 | media::VideoCapture::EventHandler* handler) |
| 82 | : vc(device) { |
| 83 | clients.push_front(handler); |
| 84 | } |
| 85 | |
| 86 | VideoCaptureImplManager::Device::~Device() {} |
| 87 | |
| 88 | } // namespace content |