• 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 #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/lib/audio_sink.h"
30 #include "host/frontend/webrtc/lib/audio_source.h"
31 #include "host/frontend/webrtc/lib/connection_observer.h"
32 #include "host/frontend/webrtc/lib/local_recorder.h"
33 #include "host/frontend/webrtc/lib/video_sink.h"
34 #include "host/frontend/webrtc/lib/ws_connection.h"
35 
36 namespace cuttlefish {
37 namespace webrtc_streaming {
38 
39 class ClientHandler;
40 
41 struct StreamerConfig {
42   // The id with which to register with the operator server.
43   std::string device_id;
44   struct {
45     // The ip address or domain name of the operator server.
46     std::string addr;
47     int port;
48     // The path component of the operator server's register url.
49     std::string path;
50     // The security level to use when connecting to the operator server.
51     WsConnection::Security security;
52     // A list of key value pairs to include as HTTP handshake headers when
53     // connecting to the operator.
54     std::vector<std::pair<std::string, std::string>> http_headers;
55   } operator_server;
56   // The port ranges webrtc is allowed to use.
57   // [0,0] means all ports
58   std::pair<uint16_t, uint16_t> udp_port_range = {15550, 15558};
59   std::pair<uint16_t, uint16_t> tcp_port_range = {15550, 15558};
60 };
61 
62 class OperatorObserver {
63  public:
64   virtual ~OperatorObserver() = default;
65   // Called when the websocket connection with the operator is established.
66   virtual void OnRegistered() = 0;
67   // Called when the websocket connection with the operator is closed.
68   virtual void OnClose() = 0;
69   // Called when an error is encountered in the connection to the operator.
70   virtual void OnError() = 0;
71 };
72 
73 class Streamer {
74  public:
75   // The observer_factory will be used to create an observer for every new
76   // client connection. Unregister() needs to be called to stop accepting
77   // connections.
78   static std::unique_ptr<Streamer> Create(
79       const StreamerConfig& cfg,
80       std::shared_ptr<ConnectionObserverFactory> factory);
81   ~Streamer() = default;
82 
83   std::shared_ptr<VideoSink> AddDisplay(const std::string& label, int width,
84                                         int height, int dpi,
85                                         bool touch_enabled);
86 
87   void SetHardwareSpec(std::string key, std::string value);
88 
89   template <typename V>
SetHardwareSpec(std::string key,V value)90   void SetHardwareSpec(std::string key, V value) {
91     SetHardwareSpec(key, std::to_string(value));
92   }
93 
94   std::shared_ptr<AudioSink> AddAudioStream(const std::string& label);
95   // Grants access to streams originating on the client side. If there are
96   // multiple streams (either because one client sends more than one or there
97   // are several clients) the audio will be mixed and provided as a single
98   // stream here.
99   std::shared_ptr<AudioSource> GetAudioSource();
100 
101   // Add a custom button to the control panel.
102   void AddCustomControlPanelButton(const std::string& command,
103                                    const std::string& title,
104                                    const std::string& icon_name);
105   void AddCustomControlPanelButtonWithShellCommand(
106       const std::string& command, const std::string& title,
107       const std::string& icon_name, const std::string& shell_command);
108   void AddCustomControlPanelButtonWithDeviceStates(
109       const std::string& command, const std::string& title,
110       const std::string& icon_name,
111       const std::vector<DeviceState>& device_states);
112 
113   // Register with the operator.
114   void Register(std::weak_ptr<OperatorObserver> operator_observer);
115   void Unregister();
116 
117   void RecordDisplays(LocalRecorder& recorder);
118  private:
119   /*
120    * Private Implementation idiom.
121    * https://en.cppreference.com/w/cpp/language/pimpl
122    */
123   class Impl;
124 
125   Streamer(std::unique_ptr<Impl> impl);
126   std::shared_ptr<Impl> impl_;
127 };
128 
129 }  // namespace webrtc_streaming
130 }  // namespace cuttlefish
131