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

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

Issue 2805533002: Reland [Mojo Video Capture] Introduce abstraction VideoCaptureSystem (Closed)
Patch Set: Fix race condition in tests Created 3 years, 8 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/in_process_buildable_video_capture _device.h" 5 #include "content/browser/renderer_host/media/in_process_buildable_video_capture _device.h"
6 6
7 #include "base/metrics/histogram_macros.h" 7 #include "base/metrics/histogram_macros.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" 9 #include "content/browser/media/capture/desktop_capture_device_uma_types.h"
10 #include "content/browser/media/capture/web_contents_video_capture_device.h" 10 #include "content/browser/media/capture/web_contents_video_capture_device.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // TODO(miu): The value for tab capture should be determined programmatically. 77 // TODO(miu): The value for tab capture should be determined programmatically.
78 // http://crbug.com/460318 78 // http://crbug.com/460318
79 const int kMaxNumberOfBuffersForTabCapture = 10; 79 const int kMaxNumberOfBuffersForTabCapture = 10;
80 80
81 } // anonymous namespace 81 } // anonymous namespace
82 82
83 namespace content { 83 namespace content {
84 84
85 InProcessBuildableVideoCaptureDevice::InProcessBuildableVideoCaptureDevice( 85 InProcessBuildableVideoCaptureDevice::InProcessBuildableVideoCaptureDevice(
86 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner, 86 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner,
87 media::VideoCaptureDeviceFactory* device_factory) 87 media::VideoCaptureSystem* video_capture_system)
88 : device_task_runner_(std::move(device_task_runner)), 88 : device_task_runner_(std::move(device_task_runner)),
89 device_factory_(device_factory) {} 89 video_capture_system_(video_capture_system) {}
90 90
91 InProcessBuildableVideoCaptureDevice::~InProcessBuildableVideoCaptureDevice() { 91 InProcessBuildableVideoCaptureDevice::~InProcessBuildableVideoCaptureDevice() {
92 DCHECK_CURRENTLY_ON(BrowserThread::IO); 92 DCHECK_CURRENTLY_ON(BrowserThread::IO);
93 DCHECK(!device_); 93 DCHECK(!device_);
94 } 94 }
95 95
96 void InProcessBuildableVideoCaptureDevice::CreateAndStartDeviceAsync( 96 void InProcessBuildableVideoCaptureDevice::CreateAndStartDeviceAsync(
97 VideoCaptureController* controller, 97 VideoCaptureController* controller,
98 const media::VideoCaptureParams& params, 98 const media::VideoCaptureParams& params,
99 Callbacks* callbacks, 99 Callbacks* callbacks,
100 base::OnceClosure done_cb) { 100 base::OnceClosure done_cb) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO); 101 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102 DCHECK_EQ(State::NO_DEVICE, state_); 102 DCHECK_EQ(State::NO_DEVICE, state_);
103 103
104 const int max_buffers = (controller->stream_type() == MEDIA_TAB_VIDEO_CAPTURE 104 const int max_buffers = (controller->stream_type() == MEDIA_TAB_VIDEO_CAPTURE
105 ? kMaxNumberOfBuffersForTabCapture 105 ? kMaxNumberOfBuffersForTabCapture
106 : kMaxNumberOfBuffers); 106 : kMaxNumberOfBuffers);
107 107
108 auto device_client = 108 auto device_client =
109 CreateDeviceClient(max_buffers, controller->GetWeakPtrForIOThread()); 109 CreateDeviceClient(max_buffers, controller->GetWeakPtrForIOThread());
110 110
111 base::Closure start_capture_closure; 111 base::Closure start_capture_closure;
112 // Use of Unretained() is safe, because |done_cb| guarantees that
113 // |this| stays alive.
114 ReceiveDeviceCallback after_start_capture_callback = media::BindToCurrentLoop(
115 base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted,
116 base::Unretained(this), controller, callbacks,
117 base::Passed(&done_cb)));
118
112 switch (controller->stream_type()) { 119 switch (controller->stream_type()) {
113 case MEDIA_DEVICE_VIDEO_CAPTURE: { 120 case MEDIA_DEVICE_VIDEO_CAPTURE: {
114 const media::VideoCaptureDeviceDescriptor* descriptor =
115 callbacks->LookupDeviceDescriptor(controller->device_id());
116 if (!descriptor) {
117 callbacks->OnDeviceStartFailed(controller);
118 base::ResetAndReturn(&done_cb).Run();
119 return;
120 }
121 controller->OnLog(base::StringPrintf(
122 "Starting device: id: %s, name: %s, api: %s",
123 descriptor->device_id.c_str(), descriptor->GetNameAndModel().c_str(),
124 descriptor->GetCaptureApiTypeString()));
125
126 callbacks->WillStartDevice(descriptor->facing);
127
128 // Use of Unretained() is safe, because |done_cb| guarantees that |this|
129 // stays alive.
130 ReceiveDeviceCallback after_start_capture_callback =
131 media::BindToCurrentLoop(
132 base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted,
133 base::Unretained(this), controller, callbacks,
134 base::Passed(&done_cb)));
135 start_capture_closure = 121 start_capture_closure =
136 base::Bind(&InProcessBuildableVideoCaptureDevice:: 122 base::Bind(&InProcessBuildableVideoCaptureDevice::
137 DoStartDeviceCaptureOnDeviceThread, 123 DoStartDeviceCaptureOnDeviceThread,
138 base::Unretained(this), *descriptor, params, 124 base::Unretained(this), controller->device_id(), params,
139 base::Passed(std::move(device_client)), 125 base::Passed(std::move(device_client)),
140 std::move(after_start_capture_callback)); 126 std::move(after_start_capture_callback));
141 break; 127 break;
142 } 128 }
143 case MEDIA_TAB_VIDEO_CAPTURE: { 129 case MEDIA_TAB_VIDEO_CAPTURE:
144 // Use of Unretained() is safe, because |done_cb| guarantees that |this|
145 // stays alive.
146 ReceiveDeviceCallback after_start_capture_callback =
147 media::BindToCurrentLoop(
148 base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted,
149 base::Unretained(this), controller, callbacks,
150 base::Passed(&done_cb)));
151 start_capture_closure = 130 start_capture_closure =
152 base::Bind(&InProcessBuildableVideoCaptureDevice:: 131 base::Bind(&InProcessBuildableVideoCaptureDevice::
153 DoStartTabCaptureOnDeviceThread, 132 DoStartTabCaptureOnDeviceThread,
154 base::Unretained(this), controller->device_id(), params, 133 base::Unretained(this), controller->device_id(), params,
155 base::Passed(std::move(device_client)), 134 base::Passed(std::move(device_client)),
156 std::move(after_start_capture_callback)); 135 std::move(after_start_capture_callback));
157 break; 136 break;
158 } 137
159 case MEDIA_DESKTOP_VIDEO_CAPTURE: { 138 case MEDIA_DESKTOP_VIDEO_CAPTURE:
160 // Use of Unretained() is safe, because |done_cb| guarantees that |this|
161 // stays alive.
162 ReceiveDeviceCallback after_start_capture_callback =
163 media::BindToCurrentLoop(
164 base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted,
165 base::Unretained(this), controller, callbacks,
166 base::Passed(&done_cb)));
167 start_capture_closure = 139 start_capture_closure =
168 base::Bind(&InProcessBuildableVideoCaptureDevice:: 140 base::Bind(&InProcessBuildableVideoCaptureDevice::
169 DoStartDesktopCaptureOnDeviceThread, 141 DoStartDesktopCaptureOnDeviceThread,
170 base::Unretained(this), controller->device_id(), params, 142 base::Unretained(this), controller->device_id(), params,
171 base::Passed(std::move(device_client)), 143 base::Passed(std::move(device_client)),
172 std::move(after_start_capture_callback)); 144 std::move(after_start_capture_callback));
173 break; 145 break;
174 } 146
175 default: { 147 default: {
176 NOTIMPLEMENTED(); 148 NOTIMPLEMENTED();
177 return; 149 return;
178 } 150 }
179 } 151 }
180 152
181 device_task_runner_->PostTask(FROM_HERE, start_capture_closure); 153 device_task_runner_->PostTask(FROM_HERE, start_capture_closure);
182 state_ = State::DEVICE_START_IN_PROGRESS; 154 state_ = State::DEVICE_START_IN_PROGRESS;
183 } 155 }
184 156
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 } 303 }
332 // Passing raw pointer |device.get()| to the controller is safe, 304 // Passing raw pointer |device.get()| to the controller is safe,
333 // because we take ownership of |device| and we call 305 // because we take ownership of |device| and we call
334 // controller->SetConsumerFeedbackObserver(nullptr) before releasing 306 // controller->SetConsumerFeedbackObserver(nullptr) before releasing
335 // |device|. 307 // |device|.
336 controller->SetConsumerFeedbackObserver( 308 controller->SetConsumerFeedbackObserver(
337 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( 309 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>(
338 device.get(), device_task_runner_)); 310 device.get(), device_task_runner_));
339 device_ = std::move(device); 311 device_ = std::move(device);
340 state_ = State::DEVICE_STARTED; 312 state_ = State::DEVICE_STARTED;
341 callbacks->DidStartDevice(controller); 313 callbacks->OnDeviceStarted(controller);
342 base::ResetAndReturn(&done_cb).Run(); 314 base::ResetAndReturn(&done_cb).Run();
343 return; 315 return;
344 case State::DEVICE_START_ABORTING: 316 case State::DEVICE_START_ABORTING:
345 if (device) { 317 if (device) {
346 device_ = std::move(device); 318 device_ = std::move(device);
347 state_ = State::DEVICE_STARTED; 319 state_ = State::DEVICE_STARTED;
348 // We do not move our |done_cb| to this invocation, because 320 // We do not move our |done_cb| to this invocation, because
349 // we still need it to stay alive for the remainder of this method 321 // we still need it to stay alive for the remainder of this method
350 // execution. Our implementation of ReleaseDeviceAsync() does not 322 // execution. Our implementation of ReleaseDeviceAsync() does not
351 // actually need the context while releasing the device. 323 // actually need the context while releasing the device.
352 ReleaseDeviceAsync(controller, base::Bind([]() {})); 324 ReleaseDeviceAsync(controller, base::Bind([]() {}));
353 } 325 }
354 state_ = State::NO_DEVICE; 326 state_ = State::NO_DEVICE;
355 callbacks->OnDeviceStartAborted(); 327 callbacks->OnDeviceStartAborted();
356 base::ResetAndReturn(&done_cb).Run(); 328 base::ResetAndReturn(&done_cb).Run();
357 return; 329 return;
358 case State::NO_DEVICE: 330 case State::NO_DEVICE:
359 case State::DEVICE_STARTED: 331 case State::DEVICE_STARTED:
360 NOTREACHED(); 332 NOTREACHED();
361 return; 333 return;
362 } 334 }
363 } 335 }
364 336
365 void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( 337 void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread(
366 const media::VideoCaptureDeviceDescriptor& descriptor, 338 const std::string& device_id,
367 const media::VideoCaptureParams& params, 339 const media::VideoCaptureParams& params,
368 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, 340 std::unique_ptr<media::VideoCaptureDeviceClient> device_client,
369 ReceiveDeviceCallback result_callback) { 341 ReceiveDeviceCallback result_callback) {
370 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); 342 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime");
371 DCHECK(device_task_runner_->BelongsToCurrentThread()); 343 DCHECK(device_task_runner_->BelongsToCurrentThread());
372 344
373 std::unique_ptr<media::VideoCaptureDevice> video_capture_device = 345 std::unique_ptr<media::VideoCaptureDevice> video_capture_device =
374 device_factory_->CreateDevice(descriptor); 346 video_capture_system_->CreateDevice(device_id);
375 347
376 if (!video_capture_device) { 348 if (!video_capture_device) {
377 result_callback.Run(nullptr); 349 result_callback.Run(nullptr);
378 return; 350 return;
379 } 351 }
380 352
381 video_capture_device->AllocateAndStart(params, std::move(device_client)); 353 video_capture_device->AllocateAndStart(params, std::move(device_client));
382 result_callback.Run(std::move(video_capture_device)); 354 result_callback.Run(std::move(video_capture_device));
383 } 355 }
384 356
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 !defined(OS_ANDROID) 435 !defined(OS_ANDROID)
464 DesktopCaptureDevice* desktop_device = 436 DesktopCaptureDevice* desktop_device =
465 static_cast<DesktopCaptureDevice*>(device); 437 static_cast<DesktopCaptureDevice*>(device);
466 desktop_device->SetNotificationWindowId(window_id); 438 desktop_device->SetNotificationWindowId(window_id);
467 VLOG(2) << "Screen capture notification window passed on device thread."; 439 VLOG(2) << "Screen capture notification window passed on device thread.";
468 #endif 440 #endif
469 base::ResetAndReturn(&done_cb).Run(); 441 base::ResetAndReturn(&done_cb).Run();
470 } 442 }
471 443
472 } // namespace content 444 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698