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

👤 Client-side session participation implementation More...

Go to the source code of this file.

Data Structures

struct  session_participant
 Client session state spanning connection, transport, media workers, and callbacks. More...

Macros

#define SESSION_PARTICIPANT_WEBCAM_INDEX_BUFFER_SIZE   32

Typedefs

typedef struct session_participant session_participant_t
 Client session state spanning connection, transport, media workers, and callbacks.

Functions

session_participant_t * session_participant_create (const session_participant_config_t *config)
 Server address, security, media, transport, and callback settings for a participant.
void session_participant_destroy (session_participant_t *p)
 Destroy session participant and free resources.
asciichat_error_t session_participant_connect (session_participant_t *p)
 Connect to session server.
void session_participant_disconnect (session_participant_t *p)
 Disconnect from session server.
bool session_participant_is_connected (session_participant_t *p)
 Check if participant is connected.
uint32_t session_participant_get_client_id (session_participant_t *p)
 Get assigned client ID.
bool session_participant_is_video_active (session_participant_t *p)
 Check if video is streaming.
bool session_participant_is_audio_active (session_participant_t *p)
 Check if audio is streaming.
asciichat_error_t session_participant_get_settings (session_participant_t *p, session_settings_t *settings)
 Get current session settings.
asciichat_error_t session_participant_request_settings (session_participant_t *p, const session_settings_t *settings)
 Request this participant's remote rendering update.
asciichat_error_t session_participant_start_video (session_participant_t *p)
 Start video capture and streaming.
void session_participant_stop_video (session_participant_t *p)
 Stop and join video capture, then release its resources Also stops audio when it may borrow the video media source.
asciichat_error_t session_participant_start_audio (session_participant_t *p)
 Start audio capture and streaming.
void session_participant_stop_audio (session_participant_t *p)
 Stop audio capture and streaming.
asciichat_error_t session_participant_set_transport (session_participant_t *p, acip_transport_t *transport, acip_transport_ownership_t ownership)
 Select a transport and its ownership mode for the participant.
asciichat_error_t session_participant_take_connection (session_participant_t *p, network_connection_t **connection)
asciichat_error_t session_participant_set_connection (session_participant_t *p, network_connection_t **owner)
 Detach the selected transport and transfer its ownership responsibility.
acip_transport_t * session_participant_get_transport (session_participant_t *p)
 Get the current transport for the participant.
bool session_participant_has_transport (session_participant_t *p)
 Check if participant has a selected transport.
network_connection_t * session_participant_connection (session_participant_t *p)
asciichat_error_t session_participant_receive_settings (session_participant_t *p, const void *payload, size_t length)

Detailed Description

👤 Client-side session participation implementation

Implements the session participant abstraction for client-side connection management and media streaming.

Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
January 2026

Definition in file participant.c.

Macro Definition Documentation

◆ SESSION_PARTICIPANT_WEBCAM_INDEX_BUFFER_SIZE

#define SESSION_PARTICIPANT_WEBCAM_INDEX_BUFFER_SIZE   32

Definition at line 1 of file participant.c.

Referenced by session_participant_start_video().

Function Documentation

◆ session_participant_connection()

network_connection_t * session_participant_connection ( session_participant_t * p)

Borrow the connection until the participant stops or transfers ownership.

Definition at line 801 of file participant.c.

801 {
802 return p ? p->network : NULL;
803}
network_connection_t * network
Definition participant.c:92

References session_participant::network.

◆ session_participant_receive_settings()

asciichat_error_t session_participant_receive_settings ( session_participant_t * p,
const void * payload,
size_t length )

Dispatch SETTINGS_ACK on the connection owner thread; callbacks run outside locks.

Definition at line 805 of file participant.c.

