• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace cuttlefish {
24 namespace webrtc_streaming {
25 
26 // The ConnectionObserver is the boundary between device specific code and
27 // general WebRTC streaming code. Device specific code should be left to
28 // implementations of this class while code that could be shared between any
29 // device using this streaming library should remain in the library.
30 // For example:
31 // - Parsing JSON messages to obtain input events is common to all android
32 // devices and should stay in the library.
33 // - Sending input events to the device by writing to a socket is cuttlefish
34 // specific and should be done in the ConnectionObserver implementation. Other
35 // devices could choose to send those events over ADB for example. A good rule
36 // of thumb is: if it was encoded client side in cf_webrtc.js it should be
37 // decoded in the library.
38 class ConnectionObserver {
39  public:
40   ConnectionObserver() = default;
41   virtual ~ConnectionObserver() = default;
42 
43   virtual void OnConnected() = 0;
44 
45   virtual void OnTouchEvent(const std::string& display_label, int x, int y,
46                             bool down) = 0;
47   virtual void OnMultiTouchEvent(const std::string& label, Json::Value id,
48                                  Json::Value slot, Json::Value x, Json::Value y,
49                                  bool down, int size) = 0;
50 
51   virtual void OnKeyboardEvent(uint16_t keycode, bool down) = 0;
52 
53   virtual void OnAdbChannelOpen(
54       std::function<bool(const uint8_t*, size_t)> adb_message_sender) = 0;
55   virtual void OnAdbMessage(const uint8_t* msg, size_t size) = 0;
56 
57   virtual void OnControlChannelOpen(
58       std::function<bool(const Json::Value)> control_message_sender) = 0;
59   virtual void OnLidStateChange(bool lid_open) = 0;
60   virtual void OnHingeAngleChange(int hinge_angle) = 0;
61   virtual void OnPowerButton(bool button_down) = 0;
62   virtual void OnBackButton(bool button_down) = 0;
63   virtual void OnHomeButton(bool button_down) = 0;
64   virtual void OnMenuButton(bool button_down) = 0;
65   virtual void OnVolumeDownButton(bool button_down) = 0;
66   virtual void OnVolumeUpButton(bool button_down) = 0;
67   virtual void OnCustomActionButton(const std::string& command,
68                                     const std::string& button_state) = 0;
69 
70   virtual void OnCameraControlMsg(const Json::Value& msg) = 0;
71 
72   virtual void OnBluetoothChannelOpen(
73       std::function<bool(const uint8_t*, size_t)> bluetooth_message_sender) = 0;
74   virtual void OnBluetoothMessage(const uint8_t* msg, size_t size) = 0;
75   virtual void OnLocationChannelOpen(
76       std::function<bool(const uint8_t*, size_t)> location_message_sender) = 0;
77   virtual void OnLocationMessage(const uint8_t* msg, size_t size) = 0;
78 
79   virtual void OnKmlLocationsChannelOpen(
80       std::function<bool(const uint8_t*, size_t)>
81           kml_locations_message_sender) = 0;
82 
83   virtual void OnGpxLocationsChannelOpen(
84       std::function<bool(const uint8_t*, size_t)>
85           gpx_locations_message_sender) = 0;
86   virtual void OnKmlLocationsMessage(const uint8_t* msg, size_t size) = 0;
87   virtual void OnGpxLocationsMessage(const uint8_t* msg, size_t size) = 0;
88 
89   virtual void OnCameraData(const std::vector<char>& data) = 0;
90 };
91 
92 class ConnectionObserverFactory {
93  public:
94   virtual ~ConnectionObserverFactory() = default;
95   // Called when a new connection is requested
96   virtual std::shared_ptr<ConnectionObserver> CreateObserver() = 0;
97 };
98 
99 }  // namespace webrtc_streaming
100 }  // namespace cuttlefish
101