Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 1 | // 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 Sorenson | 13e2827 | 2018-11-09 14:43:16 -0500 | [diff] [blame] | 8 | #include <map> |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 13 | #include <base/bind.h> |
| 14 | #include <base/single_thread_task_runner.h> |
| 15 | #include <base/threading/thread.h> |
Luke Sorenson | 29cf6a6 | 2020-10-26 23:59:50 -0400 | [diff] [blame^] | 16 | #include <mojo/public/cpp/bindings/remote.h> |
Qijiang Fan | cbd3de1 | 2019-12-06 16:52:15 +0900 | [diff] [blame] | 17 | #include <mojo/core/embedder/scoped_ipc_support.h> |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 18 | #include <mojo/public/cpp/bindings/binding.h> |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 19 | |
Luke Sorenson | 523b292 | 2018-12-26 11:09:53 -0500 | [diff] [blame] | 20 | #include "media_perception/chrome_audio_service_client.h" |
Luke Sorenson | 81a5fa0 | 2018-10-29 16:36:16 -0400 | [diff] [blame] | 21 | #include "media_perception/device_management.pb.h" |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 22 | #include "media_perception/media_perception_service_impl.h" |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 23 | #include "media_perception/producer_impl.h" |
Luke Sorenson | 13e2827 | 2018-11-09 14:43:16 -0500 | [diff] [blame] | 24 | #include "media_perception/rtanalytics.h" |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 25 | #include "media_perception/video_capture_service_client.h" |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 26 | #include "media_perception/video_frame_handler_impl.h" |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 27 | #include "mojom/media_perception_service.mojom.h" |
Qijiang Fan | b833a32 | 2020-05-26 02:15:32 +0900 | [diff] [blame] | 28 | #include "mojom/video_source.mojom.h" |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 29 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 30 | namespace mri { |
| 31 | |
Hidehiko Abe | 3474edc | 2018-10-24 18:57:31 +0900 | [diff] [blame] | 32 | class MojoConnector { |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 33 | public: |
| 34 | MojoConnector(); |
| 35 | ~MojoConnector() {} |
| 36 | // Uses a file descriptor to establish a Mojo connection. |
| 37 | void ReceiveMojoInvitationFileDescriptor(int fd_int); |
| 38 | |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 39 | // Set a shared pointer member variable of the video capture service client |
| 40 | // object. |
| 41 | void SetVideoCaptureServiceClient( |
| 42 | std::shared_ptr<VideoCaptureServiceClient> video_capture_service_client); |
| 43 | |
Luke Sorenson | 523b292 | 2018-12-26 11:09:53 -0500 | [diff] [blame] | 44 | // Set a shared pointer member variable of the chrome audio service client |
| 45 | // object. |
| 46 | void SetChromeAudioServiceClient( |
| 47 | std::shared_ptr<ChromeAudioServiceClient> chrome_audio_service_client); |
| 48 | |
Luke Sorenson | 13e2827 | 2018-11-09 14:43:16 -0500 | [diff] [blame] | 49 | // Set a shared pointer member variable of the rtanalytics object. |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 50 | void SetRtanalytics(std::shared_ptr<Rtanalytics> rtanalytics); |
Luke Sorenson | 13e2827 | 2018-11-09 14:43:16 -0500 | [diff] [blame] | 51 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 52 | // Use the Mojo connector to ensure the video capture servicec is started in |
| 53 | // Chrome and get access to the video capture service Mojo API. |
| 54 | void ConnectToVideoCaptureService(); |
| 55 | |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 56 | // Check the connection state. |
| 57 | bool IsConnectedToVideoCaptureService(); |
| 58 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 59 | // Get the list of video devices from the video capture service. |
| 60 | void GetDevices( |
| 61 | const VideoCaptureServiceClient::GetDevicesCallback& callback); |
| 62 | |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 63 | // Attempts to acquire access to a video device. |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 64 | void OpenDevice( |
| 65 | const std::string& device_id, |
Luke Sorenson | 6393cc8 | 2019-04-25 11:55:38 -0400 | [diff] [blame] | 66 | bool force_reopen_with_settings, |
Luke Sorenson | 9155956 | 2019-12-26 18:26:39 -0500 | [diff] [blame] | 67 | std::shared_ptr<VideoFrameHandlerImpl> video_frame_handler_impl, |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 68 | const VideoStreamParams& capture_format, |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 69 | const VideoCaptureServiceClient::OpenDeviceCallback& callback); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 70 | |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 71 | bool ActivateDevice(const std::string& device_id); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 72 | |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 73 | // Stops video capture on the specified active device. |
| 74 | void StopVideoCapture(const std::string& device_id); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 75 | |
| 76 | // Creates a new virtual device that frames can be fed into. |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 77 | void CreateVirtualDevice( |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 78 | const VideoDevice& video_device, |
| 79 | std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 80 | const VideoCaptureServiceClient::VirtualDeviceCallback& callback); |
| 81 | |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 82 | void PushFrameToVirtualDevice(std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 83 | base::TimeDelta timestamp, |
| 84 | std::unique_ptr<const uint8_t[]> data, |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 85 | int data_size, |
| 86 | PixelFormat pixel_format, |
| 87 | int frame_width, |
| 88 | int frame_height); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 89 | |
| 90 | private: |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 91 | // Returns a string with an obfuscated device id based on a counter. |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 92 | std::string GetObfuscatedDeviceId(const std::string& device_id, |
| 93 | const std::string& display_name); |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 94 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 95 | // Handler for when the Mojo connection is closed or errors out. |
| 96 | void OnConnectionErrorOrClosed(); |
| 97 | |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 98 | // Handler for when device pointer connection is closed or errors out. |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 99 | void OnVideoSourceProviderConnectionErrorOrClosed(); |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 100 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 101 | void AcceptConnectionOnIpcThread(base::ScopedFD fd); |
| 102 | |
| 103 | void ConnectToVideoCaptureServiceOnIpcThread(); |
| 104 | |
| 105 | void GetDevicesOnIpcThread( |
| 106 | const VideoCaptureServiceClient::GetDevicesCallback& callback); |
| 107 | |
| 108 | void OnDeviceInfosReceived( |
| 109 | const VideoCaptureServiceClient::GetDevicesCallback& callback, |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame] | 110 | std::vector<media::mojom::VideoCaptureDeviceInfoPtr> infos); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 111 | |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 112 | void OpenDeviceOnIpcThread( |
| 113 | const std::string& device_id, |
Luke Sorenson | 6393cc8 | 2019-04-25 11:55:38 -0400 | [diff] [blame] | 114 | bool force_reopen_with_settings, |
Luke Sorenson | 9155956 | 2019-12-26 18:26:39 -0500 | [diff] [blame] | 115 | std::shared_ptr<VideoFrameHandlerImpl> video_frame_handler_impl, |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 116 | const VideoStreamParams& capture_format, |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 117 | const VideoCaptureServiceClient::OpenDeviceCallback& callback); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 118 | |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 119 | void OnCreatePushSubscriptionCallback( |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 120 | const std::string& device_id, |
| 121 | const VideoCaptureServiceClient::OpenDeviceCallback& callback, |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 122 | video_capture::mojom::CreatePushSubscriptionResultCode code, |
| 123 | media::mojom::VideoCaptureParamsPtr settings_opened_with); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 124 | |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 125 | void StopVideoCaptureOnIpcThread(const std::string& device_id); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 126 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 127 | void CreateVirtualDeviceOnIpcThread( |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 128 | const VideoDevice& video_device, |
| 129 | std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 130 | const VideoCaptureServiceClient::VirtualDeviceCallback& callback); |
| 131 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 132 | void PushFrameToVirtualDeviceOnIpcThread( |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 133 | std::shared_ptr<ProducerImpl> producer_impl, |
| 134 | base::TimeDelta timestamp, |
| 135 | std::unique_ptr<const uint8_t[]> data, |
| 136 | int data_size, |
| 137 | PixelFormat pixel_format, |
| 138 | int frame_width, |
| 139 | int frame_height); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 140 | |
| 141 | // Separate thread for doing IPC via Mojo because Mojo is asynchronous |
| 142 | // by default. |
| 143 | base::Thread ipc_thread_; |
| 144 | |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 145 | // Stores pointer to the video capture service client object. |
| 146 | std::shared_ptr<VideoCaptureServiceClient> video_capture_service_client_; |
| 147 | |
Luke Sorenson | 523b292 | 2018-12-26 11:09:53 -0500 | [diff] [blame] | 148 | // Stores pointer to the chrome audio service client object. |
| 149 | std::shared_ptr<ChromeAudioServiceClient> chrome_audio_service_client_; |
| 150 | |
Luke Sorenson | 13e2827 | 2018-11-09 14:43:16 -0500 | [diff] [blame] | 151 | // Stores pointer to the rtanalytics object. |
| 152 | std::shared_ptr<Rtanalytics> rtanalytics_; |
| 153 | |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 154 | // Implementation for the media perception service Mojo interface. |
| 155 | std::unique_ptr<MediaPerceptionServiceImpl> media_perception_service_impl_; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 156 | |
| 157 | // Entry point Mojo object for talking to the video capture service API. |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 158 | video_capture::mojom::VideoSourceProviderPtr video_source_provider_; |
| 159 | |
| 160 | struct VideoSourceAndPushSubscription { |
| 161 | video_capture::mojom::VideoSourcePtr video_source; |
Luke Sorenson | 29cf6a6 | 2020-10-26 23:59:50 -0400 | [diff] [blame^] | 162 | mojo::Remote<video_capture::mojom::PushVideoStreamSubscription> |
Luke Sorenson | 813a108 | 2019-03-15 13:29:33 -0400 | [diff] [blame] | 163 | push_video_stream_subscription; |
| 164 | }; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 165 | |
Luke Sorenson | c3de601 | 2018-11-28 17:17:20 -0500 | [diff] [blame] | 166 | // Store a map from device ids to active devices. |
| 167 | std::map<std::string /* obfuscated device_id */, |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 168 | VideoSourceAndPushSubscription /* active_device */> |
| 169 | device_id_to_active_device_map_; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 170 | |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 171 | int unique_device_counter_; |
| 172 | |
| 173 | // Maps unique ids for devices (device_id + display_name) to an obfuscated |
| 174 | // device id, which is generated by a counter. |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 175 | std::map<std::string /* device_id + display_name */, |
| 176 | std::string /* obfuscated device_id */> |
| 177 | unique_id_map_; |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 178 | |
| 179 | // Map to store obfuscated device ids and their associated real device ids. |
| 180 | // Makes it so that clients of the service do not know or need to know the |
| 181 | // real id of connected devices. |
Tom Hughes | 1ef9374 | 2020-09-08 11:36:30 -0700 | [diff] [blame] | 182 | std::map<std::string /* obfuscated device_id */, std::string /* device_id */> |
| 183 | obfuscated_device_id_map_; |
Luke Sorenson | 3abab66 | 2018-10-31 15:21:26 -0400 | [diff] [blame] | 184 | |
Qijiang Fan | cbd3de1 | 2019-12-06 16:52:15 +0900 | [diff] [blame] | 185 | std::unique_ptr<mojo::core::ScopedIPCSupport> ipc_support_; |
| 186 | |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 187 | std::mutex vcs_connection_state_mutex_; |
| 188 | bool is_connected_to_vcs_ = false; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | } // namespace mri |
| 192 | |
| 193 | #endif // MEDIA_PERCEPTION_MOJO_CONNECTOR_H_ |