1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <functional>
20 
21 #include <json/json.h>
22 
23 #include "common/libs/utils/result.h"
24 
25 namespace cuttlefish {
26 namespace webrtc_streaming {
27 
28 // The ConnectionObserver is the boundary between device specific code and
29 // general WebRTC streaming code. Device specific code should be left to
30 // implementations of this class while code that could be shared between any
31 // device using this streaming library should remain in the library.
32 // For example:
33 // - Parsing JSON messages to obtain input events is common to all android
34 // devices and should stay in the library.
35 // - Sending input events to the device by writing to a socket is cuttlefish
36 // specific and should be done in the ConnectionObserver implementation. Other
37 // devices could choose to send those events over ADB for example. A good rule
38 // of thumb is: if it was encoded client side in cf_webrtc.js it should be
39 // decoded in the library.
40 class ConnectionObserver {
41  public:
42   ConnectionObserver() = default;
43   virtual ~ConnectionObserver() = default;
44 
45   virtual void OnConnected() = 0;
46 
47   virtual Result<void> OnTouchEvent(const std::string& device_label, int x,
48                                     int y, bool down) = 0;
49   virtual Result<void> OnMultiTouchEvent(const std::string& label,
50                                          Json::Value id, Json::Value slot,
51                                          Json::Value x, Json::Value y,
52                                          bool down, int size) = 0;
53 
54   virtual Result<void> OnKeyboardEvent(uint16_t keycode, bool down) = 0;
55 
56   virtual Result<void> OnWheelEvent(int pixels) = 0;
57 
58   virtual void OnAdbChannelOpen(
59       std::function<bool(const uint8_t*, size_t)> adb_message_sender) = 0;
60   virtual void OnAdbMessage(const uint8_t* msg, size_t size) = 0;
61 
62   virtual void OnControlChannelOpen(
63       std::function<bool(const Json::Value)> control_message_sender) = 0;
64   virtual Result<void> OnLidStateChange(bool lid_open) = 0;
65   virtual void OnHingeAngleChange(int hinge_angle) = 0;
66   virtual Result<void> OnPowerButton(bool button_down) = 0;
67   virtual Result<void> OnBackButton(bool button_down) = 0;
68   virtual Result<void> OnHomeButton(bool button_down) = 0;
69   virtual Result<void> OnMenuButton(bool button_down) = 0;
70   virtual Result<void> OnVolumeDownButton(bool button_down) = 0;
71   virtual Result<void> OnVolumeUpButton(bool button_down) = 0;
72   virtual void OnCustomActionButton(const std::string& command,
73                                     const std::string& button_state) = 0;
74 
75   virtual void OnCameraControlMsg(const Json::Value& msg) = 0;
76   virtual void OnDisplayControlMsg(const Json::Value& msg) = 0;
77 
78   virtual void OnBluetoothChannelOpen(
79       std::function<bool(const uint8_t*, size_t)> bluetooth_message_sender) = 0;
80   virtual void OnBluetoothMessage(const uint8_t* msg, size_t size) = 0;
81   virtual void OnSensorsChannelOpen(
82       std::function<bool(const uint8_t*, size_t)> sensors_message_sender) = 0;
83   virtual void OnSensorsMessage(const uint8_t* msg, size_t size) = 0;
84   virtual void OnSensorsChannelClosed() = 0;
85   virtual void OnLightsChannelOpen(
86       std::function<bool(const Json::Value&)> lights_message_sender) = 0;
87   virtual void OnLightsChannelClosed() = 0;
88   virtual void OnLocationChannelOpen(
89       std::function<bool(const uint8_t*, size_t)> location_message_sender) = 0;
90   virtual void OnLocationMessage(const uint8_t* msg, size_t size) = 0;
91 
92   virtual void OnKmlLocationsChannelOpen(
93       std::function<bool(const uint8_t*, size_t)>
94           kml_locations_message_sender) = 0;
95 
96   virtual void OnGpxLocationsChannelOpen(
97       std::function<bool(const uint8_t*, size_t)>
98           gpx_locations_message_sender) = 0;
99   virtual void OnKmlLocationsMessage(const uint8_t* msg, size_t size) = 0;
100   virtual void OnGpxLocationsMessage(const uint8_t* msg, size_t size) = 0;
101 
102   virtual void OnCameraData(const std::vector<char>& data) = 0;
103 };
104 
105 class ConnectionObserverFactory {
106  public:
107   virtual ~ConnectionObserverFactory() = default;
108   // Called when a new connection is requested
109   virtual std::shared_ptr<ConnectionObserver> CreateObserver() = 0;
110 };
111 
112 }  // namespace webrtc_streaming
113 }  // namespace cuttlefish
114