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> OnMouseMoveEvent(int x, int y) = 0; 48 virtual Result<void> OnMouseButtonEvent(int button, bool down) = 0; 49 virtual Result<void> OnMouseWheelEvent(int pixels) = 0; 50 virtual Result<void> OnTouchEvent(const std::string& device_label, int x, 51 int y, bool down) = 0; 52 virtual Result<void> OnMultiTouchEvent(const std::string& label, 53 Json::Value id, Json::Value slot, 54 Json::Value x, Json::Value y, 55 bool down, int size) = 0; 56 57 virtual Result<void> OnKeyboardEvent(uint16_t keycode, bool down) = 0; 58 59 virtual Result<void> OnRotaryWheelEvent(int pixels) = 0; 60 61 virtual void OnAdbChannelOpen( 62 std::function<bool(const uint8_t*, size_t)> adb_message_sender) = 0; 63 virtual void OnAdbMessage(const uint8_t* msg, size_t size) = 0; 64 65 virtual void OnControlChannelOpen( 66 std::function<bool(const Json::Value)> control_message_sender) = 0; 67 virtual Result<void> OnLidStateChange(bool lid_open) = 0; 68 virtual void OnHingeAngleChange(int hinge_angle) = 0; 69 virtual Result<void> OnPowerButton(bool button_down) = 0; 70 virtual Result<void> OnBackButton(bool button_down) = 0; 71 virtual Result<void> OnHomeButton(bool button_down) = 0; 72 virtual Result<void> OnMenuButton(bool button_down) = 0; 73 virtual Result<void> OnVolumeDownButton(bool button_down) = 0; 74 virtual Result<void> OnVolumeUpButton(bool button_down) = 0; 75 virtual void OnCustomActionButton(const std::string& command, 76 const std::string& button_state) = 0; 77 78 virtual void OnCameraControlMsg(const Json::Value& msg) = 0; 79 virtual void OnDisplayControlMsg(const Json::Value& msg) = 0; 80 81 virtual void OnBluetoothChannelOpen( 82 std::function<bool(const uint8_t*, size_t)> bluetooth_message_sender) = 0; 83 virtual void OnBluetoothMessage(const uint8_t* msg, size_t size) = 0; 84 virtual void OnSensorsChannelOpen( 85 std::function<bool(const uint8_t*, size_t)> sensors_message_sender) = 0; 86 virtual void OnSensorsMessage(const uint8_t* msg, size_t size) = 0; 87 virtual void OnSensorsChannelClosed() = 0; 88 virtual void OnLightsChannelOpen( 89 std::function<bool(const Json::Value&)> lights_message_sender) = 0; 90 virtual void OnLightsChannelClosed() = 0; 91 virtual void OnLocationChannelOpen( 92 std::function<bool(const uint8_t*, size_t)> location_message_sender) = 0; 93 virtual void OnLocationMessage(const uint8_t* msg, size_t size) = 0; 94 95 virtual void OnKmlLocationsChannelOpen( 96 std::function<bool(const uint8_t*, size_t)> 97 kml_locations_message_sender) = 0; 98 99 virtual void OnGpxLocationsChannelOpen( 100 std::function<bool(const uint8_t*, size_t)> 101 gpx_locations_message_sender) = 0; 102 virtual void OnKmlLocationsMessage(const uint8_t* msg, size_t size) = 0; 103 virtual void OnGpxLocationsMessage(const uint8_t* msg, size_t size) = 0; 104 105 virtual void OnCameraData(const std::vector<char>& data) = 0; 106 }; 107 108 class ConnectionObserverFactory { 109 public: 110 virtual ~ConnectionObserverFactory() = default; 111 // Called when a new connection is requested 112 virtual std::shared_ptr<ConnectionObserver> CreateObserver() = 0; 113 }; 114 115 } // namespace webrtc_streaming 116 } // namespace cuttlefish 117