805 {
806 if (!p || !p->initialized)
807 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid participant");
809 const char *json;
810 size_t json_length;
811 asciichat_error_t result = acip_settings_decode(payload, length, &header, &json, &json_length);
812 if (result != ASCIICHAT_OK)
813 return result;
814 mutex_lock(&p->settings_mutex);
815 if ((header.request && (header.request > p->settings_request || header.request < p->settings_applied_request)) || header.revision < p->settings.version) {
816 mutex_unlock(&p->settings_mutex);
817 return ASCIICHAT_OK;
818 }
819 options_t *current = SAFE_MALLOC(sizeof(*current), options_t *), *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
821 result = options_prepare_json(current, json, json_length, candidate, true);
822 if (result == ASCIICHAT_OK) {
824 p->settings.version = header.revision;
825 if (header.request)
826 p->settings_applied_request = header.request;
827 }
828 session_settings_t effective = p->settings;
829 SAFE_FREE(current);
830 SAFE_FREE(candidate);
831 mutex_unlock(&p->settings_mutex);
832 if (result != ASCIICHAT_OK)
833 return result;
834 if (header.status && p->callbacks.on_error)
835 p->callbacks.on_error(p, (asciichat_error_t)header.status, "Remote rendering update rejected", p->user_data);
837 p->callbacks.on_settings_changed(p, &effective, p->user_data);
838 return ASCIICHAT_OK;
839}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
asciichat_error_t acip_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
asciichat_error_t options_prepare_json(const options_t *current, const char *json, size_t length, options_t *candidate, bool remote_only)
Definition rcu.c:977
void session_settings_to_options(const session_settings_t *settings, options_t *values)
void session_settings_from_values(const options_t *values, session_settings_t *settings)
acip settings header keeps the values that move through the subsystem: schema, request,...
void(* on_settings_changed)(session_participant_t *p, const session_settings_t *settings, void *user_data)
Called when session settings change.
void(* on_error)(session_participant_t *p, asciichat_error_t error, const char *message, void *user_data)
Called when an error occurs.
session_settings_t settings
Current session settings.
uint32_t settings_applied_request
bool initialized
Context is initialized.
session_participant_callbacks_t callbacks
Event callbacks.
Definition participant.c:85
void * user_data
User data for callbacks.
Definition participant.c:88
uint32_t settings_request
When code needs to work with session settings, it keeps version, width, height, color_mode here inste...

References acip_settings_decode(), ASCIICHAT_OK, session_participant::callbacks, ERROR_INVALID_PARAM, session_participant::initialized, session_participant_callbacks_t::on_error, session_participant_callbacks_t::on_settings_changed, options_prepare_json(), SAFE_FREE, SAFE_MALLOC, session_settings_from_values(), session_settings_to_options(), SET_ERRNO, session_participant::settings, session_participant::settings_applied_request, session_participant::settings_mutex, session_participant::settings_request, session_participant::user_data, and session_settings_t::version.

◆ session_participant_set_connection()

asciichat_error_t session_participant_set_connection ( session_participant_t * p,
network_connection_t ** owner )

Detach the selected transport and transfer its ownership responsibility.

Parameters
pParticipant whose media workers have been stopped
transportReceives the detached transport, or NULL if none
ownershipReceives the detached transport's ownership mode
Returns
ASCIICHAT_OK on success; outputs are unchanged on failure

Definition at line 770 of file participant.c.

770 {
771 if (!owner || !*owner)
772 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing participant connection");
774 if (result != ASCIICHAT_OK)
775 return result;
776 p->network = *owner;
777 *owner = NULL;
781 p->connected = result == ASCIICHAT_OK && acip_transport_is_connected(p->transport);
782 return result;
783}
asciichat_error_t session_participant_set_transport(session_participant_t *p, acip_transport_t *transport, acip_transport_ownership_t ownership)
Select a transport and its ownership mode for the participant.
acip_transport_t * network_connection_transport(network_connection_t *c)
asciichat_error_t session_connection_start(session_connection_t *connection)
bool connected
Currently connected.
Definition participant.c:98
struct acip_transport * transport
Selected transport used by all media workers.
Definition participant.c:91
session_connection_t connection
Definition participant.c:59
acip_transport_ownership_t transport_ownership
Responsibility for releasing the selected transport.
Definition participant.c:95

References ACIP_TRANSPORT_BORROWED, ACIP_TRANSPORT_OWNED, ASCIICHAT_OK, session_participant::connected, session_participant::connection, ERROR_INVALID_PARAM, session_participant::network, network_connection_transport(), session_connection_start(), session_participant_set_transport(), SET_ERRNO, session_participant::transport, and session_participant::transport_ownership.

◆ session_participant_take_connection()

asciichat_error_t session_participant_take_connection ( session_participant_t * p,
network_connection_t ** connection )

Definition at line 755 of file participant.c.

755 {
756 if (!p || !p->initialized || !connection)
757 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing participant connection output");
759 return SET_ERRNO(ERROR_INVALID_STATE, "Stop media before transferring its connection");
760 if (!p->network)
761 return SET_ERRNO(ERROR_INVALID_STATE, "Participant does not own a connection");
762 *connection = p->network;
763 p->network = NULL;
764 p->transport = NULL;
766 p->connected = false;
767 return ASCIICHAT_OK;
768}
bool session_av_state_should_run(session_av_state_t *state)
Definition av_state.c:45
@ ERROR_INVALID_STATE
session_av_state_t audio
Audio streaming active.
bool audio_thread_created
Audio worker handle requires joining.
session_av_state_t video
Video streaming active.
bool video_thread_created
Video worker handle requires joining.

References ACIP_TRANSPORT_BORROWED, ASCIICHAT_OK, session_participant::audio, session_participant::audio_thread_created, session_participant::connected, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, session_participant::initialized, session_participant::network, session_av_state_should_run(), SET_ERRNO, session_participant::transport, session_participant::transport_ownership, session_participant::video, and session_participant::video_thread_created.