summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/video_capture_impl_manager.cc
blob: b2702966d5c410a45b318368aea74f79655dd16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/media/video_capture_impl_manager.h"

#include "base/bind.h"
#include "base/stl_util.h"
#include "content/renderer/media/video_capture_impl.h"
#include "content/renderer/media/video_capture_message_filter.h"

namespace content {

VideoCaptureImplManager::VideoCaptureImplManager()
    : thread_("VC manager") {
  thread_.Start();
  message_loop_proxy_ = thread_.message_loop_proxy();
  filter_ = new VideoCaptureMessageFilter();
}

media::VideoCapture* VideoCaptureImplManager::AddDevice(
    media::VideoCaptureSessionId id,
    media::VideoCapture::EventHandler* handler) {
  DCHECK(handler);

  base::AutoLock auto_lock(lock_);
  Devices::iterator it = devices_.find(id);
  if (it == devices_.end()) {
    VideoCaptureImpl* vc =
        new VideoCaptureImpl(id, message_loop_proxy_, filter_);
    devices_[id] = new Device(vc, handler);
    vc->Init();
    return vc;
  }

  devices_[id]->clients.push_front(handler);
  return it->second->vc;
}

void VideoCaptureImplManager::SuspendDevices(bool suspend) {
  base::AutoLock auto_lock(lock_);
  for (Devices::iterator it = devices_.begin(); it != devices_.end(); ++it)
    it->second->vc->SuspendCapture(suspend);
}

void VideoCaptureImplManager::RemoveDevice(
    media::VideoCaptureSessionId id,
    media::VideoCapture::EventHandler* handler) {
  DCHECK(handler);

  base::AutoLock auto_lock(lock_);
  Devices::iterator it = devices_.find(id);
  if (it == devices_.end())
    return;

  size_t size = it->second->clients.size();
  it->second->clients.remove(handler);

  if (size == it->second->clients.size() || size > 1)
    return;

  devices_[id]->vc->DeInit(base::Bind(&VideoCaptureImplManager::FreeDevice,
                                      this, devices_[id]->vc));
  delete devices_[id];
  devices_.erase(id);
}

void VideoCaptureImplManager::FreeDevice(VideoCaptureImpl* vc) {
  delete vc;
}

VideoCaptureImplManager::~VideoCaptureImplManager() {
  thread_.Stop();
  // TODO(wjia): uncomment the line below after collecting enough info for
  // crbug.com/152418.
  // STLDeleteContainerPairSecondPointers(devices_.begin(), devices_.end());
}

VideoCaptureImplManager::Device::Device(
    VideoCaptureImpl* device,
    media::VideoCapture::EventHandler* handler)
    : vc(device) {
  clients.push_front(handler);
}

VideoCaptureImplManager::Device::~Device() {}

}  // namespace content