Android-cuttlefish cvd tool
client_handler.h
Go to the documentation of this file.
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
36
37namespace cuttlefish {
38namespace webrtc_streaming {
39
40class InputChannelHandler;
41class AdbChannelHandler;
42class ControlChannelHandler;
43class BluetoothChannelHandler;
44class CameraChannelHandler;
45class SensorsChannelHandler;
46class LocationChannelHandler;
47class KmlLocationsChannelHandler;
48class GpxLocationsChannelHandler;
49
50class ClientVideoTrackInterface;
51class ClientVideoTrackImpl;
52class PeerConnectionBuilder;
53
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_to_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
73
74 void HandleMessage(const Json::Value& client_message);
75
76 // ConnectionController::Observer implementation
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.
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_to_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
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_;
120 std::unique_ptr<ClientVideoTrackImpl> camera_track_;
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>>
129};
130
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
Definition: expected.h:86
Definition: client_handler.h:56
bool AddAudio(rtc::scoped_refptr< webrtc::AudioTrackInterface > track, const std::string &label)
Definition: client_handler.cpp:143
Result< rtc::scoped_refptr< webrtc::PeerConnectionInterface > > Build(webrtc::PeerConnectionObserver &observer, const std::vector< webrtc::PeerConnectionInterface::IceServer > &per_connection_servers) override
Definition: client_handler.cpp:165
Result< void > SendMessage(const Json::Value &msg) override
Definition: client_handler.cpp:159
std::function< void(const Json::Value &)> send_to_client_
Definition: client_handler.h:115
ClientVideoTrackInterface * GetCameraStream()
Definition: client_handler.cpp:155
static std::shared_ptr< ClientHandler > Create(int client_id, std::shared_ptr< ConnectionObserver > observer, PeerConnectionBuilder &connection_builder, std::function< void(const Json::Value &)> send_to_client_cb, std::function< void(bool)> on_connection_changed_cb)
Definition: client_handler.cpp:64
std::vector< std::pair< rtc::scoped_refptr< webrtc::AudioTrackInterface >, std::string > > audio_streams_
Definition: client_handler.h:128
PeerConnectionBuilder & connection_builder_
Definition: client_handler.h:117
void OnRemoveTrack(rtc::scoped_refptr< webrtc::RtpReceiverInterface > receiver) override
Definition: client_handler.cpp:271
ConnectionController controller_
Definition: client_handler.h:118
bool AddDisplay(rtc::scoped_refptr< webrtc::VideoTrackInterface > track, const std::string &label)
Definition: client_handler.cpp:106
std::unique_ptr< ClientVideoTrackImpl > camera_track_
Definition: client_handler.h:120
void Close()
Definition: client_handler.cpp:212
void HandleMessage(const Json::Value &client_message)
Definition: client_handler.cpp:208
std::shared_ptr< ConnectionObserver > observer_
Definition: client_handler.h:114
void LogAndReplyError(const std::string &error_msg) const
void OnConnectionStateChange(Result< webrtc::PeerConnectionInterface::PeerConnectionState > new_state) override
Definition: client_handler.cpp:222
ClientHandler(int client_id, std::shared_ptr< ConnectionObserver > observer, PeerConnectionBuilder &connection_builder, std::function< void(const Json::Value &)> send_to_client_cb, std::function< void(bool)> on_connection_changed_cb)
Definition: client_handler.cpp:74
void OnDataChannel(rtc::scoped_refptr< webrtc::DataChannelInterface > data_channel) override
Definition: client_handler.cpp:256
rtc::scoped_refptr< webrtc::RtpSenderInterface > AddTrackToConnection(rtc::scoped_refptr< webrtc::MediaStreamTrackInterface > track, rtc::scoped_refptr< webrtc::PeerConnectionInterface > peer_connection, const std::string &label)
Definition: client_handler.cpp:89
std::function< void(bool)> on_connection_changed_cb_
Definition: client_handler.h:116
bool RemoveDisplay(const std::string &label)
Definition: client_handler.cpp:122
int client_id_
Definition: client_handler.h:113
std::map< std::string, DisplayTrackAndSender > displays_
Definition: client_handler.h:125
DataChannelHandlers data_channels_handler_
Definition: client_handler.h:119
void OnTrack(rtc::scoped_refptr< webrtc::RtpTransceiverInterface > transceiver) override
Definition: client_handler.cpp:261
virtual void AddOrUpdateSink(rtc::VideoSinkInterface< webrtc::VideoFrame > *sink, const rtc::VideoSinkWants &wants)=0
Definition: connection_controller.h:47
Definition: connection_controller.h:33
Definition: peer_signaling_handler.h:29
Definition: alloc_utils.cpp:23
rtc::scoped_refptr< webrtc::RtpSenderInterface > sender
Definition: client_handler.h:123
rtc::scoped_refptr< webrtc::VideoTrackInterface > track
Definition: client_handler.h:122