ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/discovery/webrtc.c
Go to the documentation of this file.
1
11
13#include "webrtc.h"
14
15#include <ascii-chat/common.h>
16#include <ascii-chat/log/log.h>
17#include <libplatform/lifecycle.h>
23#include <libplatform/compiler.h>
24#include <libplatform/system.h>
25#include <libplatform/terminal.h>
28
29#include <string.h>
30
31// =============================================================================
32// Global State
33// =============================================================================
34
42static acip_transport_t *g_tcp_transport = NULL;
43
50static struct {
53 bool is_set;
54} g_session_context = {0};
55
59static mutex_t g_webrtc_mutex;
60static lifecycle_t g_webrtc_lc = LIFECYCLE_INIT_MUTEX(&g_webrtc_mutex);
61
62// =============================================================================
63// Internal Helpers
64// =============================================================================
65
73static void ensure_mutex_initialized(void) {
74 if (!lifecycle_init(&g_webrtc_lc, "webrtc"))
75 return;
76}
77
78// =============================================================================
79// Signaling Callback Implementations (Direct TCP)
80// =============================================================================
81
92static asciichat_error_t discovery_send_sdp(const uint8_t session_id[SESSION_ID_SIZE], const uint8_t recipient_id[SESSION_ID_SIZE], const char *sdp_type, const char *sdp, void *user_data) {
93 (void)recipient_id; // Unused - direct connection, no need to route
94 (void)user_data; // Unused
95
96 ensure_mutex_initialized();
97 mutex_lock(&g_webrtc_mutex);
98
99 // Verify state
100 if (!g_tcp_transport) {
101 mutex_unlock(&g_webrtc_mutex);
102 SET_ERRNO(ERROR_INVALID_STATE, "TCP transport not set for direct signaling");
103 return ERROR_INVALID_STATE;
104 }
105
106 if (!g_session_context.is_set) {
107 mutex_unlock(&g_webrtc_mutex);
108 SET_ERRNO(ERROR_INVALID_STATE, "Session context not set for direct signaling");
109 return ERROR_INVALID_STATE;
110 }
111
112 // Verify inputs
113 if (!sdp) {
114 mutex_unlock(&g_webrtc_mutex);
115 SET_ERRNO(ERROR_INVALID_PARAM, "SDP is NULL");
116 return ERROR_INVALID_PARAM;
117 }
118
119 size_t sdp_len = strlen(sdp);
120 if (sdp_len > 4096) {
121 mutex_unlock(&g_webrtc_mutex);
122 SET_ERRNO(ERROR_INVALID_PARAM, "SDP too large (>4096 bytes)");
123 return ERROR_INVALID_PARAM;
124 }
125
126 // Construct packet
127 // Header: acip_webrtc_sdp_t (defined in network/acip/acds.h)
128 // Followed by: SDP string
129 uint8_t sdp_msg_buf[sizeof(acip_webrtc_sdp_t) + 4096];
130 acip_webrtc_sdp_t *sdp_msg = (acip_webrtc_sdp_t *)sdp_msg_buf;
131
132 memcpy(sdp_msg->session_id, session_id, SESSION_ID_SIZE);
133 memcpy(sdp_msg->sender_id, g_session_context.participant_id, SESSION_ID_SIZE);
134 memset(sdp_msg->recipient_id, 0, SESSION_ID_SIZE); // Broadcast to all
135 sdp_msg->sdp_type = (strcmp(sdp_type, "answer") == 0) ? 1 : 0; // 0=offer, 1=answer
136 sdp_msg->sdp_len = HOST_TO_NET_U16((uint16_t)sdp_len);
137 // NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary packet data
138 memcpy(sdp_msg_buf + sizeof(acip_webrtc_sdp_t), sdp, sdp_len);
139
140 // Send via direct TCP transport (not ACDS)
141 size_t msg_size = sizeof(acip_webrtc_sdp_t) + sdp_len;
142 asciichat_error_t result = packet_send_via_transport(g_tcp_transport, PACKET_TYPE_ACIP_WEBRTC_SDP, sdp_msg_buf, msg_size, 0);
143
144 mutex_unlock(&g_webrtc_mutex);
145
146 if (result != ASCIICHAT_OK) {
147 log_error("Failed to send SDP via direct TCP: %d", result);
148 return result;
149 }
150
151 log_debug("Sent SDP %s directly to peer (TCP transport)", sdp_type);
152 return ASCIICHAT_OK;
153}
154
160static asciichat_error_t discovery_send_ice(const uint8_t session_id[SESSION_ID_SIZE], const uint8_t recipient_id[SESSION_ID_SIZE], const char *candidate, const char *mid, void *user_data) {
161 (void)recipient_id; // Unused - direct connection, no need to route
162 (void)user_data; // Unused
163
164 ensure_mutex_initialized();
165 mutex_lock(&g_webrtc_mutex);
166
167 // Verify state
168 if (!g_tcp_transport) {
169 mutex_unlock(&g_webrtc_mutex);
170 SET_ERRNO(ERROR_INVALID_STATE, "TCP transport not set for direct signaling");
171 return ERROR_INVALID_STATE;
172 }
173
174 if (!g_session_context.is_set) {
175 mutex_unlock(&g_webrtc_mutex);
176 SET_ERRNO(ERROR_INVALID_STATE, "Session context not set for direct signaling");
177 return ERROR_INVALID_STATE;
178 }
179
180 // Verify inputs
181 if (!candidate || !mid) {
182 mutex_unlock(&g_webrtc_mutex);
183 SET_ERRNO(ERROR_INVALID_PARAM, "Candidate or mid is NULL");
184 return ERROR_INVALID_PARAM;
185 }
186
187 // Calculate payload length (candidate + null + mid + null)
188 size_t candidate_len = strlen(candidate);
189 size_t mid_len = strlen(mid);
190 size_t payload_len = candidate_len + 1 + mid_len + 1;
191
192 if (payload_len > 4096) {
193 mutex_unlock(&g_webrtc_mutex);
194 SET_ERRNO(ERROR_INVALID_PARAM, "ICE candidate + mid too large (>4096 bytes)");
195 return ERROR_INVALID_PARAM;
196 }
197
198 // Construct packet
199 // Header: acip_webrtc_ice_t (defined in network/acip/acds.h)
200 // Followed by: candidate string + null + mid string + null
201 uint8_t ice_msg_buf[sizeof(acip_webrtc_ice_t) + 4096];
202 acip_webrtc_ice_t *ice_msg = (acip_webrtc_ice_t *)ice_msg_buf;
203
204 // Fill header
205 memcpy(ice_msg->session_id, session_id, SESSION_ID_SIZE);
206 memcpy(ice_msg->sender_id, g_session_context.participant_id, SESSION_ID_SIZE);
207 memset(ice_msg->recipient_id, 0, SESSION_ID_SIZE); // Broadcast to all
208 ice_msg->candidate_len = HOST_TO_NET_U16((uint16_t)candidate_len);
209
210 // Copy candidate and mid after header
211 uint8_t *payload = ice_msg_buf + sizeof(acip_webrtc_ice_t);
212 memcpy(payload, candidate, candidate_len);
213 payload[candidate_len] = '\0';
214 memcpy(payload + candidate_len + 1, mid, mid_len);
215 payload[candidate_len + 1 + mid_len] = '\0';
216
217 log_debug("Sending ICE candidate directly to peer (candidate_len=%zu, mid=%s)", candidate_len, mid);
218
219 // Send via direct TCP transport (not ACDS)
220 size_t msg_size = sizeof(acip_webrtc_ice_t) + payload_len;
221 asciichat_error_t result = packet_send_via_transport(g_tcp_transport, PACKET_TYPE_ACIP_WEBRTC_ICE, ice_msg_buf, msg_size, 0);
222
223 mutex_unlock(&g_webrtc_mutex);
224
225 if (result != ASCIICHAT_OK) {
226 log_error("Failed to send ICE candidate via direct TCP: %d", result);
227 return result;
228 }
229
230 log_debug("Sent ICE candidate directly to peer (TCP transport)");
231 return ASCIICHAT_OK;
232}
233
234// =============================================================================
235// Public API
236// =============================================================================
237
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}
261
263 ensure_mutex_initialized();
264 mutex_lock(&g_webrtc_mutex);
265 g_tcp_transport = transport;
266 mutex_unlock(&g_webrtc_mutex);
267}
268
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}
283
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}
ascii-chat Discovery Service (ACDS) Protocol Message Formats
#define SET_ERRNO(code,...)
Platform initialization and static synchronization helpers.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U16(val)
Definition endian.h:100
unsigned short uint16_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
#define SESSION_ID_SIZE
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
acip_webrtc_ice_t
Definition acds.h:655
acip_webrtc_sdp_t
Definition acds.h:609
@ PACKET_TYPE_ACIP_WEBRTC_ICE
WebRTC ICE candidate (bidirectional).
Definition packet.h:466
@ PACKET_TYPE_ACIP_WEBRTC_SDP
WebRTC SDP offer/answer (bidirectional).
Definition packet.h:464
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
⚙️ Common definitions, error codes, macros, and types shared throughout the application
ascii-chat Binary Network Protocol (complete system including ACIP discovery service)
📝 Logging API with multiple log levels and terminal output control
🌐 Core network I/O operations with timeout support
asciichat_error_t packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
ACIP shared/bidirectional packet sending functions.
bool is_set
uint8_t session_id[SESSION_ID_SIZE]
uint8_t participant_id[SESSION_ID_SIZE]
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.
void discovery_webrtc_set_tcp_transport(acip_transport_t *transport)
Set the TCP transport for direct peer-to-peer signaling.
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.
WebRTC P2P signaling for discovery mode failover.
🔤 String Manipulation and Shell Escaping Utilities
Hooks that deliver local SDP offers/answers and ICE candidates to ACDS.