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   // Path of ControlEnvProxyServer for serving Rest API in WebUI.
61   std::string control_env_proxy_server_path;
62 };
63 
64 class OperatorObserver {
65  public:
66   virtual ~OperatorObserver() = default;
67   // Called when the websocket connection with the operator is established.
68   virtual void OnRegistered() = 0;
69   // Called when the websocket connection with the operator is closed.
70   virtual void OnClose() = 0;
71   // Called when an error is encountered in the connection to the operator.
72   virtual void OnError() = 0;
73 };
74 
75 class Streamer {
76  public:
77   // The observer_factory will be used to create an observer for every new
78   // client connection. Unregister() needs to be called to stop accepting
79   // connections.
80   static std::unique_ptr<Streamer> Create(
81       const StreamerConfig& cfg, RecordingManager& recording_manager,
82       std::shared_ptr<ConnectionObserverFactory> factory);
83   ~Streamer() = default;
84 
85   std::shared_ptr<VideoSink> AddDisplay(const std::string& label, int width,
86                                         int height, int dpi,
87                                         bool touch_enabled);
88   bool RemoveDisplay(const std::string& label);
89 
90   bool AddTouchpad(const std::string& label, int width, int height);
91 
92   void SetHardwareSpec(std::string key, std::string value);
93 
94   template <typename V>
SetHardwareSpec(std::string key,V value)95   void SetHardwareSpec(std::string key, V value) {
96     SetHardwareSpec(key, std::to_string(value));
97   }
98 
99   std::shared_ptr<AudioSink> AddAudioStream(const std::string& label);
100   // Grants access to streams originating on the client side. If there are
101   // multiple streams (either because one client sends more than one or there
102   // are several clients) the audio will be mixed and provided as a single
103   // stream here.
104   std::shared_ptr<AudioSource> GetAudioSource();
105 
106   CameraController* AddCamera(unsigned int port, unsigned int cid,
107                               bool vhost_user);
108 
109   // Add a custom button to the control panel.
110   void AddCustomControlPanelButton(const std::string& command,
111                                    const std::string& title,
112                                    const std::string& icon_name);
113   void AddCustomControlPanelButtonWithShellCommand(
114       const std::string& command, const std::string& title,
115       const std::string& icon_name, const std::string& shell_command);
116   void AddCustomControlPanelButtonWithDeviceStates(
117       const std::string& command, const std::string& title,
118       const std::string& icon_name,
119       const std::vector<DeviceState>& device_states);
120 
121   // Register with the operator.
122   void Register(std::weak_ptr<OperatorObserver> operator_observer);
123   void Unregister();
124 
125  private:
126   /*
127    * Private Implementation idiom.
128    * https://en.cppreference.com/w/cpp/language/pimpl
129    */
130   class Impl;
131 
132   Streamer(std::unique_ptr<Impl> impl);
133   std::shared_ptr<Impl> impl_;
134 };
135 
136 }  // namespace webrtc_streaming
137 }  // namespace cuttlefish
138