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 "host/frontend/webrtc/lib/connection_observer.h" 33 34 namespace cuttlefish { 35 namespace webrtc_streaming { 36 37 class InputChannelHandler; 38 class AdbChannelHandler; 39 class ControlChannelHandler; 40 class BluetoothChannelHandler; 41 class CameraChannelHandler; 42 43 class ClientVideoTrackInterface; 44 class ClientVideoTrackImpl; 45 46 class ClientHandler : public webrtc::PeerConnectionObserver, 47 public std::enable_shared_from_this<ClientHandler> { 48 public: 49 static std::shared_ptr<ClientHandler> Create( 50 int client_id, std::shared_ptr<ConnectionObserver> observer, 51 std::function<void(const Json::Value&)> send_client_cb, 52 std::function<void(bool)> on_connection_changed_cb); 53 ~ClientHandler() override; 54 55 bool SetPeerConnection( 56 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection); 57 58 bool AddDisplay(rtc::scoped_refptr<webrtc::VideoTrackInterface> track, 59 const std::string& label); 60 61 bool AddAudio(rtc::scoped_refptr<webrtc::AudioTrackInterface> track, 62 const std::string& label); 63 64 ClientVideoTrackInterface* GetCameraStream(); 65 66 void HandleMessage(const Json::Value& client_message); 67 68 // CreateSessionDescriptionObserver implementation 69 void OnCreateSDPSuccess(webrtc::SessionDescriptionInterface* desc); 70 void OnCreateSDPFailure(webrtc::RTCError error); 71 72 // SetSessionDescriptionObserver implementation 73 void OnSetSDPFailure(webrtc::RTCError error); 74 75 // PeerConnectionObserver implementation 76 void OnSignalingChange( 77 webrtc::PeerConnectionInterface::SignalingState new_state) override; 78 void OnDataChannel( 79 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override; 80 void OnRenegotiationNeeded() override; 81 void OnStandardizedIceConnectionChange( 82 webrtc::PeerConnectionInterface::IceConnectionState new_state) override; 83 void OnConnectionChange( 84 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override; 85 void OnIceGatheringChange( 86 webrtc::PeerConnectionInterface::IceGatheringState new_state) override; 87 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; 88 // Gathering of an ICE candidate failed. 89 // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror 90 // |host_candidate| is a stringified socket address. 91 void OnIceCandidateError(const std::string& host_candidate, 92 const std::string& url, int error_code, 93 const std::string& error_text) override; 94 // Gathering of an ICE candidate failed. 95 // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror 96 void OnIceCandidateError(const std::string& address, int port, 97 const std::string& url, int error_code, 98 const std::string& error_text) override; 99 void OnIceCandidatesRemoved( 100 const std::vector<cricket::Candidate>& candidates) override; 101 void OnTrack( 102 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override; 103 void OnRemoveTrack( 104 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override; 105 106 private: 107 enum class State { 108 kNew, 109 kCreatingOffer, 110 kAwaitingAnswer, 111 kConnecting, 112 kConnected, 113 kFailed, 114 }; 115 ClientHandler(int client_id, std::shared_ptr<ConnectionObserver> observer, 116 std::function<void(const Json::Value&)> send_client_cb, 117 std::function<void(bool)> on_connection_changed_cb); 118 119 // Intentionally private, disconnect the client by destroying the object. 120 void Close(); 121 122 void LogAndReplyError(const std::string& error_msg) const; 123 void AddPendingIceCandidates(); 124 125 int client_id_; 126 State state_ = State::kNew; 127 std::shared_ptr<ConnectionObserver> observer_; 128 std::function<void(const Json::Value&)> send_to_client_; 129 std::function<void(bool)> on_connection_changed_cb_; 130 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; 131 std::vector<rtc::scoped_refptr<webrtc::DataChannelInterface>> data_channels_; 132 std::unique_ptr<InputChannelHandler> input_handler_; 133 std::unique_ptr<AdbChannelHandler> adb_handler_; 134 std::unique_ptr<ControlChannelHandler> control_handler_; 135 std::unique_ptr<BluetoothChannelHandler> bluetooth_handler_; 136 std::unique_ptr<CameraChannelHandler> camera_data_handler_; 137 std::unique_ptr<ClientVideoTrackImpl> camera_track_; 138 bool remote_description_added_ = false; 139 std::vector<std::unique_ptr<webrtc::IceCandidateInterface>> 140 pending_ice_candidates_; 141 }; 142 143 class ClientVideoTrackInterface { 144 public: 145 virtual ~ClientVideoTrackInterface() = default; 146 virtual void AddOrUpdateSink( 147 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, 148 const rtc::VideoSinkWants& wants) = 0; 149 }; 150 151 } // namespace webrtc_streaming 152 } // namespace cuttlefish 153