Index: content/browser/renderer_host/media/mojo_service_video_capture_provider.cc |
diff --git a/content/browser/renderer_host/media/mojo_service_video_capture_provider.cc b/content/browser/renderer_host/media/mojo_service_video_capture_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa0f55175c8a102c41050e4e0d33c8f25349085e |
--- /dev/null |
+++ b/content/browser/renderer_host/media/mojo_service_video_capture_provider.cc |
@@ -0,0 +1,87 @@ |
+// 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_provider.h" |
+ |
+#include "content/browser/renderer_host/media/mojo_service_video_capture_device_launcher.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/common/service_manager_connection.h" |
+#include "services/service_manager/public/cpp/connector.h" |
+#include "services/video_capture/public/interfaces/constants.mojom.h" |
+ |
+namespace { |
+ |
+void BindToBrowserConnector(service_manager::mojom::ConnectorRequest request) { |
+ if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&BindToBrowserConnector, base::Passed(&request))); |
+ return; |
+ } |
+ |
+ content::ServiceManagerConnection::GetForProcess() |
+ ->GetConnector() |
+ ->BindConnectorRequest(std::move(request)); |
+} |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+MojoServiceVideoCaptureProvider::MojoServiceVideoCaptureProvider() = default; |
+ |
+MojoServiceVideoCaptureProvider::~MojoServiceVideoCaptureProvider() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+} |
+ |
+void MojoServiceVideoCaptureProvider::Uninitialize() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ device_factory_.reset(); |
+ device_factory_provider_.reset(); |
+} |
+ |
+void MojoServiceVideoCaptureProvider::GetDeviceInfosAsync( |
+ const base::Callback<void( |
+ const std::vector<media::VideoCaptureDeviceInfo>&)>& result_callback) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ LazyConnectToService(); |
+ device_factory_->GetDeviceInfos(result_callback); |
+} |
+ |
+std::unique_ptr<VideoCaptureDeviceLauncher> |
+MojoServiceVideoCaptureProvider::CreateDeviceLauncher() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ LazyConnectToService(); |
+ return base::MakeUnique<MojoServiceVideoCaptureDeviceLauncher>( |
+ &device_factory_); |
+} |
+ |
+void MojoServiceVideoCaptureProvider::LazyConnectToService() { |
+ if (device_factory_provider_.is_bound()) |
+ return; |
+ |
+ service_manager::mojom::ConnectorRequest connector_request; |
+ std::unique_ptr<service_manager::Connector> connector = |
+ service_manager::Connector::Create(&connector_request); |
+ BindToBrowserConnector(std::move(connector_request)); |
+ |
+ connector->BindInterface(video_capture::mojom::kServiceName, |
+ &device_factory_provider_); |
+ device_factory_provider_->ConnectToDeviceFactory( |
+ mojo::MakeRequest(&device_factory_)); |
+ // Unretained |this| is safe, because |this| owns |device_factory_|. |
+ device_factory_.set_connection_error_handler(base::Bind( |
+ &MojoServiceVideoCaptureProvider::OnLostConnectionToDeviceFactory, |
+ base::Unretained(this))); |
+} |
+ |
+void MojoServiceVideoCaptureProvider::OnLostConnectionToDeviceFactory() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ // This may indicate that the video capture service has |
+ // crashed. Uninitialize here, so that a new connection will be established |
+ // clients try to reconnect. |
+ Uninitialize(); |
+} |
+ |
+} // namespace content |