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