OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/media/video_capture_controller.h" | 5 #include "content/browser/renderer_host/media/video_capture_controller.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 160 |
161 mojo::ScopedSharedBufferHandle | 161 mojo::ScopedSharedBufferHandle |
162 VideoCaptureController::BufferContext::CloneHandle() { | 162 VideoCaptureController::BufferContext::CloneHandle() { |
163 return buffer_handle_->Clone(); | 163 return buffer_handle_->Clone(); |
164 } | 164 } |
165 | 165 |
166 VideoCaptureController::VideoCaptureController( | 166 VideoCaptureController::VideoCaptureController( |
167 const std::string& device_id, | 167 const std::string& device_id, |
168 MediaStreamType stream_type, | 168 MediaStreamType stream_type, |
169 const media::VideoCaptureParams& params, | 169 const media::VideoCaptureParams& params, |
170 std::unique_ptr<BuildableVideoCaptureDevice> buildable_device) | 170 std::unique_ptr<VideoCaptureDeviceLauncher> device_launcher) |
171 : serial_id_(g_device_start_id++), | 171 : serial_id_(g_device_start_id++), |
172 device_id_(device_id), | 172 device_id_(device_id), |
173 stream_type_(stream_type), | 173 stream_type_(stream_type), |
174 parameters_(params), | 174 parameters_(params), |
175 buildable_device_(std::move(buildable_device)), | 175 device_launcher_(std::move(device_launcher)), |
176 consumer_feedback_observer_(nullptr), | 176 device_launch_observer_(nullptr), |
177 state_(VIDEO_CAPTURE_STATE_STARTING), | 177 state_(VIDEO_CAPTURE_STATE_STARTING), |
178 has_received_frames_(false), | 178 has_received_frames_(false), |
179 weak_ptr_factory_(this) { | 179 weak_ptr_factory_(this) { |
180 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
181 } | 181 } |
182 | 182 |
183 VideoCaptureController::~VideoCaptureController() = default; | 183 VideoCaptureController::~VideoCaptureController() = default; |
184 | 184 |
185 base::WeakPtr<VideoCaptureController> | 185 base::WeakPtr<VideoCaptureController> |
186 VideoCaptureController::GetWeakPtrForIOThread() { | 186 VideoCaptureController::GetWeakPtrForIOThread() { |
187 return weak_ptr_factory_.GetWeakPtr(); | 187 return weak_ptr_factory_.GetWeakPtr(); |
188 } | 188 } |
189 | 189 |
190 void VideoCaptureController::SetConsumerFeedbackObserver( | |
191 std::unique_ptr<media::VideoFrameConsumerFeedbackObserver> | |
192 consumer_feedback_observer) { | |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
194 consumer_feedback_observer_ = std::move(consumer_feedback_observer); | |
195 // Update existing BufferContext entries. | |
196 for (auto& entry : buffer_contexts_) | |
197 entry.set_consumer_feedback_observer(consumer_feedback_observer_.get()); | |
198 } | |
199 | |
200 void VideoCaptureController::AddClient( | 190 void VideoCaptureController::AddClient( |
201 VideoCaptureControllerID id, | 191 VideoCaptureControllerID id, |
202 VideoCaptureControllerEventHandler* event_handler, | 192 VideoCaptureControllerEventHandler* event_handler, |
203 media::VideoCaptureSessionId session_id, | 193 media::VideoCaptureSessionId session_id, |
204 const media::VideoCaptureParams& params) { | 194 const media::VideoCaptureParams& params) { |
205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
206 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id | 196 DVLOG(1) << "VideoCaptureController::AddClient() -- id=" << id |
207 << ", session_id=" << session_id << ", params.requested_format=" | 197 << ", session_id=" << session_id << ", params.requested_format=" |
208 << media::VideoCaptureFormat::ToString(params.requested_format); | 198 << media::VideoCaptureFormat::ToString(params.requested_format); |
209 | 199 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 } | 368 } |
379 | 369 |
380 void VideoCaptureController::OnNewBufferHandle( | 370 void VideoCaptureController::OnNewBufferHandle( |
381 int buffer_id, | 371 int buffer_id, |
382 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer::HandleProvider> | 372 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer::HandleProvider> |
383 handle_provider) { | 373 handle_provider) { |
384 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 374 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
385 DCHECK(FindUnretiredBufferContextFromBufferId(buffer_id) == | 375 DCHECK(FindUnretiredBufferContextFromBufferId(buffer_id) == |
386 buffer_contexts_.end()); | 376 buffer_contexts_.end()); |
387 buffer_contexts_.emplace_back( | 377 buffer_contexts_.emplace_back( |
388 next_buffer_context_id_++, buffer_id, consumer_feedback_observer_.get(), | 378 next_buffer_context_id_++, buffer_id, launched_device_.get(), |
389 handle_provider->GetHandleForInterProcessTransit()); | 379 handle_provider->GetHandleForInterProcessTransit()); |
390 } | 380 } |
391 | 381 |
392 void VideoCaptureController::OnFrameReadyInBuffer( | 382 void VideoCaptureController::OnFrameReadyInBuffer( |
393 int buffer_id, | 383 int buffer_id, |
394 int frame_feedback_id, | 384 int frame_feedback_id, |
395 std::unique_ptr< | 385 std::unique_ptr< |
396 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> | 386 media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission> |
397 buffer_read_permission, | 387 buffer_read_permission, |
398 media::mojom::VideoFrameInfoPtr frame_info) { | 388 media::mojom::VideoFrameInfoPtr frame_info) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 void VideoCaptureController::OnStarted() { | 478 void VideoCaptureController::OnStarted() { |
489 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 479 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
490 state_ = VIDEO_CAPTURE_STATE_STARTED; | 480 state_ = VIDEO_CAPTURE_STATE_STARTED; |
491 PerformForClientsWithOpenSession(base::Bind(&CallOnStarted)); | 481 PerformForClientsWithOpenSession(base::Bind(&CallOnStarted)); |
492 } | 482 } |
493 | 483 |
494 void VideoCaptureController::OnStartedUsingGpuDecode() { | 484 void VideoCaptureController::OnStartedUsingGpuDecode() { |
495 PerformForClientsWithOpenSession(base::Bind(&CallOnStartedUsingGpuDecode)); | 485 PerformForClientsWithOpenSession(base::Bind(&CallOnStartedUsingGpuDecode)); |
496 } | 486 } |
497 | 487 |
| 488 void VideoCaptureController::OnDeviceLaunched( |
| 489 std::unique_ptr<LaunchedVideoCaptureDevice> device) { |
| 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 491 launched_device_ = std::move(device); |
| 492 for (auto& entry : buffer_contexts_) |
| 493 entry.set_consumer_feedback_observer(launched_device_.get()); |
| 494 if (device_launch_observer_) { |
| 495 device_launch_observer_->OnDeviceLaunched(this); |
| 496 device_launch_observer_ = nullptr; |
| 497 } |
| 498 } |
| 499 |
| 500 void VideoCaptureController::OnDeviceLaunchFailed() { |
| 501 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 502 if (device_launch_observer_) { |
| 503 device_launch_observer_->OnDeviceLaunchFailed(this); |
| 504 device_launch_observer_ = nullptr; |
| 505 } |
| 506 } |
| 507 |
| 508 void VideoCaptureController::OnDeviceLaunchAborted() { |
| 509 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 510 if (device_launch_observer_) { |
| 511 device_launch_observer_->OnDeviceLaunchAborted(); |
| 512 device_launch_observer_ = nullptr; |
| 513 } |
| 514 } |
| 515 |
498 void VideoCaptureController::CreateAndStartDeviceAsync( | 516 void VideoCaptureController::CreateAndStartDeviceAsync( |
499 const media::VideoCaptureParams& params, | 517 const media::VideoCaptureParams& params, |
500 BuildableVideoCaptureDevice::Callbacks* callbacks, | 518 VideoCaptureDeviceLaunchObserver* observer, |
501 base::OnceClosure done_cb) { | 519 base::OnceClosure done_cb) { |
502 buildable_device_->CreateAndStartDeviceAsync(this, params, callbacks, | 520 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
503 std::move(done_cb)); | 521 device_launch_observer_ = observer; |
| 522 device_launcher_->LaunchDeviceAsync(device_id_, stream_type_, params, |
| 523 GetWeakPtrForIOThread(), this, |
| 524 std::move(done_cb)); |
504 } | 525 } |
505 | 526 |
506 void VideoCaptureController::ReleaseDeviceAsync(base::OnceClosure done_cb) { | 527 void VideoCaptureController::ReleaseDeviceAsync(base::OnceClosure done_cb) { |
507 buildable_device_->ReleaseDeviceAsync(this, std::move(done_cb)); | 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 529 if (!launched_device_) { |
| 530 device_launcher_->AbortLaunch(); |
| 531 return; |
| 532 } |
| 533 launched_device_.reset(); |
508 } | 534 } |
509 | 535 |
510 bool VideoCaptureController::IsDeviceAlive() const { | 536 bool VideoCaptureController::IsDeviceAlive() const { |
511 return buildable_device_->IsDeviceAlive(); | 537 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 538 return launched_device_ != nullptr; |
512 } | 539 } |
513 | 540 |
514 void VideoCaptureController::GetPhotoCapabilities( | 541 void VideoCaptureController::GetPhotoCapabilities( |
515 media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) const { | 542 media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) const { |
516 buildable_device_->GetPhotoCapabilities(std::move(callback)); | 543 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 544 DCHECK(launched_device_); |
| 545 launched_device_->GetPhotoCapabilities(std::move(callback)); |
517 } | 546 } |
518 | 547 |
519 void VideoCaptureController::SetPhotoOptions( | 548 void VideoCaptureController::SetPhotoOptions( |
520 media::mojom::PhotoSettingsPtr settings, | 549 media::mojom::PhotoSettingsPtr settings, |
521 media::VideoCaptureDevice::SetPhotoOptionsCallback callback) { | 550 media::VideoCaptureDevice::SetPhotoOptionsCallback callback) { |
522 buildable_device_->SetPhotoOptions(std::move(settings), std::move(callback)); | 551 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 552 DCHECK(launched_device_); |
| 553 launched_device_->SetPhotoOptions(std::move(settings), std::move(callback)); |
523 } | 554 } |
524 | 555 |
525 void VideoCaptureController::TakePhoto( | 556 void VideoCaptureController::TakePhoto( |
526 media::VideoCaptureDevice::TakePhotoCallback callback) { | 557 media::VideoCaptureDevice::TakePhotoCallback callback) { |
527 buildable_device_->TakePhoto(std::move(callback)); | 558 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 559 DCHECK(launched_device_); |
| 560 launched_device_->TakePhoto(std::move(callback)); |
528 } | 561 } |
529 | 562 |
530 void VideoCaptureController::MaybeSuspend() { | 563 void VideoCaptureController::MaybeSuspend() { |
531 buildable_device_->MaybeSuspendDevice(); | 564 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 565 DCHECK(launched_device_); |
| 566 launched_device_->MaybeSuspendDevice(); |
532 } | 567 } |
533 | 568 |
534 void VideoCaptureController::Resume() { | 569 void VideoCaptureController::Resume() { |
535 buildable_device_->ResumeDevice(); | 570 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 571 DCHECK(launched_device_); |
| 572 launched_device_->ResumeDevice(); |
536 } | 573 } |
537 | 574 |
538 void VideoCaptureController::RequestRefreshFrame() { | 575 void VideoCaptureController::RequestRefreshFrame() { |
539 buildable_device_->RequestRefreshFrame(); | 576 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 577 DCHECK(launched_device_); |
| 578 launched_device_->RequestRefreshFrame(); |
540 } | 579 } |
541 | 580 |
542 void VideoCaptureController::SetDesktopCaptureWindowIdAsync( | 581 void VideoCaptureController::SetDesktopCaptureWindowIdAsync( |
543 gfx::NativeViewId window_id, | 582 gfx::NativeViewId window_id, |
544 base::OnceClosure done_cb) { | 583 base::OnceClosure done_cb) { |
545 buildable_device_->SetDesktopCaptureWindowIdAsync(window_id, | 584 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
546 std::move(done_cb)); | 585 DCHECK(launched_device_); |
| 586 launched_device_->SetDesktopCaptureWindowIdAsync(window_id, |
| 587 std::move(done_cb)); |
547 } | 588 } |
548 | 589 |
549 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( | 590 VideoCaptureController::ControllerClient* VideoCaptureController::FindClient( |
550 VideoCaptureControllerID id, | 591 VideoCaptureControllerID id, |
551 VideoCaptureControllerEventHandler* handler, | 592 VideoCaptureControllerEventHandler* handler, |
552 const ControllerClients& clients) { | 593 const ControllerClients& clients) { |
553 for (const auto& client : clients) { | 594 for (const auto& client : clients) { |
554 if (client->controller_id == id && client->event_handler == handler) | 595 if (client->controller_id == id && client->event_handler == handler) |
555 return client.get(); | 596 return client.get(); |
556 } | 597 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 EventHandlerAction action) { | 663 EventHandlerAction action) { |
623 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 664 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
624 for (const auto& client : controller_clients_) { | 665 for (const auto& client : controller_clients_) { |
625 if (client->session_closed) | 666 if (client->session_closed) |
626 continue; | 667 continue; |
627 action.Run(client->event_handler, client->controller_id); | 668 action.Run(client->event_handler, client->controller_id); |
628 } | 669 } |
629 } | 670 } |
630 | 671 |
631 } // namespace content | 672 } // namespace content |
OLD | NEW |