OLD | NEW |
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 Loading... |
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::VideoCaptureSystem* video_capture_system) | 87 media::VideoCaptureDeviceFactory* device_factory) |
88 : device_task_runner_(std::move(device_task_runner)), | 88 : device_task_runner_(std::move(device_task_runner)), |
89 video_capture_system_(video_capture_system) {} | 89 device_factory_(device_factory) {} |
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(state_ == State::NO_DEVICE); | 102 DCHECK(state_ == State::NO_DEVICE); |
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 | |
119 switch (controller->stream_type()) { | 112 switch (controller->stream_type()) { |
120 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 113 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))); |
121 start_capture_closure = | 135 start_capture_closure = |
122 base::Bind(&InProcessBuildableVideoCaptureDevice:: | 136 base::Bind(&InProcessBuildableVideoCaptureDevice:: |
123 DoStartDeviceCaptureOnDeviceThread, | 137 DoStartDeviceCaptureOnDeviceThread, |
124 base::Unretained(this), controller->device_id(), params, | 138 base::Unretained(this), *descriptor, params, |
125 base::Passed(std::move(device_client)), | 139 base::Passed(std::move(device_client)), |
126 std::move(after_start_capture_callback)); | 140 std::move(after_start_capture_callback)); |
127 break; | 141 break; |
128 } | 142 } |
129 case MEDIA_TAB_VIDEO_CAPTURE: | 143 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))); |
130 start_capture_closure = | 151 start_capture_closure = |
131 base::Bind(&InProcessBuildableVideoCaptureDevice:: | 152 base::Bind(&InProcessBuildableVideoCaptureDevice:: |
132 DoStartTabCaptureOnDeviceThread, | 153 DoStartTabCaptureOnDeviceThread, |
133 base::Unretained(this), controller->device_id(), params, | 154 base::Unretained(this), controller->device_id(), params, |
134 base::Passed(std::move(device_client)), | 155 base::Passed(std::move(device_client)), |
135 std::move(after_start_capture_callback)); | 156 std::move(after_start_capture_callback)); |
136 break; | 157 break; |
137 | 158 } |
138 case MEDIA_DESKTOP_VIDEO_CAPTURE: | 159 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))); |
139 start_capture_closure = | 167 start_capture_closure = |
140 base::Bind(&InProcessBuildableVideoCaptureDevice:: | 168 base::Bind(&InProcessBuildableVideoCaptureDevice:: |
141 DoStartDesktopCaptureOnDeviceThread, | 169 DoStartDesktopCaptureOnDeviceThread, |
142 base::Unretained(this), controller->device_id(), params, | 170 base::Unretained(this), controller->device_id(), params, |
143 base::Passed(std::move(device_client)), | 171 base::Passed(std::move(device_client)), |
144 std::move(after_start_capture_callback)); | 172 std::move(after_start_capture_callback)); |
145 break; | 173 break; |
146 | 174 } |
147 default: { | 175 default: { |
148 NOTIMPLEMENTED(); | 176 NOTIMPLEMENTED(); |
149 return; | 177 return; |
150 } | 178 } |
151 } | 179 } |
152 | 180 |
153 device_task_runner_->PostTask(FROM_HERE, start_capture_closure); | 181 device_task_runner_->PostTask(FROM_HERE, start_capture_closure); |
154 state_ = State::DEVICE_START_IN_PROGRESS; | 182 state_ = State::DEVICE_START_IN_PROGRESS; |
155 } | 183 } |
156 | 184 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 } | 331 } |
304 // Passing raw pointer |device.get()| to the controller is safe, | 332 // Passing raw pointer |device.get()| to the controller is safe, |
305 // because we take ownership of |device| and we call | 333 // because we take ownership of |device| and we call |
306 // controller->SetConsumerFeedbackObserver(nullptr) before releasing | 334 // controller->SetConsumerFeedbackObserver(nullptr) before releasing |
307 // |device|. | 335 // |device|. |
308 controller->SetConsumerFeedbackObserver( | 336 controller->SetConsumerFeedbackObserver( |
309 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | 337 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( |
310 device.get(), device_task_runner_)); | 338 device.get(), device_task_runner_)); |
311 device_ = std::move(device); | 339 device_ = std::move(device); |
312 state_ = State::DEVICE_STARTED; | 340 state_ = State::DEVICE_STARTED; |
313 callbacks->OnDeviceStarted(controller); | 341 callbacks->DidStartDevice(controller); |
314 base::ResetAndReturn(&done_cb).Run(); | 342 base::ResetAndReturn(&done_cb).Run(); |
315 return; | 343 return; |
316 case State::DEVICE_START_ABORTING: | 344 case State::DEVICE_START_ABORTING: |
317 if (device) { | 345 if (device) { |
318 device_ = std::move(device); | 346 device_ = std::move(device); |
319 state_ = State::DEVICE_STARTED; | 347 state_ = State::DEVICE_STARTED; |
320 // We do not move our |done_cb| to this invocation, because | 348 // 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 | 349 // we still need it to stay alive for the remainder of this method |
322 // execution. Our implementation of ReleaseDeviceAsync() does not | 350 // execution. Our implementation of ReleaseDeviceAsync() does not |
323 // actually need the context while releasing the device. | 351 // actually need the context while releasing the device. |
324 ReleaseDeviceAsync(controller, base::Bind([]() {})); | 352 ReleaseDeviceAsync(controller, base::Bind([]() {})); |
325 } | 353 } |
326 state_ = State::NO_DEVICE; | 354 state_ = State::NO_DEVICE; |
327 callbacks->OnDeviceStartAborted(); | 355 callbacks->OnDeviceStartAborted(); |
328 base::ResetAndReturn(&done_cb).Run(); | 356 base::ResetAndReturn(&done_cb).Run(); |
329 return; | 357 return; |
330 case State::NO_DEVICE: | 358 case State::NO_DEVICE: |
331 case State::DEVICE_STARTED: | 359 case State::DEVICE_STARTED: |
332 NOTREACHED(); | 360 NOTREACHED(); |
333 return; | 361 return; |
334 } | 362 } |
335 } | 363 } |
336 | 364 |
337 void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( | 365 void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( |
338 const std::string& device_id, | 366 const media::VideoCaptureDeviceDescriptor& descriptor, |
339 const media::VideoCaptureParams& params, | 367 const media::VideoCaptureParams& params, |
340 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, | 368 std::unique_ptr<media::VideoCaptureDeviceClient> device_client, |
341 ReceiveDeviceCallback result_callback) { | 369 ReceiveDeviceCallback result_callback) { |
342 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); | 370 SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StartDeviceTime"); |
343 DCHECK(device_task_runner_->BelongsToCurrentThread()); | 371 DCHECK(device_task_runner_->BelongsToCurrentThread()); |
344 | 372 |
345 std::unique_ptr<media::VideoCaptureDevice> video_capture_device = | 373 std::unique_ptr<media::VideoCaptureDevice> video_capture_device = |
346 video_capture_system_->CreateDevice(device_id); | 374 device_factory_->CreateDevice(descriptor); |
347 | 375 |
348 if (!video_capture_device) { | 376 if (!video_capture_device) { |
349 result_callback.Run(nullptr); | 377 result_callback.Run(nullptr); |
350 return; | 378 return; |
351 } | 379 } |
352 | 380 |
353 video_capture_device->AllocateAndStart(params, std::move(device_client)); | 381 video_capture_device->AllocateAndStart(params, std::move(device_client)); |
354 result_callback.Run(std::move(video_capture_device)); | 382 result_callback.Run(std::move(video_capture_device)); |
355 } | 383 } |
356 | 384 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 !defined(OS_ANDROID) | 463 !defined(OS_ANDROID) |
436 DesktopCaptureDevice* desktop_device = | 464 DesktopCaptureDevice* desktop_device = |
437 static_cast<DesktopCaptureDevice*>(device); | 465 static_cast<DesktopCaptureDevice*>(device); |
438 desktop_device->SetNotificationWindowId(window_id); | 466 desktop_device->SetNotificationWindowId(window_id); |
439 VLOG(2) << "Screen capture notification window passed on device thread."; | 467 VLOG(2) << "Screen capture notification window passed on device thread."; |
440 #endif | 468 #endif |
441 base::ResetAndReturn(&done_cb).Run(); | 469 base::ResetAndReturn(&done_cb).Run(); |
442 } | 470 } |
443 | 471 |
444 } // namespace content | 472 } // namespace content |
OLD | NEW |