webrtc: Merge ICE candidates to local descriptions
authorJan Schmidt <jan@centricular.com>
Fri, 14 Feb 2020 01:52:30 +0000 (12:52 +1100)
committerJan Schmidt <thaytan@noraisin.net>
Mon, 17 Feb 2020 14:23:56 +0000 (14:23 +0000)
When emitting ICE candidates, also merge them to the local and
pending description so they show up in the SDP if those are
retrieved from the current-local-description and
pending-local-description properties.

https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/issues/676

ext/webrtc/gstwebrtcbin.c

index 9c2cb40..e12617b 100644 (file)
@@ -3567,6 +3567,31 @@ _add_ice_candidates_from_sdp (GstWebRTCBin * webrtc, gint mlineindex,
   }
 }
 
+static void
+_add_ice_candidate_to_sdp (GstWebRTCBin * webrtc,
+    GstSDPMessage * sdp, gint mline_index, const gchar * candidate)
+{
+  GstSDPMedia *media = NULL;
+
+  if (mline_index < sdp->medias->len) {
+    media = &g_array_index (sdp->medias, GstSDPMedia, mline_index);
+  }
+
+  if (media == NULL) {
+    GST_WARNING_OBJECT (webrtc, "Couldn't find mline %d to merge ICE candidate",
+        mline_index);
+    return;
+  }
+  // Add the candidate as an attribute, first stripping off the existing
+  // candidate: key from the string description
+  if (strlen (candidate) < 10) {
+    GST_WARNING_OBJECT (webrtc,
+        "Dropping invalid ICE candidate for mline %d: %s", mline_index,
+        candidate);
+    return;
+  }
+  gst_sdp_media_add_attribute (media, "candidate", candidate + 10);
+}
 
 static gboolean
 _filter_sdp_fields (GQuark field_id, const GValue * value,
@@ -4581,6 +4606,20 @@ _on_ice_candidate_task (GstWebRTCBin * webrtc, IceCandidateItem * item)
   GST_TRACE_OBJECT (webrtc, "produced ICE candidate for mline:%u and %s",
       item->mlineindex, cand);
 
+  /* First, merge this ice candidate into the appropriate mline
+   * in the local-description SDP.
+   * Second, emit the on-ice-candidate signal for the app.
+   *
+   * FIXME: This ICE candidate should be stored somewhere with
+   * the associated mid and also merged back into any subsequent
+   * local descriptions on renegotiation */
+  if (webrtc->current_local_description)
+    _add_ice_candidate_to_sdp (webrtc, webrtc->current_local_description->sdp,
+        item->mlineindex, cand);
+  if (webrtc->pending_local_description)
+    _add_ice_candidate_to_sdp (webrtc, webrtc->pending_local_description->sdp,
+        item->mlineindex, cand);
+
   PC_UNLOCK (webrtc);
   g_signal_emit (webrtc, gst_webrtc_bin_signals[ON_ICE_CANDIDATE_SIGNAL],
       0, item->mlineindex, cand);