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