blob: 672fbd403c3374b0ed97a645119027e29808e7a9 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
12namespace content {
13
14VideoCaptureImplManager::VideoCaptureImplManager()
15 : thread_("VC manager") {
16 thread_.Start();
17 message_loop_proxy_ = thread_.message_loop_proxy();
18 filter_ = new VideoCaptureMessageFilter();
19}
20
21media::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)868fa2f2013-06-11 10:57:03 +010030 new VideoCaptureImpl(id, message_loop_proxy_.get(), filter_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031 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)c2e0dbd2013-05-09 18:35:53 +010040void 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)58218062012-11-14 11:43:16 +000046void 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
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062 devices_[id]->vc->DeInit(base::Bind(&VideoCaptureImplManager::FreeDevice,
63 this, devices_[id]->vc));
64 delete devices_[id];
65 devices_.erase(id);
66}
67
68void VideoCaptureImplManager::FreeDevice(VideoCaptureImpl* vc) {
69 delete vc;
70}
71
72VideoCaptureImplManager::~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
79VideoCaptureImplManager::Device::Device(
80 VideoCaptureImpl* device,
81 media::VideoCapture::EventHandler* handler)
82 : vc(device) {
83 clients.push_front(handler);
84}
85
86VideoCaptureImplManager::Device::~Device() {}
87
88} // namespace content