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/local_recorder.h" 34 #include "host/frontend/webrtc/libdevice/video_sink.h" 35 #include "host/frontend/webrtc/libdevice/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, 15599}; 51 std::pair<uint16_t, uint16_t> tcp_port_range = {15550, 15599}; 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, LocalRecorder* recorder, 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 bool RemoveDisplay(const std::string& label); 79 80 void SetHardwareSpec(std::string key, std::string value); 81 82 template <typename V> SetHardwareSpec(std::string key,V value)83 void SetHardwareSpec(std::string key, V value) { 84 SetHardwareSpec(key, std::to_string(value)); 85 } 86 87 std::shared_ptr<AudioSink> AddAudioStream(const std::string& label); 88 // Grants access to streams originating on the client side. If there are 89 // multiple streams (either because one client sends more than one or there 90 // are several clients) the audio will be mixed and provided as a single 91 // stream here. 92 std::shared_ptr<AudioSource> GetAudioSource(); 93 94 CameraController* AddCamera(unsigned int port, unsigned int cid); 95 96 // Add a custom button to the control panel. 97 void AddCustomControlPanelButton(const std::string& command, 98 const std::string& title, 99 const std::string& icon_name); 100 void AddCustomControlPanelButtonWithShellCommand( 101 const std::string& command, const std::string& title, 102 const std::string& icon_name, const std::string& shell_command); 103 void AddCustomControlPanelButtonWithDeviceStates( 104 const std::string& command, const std::string& title, 105 const std::string& icon_name, 106 const std::vector<DeviceState>& device_states); 107 108 // Register with the operator. 109 void Register(std::weak_ptr<OperatorObserver> operator_observer); 110 void Unregister(); 111 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