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 ClientHandler : public webrtc::PeerConnectionObserver,
37                       public std::enable_shared_from_this<ClientHandler> {
38  public:
39   static std::shared_ptr<ClientHandler> Create(
40       int client_id, std::shared_ptr<ConnectionObserver> observer,
41       std::function<void(const Json::Value&)> send_client_cb,
42       std::function<void()> on_connection_closed_cb);
43   ~ClientHandler() override;
44 
45   bool SetPeerConnection(
46       rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection);
47 
48   bool AddDisplay(rtc::scoped_refptr<webrtc::VideoTrackInterface> track,
49                   const std::string& label);
50 
51   void HandleMessage(const Json::Value& client_message);
52 
53   // CreateSessionDescriptionObserver implementation
54   void OnCreateSDPSuccess(webrtc::SessionDescriptionInterface* desc);
55   void OnCreateSDPFailure(webrtc::RTCError error);
56 
57   // SetSessionDescriptionObserver implementation
58   void OnSetSDPFailure(webrtc::RTCError error);
59 
60   // PeerConnectionObserver implementation
61   void OnSignalingChange(
62       webrtc::PeerConnectionInterface::SignalingState new_state) override;
63   void OnDataChannel(
64       rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override;
65   void OnRenegotiationNeeded() override;
66   void OnStandardizedIceConnectionChange(
67       webrtc::PeerConnectionInterface::IceConnectionState new_state) override;
68   void OnConnectionChange(
69       webrtc::PeerConnectionInterface::PeerConnectionState new_state) override;
70   void OnIceGatheringChange(
71       webrtc::PeerConnectionInterface::IceGatheringState new_state) override;
72   void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
73   // Gathering of an ICE candidate failed.
74   // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror
75   // |host_candidate| is a stringified socket address.
76   void OnIceCandidateError(const std::string& host_candidate,
77                            const std::string& url, int error_code,
78                            const std::string& error_text) override;
79   // Gathering of an ICE candidate failed.
80   // See https://w3c.github.io/webrtc-pc/#event-icecandidateerror
81   void OnIceCandidateError(const std::string& address, int port,
82                            const std::string& url, int error_code,
83                            const std::string& error_text) override;
84   void OnIceCandidatesRemoved(
85       const std::vector<cricket::Candidate>& candidates) override;
86   void OnTrack(
87       rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override;
88   void OnRemoveTrack(
89       rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override;
90 
91  private:
92   class InputHandler : public webrtc::DataChannelObserver {
93    public:
94     InputHandler(rtc::scoped_refptr<webrtc::DataChannelInterface> input_channel,
95                  std::shared_ptr<ConnectionObserver> observer);
96     ~InputHandler() override;
97 
98     void OnStateChange() override;
99     void OnMessage(const webrtc::DataBuffer& msg) override;
100 
101    private:
102     rtc::scoped_refptr<webrtc::DataChannelInterface> input_channel_;
103     std::shared_ptr<ConnectionObserver> observer_;
104   };
105   class AdbHandler : public webrtc::DataChannelObserver {
106    public:
107     AdbHandler(rtc::scoped_refptr<webrtc::DataChannelInterface> input_channel,
108                  std::shared_ptr<ConnectionObserver> observer);
109     ~AdbHandler() override;
110 
111     void OnStateChange() override;
112     void OnMessage(const webrtc::DataBuffer& msg) override;
113 
114    private:
115     rtc::scoped_refptr<webrtc::DataChannelInterface> adb_channel_;
116     std::shared_ptr<ConnectionObserver> observer_;
117     bool channel_open_reported_ = false;
118   };
119 
120   ClientHandler(int client_id, std::shared_ptr<ConnectionObserver> observer,
121                 std::function<void(const Json::Value&)> send_client_cb,
122                 std::function<void()> on_connection_closed_cb);
123 
124   // Intentionally private, disconnect the client by destroying the object.
125   void Close();
126 
127   void LogAndReplyError(const std::string& error_msg) const;
128 
129   int client_id_;
130   std::shared_ptr<ConnectionObserver> observer_;
131   std::function<void(const Json::Value&)> send_to_client_;
132   std::function<void()> on_connection_closed_cb_;
133   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
134   std::vector<rtc::scoped_refptr<webrtc::DataChannelInterface>> data_channels_;
135   std::unique_ptr<InputHandler> input_handler_;
136   std::unique_ptr<AdbHandler> adb_handler_;
137 };
138 
139 }  // namespace webrtc_streaming
140 }  // namespace cuttlefish
141