OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/renderer_host/media/in_process_buildable_video_capture
_device.h" | |
6 | |
7 #include "base/metrics/histogram_macros.h" | |
8 #include "base/strings/stringprintf.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" | |
11 #include "content/browser/renderer_host/media/video_capture_controller.h" | |
12 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/desktop_media_id.h" | |
15 #include "content/public/common/media_stream_request.h" | |
16 #include "media/base/bind_to_current_loop.h" | |
17 #include "media/capture/video/video_capture_buffer_pool_impl.h" | |
18 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" | |
19 #include "media/capture/video/video_capture_device_client.h" | |
20 #include "media/capture/video/video_frame_receiver_on_task_runner.h" | |
21 | |
22 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) | |
23 #include "content/browser/media/capture/desktop_capture_device.h" | |
24 #if defined(USE_AURA) | |
25 #include "content/browser/media/capture/desktop_capture_device_aura.h" | |
26 #endif | |
27 #endif | |
28 | |
29 #if defined(ENABLE_SCREEN_CAPTURE) && defined(OS_ANDROID) | |
30 #include "content/browser/media/capture/screen_capture_device_android.h" | |
31 #endif | |
32 | |
33 namespace { | |
34 | |
35 class VideoFrameConsumerFeedbackObserverOnTaskRunner | |
36 : public media::VideoFrameConsumerFeedbackObserver { | |
37 public: | |
38 VideoFrameConsumerFeedbackObserverOnTaskRunner( | |
39 media::VideoFrameConsumerFeedbackObserver* observer, | |
40 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
41 : observer_(observer), task_runner_(std::move(task_runner)) {} | |
42 | |
43 void OnUtilizationReport(int frame_feedback_id, double utilization) override { | |
44 task_runner_->PostTask( | |
45 FROM_HERE, | |
46 base::Bind( | |
47 &media::VideoFrameConsumerFeedbackObserver::OnUtilizationReport, | |
48 base::Unretained(observer_), frame_feedback_id, utilization)); | |
49 } | |
50 | |
51 private: | |
52 media::VideoFrameConsumerFeedbackObserver* const observer_; | |
53 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
54 }; | |
55 | |
56 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( | |
57 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { | |
58 return base::MakeUnique<content::VideoCaptureGpuJpegDecoder>(decode_done_cb); | |
59 } | |
60 | |
61 void StopAndReleaseDeviceOnDeviceThread(media::VideoCaptureDevice* device, | |
62 base::OnceClosure done_cb) { | |
63 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StopDeviceTime"); | |
64 device->StopAndDeAllocate(); | |
65 DVLOG(3) << "StopAndReleaseDeviceOnDeviceThread"; | |
66 delete device; | |
67 base::ResetAndReturn(&done_cb).Run(); | |
68 } | |
69 | |
70 // The maximum number of video frame buffers in-flight at any one time. This | |
71 // value should be based on the logical capacity of the capture pipeline, and | |
72 // not on hardware performance. For example, tab capture requires more buffers | |
73 // than webcam capture because the pipeline is longer (it includes read-backs | |
74 // pending in the GPU pipeline). | |
75 const int kMaxNumberOfBuffers = 3; | |
76 // TODO(miu): The value for tab capture should be determined programmatically. | |
77 // http://crbug.com/460318 | |
78 const int kMaxNumberOfBuffersForTabCapture = 10; | |
79 | |
80 } // anonymous namespace | |
81 | |
82 namespace content { | |
83 | |
84 InProcessBuildableVideoCaptureDevice::InProcessBuildableVideoCaptureDevice( | |
85 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner, | |
86 media::VideoCaptureSystem* video_capture_system) | |
87 : device_task_runner_(std::move(device_task_runner)), | |
88 video_capture_system_(video_capture_system) {} | |
89 | |
90 InProcessBuildableVideoCaptureDevice::~InProcessBuildableVideoCaptureDevice() { | |
91 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
92 DCHECK(!device_); | |
93 } | |
94 | |
95 void InProcessBuildableVideoCaptureDevice::CreateAndStartDeviceAsync( | |
96 VideoCaptureController* controller, | |
97 const media::VideoCaptureParams& params, | |
98 Callbacks* callbacks, | |
99 base::OnceClosure done_cb) { | |
100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
101 DCHECK_EQ(State::NO_DEVICE, state_); | |
102 | |
103 const int max_buffers = (controller->stream_type() == MEDIA_TAB_VIDEO_CAPTURE | |
104 ? kMaxNumberOfBuffersForTabCapture | |
105 : kMaxNumberOfBuffers); | |
106 | |
107 auto device_client = | |
108 CreateDeviceClient(max_buffers, controller->GetWeakPtrForIOThread()); | |
109 | |
110 base::Closure start_capture_closure; | |
111 // Use of Unretained() is safe, because |done_cb| guarantees that | |
112 // |this| stays alive. | |
113 ReceiveDeviceCallback after_start_capture_callback = media::BindToCurrentLoop( | |
114 base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted, | |
115 base::Unretained(this), controller, callbacks, | |
116 base::Passed(&done_cb))); | |
117 | |
118 switch (controller->stream_type()) { | |
119 case MEDIA_DEVICE_VIDEO_CAPTURE: { | |
120 start_capture_closure = | |
121 base::Bind(&InProcessBuildableVideoCaptureDevice:: | |
122 DoStartDeviceCaptureOnDeviceThread, | |
123 base::Unretained(this), controller->device_id(), params, | |
124 base::Passed(std::move(device_client)), | |
125 std::move(after_start_capture_callback)); | |
126 break; | |
127 } | |
128 case MEDIA_TAB_VIDEO_CAPTURE: | |
129 start_capture_closure = | |
130 base::Bind(&InProcessBuildableVideoCaptureDevice:: | |
131 DoStartTabCaptureOnDeviceThread, | |
132 base::Unretained(this), controller->device_id(), params, | |
133 base::Passed(std::move(device_client)), | |
134 std::move(after_start_capture_callback)); | |
135 break; | |
136 | |
137 case MEDIA_DESKTOP_VIDEO_CAPTURE: | |
138 start_capture_closure = | |
139 base::Bind(&InProcessBuildableVideoCaptureDevice:: | |
140 DoStartDesktopCaptureOnDeviceThread, | |
141 base::Unretained(this), controller->device_id(), params, | |
142 base::Passed(std::move(device_client)), | |
143 std::move(after_start_capture_callback)); | |
144 break; | |
145 | |
146 default: { | |
147 NOTIMPLEMENTED(); | |
148 return; | |
149 } | |
150 } | |
151 | |
152 device_task_runner_->PostTask(FROM_HERE, start_capture_closure); | |
153 state_ = State::DEVICE_START_IN_PROGRESS; | |
154 } | |
155 | |
156 void InProcessBuildableVideoCaptureDevice::ReleaseDeviceAsync( | |
157 VideoCaptureController* controller, | |
158 base::OnceClosure done_cb) { | |
159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
160 controller->SetConsumerFeedbackObserver(nullptr); | |
161 switch (state_) { | |
162 case State::DEVICE_START_IN_PROGRESS: | |
163 state_ = State::DEVICE_START_ABORTING; | |
164 return; | |
165 case State::NO_DEVICE: | |
166 case State::DEVICE_START_ABORTING: | |
167 return; | |
168 case State::DEVICE_STARTED: | |
169 media::VideoCaptureDevice* device_ptr = device_.release(); | |
170 bool posting_task_succeeded = device_task_runner_->PostTask( | |
171 FROM_HERE, | |
172 base::Bind( | |
173 &StopAndReleaseDeviceOnDeviceThread, device_ptr, | |
174 base::Bind([](scoped_refptr<base::SingleThreadTaskRunner>) {}, | |
175 device_task_runner_))); | |
176 if (posting_task_succeeded == false) { | |
177 // Since posting to the task runner has failed, we attempt doing it on | |
178 // the calling thread instead. | |
179 StopAndReleaseDeviceOnDeviceThread(device_ptr, base::Bind([]() {})); | |
180 } | |
181 state_ = State::NO_DEVICE; | |
182 return; | |
183 } | |
184 base::ResetAndReturn(&done_cb).Run(); | |
185 } | |
186 | |
187 bool InProcessBuildableVideoCaptureDevice::IsDeviceAlive() const { | |
188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
189 return device_ != nullptr; | |
190 } | |
191 | |
192 void InProcessBuildableVideoCaptureDevice::GetPhotoCapabilities( | |
193 media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) const { | |
194 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
195 // Unretained() is safe to use here because |device| would be null if it | |
196 // was scheduled for shutdown and destruction, and because this task is | |
197 // guaranteed to run before the task that destroys the |device|. | |
198 device_task_runner_->PostTask( | |
199 FROM_HERE, | |
200 base::Bind(&media::VideoCaptureDevice::GetPhotoCapabilities, | |
201 base::Unretained(device_.get()), base::Passed(&callback))); | |
202 } | |
203 | |
204 void InProcessBuildableVideoCaptureDevice::SetPhotoOptions( | |
205 media::mojom::PhotoSettingsPtr settings, | |
206 media::VideoCaptureDevice::SetPhotoOptionsCallback callback) { | |
207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
208 // Unretained() is safe to use here because |device| would be null if it | |
209 // was scheduled for shutdown and destruction, and because this task is | |
210 // guaranteed to run before the task that destroys the |device|. | |
211 device_task_runner_->PostTask( | |
212 FROM_HERE, base::Bind(&media::VideoCaptureDevice::SetPhotoOptions, | |
213 base::Unretained(device_.get()), | |
214 base::Passed(&settings), base::Passed(&callback))); | |
215 } | |
216 | |
217 void InProcessBuildableVideoCaptureDevice::TakePhoto( | |
218 media::VideoCaptureDevice::TakePhotoCallback callback) { | |
219 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
220 // Unretained() is safe to use here because |device| would be null if it | |
221 // was scheduled for shutdown and destruction, and because this task is | |
222 // guaranteed to run before the task that destroys the |device|. | |
223 device_task_runner_->PostTask( | |
224 FROM_HERE, | |
225 base::Bind(&media::VideoCaptureDevice::TakePhoto, | |
226 base::Unretained(device_.get()), base::Passed(&callback))); | |
227 } | |
228 | |
229 void InProcessBuildableVideoCaptureDevice::MaybeSuspendDevice() { | |
230 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
231 // Unretained() is safe to use here because |device| would be null if it | |
232 // was scheduled for shutdown and destruction, and because this task is | |
233 // guaranteed to run before the task that destroys the |device|. | |
234 device_task_runner_->PostTask( | |
235 FROM_HERE, base::Bind(&media::VideoCaptureDevice::MaybeSuspend, | |
236 base::Unretained(device_.get()))); | |
237 } | |
238 | |
239 void InProcessBuildableVideoCaptureDevice::ResumeDevice() { | |
240 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
241 // Unretained() is safe to use here because |device| would be null if it | |
242 // was scheduled for shutdown and destruction, and because this task is | |
243 // guaranteed to run before the task that destroys the |device|. | |
244 device_task_runner_->PostTask(FROM_HERE, | |
245 base::Bind(&media::VideoCaptureDevice::Resume, | |
246 base::Unretained(device_.get()))); | |
247 } | |
248 | |
249 void InProcessBuildableVideoCaptureDevice::RequestRefreshFrame() { | |
250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
251 // Unretained() is safe to use here because |device| would be null if it | |
252 // was scheduled for shutdown and destruction, and because this task is | |
253 // guaranteed to run before the task that destroys the |device|. | |
254 device_task_runner_->PostTask( | |
255 FROM_HERE, base::Bind(&media::VideoCaptureDevice::RequestRefreshFrame, | |
256 base::Unretained(device_.get()))); | |
257 } | |
258 | |
259 void InProcessBuildableVideoCaptureDevice::SetDesktopCaptureWindowIdAsync( | |
260 gfx::NativeViewId window_id, | |
261 base::OnceClosure done_cb) { | |
262 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
263 // Post |device_| to the the |device_task_runner_|. This is safe since the | |
264 // device is destroyed on the |device_task_runner_| and |done_cb| guarantees | |
265 // that |this| stays alive. | |
266 device_task_runner_->PostTask( | |
267 FROM_HERE, base::Bind(&InProcessBuildableVideoCaptureDevice:: | |
268 SetDesktopCaptureWindowIdOnDeviceThread, | |
269 base::Unretained(this), device_.get(), window_id, | |
270 base::Passed(&done_cb))); | |
271 } | |
272 | |
273 std::unique_ptr<media::VideoCaptureDeviceClient> | |
274 InProcessBuildableVideoCaptureDevice::CreateDeviceClient( | |
275 int buffer_pool_max_buffer_count, | |
276 base::WeakPtr<media::VideoFrameReceiver> receiver) { | |
277 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
278 | |
279 return base::MakeUnique<media::VideoCaptureDeviceClient>( | |
280 base::MakeUnique<media::VideoFrameReceiverOnTaskRunner>( | |
281 receiver, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | |
282 new media::VideoCaptureBufferPoolImpl( | |
283 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), | |
284 buffer_pool_max_buffer_count), | |
285 base::Bind(&CreateGpuJpegDecoder, | |
286 base::Bind(&media::VideoFrameReceiver::OnFrameReadyInBuffer, | |
287 receiver))); | |
288 } | |
289 | |
290 void InProcessBuildableVideoCaptureDevice::OnDeviceStarted( | |
291 VideoCaptureController* controller, | |
292 Callbacks* callbacks, | |
293 base::OnceClosure done_cb, | |
294 std::unique_ptr<media::VideoCaptureDevice> device) { | |
295 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
296 switch (state_) { | |
297 case State::DEVICE_START_IN_PROGRESS: | |
298 if (!device) { | |
299 state_ = State::NO_DEVICE; | |
300 callbacks->OnDeviceStartFailed(controller); | |
301 base::ResetAndReturn(&done_cb).Run(); | |
302 return; | |
303 } | |
304 // Passing raw pointer |device.get()| to the controller is safe, | |
305 // because we take ownership of |device| and we call | |
306 // controller->SetConsumerFeedbackObserver(nullptr) before releasing | |
307 // |device|. | |
308 controller->SetConsumerFeedbackObserver( | |
309 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | |
310 device.get(), device_task_runner_)); | |
311 device_ = std::move(device); | |
312 state_ = State::DEVICE_STARTED; | |
313 callbacks->OnDeviceStarted(controller); | |
314 base::ResetAndReturn(&done_cb).Run(); | |
315 return; | |
316 case State::DEVICE_START_ABORTING: | |
317 if (device) { | |
318 device_ = std::move(device); | |
319 state_ = State::DEVICE_STARTED; | |
320 // We do not move our |done_cb| to this invocation, because | |
321 // we still need it to stay alive for the remainder of this method | |
322 // execution. Our implementation of ReleaseDeviceAsync() does not | |
323 // actually need the context while releasing the device. | |
324 ReleaseDeviceAsync(controller, base::Bind([]() {})); | |
325 } | |
326 state_ = State::NO_DEVICE; | |
327 callbacks->OnDeviceStartAborted(); | |
328 base::ResetAndReturn(&done_cb).Run(); | |
329 return; | |
330 case State::NO_DEVICE: | |
331 case State::DEVICE_STARTED: | |
332 NOTREACHED(); | |
333 return; | |
334 } | |
335 } | |
336 | |
337 void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( | |
338 const std::string& device_id, | |
339 const media::VideoCaptureParams& params, | |
340 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, | |
341 ReceiveDeviceCallback result_callback) { | |
342 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); | |
343 DCHECK(device_task_runner_->BelongsToCurrentThread()); | |
344 | |
345 std::unique_ptr<media::VideoCaptureDevice> video_capture_device = | |
346 video_capture_system_->CreateDevice(device_id); | |
347 | |
348 if (!video_capture_device) { | |
349 result_callback.Run(nullptr); | |
350 return; | |
351 } | |
352 | |
353 video_capture_device->AllocateAndStart(params, std::move(device_client)); | |
354 result_callback.Run(std::move(video_capture_device)); | |
355 } | |
356 | |
357 void InProcessBuildableVideoCaptureDevice::DoStartTabCaptureOnDeviceThread( | |
358 const std::string& id, | |
359 const media::VideoCaptureParams& params, | |
360 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, | |
361 ReceiveDeviceCallback result_callback) { | |
362 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); | |
363 DCHECK(device_task_runner_->BelongsToCurrentThread()); | |
364 | |
365 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; | |
366 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN) | |
367 video_capture_device = WebContentsVideoCaptureDevice::Create(id); | |
368 #endif | |
369 | |
370 if (!video_capture_device) { | |
371 result_callback.Run(nullptr); | |
372 return; | |
373 } | |
374 | |
375 video_capture_device->AllocateAndStart(params, std::move(device_client)); | |
376 result_callback.Run(std::move(video_capture_device)); | |
377 } | |
378 | |
379 void InProcessBuildableVideoCaptureDevice::DoStartDesktopCaptureOnDeviceThread( | |
380 const std::string& id, | |
381 const media::VideoCaptureParams& params, | |
382 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, | |
383 ReceiveDeviceCallback result_callback) { | |
384 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); | |
385 DCHECK(device_task_runner_->BelongsToCurrentThread()); | |
386 | |
387 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; | |
388 #if defined(ENABLE_SCREEN_CAPTURE) | |
389 DesktopMediaID desktop_id = DesktopMediaID::Parse(id); | |
390 if (desktop_id.is_null()) { | |
391 DLOG(ERROR) << "Desktop media ID is null"; | |
392 result_callback.Run(nullptr); | |
393 return; | |
394 } | |
395 | |
396 if (desktop_id.type == DesktopMediaID::TYPE_WEB_CONTENTS) { | |
397 #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN) | |
398 video_capture_device = WebContentsVideoCaptureDevice::Create(id); | |
399 IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED); | |
400 if (desktop_id.audio_share) | |
401 IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED_WITH_AUDIO); | |
402 else | |
403 IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED_WITHOUT_AUDIO); | |
404 #endif | |
405 } else { | |
406 #if defined(OS_ANDROID) | |
407 video_capture_device = base::MakeUnique<ScreenCaptureDeviceAndroid>(); | |
408 #else | |
409 #if defined(USE_AURA) | |
410 video_capture_device = DesktopCaptureDeviceAura::Create(desktop_id); | |
411 #endif // defined(USE_AURA) | |
412 #if BUILDFLAG(ENABLE_WEBRTC) | |
413 if (!video_capture_device) | |
414 video_capture_device = DesktopCaptureDevice::Create(desktop_id); | |
415 #endif // BUILDFLAG(ENABLE_WEBRTC) | |
416 #endif // defined (OS_ANDROID) | |
417 } | |
418 #endif // defined(ENABLE_SCREEN_CAPTURE) | |
419 | |
420 if (!video_capture_device) { | |
421 result_callback.Run(nullptr); | |
422 return; | |
423 } | |
424 | |
425 video_capture_device->AllocateAndStart(params, std::move(device_client)); | |
426 result_callback.Run(std::move(video_capture_device)); | |
427 } | |
428 | |
429 void InProcessBuildableVideoCaptureDevice:: | |
430 SetDesktopCaptureWindowIdOnDeviceThread(media::VideoCaptureDevice* device, | |
431 gfx::NativeViewId window_id, | |
432 base::OnceClosure done_cb) { | |
433 DCHECK(device_task_runner_->BelongsToCurrentThread()); | |
434 #if defined(ENABLE_SCREEN_CAPTURE) && BUILDFLAG(ENABLE_WEBRTC) && \ | |
435 !defined(OS_ANDROID) | |
436 DesktopCaptureDevice* desktop_device = | |
437 static_cast<DesktopCaptureDevice*>(device); | |
438 desktop_device->SetNotificationWindowId(window_id); | |
439 VLOG(2) << "Screen capture notification window passed on device thread."; | |
440 #endif | |
441 base::ResetAndReturn(&done_cb).Run(); | |
442 } | |
443 | |
444 } // namespace content | |
OLD | NEW |