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 #include <memory>
21 #include <mutex>
22 #include <optional>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "host/libs/config/custom_actions.h"
28 
29 #include "host/frontend/webrtc/libcommon/audio_source.h"
30 #include "host/frontend/webrtc/libdevice/audio_sink.h"
31 #include "host/frontend/webrtc/libdevice/camera_controller.h"
32 #include "host/frontend/webrtc/libdevice/connection_observer.h"
33 #include "host/frontend/webrtc/libdevice/recording_manager.h"
34 #include "host/frontend/webrtc/libdevice/server_connection.h"
35 #include "host/frontend/webrtc/libdevice/video_sink.h"
36 
37 namespace cuttlefish {
38 namespace webrtc_streaming {
39 
40 class ClientHandler;
41 
42 struct StreamerConfig {
43   // The id with which to register with the operator server.
44   std::string device_id;
45 
46   // The group id with which to register with the operator server.
47   std::string group_id;
48 
49   // The port on which the client files are being served
50   int client_files_port;
51   ServerConfig operator_server;
52   // The port ranges webrtc is allowed to use.
53   // [0,0] means all ports
54   std::pair<uint16_t, uint16_t> udp_port_range = {15550, 15599};
55   std::pair<uint16_t, uint16_t> tcp_port_range = {15550, 15599};
56   // WebRTC device id obtaining openwrt instance.
57   std::string openwrt_device_id;
58   // Openwrt IP address for accessing Luci interface.
59   std::string openwrt_addr;
60   // Adb port number of the device.
61   int adb_port;
62   // Path of ControlEnvProxyServer for serving Rest API in WebUI.
63   std::string control_env_proxy_server_path;
64   // Whether mouse is enabled.
65   bool enable_mouse;
66 };
67 
68 class OperatorObserver {
69  public:
70   virtual ~OperatorObserver() = default;
71   // Called when the websocket connection with the operator is established.
72   virtual void OnRegistered() = 0;
73   // Called when the websocket connection with the operator is closed.
74   virtual void OnClose() = 0;
75   // Called when an error is encountered in the connection to the operator.
76   virtual void OnError() = 0;
77 };
78 
79 class Streamer {
80  public:
81   // The observer_factory will be used to create an observer for every new
82   // client connection. Unregister() needs to be called to stop accepting
83   // connections.
84   static std::unique_ptr<Streamer> Create(
85       const StreamerConfig& cfg, RecordingManager& recording_manager,
86       std::shared_ptr<ConnectionObserverFactory> factory);
87   ~Streamer() = default;
88 
89   std::shared_ptr<VideoSink> AddDisplay(const std::string& label, int width,
90                                         int height, int dpi,
91                                         bool touch_enabled);
92   bool RemoveDisplay(const std::string& label);
93 
94   bool AddTouchpad(const std::string& label, int width, int height);
95 
96   void SetHardwareSpec(std::string key, std::string value);
97 
98   template <typename V>
SetHardwareSpec(std::string key,V value)99   void SetHardwareSpec(std::string key, V value) {
100     SetHardwareSpec(key, std::to_string(value));
101   }
102 
103   std::shared_ptr<AudioSink> AddAudioStream(const std::string& label);
104   // Grants access to streams originating on the client side. If there are
105   // multiple streams (either because one client sends more than one or there
106   // are several clients) the audio will be mixed and provided as a single
107   // stream here.
108   std::shared_ptr<AudioSource> GetAudioSource();
109 
110   CameraController* AddCamera(unsigned int port, unsigned int cid,
111                               bool vhost_user);
112 
113   // Add a custom button to the control panel.
114   void AddCustomControlPanelButton(const std::string& command,
115                                    const std::string& title,
116                                    const std::string& icon_name);
117   void AddCustomControlPanelButtonWithShellCommand(
118       const std::string& command, const std::string& title,
119       const std::string& icon_name, const std::string& shell_command);
120   void AddCustomControlPanelButtonWithDeviceStates(
121       const std::string& command, const std::string& title,
122       const std::string& icon_name,
123       const std::vector<DeviceState>& device_states);
124 
125   // Register with the operator.
126   void Register(std::weak_ptr<OperatorObserver> operator_observer);
127   void Unregister();
128 
129  private:
130   /*
131    * Private Implementation idiom.
132    * https://en.cppreference.com/w/cpp/language/pimpl
133    */
134   class Impl;
135 
136   Streamer(std::unique_ptr<Impl> impl);
137   std::shared_ptr<Impl> impl_;
138 };
139 
140 }  // namespace webrtc_streaming
141 }  // namespace cuttlefish
142