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/service_video_capture_device_launc
her.h" | 5 #include "content/browser/renderer_host/media/service_video_capture_device_launc
her.h" |
6 | 6 |
| 7 #include "content/browser/renderer_host/media/service_launched_video_capture_dev
ice.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "media/capture/video/video_frame_receiver_on_task_runner.h" |
| 10 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 11 #include "services/video_capture/public/cpp/receiver_media_to_mojo_adapter.h" |
| 12 |
7 namespace content { | 13 namespace content { |
8 | 14 |
| 15 namespace { |
| 16 |
| 17 void ConcludeLaunchDeviceWithSuccess( |
| 18 bool abort_requested, |
| 19 const media::VideoCaptureParams& params, |
| 20 video_capture::mojom::DevicePtr device, |
| 21 base::WeakPtr<media::VideoFrameReceiver> receiver, |
| 22 VideoCaptureDeviceLauncher::Callbacks* callbacks, |
| 23 base::OnceClosure done_cb) { |
| 24 if (abort_requested) { |
| 25 device.reset(); |
| 26 callbacks->OnDeviceLaunchAborted(); |
| 27 base::ResetAndReturn(&done_cb).Run(); |
| 28 return; |
| 29 } |
| 30 |
| 31 auto receiver_adapter = |
| 32 base::MakeUnique<video_capture::ReceiverMediaToMojoAdapter>( |
| 33 base::MakeUnique<media::VideoFrameReceiverOnTaskRunner>( |
| 34 std::move(receiver), |
| 35 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))); |
| 36 video_capture::mojom::ReceiverPtr receiver_proxy; |
| 37 mojo::MakeStrongBinding<video_capture::mojom::Receiver>( |
| 38 std::move(receiver_adapter), mojo::MakeRequest(&receiver_proxy)); |
| 39 device->Start(params, std::move(receiver_proxy)); |
| 40 callbacks->OnDeviceLaunched( |
| 41 base::MakeUnique<ServiceLaunchedVideoCaptureDevice>(std::move(device))); |
| 42 base::ResetAndReturn(&done_cb).Run(); |
| 43 } |
| 44 |
| 45 void ConcludeLaunchDeviceWithFailure( |
| 46 bool abort_requested, |
| 47 VideoCaptureDeviceLauncher::Callbacks* callbacks, |
| 48 base::OnceClosure done_cb) { |
| 49 if (abort_requested) |
| 50 callbacks->OnDeviceLaunchAborted(); |
| 51 else |
| 52 callbacks->OnDeviceLaunchFailed(); |
| 53 base::ResetAndReturn(&done_cb).Run(); |
| 54 } |
| 55 |
| 56 } // anonymous namespace |
| 57 |
9 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher( | 58 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher( |
10 video_capture::mojom::DeviceFactoryPtr* device_factory) | 59 video_capture::mojom::DeviceFactoryPtr* device_factory) |
11 : device_factory_(device_factory) {} | 60 : device_factory_(device_factory), state_(State::READY_TO_LAUNCH) {} |
12 | 61 |
13 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() {} | 62 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() { |
| 63 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 64 DCHECK(state_ == State::READY_TO_LAUNCH); |
| 65 } |
14 | 66 |
15 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( | 67 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( |
16 const std::string& device_id, | 68 const std::string& device_id, |
17 MediaStreamType stream_type, | 69 MediaStreamType stream_type, |
18 const media::VideoCaptureParams& params, | 70 const media::VideoCaptureParams& params, |
19 base::WeakPtr<media::VideoFrameReceiver> receiver, | 71 base::WeakPtr<media::VideoFrameReceiver> receiver, |
20 Callbacks* callbacks, | 72 Callbacks* callbacks, |
21 base::OnceClosure done_cb) { | 73 base::OnceClosure done_cb) { |
22 NOTIMPLEMENTED(); | 74 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 75 DCHECK(state_ == State::READY_TO_LAUNCH); |
| 76 |
| 77 if (stream_type != content::MEDIA_DEVICE_VIDEO_CAPTURE) { |
| 78 // This launcher only supports MEDIA_DEVICE_VIDEO_CAPTURE. |
| 79 NOTREACHED(); |
| 80 return; |
| 81 } |
| 82 |
| 83 if (!device_factory_->is_bound()) { |
| 84 // This can happen when the ServiceVideoCaptureProvider owning |
| 85 // |device_factory_| loses connection to the service process and resets |
| 86 // |device_factory_|. |
| 87 ConcludeLaunchDeviceWithFailure(false, callbacks, std::move(done_cb)); |
| 88 return; |
| 89 } |
| 90 video_capture::mojom::DevicePtr device; |
| 91 (*device_factory_) |
| 92 ->CreateDevice( |
| 93 device_id, mojo::MakeRequest(&device), |
| 94 base::Bind( |
| 95 // Use of Unretained |this| is safe, because |done_cb| guarantees |
| 96 // that |this| stays alive. |
| 97 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback, |
| 98 base::Unretained(this), params, base::Passed(&device), |
| 99 std::move(receiver), callbacks, base::Passed(&done_cb))); |
| 100 state_ = State::DEVICE_START_IN_PROGRESS; |
23 } | 101 } |
24 | 102 |
25 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { | 103 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { |
26 NOTIMPLEMENTED(); | 104 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 105 if (state_ == State::DEVICE_START_IN_PROGRESS) |
| 106 state_ = State::DEVICE_START_ABORTING; |
| 107 } |
| 108 |
| 109 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback( |
| 110 const media::VideoCaptureParams& params, |
| 111 video_capture::mojom::DevicePtr device, |
| 112 base::WeakPtr<media::VideoFrameReceiver> receiver, |
| 113 Callbacks* callbacks, |
| 114 base::OnceClosure done_cb, |
| 115 video_capture::mojom::DeviceAccessResultCode result_code) { |
| 116 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 117 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING); |
| 118 state_ = State::READY_TO_LAUNCH; |
| 119 switch (result_code) { |
| 120 case video_capture::mojom::DeviceAccessResultCode::SUCCESS: |
| 121 ConcludeLaunchDeviceWithSuccess(abort_requested, params, |
| 122 std::move(device), std::move(receiver), |
| 123 callbacks, std::move(done_cb)); |
| 124 return; |
| 125 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND: |
| 126 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED: |
| 127 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks, |
| 128 std::move(done_cb)); |
| 129 return; |
| 130 } |
27 } | 131 } |
28 | 132 |
29 } // namespace content | 133 } // namespace content |
OLD | NEW |