Chromium Code Reviews| Index: content/browser/renderer_host/media/service_video_capture_device_launcher.cc |
| diff --git a/content/browser/renderer_host/media/service_video_capture_device_launcher.cc b/content/browser/renderer_host/media/service_video_capture_device_launcher.cc |
| index 94d4e0617c3ab107963fd72f075933bd7f451e66..15600454be4aa430fdf35918b53f86d405959ab3 100644 |
| --- a/content/browser/renderer_host/media/service_video_capture_device_launcher.cc |
| +++ b/content/browser/renderer_host/media/service_video_capture_device_launcher.cc |
| @@ -4,13 +4,22 @@ |
| #include "content/browser/renderer_host/media/service_video_capture_device_launcher.h" |
| +#include "content/browser/renderer_host/media/service_launched_video_capture_device.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "media/capture/video/video_frame_receiver_on_task_runner.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "services/video_capture/public/cpp/receiver_media_to_mojo_adapter.h" |
| + |
| namespace content { |
| ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher( |
| video_capture::mojom::DeviceFactoryPtr* device_factory) |
| - : device_factory_(device_factory) {} |
| + : device_factory_(device_factory), state_(State::READY_TO_LAUNCH) {} |
| -ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() {} |
| +ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(state_ == State::READY_TO_LAUNCH); |
| +} |
| void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( |
| const std::string& device_id, |
| @@ -19,11 +28,104 @@ void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( |
| base::WeakPtr<media::VideoFrameReceiver> receiver, |
| Callbacks* callbacks, |
| base::OnceClosure done_cb) { |
| - NOTIMPLEMENTED(); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(state_ == State::READY_TO_LAUNCH); |
| + |
| + if (stream_type != content::MEDIA_DEVICE_VIDEO_CAPTURE) { |
| + // This launcher only supports MEDIA_DEVICE_VIDEO_CAPTURE. |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + if (!device_factory_->is_bound()) { |
| + // This can happen when the ServiceVideoCaptureProvider owning |
| + // |device_factory_| loses connection to the service process and resets |
| + // |device_factory_|. |
| + OnDeviceCreationFailed(callbacks, std::move(done_cb)); |
| + return; |
| + } |
| + video_capture::mojom::DevicePtr device; |
| + (*device_factory_) |
| + ->CreateDevice( |
| + device_id, mojo::MakeRequest(&device), |
| + base::Bind( |
| + // Use of Unretained |this| is safe, because |done_cb| guarantees |
| + // that |this| stays alive. |
| + &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback, |
| + base::Unretained(this), params, base::Passed(&device), |
| + std::move(receiver), callbacks, base::Passed(&done_cb))); |
| + state_ = State::DEVICE_START_IN_PROGRESS; |
| } |
| void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { |
| - NOTIMPLEMENTED(); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (state_ == State::DEVICE_START_IN_PROGRESS) |
| + state_ = State::DEVICE_START_ABORTING; |
| +} |
| + |
| +void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback( |
| + const media::VideoCaptureParams& params, |
| + video_capture::mojom::DevicePtr device, |
| + base::WeakPtr<media::VideoFrameReceiver> receiver, |
| + Callbacks* callbacks, |
| + base::OnceClosure done_cb, |
| + video_capture::mojom::DeviceAccessResultCode result_code) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + switch (result_code) { |
| + case video_capture::mojom::DeviceAccessResultCode::SUCCESS: |
| + OnDeviceCreatedSuccessfully(params, std::move(device), |
| + std::move(receiver), callbacks, |
| + std::move(done_cb)); |
| + return; |
| + case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND: |
| + case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED: |
| + OnDeviceCreationFailed(callbacks, std::move(done_cb)); |
| + return; |
| + } |
| +} |
| + |
| +void ServiceVideoCaptureDeviceLauncher::OnDeviceCreatedSuccessfully( |
| + const media::VideoCaptureParams& params, |
| + video_capture::mojom::DevicePtr device, |
| + base::WeakPtr<media::VideoFrameReceiver> receiver, |
| + Callbacks* callbacks, |
| + base::OnceClosure done_cb) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + State state_copy = state_; |
| + state_ = State::READY_TO_LAUNCH; |
|
mcasas
2017/05/05 22:41:02
If we move this update of |state_| (resp. the one
chfremer
2017/05/08 17:01:54
What is the advantage of having these two methods
mcasas
2017/05/08 21:08:04
Reduces the exposed surface of the class (the one
chfremer
2017/05/08 21:56:31
Hmm, I now understand that the goal is to free up
chfremer
2017/05/09 18:41:33
Moved the two private methods to cc-file-scope and
|
| + |
| + if (state_copy == State::DEVICE_START_ABORTING) { |
| + device.reset(); |
| + callbacks->OnDeviceLaunchAborted(); |
| + base::ResetAndReturn(&done_cb).Run(); |
| + return; |
| + } |
| + |
| + auto receiver_adapter = |
| + base::MakeUnique<video_capture::ReceiverMediaToMojoAdapter>( |
| + base::MakeUnique<media::VideoFrameReceiverOnTaskRunner>( |
| + std::move(receiver), |
| + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))); |
| + video_capture::mojom::ReceiverPtr receiver_proxy; |
| + mojo::MakeStrongBinding<video_capture::mojom::Receiver>( |
| + std::move(receiver_adapter), mojo::MakeRequest(&receiver_proxy)); |
| + device->Start(params, std::move(receiver_proxy)); |
| + callbacks->OnDeviceLaunched( |
| + base::MakeUnique<ServiceLaunchedVideoCaptureDevice>(std::move(device))); |
| + base::ResetAndReturn(&done_cb).Run(); |
| +} |
| + |
| +void ServiceVideoCaptureDeviceLauncher::OnDeviceCreationFailed( |
| + Callbacks* callbacks, |
| + base::OnceClosure done_cb) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + State state_copy = state_; |
| + state_ = State::READY_TO_LAUNCH; |
| + if (state_copy == State::DEVICE_START_ABORTING) |
| + callbacks->OnDeviceLaunchAborted(); |
| + else |
| + callbacks->OnDeviceLaunchFailed(); |
| + base::ResetAndReturn(&done_cb).Run(); |
| } |
| } // namespace content |