Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: content/browser/renderer_host/media/video_capture_manager.cc

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

Powered by Google App Engine
This is Rietveld 408576698