From 97261b79b20e4e53ef3a5b7a37d1ffb0b5665bcb Mon Sep 17 00:00:00 2001 From: Chris Hiszpanski Date: Sat, 21 May 2022 15:25:35 -0700 Subject: * Added trtc_ prefixes * Split into tinyrtc.c/h * Sending answer * Answer validates --- example/index.js | 7 +++++-- example/main.c | 32 ++++++++++++++++++-------------- example/tinyrtc.png | Bin 0 -> 4707 bytes 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 example/tinyrtc.png (limited to 'example') diff --git a/example/index.js b/example/index.js index 35fdbba..a161a10 100644 --- a/example/index.js +++ b/example/index.js @@ -26,10 +26,13 @@ window.onload = function demo() { let d = JSON.parse(event.data); if ('candidate' in d) { if (d.candidate) { - console.log("remote candidate:\n%c%s", "color: blue;", event.data); + console.log("remote candidate:\n%c%s", "color: blue;", d.candidate); } } else if ('sdp' in d) { - console.log("remote answer:\n%c%s", "color: blue;", event.data); + pc.setRemoteDescription(d).catch((e) => { + console.log("local error:\n%c%s", "color: red;", e); + }); + console.log("remote answer:\n%c%s", "color: blue;", d.sdp); } } catch(e) { console.log("remote error:\n%c%s", "color: red;", event.data); diff --git a/example/main.c b/example/main.c index 6223898..33ef4bb 100644 --- a/example/main.c +++ b/example/main.c @@ -17,9 +17,9 @@ static bool matches(const char *json, jsmntok_t *tok, const char *s) { } // Parses ICE candidate from stringified JSON -struct ice_candidate_t parse_ice_candidate(const char *s) +struct trtc_ice_candidate_t parse_ice_candidate(const char *s) { - struct ice_candidate_t c = { 0 }; + struct trtc_ice_candidate_t c = { 0 }; int i, r; jsmn_parser p; @@ -60,7 +60,7 @@ struct ice_candidate_t parse_ice_candidate(const char *s) } // (callback) Called for each discovered local ICE candidate -int on_ice_candidate(const struct ice_candidate_t c, void *arg) { +int on_ice_candidate(const struct trtc_ice_candidate_t c, void *arg) { // send ice candidate over signaling channel printf("{" "\"candidate\":\"%s\"," @@ -78,7 +78,7 @@ int main(int argc, char **argv) setvbuf(stdout, NULL, _IONBF, 0); // create peer connection - struct configuration_t cfg = { + struct trtc_config_t cfg = { .ice_servers = { { .urls = { @@ -87,25 +87,29 @@ int main(int argc, char **argv) } } }; - struct peerconn_t* pc = rtc_peer_connection(cfg); - - printf("alert! starting\n"); + struct trtc_peerconn_t* pc = trtc_peer_connection(cfg); while (true) { int n; - char *m = NULL; - size_t len = 0; + char *msg = NULL; + size_t msglen = 0; - if (n = getline(&m, &len, stdin), -1 == n) { + if (n = getline(&msg, &msglen, stdin), -1 == n) { return -1; } - struct ice_candidate_t c = parse_ice_candidate(m); - - on_ice_candidate(c, NULL); + if (strstr(msg, "\"sdp\"")) { + // got offer. send answer. + printf("{\"sdp\":\"%s\",\"type\":\"answer\"}\n", trtc_create_answer(pc)); + } else if (strstr(msg, "\"candidate\"")) { + struct trtc_ice_candidate_t c = parse_ice_candidate(msg); + on_ice_candidate(c, NULL); + trtc_add_ice_candidate(pc, c); + } else { + printf("unrecognized signaling message\n"); + } - add_ice_candidate(pc, c); } return 0; diff --git a/example/tinyrtc.png b/example/tinyrtc.png new file mode 100644 index 0000000..127dd06 Binary files /dev/null and b/example/tinyrtc.png differ -- cgit v1.2.3