From 9175048aa48dee44946bdf1f10f47fe5b3e47675 Mon Sep 17 00:00:00 2001 From: Chris Hiszpanski Date: Mon, 23 May 2022 01:14:00 -0700 Subject: Rename from trtc_ to rtc_ --- example/main.c | 26 ++++++++-------- tinyrtc.c | 63 +++++++++++++++++++++------------------ tinyrtc.h | 93 +++++++++++++++++++++++++++++++++------------------------- 3 files changed, 100 insertions(+), 82 deletions(-) diff --git a/example/main.c b/example/main.c index 77b68da..a5f786c 100644 --- a/example/main.c +++ b/example/main.c @@ -18,9 +18,9 @@ static bool matches(const char *json, jsmntok_t *tok, const char *s) { } // Parses ICE candidate from stringified JSON -struct trtc_ice_candidate_t parse_ice_candidate(const char *s) +struct rtc_ice_candidate parse_ice_candidate(const char *s) { - struct trtc_ice_candidate_t c = { 0 }; + struct rtc_ice_candidate c = { 0 }; int i, r; jsmn_parser p; @@ -61,7 +61,7 @@ struct trtc_ice_candidate_t parse_ice_candidate(const char *s) } // (callback) Called for each discovered local ICE candidate -void on_ice_candidate(const struct trtc_ice_candidate_t c, void *arg) { +void on_ice_candidate(const struct rtc_ice_candidate c, void *arg) { // send ice candidate over signaling channel printf("{" "\"candidate\":\"%s\"," @@ -79,10 +79,10 @@ int main(int argc, char **argv) setvbuf(stdout, NULL, _IONBF, 0); // initialize tinyrtc library - trtc_init(); + rtc_init(); // create peer connection - struct trtc_config_t cfg = { + struct rtc_configuration cfg = { .ice_servers = { { .urls = { @@ -91,13 +91,13 @@ int main(int argc, char **argv) } } }; - struct trtc_peerconn_t* pc = trtc_peer_connection(cfg); + struct rtc_peer_connection* pc = rtc_peer_connection_create(cfg); if (!pc) { - syslog(LOG_ERR, "trtc_peer_connection() failed"); + syslog(LOG_ERR, "rtc_peer_connection_create() failed"); return -1; } - trtc_set_on_ice_candidate(pc, on_ice_candidate, NULL); + rtc_set_on_ice_candidate(pc, on_ice_candidate, NULL); while (true) { int n; @@ -112,18 +112,18 @@ int main(int argc, char **argv) // case: sdp offer if (strstr(msg, "\"sdp\"")) { - // XXX trtc_set_remote_description(pc, offer); - const char *answer = trtc_create_answer(pc); + // XXX rtc_set_remote_description(pc, offer); + const char *answer = rtc_create_answer(pc); /* send SDP answer via stdout signaling channel */ printf("{\"sdp\":\"%s\",\"type\":\"answer\"}\n", answer); - trtc_set_local_description(pc, answer); + rtc_set_local_description(pc, answer); // case: ice candidate } else if (strstr(msg, "\"candidate\"")) { - struct trtc_ice_candidate_t c = parse_ice_candidate(msg); - trtc_add_ice_candidate(pc, c); + struct rtc_ice_candidate c = parse_ice_candidate(msg); + rtc_add_ice_candidate(pc, c); // case: error } else { diff --git a/tinyrtc.c b/tinyrtc.c index 54bb9b9..98aba07 100644 --- a/tinyrtc.c +++ b/tinyrtc.c @@ -42,11 +42,11 @@ /// OPAQUE TYPE DEFINITIONS //////////////////////////////////////////////// -struct trtc_peerconn_t { +struct rtc_peer_connection { bool active; - char offer[TRTC_MAX_SDP_SIZE]; - char answer[TRTC_MAX_SDP_SIZE]; + char offer[TINYRTC_MAX_SDP_SIZE]; + char answer[TINYRTC_MAX_SDP_SIZE]; /* local description */ const char* ldesc; @@ -54,11 +54,11 @@ struct trtc_peerconn_t { /* remote description */ const char* rdesc; - char l_ice_pwd[TRTC_MAX_ICE_PWD_SIZE]; - char l_ice_ufrag[TRTC_MAX_ICE_UFRAG_SIZE]; + char l_ice_pwd[TINYRTC_MAX_ICE_PWD_SIZE]; + char l_ice_ufrag[TINYRTC_MAX_ICE_UFRAG_SIZE]; /* onIceCandidate callback */ - trtc_on_ice_candidate_t* on_ice_candidate; + rtc_on_ice_candidate* on_ice_candidate; void* on_ice_candidate_arg; /* tcp host candidate socket */ @@ -68,12 +68,13 @@ struct trtc_peerconn_t { /// STATIC ALLOCATIONS ///////////////////////////////////////////////////// -static struct trtc_peerconn_t peer_connection_pool[TRTC_MAX_PEER_CONNECTIONS]; +static struct rtc_peer_connection +peer_connection_pool[TINYRTC_MAX_PEER_CONNECTIONS]; /// RTC PEER CONNECTION API //////////////////////////////////////////////// -void trtc_init() { +void rtc_init() { memset(peer_connection_pool, 0, sizeof(peer_connection_pool)); } @@ -82,7 +83,7 @@ void trtc_init() { * * \return 0 on success, -1 on error. */ -static int get_host_candidates(struct trtc_peerconn_t* pc) { +static int get_host_candidates(struct rtc_peer_connection* pc) { struct ifaddrs *ifaddr, *ifa; socklen_t laddrlen; @@ -130,11 +131,11 @@ static int get_host_candidates(struct trtc_peerconn_t* pc) { struct sockaddr_in *sa = (struct sockaddr_in*)ifa->ifa_addr; /* host tcp candidate */ - struct trtc_ice_candidate_t c = { + struct rtc_ice_candidate c = { .sdp_mid = "0", .sdp_mline_index = 0 }; - snprintf(c.candidate, TRTC_MAX_ICE_CANDIDATE_SIZE, + snprintf(c.candidate, TINYRTC_MAX_ICE_CANDIDATE_SIZE, "candidate:2894319779 1 tcp 212260223 %s %d typ host tcptype passive", inet_ntoa(sa->sin_addr), laddr.sin_port); // XXXX @@ -165,12 +166,12 @@ static void* peer_connection_thread(void *arg) { return NULL; } -struct trtc_peerconn_t* trtc_peer_connection(struct trtc_config_t cfg) { - struct trtc_peerconn_t* pc = NULL; +struct rtc_peer_connection* rtc_peer_connection_create(struct rtc_configuration cfg) { + struct rtc_peer_connection* pc = NULL; - for (int i = 0; i < TRTC_MAX_PEER_CONNECTIONS; i++) { + for (int i = 0; i < TINYRTC_MAX_PEER_CONNECTIONS; i++) { if (!peer_connection_pool[i].active) { - memset(&peer_connection_pool[i], 0, sizeof(struct trtc_peerconn_t)); + memset(&peer_connection_pool[i], 0, sizeof(struct rtc_peer_connection)); pc = &peer_connection_pool[i]; break; } @@ -179,7 +180,8 @@ struct trtc_peerconn_t* trtc_peer_connection(struct trtc_config_t cfg) { return pc; } -void trtc_peer_connection_destroy(struct trtc_peerconn_t* pc) { +void rtc_peer_connection_destroy(struct rtc_peer_connection* pc) +{ if (!pc) return; close(pc->host_tcp_fd); @@ -187,11 +189,9 @@ void trtc_peer_connection_destroy(struct trtc_peerconn_t* pc) { // XXX mark as available } -int trtc_set_on_ice_candidate( - struct trtc_peerconn_t* pc, - trtc_on_ice_candidate_t* cb, - void* arg -) { +int rtc_set_on_ice_candidate(struct rtc_peer_connection* pc, + rtc_on_ice_candidate* cb, void* arg) +{ if (!pc || !cb) return -1; pc->on_ice_candidate = cb; @@ -200,21 +200,24 @@ int trtc_set_on_ice_candidate( return 0; } -int trtc_add_ice_candidate(struct trtc_peerconn_t *pc, const struct trtc_ice_candidate_t c) { +int rtc_add_ice_candidate(struct rtc_peer_connection* pc, + const struct rtc_ice_candidate c) +{ return -1; }; -const char * trtc_create_answer(struct trtc_peerconn_t *pc) { +const char* rtc_create_answer(struct rtc_peer_connection* pc) +{ unsigned char fp[32]; - snprintf(pc->l_ice_ufrag, TRTC_MAX_ICE_UFRAG_SIZE, "xxxx"); // XXX - snprintf(pc->l_ice_pwd, TRTC_MAX_ICE_PWD_SIZE, "xxxxxxxxxxxxxxxxxxxxxx"); // XXX + snprintf(pc->l_ice_ufrag, TINYRTC_MAX_ICE_UFRAG_SIZE, "xxxx"); // XXX + snprintf(pc->l_ice_pwd, TINYRTC_MAX_ICE_PWD_SIZE, "xxxxxxxxxxxxxxxxxxxxxx"); // XXX - snprintf(pc->answer, TRTC_MAX_SDP_SIZE, + snprintf(pc->answer, TINYRTC_MAX_SDP_SIZE, "v=0\\r\\n" "o=- 2210401696197537454 2 IN IP4 127.0.0.1\\r\\n" // XXX "s=-\\r\\n" - "u=https://tinyrtc.org/\\r\\n" + "u=https://liburtc.org/\\r\\n" "t=0 0\\r\\n" "a=group:BUNDLE 0\\r\\n" "a=msid-semantic: WMS\\r\\n" @@ -249,13 +252,15 @@ const char * trtc_create_answer(struct trtc_peerconn_t *pc) { return pc->answer; }; -int trtc_set_local_description(struct trtc_peerconn_t *pc, const char *sdp) { +int rtc_set_local_description(struct rtc_peer_connection* pc, const char* sdp) +{ pc->ldesc = sdp; get_host_candidates(pc); return -1; }; -int trtc_set_remote_description(struct trtc_peerconn_t *pc, const char *sdp) { +int rtc_set_remote_description(struct rtc_peer_connection* pc, const char* sdp) +{ pc->rdesc = sdp; return -1; }; diff --git a/tinyrtc.h b/tinyrtc.h index 83666d1..02c8459 100644 --- a/tinyrtc.h +++ b/tinyrtc.h @@ -29,48 +29,62 @@ #ifndef TINYRTC_H #define TINYRTC_H -#define TRTC_MAX_ICE_CANDIDATE_SIZE 256 -#define TRTC_MAX_ICE_SERVER_CREDENTIAL_SIZE 128 -#define TRTC_MAX_ICE_SERVERS 2 -#define TRTC_MAX_ICE_SERVER_URL_SIZE 128 -#define TRTC_MAX_ICE_SERVER_URLS 3 -#define TRTC_MAX_ICE_SERVER_USERNAME_SIZE 128 -#define TRTC_MAX_ICE_CANDIDATE_USERNAME_FRAGMENT_SIZE 8 -#define TRTC_MAX_ICE_CANDIDATE_SDP_MID_SIZE 32 -#define TRTC_MAX_PEER_CONNECTIONS 5 -#define TRTC_MAX_SDP_SIZE 4096 -#define TRTC_MAX_ICE_UFRAG_SIZE 5 -#define TRTC_MAX_ICE_PWD_SIZE 32 - - -/// PRIVATE TYPE DEFINITIONS /////////////////////////////////////////////// +/// CUSTOMIZABLE PARAMETERS //////////////////////////////////////////////// + +#define TINYRTC_MAX_ICE_CANDIDATE_SIZE 256 +#define TINYRTC_MAX_ICE_SERVER_CREDENTIAL_SIZE 128 +#define TINYRTC_MAX_ICE_SERVERS 2 +#define TINYRTC_MAX_ICE_SERVER_URL_SIZE 128 +#define TINYRTC_MAX_ICE_SERVER_URLS 3 +#define TINYRTC_MAX_ICE_SERVER_USERNAME_SIZE 128 +#define TINYRTC_MAX_ICE_CANDIDATE_USERNAME_FRAGMENT_SIZE 8 +#define TINYRTC_MAX_ICE_CANDIDATE_SDP_MID_SIZE 32 +#define TINYRTC_MAX_PEER_CONNECTIONS 5 +#define TINYRTC_MAX_SDP_SIZE 4096 +#define TINYRTC_MAX_ICE_UFRAG_SIZE 5 +#define TINYRTC_MAX_ICE_PWD_SIZE 32 + + +/// PUBLIC ENUMS /////////////////////////////////////////////////////////// + +enum rtc_session_description_type { + RTC_SESSION_DESCRIPTION_TYPE_NONE = 0, + RTC_SESSION_DESCRIPTION_TYPE_OFFER, + RTC_SESSION_DESCRIPTION_TYPE_ANSWER +}; -struct trtc_peerconn_t; +/// PUBLIC STRUCTS ///////////////////////////////////////////////////////// -/// PUBLIC TYPE DEFINITIONS //////////////////////////////////////////////// +struct rtc_peer_connection; -struct trtc_ice_server_t { - char credential[TRTC_MAX_ICE_SERVER_CREDENTIAL_SIZE]; - char urls[TRTC_MAX_ICE_SERVER_URLS][TRTC_MAX_ICE_SERVER_URL_SIZE]; - char username[TRTC_MAX_ICE_SERVER_USERNAME_SIZE]; +struct rtc_ice_server { + char credential[TINYRTC_MAX_ICE_SERVER_CREDENTIAL_SIZE]; + char urls[TINYRTC_MAX_ICE_SERVER_URLS][TINYRTC_MAX_ICE_SERVER_URL_SIZE]; + char username[TINYRTC_MAX_ICE_SERVER_USERNAME_SIZE]; }; -struct trtc_config_t { - struct trtc_ice_server_t ice_servers[TRTC_MAX_ICE_SERVERS]; +struct rtc_configuration { + struct rtc_ice_server ice_servers[TINYRTC_MAX_ICE_SERVERS]; }; -struct trtc_ice_candidate_t { - char candidate[TRTC_MAX_ICE_CANDIDATE_SIZE]; +struct rtc_ice_candidate { + char candidate[TINYRTC_MAX_ICE_CANDIDATE_SIZE]; int sdp_mline_index; - char sdp_mid[TRTC_MAX_ICE_CANDIDATE_SDP_MID_SIZE]; - char username_fragment[TRTC_MAX_ICE_CANDIDATE_USERNAME_FRAGMENT_SIZE]; + char sdp_mid[TINYRTC_MAX_ICE_CANDIDATE_SDP_MID_SIZE]; + char username_fragment[TINYRTC_MAX_ICE_CANDIDATE_USERNAME_FRAGMENT_SIZE]; +}; + +struct rtc_session_description { + char sdp[TINYRTC_MAX_SDP_SIZE]; + enum rtc_session_description_type type; }; /// CALLBACKS ////////////////////////////////////////////////////////////// -typedef void (trtc_on_ice_candidate_t)(const struct trtc_ice_candidate_t c, void *arg); +typedef void (rtc_on_ice_candidate)(const struct rtc_ice_candidate c, + void *arg); /// RTC PEER CONNECTION API //////////////////////////////////////////////// @@ -78,7 +92,7 @@ typedef void (trtc_on_ice_candidate_t)(const struct trtc_ice_candidate_t c, void /** * Initialize library. Must call once before calling other functions. */ -void trtc_init(); +void rtc_init(); /** * Create new peer connection @@ -87,9 +101,10 @@ void trtc_init(); * * \return Peer connection. NULL if all peer connections are already in use. */ -struct trtc_peerconn_t* trtc_peer_connection(struct trtc_config_t cfg); +struct rtc_peer_connection* rtc_peer_connection_create( + struct rtc_configuration cfg); -void trtc_peer_connection_destroy(struct trtc_peerconn_t* pc); +void rtc_peer_connection_destroy(struct rtc_peer_connection* pc); /** * Adds remote ICE candidate to local ICE agent @@ -99,10 +114,8 @@ void trtc_peer_connection_destroy(struct trtc_peerconn_t* pc); * * \return 0 on success. -1 on error. */ -int trtc_add_ice_candidate( - struct trtc_peerconn_t *pc, const struct trtc_ice_candidate_t c); - -int trtc_add_track(struct trtc_peerconn_t *pc); +int rtc_add_ice_candidate( + struct rtc_peer_connection* pc, const struct rtc_ice_candidate c); /** * Create SDP answer in response to a received SDP offer. @@ -111,7 +124,7 @@ int trtc_add_track(struct trtc_peerconn_t *pc); * * \return SDP answer. NULL on error. */ -const char * trtc_create_answer(struct trtc_peerconn_t *pc); +const char* rtc_create_answer(struct rtc_peer_connection* pc); /** * Sets callback to call for each discovered local ICE candidate @@ -122,11 +135,11 @@ const char * trtc_create_answer(struct trtc_peerconn_t *pc); * * \return 0 on success. -1 on error. */ -int trtc_set_on_ice_candidate( - struct trtc_peerconn_t* pc, trtc_on_ice_candidate_t* cb, void* arg); +int rtc_set_on_ice_candidate( + struct rtc_peer_connection* pc, rtc_on_ice_candidate* cb, void* arg); -int trtc_set_local_description(struct trtc_peerconn_t *pc, const char *sdp); -int trtc_set_remote_description(struct trtc_peerconn_t *pc, const char *sdp); +int rtc_set_local_description(struct rtc_peer_connection* pc, const char* sdp); +int rtc_set_remote_description(struct rtc_peer_connection* pc, const char* sdp); #endif -- cgit v1.2.3