A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
webrtc_wire_stream.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
18#ifndef A11_NET_WEBRTC_WIRE_STREAM_H_
19#define A11_NET_WEBRTC_WIRE_STREAM_H_
20
21#include <cstddef>
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <string>
27#include <string_view>
28#include <utility>
29#include <vector>
30
31#include <absl/status/status.h>
32#include <absl/status/statusor.h>
33
36#include "a11/net/signalling.h"
37#include "a11/net/wire_stream.h"
38
39namespace rtc {
40class DataChannel;
41class PeerConnection;
42} // namespace rtc
43
44namespace a11::net {
45
47enum class TurnRelayType { kUdp, kTcp, kTls };
48
55struct TurnServer {
58 static absl::StatusOr<TurnServer> FromString(std::string_view value);
59
60 std::string hostname;
61 std::uint16_t port = 3478;
62 std::string username;
63 std::string password;
65
66 friend bool operator==(const TurnServer&, const TurnServer&) = default;
67};
68
76 // Large logical WireMessages are fragmented by A11 before reaching SCTP;
77 // this remains the advertised local libdatachannel message ceiling.
78 std::optional<size_t> max_message_size =
79 64 * 1024;
81 48 * 1024;
83 false;
84 std::vector<std::string> stun_servers;
85 std::vector<TurnServer> turn_servers;
87 std::optional<std::pair<std::uint16_t, std::uint16_t>> preferred_port_range;
88 std::optional<std::string>
90 // A stream stripes A11 packets across several data channels on one peer
91 // connection so slow per-channel acknowledgement round-trips overlap. This
92 // is an internal detail: the WireStream still behaves as one ordered,
93 // reliable channel. A dialing client opens and maintains `desired_channels`;
94 // an accepting server admits at most `max_channels` per peer.
95 size_t desired_channels = 8;
96 size_t max_channels = 8;
97
99 absl::Status Validate() const;
100};
101
111 private:
112 struct ConstructorToken {};
113
114 public:
126 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> CreateClient(
127 std::string identity, std::string peer_identity,
128 std::shared_ptr<SignallingService> signalling,
129 WebRtcConfiguration configuration = {}, WireStreamOptions options = {});
130
144 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> CreateClient(
145 std::string peer_identity,
146 std::shared_ptr<SignallingTransport> signalling,
147 WebRtcConfiguration configuration = {}, WireStreamOptions options = {});
148
164 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> Create(
165 std::shared_ptr<rtc::DataChannel> data_channel,
166 std::shared_ptr<rtc::PeerConnection> connection,
167 std::shared_ptr<SignallingTransport> signalling_endpoint,
168 ChannelEndpointRole role, WireStreamOptions options = {},
169 OpenOperation open_operation = {}, size_t split_size = 48 * 1024);
170
179 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> BuildMultiplexedStream(
180 std::shared_ptr<internal::BinaryChannel> channel,
181 std::shared_ptr<rtc::DataChannel> primary_channel,
182 std::shared_ptr<rtc::PeerConnection> connection,
183 std::shared_ptr<SignallingTransport> signalling_endpoint, std::string id,
184 ChannelEndpointRole role, WireStreamOptions options = {},
185 OpenOperation open_operation = {}, size_t split_size = 48 * 1024);
186
187 ~WebRtcWireStream() override;
188
190 [[nodiscard]] std::shared_ptr<rtc::DataChannel> data_channel() const;
193 [[nodiscard]] std::shared_ptr<rtc::PeerConnection> peer_connection() const;
195 [[nodiscard]] std::shared_ptr<SignallingTransport> signalling_endpoint()
196 const;
197
198 WebRtcWireStream(ConstructorToken, std::shared_ptr<State> state,
199 std::shared_ptr<rtc::DataChannel> data_channel,
200 std::shared_ptr<rtc::PeerConnection> connection,
201 std::shared_ptr<SignallingTransport> signalling_endpoint)
202 : ChannelWireStream(std::move(state)),
203 data_channel_(std::move(data_channel)),
204 connection_(std::move(connection)),
205 signalling_endpoint_(std::move(signalling_endpoint)) {}
206
207 private:
208 std::shared_ptr<rtc::DataChannel> data_channel_;
209 std::shared_ptr<rtc::PeerConnection> connection_;
210 std::shared_ptr<SignallingTransport> signalling_endpoint_;
211};
212
215 std::function<a11::Task(std::shared_ptr<WebRtcWireStream>)>;
216
224class WebRtcWireServer : public std::enable_shared_from_this<WebRtcWireServer> {
225 public:
236 static absl::StatusOr<std::shared_ptr<WebRtcWireServer>> Create(
237 std::string identity, std::shared_ptr<SignallingService> signalling,
238 OnWebRtcStream on_stream, WebRtcConfiguration configuration = {},
239 WireStreamOptions stream_options = {});
240
242
244 absl::Status Stop();
246 [[nodiscard]] std::string identity() const;
248 [[nodiscard]] bool running() const;
250 [[nodiscard]] size_t pending_peer_count() const;
252 [[nodiscard]] std::shared_ptr<SignallingEndpoint> signalling_endpoint() const;
253
254 private:
255 struct State;
256
257 explicit WebRtcWireServer(std::shared_ptr<State> state)
258 : state_(std::move(state)) {}
259
260 static a11::Task OnSignal(const std::shared_ptr<State>& state,
261 SignallingMessage message);
262 static absl::Status HandleOffer(const std::shared_ptr<State>& state,
263 const SignallingMessage& message);
264 static absl::Status HandleCandidate(const std::shared_ptr<State>& state,
265 const SignallingMessage& message);
266 static void ReportPeerError(const std::shared_ptr<State>& state,
267 std::string peer_identity, absl::Status status);
268
269 std::shared_ptr<State> state_;
270};
271
272} // namespace a11::net
273
274#endif // A11_NET_WEBRTC_WIRE_STREAM_H_
Shared WireStream lifecycle and framing for binary channels.
Definition channel_wire_stream.h:55
std::function< absl::Status()> OpenOperation
Transport-specific operation that initiates or accepts the channel.
Definition channel_wire_stream.h:58
Accepts incoming WebRTC peer connections under a fixed identity.
Definition webrtc_wire_stream.h:224
~WebRtcWireServer()
Definition webrtc_wire_stream.cc:695
size_t pending_peer_count() const
Definition webrtc_wire_stream.cc:1060
std::shared_ptr< SignallingEndpoint > signalling_endpoint() const
Definition webrtc_wire_stream.cc:1065
static absl::StatusOr< std::shared_ptr< WebRtcWireServer > > Create(std::string identity, std::shared_ptr< SignallingService > signalling, OnWebRtcStream on_stream, WebRtcConfiguration configuration={}, WireStreamOptions stream_options={})
Creates a WebRTC server that accepts peer connections.
Definition webrtc_wire_stream.cc:645
std::string identity() const
Definition webrtc_wire_stream.cc:1050
absl::Status Stop()
Stops the server and stops accepting new peer connections.
Definition webrtc_wire_stream.cc:1016
bool running() const
Definition webrtc_wire_stream.cc:1055
A WireStream that carries A11 traffic over a WebRTC data channel.
Definition webrtc_wire_stream.h:110
WebRtcWireStream(ConstructorToken, std::shared_ptr< State > state, std::shared_ptr< rtc::DataChannel > data_channel, std::shared_ptr< rtc::PeerConnection > connection, std::shared_ptr< SignallingTransport > signalling_endpoint)
Definition webrtc_wire_stream.h:198
std::shared_ptr< rtc::DataChannel > data_channel() const
Definition webrtc_wire_stream.cc:581
~WebRtcWireStream() override
Definition webrtc_wire_stream.cc:572
static absl::StatusOr< std::shared_ptr< WebRtcWireStream > > BuildMultiplexedStream(std::shared_ptr< internal::BinaryChannel > channel, std::shared_ptr< rtc::DataChannel > primary_channel, std::shared_ptr< rtc::PeerConnection > connection, std::shared_ptr< SignallingTransport > signalling_endpoint, std::string id, ChannelEndpointRole role, WireStreamOptions options={}, OpenOperation open_operation={}, size_t split_size=48 *1024)
Adopts an already-built binary channel (possibly multiplexed).
Definition webrtc_wire_stream.cc:320
std::shared_ptr< SignallingTransport > signalling_endpoint() const
Definition webrtc_wire_stream.cc:589
static absl::StatusOr< std::shared_ptr< WebRtcWireStream > > Create(std::shared_ptr< rtc::DataChannel > data_channel, std::shared_ptr< rtc::PeerConnection > connection, std::shared_ptr< SignallingTransport > signalling_endpoint, ChannelEndpointRole role, WireStreamOptions options={}, OpenOperation open_operation={}, size_t split_size=48 *1024)
Adopts an already-established data channel as a WireStream.
Definition webrtc_wire_stream.cc:346
std::shared_ptr< rtc::PeerConnection > peer_connection() const
Definition webrtc_wire_stream.cc:585
static absl::StatusOr< std::shared_ptr< WebRtcWireStream > > CreateClient(std::string identity, std::string peer_identity, std::shared_ptr< SignallingService > signalling, WebRtcConfiguration configuration={}, WireStreamOptions options={})
Dials a peer over a shared in-process signalling service.
Definition webrtc_wire_stream.cc:386
Completion values used by every asynchronous A11 operation.
Definition action.h:46
std::function< a11::Task(std::shared_ptr< WebRtcWireStream >)> OnWebRtcStream
Callback invoked with each newly negotiated peer WireStream.
Definition webrtc_wire_stream.h:215
TurnRelayType
Transport used to reach a TURN relay server.
Definition webrtc_wire_stream.h:47
ChannelEndpointRole
Handshake role a binary channel endpoint is permitted to assume.
Definition channel_wire_stream.h:29
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:403
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
Definition webrtc_wire_stream.h:39
Out-of-band signalling used to negotiate WebRTC peer connections.
One signalling payload: an SDP description, an ICE candidate, or an error.
Definition signalling.h:48
Address and credentials for a TURN relay used during ICE.
Definition webrtc_wire_stream.h:55
std::string password
Relay credential password.
Definition webrtc_wire_stream.h:63
std::string username
Relay credential username.
Definition webrtc_wire_stream.h:62
friend bool operator==(const TurnServer &, const TurnServer &)=default
std::string hostname
TURN host name or address.
Definition webrtc_wire_stream.h:60
static absl::StatusOr< TurnServer > FromString(std::string_view value)
Parses a TURN server from a URL-like string.
Definition webrtc_wire_stream.cc:179
std::uint16_t port
TURN service port.
Definition webrtc_wire_stream.h:61
TurnRelayType relay_type
Transport to the relay.
Definition webrtc_wire_stream.h:64
ICE, fragmentation, and binding settings for a WebRTC connection.
Definition webrtc_wire_stream.h:75
std::optional< std::pair< std::uint16_t, std::uint16_t > > preferred_port_range
Optional inclusive local UDP port range for ICE.
Definition webrtc_wire_stream.h:87
absl::Status Validate() const
Definition webrtc_wire_stream.cc:243
std::optional< std::string > bind_address
Optional local candidate address.
Definition webrtc_wire_stream.h:89
std::vector< std::string > stun_servers
STUN URLs used for NAT discovery.
Definition webrtc_wire_stream.h:84
std::optional< size_t > max_message_size
Advertised channel ceiling.
Definition webrtc_wire_stream.h:78
size_t channel_split_size
A11 packet size below SCTP limits.
Definition webrtc_wire_stream.h:80
bool enable_ice_udp_mux
Reuse one UDP socket for ICE candidates.
Definition webrtc_wire_stream.h:82
size_t desired_channels
Data channels a client opens and replenishes.
Definition webrtc_wire_stream.h:95
std::vector< TurnServer > turn_servers
Fallback relay servers.
Definition webrtc_wire_stream.h:85
size_t max_channels
Data channels a server admits per peer.
Definition webrtc_wire_stream.h:96
Definition webrtc_wire_stream.cc:622
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:44
const std::string identity
Definition webrtc_wire_stream.cc:90
const std::string peer_identity
Definition webrtc_wire_stream.cc:91
const std::shared_ptr< rtc::PeerConnection > connection
Definition webrtc_wire_stream.cc:93
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...