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