ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
transport.c File Reference

Go to the source code of this file.

Data Structures

struct  webrtc_recv_msg_t
 Receive queue element (variable-length message) More...
 
struct  webrtc_transport_data_t
 WebRTC transport implementation data. More...
 

Macros

#define WEBRTC_RECV_QUEUE_SIZE   64
 Maximum receive queue size (messages buffered before recv())
 

Functions

acip_transport_tacip_webrtc_transport_create (webrtc_peer_connection_t *peer_conn, webrtc_data_channel_t *data_channel, crypto_context_t *crypto_ctx)
 

Macro Definition Documentation

◆ WEBRTC_RECV_QUEUE_SIZE

#define WEBRTC_RECV_QUEUE_SIZE   64

Maximum receive queue size (messages buffered before recv())

Power of 2 for ringbuffer optimization. 64 messages = ~2-3 seconds of video frames at 30 FPS, enough for network jitter and processing delays.

Definition at line 45 of file webrtc/transport.c.

Function Documentation

◆ acip_webrtc_transport_create()

acip_transport_t * acip_webrtc_transport_create ( webrtc_peer_connection_t peer_conn,
webrtc_data_channel_t data_channel,
crypto_context_t crypto_ctx 
)

Definition at line 374 of file webrtc/transport.c.

375 {
376 if (!peer_conn || !data_channel) {
377 SET_ERRNO(ERROR_INVALID_PARAM, "peer_conn and data_channel are required");
378 return NULL;
379 }
380
381 // Allocate transport structure
383 if (!transport) {
384 SET_ERRNO(ERROR_MEMORY, "Failed to allocate WebRTC transport");
385 return NULL;
386 }
387
388 // Allocate WebRTC-specific data
390 if (!wrtc_data) {
391 SAFE_FREE(transport);
392 SET_ERRNO(ERROR_MEMORY, "Failed to allocate WebRTC transport data");
393 return NULL;
394 }
395
396 // Create receive queue
398 if (!wrtc_data->recv_queue) {
399 SAFE_FREE(wrtc_data);
400 SAFE_FREE(transport);
401 SET_ERRNO(ERROR_MEMORY, "Failed to create receive queue");
402 return NULL;
403 }
404
405 // Initialize synchronization primitives
406 if (mutex_init(&wrtc_data->queue_mutex) != 0) {
407 ringbuffer_destroy(wrtc_data->recv_queue);
408 SAFE_FREE(wrtc_data);
409 SAFE_FREE(transport);
410 SET_ERRNO(ERROR_INTERNAL, "Failed to initialize queue mutex");
411 return NULL;
412 }
413
414 if (cond_init(&wrtc_data->queue_cond) != 0) {
415 mutex_destroy(&wrtc_data->queue_mutex);
416 ringbuffer_destroy(wrtc_data->recv_queue);
417 SAFE_FREE(wrtc_data);
418 SAFE_FREE(transport);
419 SET_ERRNO(ERROR_INTERNAL, "Failed to initialize queue condition variable");
420 return NULL;
421 }
422
423 if (mutex_init(&wrtc_data->state_mutex) != 0) {
424 cond_destroy(&wrtc_data->queue_cond);
425 mutex_destroy(&wrtc_data->queue_mutex);
426 ringbuffer_destroy(wrtc_data->recv_queue);
427 SAFE_FREE(wrtc_data);
428 SAFE_FREE(transport);
429 SET_ERRNO(ERROR_INTERNAL, "Failed to initialize state mutex");
430 return NULL;
431 }
432
433 // Initialize WebRTC data
434 wrtc_data->peer_conn = peer_conn;
435 wrtc_data->data_channel = data_channel;
436 wrtc_data->is_connected = false; // Will be set to true in on_open callback
437
438 // Register DataChannel callbacks
440 .on_open = webrtc_on_open,
441 .on_close = webrtc_on_close,
442 .on_error = webrtc_on_error,
443 .on_message = webrtc_on_message,
444 .user_data = wrtc_data,
445 };
446
447 asciichat_error_t result = webrtc_datachannel_set_callbacks(data_channel, &callbacks);
448 if (result != ASCIICHAT_OK) {
449 mutex_destroy(&wrtc_data->state_mutex);
450 cond_destroy(&wrtc_data->queue_cond);
451 mutex_destroy(&wrtc_data->queue_mutex);
452 ringbuffer_destroy(wrtc_data->recv_queue);
453 SAFE_FREE(wrtc_data);
454 SAFE_FREE(transport);
455 SET_ERRNO(ERROR_INTERNAL, "Failed to set DataChannel callbacks");
456 return NULL;
457 }
458
459 // Initialize transport
460 transport->methods = &webrtc_methods;
461 transport->crypto_ctx = crypto_ctx;
462 transport->impl_data = wrtc_data;
463
464 log_info("Created WebRTC transport (crypto: %s)", crypto_ctx ? "enabled" : "disabled");
465
466 return transport;
467}
#define SAFE_FREE(ptr)
Definition common.h:320
#define SAFE_MALLOC(size, cast)
Definition common.h:208
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
@ ERROR_MEMORY
Definition error_codes.h:53
@ ASCIICHAT_OK
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
@ ERROR_INTERNAL
Definition error_codes.h:84
#define log_info(...)
Log an INFO message.
int mutex_init(mutex_t *mutex)
Initialize a mutex.
int cond_init(cond_t *cond)
Initialize a condition variable.
int cond_destroy(cond_t *cond)
Destroy a condition variable.
int mutex_destroy(mutex_t *mutex)
Destroy a mutex.
ringbuffer_t * ringbuffer_create(size_t element_size, size_t capacity)
Create a new ring buffer.
Definition ringbuffer.c:28
void ringbuffer_destroy(ringbuffer_t *rb)
Destroy a ring buffer and free its memory.
Definition ringbuffer.c:54
asciichat_error_t webrtc_datachannel_set_callbacks(webrtc_data_channel_t *dc, const webrtc_datachannel_callbacks_t *callbacks)
Set DataChannel callbacks.
Transport instance structure.
Definition transport.h:169
void * impl_data
Transport-specific state.
Definition transport.h:172
const acip_transport_methods_t * methods
Method table (virtual functions)
Definition transport.h:170
crypto_context_t * crypto_ctx
Optional encryption context.
Definition transport.h:171
DataChannel callback structure.
void(* on_open)(webrtc_data_channel_t *dc, void *user_data)
Channel opened.
Receive queue element (variable-length message)
WebRTC transport implementation data.
webrtc_peer_connection_t * peer_conn
Peer connection (owned)
bool is_connected
Connection state.
mutex_t queue_mutex
Protect queue operations.
webrtc_data_channel_t * data_channel
Data channel (owned)
mutex_t state_mutex
Protect state changes.
cond_t queue_cond
Signal when messages arrive.
ringbuffer_t * recv_queue
Receive message queue.
#define WEBRTC_RECV_QUEUE_SIZE
Maximum receive queue size (messages buffered before recv())

References ASCIICHAT_OK, cond_destroy(), cond_init(), acip_transport::crypto_ctx, webrtc_transport_data_t::data_channel, ERROR_INTERNAL, ERROR_INVALID_PARAM, ERROR_MEMORY, acip_transport::impl_data, webrtc_transport_data_t::is_connected, log_info, acip_transport::methods, mutex_destroy(), mutex_init(), webrtc_datachannel_callbacks_t::on_open, webrtc_transport_data_t::peer_conn, webrtc_transport_data_t::queue_cond, webrtc_transport_data_t::queue_mutex, webrtc_transport_data_t::recv_queue, ringbuffer_create(), ringbuffer_destroy(), SAFE_FREE, SAFE_MALLOC, SET_ERRNO, webrtc_transport_data_t::state_mutex, webrtc_datachannel_set_callbacks(), and WEBRTC_RECV_QUEUE_SIZE.