diff --git a/webrtc/.gitignore b/webrtc/.gitignore index c6127b38c1..bab2990ef5 100644 --- a/webrtc/.gitignore +++ b/webrtc/.gitignore @@ -42,11 +42,6 @@ *.idb *.pdb -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf +# Our stuff +*.pem +webrtc-sendrecv diff --git a/webrtc/README.md b/webrtc/README.md new file mode 100644 index 0000000000..3c3ae09ef3 --- /dev/null +++ b/webrtc/README.md @@ -0,0 +1,31 @@ +## GStreamer WebRTC demos + +All demos use the same signalling server in the `signalling/` directory + +You will need the following repositories till the GStreamer WebRTC implementation is merged upstream: + +https://github.com/ystreet/gstreamer/tree/promise + +https://github.com/ystreet/gst-plugins-bad/tree/webrtc + +You can build these with either Autotools gst-uninstalled: + +https://arunraghavan.net/2014/07/quick-start-guide-to-gst-uninstalled-1-x/ + +Or with Meson gst-build: + +https://cgit.freedesktop.org/gstreamer/gst-build/ + +### sendrecv: Send and receive audio and video + +* Serve the `js/` directory on the root of your website, or open https://webrtc.nirbheek.in + - The JS code assumes the signalling server is on port 8443 of the same server serving the HTML +* Build and run the sources in the `gst/` directory on your machine + +```console +$ gcc webrtc-sendrecv.c $(pkg-config --cflags --libs gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) -o webrtc-sendrecv +``` + +* Open the website in a browser and ensure that the status is "Registered with server, waiting for call", and note the `id` too. +* Run `webrtc-sendrecv --peer-id=ID` with the `id` from the browser. You will see state changes and an SDP exchange. +* You will see a bouncing ball + hear red noise in the browser, and your browser's webcam + mic in the gst app diff --git a/webrtc/sendrecv/gst/webrtc-sendrecv.c b/webrtc/sendrecv/gst/webrtc-sendrecv.c new file mode 100644 index 0000000000..8c25f97cfb --- /dev/null +++ b/webrtc/sendrecv/gst/webrtc-sendrecv.c @@ -0,0 +1,596 @@ +/* + * Demo gstreamer app for negotiating and streaming a sendrecv webrtc stream + * with a browser JS app. + * + * gcc webrtc-sendrecv.c $(pkg-config --cflags --libs gstreamer-webrtc-1.0 gstreamer-sdp-1.0 libsoup-2.4 json-glib-1.0) -o webrtc-sendrecv + * + * Author: Nirbheek Chauhan + */ +#include +#include +#include + +/* For signalling */ +#include +#include + +#include + +enum AppState { + APP_STATE_UNKNOWN = 0, + APP_STATE_ERROR = 1, /* generic error */ + SERVER_CONNECTING = 1000, + SERVER_CONNECTION_ERROR, + SERVER_CONNECTED, /* Ready to register */ + SERVER_REGISTERING = 2000, + SERVER_REGISTRATION_ERROR, + SERVER_REGISTERED, /* Ready to call a peer */ + SERVER_CLOSED, /* server connection closed by us or the server */ + PEER_CONNECTING = 3000, + PEER_CONNECTION_ERROR, + PEER_CONNECTED, + PEER_CALL_NEGOTIATING = 4000, + PEER_CALL_STARTED, + PEER_CALL_STOPPING, + PEER_CALL_STOPPED, + PEER_CALL_ERROR, +}; + +static GMainLoop *loop; +static GstElement *pipe1, *webrtc1; + +static SoupWebsocketConnection *ws_conn = NULL; +static enum AppState app_state = 0; +static const gchar *peer_id = NULL; +static const gchar *server_url = "wss://webrtc.nirbheek.in:8443"; + +static GOptionEntry entries[] = +{ + { "peer-id", 0, 0, G_OPTION_ARG_STRING, &peer_id, "String ID of the peer to connect to", "ID" }, + { "server", 0, 0, G_OPTION_ARG_STRING, &server_url, "Signalling server to connect to", "URL" }, +}; + +static gboolean +cleanup_and_quit_loop (const gchar * msg, enum AppState state) +{ + if (msg) + g_printerr ("%s\n", msg); + if (state > 0) + app_state = state; + + if (ws_conn) { + if (soup_websocket_connection_get_state (ws_conn) == + SOUP_WEBSOCKET_STATE_OPEN) + /* This will call us again */ + soup_websocket_connection_close (ws_conn, 1000, ""); + else + g_object_unref (ws_conn); + } + + if (loop) { + g_main_loop_quit (loop); + loop = NULL; + } + + /* To allow usage as a GSourceFunc */ + return G_SOURCE_REMOVE; +} + +static gchar* +get_string_from_json_object (JsonObject * object) +{ + JsonNode *root; + JsonGenerator *generator; + gchar *text; + + /* Make it the root node */ + root = json_node_init_object (json_node_alloc (), object); + generator = json_generator_new (); + json_generator_set_root (generator, root); + text = json_generator_to_data (generator, NULL); + + /* Release everything */ + g_object_unref (generator); + json_node_free (root); + return text; +} + +static void +handle_media_stream (GstPad * pad, GstElement * pipe, const char * convert_name, + const char * sink_name) +{ + GstPad *qpad; + GstElement *q, *conv, *sink; + GstPadLinkReturn ret; + + q = gst_element_factory_make ("queue", NULL); + g_assert (q); + conv = gst_element_factory_make (convert_name, NULL); + g_assert (conv); + sink = gst_element_factory_make (sink_name, NULL); + g_assert (sink); + gst_bin_add_many (GST_BIN (pipe), q, conv, sink, NULL); + gst_element_sync_state_with_parent (q); + gst_element_sync_state_with_parent (conv); + gst_element_sync_state_with_parent (sink); + gst_element_link_many (q, conv, sink, NULL); + + qpad = gst_element_get_static_pad (q, "sink"); + + ret = gst_pad_link (pad, qpad); + g_assert (ret == GST_PAD_LINK_OK); +} + +static void +on_incoming_decodebin_stream (GstElement * decodebin, GstPad * pad, + GstElement * pipe) +{ + GstCaps *caps; + const gchar *name; + + if (!gst_pad_has_current_caps (pad)) { + g_printerr ("Pad '%s' has no caps, can't do anything, ignoring\n", + GST_PAD_NAME (pad)); + return; + } + + caps = gst_pad_get_current_caps (pad); + name = gst_structure_get_name (gst_caps_get_structure (caps, 0)); + + if (g_str_has_prefix (name, "video")) { + handle_media_stream (pad, pipe, "videoconvert", "autovideosink"); + } else if (g_str_has_prefix (name, "audio")) { + handle_media_stream (pad, pipe, "audioconvert", "autoaudiosink"); + } else { + g_printerr ("Unknown pad %s, ignoring", GST_PAD_NAME (pad)); + } +} + +static void +on_incoming_stream (GstElement * webrtc, GstPad * pad, GstElement * pipe) +{ + GstElement *decodebin; + + if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC) + return; + + decodebin = gst_element_factory_make ("decodebin", NULL); + g_signal_connect (decodebin, "pad-added", + G_CALLBACK (on_incoming_decodebin_stream), pipe); + gst_bin_add (GST_BIN (pipe), decodebin); + gst_element_sync_state_with_parent (decodebin); + gst_element_link (webrtc, decodebin); +} + +static void +send_ice_candidate_message (GstElement * webrtc G_GNUC_UNUSED, guint mlineindex, + gchar * candidate, gpointer user_data G_GNUC_UNUSED) +{ + gchar *text; + JsonObject *ice, *msg; + + if (app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop ("Can't send ICE, not in call", APP_STATE_ERROR); + return; + } + + ice = json_object_new (); + json_object_set_string_member (ice, "candidate", candidate); + json_object_set_int_member (ice, "sdpMLineIndex", mlineindex); + msg = json_object_new (); + json_object_set_object_member (msg, "ice", ice); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (ws_conn, text); + g_free (text); +} + +static void +send_sdp_offer (GstWebRTCSessionDescription * offer) +{ + gchar *text; + JsonObject *msg, *sdp; + + if (app_state < PEER_CALL_NEGOTIATING) { + cleanup_and_quit_loop ("Can't send offer, not in call", APP_STATE_ERROR); + return; + } + + text = gst_sdp_message_as_text (offer->sdp); + g_print ("Sending offer:\n%s\n", text); + + sdp = json_object_new (); + json_object_set_string_member (sdp, "type", "offer"); + json_object_set_string_member (sdp, "sdp", text); + g_free (text); + + msg = json_object_new (); + json_object_set_object_member (msg, "sdp", sdp); + text = get_string_from_json_object (msg); + json_object_unref (msg); + + soup_websocket_connection_send_text (ws_conn, text); + g_free (text); +} + +/* Offer created by our pipeline, to be sent to the peer */ +static void +on_offer_received (GstPromise * promise, gpointer user_data) +{ + GstWebRTCSessionDescription *offer = NULL; + gchar *desc; + + g_assert (app_state == PEER_CALL_NEGOTIATING); + + g_assert (promise->result == GST_PROMISE_RESULT_REPLIED); + gst_structure_get (promise->promise, "offer", + GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL); + gst_promise_unref (promise); + + promise = gst_promise_new (); + g_signal_emit_by_name (webrtc1, "set-local-description", offer, promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + + /* Send offer to peer */ + send_sdp_offer (offer); + gst_webrtc_session_description_free (offer); +} + +static void +on_negotiation_needed (GstElement * element, gpointer user_data) +{ + GstPromise *promise = gst_promise_new (); + + app_state = PEER_CALL_NEGOTIATING; + g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise); + gst_promise_set_change_callback (promise, on_offer_received, user_data, + NULL); +} + +#define RTP_CAPS_OPUS "application/x-rtp,media=audio,encoding-name=OPUS,payload=" +#define RTP_CAPS_VP8 "application/x-rtp,media=video,encoding-name=VP8,payload=" + +static gboolean +start_pipeline (void) +{ + GstStateChangeReturn ret; + GError *error = NULL; + + pipe1 = + gst_parse_launch ("webrtcbin name=sendrecv " + "videotestsrc pattern=ball ! queue ! vp8enc deadline=1 ! rtpvp8pay ! " + "queue ! " RTP_CAPS_VP8 "96 ! sendrecv. " + "audiotestsrc wave=red-noise ! queue ! opusenc ! rtpopuspay ! " + "queue ! " RTP_CAPS_OPUS "97 ! sendrecv. ", + &error); + + if (error) { + g_printerr ("Failed to parse launch: %s\n", error->message); + g_error_free (error); + goto err; + } + + webrtc1 = gst_bin_get_by_name (GST_BIN (pipe1), "sendrecv"); + g_assert (webrtc1 != NULL); + + /* This is the gstwebrtc entry point where we create the offer and so on. It + * will be called when the pipeline goes to PLAYING. */ + g_signal_connect (webrtc1, "on-negotiation-needed", + G_CALLBACK (on_negotiation_needed), NULL); + /* We need to transmit this ICE candidate to the browser via the websockets + * signalling server. Incoming ice candidates from the browser need to be + * added by us too, see on_server_message() */ + g_signal_connect (webrtc1, "on-ice-candidate", + G_CALLBACK (send_ice_candidate_message), NULL); + /* Incoming streams will be exposed via this signal */ + g_signal_connect (webrtc1, "pad-added", G_CALLBACK (on_incoming_stream), + pipe1); + /* Lifetime is the same as the pipeline itself */ + gst_object_unref (webrtc1); + + g_print ("Starting pipeline\n"); + ret = gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_PLAYING); + if (ret == GST_STATE_CHANGE_FAILURE) + goto err; + + return TRUE; + +err: + if (pipe1) + g_clear_object (&pipe1); + if (webrtc1) + webrtc1 = NULL; + return FALSE; +} + +static gboolean +setup_call (void) +{ + gchar *msg; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + if (!peer_id) + return FALSE; + + g_print ("Setting up signalling server call with %s\n", peer_id); + app_state = PEER_CONNECTING; + msg = g_strdup_printf ("SESSION %s", peer_id); + soup_websocket_connection_send_text (ws_conn, msg); + g_free (msg); + return TRUE; +} + +static gboolean +register_with_server (void) +{ + gchar *hello; + gint32 our_id; + + if (soup_websocket_connection_get_state (ws_conn) != + SOUP_WEBSOCKET_STATE_OPEN) + return FALSE; + + our_id = g_random_int_range (10, 10000); + g_print ("Registering id %i with server\n", our_id); + app_state = SERVER_REGISTERING; + + /* Register with the server with a random integer id. Reply will be received + * by on_server_message() */ + hello = g_strdup_printf ("HELLO %i", our_id); + soup_websocket_connection_send_text (ws_conn, hello); + g_free (hello); + + return TRUE; +} + +static void +on_server_closed (SoupWebsocketConnection * conn G_GNUC_UNUSED, + gpointer user_data G_GNUC_UNUSED) +{ + app_state = SERVER_CLOSED; + cleanup_and_quit_loop ("Server connection closed", 0); +} + +/* One mega message handler for our asynchronous calling mechanism */ +static void +on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, + GBytes * message, gpointer user_data) +{ + gsize size; + gchar *text, *data; + + switch (type) { + case SOUP_WEBSOCKET_DATA_BINARY: + g_printerr ("Received unknown binary message, ignoring\n"); + g_bytes_unref (message); + return; + case SOUP_WEBSOCKET_DATA_TEXT: + data = g_bytes_unref_to_data (message, &size); + /* Convert to NULL-terminated string */ + text = g_strndup (data, size); + g_free (data); + break; + default: + g_assert_not_reached (); + } + + /* Server has accepted our registration, we are ready to send commands */ + if (g_strcmp0 (text, "HELLO") == 0) { + if (app_state != SERVER_REGISTERING) { + cleanup_and_quit_loop ("ERROR: Received HELLO when not registering", + APP_STATE_ERROR); + goto out; + } + app_state = SERVER_REGISTERED; + g_print ("Registered with server\n"); + /* Ask signalling server to connect us with a specific peer */ + if (!setup_call ()) { + cleanup_and_quit_loop ("ERROR: Failed to setup call", PEER_CALL_ERROR); + goto out; + } + /* Call has been setup by the server, now we can start negotiation */ + } else if (g_strcmp0 (text, "SESSION_OK") == 0) { + if (app_state != PEER_CONNECTING) { + cleanup_and_quit_loop ("ERROR: Received SESSION_OK when not calling", + PEER_CONNECTION_ERROR); + goto out; + } + + app_state = PEER_CONNECTED; + /* Start negotiation (exchange SDP and ICE candidates) */ + if (!start_pipeline ()) + cleanup_and_quit_loop ("ERROR: failed to start pipeline", + PEER_CALL_ERROR); + /* Handle errors */ + } else if (g_str_has_prefix (text, "ERROR")) { + switch (app_state) { + case SERVER_CONNECTING: + app_state = SERVER_CONNECTION_ERROR; + break; + case SERVER_REGISTERING: + app_state = SERVER_REGISTRATION_ERROR; + break; + case PEER_CONNECTING: + app_state = PEER_CONNECTION_ERROR; + break; + case PEER_CONNECTED: + case PEER_CALL_NEGOTIATING: + app_state = PEER_CALL_ERROR; + default: + app_state = APP_STATE_ERROR; + } + cleanup_and_quit_loop (text, 0); + /* Look for JSON messages containing SDP and ICE candidates */ + } else { + JsonNode *root; + JsonObject *object; + JsonParser *parser = json_parser_new (); + if (!json_parser_load_from_data (parser, text, -1, NULL)) { + g_printerr ("Unknown message '%s', ignoring", text); + g_object_unref (parser); + goto out; + } + + root = json_parser_get_root (parser); + if (!JSON_NODE_HOLDS_OBJECT (root)) { + g_printerr ("Unknown json message '%s', ignoring", text); + g_object_unref (parser); + goto out; + } + + object = json_node_get_object (root); + /* Check type of JSON message */ + if (json_object_has_member (object, "sdp")) { + int ret; + const gchar *text; + GstSDPMessage *sdp; + GstWebRTCSessionDescription *answer; + + g_assert (app_state == PEER_CALL_NEGOTIATING); + + g_assert (json_object_has_member (object, "type")); + /* In this example, we always create the offer and receive one answer. + * See tests/examples/webrtcbidirectional.c in gst-plugins-bad for how to + * handle offers from peers and reply with answers using webrtcbin. */ + g_assert_cmpstr (json_object_get_string_member (object, "type"), ==, + "answer"); + + text = json_object_get_string_member (object, "sdp"); + + g_print ("Received answer:\n%s\n", text); + + ret = gst_sdp_message_new (&sdp); + g_assert (ret == GST_SDP_OK); + + ret = gst_sdp_message_parse_buffer (text, strlen (text), sdp); + g_assert (ret == GST_SDP_OK); + + answer = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_ANSWER, + sdp); + g_assert (answer); + + /* Set remote description on our pipeline */ + { + GstPromise *promise = gst_promise_new (); + g_signal_emit_by_name (webrtc1, "set-remote-description", answer, + promise); + gst_promise_interrupt (promise); + gst_promise_unref (promise); + } + + app_state = PEER_CALL_STARTED; + } else if (json_object_has_member (object, "ice")) { + JsonObject *ice; + const gchar *candidate; + gint sdpmlineindex; + + ice = json_object_get_object_member (object, "ice"); + candidate = json_object_get_string_member (ice, "candidate"); + sdpmlineindex = json_object_get_int_member (ice, "sdpMLineIndex"); + + /* Add ice candidate sent by remote peer */ + g_signal_emit_by_name (webrtc1, "add-ice-candidate", sdpmlineindex, + candidate); + } else { + g_printerr ("Ignoring unknown JSON message:\n%s\n", text); + } + g_object_unref (parser); + } + +out: + g_free (text); +} + +static void +on_server_connected (SoupSession * session, GAsyncResult * res, + SoupMessage *msg) +{ + GError *error = NULL; + + ws_conn = soup_session_websocket_connect_finish (session, res, &error); + if (error) { + cleanup_and_quit_loop (error->message, SERVER_CONNECTION_ERROR); + g_error_free (error); + return; + } + + g_assert (ws_conn != NULL); + + app_state = SERVER_CONNECTED; + g_print ("Connected to signalling server\n"); + + g_signal_connect (ws_conn, "closed", G_CALLBACK (on_server_closed), NULL); + g_signal_connect (ws_conn, "message", G_CALLBACK (on_server_message), NULL); + + /* Register with the server so it knows about us and can accept commands */ + register_with_server (); +} + +/* + * Connect to the signalling server. This is the entrypoint for everything else. + */ +static void +connect_to_websocket_server_async (void) +{ + SoupLogger *logger; + SoupMessage *message; + SoupSession *session; + const char *https_aliases[] = {"wss", NULL}; + + session = soup_session_new_with_options (SOUP_SESSION_SSL_STRICT, TRUE, + SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE, + //SOUP_SESSION_SSL_CA_FILE, "/etc/ssl/certs/ca-bundle.crt", + SOUP_SESSION_HTTPS_ALIASES, https_aliases, NULL); + + logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1); + soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger)); + g_object_unref (logger); + + message = soup_message_new (SOUP_METHOD_GET, server_url); + + g_print ("Connecting to server...\n"); + + /* Once connected, we will register */ + soup_session_websocket_connect_async (session, message, NULL, NULL, NULL, + (GAsyncReadyCallback) on_server_connected, message); + app_state = SERVER_CONNECTING; +} + +int +main (int argc, char *argv[]) +{ + SoupSession *session; + GOptionContext *context; + GError *error = NULL; + + context = g_option_context_new ("- gstreamer webrtc sendrecv demo"); + g_option_context_add_main_entries (context, entries, NULL); + g_option_context_add_group (context, gst_init_get_option_group ()); + if (!g_option_context_parse (context, &argc, &argv, &error)) { + g_printerr ("Error initializing: %s\n", error->message); + return -1; + } + + if (!peer_id) { + g_printerr ("--peer-id is a required argument\n"); + return -1; + } + + loop = g_main_loop_new (NULL, FALSE); + + connect_to_websocket_server_async (); + + g_main_loop_run (loop); + + gst_element_set_state (GST_ELEMENT (pipe1), GST_STATE_NULL); + g_print ("Pipeline stopped\n"); + + gst_object_unref (pipe1); + + return 0; +} diff --git a/webrtc/sendrecv/js/index.html b/webrtc/sendrecv/js/index.html new file mode 100644 index 0000000000..0a0b7fec8a --- /dev/null +++ b/webrtc/sendrecv/js/index.html @@ -0,0 +1,26 @@ + + + + + + + + + + +
+
Status: unknown
+
Our id is unknown
+ + diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js new file mode 100644 index 0000000000..31116c017a --- /dev/null +++ b/webrtc/sendrecv/js/webrtc.js @@ -0,0 +1,203 @@ +/* vim: set sts=4 sw=4 et : + * + * Demo Javascript app for negotiating and streaming a sendrecv webrtc stream + * with a GStreamer app. Runs only in passive mode, i.e., responds to offers + * with answers, exchanges ICE candidates, and streams. + * + * Author: Nirbheek Chauhan + */ + +var connect_attempts = 0; + +var peer_connection = null; +var rtc_configuration = {iceServers: [{urls: "stun:stun.services.mozilla.com"}, + {urls: "stun:stun.l.google.com:19302"}]}; +var ws_conn; +var local_stream; +var peer_id; + +function getOurId() { + return Math.floor(Math.random() * (9000 - 10) + 10).toString(); +} + +function resetState() { + // This will call onServerClose() + ws_conn.close(); +} + +function handleIncomingError(error) { + setStatus("ERROR: " + error); + resetState(); +} + +function getVideoElement() { + return document.getElementById("stream"); +} + +function setStatus(text) { + console.log(text); + document.getElementById("status").textContent = text; +} + +function resetVideoElement() { + var videoElement = getVideoElement(); + videoElement.pause(); + videoElement.src = ""; + videoElement.load(); +} + +// SDP offer received from peer, set remote description and create an answer +function onIncomingSDP(sdp) { + console.log("Incoming SDP is "+ JSON.stringify(sdp)); + peer_connection.setRemoteDescription(sdp).then(() => { + setStatus("Remote SDP set"); + if (sdp.type != "offer") + return; + setStatus("Got SDP offer, creating answer"); + peer_connection.createAnswer().then(onLocalDescription).catch(setStatus); + }).catch(setStatus); +} + +// Local description was set, send it to peer +function onLocalDescription(desc) { + console.log("Got local description: " + JSON.stringify(desc)); + peer_connection.setLocalDescription(desc).then(function() { + setStatus("Sending SDP answer"); + ws_conn.send(JSON.stringify(peer_connection.localDescription)); + }); +} + +// ICE candidate received from peer, add it to the peer connection +function onIncomingICE(ice) { + console.log("Incoming ICE: " + JSON.stringify(ice)); + var candidate = new RTCIceCandidate(ice); + peer_connection.addIceCandidate(candidate).catch(setStatus); +} + +function onServerMessage(event) { + console.log("Received " + event.data); + switch (event.data) { + case "HELLO": + setStatus("Registered with server, waiting for call"); + return; + default: + if (event.data.startsWith("ERROR")) { + handleIncomingError(event.data); + return; + } + // Handle incoming JSON SDP and ICE messages + try { + msg = JSON.parse(event.data); + } catch (e) { + if (e instanceof SyntaxError) { + handleIncomingError("Error parsing incoming JSON: " + event.data); + } else { + handleIncomingError("Unknown error parsing response: " + event.data); + } + return; + } + + // Incoming JSON signals the beginning of a call + if (peer_connection == null) + createCall(msg); + + if (msg.sdp != null) { + onIncomingSDP(msg.sdp); + } else if (msg.ice != null) { + onIncomingICE(msg.ice); + } else { + handleIncomingError("Unknown incoming JSON: " + msg); + } + } +} + +function onServerClose(event) { + resetVideoElement(); + + if (peer_connection != null) { + peer_connection.close(); + peer_connection = null; + } + + // Reset after a second + window.setTimeout(websocketServerConnect, 1000); +} + +function onServerError(event) { + setStatus("Unable to connect to server, did you add an exception for the certificate?") + // Retry after 3 seconds + window.setTimeout(websocketServerConnect, 3000); +} + +function websocketServerConnect() { + connect_attempts++; + if (connect_attempts > 3) { + setStatus("Too many connection attempts, aborting. Refresh page to try again"); + return; + } + peer_id = getOurId(); + setStatus("Connecting to server"); + ws_conn = new WebSocket('wss://' + window.location.hostname + ':8443'); + /* When connected, immediately register with the server */ + ws_conn.addEventListener('open', (event) => { + document.getElementById("peer-id").textContent = peer_id; + ws_conn.send('HELLO ' + peer_id); + setStatus("Registering with server"); + }); + ws_conn.addEventListener('error', onServerError); + ws_conn.addEventListener('message', onServerMessage); + ws_conn.addEventListener('close', onServerClose); + + var constraints = {video: true, audio: true}; + + // Add local stream + if (navigator.mediaDevices.getUserMedia) { + navigator.mediaDevices.getUserMedia(constraints) + .then((stream) => { local_stream = stream }) + .catch(errorUserMediaHandler); + } else { + errorUserMediaHandler(); + } +} + +function onRemoteStreamAdded(event) { + videoTracks = event.stream.getVideoTracks(); + audioTracks = event.stream.getAudioTracks(); + + if (videoTracks.length > 0) { + console.log('Incoming stream: ' + videoTracks.length + ' video tracks and ' + audioTracks.length + ' audio tracks'); + getVideoElement().srcObject = event.stream; + } else { + handleIncomingError('Stream with unknown tracks added, resetting'); + } +} + +function errorUserMediaHandler() { + setStatus("Browser doesn't support getUserMedia!"); +} + +function createCall(msg) { + // Reset connection attempts because we connected successfully + connect_attempts = 0; + + peer_connection = new RTCPeerConnection(rtc_configuration); + peer_connection.onaddstream = onRemoteStreamAdded; + /* Send our video/audio to the other peer */ + peer_connection.addStream(local_stream); + + if (!msg.sdp) { + console.log("WARNING: First message wasn't an SDP message!?"); + } + + peer_connection.onicecandidate = (event) => { + // We have a candidate, send it to the remote party with the + // same uuid + if (event.candidate == null) { + console.log("ICE Candidate was null, done"); + return; + } + ws_conn.send(JSON.stringify({'ice': event.candidate})); + }; + + setStatus("Created peer connection for call, waiting for SDP"); +}