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