Index: content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.cc |
diff --git a/content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.cc b/content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d2a60dbd281b5bd09c86b2ca212acbb3615d6786 |
--- /dev/null |
+++ b/content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.cc |
@@ -0,0 +1,127 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.h" |
+ |
+#include "content/browser/renderer_host/media/mojo_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 { |
+ |
+MojoServiceVideoCaptureDeviceLauncher::MojoServiceVideoCaptureDeviceLauncher( |
+ video_capture::mojom::DeviceFactoryPtr* device_factory) |
+ : device_factory_(device_factory), state_(State::READY_TO_LAUNCH) {} |
+ |
+MojoServiceVideoCaptureDeviceLauncher:: |
+ ~MojoServiceVideoCaptureDeviceLauncher() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(state_ == State::READY_TO_LAUNCH); |
+} |
+ |
+void MojoServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( |
+ const std::string& device_id, |
+ MediaStreamType stream_type, |
+ const media::VideoCaptureParams& params, |
+ base::WeakPtr<media::VideoFrameReceiver> receiver, |
+ Callbacks* callbacks, |
+ base::OnceClosure done_cb) { |
+ 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 MojoServiceVideoCaptureProvider 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( |
+ &MojoServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback, |
+ base::Unretained(this), params, base::Passed(&device), |
+ std::move(receiver), callbacks, base::Passed(&done_cb))); |
+ state_ = State::DEVICE_START_IN_PROGRESS; |
+} |
+ |
+void MojoServiceVideoCaptureDeviceLauncher::AbortLaunch() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (state_ == State::DEVICE_START_IN_PROGRESS) |
+ state_ = State::DEVICE_START_ABORTING; |
+} |
+ |
+void MojoServiceVideoCaptureDeviceLauncher::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 MojoServiceVideoCaptureDeviceLauncher::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()); |
+ |
+ if (state_ == 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<MojoServiceLaunchedVideoCaptureDevice>( |
+ std::move(device))); |
+ base::ResetAndReturn(&done_cb).Run(); |
+} |
+ |
+void MojoServiceVideoCaptureDeviceLauncher::OnDeviceCreationFailed( |
+ Callbacks* callbacks, |
+ base::OnceClosure done_cb) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (state_ == State::DEVICE_START_ABORTING) |
+ callbacks->OnDeviceLaunchAborted(); |
+ else |
+ callbacks->OnDeviceLaunchFailed(); |
+ base::ResetAndReturn(&done_cb).Run(); |
+} |
+ |
+} // namespace content |