Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(962)

Unified Diff: content/browser/renderer_host/media/service_video_capture_device_launcher.cc

Issue 2857303002: [Mojo Video Capture] Implement a VideoCaptureProvider using the Mojo service (part 2) (Closed)
Patch Set: Added back update to |state_| which was accidentally dropped in PatchSet 3 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b74cf52865a5a817f84c712489d31c1dc6316391 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,65 @@
#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 {
+namespace {
+
+void ConcludeLaunchDeviceWithSuccess(
+ bool abort_requested,
+ const media::VideoCaptureParams& params,
+ video_capture::mojom::DevicePtr device,
+ base::WeakPtr<media::VideoFrameReceiver> receiver,
+ VideoCaptureDeviceLauncher::Callbacks* callbacks,
+ base::OnceClosure done_cb) {
+ if (abort_requested) {
+ 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 ConcludeLaunchDeviceWithFailure(
+ bool abort_requested,
+ VideoCaptureDeviceLauncher::Callbacks* callbacks,
+ base::OnceClosure done_cb) {
+ if (abort_requested)
+ callbacks->OnDeviceLaunchAborted();
+ else
+ callbacks->OnDeviceLaunchFailed();
+ base::ResetAndReturn(&done_cb).Run();
+}
+
+} // anonymous namespace
+
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(sequence_checker_.CalledOnValidSequence());
+ DCHECK(state_ == State::READY_TO_LAUNCH);
+}
void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync(
const std::string& device_id,
@@ -19,11 +71,63 @@ void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync(
base::WeakPtr<media::VideoFrameReceiver> receiver,
Callbacks* callbacks,
base::OnceClosure done_cb) {
- NOTIMPLEMENTED();
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+ 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_|.
+ ConcludeLaunchDeviceWithFailure(false, 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(sequence_checker_.CalledOnValidSequence());
+ 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(sequence_checker_.CalledOnValidSequence());
+ const bool abort_requested = (state_ == State::DEVICE_START_ABORTING);
+ state_ = State::READY_TO_LAUNCH;
+ switch (result_code) {
+ case video_capture::mojom::DeviceAccessResultCode::SUCCESS:
+ ConcludeLaunchDeviceWithSuccess(abort_requested, 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:
+ ConcludeLaunchDeviceWithFailure(abort_requested, callbacks,
+ std::move(done_cb));
+ return;
+ }
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698