Android-cuttlefish cvd tool
connection_controller.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 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 <memory>
20#include <mutex>
21
22#include <api/peer_connection_interface.h>
23#include <json/json.h>
24
27
28namespace cuttlefish {
29namespace webrtc_streaming {
30
31// Creating the peer connection is different on the client and device, but for
32// both the pc needs to be created during the signaling process.
34 public:
35 virtual ~PeerConnectionBuilder() = default;
37 webrtc::PeerConnectionObserver& observer,
38 const std::vector<webrtc::PeerConnectionInterface::IceServer>&
39 per_connection_servers) = 0;
40};
41
42// Encapsulates the signaling protocol, which is mostly the same for client and
43// device. Devices will create a connection controller for each new client and
44// simply provide implementations of the callbacks in
45// ConnectionController::Observer. Clients must additionally call RequestOffer
46// to start the signaling process.
47class ConnectionController : public webrtc::PeerConnectionObserver {
48 public:
49 // These callbacks will be called from the signaling thread. Implementations
50 // should return as soon as possible, particularly not blocking on IO.
51 // Implementations must never destroy the ConnectionController object from
52 // inside these callbacks as that would lead to undefined behavior.
53 // TODO (b/240578845): This avoids having to create an extra thread per
54 // client just to monitor changes in the device side, but opens problems by
55 // allowing it to react to state changes on a peer connection callback. The
56 // device already has code to avoid these issues, but the ideal solution
57 // would be for this callback to be posted to a thread or not to be used at
58 // all.
59 class Observer {
60 public:
61 virtual ~Observer() = default;
64 status) = 0;
65
66 // Called when new media tracks are added to the peer connection.
67 virtual void OnTrack(
68 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) = 0;
69 // Called when media tracks are removed from the peer connection.
70 virtual void OnRemoveTrack(
71 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) = 0;
72 // Called when a data channel is added to the peer connection.
73 virtual void OnDataChannel(
74 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) = 0;
75 };
76
78 PeerConnectionBuilder& connection_builder,
79 Observer& observer);
80 ~ConnectionController() override = default;
81
82 // Sends a request-offer message to the peer to kickstart the signaling
83 // process.
85 const std::vector<webrtc::PeerConnectionInterface::IceServer>&
86 ice_servers);
87
88 // This class doesn't poll for signaling messages from the server, instead
89 // users must do that themselves and provide them here for the connection
90 // controller to process them. As the result of this processing some callbacks
91 // may be called or new messages may be sent to the peer, most likely after
92 // the function returns, but that's not guaranteed.
93 void HandleSignalingMessage(const Json::Value& msg);
94
95 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection() {
96 return peer_connection_;
97 }
98
99 // webrtc::PeerConnectionObserver implementation
101 webrtc::PeerConnectionInterface::SignalingState new_state) override;
102 void OnAddStream(
103 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;
104 void OnRemoveStream(
105 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override;
106 void OnDataChannel(
107 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override;
108 void OnRenegotiationNeeded() override;
110 webrtc::PeerConnectionInterface::IceConnectionState new_state) override;
112 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override;
114 webrtc::PeerConnectionInterface::IceGatheringState new_state) override;
115 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
116 void OnIceCandidateError(const std::string& address, int port,
117 const std::string& url, int error_code,
118 const std::string& error_text) override;
120 const std::vector<cricket::Candidate>& candidates) override;
121 // The following can be overridden but are not needed by either the device or
122 // client at the moement. void OnIceConnectionReceivingChange(bool receiving)
123 // override; void OnIceSelectedCandidatePairChanged( const
124 // cricket::CandidatePairChangeEvent& event) override; void OnAddTrack(
125 // rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
126 // const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
127 // streams) override;
128 // void OnInterestingUsage(int usage_pattern) override;
129 void OnTrack(
130 rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override;
131 void OnRemoveTrack(
132 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override;
133
134 private:
138 void CreateOffer();
140 void FailConnection(const std::string& message);
141
142 Result<void> HandleSignalingMessageInner(const Json::Value& message);
144 const std::vector<webrtc::PeerConnectionInterface::IceServer>&
145 ice_servers);
147 std::unique_ptr<webrtc::SessionDescriptionInterface> offer);
149 std::unique_ptr<webrtc::SessionDescriptionInterface> answer);
151 std::unique_ptr<webrtc::IceCandidateInterface> ice_candidate);
152 Result<void> OnErrorMsg(const std::string& msg);
153
154 webrtc::CreateSessionDescriptionObserver* ThisAsCreateSDPObserver();
155 webrtc::SetSessionDescriptionObserver* ThisAsSetSDPObserver();
156 rtc::scoped_refptr<webrtc::SetRemoteDescriptionObserverInterface>
158
159 Result<void> OnCreateSDPSuccess(webrtc::SessionDescriptionInterface* desc);
160 void OnCreateSDPFailure(const webrtc::RTCError& error);
162 void OnSetLocalDescriptionFailure(const webrtc::RTCError& error);
163 void OnSetRemoteDescriptionComplete(const webrtc::RTCError& error);
164
168
169 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
170 std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
172
173 // To await for a connection to be established:
174 std::mutex status_mtx_;
175 std::condition_variable status_cond_var_;
178};
179
180} // namespace webrtc_streaming
181} // namespace cuttlefish
Definition: expected.h:86
virtual void OnTrack(rtc::scoped_refptr< webrtc::RtpTransceiverInterface > transceiver)=0
virtual void OnRemoveTrack(rtc::scoped_refptr< webrtc::RtpReceiverInterface > receiver)=0
virtual void OnConnectionStateChange(Result< webrtc::PeerConnectionInterface::PeerConnectionState > status)=0
virtual void OnDataChannel(rtc::scoped_refptr< webrtc::DataChannelInterface > data_channel)=0
Definition: connection_controller.h:47
Observer & observer_
Definition: connection_controller.h:167
rtc::scoped_refptr< webrtc::PeerConnectionInterface > peer_connection_
Definition: connection_controller.h:169
Result< void > OnCreateSDPSuccess(webrtc::SessionDescriptionInterface *desc)
Definition: connection_controller.cpp:190
void OnIceCandidate(const webrtc::IceCandidateInterface *candidate) override
Definition: connection_controller.cpp:394
void OnDataChannel(rtc::scoped_refptr< webrtc::DataChannelInterface > data_channel) override
Definition: connection_controller.cpp:318
Result< void > OnOfferMsg(std::unique_ptr< webrtc::SessionDescriptionInterface > offer)
Definition: connection_controller.cpp:150
void FailConnection(const std::string &message)
Definition: connection_controller.cpp:116
rtc::scoped_refptr< webrtc::SetRemoteDescriptionObserverInterface > ThisAsSetRemoteSDPObserver()
Definition: connection_controller.cpp:258
std::condition_variable status_cond_var_
Definition: connection_controller.h:175
void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) override
Definition: connection_controller.cpp:300
std::vector< std::unique_ptr< webrtc::IceCandidateInterface > > pending_ice_candidates_
Definition: connection_controller.h:171
Result< void > OnAnswerMsg(std::unique_ptr< webrtc::SessionDescriptionInterface > answer)
Definition: connection_controller.cpp:157
void OnSetLocalDescriptionSuccess()
Definition: connection_controller.cpp:213
void OnIceCandidatesRemoved(const std::vector< cricket::Candidate > &candidates) override
Definition: connection_controller.cpp:425
webrtc::CreateSessionDescriptionObserver * ThisAsCreateSDPObserver()
Definition: connection_controller.cpp:248
std::mutex status_mtx_
Definition: connection_controller.h:174
void AddPendingIceCandidates()
Definition: connection_controller.cpp:127
void OnIceCandidateError(const std::string &address, int port, const std::string &url, int error_code, const std::string &error_text) override
Definition: connection_controller.cpp:415
webrtc::SetSessionDescriptionObserver * ThisAsSetSDPObserver()
Definition: connection_controller.cpp:253
Result< void > HandleSignalingMessageInner(const Json::Value &message)
Definition: connection_controller.cpp:272
void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) override
Definition: connection_controller.cpp:372
void OnTrack(rtc::scoped_refptr< webrtc::RtpTransceiverInterface > transceiver) override
Definition: connection_controller.cpp:438
void OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) override
Definition: connection_controller.cpp:333
Result< void > RequestOffer(const std::vector< webrtc::PeerConnectionInterface::IceServer > &ice_servers)
Definition: connection_controller.cpp:100
PeerSignalingHandler & sig_handler_
Definition: connection_controller.h:165
Result< void > OnIceCandidateMsg(std::unique_ptr< webrtc::IceCandidateInterface > ice_candidate)
Definition: connection_controller.cpp:164
Result< webrtc::PeerConnectionInterface::PeerConnectionState > connection_status_
Definition: connection_controller.h:177
void OnSetLocalDescriptionFailure(const webrtc::RTCError &error)
Definition: connection_controller.cpp:217
void HandleSignalingMessage(const Json::Value &msg)
Definition: connection_controller.cpp:264
void OnAddStream(rtc::scoped_refptr< webrtc::MediaStreamInterface > stream) override
Definition: connection_controller.cpp:306
void OnRemoveTrack(rtc::scoped_refptr< webrtc::RtpReceiverInterface > receiver) override
Definition: connection_controller.cpp:450
rtc::scoped_refptr< webrtc::PeerConnectionInterface > peer_connection()
Definition: connection_controller.h:95
PeerConnectionBuilder & connection_builder_
Definition: connection_controller.h:166
void OnRenegotiationNeeded() override
Definition: connection_controller.cpp:325
void OnSetRemoteDescriptionComplete(const webrtc::RTCError &error)
Definition: connection_controller.cpp:226
Result< void > OnOfferRequestMsg(const std::vector< webrtc::PeerConnectionInterface::IceServer > &ice_servers)
Definition: connection_controller.cpp:141
void OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState new_state) override
Definition: connection_controller.cpp:366
void OnRemoveStream(rtc::scoped_refptr< webrtc::MediaStreamInterface > stream) override
Definition: connection_controller.cpp:312
void OnCreateSDPFailure(const webrtc::RTCError &error)
Definition: connection_controller.cpp:208
ConnectionController(PeerSignalingHandler &sig_handler, PeerConnectionBuilder &connection_builder, Observer &observer)
Definition: connection_controller.cpp:86
Result< void > OnErrorMsg(const std::string &msg)
Definition: connection_controller.cpp:185
void CreateOffer()
Definition: connection_controller.cpp:94
Definition: connection_controller.h:33
virtual Result< rtc::scoped_refptr< webrtc::PeerConnectionInterface > > Build(webrtc::PeerConnectionObserver &observer, const std::vector< webrtc::PeerConnectionInterface::IceServer > &per_connection_servers)=0
Definition: peer_signaling_handler.h:29
#define error(format, args...)
Definition: fec_private.h:201
int status()
Definition: health.cpp:42
Definition: alloc_utils.cpp:23