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 #include "content/browser/renderer_host/media/video_capture_manager.h" | 5 #include "content/browser/renderer_host/media/video_capture_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
24 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" | 24 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" |
25 #include "content/browser/media/capture/web_contents_video_capture_device.h" | 25 #include "content/browser/media/capture/web_contents_video_capture_device.h" |
26 #include "content/browser/media/media_internals.h" | 26 #include "content/browser/media/media_internals.h" |
27 #include "content/browser/renderer_host/media/video_capture_controller.h" | 27 #include "content/browser/renderer_host/media/video_capture_controller.h" |
28 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" | 28 #include "content/browser/renderer_host/media/video_capture_controller_event_han dler.h" |
29 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" | |
30 #include "content/browser/renderer_host/media/video_frame_receiver_on_io_thread. h" | |
29 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/desktop_media_id.h" | 32 #include "content/public/browser/desktop_media_id.h" |
31 #include "content/public/common/media_stream_request.h" | 33 #include "content/public/common/media_stream_request.h" |
32 #include "media/base/bind_to_current_loop.h" | 34 #include "media/base/bind_to_current_loop.h" |
33 #include "media/base/media_switches.h" | 35 #include "media/base/media_switches.h" |
36 #include "media/capture/video/video_capture_buffer_pool_impl.h" | |
37 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | |
34 #include "media/capture/video/video_capture_device.h" | 38 #include "media/capture/video/video_capture_device.h" |
39 #include "media/capture/video/video_capture_device_client.h" | |
35 #include "media/capture/video/video_capture_device_factory.h" | 40 #include "media/capture/video/video_capture_device_factory.h" |
36 | 41 |
37 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) | 42 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) |
38 #include "content/browser/media/capture/desktop_capture_device.h" | 43 #include "content/browser/media/capture/desktop_capture_device.h" |
39 #if defined(USE_AURA) | 44 #if defined(USE_AURA) |
40 #include "content/browser/media/capture/desktop_capture_device_aura.h" | 45 #include "content/browser/media/capture/desktop_capture_device_aura.h" |
41 #endif | 46 #endif |
42 #endif | 47 #endif |
43 | 48 |
44 #if defined(ENABLE_SCREEN_CAPTURE) && defined(OS_ANDROID) | 49 #if defined(ENABLE_SCREEN_CAPTURE) && defined(OS_ANDROID) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 formats->erase(last, formats->end()); | 111 formats->erase(last, formats->end()); |
107 // Mark all formats as I420, since this is what the renderer side will get | 112 // Mark all formats as I420, since this is what the renderer side will get |
108 // anyhow: the actual pixel format is decided at the device level. | 113 // anyhow: the actual pixel format is decided at the device level. |
109 // Don't do this for Y16 format as it is handled separatelly. | 114 // Don't do this for Y16 format as it is handled separatelly. |
110 for (auto& format : *formats) { | 115 for (auto& format : *formats) { |
111 if (format.pixel_format != media::PIXEL_FORMAT_Y16) | 116 if (format.pixel_format != media::PIXEL_FORMAT_Y16) |
112 format.pixel_format = media::PIXEL_FORMAT_I420; | 117 format.pixel_format = media::PIXEL_FORMAT_I420; |
113 } | 118 } |
114 } | 119 } |
115 | 120 |
116 // The maximum number of buffers in the capture pipeline. See | 121 // The maximum number of video frame buffers in-flight at any one time. This |
117 // VideoCaptureController ctor comments for more details. | 122 // value should be based on the logical capacity of the capture pipeline, and |
123 // not on hardware performance. For example, tab capture requires more buffers | |
124 // than webcam capture because the pipeline is longer (it includes read-backs | |
125 // pending in the GPU pipeline). | |
118 const int kMaxNumberOfBuffers = 3; | 126 const int kMaxNumberOfBuffers = 3; |
119 // TODO(miu): The value for tab capture should be determined programmatically. | 127 // TODO(miu): The value for tab capture should be determined programmatically. |
120 // http://crbug.com/460318 | 128 // http://crbug.com/460318 |
121 const int kMaxNumberOfBuffersForTabCapture = 10; | 129 const int kMaxNumberOfBuffersForTabCapture = 10; |
122 | 130 |
123 // Used for logging capture events. | 131 // Used for logging capture events. |
124 // Elements in this enum should not be deleted or rearranged; the only | 132 // Elements in this enum should not be deleted or rearranged; the only |
125 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT. | 133 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT. |
126 enum VideoCaptureEvent { | 134 enum VideoCaptureEvent { |
127 VIDEO_CAPTURE_START_CAPTURE = 0, | 135 VIDEO_CAPTURE_START_CAPTURE = 0, |
128 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1, | 136 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1, |
129 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2, | 137 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2, |
130 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3, | 138 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3, |
131 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4, | 139 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4, |
132 NUM_VIDEO_CAPTURE_EVENT | 140 NUM_VIDEO_CAPTURE_EVENT |
133 }; | 141 }; |
134 | 142 |
135 void LogVideoCaptureEvent(VideoCaptureEvent event) { | 143 void LogVideoCaptureEvent(VideoCaptureEvent event) { |
136 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", | 144 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", |
137 event, | 145 event, |
138 NUM_VIDEO_CAPTURE_EVENT); | 146 NUM_VIDEO_CAPTURE_EVENT); |
139 } | 147 } |
140 | 148 |
141 // Counter used for identifying a DeviceRequest to start a capture device. | 149 // Counter used for identifying a DeviceRequest to start a capture device. |
142 static int g_device_start_id = 0; | 150 static int g_device_start_id = 0; |
143 | 151 |
144 const media::VideoCaptureSessionId kFakeSessionId = -1; | 152 const media::VideoCaptureSessionId kFakeSessionId = -1; |
145 | 153 |
154 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( | |
155 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { | |
156 return base::MakeUnique<content::VideoCaptureGpuJpegDecoder>(decode_done_cb); | |
157 } | |
158 | |
146 } // namespace | 159 } // namespace |
147 | 160 |
148 namespace content { | 161 namespace content { |
149 | 162 |
150 // Instances of this struct go through 3 different phases during their lifetime. | 163 // Instances of this struct go through 3 different phases during their lifetime. |
151 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of | 164 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of |
152 // only a controller. Clients can already connect to the controller, but there | 165 // only a controller. Clients can already connect to the controller, but there |
153 // is no device present. | 166 // is no device present. |
154 // Phase 2: When a request to "start" the entry comes in (via | 167 // Phase 2: When a request to "start" the entry comes in (via |
155 // HandleQueuedStartRequest()), a VideoCaptureDevice::Client is created | 168 // HandleQueuedStartRequest()), a VideoCaptureDevice::Client is created |
156 // via video_capture_controller()->NewDeviceClient() and is used to schedule the | 169 // via CreateDeviceClient() and is used to schedule the |
157 // creation and start of a VideoCaptureDevice on the Device Thread. | 170 // creation and start of a VideoCaptureDevice on the Device Thread. |
158 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this | 171 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this |
159 // newly created VideoCaptureDevice instance is connected to the | 172 // newly created VideoCaptureDevice instance is connected to the |
160 // VideoCaptureController via SetConsumerFeedbackObserver(). | 173 // VideoCaptureController via SetConsumerFeedbackObserver(). |
161 class VideoCaptureManager::DeviceEntry { | 174 struct VideoCaptureManager::DeviceEntry { |
162 public: | 175 public: |
163 DeviceEntry(MediaStreamType stream_type, | 176 DeviceEntry(MediaStreamType stream_type, |
164 const std::string& id, | 177 const std::string& id, |
165 std::unique_ptr<VideoCaptureController> controller, | 178 std::unique_ptr<VideoCaptureController> controller, |
166 const media::VideoCaptureParams& params); | 179 const media::VideoCaptureParams& params); |
167 ~DeviceEntry(); | 180 ~DeviceEntry(); |
181 std::unique_ptr<media::VideoCaptureDevice::Client> CreateDeviceClient(); | |
182 std::unique_ptr<media::FrameBufferHost> CreateFrameBufferHost(); | |
168 | 183 |
169 const int serial_id; | 184 const int serial_id; |
170 const MediaStreamType stream_type; | 185 const MediaStreamType stream_type; |
171 const std::string id; | 186 const std::string id; |
172 const media::VideoCaptureParams parameters; | 187 const media::VideoCaptureParams parameters; |
173 | 188 const std::unique_ptr<VideoCaptureController> video_capture_controller; |
174 VideoCaptureController* video_capture_controller() const; | 189 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool; |
175 media::VideoCaptureDevice* video_capture_device() const; | 190 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; |
176 | |
177 void SetVideoCaptureDevice(std::unique_ptr<VideoCaptureDevice> device); | |
178 std::unique_ptr<VideoCaptureDevice> ReleaseVideoCaptureDevice(); | |
179 | |
180 private: | |
181 const std::unique_ptr<VideoCaptureController> video_capture_controller_; | |
182 | |
183 std::unique_ptr<VideoCaptureDevice> video_capture_device_; | |
184 | |
185 base::ThreadChecker thread_checker_; | |
186 }; | 191 }; |
187 | 192 |
188 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported | 193 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported |
189 // video formats. | 194 // video formats. |
190 struct VideoCaptureManager::DeviceInfo { | 195 struct VideoCaptureManager::DeviceInfo { |
191 DeviceInfo(); | 196 DeviceInfo(); |
192 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); | 197 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); |
193 DeviceInfo(const DeviceInfo& other); | 198 DeviceInfo(const DeviceInfo& other); |
194 ~DeviceInfo(); | 199 ~DeviceInfo(); |
195 DeviceInfo& operator=(const DeviceInfo& other); | 200 DeviceInfo& operator=(const DeviceInfo& other); |
196 | 201 |
197 media::VideoCaptureDeviceDescriptor descriptor; | 202 media::VideoCaptureDeviceDescriptor descriptor; |
198 media::VideoCaptureFormats supported_formats; | 203 media::VideoCaptureFormats supported_formats; |
199 }; | 204 }; |
200 | 205 |
206 class BufferPoolFrameBufferHost : public media::FrameBufferHost { | |
207 public: | |
208 BufferPoolFrameBufferHost( | |
209 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool) | |
210 : buffer_pool_(std::move(buffer_pool)) {} | |
211 | |
212 void SetBufferHold(int buffer_id) override { | |
213 buffer_pool_->HoldForConsumers(buffer_id, 1); | |
214 } | |
215 | |
216 void ReleaseBufferHold(int buffer_id) override { | |
217 buffer_pool_->RelinquishConsumerHold(buffer_id, 1); | |
218 } | |
219 | |
220 mojo::ScopedSharedBufferHandle GetHandleForTransit(int buffer_id) override { | |
221 return buffer_pool_->GetHandleForTransit(buffer_id); | |
222 } | |
223 | |
224 private: | |
225 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool_; | |
mcasas
2016/12/06 20:02:24
media::FrameBufferHost (and BufferPoolFrameBufferH
chfremer
2016/12/06 21:07:41
The reason is that I am trying to avoid shared own
| |
226 }; | |
227 | |
201 // Class used for queuing request for starting a device. | 228 // Class used for queuing request for starting a device. |
202 class VideoCaptureManager::CaptureDeviceStartRequest { | 229 class VideoCaptureManager::CaptureDeviceStartRequest { |
203 public: | 230 public: |
204 CaptureDeviceStartRequest(int serial_id, | 231 CaptureDeviceStartRequest(int serial_id, |
205 media::VideoCaptureSessionId session_id, | 232 media::VideoCaptureSessionId session_id, |
206 const media::VideoCaptureParams& params); | 233 const media::VideoCaptureParams& params); |
207 int serial_id() const { return serial_id_; } | 234 int serial_id() const { return serial_id_; } |
208 media::VideoCaptureSessionId session_id() const { return session_id_; } | 235 media::VideoCaptureSessionId session_id() const { return session_id_; } |
209 media::VideoCaptureParams params() const { return params_; } | 236 media::VideoCaptureParams params() const { return params_; } |
210 | 237 |
(...skipping 13 matching lines...) Expand all Loading... | |
224 | 251 |
225 VideoCaptureManager::DeviceEntry::DeviceEntry( | 252 VideoCaptureManager::DeviceEntry::DeviceEntry( |
226 MediaStreamType stream_type, | 253 MediaStreamType stream_type, |
227 const std::string& id, | 254 const std::string& id, |
228 std::unique_ptr<VideoCaptureController> controller, | 255 std::unique_ptr<VideoCaptureController> controller, |
229 const media::VideoCaptureParams& params) | 256 const media::VideoCaptureParams& params) |
230 : serial_id(g_device_start_id++), | 257 : serial_id(g_device_start_id++), |
231 stream_type(stream_type), | 258 stream_type(stream_type), |
232 id(id), | 259 id(id), |
233 parameters(params), | 260 parameters(params), |
234 video_capture_controller_(std::move(controller)) {} | 261 video_capture_controller(std::move(controller)) {} |
235 | 262 |
236 VideoCaptureManager::DeviceEntry::~DeviceEntry() { | 263 VideoCaptureManager::DeviceEntry::~DeviceEntry() { |
237 DCHECK(thread_checker_.CalledOnValidThread()); | |
238 // DCHECK that this DeviceEntry does not still own a | 264 // DCHECK that this DeviceEntry does not still own a |
239 // media::VideoCaptureDevice. media::VideoCaptureDevice must be deleted on | 265 // media::VideoCaptureDevice. media::VideoCaptureDevice must be deleted on |
240 // the device thread. | 266 // the device thread. |
241 DCHECK(video_capture_device_ == nullptr); | 267 DCHECK(video_capture_device == nullptr); |
242 } | 268 } |
243 | 269 |
244 void VideoCaptureManager::DeviceEntry::SetVideoCaptureDevice( | 270 std::unique_ptr<media::VideoCaptureDevice::Client> |
245 std::unique_ptr<VideoCaptureDevice> device) { | 271 VideoCaptureManager::DeviceEntry::CreateDeviceClient() { |
246 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
247 video_capture_device_.swap(device); | 273 |
274 const int max_buffers = stream_type == MEDIA_TAB_VIDEO_CAPTURE | |
275 ? kMaxNumberOfBuffersForTabCapture | |
276 : kMaxNumberOfBuffers; | |
277 buffer_pool = new media::VideoCaptureBufferPoolImpl( | |
278 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | |
279 max_buffers); | |
280 | |
281 return base::MakeUnique<media::VideoCaptureDeviceClient>( | |
282 base::MakeUnique<VideoFrameReceiverOnIOThread>( | |
283 video_capture_controller->GetWeakPtrForIOThread()), | |
284 buffer_pool, | |
285 base::Bind( | |
286 &CreateGpuJpegDecoder, | |
287 base::Bind(&media::VideoFrameReceiver::OnIncomingCapturedVideoFrame, | |
288 video_capture_controller->GetWeakPtrForIOThread()))); | |
248 } | 289 } |
249 | 290 |
250 std::unique_ptr<media::VideoCaptureDevice> | 291 std::unique_ptr<media::FrameBufferHost> |
251 VideoCaptureManager::DeviceEntry::ReleaseVideoCaptureDevice() { | 292 VideoCaptureManager::DeviceEntry::CreateFrameBufferHost() { |
252 DCHECK(thread_checker_.CalledOnValidThread()); | 293 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
253 return std::move(video_capture_device_); | 294 DCHECK(buffer_pool); |
254 } | 295 return base::MakeUnique<BufferPoolFrameBufferHost>(buffer_pool); |
255 | |
256 VideoCaptureController* | |
257 VideoCaptureManager::DeviceEntry::video_capture_controller() const { | |
258 DCHECK(thread_checker_.CalledOnValidThread()); | |
259 return video_capture_controller_.get(); | |
260 } | |
261 | |
262 media::VideoCaptureDevice* | |
263 VideoCaptureManager::DeviceEntry::video_capture_device() const { | |
264 DCHECK(thread_checker_.CalledOnValidThread()); | |
265 return video_capture_device_.get(); | |
266 } | 296 } |
267 | 297 |
268 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; | 298 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; |
269 | 299 |
270 VideoCaptureManager::DeviceInfo::DeviceInfo( | 300 VideoCaptureManager::DeviceInfo::DeviceInfo( |
271 media::VideoCaptureDeviceDescriptor descriptor) | 301 media::VideoCaptureDeviceDescriptor descriptor) |
272 : descriptor(descriptor) {} | 302 : descriptor(descriptor) {} |
273 | 303 |
274 VideoCaptureManager::DeviceInfo::DeviceInfo( | 304 VideoCaptureManager::DeviceInfo::DeviceInfo( |
275 const VideoCaptureManager::DeviceInfo& other) = default; | 305 const VideoCaptureManager::DeviceInfo& other) = default; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 if (session_it == sessions_.end()) { | 407 if (session_it == sessions_.end()) { |
378 NOTREACHED(); | 408 NOTREACHED(); |
379 return; | 409 return; |
380 } | 410 } |
381 | 411 |
382 DeviceEntry* const existing_device = | 412 DeviceEntry* const existing_device = |
383 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); | 413 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
384 if (existing_device) { | 414 if (existing_device) { |
385 // Remove any client that is still using the session. This is safe to call | 415 // Remove any client that is still using the session. This is safe to call |
386 // even if there are no clients using the session. | 416 // even if there are no clients using the session. |
387 existing_device->video_capture_controller() | 417 existing_device->video_capture_controller->StopSession(capture_session_id); |
388 ->StopSession(capture_session_id); | |
389 | 418 |
390 // StopSession() may have removed the last client, so we might need to | 419 // StopSession() may have removed the last client, so we might need to |
391 // close the device. | 420 // close the device. |
392 DestroyDeviceEntryIfNoClients(existing_device); | 421 DestroyDeviceEntryIfNoClients(existing_device); |
393 } | 422 } |
394 | 423 |
395 // Notify listeners asynchronously, and forget the session. | 424 // Notify listeners asynchronously, and forget the session. |
396 base::ThreadTaskRunnerHandle::Get()->PostTask( | 425 base::ThreadTaskRunnerHandle::Get()->PostTask( |
397 FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, | 426 FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, |
398 session_it->second.type, capture_session_id)); | 427 session_it->second.type, capture_session_id)); |
(...skipping 27 matching lines...) Expand all Loading... | |
426 if (request->serial_id() == entry->serial_id) { | 455 if (request->serial_id() == entry->serial_id) { |
427 request->set_abort_start(); | 456 request->set_abort_start(); |
428 DVLOG(3) << "DoStopDevice, aborting start request for device " | 457 DVLOG(3) << "DoStopDevice, aborting start request for device " |
429 << entry->id << " serial_id = " << entry->serial_id; | 458 << entry->id << " serial_id = " << entry->serial_id; |
430 return; | 459 return; |
431 } | 460 } |
432 } | 461 } |
433 | 462 |
434 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id | 463 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id |
435 << " serial_id = " << entry->serial_id << "."; | 464 << " serial_id = " << entry->serial_id << "."; |
436 entry->video_capture_controller()->OnLog( | 465 entry->video_capture_controller->OnLog( |
437 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); | 466 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); |
438 entry->video_capture_controller()->SetConsumerFeedbackObserver(nullptr); | 467 entry->video_capture_controller->SetConsumerFeedbackObserver(nullptr); |
468 entry->video_capture_controller->SetFrameBufferHost(nullptr); | |
439 | 469 |
440 // |entry->video_capture_device| can be null if creating the device has | 470 // |entry->video_capture_device| can be null if creating the device has |
441 // failed. | 471 // failed. |
442 if (entry->video_capture_device()) { | 472 if (entry->video_capture_device) { |
443 device_task_runner_->PostTask( | 473 device_task_runner_->PostTask( |
444 FROM_HERE, | 474 FROM_HERE, |
445 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 475 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
446 base::Passed(entry->ReleaseVideoCaptureDevice()))); | 476 base::Passed(&entry->video_capture_device))); |
mcasas
2016/12/06 20:02:24
std::move(entry->video_capture_device())
chfremer
2016/12/06 21:07:41
Hmm, I don't fully understand the documentation of
| |
447 } | 477 } |
448 } | 478 } |
449 | 479 |
450 void VideoCaptureManager::HandleQueuedStartRequest() { | 480 void VideoCaptureManager::HandleQueuedStartRequest() { |
451 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 481 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
452 // Remove all start requests that have been aborted. | 482 // Remove all start requests that have been aborted. |
453 while (device_start_queue_.begin() != device_start_queue_.end() && | 483 while (device_start_queue_.begin() != device_start_queue_.end() && |
454 device_start_queue_.begin()->abort_start()) { | 484 device_start_queue_.begin()->abort_start()) { |
455 device_start_queue_.pop_front(); | 485 device_start_queue_.pop_front(); |
456 } | 486 } |
457 DeviceStartQueue::iterator request = device_start_queue_.begin(); | 487 DeviceStartQueue::iterator request = device_start_queue_.begin(); |
458 if (request == device_start_queue_.end()) | 488 if (request == device_start_queue_.end()) |
459 return; | 489 return; |
460 | 490 |
461 const int serial_id = request->serial_id(); | 491 const int serial_id = request->serial_id(); |
462 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 492 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
463 DCHECK(entry); | 493 DCHECK(entry); |
464 | 494 |
465 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 495 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " |
466 << entry->id << " start id = " << entry->serial_id; | 496 << entry->id << " start id = " << entry->serial_id; |
467 | 497 |
498 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = | |
499 entry->CreateDeviceClient(); | |
500 std::unique_ptr<media::FrameBufferHost> frame_buffer_host = | |
501 entry->CreateFrameBufferHost(); | |
502 | |
468 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> | 503 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> |
469 start_capture_function; | 504 start_capture_function; |
470 | 505 |
471 switch (entry->stream_type) { | 506 switch (entry->stream_type) { |
472 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 507 case MEDIA_DEVICE_VIDEO_CAPTURE: { |
473 // We look up the device id from the renderer in our local enumeration | 508 // We look up the device id from the renderer in our local enumeration |
474 // since the renderer does not have all the information that might be | 509 // since the renderer does not have all the information that might be |
475 // held in the browser-side VideoCaptureDevice::Name structure. | 510 // held in the browser-side VideoCaptureDevice::Name structure. |
476 const DeviceInfo* found = GetDeviceInfoById(entry->id); | 511 const DeviceInfo* found = GetDeviceInfoById(entry->id); |
477 if (found) { | 512 if (found) { |
478 entry->video_capture_controller()->OnLog( | 513 entry->video_capture_controller->OnLog( |
479 base::StringPrintf("Starting device: id: %s, name: %s, api: %s", | 514 base::StringPrintf("Starting device: id: %s, name: %s, api: %s", |
480 found->descriptor.device_id.c_str(), | 515 found->descriptor.device_id.c_str(), |
481 found->descriptor.GetNameAndModel().c_str(), | 516 found->descriptor.GetNameAndModel().c_str(), |
482 found->descriptor.GetCaptureApiTypeString())); | 517 found->descriptor.GetCaptureApiTypeString())); |
483 | 518 |
484 start_capture_function = base::Bind( | 519 start_capture_function = |
485 &VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, this, | 520 base::Bind(&VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, |
486 found->descriptor, request->params(), | 521 this, found->descriptor, request->params(), |
487 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | 522 base::Passed(std::move(device_client))); |
488 } else { | 523 } else { |
489 // Errors from DoStartDeviceCaptureOnDeviceThread go via | 524 // Errors from DoStartDeviceCaptureOnDeviceThread go via |
490 // VideoCaptureDeviceClient::OnError, which needs some thread | 525 // VideoCaptureDeviceClient::OnError, which needs some thread |
491 // dancing to get errors processed on the IO thread. But since | 526 // dancing to get errors processed on the IO thread. But since |
492 // we're on that thread, we call VideoCaptureController | 527 // we're on that thread, we call VideoCaptureController |
493 // methods directly. | 528 // methods directly. |
494 const std::string log_message = base::StringPrintf( | 529 const std::string log_message = base::StringPrintf( |
495 "Error on %s:%d: device %s unknown. Maybe recently disconnected?", | 530 "Error on %s:%d: device %s unknown. Maybe recently disconnected?", |
496 __FILE__, __LINE__, entry->id.c_str()); | 531 __FILE__, __LINE__, entry->id.c_str()); |
497 DLOG(ERROR) << log_message; | 532 DLOG(ERROR) << log_message; |
498 entry->video_capture_controller()->OnLog(log_message); | 533 entry->video_capture_controller->OnLog(log_message); |
499 entry->video_capture_controller()->OnError(); | 534 entry->video_capture_controller->OnError(); |
500 // Drop the failed start request. | 535 // Drop the failed start request. |
501 device_start_queue_.pop_front(); | 536 device_start_queue_.pop_front(); |
502 | 537 |
503 return; | 538 return; |
504 } | 539 } |
505 break; | 540 break; |
506 } | 541 } |
507 case MEDIA_TAB_VIDEO_CAPTURE: | 542 case MEDIA_TAB_VIDEO_CAPTURE: |
508 start_capture_function = base::Bind( | 543 start_capture_function = base::Bind( |
509 &VideoCaptureManager::DoStartTabCaptureOnDeviceThread, this, | 544 &VideoCaptureManager::DoStartTabCaptureOnDeviceThread, this, |
510 entry->id, request->params(), | 545 entry->id, request->params(), base::Passed(std::move(device_client))); |
511 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | |
512 break; | 546 break; |
513 | 547 |
514 case MEDIA_DESKTOP_VIDEO_CAPTURE: | 548 case MEDIA_DESKTOP_VIDEO_CAPTURE: |
515 start_capture_function = base::Bind( | 549 start_capture_function = base::Bind( |
516 &VideoCaptureManager::DoStartDesktopCaptureOnDeviceThread, this, | 550 &VideoCaptureManager::DoStartDesktopCaptureOnDeviceThread, this, |
517 entry->id, request->params(), | 551 entry->id, request->params(), base::Passed(std::move(device_client))); |
518 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | |
519 break; | 552 break; |
520 | 553 |
521 default: { | 554 default: { |
522 NOTIMPLEMENTED(); | 555 NOTIMPLEMENTED(); |
523 return; | 556 return; |
524 } | 557 } |
525 } | 558 } |
559 // Passing raw pointer |device_client_ptr| is safe, because it will | |
560 // be owned by the device returned by |start_capture_function|. | |
526 base::PostTaskAndReplyWithResult( | 561 base::PostTaskAndReplyWithResult( |
527 device_task_runner_.get(), FROM_HERE, start_capture_function, | 562 device_task_runner_.get(), FROM_HERE, start_capture_function, |
528 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, | 563 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, |
529 request->serial_id())); | 564 request->serial_id(), base::Passed(&frame_buffer_host))); |
530 } | 565 } |
531 | 566 |
532 void VideoCaptureManager::OnDeviceStarted( | 567 void VideoCaptureManager::OnDeviceStarted( |
533 int serial_id, | 568 int serial_id, |
569 std::unique_ptr<media::FrameBufferHost> frame_buffer_host, | |
534 std::unique_ptr<VideoCaptureDevice> device) { | 570 std::unique_ptr<VideoCaptureDevice> device) { |
535 DVLOG(3) << __func__; | 571 DVLOG(3) << __func__; |
536 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
537 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); | 573 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); |
538 // |device| can be null if creation failed in | 574 // |device| can be null if creation failed in |
539 // DoStartDeviceCaptureOnDeviceThread. | 575 // DoStartDeviceCaptureOnDeviceThread. |
540 if (device_start_queue_.front().abort_start()) { | 576 if (device_start_queue_.front().abort_start()) { |
541 // The device is no longer wanted. Stop the device again. | 577 // The device is no longer wanted. Stop the device again. |
542 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; | 578 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; |
543 media::VideoCaptureDevice* device_ptr = device.get(); | 579 media::VideoCaptureDevice* device_ptr = device.get(); |
544 base::Closure closure = | 580 base::Closure closure = |
545 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 581 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
546 base::Passed(&device)); | 582 base::Passed(&device)); |
547 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { | 583 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { |
548 // PostTask failed. The device must be stopped anyway. | 584 // PostTask failed. The device must be stopped anyway. |
549 device_ptr->StopAndDeAllocate(); | 585 device_ptr->StopAndDeAllocate(); |
550 } | 586 } |
551 } else { | 587 } else { |
552 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 588 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
553 DCHECK(entry); | 589 DCHECK(entry); |
554 DCHECK(!entry->video_capture_device()); | 590 DCHECK(!entry->video_capture_device); |
555 // Passing raw pointer |device.get()| to the controller is safe, | 591 if (device) { |
556 // because we transfer ownership of it to |entry|. We are calling | 592 entry->video_capture_controller->SetFrameBufferHost( |
557 // SetConsumerFeedbackObserver(nullptr) before releasing | 593 std::move(frame_buffer_host)); |
558 // |entry->video_capture_device_| on the |device_task_runner_|. | 594 // Passing raw pointer |device.get()| to the controller is safe, |
559 entry->video_capture_controller()->SetConsumerFeedbackObserver( | 595 // because we transfer ownership of it to |entry|. We are calling |
560 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | 596 // SetConsumerFeedbackObserver(nullptr) before releasing |
561 device.get(), device_task_runner_)); | 597 // |entry->video_capture_device_| on the |device_task_runner_|. |
562 entry->SetVideoCaptureDevice(std::move(device)); | 598 entry->video_capture_controller->SetConsumerFeedbackObserver( |
599 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | |
600 device.get(), device_task_runner_)); | |
601 } | |
602 entry->video_capture_device = std::move(device); | |
563 | 603 |
564 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { | 604 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { |
565 const media::VideoCaptureSessionId session_id = | 605 const media::VideoCaptureSessionId session_id = |
566 device_start_queue_.front().session_id(); | 606 device_start_queue_.front().session_id(); |
567 DCHECK(session_id != kFakeSessionId); | 607 DCHECK(session_id != kFakeSessionId); |
568 MaybePostDesktopCaptureWindowId(session_id); | 608 MaybePostDesktopCaptureWindowId(session_id); |
569 } | 609 } |
570 | 610 |
571 auto it = photo_request_queue_.begin(); | 611 auto it = photo_request_queue_.begin(); |
572 while (it != photo_request_queue_.end()) { | 612 while (it != photo_request_queue_.end()) { |
573 auto request = it++; | 613 auto request = it++; |
574 DeviceEntry* maybe_entry = GetDeviceEntryBySessionId(request->first); | 614 DeviceEntry* maybe_entry = GetDeviceEntryBySessionId(request->first); |
575 if (maybe_entry && maybe_entry->video_capture_device()) { | 615 if (maybe_entry && maybe_entry->video_capture_device) { |
576 request->second.Run(maybe_entry->video_capture_device()); | 616 request->second.Run(maybe_entry->video_capture_device.get()); |
577 photo_request_queue_.erase(request); | 617 photo_request_queue_.erase(request); |
578 } | 618 } |
579 } | 619 } |
580 } | 620 } |
581 | 621 |
582 device_start_queue_.pop_front(); | 622 device_start_queue_.pop_front(); |
583 HandleQueuedStartRequest(); | 623 HandleQueuedStartRequest(); |
584 } | 624 } |
585 | 625 |
586 std::unique_ptr<media::VideoCaptureDevice> | 626 std::unique_ptr<media::VideoCaptureDevice> |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
679 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 719 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
680 DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " | 720 DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " |
681 << media::VideoCaptureFormat::ToString(params.requested_format); | 721 << media::VideoCaptureFormat::ToString(params.requested_format); |
682 | 722 |
683 DeviceEntry* entry = GetOrCreateDeviceEntry(session_id, params); | 723 DeviceEntry* entry = GetOrCreateDeviceEntry(session_id, params); |
684 if (!entry) { | 724 if (!entry) { |
685 done_cb.Run(base::WeakPtr<VideoCaptureController>()); | 725 done_cb.Run(base::WeakPtr<VideoCaptureController>()); |
686 return; | 726 return; |
687 } | 727 } |
688 | 728 |
689 DCHECK(entry->video_capture_controller()); | 729 DCHECK(entry->video_capture_controller); |
690 | 730 |
691 LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); | 731 LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); |
692 | 732 |
693 // First client starts the device. | 733 // First client starts the device. |
694 if (!entry->video_capture_controller()->HasActiveClient() && | 734 if (!entry->video_capture_controller->HasActiveClient() && |
695 !entry->video_capture_controller()->HasPausedClient()) { | 735 !entry->video_capture_controller->HasPausedClient()) { |
696 DVLOG(1) << "VideoCaptureManager starting device (type = " | 736 DVLOG(1) << "VideoCaptureManager starting device (type = " |
697 << entry->stream_type << ", id = " << entry->id << ")"; | 737 << entry->stream_type << ", id = " << entry->id << ")"; |
698 QueueStartDevice(session_id, entry, params); | 738 QueueStartDevice(session_id, entry, params); |
699 } | 739 } |
700 // Run the callback first, as AddClient() may trigger OnFrameInfo(). | 740 // Run the callback first, as AddClient() may trigger OnFrameInfo(). |
701 done_cb.Run(entry->video_capture_controller()->GetWeakPtrForIOThread()); | 741 done_cb.Run(entry->video_capture_controller->GetWeakPtrForIOThread()); |
702 entry->video_capture_controller()->AddClient( | 742 entry->video_capture_controller->AddClient(client_id, client_handler, |
703 client_id, client_handler, session_id, params); | 743 session_id, params); |
704 } | 744 } |
705 | 745 |
706 void VideoCaptureManager::StopCaptureForClient( | 746 void VideoCaptureManager::StopCaptureForClient( |
707 VideoCaptureController* controller, | 747 VideoCaptureController* controller, |
708 VideoCaptureControllerID client_id, | 748 VideoCaptureControllerID client_id, |
709 VideoCaptureControllerEventHandler* client_handler, | 749 VideoCaptureControllerEventHandler* client_handler, |
710 bool aborted_due_to_error) { | 750 bool aborted_due_to_error) { |
711 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 751 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
712 DCHECK(controller); | 752 DCHECK(controller); |
713 DCHECK(client_handler); | 753 DCHECK(client_handler); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
758 DCHECK(controller); | 798 DCHECK(controller); |
759 DCHECK(client_handler); | 799 DCHECK(client_handler); |
760 DeviceEntry* entry = GetDeviceEntryByController(controller); | 800 DeviceEntry* entry = GetDeviceEntryByController(controller); |
761 if (!entry) | 801 if (!entry) |
762 NOTREACHED() << "Got Null entry while pausing capture"; | 802 NOTREACHED() << "Got Null entry while pausing capture"; |
763 | 803 |
764 const bool had_active_client = controller->HasActiveClient(); | 804 const bool had_active_client = controller->HasActiveClient(); |
765 controller->PauseClient(client_id, client_handler); | 805 controller->PauseClient(client_id, client_handler); |
766 if (!had_active_client || controller->HasActiveClient()) | 806 if (!had_active_client || controller->HasActiveClient()) |
767 return; | 807 return; |
768 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 808 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
769 device_task_runner_->PostTask( | 809 device_task_runner_->PostTask( |
770 FROM_HERE, | 810 FROM_HERE, |
771 base::Bind(&VideoCaptureDevice::MaybeSuspend, | 811 base::Bind(&VideoCaptureDevice::MaybeSuspend, |
772 // Unretained is safe to use here because |device| would be | 812 // Unretained is safe to use here because |device| would be |
773 // null if it was scheduled for shutdown and destruction, and | 813 // null if it was scheduled for shutdown and destruction, and |
774 // because this task is guaranteed to run before the task | 814 // because this task is guaranteed to run before the task |
775 // that destroys the |device|. | 815 // that destroys the |device|. |
776 base::Unretained(device))); | 816 base::Unretained(device))); |
777 } | 817 } |
778 } | 818 } |
779 | 819 |
780 void VideoCaptureManager::ResumeCaptureForClient( | 820 void VideoCaptureManager::ResumeCaptureForClient( |
781 media::VideoCaptureSessionId session_id, | 821 media::VideoCaptureSessionId session_id, |
782 const media::VideoCaptureParams& params, | 822 const media::VideoCaptureParams& params, |
783 VideoCaptureController* controller, | 823 VideoCaptureController* controller, |
784 VideoCaptureControllerID client_id, | 824 VideoCaptureControllerID client_id, |
785 VideoCaptureControllerEventHandler* client_handler) { | 825 VideoCaptureControllerEventHandler* client_handler) { |
786 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 826 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
787 DCHECK(controller); | 827 DCHECK(controller); |
788 DCHECK(client_handler); | 828 DCHECK(client_handler); |
789 | 829 |
790 DeviceEntry* entry = GetDeviceEntryByController(controller); | 830 DeviceEntry* entry = GetDeviceEntryByController(controller); |
791 if (!entry) | 831 if (!entry) |
792 NOTREACHED() << "Got Null entry while resuming capture"; | 832 NOTREACHED() << "Got Null entry while resuming capture"; |
793 | 833 |
794 const bool had_active_client = controller->HasActiveClient(); | 834 const bool had_active_client = controller->HasActiveClient(); |
795 controller->ResumeClient(client_id, client_handler); | 835 controller->ResumeClient(client_id, client_handler); |
796 if (had_active_client || !controller->HasActiveClient()) | 836 if (had_active_client || !controller->HasActiveClient()) |
797 return; | 837 return; |
798 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 838 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
799 device_task_runner_->PostTask( | 839 device_task_runner_->PostTask( |
800 FROM_HERE, | 840 FROM_HERE, |
801 base::Bind(&VideoCaptureDevice::Resume, | 841 base::Bind(&VideoCaptureDevice::Resume, |
802 // Unretained is safe to use here because |device| would be | 842 // Unretained is safe to use here because |device| would be |
803 // null if it was scheduled for shutdown and destruction, and | 843 // null if it was scheduled for shutdown and destruction, and |
804 // because this task is guaranteed to run before the task | 844 // because this task is guaranteed to run before the task |
805 // that destroys the |device|. | 845 // that destroys the |device|. |
806 base::Unretained(device))); | 846 base::Unretained(device))); |
807 } | 847 } |
808 } | 848 } |
809 | 849 |
810 void VideoCaptureManager::RequestRefreshFrameForClient( | 850 void VideoCaptureManager::RequestRefreshFrameForClient( |
811 VideoCaptureController* controller) { | 851 VideoCaptureController* controller) { |
812 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 852 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
813 | 853 |
814 if (DeviceEntry* entry = GetDeviceEntryByController(controller)) { | 854 if (DeviceEntry* entry = GetDeviceEntryByController(controller)) { |
815 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 855 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
816 device_task_runner_->PostTask( | 856 device_task_runner_->PostTask( |
817 FROM_HERE, | 857 FROM_HERE, |
818 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, | 858 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, |
819 // Unretained is safe to use here because |device| would be | 859 // Unretained is safe to use here because |device| would be |
820 // null if it was scheduled for shutdown and destruction, | 860 // null if it was scheduled for shutdown and destruction, |
821 // and because this task is guaranteed to run before the | 861 // and because this task is guaranteed to run before the |
822 // task that destroys the |device|. | 862 // task that destroys the |device|. |
823 base::Unretained(device))); | 863 base::Unretained(device))); |
824 } | 864 } |
825 } | 865 } |
(...skipping 27 matching lines...) Expand all Loading... | |
853 if (it == sessions_.end()) | 893 if (it == sessions_.end()) |
854 return false; | 894 return false; |
855 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; | 895 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; |
856 | 896 |
857 // Return the currently in-use format(s) of the device, if it's started. | 897 // Return the currently in-use format(s) of the device, if it's started. |
858 DeviceEntry* device_in_use = | 898 DeviceEntry* device_in_use = |
859 GetDeviceEntryByTypeAndId(it->second.type, it->second.id); | 899 GetDeviceEntryByTypeAndId(it->second.type, it->second.id); |
860 if (device_in_use) { | 900 if (device_in_use) { |
861 // Currently only one format-in-use is supported at the VCC level. | 901 // Currently only one format-in-use is supported at the VCC level. |
862 formats_in_use->push_back( | 902 formats_in_use->push_back( |
863 device_in_use->video_capture_controller()->GetVideoCaptureFormat()); | 903 device_in_use->video_capture_controller->GetVideoCaptureFormat()); |
864 } | 904 } |
865 return true; | 905 return true; |
866 } | 906 } |
867 | 907 |
868 void VideoCaptureManager::SetDesktopCaptureWindowId( | 908 void VideoCaptureManager::SetDesktopCaptureWindowId( |
869 media::VideoCaptureSessionId session_id, | 909 media::VideoCaptureSessionId session_id, |
870 gfx::NativeViewId window_id) { | 910 gfx::NativeViewId window_id) { |
871 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 911 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
872 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; | 912 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; |
873 | 913 |
874 notification_window_ids_[session_id] = window_id; | 914 notification_window_ids_[session_id] = window_id; |
875 MaybePostDesktopCaptureWindowId(session_id); | 915 MaybePostDesktopCaptureWindowId(session_id); |
876 } | 916 } |
877 | 917 |
878 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( | 918 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( |
879 media::VideoCaptureSessionId session_id) { | 919 media::VideoCaptureSessionId session_id) { |
880 SessionMap::iterator session_it = sessions_.find(session_id); | 920 SessionMap::iterator session_it = sessions_.find(session_id); |
881 if (session_it == sessions_.end()) | 921 if (session_it == sessions_.end()) |
882 return; | 922 return; |
883 | 923 |
884 DeviceEntry* const existing_device = | 924 DeviceEntry* const existing_device = |
885 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); | 925 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
886 if (!existing_device) { | 926 if (!existing_device) { |
887 DVLOG(2) << "Failed to find an existing screen capture device."; | 927 DVLOG(2) << "Failed to find an existing screen capture device."; |
888 return; | 928 return; |
889 } | 929 } |
890 | 930 |
891 if (!existing_device->video_capture_device()) { | 931 if (!existing_device->video_capture_device) { |
892 DVLOG(2) << "Screen capture device not yet started."; | 932 DVLOG(2) << "Screen capture device not yet started."; |
893 return; | 933 return; |
894 } | 934 } |
895 | 935 |
896 DCHECK_EQ(MEDIA_DESKTOP_VIDEO_CAPTURE, existing_device->stream_type); | 936 DCHECK_EQ(MEDIA_DESKTOP_VIDEO_CAPTURE, existing_device->stream_type); |
897 DesktopMediaID id = DesktopMediaID::Parse(existing_device->id); | 937 DesktopMediaID id = DesktopMediaID::Parse(existing_device->id); |
898 if (id.is_null()) | 938 if (id.is_null()) |
899 return; | 939 return; |
900 | 940 |
901 auto window_id_it = notification_window_ids_.find(session_id); | 941 auto window_id_it = notification_window_ids_.find(session_id); |
902 if (window_id_it == notification_window_ids_.end()) { | 942 if (window_id_it == notification_window_ids_.end()) { |
903 DVLOG(2) << "Notification window id not set for screen capture."; | 943 DVLOG(2) << "Notification window id not set for screen capture."; |
904 return; | 944 return; |
905 } | 945 } |
906 | 946 |
907 // Post |existing_device->video_capture_device| to the VideoCaptureDevice to | 947 // Post |existing_device->video_capture_device| to the VideoCaptureDevice to |
908 // the device_task_runner_. This is safe since the device is destroyed on the | 948 // the device_task_runner_. This is safe since the device is destroyed on the |
909 // device_task_runner_. | 949 // device_task_runner_. |
910 device_task_runner_->PostTask( | 950 device_task_runner_->PostTask( |
911 FROM_HERE, | 951 FROM_HERE, |
912 base::Bind(&VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread, | 952 base::Bind(&VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread, |
913 this, | 953 this, existing_device->video_capture_device.get(), |
914 existing_device->video_capture_device(), | |
915 window_id_it->second)); | 954 window_id_it->second)); |
916 | 955 |
917 notification_window_ids_.erase(window_id_it); | 956 notification_window_ids_.erase(window_id_it); |
918 } | 957 } |
919 | 958 |
920 void VideoCaptureManager::GetPhotoCapabilities( | 959 void VideoCaptureManager::GetPhotoCapabilities( |
921 int session_id, | 960 int session_id, |
922 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) { | 961 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) { |
923 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 962 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
924 | 963 |
925 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 964 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
926 if (!entry) | 965 if (!entry) |
927 return; | 966 return; |
928 VideoCaptureDevice* device = entry->video_capture_device(); | 967 VideoCaptureDevice* device = entry->video_capture_device.get(); |
929 if (device) { | 968 if (device) { |
930 VideoCaptureManager::DoGetPhotoCapabilities(std::move(callback), device); | 969 VideoCaptureManager::DoGetPhotoCapabilities(std::move(callback), device); |
931 return; | 970 return; |
932 } | 971 } |
933 // |entry| is known but |device| is nullptr, queue up a request for later. | 972 // |entry| is known but |device| is nullptr, queue up a request for later. |
934 photo_request_queue_.emplace_back( | 973 photo_request_queue_.emplace_back( |
935 session_id, base::Bind(&VideoCaptureManager::DoGetPhotoCapabilities, this, | 974 session_id, base::Bind(&VideoCaptureManager::DoGetPhotoCapabilities, this, |
936 base::Passed(&callback))); | 975 base::Passed(&callback))); |
937 } | 976 } |
938 | 977 |
939 void VideoCaptureManager::SetPhotoOptions( | 978 void VideoCaptureManager::SetPhotoOptions( |
940 int session_id, | 979 int session_id, |
941 media::mojom::PhotoSettingsPtr settings, | 980 media::mojom::PhotoSettingsPtr settings, |
942 VideoCaptureDevice::SetPhotoOptionsCallback callback) { | 981 VideoCaptureDevice::SetPhotoOptionsCallback callback) { |
943 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 982 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
944 | 983 |
945 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 984 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
946 if (!entry) | 985 if (!entry) |
947 return; | 986 return; |
948 VideoCaptureDevice* device = entry->video_capture_device(); | 987 VideoCaptureDevice* device = entry->video_capture_device.get(); |
949 if (device) { | 988 if (device) { |
950 VideoCaptureManager::DoSetPhotoOptions(std::move(callback), | 989 VideoCaptureManager::DoSetPhotoOptions(std::move(callback), |
951 std::move(settings), device); | 990 std::move(settings), device); |
952 return; | 991 return; |
953 } | 992 } |
954 // |entry| is known but |device| is nullptr, queue up a request for later. | 993 // |entry| is known but |device| is nullptr, queue up a request for later. |
955 photo_request_queue_.emplace_back( | 994 photo_request_queue_.emplace_back( |
956 session_id, base::Bind(&VideoCaptureManager::DoSetPhotoOptions, this, | 995 session_id, base::Bind(&VideoCaptureManager::DoSetPhotoOptions, this, |
957 base::Passed(&callback), base::Passed(&settings))); | 996 base::Passed(&callback), base::Passed(&settings))); |
958 } | 997 } |
959 | 998 |
960 void VideoCaptureManager::TakePhoto( | 999 void VideoCaptureManager::TakePhoto( |
961 int session_id, | 1000 int session_id, |
962 VideoCaptureDevice::TakePhotoCallback callback) { | 1001 VideoCaptureDevice::TakePhotoCallback callback) { |
963 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1002 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
964 | 1003 |
965 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 1004 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
966 if (!entry) | 1005 if (!entry) |
967 return; | 1006 return; |
968 VideoCaptureDevice* device = entry->video_capture_device(); | 1007 VideoCaptureDevice* device = entry->video_capture_device.get(); |
969 if (device) { | 1008 if (device) { |
970 VideoCaptureManager::DoTakePhoto(std::move(callback), device); | 1009 VideoCaptureManager::DoTakePhoto(std::move(callback), device); |
971 return; | 1010 return; |
972 } | 1011 } |
973 // |entry| is known but |device| is nullptr, queue up a request for later. | 1012 // |entry| is known but |device| is nullptr, queue up a request for later. |
974 photo_request_queue_.emplace_back( | 1013 photo_request_queue_.emplace_back( |
975 session_id, base::Bind(&VideoCaptureManager::DoTakePhoto, this, | 1014 session_id, base::Bind(&VideoCaptureManager::DoTakePhoto, this, |
976 base::Passed(&callback))); | 1015 base::Passed(&callback))); |
977 } | 1016 } |
978 | 1017 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1066 ConsolidateCaptureFormats(&device_info.supported_formats); | 1105 ConsolidateCaptureFormats(&device_info.supported_formats); |
1067 new_devices_info_cache.push_back(device_info); | 1106 new_devices_info_cache.push_back(device_info); |
1068 } | 1107 } |
1069 | 1108 |
1070 on_devices_enumerated_callback.Run(new_devices_info_cache); | 1109 on_devices_enumerated_callback.Run(new_devices_info_cache); |
1071 } | 1110 } |
1072 | 1111 |
1073 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { | 1112 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { |
1074 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1113 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1075 // Removal of the last client stops the device. | 1114 // Removal of the last client stops the device. |
1076 if (!entry->video_capture_controller()->HasActiveClient() && | 1115 if (!entry->video_capture_controller->HasActiveClient() && |
1077 !entry->video_capture_controller()->HasPausedClient()) { | 1116 !entry->video_capture_controller->HasPausedClient()) { |
1078 DVLOG(1) << "VideoCaptureManager stopping device (type = " | 1117 DVLOG(1) << "VideoCaptureManager stopping device (type = " |
1079 << entry->stream_type << ", id = " << entry->id << ")"; | 1118 << entry->stream_type << ", id = " << entry->id << ")"; |
1080 | 1119 |
1081 // The DeviceEntry is removed from |devices_| immediately. The controller is | 1120 // The DeviceEntry is removed from |devices_| immediately. The controller is |
1082 // deleted immediately, and the device is freed asynchronously. After this | 1121 // deleted immediately, and the device is freed asynchronously. After this |
1083 // point, subsequent requests to open this same device ID will create a new | 1122 // point, subsequent requests to open this same device ID will create a new |
1084 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. | 1123 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. |
1085 DoStopDevice(entry); | 1124 DoStopDevice(entry); |
1086 // TODO(mcasas): use a helper function https://crbug.com/624854. | 1125 // TODO(mcasas): use a helper function https://crbug.com/624854. |
1087 DeviceEntries::iterator device_it = | 1126 DeviceEntries::iterator device_it = |
(...skipping 29 matching lines...) Expand all Loading... | |
1117 return nullptr; | 1156 return nullptr; |
1118 } | 1157 } |
1119 | 1158 |
1120 VideoCaptureManager::DeviceEntry* | 1159 VideoCaptureManager::DeviceEntry* |
1121 VideoCaptureManager::GetDeviceEntryByController( | 1160 VideoCaptureManager::GetDeviceEntryByController( |
1122 const VideoCaptureController* controller) const { | 1161 const VideoCaptureController* controller) const { |
1123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1124 | 1163 |
1125 // Look up |controller| in |devices_|. | 1164 // Look up |controller| in |devices_|. |
1126 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | 1165 for (const std::unique_ptr<DeviceEntry>& device : devices_) { |
1127 if (device->video_capture_controller() == controller) | 1166 if (device->video_capture_controller.get() == controller) |
1128 return device.get(); | 1167 return device.get(); |
1129 } | 1168 } |
1130 return nullptr; | 1169 return nullptr; |
1131 } | 1170 } |
1132 | 1171 |
1133 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetDeviceEntryBySerialId( | 1172 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetDeviceEntryBySerialId( |
1134 int serial_id) const { | 1173 int serial_id) const { |
1135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1174 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1136 | 1175 |
1137 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | 1176 for (const std::unique_ptr<DeviceEntry>& device : devices_) { |
(...skipping 24 matching lines...) Expand all Loading... | |
1162 | 1201 |
1163 // Check if another session has already opened this device. If so, just | 1202 // Check if another session has already opened this device. If so, just |
1164 // use that opened device. | 1203 // use that opened device. |
1165 DeviceEntry* const existing_device = | 1204 DeviceEntry* const existing_device = |
1166 GetDeviceEntryByTypeAndId(device_info.type, device_info.id); | 1205 GetDeviceEntryByTypeAndId(device_info.type, device_info.id); |
1167 if (existing_device) { | 1206 if (existing_device) { |
1168 DCHECK_EQ(device_info.type, existing_device->stream_type); | 1207 DCHECK_EQ(device_info.type, existing_device->stream_type); |
1169 return existing_device; | 1208 return existing_device; |
1170 } | 1209 } |
1171 | 1210 |
1172 const int max_buffers = device_info.type == MEDIA_TAB_VIDEO_CAPTURE ? | 1211 auto video_capture_controller = base::MakeUnique<VideoCaptureController>(); |
1173 kMaxNumberOfBuffersForTabCapture : kMaxNumberOfBuffers; | 1212 auto new_device_entry = |
1174 std::unique_ptr<VideoCaptureController> video_capture_controller( | |
1175 new VideoCaptureController(max_buffers)); | |
1176 DeviceEntry* new_device = | |
1177 new DeviceEntry(device_info.type, device_info.id, | 1213 new DeviceEntry(device_info.type, device_info.id, |
1178 std::move(video_capture_controller), params); | 1214 std::move(video_capture_controller), params); |
1179 devices_.emplace_back(new_device); | 1215 devices_.emplace_back(new_device_entry); |
1180 return new_device; | 1216 return new_device_entry; |
1181 } | 1217 } |
1182 | 1218 |
1183 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( | 1219 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( |
1184 media::VideoCaptureDevice* device, | 1220 media::VideoCaptureDevice* device, |
1185 gfx::NativeViewId window_id) { | 1221 gfx::NativeViewId window_id) { |
1186 DCHECK(IsOnDeviceThread()); | 1222 DCHECK(IsOnDeviceThread()); |
1187 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) | 1223 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) |
1188 DesktopCaptureDevice* desktop_device = | 1224 DesktopCaptureDevice* desktop_device = |
1189 static_cast<DesktopCaptureDevice*>(device); | 1225 static_cast<DesktopCaptureDevice*>(device); |
1190 desktop_device->SetNotificationWindowId(window_id); | 1226 desktop_device->SetNotificationWindowId(window_id); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1256 } | 1292 } |
1257 } | 1293 } |
1258 | 1294 |
1259 void VideoCaptureManager::ResumeDevices() { | 1295 void VideoCaptureManager::ResumeDevices() { |
1260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1296 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1261 | 1297 |
1262 for (auto& entry : devices_) { | 1298 for (auto& entry : devices_) { |
1263 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 1299 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. |
1264 // Do not try to restart already running devices. | 1300 // Do not try to restart already running devices. |
1265 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || | 1301 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || |
1266 entry->video_capture_device()) | 1302 entry->video_capture_device) |
1267 continue; | 1303 continue; |
1268 | 1304 |
1269 // Check if the device is already in the start queue. | 1305 // Check if the device is already in the start queue. |
1270 bool device_in_queue = false; | 1306 bool device_in_queue = false; |
1271 for (auto& request : device_start_queue_) { | 1307 for (auto& request : device_start_queue_) { |
1272 if (request.serial_id() == entry->serial_id) { | 1308 if (request.serial_id() == entry->serial_id) { |
1273 device_in_queue = true; | 1309 device_in_queue = true; |
1274 break; | 1310 break; |
1275 } | 1311 } |
1276 } | 1312 } |
1277 | 1313 |
1278 if (!device_in_queue) { | 1314 if (!device_in_queue) { |
1279 // Session ID is only valid for Screen capture. So we can fake it to | 1315 // Session ID is only valid for Screen capture. So we can fake it to |
1280 // resume video capture devices here. | 1316 // resume video capture devices here. |
1281 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); | 1317 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); |
1282 } | 1318 } |
1283 } | 1319 } |
1284 } | 1320 } |
1285 #endif // defined(OS_ANDROID) | 1321 #endif // defined(OS_ANDROID) |
1286 | 1322 |
1287 } // namespace content | 1323 } // namespace content |
OLD | NEW |