From f8cbae9d6eb85381c4428998880dfcd863e205d3 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Tue, 9 Feb 2021 15:28:57 +0530 Subject: [PATCH] sendrecv: Implement remote-offerer option for JS example Now you can check the "Remote offerer" checkbox in the JS example to force the peer to send the SDP offer. This involved implementing support for receiving the OFFER_REQUEST message in the C example. As a side-effect of this, the C example will no longer send OFFER_REQUEST automatically when the --our-id option is passed. It will only do so when the --remote-offerer option is explicitly passed. Part-of: --- webrtc/sendrecv/gst/webrtc-sendrecv.c | 23 +++++++++++++++++------ webrtc/sendrecv/js/index.html | 3 +++ webrtc/sendrecv/js/webrtc.js | 9 +++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/webrtc/sendrecv/gst/webrtc-sendrecv.c b/webrtc/sendrecv/gst/webrtc-sendrecv.c index e16a8af8ad..709ac70102 100644 --- a/webrtc/sendrecv/gst/webrtc-sendrecv.c +++ b/webrtc/sendrecv/gst/webrtc-sendrecv.c @@ -285,11 +285,12 @@ on_offer_created (GstPromise * promise, gpointer user_data) static void on_negotiation_needed (GstElement * element, gpointer user_data) { + gboolean create_offer = GPOINTER_TO_INT (user_data); app_state = PEER_CALL_NEGOTIATING; - if (remote_is_offerer || our_id) { + if (remote_is_offerer) { soup_websocket_connection_send_text (ws_conn, "OFFER_REQUEST"); - } else { + } else if (create_offer) { GstPromise *promise = gst_promise_new_with_change_func (on_offer_created, NULL, NULL); g_signal_emit_by_name (webrtc1, "create-offer", NULL, promise); @@ -372,7 +373,7 @@ on_ice_gathering_state_notify (GstElement * webrtcbin, GParamSpec * pspec, } static gboolean -start_pipeline (void) +start_pipeline (gboolean create_offer) { GstStateChangeReturn ret; GError *error = NULL; @@ -397,7 +398,7 @@ start_pipeline (void) /* 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); + G_CALLBACK (on_negotiation_needed), GINT_TO_POINTER (create_offer)); /* 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() */ @@ -601,7 +602,17 @@ on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, app_state = PEER_CONNECTED; /* Start negotiation (exchange SDP and ICE candidates) */ - if (!start_pipeline ()) + if (!start_pipeline (TRUE)) + cleanup_and_quit_loop ("ERROR: failed to start pipeline", + PEER_CALL_ERROR); + } else if (g_strcmp0 (text, "OFFER_REQUEST") == 0) { + if (app_state != SERVER_REGISTERED) { + gst_printerr ("Received OFFER_REQUEST at a strange time, ignoring\n"); + goto out; + } + gst_print ("Received OFFER_REQUEST, sending offer\n"); + /* Peer wants us to start negotiation (exchange SDP and ICE candidates) */ + if (!start_pipeline (TRUE)) cleanup_and_quit_loop ("ERROR: failed to start pipeline", PEER_CALL_ERROR); } else if (g_str_has_prefix (text, "ERROR")) { @@ -645,7 +656,7 @@ on_server_message (SoupWebsocketConnection * conn, SoupWebsocketDataType type, /* If peer connection wasn't made yet and we are expecting peer will * connect to us, launch pipeline at this moment */ if (!webrtc1 && our_id) { - if (!start_pipeline ()) { + if (!start_pipeline (FALSE)) { cleanup_and_quit_loop ("ERROR: failed to start pipeline", PEER_CALL_ERROR); } diff --git a/webrtc/sendrecv/js/index.html b/webrtc/sendrecv/js/index.html index 772b11996d..2c5f65ee7c 100644 --- a/webrtc/sendrecv/js/index.html +++ b/webrtc/sendrecv/js/index.html @@ -31,6 +31,9 @@ + + Remote offerer
Our id is unknown
diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js index 433462fb0c..02d1ea046e 100644 --- a/webrtc/sendrecv/js/webrtc.js +++ b/webrtc/sendrecv/js/webrtc.js @@ -29,6 +29,10 @@ function setConnectButtonState(value) { document.getElementById("peer-connect-button").value = value; } +function wantRemoteOfferer() { + return document.getElementById("remote-offerer").checked; +} + function onConnectClicked() { if (document.getElementById("peer-connect-button").value == "Disconnect") { resetState(); @@ -137,6 +141,11 @@ function onServerMessage(event) { return; case "SESSION_OK": setStatus("Starting negotiation"); + if (wantRemoteOfferer()) { + ws_conn.send("OFFER_REQUEST"); + setStatus("Sent OFFER_REQUEST, waiting for offer"); + return; + } if (!peer_connection) createCall(null).then (generateOffer); return;