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 | |
| 8 | #include <base/bind.h> |
| 9 | #include <base/single_thread_task_runner.h> |
| 10 | #include <base/threading/thread.h> |
| 11 | #include <mojo/edk/embedder/embedder.h> |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 12 | #include <mojo/public/cpp/bindings/binding.h> |
| 13 | #include <memory> |
| 14 | #include <string> |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame^] | 15 | #include <vector> |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 16 | |
Luke Sorenson | 81a5fa0 | 2018-10-29 16:36:16 -0400 | [diff] [blame] | 17 | #include "media_perception/device_management.pb.h" |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 18 | #include "media_perception/media_perception_service_impl.h" |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 19 | #include "media_perception/producer_impl.h" |
| 20 | #include "media_perception/receiver_impl.h" |
| 21 | #include "media_perception/video_capture_service_client.h" |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 22 | #include "mojom/media_perception_service.mojom.h" |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 23 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 24 | namespace mri { |
| 25 | |
Hidehiko Abe | 3474edc | 2018-10-24 18:57:31 +0900 | [diff] [blame] | 26 | class MojoConnector { |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 27 | public: |
| 28 | MojoConnector(); |
| 29 | ~MojoConnector() {} |
| 30 | // Uses a file descriptor to establish a Mojo connection. |
| 31 | void ReceiveMojoInvitationFileDescriptor(int fd_int); |
| 32 | |
| 33 | // Use the Mojo connector to ensure the video capture servicec is started in |
| 34 | // Chrome and get access to the video capture service Mojo API. |
| 35 | void ConnectToVideoCaptureService(); |
| 36 | |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 37 | // Check the connection state. |
| 38 | bool IsConnectedToVideoCaptureService(); |
| 39 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 40 | // Get the list of video devices from the video capture service. |
| 41 | void GetDevices( |
| 42 | const VideoCaptureServiceClient::GetDevicesCallback& callback); |
| 43 | |
| 44 | // Attempts to acquire exclusive access to a video device. Note that this does |
| 45 | // not block another client of the video capture service from taking over |
| 46 | // access on this device, which would disconnect this client. |
| 47 | void SetActiveDevice( |
| 48 | std::string device_id, |
| 49 | const VideoCaptureServiceClient::SetActiveDeviceCallback& callback); |
| 50 | |
| 51 | // Starts video capture on the active device. |
Luke Sorenson | 81a5fa0 | 2018-10-29 16:36:16 -0400 | [diff] [blame] | 52 | void StartVideoCapture(const VideoStreamParams& capture_format, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 53 | std::function<void(uint64_t timestamp_in_microseconds, |
| 54 | const uint8_t* data, int data_size)> |
| 55 | frame_handler); |
| 56 | |
| 57 | // Stops video capture on the active device. |
| 58 | void StopVideoCapture(); |
| 59 | |
| 60 | // Creates a new virtual device that frames can be fed into. |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 61 | void CreateVirtualDevice( |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 62 | const VideoDevice& video_device, |
| 63 | std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 64 | const VideoCaptureServiceClient::VirtualDeviceCallback& callback); |
| 65 | |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 66 | void PushFrameToVirtualDevice(std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 67 | base::TimeDelta timestamp, |
| 68 | std::unique_ptr<const uint8_t[]> data, |
| 69 | int data_size, PixelFormat pixel_format, |
| 70 | int frame_width, int frame_height); |
| 71 | |
| 72 | private: |
| 73 | // Handler for when the Mojo connection is closed or errors out. |
| 74 | void OnConnectionErrorOrClosed(); |
| 75 | |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 76 | // Handler for when device pointer connection is closed or errors out. |
| 77 | void OnDeviceFactoryConnectionErrorOrClosed(); |
| 78 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 79 | void AcceptConnectionOnIpcThread(base::ScopedFD fd); |
| 80 | |
| 81 | void ConnectToVideoCaptureServiceOnIpcThread(); |
| 82 | |
| 83 | void GetDevicesOnIpcThread( |
| 84 | const VideoCaptureServiceClient::GetDevicesCallback& callback); |
| 85 | |
| 86 | void OnDeviceInfosReceived( |
| 87 | const VideoCaptureServiceClient::GetDevicesCallback& callback, |
Hidehiko Abe | 31bb963 | 2018-11-23 02:49:56 +0900 | [diff] [blame^] | 88 | std::vector<media::mojom::VideoCaptureDeviceInfoPtr> infos); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 89 | |
| 90 | void SetActiveDeviceOnIpcThread( |
| 91 | std::string device_id, |
| 92 | const VideoCaptureServiceClient::SetActiveDeviceCallback& callback); |
| 93 | |
| 94 | void OnSetActiveDeviceCallback( |
| 95 | const VideoCaptureServiceClient::SetActiveDeviceCallback& callback, |
| 96 | video_capture::mojom::DeviceAccessResultCode code); |
| 97 | |
Luke Sorenson | 81a5fa0 | 2018-10-29 16:36:16 -0400 | [diff] [blame] | 98 | void StartVideoCaptureOnIpcThread(const VideoStreamParams& capture_format); |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 99 | |
| 100 | void StopVideoCaptureOnIpcThread(); |
| 101 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 102 | void CreateVirtualDeviceOnIpcThread( |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 103 | const VideoDevice& video_device, |
| 104 | std::shared_ptr<ProducerImpl> producer_impl, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 105 | const VideoCaptureServiceClient::VirtualDeviceCallback& callback); |
| 106 | |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 107 | void PushFrameToVirtualDeviceOnIpcThread( |
Luke Sorenson | ef34f39 | 2018-09-26 15:00:55 -0400 | [diff] [blame] | 108 | std::shared_ptr<ProducerImpl> producer_impl, base::TimeDelta timestamp, |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 109 | std::unique_ptr<const uint8_t[]> data, int data_size, |
| 110 | PixelFormat pixel_format, int frame_width, int frame_height); |
| 111 | |
| 112 | // Separate thread for doing IPC via Mojo because Mojo is asynchronous |
| 113 | // by default. |
| 114 | base::Thread ipc_thread_; |
| 115 | |
Luke Sorenson | d7e058f | 2018-09-12 11:33:16 -0400 | [diff] [blame] | 116 | // Implementation for the media perception service Mojo interface. |
| 117 | std::unique_ptr<MediaPerceptionServiceImpl> media_perception_service_impl_; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 118 | |
| 119 | // Entry point Mojo object for talking to the video capture service API. |
| 120 | video_capture::mojom::DeviceFactoryPtr device_factory_; |
| 121 | |
| 122 | // Provides interface to an open device. |
| 123 | video_capture::mojom::DevicePtr active_device_; |
| 124 | |
| 125 | // Provides interface for receiving frames from the video capture service. |
| 126 | ReceiverImpl receiver_impl_; |
Brandon Mayer | facb67a | 2018-10-11 16:25:45 -0400 | [diff] [blame] | 127 | |
| 128 | std::mutex vcs_connection_state_mutex_; |
| 129 | bool is_connected_to_vcs_ = false; |
Luke Sorenson | 1261c43 | 2018-09-07 16:01:41 -0400 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } // namespace mri |
| 133 | |
| 134 | #endif // MEDIA_PERCEPTION_MOJO_CONNECTOR_H_ |