summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/video_capture_impl_manager.cc
blob: 686e6ad74c1a0746d5aed659680b0a3ac1de4c46 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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.
//
// Implementation notes about interactions with VideoCaptureImpl.
//
// How is VideoCaptureImpl used:
//
// VideoCaptureImpl is an IO thread object while VideoCaptureImplManager
// lives only on the render thread. It is only possible to access an
// object of VideoCaptureImpl via a task on the IO thread.
//
// How is VideoCaptureImpl deleted:
//
// A task is posted to the IO thread to delete a VideoCaptureImpl.
// Immediately after that the pointer to it is dropped. This means no
// access to this VideoCaptureImpl object is possible on the render
// thread. Also note that VideoCaptureImpl does not post task to itself.
//
// The use of Unretained:
//
// We make sure deletion is the last task on the IO thread for a
// VideoCaptureImpl object. This allows the use of Unretained() binding.

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

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "content/child/child_process.h"
#include "content/renderer/media/video_capture_impl.h"
#include "content/renderer/media/video_capture_message_filter.h"
#include "media/base/bind_to_current_loop.h"

namespace content {

VideoCaptureImplManager::VideoCaptureImplManager()
    : next_client_id_(0),
      filter_(new VideoCaptureMessageFilter()),
      weak_factory_(this) {
}

VideoCaptureImplManager::~VideoCaptureImplManager() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (devices_.empty())
    return;
  // Forcibly release all video capture resources.
  for (VideoCaptureDeviceMap::iterator it = devices_.begin();
       it != devices_.end(); ++it) {
    VideoCaptureImpl* impl = it->second.second;
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&VideoCaptureImpl::DeInit,
                   base::Unretained(impl)));
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&base::DeletePointer<VideoCaptureImpl>,
                   base::Unretained(impl)));
  }
  devices_.clear();
}

base::Closure VideoCaptureImplManager::UseDevice(
    media::VideoCaptureSessionId id) {
  DCHECK(thread_checker_.CalledOnValidThread());

  VideoCaptureImpl* impl = NULL;
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  if (it == devices_.end()) {
    impl = CreateVideoCaptureImplForTesting(id, filter_.get());
    if (!impl)
      impl = new VideoCaptureImpl(id, filter_.get());
    devices_[id] = std::make_pair(1, impl);
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&VideoCaptureImpl::Init,
                   base::Unretained(impl)));
  } else {
    ++it->second.first;
  }
  return base::Bind(&VideoCaptureImplManager::UnrefDevice,
                    weak_factory_.GetWeakPtr(), id);
}

base::Closure VideoCaptureImplManager::StartCapture(
    media::VideoCaptureSessionId id,
    const media::VideoCaptureParams& params,
    const VideoCaptureStateUpdateCB& state_update_cb,
    const VideoCaptureDeliverFrameCB& deliver_frame_cb) {
  DCHECK(thread_checker_.CalledOnValidThread());
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  DCHECK(it != devices_.end());
  VideoCaptureImpl* impl = it->second.second;

  // This ID is used to identify a client of VideoCaptureImpl.
  const int client_id = ++next_client_id_;

  ChildProcess::current()->io_message_loop_proxy()->PostTask(
      FROM_HERE,
      base::Bind(&VideoCaptureImpl::StartCapture,
                 base::Unretained(impl),
                 client_id,
                 params,
                 state_update_cb,
                 deliver_frame_cb));
  return base::Bind(&VideoCaptureImplManager::StopCapture,
                    weak_factory_.GetWeakPtr(),
                    client_id, id);
}

void VideoCaptureImplManager::GetDeviceSupportedFormats(
    media::VideoCaptureSessionId id,
    const VideoCaptureDeviceFormatsCB& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  DCHECK(it != devices_.end());
  VideoCaptureImpl* impl = it->second.second;
  ChildProcess::current()->io_message_loop_proxy()->PostTask(
      FROM_HERE,
      base::Bind(&VideoCaptureImpl::GetDeviceSupportedFormats,
                 base::Unretained(impl), callback));
}

void VideoCaptureImplManager::GetDeviceFormatsInUse(
    media::VideoCaptureSessionId id,
    const VideoCaptureDeviceFormatsCB& callback) {
  DCHECK(thread_checker_.CalledOnValidThread());
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  DCHECK(it != devices_.end());
  VideoCaptureImpl* impl = it->second.second;
  ChildProcess::current()->io_message_loop_proxy()->PostTask(
      FROM_HERE,
      base::Bind(&VideoCaptureImpl::GetDeviceFormatsInUse,
                 base::Unretained(impl), callback));
}

VideoCaptureImpl*
VideoCaptureImplManager::CreateVideoCaptureImplForTesting(
    media::VideoCaptureSessionId id,
    VideoCaptureMessageFilter* filter) const {
  return NULL;
}

void VideoCaptureImplManager::StopCapture(
    int client_id, media::VideoCaptureSessionId id) {
  DCHECK(thread_checker_.CalledOnValidThread());
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  DCHECK(it != devices_.end());
  VideoCaptureImpl* impl = it->second.second;
  ChildProcess::current()->io_message_loop_proxy()->PostTask(
      FROM_HERE,
      base::Bind(&VideoCaptureImpl::StopCapture,
                 base::Unretained(impl), client_id));
}

void VideoCaptureImplManager::UnrefDevice(
    media::VideoCaptureSessionId id) {
  DCHECK(thread_checker_.CalledOnValidThread());
  VideoCaptureDeviceMap::iterator it = devices_.find(id);
  DCHECK(it != devices_.end());
  VideoCaptureImpl* impl = it->second.second;

  // Unref and destroy on the IO thread if there's no more client.
  DCHECK(it->second.first);
  --it->second.first;
  if (!it->second.first) {
    devices_.erase(id);
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&VideoCaptureImpl::DeInit,
                   base::Unretained(impl)));
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&base::DeletePointer<VideoCaptureImpl>,
                   base::Unretained(impl)));
  }
}

void VideoCaptureImplManager::SuspendDevices(bool suspend) {
  DCHECK(thread_checker_.CalledOnValidThread());
  for (VideoCaptureDeviceMap::iterator it = devices_.begin();
       it != devices_.end(); ++it) {
    VideoCaptureImpl* impl = it->second.second;
    ChildProcess::current()->io_message_loop_proxy()->PostTask(
        FROM_HERE,
        base::Bind(&VideoCaptureImpl::SuspendCapture,
                   base::Unretained(impl), suspend));
  }
}

}  // namespace content