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 <optional> 22 #include <sstream> 23 #include <string> 24 #include <utility> 25 #include <vector> 26 27 #include <json/json.h> 28 29 #include <api/peer_connection_interface.h> 30 #include <pc/video_track_source.h> 31 32 #include "common/libs/utils/result.h" 33 #include "host/frontend/webrtc/libcommon/connection_controller.h" 34 #include "host/frontend/webrtc/libdevice/data_channels.h" 35 #include "host/frontend/webrtc/libdevice/connection_observer.h" 36 37 namespace cuttlefish { 38 namespace webrtc_streaming { 39 40 class InputChannelHandler; 41 class AdbChannelHandler; 42 class ControlChannelHandler; 43 class BluetoothChannelHandler; 44 class CameraChannelHandler; 45 class LocationChannelHandler; 46 class KmlLocationsChannelHandler; 47 class GpxLocationsChannelHandler; 48 49 class ClientVideoTrackInterface; 50 class ClientVideoTrackImpl; 51 class PeerConnectionBuilder; 52 53 class ClientHandler : public ConnectionController::Observer, 54 public PeerConnectionBuilder, 55 public PeerSignalingHandler { 56 public: 57 static std::shared_ptr<ClientHandler> Create( 58 int client_id, std::shared_ptr<ConnectionObserver> observer, 59 PeerConnectionBuilder& connection_builder, 60 std::function<void(const Json::Value&)> send_client_cb, 61 std::function<void(bool)> on_connection_changed_cb); 62 ~ClientHandler() override = default; 63 64 bool AddDisplay(rtc::scoped_refptr<webrtc::VideoTrackInterface> track, 65 const std::string& label); 66 bool RemoveDisplay(const std::string& label); 67 68 bool AddAudio(rtc::scoped_refptr<webrtc::AudioTrackInterface> track, 69 const std::string& label); 70 71 ClientVideoTrackInterface* GetCameraStream(); 72 73 void HandleMessage(const Json::Value& client_message); 74 75 // ConnectionController::Observer implementation 76 void OnConnectionStateChange( 77 Result<webrtc::PeerConnectionInterface::PeerConnectionState> status) override; 78 void OnDataChannel( 79 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override; 80 void OnTrack( 81 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override; 82 void OnRemoveTrack( 83 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override; 84 85 // PeerSignalingHandling implementation 86 Result<void> SendMessage(const Json::Value& msg) override; 87 88 // PeerConnectionBuilder implementation 89 // Delegates on its own pc builder to create the pc and then adds the displays 90 // and other streams as required. 91 Result<rtc::scoped_refptr<webrtc::PeerConnectionInterface>> Build( 92 webrtc::PeerConnectionObserver& observer, 93 const std::vector<webrtc::PeerConnectionInterface::IceServer>& 94 per_connection_servers) override; 95 96 private: 97 ClientHandler(int client_id, std::shared_ptr<ConnectionObserver> observer, 98 PeerConnectionBuilder& connection_builder, 99 std::function<void(const Json::Value&)> send_client_cb, 100 std::function<void(bool)> on_connection_changed_cb); 101 102 // Intentionally private, disconnect the client by destroying the object. 103 void Close(); 104 105 void LogAndReplyError(const std::string& error_msg) const; 106 Result<void> CreateOffer(); 107 rtc::scoped_refptr<webrtc::RtpSenderInterface> AddTrackToConnection( 108 rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track, 109 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection, 110 const std::string& label); 111 112 int client_id_; 113 std::shared_ptr<ConnectionObserver> observer_; 114 std::function<void(const Json::Value&)> send_to_client_; 115 std::function<void(bool)> on_connection_changed_cb_; 116 PeerConnectionBuilder& connection_builder_; 117 ConnectionController controller_; 118 DataChannelHandlers data_channels_handler_; 119 std::unique_ptr<ClientVideoTrackImpl> camera_track_; 120 struct DisplayTrackAndSender { 121 rtc::scoped_refptr<webrtc::VideoTrackInterface> track; 122 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender; 123 }; 124 std::map<std::string, DisplayTrackAndSender> displays_; 125 std::vector< 126 std::pair<rtc::scoped_refptr<webrtc::AudioTrackInterface>, std::string>> 127 audio_streams_; 128 }; 129 130 class ClientVideoTrackInterface { 131 public: 132 virtual ~ClientVideoTrackInterface() = default; 133 virtual void AddOrUpdateSink( 134 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, 135 const rtc::VideoSinkWants& wants) = 0; 136 }; 137 138 } // namespace webrtc_streaming 139 } // namespace cuttlefish 140