/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include #include #include "host/frontend/webrtc/lib/connection_observer.h" namespace cuttlefish { namespace webrtc_streaming { class ClientHandler : public webrtc::PeerConnectionObserver, public std::enable_shared_from_this { public: static std::shared_ptr Create( int client_id, std::shared_ptr observer, std::function send_client_cb, std::function on_connection_closed_cb); ~ClientHandler() override; bool SetPeerConnection( rtc::scoped_refptr peer_connection); bool AddDisplay(rtc::scoped_refptr track, const std::string& label); void HandleMessage(const Json::Value& client_message); // CreateSessionDescriptionObserver implementation void OnCreateSDPSuccess(webrtc::SessionDescriptionInterface* desc); void OnCreateSDPFailure(webrtc::RTCError error); // SetSessionDescriptionObserver implementation void OnSetSDPFailure(webrtc::RTCError error); // PeerConnectionObserver implementation void OnSignalingChange( webrtc::PeerConnectionInterface::SignalingState new_state) override; void OnDataChannel( rtc::scoped_refptr data_channel) override; void OnRenegotiationNeeded() override; void OnStandardizedIceConnectionChange( webrtc::PeerConnectionInterface::IceConnectionState new_state) override; void OnConnectionChange( webrtc::PeerConnectionInterface::PeerConnectionState new_state) override; void OnIceGatheringChange( webrtc::PeerConnectionInterface::IceGatheringState new_state) override; void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override; // Gathering of an ICE candidate failed. // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror // |host_candidate| is a stringified socket address. void OnIceCandidateError(const std::string& host_candidate, const std::string& url, int error_code, const std::string& error_text) override; // Gathering of an ICE candidate failed. // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror void OnIceCandidateError(const std::string& address, int port, const std::string& url, int error_code, const std::string& error_text) override; void OnIceCandidatesRemoved( const std::vector& candidates) override; void OnTrack( rtc::scoped_refptr transceiver) override; void OnRemoveTrack( rtc::scoped_refptr receiver) override; private: class InputHandler : public webrtc::DataChannelObserver { public: InputHandler(rtc::scoped_refptr input_channel, std::shared_ptr observer); ~InputHandler() override; void OnStateChange() override; void OnMessage(const webrtc::DataBuffer& msg) override; private: rtc::scoped_refptr input_channel_; std::shared_ptr observer_; }; class AdbHandler : public webrtc::DataChannelObserver { public: AdbHandler(rtc::scoped_refptr input_channel, std::shared_ptr observer); ~AdbHandler() override; void OnStateChange() override; void OnMessage(const webrtc::DataBuffer& msg) override; private: rtc::scoped_refptr adb_channel_; std::shared_ptr observer_; bool channel_open_reported_ = false; }; ClientHandler(int client_id, std::shared_ptr observer, std::function send_client_cb, std::function on_connection_closed_cb); // Intentionally private, disconnect the client by destroying the object. void Close(); void LogAndReplyError(const std::string& error_msg) const; int client_id_; std::shared_ptr observer_; std::function send_to_client_; std::function on_connection_closed_cb_; rtc::scoped_refptr peer_connection_; std::vector> data_channels_; std::unique_ptr input_handler_; std::unique_ptr adb_handler_; }; } // namespace webrtc_streaming } // namespace cuttlefish