ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
webrtc.h File Reference

WebRTC P2P signaling for discovery mode failover. More...

Go to the source code of this file.

Functions

webrtc_signaling_callbacks_t discovery_webrtc_get_direct_signaling_callbacks (acip_transport_t *tcp_transport, const uint8_t session_id[SESSION_ID_SIZE], const uint8_t participant_id[SESSION_ID_SIZE])
 Create signaling callbacks for direct peer-to-peer connection during failover.
void discovery_webrtc_set_tcp_transport (acip_transport_t *transport)
 Set the TCP transport for direct peer-to-peer signaling.
void discovery_webrtc_set_session_context (const uint8_t session_id[SESSION_ID_SIZE], const uint8_t participant_id[SESSION_ID_SIZE])
 Set session and participant IDs for direct signaling.
void discovery_webrtc_cleanup_transport (void)
 Cleanup and release the direct peer-to-peer transport.

Detailed Description

WebRTC P2P signaling for discovery mode failover.

Provides WebRTC peer connection management for discovery mode failover scenarios. When a host dies and participants need to connect to the pre-elected future host, they establish P2P connections using WebRTC DataChannels.

Key Difference from Client Mode:

  • Client mode: SDP/ICE exchanged through ACDS relay (client connected to host for media)
  • Discovery failover: SDP/ICE exchanged directly over TCP to new host (P2P connection)
  • Both use the same WebRTC library (libdatachannel) but with different signaling paths

Architecture

During migration:

  1. Participant detects host disconnect
  2. Participant knows new host's address from pre-election (FUTURE_HOST_ELECTED)
  3. Participant connects TCP to new host
  4. Participant sends SDP OFFER directly over TCP (not through ACDS)
  5. New host sends SDP ANSWER directly over TCP
  6. Both exchange ICE candidates directly
  7. WebRTC P2P connection established
  8. Media flows peer-to-peer (or through TURN)
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file src/discovery/webrtc.h.

Function Documentation

◆ discovery_webrtc_cleanup_transport()

void discovery_webrtc_cleanup_transport ( void )

Cleanup and release the direct peer-to-peer transport.

Clears the TCP transport used for direct signaling. Called on migration completion or when the P2P connection is torn down.

Note
Actual transport cleanup (closing connections, freeing resources) should be done by caller before calling this.
Thread-safe.

Definition at line 284 of file src/discovery/webrtc.c.

284 {
285 ensure_mutex_initialized();
286 mutex_lock(&g_webrtc_mutex);
287
288 g_tcp_transport = NULL;
289 g_session_context.is_set = false;
290
291 mutex_unlock(&g_webrtc_mutex);
292}

◆ discovery_webrtc_get_direct_signaling_callbacks()

webrtc_signaling_callbacks_t discovery_webrtc_get_direct_signaling_callbacks ( acip_transport_t * tcp_transport,
const uint8_t session_id[SESSION_ID_SIZE],
const uint8_t participant_id[SESSION_ID_SIZE] )

Create signaling callbacks for direct peer-to-peer connection during failover.

Returns callbacks that send SDP/ICE directly over TCP to the peer (not through ACDS). Used when participants reconnect to a new host after migration.

Parameters
tcp_transportTCP transport connected to the new host
session_idSession UUID (16 bytes, copied)
participant_idLocal participant UUID (16 bytes, copied)
Returns
Signaling callbacks for use with peer manager
Note
The returned structure points to static callbacks and is valid for program lifetime.
TCP transport must be connected and valid when callbacks are invoked.
Set tcp_transport to NULL to disable the callbacks.

Definition at line 238 of file src/discovery/webrtc.c.

239 {
240 ensure_mutex_initialized();
241 mutex_lock(&g_webrtc_mutex);
242
243 g_tcp_transport = tcp_transport;
244
245 if (session_id && participant_id) {
246 memcpy(g_session_context.session_id, session_id, SESSION_ID_SIZE);
247 memcpy(g_session_context.participant_id, participant_id, SESSION_ID_SIZE);
248 g_session_context.is_set = true;
249 }
250
251 mutex_unlock(&g_webrtc_mutex);
252
253 webrtc_signaling_callbacks_t callbacks = {
254 .send_sdp = discovery_send_sdp,
255 .send_ice = discovery_send_ice,
256 .user_data = NULL, // Unused - we use global state
257 };
258
259 return callbacks;
260}
#define SESSION_ID_SIZE
uint8_t session_id[SESSION_ID_SIZE]
uint8_t participant_id[SESSION_ID_SIZE]
Hooks that deliver local SDP offers/answers and ICE candidates to ACDS.

References participant_id, session_id, and SESSION_ID_SIZE.

◆ discovery_webrtc_set_session_context()

void discovery_webrtc_set_session_context ( const uint8_t session_id[SESSION_ID_SIZE],
const uint8_t participant_id[SESSION_ID_SIZE] )

Set session and participant IDs for direct signaling.

Configures the session context used when sending SDP/ICE messages directly. Must be called before peer manager generates any local descriptions.

Parameters
session_idSession UUID (16 bytes, copied)
participant_idLocal participant UUID (16 bytes, copied)
Note
Callbacks will fail with ERROR_INVALID_STATE if IDs are not set.

Definition at line 269 of file src/discovery/webrtc.c.

269 {
270 ensure_mutex_initialized();
271 mutex_lock(&g_webrtc_mutex);
272
273 if (session_id && participant_id) {
274 memcpy(g_session_context.session_id, session_id, SESSION_ID_SIZE);
275 memcpy(g_session_context.participant_id, participant_id, SESSION_ID_SIZE);
276 g_session_context.is_set = true;
277 } else {
278 g_session_context.is_set = false;
279 }
280
281 mutex_unlock(&g_webrtc_mutex);
282}

References participant_id, session_id, and SESSION_ID_SIZE.

◆ discovery_webrtc_set_tcp_transport()

void discovery_webrtc_set_tcp_transport ( acip_transport_t * transport)

Set the TCP transport for direct peer-to-peer signaling.

Configures the transport that will be used to send SDP/ICE messages directly to the peer (bypassing ACDS). Must be called before peer manager generates any local descriptions.

Parameters
transportTCP transport to new host (NULL to disable)
Note
This is separate from the ACDS transport used in client mode.
Callbacks will fail with ERROR_INVALID_STATE if transport is NULL.

Definition at line 262 of file src/discovery/webrtc.c.

262 {
263 ensure_mutex_initialized();
264 mutex_lock(&g_webrtc_mutex);
265 g_tcp_transport = transport;
266 mutex_unlock(&g_webrtc_mutex);
267}