blob: 9d6afd71026c98bb96b1b297c1e1f9fd3e166960 [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
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 Sorenson1261c432018-09-07 16:01:41 -040012#include <mojo/public/cpp/bindings/binding.h>
13#include <memory>
14#include <string>
15
Luke Sorenson81a5fa02018-10-29 16:36:16 -040016#include "media_perception/device_management.pb.h"
Luke Sorensond7e058f2018-09-12 11:33:16 -040017#include "media_perception/media_perception_service_impl.h"
Luke Sorenson1261c432018-09-07 16:01:41 -040018#include "media_perception/producer_impl.h"
19#include "media_perception/receiver_impl.h"
20#include "media_perception/video_capture_service_client.h"
Luke Sorensond7e058f2018-09-12 11:33:16 -040021#include "mojom/media_perception_service.mojom.h"
Luke Sorenson1261c432018-09-07 16:01:41 -040022
Luke Sorenson1261c432018-09-07 16:01:41 -040023namespace mri {
24
Hidehiko Abe3474edc2018-10-24 18:57:31 +090025class MojoConnector {
Luke Sorenson1261c432018-09-07 16:01:41 -040026 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 Mayerfacb67a2018-10-11 16:25:45 -040036 // Check the connection state.
37 bool IsConnectedToVideoCaptureService();
38
Luke Sorenson1261c432018-09-07 16:01:41 -040039 // 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.
Luke Sorenson81a5fa02018-10-29 16:36:16 -040051 void StartVideoCapture(const VideoStreamParams& capture_format,
Luke Sorenson1261c432018-09-07 16:01:41 -040052 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 Sorenson1261c432018-09-07 16:01:41 -040060 void CreateVirtualDevice(
Luke Sorensonef34f392018-09-26 15:00:55 -040061 const VideoDevice& video_device,
62 std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -040063 const VideoCaptureServiceClient::VirtualDeviceCallback& callback);
64
Luke Sorensonef34f392018-09-26 15:00:55 -040065 void PushFrameToVirtualDevice(std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -040066 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 Mayerfacb67a2018-10-11 16:25:45 -040075 // Handler for when device pointer connection is closed or errors out.
76 void OnDeviceFactoryConnectionErrorOrClosed();
77
Luke Sorenson1261c432018-09-07 16:01:41 -040078 void AcceptConnectionOnIpcThread(base::ScopedFD fd);
79
80 void ConnectToVideoCaptureServiceOnIpcThread();
81
82 void GetDevicesOnIpcThread(
83 const VideoCaptureServiceClient::GetDevicesCallback& callback);
84
85 void OnDeviceInfosReceived(
86 const VideoCaptureServiceClient::GetDevicesCallback& callback,
87 mojo::Array<media::mojom::VideoCaptureDeviceInfoPtr> infos);
88
89 void SetActiveDeviceOnIpcThread(
90 std::string device_id,
91 const VideoCaptureServiceClient::SetActiveDeviceCallback& callback);
92
93 void OnSetActiveDeviceCallback(
94 const VideoCaptureServiceClient::SetActiveDeviceCallback& callback,
95 video_capture::mojom::DeviceAccessResultCode code);
96
Luke Sorenson81a5fa02018-10-29 16:36:16 -040097 void StartVideoCaptureOnIpcThread(const VideoStreamParams& capture_format);
Luke Sorenson1261c432018-09-07 16:01:41 -040098
99 void StopVideoCaptureOnIpcThread();
100
Luke Sorenson1261c432018-09-07 16:01:41 -0400101 void CreateVirtualDeviceOnIpcThread(
Luke Sorensonef34f392018-09-26 15:00:55 -0400102 const VideoDevice& video_device,
103 std::shared_ptr<ProducerImpl> producer_impl,
Luke Sorenson1261c432018-09-07 16:01:41 -0400104 const VideoCaptureServiceClient::VirtualDeviceCallback& callback);
105
Luke Sorenson1261c432018-09-07 16:01:41 -0400106 void PushFrameToVirtualDeviceOnIpcThread(
Luke Sorensonef34f392018-09-26 15:00:55 -0400107 std::shared_ptr<ProducerImpl> producer_impl, base::TimeDelta timestamp,
Luke Sorenson1261c432018-09-07 16:01:41 -0400108 std::unique_ptr<const uint8_t[]> data, int data_size,
109 PixelFormat pixel_format, int frame_width, int frame_height);
110
111 // Separate thread for doing IPC via Mojo because Mojo is asynchronous
112 // by default.
113 base::Thread ipc_thread_;
114
Luke Sorensond7e058f2018-09-12 11:33:16 -0400115 // Implementation for the media perception service Mojo interface.
116 std::unique_ptr<MediaPerceptionServiceImpl> media_perception_service_impl_;
Luke Sorenson1261c432018-09-07 16:01:41 -0400117
118 // Entry point Mojo object for talking to the video capture service API.
119 video_capture::mojom::DeviceFactoryPtr device_factory_;
120
121 // Provides interface to an open device.
122 video_capture::mojom::DevicePtr active_device_;
123
124 // Provides interface for receiving frames from the video capture service.
125 ReceiverImpl receiver_impl_;
Brandon Mayerfacb67a2018-10-11 16:25:45 -0400126
127 std::mutex vcs_connection_state_mutex_;
128 bool is_connected_to_vcs_ = false;
Luke Sorenson1261c432018-09-07 16:01:41 -0400129};
130
131} // namespace mri
132
133#endif // MEDIA_PERCEPTION_MOJO_CONNECTOR_H_