A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
webrtc_wire_stream.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
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
32#ifndef A11_NET_WEBRTC_WIRE_STREAM_H_
33#define A11_NET_WEBRTC_WIRE_STREAM_H_
34
35#include <cstddef>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <optional>
40#include <string>
41#include <string_view>
42#include <utility>
43#include <vector>
44
45#include <absl/base/thread_annotations.h>
46#include <absl/status/status.h>
47#include <absl/status/statusor.h>
48#include <absl/time/time.h>
49
52#include "a11/net/signalling.h"
53#include "a11/net/wire_stream.h"
54#include "thread/boost_primitives.h"
55
56namespace rtc {
57class DataChannel;
58class PeerConnection;
59} // namespace rtc
60
61namespace a11::net {
62
63namespace internal {
64class MultiplexedBinaryChannel;
65class PathMtuDiscovery;
66} // namespace internal
67
69enum class TurnRelayType { kUdp, kTcp, kTls };
70
77inline constexpr size_t kWebRtcBaseMtu = 1280;
78
80inline constexpr size_t kWebRtcMinMtu = 512;
81
88struct TurnServer {
91 static absl::StatusOr<TurnServer> FromString(std::string_view value);
92
93 std::string hostname;
94 std::uint16_t port = 3478;
95 std::string username;
96 std::string password;
98
99 friend bool operator==(const TurnServer&, const TurnServer&) = default;
100};
101
109 // Large logical WireMessages are fragmented by A11 before reaching SCTP;
110 // this remains the advertised local libdatachannel message ceiling.
111 std::optional<size_t> max_message_size =
112 64 * 1024;
114 48 * 1024;
122 std::optional<size_t> mtu;
124 false;
125 std::vector<std::string> stun_servers;
126 std::vector<TurnServer> turn_servers;
128 std::optional<std::pair<std::uint16_t, std::uint16_t>> preferred_port_range;
129 std::optional<std::string>
131 // Stripe packets across data channels while preserving one reliable
132 // WireStream. Clients maintain `desired_channels`; servers admit at most
133 // `max_channels` per peer.
135 8;
136 size_t max_channels = 8;
137
148 size_t max_discovered_mtu = 9216;
150 absl::Duration probe_timeout = absl::Milliseconds(500);
152 absl::Duration path_mtu_raise_interval = absl::Seconds(600);
154 absl::Duration path_mtu_startup_retry = absl::Milliseconds(250);
155
157 absl::Status Validate() const;
158};
159
169 private:
170 struct ConstructorToken {};
171
172 public:
184 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> CreateClient(
185 std::string identity, std::string peer_identity,
186 const std::shared_ptr<SignallingService>& signalling,
188 WireStreamOptions options = {});
189
203 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> CreateClient(
204 std::string peer_identity,
205 const std::shared_ptr<SignallingTransport>& signalling,
207 WireStreamOptions options = {});
208
224 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>> Create(
225 std::shared_ptr<rtc::DataChannel> data_channel,
226 std::shared_ptr<rtc::PeerConnection> connection,
227 std::shared_ptr<SignallingTransport> signalling_endpoint,
228 ChannelEndpointRole role, WireStreamOptions options = {},
229 OpenOperation open_operation = {}, size_t split_size = 48 * 1024);
230
239 static absl::StatusOr<std::shared_ptr<WebRtcWireStream>>
241 std::shared_ptr<internal::BinaryChannel> channel,
242 std::shared_ptr<rtc::DataChannel> primary_channel,
243 std::shared_ptr<rtc::PeerConnection> connection,
244 std::shared_ptr<SignallingTransport> signalling_endpoint, std::string id,
245 ChannelEndpointRole role, WireStreamOptions options = {},
246 OpenOperation open_operation = {}, size_t split_size = 48 * 1024,
247 size_t configured_mtu = kWebRtcBaseMtu);
248
249 ~WebRtcWireStream() override;
250
252 [[nodiscard]] std::shared_ptr<rtc::DataChannel> data_channel() const;
255 [[nodiscard]] std::shared_ptr<rtc::PeerConnection> peer_connection() const;
257 [[nodiscard]] std::shared_ptr<SignallingTransport> signalling_endpoint()
258 const;
259
279 absl::Status SetPathMtu(size_t mtu);
280
285 [[nodiscard]] size_t current_path_mtu() const;
286
295 const std::shared_ptr<internal::MultiplexedBinaryChannel>& stream_data);
296
299 [[nodiscard]] size_t discovered_path_mtu() const;
300
301 WebRtcWireStream(ConstructorToken, std::shared_ptr<State> state,
302 std::shared_ptr<rtc::DataChannel> data_channel,
303 std::shared_ptr<rtc::PeerConnection> connection,
304 std::shared_ptr<SignallingTransport> signalling_endpoint,
305 size_t configured_mtu)
306 : ChannelWireStream(std::move(state)),
307 data_channel_(std::move(data_channel)),
308 connection_(std::move(connection)),
309 signalling_endpoint_(std::move(signalling_endpoint)),
310 path_mtu_(configured_mtu) {}
311
312 private:
313 std::shared_ptr<rtc::DataChannel> data_channel_;
314 std::shared_ptr<rtc::PeerConnection> connection_;
315 std::shared_ptr<SignallingTransport> signalling_endpoint_;
316 mutable thread::Mutex path_mtu_mu_;
317 size_t path_mtu_ ABSL_GUARDED_BY(path_mtu_mu_);
318 std::shared_ptr<internal::PathMtuDiscovery> discovery_
319 ABSL_GUARDED_BY(path_mtu_mu_);
320};
321
324 std::function<a11::Task(std::shared_ptr<WebRtcWireStream>)>;
325
333class WebRtcWireServer : public std::enable_shared_from_this<WebRtcWireServer> {
334 public:
349 static absl::StatusOr<std::shared_ptr<WebRtcWireServer>> Create(
350 std::string identity,
351 const std::shared_ptr<SignallingService>& signalling,
353 WireStreamOptions stream_options = {});
354
376 static absl::StatusOr<std::shared_ptr<WebRtcWireServer>> Create(
377 const std::shared_ptr<SignallingTransport>& signalling,
379 WireStreamOptions stream_options = {});
380
382
384 absl::Status Stop();
386 [[nodiscard]] std::string identity() const;
388 [[nodiscard]] bool running() const;
390 [[nodiscard]] size_t pending_peer_count() const;
392 [[nodiscard]] std::shared_ptr<SignallingTransport> signalling_endpoint()
393 const;
394
395 private:
396 struct State;
397
398 explicit WebRtcWireServer(std::shared_ptr<State> state)
399 : state_(std::move(state)) {}
400
401 static a11::Task OnSignal(const std::shared_ptr<State>& state,
402 const SignallingMessage& message);
403 static absl::Status HandleOffer(const std::shared_ptr<State>& state,
404 const SignallingMessage& message);
405 static absl::Status HandleCandidate(const std::shared_ptr<State>& state,
406 const SignallingMessage& message);
407 static void ReportPeerError(const std::shared_ptr<State>& state,
408 std::string peer_identity, absl::Status status);
409
410 std::shared_ptr<State> state_;
411};
412
413} // namespace a11::net
414
415#endif // A11_NET_WEBRTC_WIRE_STREAM_H_
Shared WireStream lifecycle and framing for binary channels.
Definition channel_wire_stream.h:69
std::function< absl::Status()> OpenOperation
Transport-specific operation that initiates or accepts the channel.
Definition channel_wire_stream.h:72
Accepts incoming WebRTC peer connections under a fixed identity.
Definition webrtc_wire_stream.h:333
~WebRtcWireServer()
Definition webrtc_wire_stream.cc:937
size_t pending_peer_count() const
Definition webrtc_wire_stream.cc:1327
std::shared_ptr< SignallingTransport > signalling_endpoint() const
Definition webrtc_wire_stream.cc:1332
std::string identity() const
Definition webrtc_wire_stream.cc:1317
absl::Status Stop()
Stops the server and stops accepting new peer connections.
Definition webrtc_wire_stream.cc:1280
bool running() const
Definition webrtc_wire_stream.cc:1322
static absl::StatusOr< std::shared_ptr< WebRtcWireServer > > Create(std::string identity, const 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:870
A WireStream that carries A11 traffic over a WebRTC data channel.
Definition webrtc_wire_stream.h:168
size_t discovered_path_mtu() const
Definition webrtc_wire_stream.cc:804
void StartPathMtuDiscovery(const WebRtcConfiguration &configuration, const std::shared_ptr< internal::MultiplexedBinaryChannel > &stream_data)
Starts path MTU discovery on this stream.
Definition webrtc_wire_stream.cc:707
std::shared_ptr< rtc::DataChannel > data_channel() const
Definition webrtc_wire_stream.cc:657
~WebRtcWireStream() override
Definition webrtc_wire_stream.cc:648
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, size_t configured_mtu=kWebRtcBaseMtu)
Adopts an already-built binary channel (possibly multiplexed).
Definition webrtc_wire_stream.cc:379
std::shared_ptr< SignallingTransport > signalling_endpoint() const
Definition webrtc_wire_stream.cc:665
static absl::StatusOr< std::shared_ptr< WebRtcWireStream > > CreateClient(std::string identity, std::string peer_identity, const std::shared_ptr< SignallingService > &signalling, const WebRtcConfiguration &configuration={}, WireStreamOptions options={})
Dials a peer over a shared in-process signalling service.
Definition webrtc_wire_stream.cc:443
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:406
size_t current_path_mtu() const
Definition webrtc_wire_stream.cc:813
std::shared_ptr< rtc::PeerConnection > peer_connection() const
Definition webrtc_wire_stream.cc:661
absl::Status SetPathMtu(size_t mtu)
Sets the SCTP path MTU on this already-connected association.
Definition webrtc_wire_stream.cc:676
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, size_t configured_mtu)
Definition webrtc_wire_stream.h:301
std::string value
Definition discover.cc:114
Completion values used by every asynchronous A11 operation.
Definition action.h:65
std::function< a11::Task(std::shared_ptr< WebRtcWireStream >)> OnWebRtcStream
Callback invoked with each newly negotiated peer WireStream.
Definition webrtc_wire_stream.h:324
TurnRelayType
Transport used to reach a TURN relay server.
Definition webrtc_wire_stream.h:69
constexpr size_t kWebRtcBaseMtu
The MTU a WebRTC association uses until something better is confirmed.
Definition webrtc_wire_stream.h:77
constexpr size_t kWebRtcMinMtu
Smallest MTU usrsctp will accept (SCTP_SMALLEST_PMTU).
Definition webrtc_wire_stream.h:80
ChannelEndpointRole
Handshake role a binary channel endpoint is permitted to assume.
Definition channel_wire_stream.h:43
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:411
Definition webrtc_wire_stream.h:56
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:62
Address and credentials for a TURN relay used during ICE.
Definition webrtc_wire_stream.h:88
std::string password
Relay credential password.
Definition webrtc_wire_stream.h:96
std::string username
Relay credential username.
Definition webrtc_wire_stream.h:95
friend bool operator==(const TurnServer &, const TurnServer &)=default
std::string hostname
TURN host name or address.
Definition webrtc_wire_stream.h:93
static absl::StatusOr< TurnServer > FromString(std::string_view value)
Parses a TURN server from a URL-like string.
Definition webrtc_wire_stream.cc:211
std::uint16_t port
TURN service port.
Definition webrtc_wire_stream.h:94
TurnRelayType relay_type
Transport to the relay.
Definition webrtc_wire_stream.h:97
ICE, fragmentation, and binding settings for a WebRTC connection.
Definition webrtc_wire_stream.h:108
size_t max_discovered_mtu
Bounds and timers for the search.
Definition webrtc_wire_stream.h:148
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:128
std::optional< size_t > mtu
Network MTU SCTP builds packets to, in bytes.
Definition webrtc_wire_stream.h:122
absl::Status Validate() const
Definition webrtc_wire_stream.cc:275
std::optional< std::string > bind_address
Optional local candidate address.
Definition webrtc_wire_stream.h:130
std::vector< std::string > stun_servers
STUN URLs used for NAT discovery.
Definition webrtc_wire_stream.h:125
std::optional< size_t > max_message_size
Advertised channel ceiling.
Definition webrtc_wire_stream.h:111
size_t channel_split_size
A11 packet size below SCTP limits.
Definition webrtc_wire_stream.h:113
bool enable_ice_udp_mux
Reuse one UDP socket for ICE candidates.
Definition webrtc_wire_stream.h:123
bool path_mtu_discovery
Discover the path MTU by probing, instead of assuming mtu.
Definition webrtc_wire_stream.h:146
absl::Duration path_mtu_raise_interval
Delay before searching upward again after convergence.
Definition webrtc_wire_stream.h:152
size_t desired_channels
Data channels a client opens and replenishes.
Definition webrtc_wire_stream.h:134
absl::Duration path_mtu_startup_retry
Delay before retrying while the SCTP association is unavailable.
Definition webrtc_wire_stream.h:154
std::vector< TurnServer > turn_servers
Fallback relay servers.
Definition webrtc_wire_stream.h:126
absl::Duration probe_timeout
How long one probe has to be acknowledged before it counts as lost.
Definition webrtc_wire_stream.h:150
size_t max_channels
Data channels a server admits per peer.
Definition webrtc_wire_stream.h:136
Definition webrtc_wire_stream.cc:847
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:58
const std::string identity
Definition webrtc_wire_stream.cc:107
const std::string peer_identity
Definition webrtc_wire_stream.cc:108
const std::shared_ptr< rtc::PeerConnection > connection
Definition webrtc_wire_stream.cc:110
WebRtcConfiguration configuration
Definition webrtc_wire_stream.cc:120
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...