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 // Implementation notes about interactions with VideoCaptureImpl. | 5 // Implementation notes about interactions with VideoCaptureImpl. |
6 // | 6 // |
7 // How is VideoCaptureImpl used: | 7 // How is VideoCaptureImpl used: |
8 // | 8 // |
9 // VideoCaptureImpl is an IO thread object while VideoCaptureImplManager | 9 // VideoCaptureImpl is an IO thread object while VideoCaptureImplManager |
10 // lives only on the render thread. It is only possible to access an | 10 // lives only on the render thread. It is only possible to access an |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "content/renderer/media/video_capture_impl_manager.h" | 25 #include "content/renderer/media/video_capture_impl_manager.h" |
26 | 26 |
27 #include <algorithm> | 27 #include <algorithm> |
28 | 28 |
29 #include "base/bind.h" | 29 #include "base/bind.h" |
30 #include "base/bind_helpers.h" | 30 #include "base/bind_helpers.h" |
31 #include "base/location.h" | 31 #include "base/location.h" |
32 #include "base/threading/thread_task_runner_handle.h" | 32 #include "base/threading/thread_task_runner_handle.h" |
33 #include "content/child/child_process.h" | 33 #include "content/child/child_process.h" |
34 #include "content/renderer/media/video_capture_impl.h" | 34 #include "content/renderer/media/video_capture_impl.h" |
35 #include "content/renderer/media/video_capture_message_filter.h" | |
36 | 35 |
37 namespace content { | 36 namespace content { |
38 | 37 |
39 struct VideoCaptureImplManager::DeviceEntry { | 38 struct VideoCaptureImplManager::DeviceEntry { |
40 media::VideoCaptureSessionId session_id; | 39 media::VideoCaptureSessionId session_id; |
41 | 40 |
42 // To be used and destroyed only on the IO thread. | 41 // To be used and destroyed only on the IO thread. |
43 std::unique_ptr<VideoCaptureImpl> impl; | 42 std::unique_ptr<VideoCaptureImpl> impl; |
44 | 43 |
45 // Number of clients using |impl|. | 44 // Number of clients using |impl|. |
46 int client_count; | 45 int client_count; |
47 | 46 |
48 // This is set to true if this device is being suspended, via | 47 // This is set to true if this device is being suspended, via |
49 // VideoCaptureImplManager::Suspend(). | 48 // VideoCaptureImplManager::Suspend(). |
50 // See also: VideoCaptureImplManager::is_suspending_all_. | 49 // See also: VideoCaptureImplManager::is_suspending_all_. |
51 bool is_individually_suspended; | 50 bool is_individually_suspended; |
52 | 51 |
53 DeviceEntry() | 52 DeviceEntry() |
54 : session_id(0), client_count(0), is_individually_suspended(false) {} | 53 : session_id(0), client_count(0), is_individually_suspended(false) {} |
55 DeviceEntry(DeviceEntry&& other) = default; | 54 DeviceEntry(DeviceEntry&& other) = default; |
56 DeviceEntry& operator=(DeviceEntry&& other) = default; | 55 DeviceEntry& operator=(DeviceEntry&& other) = default; |
57 ~DeviceEntry() = default; | 56 ~DeviceEntry() = default; |
58 }; | 57 }; |
59 | 58 |
60 VideoCaptureImplManager::VideoCaptureImplManager() | 59 VideoCaptureImplManager::VideoCaptureImplManager() |
61 : next_client_id_(0), | 60 : next_client_id_(0), |
62 filter_(new VideoCaptureMessageFilter()), | |
63 render_main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 61 render_main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
64 is_suspending_all_(false), | 62 is_suspending_all_(false), |
65 weak_factory_(this) {} | 63 weak_factory_(this) {} |
66 | 64 |
67 VideoCaptureImplManager::~VideoCaptureImplManager() { | 65 VideoCaptureImplManager::~VideoCaptureImplManager() { |
68 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 66 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
69 if (devices_.empty()) | 67 if (devices_.empty()) |
70 return; | 68 return; |
71 // Forcibly release all video capture resources. | 69 // Forcibly release all video capture resources. |
72 for (auto& entry : devices_) { | 70 for (auto& entry : devices_) { |
73 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, | 71 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, |
74 entry.impl.release()); | 72 entry.impl.release()); |
75 } | 73 } |
76 devices_.clear(); | 74 devices_.clear(); |
77 } | 75 } |
78 | 76 |
79 base::Closure VideoCaptureImplManager::UseDevice( | 77 base::Closure VideoCaptureImplManager::UseDevice( |
80 media::VideoCaptureSessionId id) { | 78 media::VideoCaptureSessionId id) { |
| 79 DVLOG(1) << __func__ << " session id: " << id; |
81 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 80 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
82 auto it = std::find_if( | 81 auto it = std::find_if( |
83 devices_.begin(), devices_.end(), | 82 devices_.begin(), devices_.end(), |
84 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); | 83 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); |
85 if (it == devices_.end()) { | 84 if (it == devices_.end()) { |
86 devices_.push_back(DeviceEntry()); | 85 devices_.push_back(DeviceEntry()); |
87 it = devices_.end() - 1; | 86 it = devices_.end() - 1; |
88 it->session_id = id; | 87 it->session_id = id; |
89 it->impl = CreateVideoCaptureImplForTesting(id, filter_.get()); | 88 it->impl = CreateVideoCaptureImplForTesting(id); |
90 if (!it->impl) { | 89 if (!it->impl) |
91 it->impl.reset(new VideoCaptureImpl( | 90 it->impl.reset(new VideoCaptureImpl(id)); |
92 id, filter_.get(), ChildProcess::current()->io_task_runner())); | |
93 } | |
94 } | 91 } |
95 ++it->client_count; | 92 ++it->client_count; |
96 | 93 |
97 // Design limit: When there are multiple clients, VideoCaptureImplManager | 94 // Design limit: When there are multiple clients, VideoCaptureImplManager |
98 // would have to individually track which ones requested suspending/resuming, | 95 // would have to individually track which ones requested suspending/resuming, |
99 // in order to determine whether the whole device should be suspended. | 96 // in order to determine whether the whole device should be suspended. |
100 // Instead, handle the non-common use case of multiple clients by just | 97 // Instead, handle the non-common use case of multiple clients by just |
101 // resuming the suspended device, and disable suspend functionality while | 98 // resuming the suspended device, and disable suspend functionality while |
102 // there are multiple clients. | 99 // there are multiple clients. |
103 if (it->is_individually_suspended) | 100 if (it->is_individually_suspended) |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 devices_.begin(), devices_.end(), | 195 devices_.begin(), devices_.end(), |
199 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); | 196 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); |
200 DCHECK(it != devices_.end()); | 197 DCHECK(it != devices_.end()); |
201 ChildProcess::current()->io_task_runner()->PostTask( | 198 ChildProcess::current()->io_task_runner()->PostTask( |
202 FROM_HERE, base::Bind(&VideoCaptureImpl::GetDeviceFormatsInUse, | 199 FROM_HERE, base::Bind(&VideoCaptureImpl::GetDeviceFormatsInUse, |
203 base::Unretained(it->impl.get()), callback)); | 200 base::Unretained(it->impl.get()), callback)); |
204 } | 201 } |
205 | 202 |
206 std::unique_ptr<VideoCaptureImpl> | 203 std::unique_ptr<VideoCaptureImpl> |
207 VideoCaptureImplManager::CreateVideoCaptureImplForTesting( | 204 VideoCaptureImplManager::CreateVideoCaptureImplForTesting( |
208 media::VideoCaptureSessionId id, | 205 media::VideoCaptureSessionId session_id) const { |
209 VideoCaptureMessageFilter* filter) const { | |
210 return std::unique_ptr<VideoCaptureImpl>(); | 206 return std::unique_ptr<VideoCaptureImpl>(); |
211 } | 207 } |
212 | 208 |
213 void VideoCaptureImplManager::StopCapture(int client_id, | 209 void VideoCaptureImplManager::StopCapture(int client_id, |
214 media::VideoCaptureSessionId id) { | 210 media::VideoCaptureSessionId id) { |
215 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 211 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
216 const auto it = std::find_if( | 212 const auto it = std::find_if( |
217 devices_.begin(), devices_.end(), | 213 devices_.begin(), devices_.end(), |
218 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); | 214 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); |
219 DCHECK(it != devices_.end()); | 215 DCHECK(it != devices_.end()); |
(...skipping 26 matching lines...) Expand all Loading... |
246 for (auto& entry : devices_) { | 242 for (auto& entry : devices_) { |
247 if (entry.is_individually_suspended) | 243 if (entry.is_individually_suspended) |
248 continue; // Either: 1) Already suspended; or 2) Should not be resumed. | 244 continue; // Either: 1) Already suspended; or 2) Should not be resumed. |
249 ChildProcess::current()->io_task_runner()->PostTask( | 245 ChildProcess::current()->io_task_runner()->PostTask( |
250 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, | 246 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, |
251 base::Unretained(entry.impl.get()), suspend)); | 247 base::Unretained(entry.impl.get()), suspend)); |
252 } | 248 } |
253 } | 249 } |
254 | 250 |
255 } // namespace content | 251 } // namespace content |
OLD | NEW |