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