blob: 811e8ba9896ff3818e8172ac5acd252fb9fcb62f [file] [log] [blame]
Luke Sorenson1261c432018-09-07 16:01:41 -04001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MEDIA_PERCEPTION_MOJO_CONNECTOR_H_
6#define MEDIA_PERCEPTION_MOJO_CONNECTOR_H_
7
Luke Sorenson13e28272018-11-09 14:43:16 -05008#include <map>
Luke Sorenson3abab662018-10-31 15:21:26 -04009#include <memory>
10#include <string>
Luke Sorenson3abab662018-10-31 15:21:26 -040011#include <vector>
12
Luke Sorenson1261c432018-09-07 16:01:41 -040013#include <base/bind.h>
14#include <base/single_thread_task_runner.h>
15#include <base/threading/thread.h>
Qijiang Fancbd3de12019-12-06 16:52:15 +090016#include <mojo/core/embedder/scoped_ipc_support.h>
Luke Sorenson1261c432018-09-07 16:01:41 -040017#include <mojo/public/cpp/bindings/binding.h>
Luke Sorenson1261c432018-09-07 16:01:41 -040018
Luke Sorenson523b2922018-12-26 11:09:53 -050019#include "media_perception/chrome_audio_service_client.h"
Luke Sorenson81a5fa02018-10-29 16:36:16 -040020#include "media_perception/device_management.pb.h"
Luke Sorensond7e058f2018-09-12 11:33:16 -040021#include "media_perception/media_perception_service_impl.h"
Luke Sorenson1261c432018-09-07 16:01:41 -040022#include "media_perception/producer_impl.h"
23#include "media_perception/receiver_impl.h"
Luke Sorenson13e28272018-11-09 14:43:16 -050024#include "media_perception/rtanalytics.h"
Luke Sorenson1261c432018-09-07 16:01:41 -040025#include "media_perception/video_capture_service_client.h"
Luke Sorensond7e058f2018-09-12 11:33:16 -040026#include "mojom/media_perception_service.mojom.h"
Luke Sorenson1261c432018-09-07 16:01:41 -040027
Luke Sorenson1261c432018-09-07 16:01:41 -040028namespace mri {
29
Hidehiko Abe3474edc2018-10-24 18:57:31 +090030class MojoConnector {
Luke Sorenson1261c432018-09-07 16:01:41 -040031 public:
32 MojoConnector();
33 ~MojoConnector() {}
34 // Uses a file descriptor to establish a Mojo connection.
35 void ReceiveMojoInvitationFileDescriptor(int fd_int);
36
Luke Sorenson3abab662018-10-31 15:21:26 -040037 // Set a shared pointer member variable of the video capture service client
38 // object.
39 void SetVideoCaptureServiceClient(
40 std::shared_ptr<VideoCaptureServiceClient> video_capture_service_client);
41
Luke Sorenson523b2922018-12-26 11:09:53 -050042 // Set a shared pointer member variable of the chrome audio service client
43 // object.
44 void SetChromeAudioServiceClient(
45 std::shared_ptr<ChromeAudioServiceClient> chrome_audio_service_client);
46
Luke Sorenson13e28272018-11-09 14:43:16 -050047 // Set a shared pointer member variable of the rtanalytics object.
48 void SetRtanalytics(
49 std::shared_ptr<Rtanalytics> rtanalytics);
50
Luke Sorenson1261c432018-09-07 16:01:41 -040051 // Use the Mojo connector to ensure the video capture servicec is started in
52 // Chrome and get access to the video capture service Mojo API.
53 void ConnectToVideoCaptureService();
54
Brandon Mayerfacb67a2018-10-11 16:25:45 -040055 // Check the connection state.
56 bool IsConnectedToVideoCaptureService();
57
Luke Sorenson1261c432018-09-07 16:01:41 -040058 // Get the list of video devices from the video capture service.
59 void GetDevices(
60 const VideoCaptureServiceClient::GetDevicesCallback& callback);
61
Luke Sorenson813a1082019-03-15 13:29:33 -040062 // Attempts to acquire access to a video device.
Luke Sorensonc3de6012018-11-28 17:17:20 -050063 void OpenDevice(
64 const std::string& device_id,
Luke Sorenson6393cc82019-04-25 11:55:38 -040065 bool force_reopen_with_settings,
Luke Sorenson813a1082019-03-15 13:29:33 -040066 std::shared_ptr<ReceiverImpl> receiver_impl,
67 const VideoStreamParams& capture_format,
Luke Sorensonc3de6012018-11-28 17:17:20 -050068 const VideoCaptureServiceClient::OpenDeviceCallback& callback);
Luke Sorenson1261c432018-09-07 16:01:41 -040069
Luke Sorenson813a1082019-03-15 13:29:33 -040070 bool ActivateDevice(const std::string& device_id);
Luke Sorenson1261c432018-09-07 16:01:41 -040071
Luke Sorensonc3de6012018-11-28 17:17:20 -050072 // Stops video capture on the specified active device.
73 void StopVideoCapture(const std::string& device_id);
Luke Sorenson1261c432018-09-07 16:01:41 -040074
75 // Creates a new virtual device that frames can be fed into.
Luke Sorenson1261c432018-09-07 16:01:41 -040076 void CreateVirtualDevice(
Luke Sorensonef34f392018-09-26 15:00:55 -040077 const VideoDevice& video_device,
78 std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -040079 const VideoCaptureServiceClient::VirtualDeviceCallback& callback);
80
Luke Sorensonef34f392018-09-26 15:00:55 -040081 void PushFrameToVirtualDevice(std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -040082 base::TimeDelta timestamp,
83 std::unique_ptr<const uint8_t[]> data,
84 int data_size, PixelFormat pixel_format,
85 int frame_width, int frame_height);
86
87 private:
Luke Sorenson3abab662018-10-31 15:21:26 -040088 // Returns a string with an obfuscated device id based on a counter.
89 std::string GetObfuscatedDeviceId(
90 const std::string& device_id,
91 const std::string& display_name);
92
Luke Sorenson1261c432018-09-07 16:01:41 -040093 // Handler for when the Mojo connection is closed or errors out.
94 void OnConnectionErrorOrClosed();
95
Brandon Mayerfacb67a2018-10-11 16:25:45 -040096 // Handler for when device pointer connection is closed or errors out.
Luke Sorenson813a1082019-03-15 13:29:33 -040097 void OnVideoSourceProviderConnectionErrorOrClosed();
Brandon Mayerfacb67a2018-10-11 16:25:45 -040098
Luke Sorenson1261c432018-09-07 16:01:41 -040099 void AcceptConnectionOnIpcThread(base::ScopedFD fd);
100
101 void ConnectToVideoCaptureServiceOnIpcThread();
102
103 void GetDevicesOnIpcThread(
104 const VideoCaptureServiceClient::GetDevicesCallback& callback);
105
106 void OnDeviceInfosReceived(
107 const VideoCaptureServiceClient::GetDevicesCallback& callback,
Hidehiko Abe31bb9632018-11-23 02:49:56 +0900108 std::vector<media::mojom::VideoCaptureDeviceInfoPtr> infos);
Luke Sorenson1261c432018-09-07 16:01:41 -0400109
Luke Sorensonc3de6012018-11-28 17:17:20 -0500110 void OpenDeviceOnIpcThread(
111 const std::string& device_id,
Luke Sorenson6393cc82019-04-25 11:55:38 -0400112 bool force_reopen_with_settings,
Luke Sorenson813a1082019-03-15 13:29:33 -0400113 std::shared_ptr<ReceiverImpl> receiver_impl,
114 const VideoStreamParams& capture_format,
Luke Sorensonc3de6012018-11-28 17:17:20 -0500115 const VideoCaptureServiceClient::OpenDeviceCallback& callback);
Luke Sorenson1261c432018-09-07 16:01:41 -0400116
Luke Sorenson813a1082019-03-15 13:29:33 -0400117 void OnCreatePushSubscriptionCallback(
Luke Sorensonc3de6012018-11-28 17:17:20 -0500118 const std::string& device_id,
119 const VideoCaptureServiceClient::OpenDeviceCallback& callback,
Luke Sorenson813a1082019-03-15 13:29:33 -0400120 video_capture::mojom::CreatePushSubscriptionResultCode code,
121 media::mojom::VideoCaptureParamsPtr settings_opened_with);
Luke Sorenson1261c432018-09-07 16:01:41 -0400122
Luke Sorensonc3de6012018-11-28 17:17:20 -0500123 void StopVideoCaptureOnIpcThread(const std::string& device_id);
Luke Sorenson1261c432018-09-07 16:01:41 -0400124
Luke Sorenson1261c432018-09-07 16:01:41 -0400125 void CreateVirtualDeviceOnIpcThread(
Luke Sorensonef34f392018-09-26 15:00:55 -0400126 const VideoDevice& video_device,
127 std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -0400128 const VideoCaptureServiceClient::VirtualDeviceCallback& callback);
129
Luke Sorenson1261c432018-09-07 16:01:41 -0400130 void PushFrameToVirtualDeviceOnIpcThread(
Luke Sorensonef34f392018-09-26 15:00:55 -0400131 std::shared_ptr<ProducerImpl> producer_impl, base::TimeDelta timestamp,
Luke Sorenson1261c432018-09-07 16:01:41 -0400132 std::unique_ptr<const uint8_t[]> data, int data_size,
133 PixelFormat pixel_format, int frame_width, int frame_height);
134
135 // Separate thread for doing IPC via Mojo because Mojo is asynchronous
136 // by default.
137 base::Thread ipc_thread_;
138
Luke Sorenson3abab662018-10-31 15:21:26 -0400139 // Stores pointer to the video capture service client object.
140 std::shared_ptr<VideoCaptureServiceClient> video_capture_service_client_;
141
Luke Sorenson523b2922018-12-26 11:09:53 -0500142 // Stores pointer to the chrome audio service client object.
143 std::shared_ptr<ChromeAudioServiceClient> chrome_audio_service_client_;
144
Luke Sorenson13e28272018-11-09 14:43:16 -0500145 // Stores pointer to the rtanalytics object.
146 std::shared_ptr<Rtanalytics> rtanalytics_;
147
Luke Sorensond7e058f2018-09-12 11:33:16 -0400148 // Implementation for the media perception service Mojo interface.
149 std::unique_ptr<MediaPerceptionServiceImpl> media_perception_service_impl_;
Luke Sorenson1261c432018-09-07 16:01:41 -0400150
151 // Entry point Mojo object for talking to the video capture service API.
Luke Sorenson813a1082019-03-15 13:29:33 -0400152 video_capture::mojom::VideoSourceProviderPtr video_source_provider_;
153
154 struct VideoSourceAndPushSubscription {
155 video_capture::mojom::VideoSourcePtr video_source;
156 video_capture::mojom::PushVideoStreamSubscriptionPtr
157 push_video_stream_subscription;
158 };
Luke Sorenson1261c432018-09-07 16:01:41 -0400159
Luke Sorensonc3de6012018-11-28 17:17:20 -0500160 // Store a map from device ids to active devices.
161 std::map<std::string /* obfuscated device_id */,
Luke Sorenson813a1082019-03-15 13:29:33 -0400162 VideoSourceAndPushSubscription /* active_device */>
Luke Sorensonc3de6012018-11-28 17:17:20 -0500163 device_id_to_active_device_map_;
Luke Sorenson1261c432018-09-07 16:01:41 -0400164
Luke Sorenson3abab662018-10-31 15:21:26 -0400165 int unique_device_counter_;
166
167 // Maps unique ids for devices (device_id + display_name) to an obfuscated
168 // device id, which is generated by a counter.
169 std::map<
170 std::string /* device_id + display_name */,
171 std::string /* obfuscated device_id */> unique_id_map_;
172
173 // Map to store obfuscated device ids and their associated real device ids.
174 // Makes it so that clients of the service do not know or need to know the
175 // real id of connected devices.
176 std::map<
177 std::string /* obfuscated device_id */,
178 std::string /* device_id */> obfuscated_device_id_map_;
179
Qijiang Fancbd3de12019-12-06 16:52:15 +0900180 std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_;
181
Brandon Mayerfacb67a2018-10-11 16:25:45 -0400182 std::mutex vcs_connection_state_mutex_;
183 bool is_connected_to_vcs_ = false;
Luke Sorenson1261c432018-09-07 16:01:41 -0400184};
185
186} // namespace mri
187
188#endif // MEDIA_PERCEPTION_MOJO_CONNECTOR_H_