summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNirbheek Chauhan <nirbheek@centricular.com>2021-02-09 15:28:57 +0530
committerNirbheek Chauhan <nirbheek@centricular.com>2021-02-10 16:23:40 +0530
commitf8cbae9d6eb85381c4428998880dfcd863e205d3 (patch)
tree6525da564e0f5104b6719cecc0d560cef316ccca
parent28aa23dc20dd7e3dab8d193cbe554ab8eb41f9ae (diff)
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: <https://gitlab.freedesktop.org/gstreamer/gst-examples/-/merge_requests/31>
-rw-r--r--webrtc/sendrecv/gst/webrtc-sendrecv.c23
-rw-r--r--webrtc/sendrecv/js/index.html3
-rw-r--r--webrtc/sendrecv/js/webrtc.js9
3 files changed, 29 insertions, 6 deletions
diff --git a/webrtc/sendrecv/gst/webrtc-sendrecv.c b/webrtc/sendrecv/gst/webrtc-sendrecv.c
index e16a8af..709ac70 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 772b119..2c5f65e 100644
--- a/webrtc/sendrecv/js/index.html
+++ b/webrtc/sendrecv/js/index.html
@@ -31,6 +31,9 @@
<label for="peer-connect">Enter peer id</label>
<input id="peer-connect" type="text" name="text">
<input id="peer-connect-button" onclick="onConnectClicked();" type="button" value="Connect">
+ <!-- Request the peer to send the offer by sending the OFFER_REQUEST message.
+ Same as the -​-remote-offerer flag in the sendrecv C example -->
+ <input id="remote-offerer" type="checkbox" autocomplete="off"><span>Remote offerer</span>
</div>
<div>Our id is <b id="peer-id">unknown</b></div>
diff --git a/webrtc/sendrecv/js/webrtc.js b/webrtc/sendrecv/js/webrtc.js
index 433462f..02d1ea0 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;