| 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 28 matching lines...) Expand all Loading... |
| 39 media::VideoCaptureSessionId session_id; | 39 media::VideoCaptureSessionId session_id; |
| 40 | 40 |
| 41 // To be used and destroyed only on the IO thread. | 41 // To be used and destroyed only on the IO thread. |
| 42 std::unique_ptr<VideoCaptureImpl> impl; | 42 std::unique_ptr<VideoCaptureImpl> impl; |
| 43 | 43 |
| 44 // Number of clients using |impl|. | 44 // Number of clients using |impl|. |
| 45 int client_count; | 45 int client_count; |
| 46 | 46 |
| 47 // 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 |
| 48 // VideoCaptureImplManager::Suspend(). | 48 // VideoCaptureImplManager::Suspend(). |
| 49 // See also: VideoCaptureImplManager::is_suspending_all_. | |
| 50 bool is_individually_suspended; | 49 bool is_individually_suspended; |
| 51 | 50 |
| 52 DeviceEntry() | 51 DeviceEntry() |
| 53 : session_id(0), client_count(0), is_individually_suspended(false) {} | 52 : session_id(0), client_count(0), is_individually_suspended(false) {} |
| 54 DeviceEntry(DeviceEntry&& other) = default; | 53 DeviceEntry(DeviceEntry&& other) = default; |
| 55 DeviceEntry& operator=(DeviceEntry&& other) = default; | 54 DeviceEntry& operator=(DeviceEntry&& other) = default; |
| 56 ~DeviceEntry() = default; | 55 ~DeviceEntry() = default; |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 VideoCaptureImplManager::VideoCaptureImplManager() | 58 VideoCaptureImplManager::VideoCaptureImplManager() |
| 60 : next_client_id_(0), | 59 : next_client_id_(0), |
| 61 render_main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 60 render_main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 62 is_suspending_all_(false), | |
| 63 weak_factory_(this) {} | 61 weak_factory_(this) {} |
| 64 | 62 |
| 65 VideoCaptureImplManager::~VideoCaptureImplManager() { | 63 VideoCaptureImplManager::~VideoCaptureImplManager() { |
| 66 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 64 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
| 67 if (devices_.empty()) | 65 if (devices_.empty()) |
| 68 return; | 66 return; |
| 69 // Forcibly release all video capture resources. | 67 // Forcibly release all video capture resources. |
| 70 for (auto& entry : devices_) { | 68 for (auto& entry : devices_) { |
| 71 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, | 69 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, |
| 72 entry.impl.release()); | 70 entry.impl.release()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 142 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
| 145 const auto it = std::find_if( | 143 const auto it = std::find_if( |
| 146 devices_.begin(), devices_.end(), | 144 devices_.begin(), devices_.end(), |
| 147 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); | 145 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); |
| 148 DCHECK(it != devices_.end()); | 146 DCHECK(it != devices_.end()); |
| 149 if (it->is_individually_suspended) | 147 if (it->is_individually_suspended) |
| 150 return; // Device has already been individually suspended. | 148 return; // Device has already been individually suspended. |
| 151 if (it->client_count > 1) | 149 if (it->client_count > 1) |
| 152 return; // Punt when there is >1 client (see comments in UseDevice()). | 150 return; // Punt when there is >1 client (see comments in UseDevice()). |
| 153 it->is_individually_suspended = true; | 151 it->is_individually_suspended = true; |
| 154 if (is_suspending_all_) | |
| 155 return; // Device should already be suspended. | |
| 156 ChildProcess::current()->io_task_runner()->PostTask( | 152 ChildProcess::current()->io_task_runner()->PostTask( |
| 157 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, | 153 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, |
| 158 base::Unretained(it->impl.get()), true)); | 154 base::Unretained(it->impl.get()), true)); |
| 159 } | 155 } |
| 160 | 156 |
| 161 void VideoCaptureImplManager::Resume(media::VideoCaptureSessionId id) { | 157 void VideoCaptureImplManager::Resume(media::VideoCaptureSessionId id) { |
| 162 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 158 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
| 163 const auto it = std::find_if( | 159 const auto it = std::find_if( |
| 164 devices_.begin(), devices_.end(), | 160 devices_.begin(), devices_.end(), |
| 165 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); | 161 [id] (const DeviceEntry& entry) { return entry.session_id == id; }); |
| 166 DCHECK(it != devices_.end()); | 162 DCHECK(it != devices_.end()); |
| 167 if (!it->is_individually_suspended) | 163 if (!it->is_individually_suspended) |
| 168 return; // Device was not individually suspended. | 164 return; // Device was not individually suspended. |
| 169 it->is_individually_suspended = false; | 165 it->is_individually_suspended = false; |
| 170 if (is_suspending_all_) | |
| 171 return; // Device must remain suspended until all are resumed. | |
| 172 ChildProcess::current()->io_task_runner()->PostTask( | 166 ChildProcess::current()->io_task_runner()->PostTask( |
| 173 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, | 167 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, |
| 174 base::Unretained(it->impl.get()), false)); | 168 base::Unretained(it->impl.get()), false)); |
| 175 } | 169 } |
| 176 | 170 |
| 177 void VideoCaptureImplManager::GetDeviceSupportedFormats( | 171 void VideoCaptureImplManager::GetDeviceSupportedFormats( |
| 178 media::VideoCaptureSessionId id, | 172 media::VideoCaptureSessionId id, |
| 179 const VideoCaptureDeviceFormatsCB& callback) { | 173 const VideoCaptureDeviceFormatsCB& callback) { |
| 180 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | 174 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); |
| 181 const auto it = std::find_if( | 175 const auto it = std::find_if( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 DCHECK(it != devices_.end()); | 221 DCHECK(it != devices_.end()); |
| 228 DCHECK_GT(it->client_count, 0); | 222 DCHECK_GT(it->client_count, 0); |
| 229 --it->client_count; | 223 --it->client_count; |
| 230 if (it->client_count > 0) | 224 if (it->client_count > 0) |
| 231 return; | 225 return; |
| 232 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, | 226 ChildProcess::current()->io_task_runner()->DeleteSoon(FROM_HERE, |
| 233 it->impl.release()); | 227 it->impl.release()); |
| 234 devices_.erase(it); | 228 devices_.erase(it); |
| 235 } | 229 } |
| 236 | 230 |
| 237 void VideoCaptureImplManager::SuspendDevices(bool suspend) { | |
| 238 DCHECK(render_main_task_runner_->BelongsToCurrentThread()); | |
| 239 if (is_suspending_all_ == suspend) | |
| 240 return; | |
| 241 is_suspending_all_ = suspend; | |
| 242 for (auto& entry : devices_) { | |
| 243 if (entry.is_individually_suspended) | |
| 244 continue; // Either: 1) Already suspended; or 2) Should not be resumed. | |
| 245 ChildProcess::current()->io_task_runner()->PostTask( | |
| 246 FROM_HERE, base::Bind(&VideoCaptureImpl::SuspendCapture, | |
| 247 base::Unretained(entry.impl.get()), suspend)); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 } // namespace content | 231 } // namespace content |
| OLD | NEW |