webrtcbin: Try to match an existing transceiver on pad request
[platform/upstream/gstreamer.git] / tests / check / elements / webrtcbin.c
index 4cc2624..376ad3a 100644 (file)
 #define OPUS_RTP_CAPS(pt) "application/x-rtp,payload=" G_STRINGIFY(pt) ",encoding-name=OPUS,media=audio,clock-rate=48000,ssrc=(uint)3384078950"
 #define VP8_RTP_CAPS(pt) "application/x-rtp,payload=" G_STRINGIFY(pt) ",encoding-name=VP8,media=video,clock-rate=90000,ssrc=(uint)3484078950"
 
+#define TEST_IS_OFFER_ELEMENT(t, e) ((t)->offerror == 1 && (e) == (t)->webrtc1 ? TRUE : FALSE)
+#define TEST_GET_OFFEROR(t) (TEST_IS_OFFER_ELEMENT(t, t->webrtc1) ? (t)->webrtc1 : t->webrtc2)
+#define TEST_GET_ANSWERER(t) (TEST_IS_OFFER_ELEMENT(t, t->webrtc1) ? (t)->webrtc2 : t->webrtc1)
+
+#define TEST_SDP_IS_LOCAL(t, e, d) ((TEST_IS_OFFER_ELEMENT (t, e) ^ ((d)->type == GST_WEBRTC_SDP_TYPE_OFFER)) == 0)
+
 typedef enum
 {
   STATE_NEW,
   STATE_NEGOTIATION_NEEDED,
   STATE_OFFER_CREATED,
+  STATE_OFFER_SET,
   STATE_ANSWER_CREATED,
+  STATE_ANSWER_SET,
   STATE_EOS,
   STATE_ERROR,
   STATE_CUSTOM,
@@ -80,24 +88,40 @@ struct test_webrtc
                                          gpointer user_data);
   gpointer ice_candidate_data;
   GDestroyNotify ice_candidate_notify;
-  GstWebRTCSessionDescription * (*on_offer_created)     (struct test_webrtc * t,
-                                                         GstElement * element,
-                                                         GstPromise * promise,
-                                                         gpointer user_data);
+  void      (*on_offer_created)         (struct test_webrtc * t,
+                                         GstElement * element,
+                                         GstPromise * promise,
+                                         gpointer user_data);
+  GstWebRTCSessionDescription *offer_desc;
+  guint offer_set_count;
   gpointer offer_data;
   GDestroyNotify offer_notify;
-  GstWebRTCSessionDescription * (*on_answer_created)    (struct test_webrtc * t,
-                                                         GstElement * element,
-                                                         GstPromise * promise,
-                                                         gpointer user_data);
-  gpointer data_channel_data;
-  GDestroyNotify data_channel_notify;
-  void       (*on_data_channel)                         (struct test_webrtc * t,
-                                                         GstElement * element,
-                                                         GObject *data_channel,
-                                                         gpointer user_data);
+  void      (*on_offer_set)             (struct test_webrtc * t,
+                                         GstElement * element,
+                                         GstPromise * promise,
+                                         gpointer user_data);
+  gpointer offer_set_data;
+  GDestroyNotify offer_set_notify;
+  void      (*on_answer_created)        (struct test_webrtc * t,
+                                         GstElement * element,
+                                         GstPromise * promise,
+                                         gpointer user_data);
+  GstWebRTCSessionDescription *answer_desc;
+  guint answer_set_count;
   gpointer answer_data;
   GDestroyNotify answer_notify;
+  void      (*on_answer_set)            (struct test_webrtc * t,
+                                         GstElement * element,
+                                         GstPromise * promise,
+                                         gpointer user_data);
+  gpointer answer_set_data;
+  GDestroyNotify answer_set_notify;
+  void      (*on_data_channel)          (struct test_webrtc * t,
+                                         GstElement * element,
+                                         GObject *data_channel,
+                                         gpointer user_data);
+  gpointer data_channel_data;
+  GDestroyNotify data_channel_notify;
   void      (*on_pad_added)             (struct test_webrtc * t,
                                          GstElement * element,
                                          GstPad * pad,
@@ -114,74 +138,167 @@ struct test_webrtc
 };
 
 static void
+test_webrtc_signal_state_unlocked (struct test_webrtc *t, TestState state)
+{
+  t->state = state;
+  g_cond_broadcast (&t->cond);
+}
+
+static void
+test_webrtc_signal_state (struct test_webrtc *t, TestState state)
+{
+  g_mutex_lock (&t->lock);
+  test_webrtc_signal_state_unlocked (t, state);
+  g_mutex_unlock (&t->lock);
+}
+
+static void
+_on_answer_set (GstPromise * promise, gpointer user_data)
+{
+  struct test_webrtc *t = user_data;
+  GstElement *answerer = TEST_GET_ANSWERER (t);
+
+  g_mutex_lock (&t->lock);
+  if (++t->answer_set_count >= 2 && t->on_answer_set) {
+    t->on_answer_set (t, answerer, promise, t->answer_set_data);
+  }
+  if (t->state == STATE_ANSWER_CREATED)
+    t->state = STATE_ANSWER_SET;
+  g_cond_broadcast (&t->cond);
+  gst_promise_unref (promise);
+  g_mutex_unlock (&t->lock);
+}
+
+static void
 _on_answer_received (GstPromise * promise, gpointer user_data)
 {
   struct test_webrtc *t = user_data;
-  GstElement *offeror = t->offerror == 1 ? t->webrtc1 : t->webrtc2;
-  GstElement *answerer = t->offerror == 2 ? t->webrtc1 : t->webrtc2;
+  GstElement *offeror = TEST_GET_OFFEROR (t);
+  GstElement *answerer = TEST_GET_ANSWERER (t);
   const GstStructure *reply;
   GstWebRTCSessionDescription *answer = NULL;
-  gchar *desc;
+  GError *error = NULL;
 
   reply = gst_promise_get_reply (promise);
-  gst_structure_get (reply, "answer",
-      GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
-  desc = gst_sdp_message_as_text (answer->sdp);
-  GST_INFO ("Created Answer: %s", desc);
-  g_free (desc);
+  if (gst_structure_get (reply, "answer",
+          GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL)) {
+    gchar *desc = gst_sdp_message_as_text (answer->sdp);
+    GST_INFO ("Created Answer: %s", desc);
+    g_free (desc);
+  } else if (gst_structure_get (reply, "error", G_TYPE_ERROR, &error, NULL)) {
+    GST_INFO ("Creating answer resulted in error: %s", error->message);
+  } else {
+    g_assert_not_reached ();
+  }
 
   g_mutex_lock (&t->lock);
+
+  g_assert (t->answer_desc == NULL);
+  t->answer_desc = answer;
+
   if (t->on_answer_created) {
-    gst_webrtc_session_description_free (answer);
-    answer = t->on_answer_created (t, answerer, promise, t->answer_data);
+    t->on_answer_created (t, answerer, promise, t->answer_data);
   }
   gst_promise_unref (promise);
 
-  g_signal_emit_by_name (answerer, "set-local-description", answer, NULL);
-  g_signal_emit_by_name (offeror, "set-remote-description", answer, NULL);
+  if (error)
+    goto error;
 
-  t->state = STATE_ANSWER_CREATED;
-  g_cond_broadcast (&t->cond);
+  if (t->answer_desc) {
+    promise = gst_promise_new_with_change_func (_on_answer_set, t, NULL);
+    g_signal_emit_by_name (answerer, "set-local-description", t->answer_desc,
+        promise);
+    promise = gst_promise_new_with_change_func (_on_answer_set, t, NULL);
+    g_signal_emit_by_name (offeror, "set-remote-description", t->answer_desc,
+        promise);
+  }
+
+  test_webrtc_signal_state_unlocked (t, STATE_ANSWER_CREATED);
+  g_mutex_unlock (&t->lock);
+  return;
+
+error:
+  g_clear_error (&error);
+  if (t->state < STATE_ERROR)
+    test_webrtc_signal_state_unlocked (t, STATE_ERROR);
   g_mutex_unlock (&t->lock);
+  return;
+}
+
+static void
+_on_offer_set (GstPromise * promise, gpointer user_data)
+{
+  struct test_webrtc *t = user_data;
+  GstElement *offeror = TEST_GET_OFFEROR (t);
 
-  gst_webrtc_session_description_free (answer);
+  g_mutex_lock (&t->lock);
+  if (++t->offer_set_count >= 2 && t->on_offer_set) {
+    t->on_offer_set (t, offeror, promise, t->offer_set_data);
+  }
+  if (t->state == STATE_OFFER_CREATED)
+    t->state = STATE_OFFER_SET;
+  g_cond_broadcast (&t->cond);
+  gst_promise_unref (promise);
+  g_mutex_unlock (&t->lock);
 }
 
 static void
 _on_offer_received (GstPromise * promise, gpointer user_data)
 {
   struct test_webrtc *t = user_data;
-  GstElement *offeror = t->offerror == 1 ? t->webrtc1 : t->webrtc2;
-  GstElement *answerer = t->offerror == 2 ? t->webrtc1 : t->webrtc2;
+  GstElement *offeror = TEST_GET_OFFEROR (t);
+  GstElement *answerer = TEST_GET_ANSWERER (t);
   const GstStructure *reply;
   GstWebRTCSessionDescription *offer = NULL;
-  gchar *desc;
+  GError *error = NULL;
 
   reply = gst_promise_get_reply (promise);
-  gst_structure_get (reply, "offer",
-      GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
-  desc = gst_sdp_message_as_text (offer->sdp);
-  GST_INFO ("Created offer: %s", desc);
-  g_free (desc);
+  if (gst_structure_get (reply, "offer",
+          GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL)) {
+    gchar *desc = gst_sdp_message_as_text (offer->sdp);
+    GST_INFO ("Created offer: %s", desc);
+    g_free (desc);
+  } else if (gst_structure_get (reply, "error", G_TYPE_ERROR, &error, NULL)) {
+    GST_INFO ("Creating offer resulted in error: %s", error->message);
+  } else {
+    g_assert_not_reached ();
+  }
 
   g_mutex_lock (&t->lock);
+
+  g_assert (t->offer_desc == NULL);
+  t->offer_desc = offer;
+
   if (t->on_offer_created) {
-    gst_webrtc_session_description_free (offer);
-    offer = t->on_offer_created (t, offeror, promise, t->offer_data);
+    t->on_offer_created (t, offeror, promise, t->offer_data);
   }
   gst_promise_unref (promise);
 
-  g_signal_emit_by_name (offeror, "set-local-description", offer, NULL);
-  g_signal_emit_by_name (answerer, "set-remote-description", offer, NULL);
+  if (error)
+    goto error;
 
-  promise = gst_promise_new_with_change_func (_on_answer_received, t, NULL);
-  g_signal_emit_by_name (answerer, "create-answer", NULL, promise);
+  if (t->offer_desc) {
+    promise = gst_promise_new_with_change_func (_on_offer_set, t, NULL);
+    g_signal_emit_by_name (offeror, "set-local-description", t->offer_desc,
+        promise);
+    promise = gst_promise_new_with_change_func (_on_offer_set, t, NULL);
+    g_signal_emit_by_name (answerer, "set-remote-description", t->offer_desc,
+        promise);
 
-  t->state = STATE_OFFER_CREATED;
-  g_cond_broadcast (&t->cond);
+    promise = gst_promise_new_with_change_func (_on_answer_received, t, NULL);
+    g_signal_emit_by_name (answerer, "create-answer", NULL, promise);
+  }
+
+  test_webrtc_signal_state_unlocked (t, STATE_OFFER_CREATED);
   g_mutex_unlock (&t->lock);
+  return;
 
-  gst_webrtc_session_description_free (offer);
+error:
+  g_clear_error (&error);
+  if (t->state < STATE_ERROR)
+    test_webrtc_signal_state_unlocked (t, STATE_ERROR);
+  g_mutex_unlock (&t->lock);
+  return;
 }
 
 static gboolean
@@ -225,13 +342,12 @@ _bus_watch (GstBus * bus, GstMessage * msg, struct test_webrtc *t)
       }
 
       gst_message_parse_error (msg, &err, &dbg_info);
-      GST_WARNING ("ERROR from element %s: %s\n",
+      GST_WARNING ("ERROR from element %s: %s",
           GST_OBJECT_NAME (msg->src), err->message);
-      GST_WARNING ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
+      GST_WARNING ("Debugging info: %s", (dbg_info) ? dbg_info : "none");
       g_error_free (err);
       g_free (dbg_info);
-      t->state = STATE_ERROR;
-      g_cond_broadcast (&t->cond);
+      test_webrtc_signal_state_unlocked (t, STATE_ERROR);
       break;
     }
     case GST_MESSAGE_EOS:{
@@ -246,9 +362,8 @@ _bus_watch (GstBus * bus, GstMessage * msg, struct test_webrtc *t)
             GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
         g_free (dump_name);
       }
-      GST_INFO ("EOS received\n");
-      t->state = STATE_EOS;
-      g_cond_broadcast (&t->cond);
+      GST_INFO ("EOS received");
+      test_webrtc_signal_state_unlocked (t, STATE_EOS);
       break;
     }
     default:
@@ -345,7 +460,7 @@ _bus_no_errors (struct test_webrtc *t, GstBus * bus, GstMessage * msg,
   }
 }
 
-static GstWebRTCSessionDescription *
+static void
 _offer_answer_not_reached (struct test_webrtc *t, GstElement * element,
     GstPromise * promise, gpointer user_data)
 {
@@ -420,6 +535,13 @@ test_webrtc_new (void)
   g_mutex_init (&ret->lock);
   g_cond_init (&ret->cond);
 
+  ret->thread = g_thread_new ("test-webrtc", (GThreadFunc) _bus_thread, ret);
+
+  g_mutex_lock (&ret->lock);
+  while (!ret->loop)
+    g_cond_wait (&ret->cond, &ret->lock);
+  g_mutex_unlock (&ret->lock);
+
   ret->bus1 = gst_bus_new ();
   ret->bus2 = gst_bus_new ();
   gst_bus_add_watch (ret->bus1, (GstBusFunc) _bus_watch, ret);
@@ -458,14 +580,22 @@ test_webrtc_new (void)
   g_signal_connect_swapped (ret->webrtc2, "notify::ice-connection-state",
       G_CALLBACK (_broadcast), ret);
 
-  ret->thread = g_thread_new ("test-webrtc", (GThreadFunc) _bus_thread, ret);
+  return ret;
+}
 
-  g_mutex_lock (&ret->lock);
-  while (!ret->loop)
-    g_cond_wait (&ret->cond, &ret->lock);
-  g_mutex_unlock (&ret->lock);
+static void
+test_webrtc_reset_negotiation (struct test_webrtc *t)
+{
+  if (t->offer_desc)
+    gst_webrtc_session_description_free (t->offer_desc);
+  t->offer_desc = NULL;
+  t->offer_set_count = 0;
+  if (t->answer_desc)
+    gst_webrtc_session_description_free (t->answer_desc);
+  t->answer_desc = NULL;
+  t->answer_set_count = 0;
 
-  return ret;
+  test_webrtc_signal_state (t, STATE_NEGOTIATION_NEEDED);
 }
 
 static void
@@ -503,8 +633,12 @@ test_webrtc_free (struct test_webrtc *t)
     t->ice_candidate_notify (t->ice_candidate_data);
   if (t->offer_notify)
     t->offer_notify (t->offer_data);
+  if (t->offer_set_notify)
+    t->offer_set_notify (t->offer_set_data);
   if (t->answer_notify)
     t->answer_notify (t->answer_data);
+  if (t->answer_set_notify)
+    t->answer_set_notify (t->answer_set_data);
   if (t->pad_added_notify)
     t->pad_added_notify (t->pad_added_data);
   if (t->data_channel_notify)
@@ -515,6 +649,8 @@ test_webrtc_free (struct test_webrtc *t)
   fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
       gst_element_set_state (t->webrtc2, GST_STATE_NULL));
 
+  test_webrtc_reset_negotiation (t);
+
   gst_object_unref (t->webrtc1);
   gst_object_unref (t->webrtc2);
 
@@ -550,29 +686,13 @@ static void
 test_webrtc_wait_for_answer_error_eos (struct test_webrtc *t)
 {
   TestState states = 0;
-  states |= (1 << STATE_ANSWER_CREATED);
+  states |= (1 << STATE_ANSWER_SET);
   states |= (1 << STATE_EOS);
   states |= (1 << STATE_ERROR);
   test_webrtc_wait_for_state_mask (t, states);
 }
 
 static void
-test_webrtc_signal_state_unlocked (struct test_webrtc *t, TestState state)
-{
-  t->state = state;
-  g_cond_broadcast (&t->cond);
-}
-
-static void
-test_webrtc_signal_state (struct test_webrtc *t, TestState state)
-{
-  g_mutex_lock (&t->lock);
-  test_webrtc_signal_state_unlocked (t, state);
-  g_mutex_unlock (&t->lock);
-}
-
-#if 0
-static void
 test_webrtc_wait_for_ice_gathering_complete (struct test_webrtc *t)
 {
   GstWebRTCICEGatheringState ice_state1, ice_state2;
@@ -588,6 +708,7 @@ test_webrtc_wait_for_ice_gathering_complete (struct test_webrtc *t)
   g_mutex_unlock (&t->lock);
 }
 
+#if 0
 static void
 test_webrtc_wait_for_ice_connection (struct test_webrtc *t,
     GstWebRTCICEConnectionState states)
@@ -621,53 +742,225 @@ _pad_added_fakesink (struct test_webrtc *t, GstElement * element,
   t->harnesses = g_list_prepend (t->harnesses, h);
 }
 
-static GstWebRTCSessionDescription *
-_count_num_sdp_media (struct test_webrtc *t, GstElement * element,
+static void
+on_negotiation_needed_hit (struct test_webrtc *t, GstElement * element,
+    gpointer user_data)
+{
+  guint *flag = (guint *) user_data;
+
+  *flag = 1;
+}
+
+typedef void (*ValidateSDPFunc) (struct test_webrtc * t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data);
+
+struct validate_sdp;
+struct validate_sdp
+{
+  ValidateSDPFunc validate;
+  gpointer user_data;
+  struct validate_sdp *next;
+};
+
+#define VAL_SDP_INIT(name,func,data,next) \
+    struct validate_sdp name = { func, data, next }
+
+static void
+_check_validate_sdp (struct test_webrtc *t, GstElement * element,
     GstPromise * promise, gpointer user_data)
 {
-  GstWebRTCSessionDescription *offer = NULL;
-  guint expected = GPOINTER_TO_UINT (user_data);
-  const GstStructure *reply;
-  const gchar *field;
+  struct validate_sdp *validate = user_data;
+  GstWebRTCSessionDescription *desc = NULL;
 
-  field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
+  if (t->offerror == 1 && t->webrtc1 == element)
+    desc = t->offer_desc;
+  else
+    desc = t->answer_desc;
 
-  reply = gst_promise_get_reply (promise);
-  gst_structure_get (reply, field,
-      GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
+  while (validate) {
+    validate->validate (t, element, desc, validate->user_data);
+    validate = validate->next;
+  }
+}
+
+static void
+test_validate_sdp_full (struct test_webrtc *t, struct validate_sdp *offer,
+    struct validate_sdp *answer, TestState wait_mask,
+    gboolean perform_state_change)
+{
+  if (offer) {
+    t->offer_data = offer;
+    t->on_offer_created = _check_validate_sdp;
+  } else {
+    t->offer_data = NULL;
+    t->on_offer_created = NULL;
+  }
+  if (answer) {
+    t->answer_data = answer;
+    t->on_answer_created = _check_validate_sdp;
+  } else {
+    t->answer_data = NULL;
+    t->on_answer_created = NULL;
+  }
+
+  if (perform_state_change) {
+    fail_if (gst_element_set_state (t->webrtc1,
+            GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
+    fail_if (gst_element_set_state (t->webrtc2,
+            GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
+  }
+
+  test_webrtc_create_offer (t, t->webrtc1);
+
+  if (wait_mask == 0) {
+    test_webrtc_wait_for_answer_error_eos (t);
+    fail_unless (t->state == STATE_ANSWER_SET);
+  } else {
+    test_webrtc_wait_for_state_mask (t, wait_mask);
+  }
+}
+
+static void
+test_validate_sdp (struct test_webrtc *t, struct validate_sdp *offer,
+    struct validate_sdp *answer)
+{
+  test_validate_sdp_full (t, offer, answer, 0, TRUE);
+}
 
-  fail_unless_equals_int (gst_sdp_message_medias_len (offer->sdp), expected);
+static void
+_count_num_sdp_media (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  guint expected = GPOINTER_TO_UINT (user_data);
 
-  return offer;
+  fail_unless_equals_int (gst_sdp_message_medias_len (desc->sdp), expected);
 }
 
 GST_START_TEST (test_sdp_no_media)
 {
   struct test_webrtc *t = test_webrtc_new ();
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (0), NULL);
 
   /* check that a no stream connection creates 0 media sections */
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = GUINT_TO_POINTER (0);
-  t->on_offer_created = _count_num_sdp_media;
-  t->answer_data = GUINT_TO_POINTER (0);
-  t->on_answer_created = _count_num_sdp_media;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
+  test_validate_sdp (t, &count, &count);
 
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless (t->state == STATE_ANSWER_CREATED);
   test_webrtc_free (t);
 }
 
 GST_END_TEST;
 
 static void
+on_sdp_media_direction (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  gchar **expected_directions = user_data;
+  int i;
+
+  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
+    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
+
+    if (g_strcmp0 (gst_sdp_media_get_media (media), "audio") == 0
+        || g_strcmp0 (gst_sdp_media_get_media (media), "video") == 0) {
+      gboolean have_direction = FALSE;
+      int j;
+
+      for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
+        const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
+
+        if (g_strcmp0 (attr->key, "inactive") == 0) {
+          fail_unless (have_direction == FALSE,
+              "duplicate/multiple directions for media %u", j);
+          have_direction = TRUE;
+          fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
+        } else if (g_strcmp0 (attr->key, "sendonly") == 0) {
+          fail_unless (have_direction == FALSE,
+              "duplicate/multiple directions for media %u", j);
+          have_direction = TRUE;
+          fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
+        } else if (g_strcmp0 (attr->key, "recvonly") == 0) {
+          fail_unless (have_direction == FALSE,
+              "duplicate/multiple directions for media %u", j);
+          have_direction = TRUE;
+          fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
+        } else if (g_strcmp0 (attr->key, "sendrecv") == 0) {
+          fail_unless (have_direction == FALSE,
+              "duplicate/multiple directions for media %u", j);
+          have_direction = TRUE;
+          fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
+        }
+      }
+      fail_unless (have_direction, "no direction attribute in media %u", i);
+    }
+  }
+}
+
+static void
+on_sdp_media_no_duplicate_payloads (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  int i, j, k;
+
+  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
+    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
+
+    GArray *media_formats = g_array_new (FALSE, FALSE, sizeof (int));
+    for (j = 0; j < gst_sdp_media_formats_len (media); j++) {
+      int pt = atoi (gst_sdp_media_get_format (media, j));
+      for (k = 0; k < media_formats->len; k++) {
+        int val = g_array_index (media_formats, int, k);
+        if (pt == val)
+          fail ("found an unexpected duplicate payload type %u within media %u",
+              pt, i);
+      }
+      g_array_append_val (media_formats, pt);
+    }
+    g_array_free (media_formats, TRUE);
+  }
+}
+
+static void
+on_sdp_media_count_formats (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  guint *expected_n_media_formats = user_data;
+  int i;
+
+  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
+    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
+    fail_unless_equals_int (gst_sdp_media_formats_len (media),
+        expected_n_media_formats[i]);
+  }
+}
+
+static void
+on_sdp_media_setup (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  gchar **expected_setup = user_data;
+  int i;
+
+  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
+    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
+    gboolean have_setup = FALSE;
+    int j;
+
+    for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
+      const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
+
+      if (g_strcmp0 (attr->key, "setup") == 0) {
+        fail_unless (have_setup == FALSE,
+            "duplicate/multiple setup for media %u", j);
+        have_setup = TRUE;
+        fail_unless (g_strcmp0 (attr->value, expected_setup[i]) == 0);
+      }
+    }
+    fail_unless (have_setup, "no setup attribute in media %u", i);
+  }
+}
+
+static void
 add_fake_audio_src_harness (GstHarness * h, gint pt)
 {
   GstCaps *caps = gst_caps_from_string (OPUS_RTP_CAPS (pt));
@@ -694,6 +987,7 @@ create_audio_test (void)
   GstHarness *h;
 
   t->on_negotiation_needed = NULL;
+  t->on_ice_candidate = NULL;
   t->on_pad_added = _pad_added_fakesink;
 
   h = gst_harness_new_with_element (t->webrtc1, "sink_0", NULL);
@@ -706,162 +1000,172 @@ create_audio_test (void)
 GST_START_TEST (test_audio)
 {
   struct test_webrtc *t = create_audio_test ();
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (1),
+      &media_formats);
+  const gchar *expected_offer_setup[] = { "actpass", };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_answer_setup[] = { "active", };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &count);
+  const gchar *expected_offer_direction[] = { "sendrecv", };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
 
   /* check that a single stream connection creates the associated number
    * of media sections */
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = GUINT_TO_POINTER (1);
-  t->on_offer_created = _count_num_sdp_media;
-  t->answer_data = GUINT_TO_POINTER (1);
-  t->on_answer_created = _count_num_sdp_media;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
   test_webrtc_free (t);
 }
 
 GST_END_TEST;
 
-static struct test_webrtc *
-create_audio_video_test (void)
+static void
+_check_ice_port_restriction (struct test_webrtc *t, GstElement * element,
+    guint mlineindex, gchar * candidate, GstElement * other, gpointer user_data)
 {
-  struct test_webrtc *t = test_webrtc_new ();
-  GstHarness *h;
+  GRegex *regex;
+  GMatchInfo *match_info;
 
-  t->on_negotiation_needed = NULL;
-  t->on_pad_added = _pad_added_fakesink;
+  gchar *candidate_port;
+  gchar *candidate_protocol;
+  gchar *candidate_typ;
+  guint port_as_int;
+  guint peer_number;
 
-  h = gst_harness_new_with_element (t->webrtc1, "sink_0", NULL);
-  add_fake_audio_src_harness (h, 96);
-  t->harnesses = g_list_prepend (t->harnesses, h);
+  regex =
+      g_regex_new ("candidate:(\\d+) (1) (UDP|TCP) (\\d+) ([0-9.]+|[0-9a-f:]+)"
+      " (\\d+) typ ([a-z]+)", 0, 0, NULL);
 
-  h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
-  add_fake_video_src_harness (h, 97);
-  t->harnesses = g_list_prepend (t->harnesses, h);
+  g_regex_match (regex, candidate, 0, &match_info);
+  fail_unless (g_match_info_get_match_count (match_info) == 8, candidate);
 
-  return t;
+  candidate_protocol = g_match_info_fetch (match_info, 2);
+  candidate_port = g_match_info_fetch (match_info, 6);
+  candidate_typ = g_match_info_fetch (match_info, 7);
+
+  peer_number = t->webrtc1 == element ? 1 : 2;
+
+  port_as_int = atoi (candidate_port);
+
+  if (!g_strcmp0 (candidate_typ, "host") && port_as_int != 9) {
+    guint expected_min = peer_number * 10000 + 1000;
+    guint expected_max = expected_min + 999;
+
+    fail_unless (port_as_int >= expected_min);
+    fail_unless (port_as_int <= expected_max);
+  }
+
+  g_free (candidate_port);
+  g_free (candidate_protocol);
+  g_free (candidate_typ);
+  g_match_info_free (match_info);
+  g_regex_unref (regex);
 }
 
-GST_START_TEST (test_audio_video)
+GST_START_TEST (test_ice_port_restriction)
 {
-  struct test_webrtc *t = create_audio_video_test ();
+  struct test_webrtc *t = create_audio_test ();
+  GObject *webrtcice;
 
-  /* check that a dual stream connection creates the associated number
-   * of media sections */
+  VAL_SDP_INIT (offer, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (answer, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = GUINT_TO_POINTER (2);
-  t->on_offer_created = _count_num_sdp_media;
-  t->answer_data = GUINT_TO_POINTER (2);
-  t->on_answer_created = _count_num_sdp_media;
-  t->on_ice_candidate = NULL;
+  /*
+   *  Ports are defined as follows "{peer}{protocol}000"
+   *  - peer number: "1" for t->webrtc1, "2" for t->webrtc2
+   */
+  g_object_get (t->webrtc1, "ice-agent", &webrtcice, NULL);
+  g_object_set (webrtcice, "min-rtp-port", 11000, "max-rtp-port", 11999, NULL);
+  g_object_unref (webrtcice);
 
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
+  g_object_get (t->webrtc2, "ice-agent", &webrtcice, NULL);
+  g_object_set (webrtcice, "min-rtp-port", 21000, "max-rtp-port", 21999, NULL);
+  g_object_unref (webrtcice);
 
-  test_webrtc_create_offer (t, t->webrtc1);
+  t->on_ice_candidate = _check_ice_port_restriction;
+  test_validate_sdp (t, &offer, &answer);
 
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_webrtc_wait_for_ice_gathering_complete (t);
   test_webrtc_free (t);
 }
 
 GST_END_TEST;
 
-typedef void (*ValidateSDPFunc) (struct test_webrtc * t, GstElement * element,
-    GstWebRTCSessionDescription * desc, gpointer user_data);
-
-struct validate_sdp;
-struct validate_sdp
-{
-  ValidateSDPFunc validate;
-  gpointer user_data;
-  struct validate_sdp *next;
-};
-
-static GstWebRTCSessionDescription *
-_check_validate_sdp (struct test_webrtc *t, GstElement * element,
-    GstPromise * promise, gpointer user_data)
+static struct test_webrtc *
+create_audio_video_test (void)
 {
-  struct validate_sdp *validate = user_data;
-  GstWebRTCSessionDescription *offer = NULL;
-  const GstStructure *reply;
-  const gchar *field;
-
-  field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
-
-  reply = gst_promise_get_reply (promise);
-  gst_structure_get (reply, field,
-      GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
+  struct test_webrtc *t = create_audio_test ();
+  GstHarness *h;
 
-  while (validate) {
-    validate->validate (t, element, offer, validate->user_data);
-    validate = validate->next;
-  }
+  h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
+  add_fake_video_src_harness (h, 97);
+  t->harnesses = g_list_prepend (t->harnesses, h);
 
-  return offer;
+  return t;
 }
 
-static void
-on_sdp_media_direction (struct test_webrtc *t, GstElement * element,
-    GstWebRTCSessionDescription * desc, gpointer user_data)
+GST_START_TEST (test_audio_video)
 {
-  gchar **expected_directions = user_data;
-  int i;
-
-  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
-    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
-    gboolean have_direction = FALSE;
-    int j;
+  struct test_webrtc *t = create_audio_video_test ();
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &count);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
 
-    for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
-      const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
+  /* check that a dual stream connection creates the associated number
+   * of media sections */
 
-      if (g_strcmp0 (attr->key, "inactive") == 0) {
-        fail_unless (have_direction == FALSE,
-            "duplicate/multiple directions for media %u", j);
-        have_direction = TRUE;
-        fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
-      } else if (g_strcmp0 (attr->key, "sendonly") == 0) {
-        fail_unless (have_direction == FALSE,
-            "duplicate/multiple directions for media %u", j);
-        have_direction = TRUE;
-        fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
-      } else if (g_strcmp0 (attr->key, "recvonly") == 0) {
-        fail_unless (have_direction == FALSE,
-            "duplicate/multiple directions for media %u", j);
-        have_direction = TRUE;
-        fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
-      } else if (g_strcmp0 (attr->key, "sendrecv") == 0) {
-        fail_unless (have_direction == FALSE,
-            "duplicate/multiple directions for media %u", j);
-        have_direction = TRUE;
-        fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
-      }
-    }
-    fail_unless (have_direction, "no direction attribute in media %u", j);
-  }
+  test_validate_sdp (t, &offer, &answer);
+  test_webrtc_free (t);
 }
 
+GST_END_TEST;
+
 GST_START_TEST (test_media_direction)
 {
   struct test_webrtc *t = create_audio_video_test ();
-  const gchar *expected_offer[] = { "sendrecv", "sendrecv" };
-  const gchar *expected_answer[] = { "sendrecv", "recvonly" };
-  struct validate_sdp offer = { on_sdp_media_direction, expected_offer, NULL };
-  struct validate_sdp answer =
-      { on_sdp_media_direction, expected_answer, NULL };
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &count);
+
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendrecv", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
   GstHarness *h;
 
   /* check the default media directions for transceivers */
@@ -870,22 +1174,7 @@ GST_START_TEST (test_media_direction)
   add_fake_audio_src_harness (h, 96);
   t->harnesses = g_list_prepend (t->harnesses, h);
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
   test_webrtc_free (t);
 }
 
@@ -898,8 +1187,6 @@ on_sdp_media_payload_types (struct test_webrtc *t, GstElement * element,
   const GstSDPMedia *vmedia;
   guint j;
 
-  fail_unless_equals_int (gst_sdp_message_medias_len (desc->sdp), 2);
-
   vmedia = gst_sdp_message_get_media (desc->sdp, 1);
 
   for (j = 0; j < gst_sdp_media_attributes_len (vmedia); j++) {
@@ -926,17 +1213,21 @@ on_sdp_media_payload_types (struct test_webrtc *t, GstElement * element,
 GST_START_TEST (test_payload_types)
 {
   struct test_webrtc *t = create_audio_video_test ();
-  struct validate_sdp offer = { on_sdp_media_payload_types, NULL, NULL };
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1, 5, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (payloads, on_sdp_media_payload_types, NULL, &media_formats);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2), &payloads);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
   GstWebRTCRTPTransceiver *trans;
   GArray *transceivers;
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-  /* We don't really care about the answer here */
-  t->on_answer_created = NULL;
-
   g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
   fail_unless_equals_int (transceivers->len, 2);
   trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1);
@@ -944,72 +1235,8 @@ GST_START_TEST (test_payload_types)
       NULL);
   g_array_unref (transceivers);
 
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
-  test_webrtc_free (t);
-}
-
-GST_END_TEST;
-
-static void
-on_sdp_media_setup (struct test_webrtc *t, GstElement * element,
-    GstWebRTCSessionDescription * desc, gpointer user_data)
-{
-  gchar **expected_setup = user_data;
-  int i;
-
-  for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
-    const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
-    gboolean have_setup = FALSE;
-    int j;
-
-    for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
-      const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
-
-      if (g_strcmp0 (attr->key, "setup") == 0) {
-        fail_unless (have_setup == FALSE,
-            "duplicate/multiple setup for media %u", j);
-        have_setup = TRUE;
-        fail_unless (g_strcmp0 (attr->value, expected_setup[i]) == 0);
-      }
-    }
-    fail_unless (have_setup, "no setup attribute in media %u", j);
-  }
-}
-
-GST_START_TEST (test_media_setup)
-{
-  struct test_webrtc *t = create_audio_test ();
-  const gchar *expected_offer[] = { "actpass" };
-  const gchar *expected_answer[] = { "active" };
-  struct validate_sdp offer = { on_sdp_media_setup, expected_offer, NULL };
-  struct validate_sdp answer = { on_sdp_media_setup, expected_answer, NULL };
-
-  /* check the default dtls setup negotiation values */
-
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  /* We don't really care about the answer here */
+  test_validate_sdp (t, &offer, NULL);
   test_webrtc_free (t);
 }
 
@@ -1321,20 +1548,8 @@ GST_START_TEST (test_session_stats)
   GstPromise *p;
 
   /* test that the stats generated without any streams are sane */
-
   t->on_negotiation_needed = NULL;
-  t->on_offer_created = NULL;
-  t->on_answer_created = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, NULL, NULL);
 
   p = gst_promise_new_with_change_func (_on_stats, t, NULL);
   g_signal_emit_by_name (t->webrtc1, "get-stats", NULL, p);
@@ -1392,25 +1607,32 @@ GST_START_TEST (test_add_recvonly_transceiver)
   struct test_webrtc *t = test_webrtc_new ();
   GstWebRTCRTPTransceiverDirection direction;
   GstWebRTCRTPTransceiver *trans;
-  const gchar *expected_offer[] = { "recvonly" };
-  const gchar *expected_answer[] = { "sendonly" };
-  struct validate_sdp offer = { on_sdp_media_direction, expected_offer, NULL };
-  struct validate_sdp answer =
-      { on_sdp_media_direction, expected_answer, NULL };
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (1),
+      &media_formats);
+  const gchar *expected_offer_setup[] = { "actpass", };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_answer_setup[] = { "active", };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &count);
+  const gchar *expected_offer_direction[] = { "recvonly", };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendonly", };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
   GstCaps *caps;
   GstHarness *h;
 
   /* add a transceiver that will only receive an opus stream and check that
    * the created offer is marked as recvonly */
-
-  t->on_negotiation_needed = NULL;
-  t->on_pad_added = _pad_added_fakesink;
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
+  t->on_pad_added = _pad_added_fakesink;
 
   /* setup recvonly transceiver */
   caps = gst_caps_from_string (OPUS_RTP_CAPS (96));
@@ -1425,16 +1647,8 @@ GST_START_TEST (test_add_recvonly_transceiver)
   h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
   add_fake_audio_src_harness (h, 96);
   t->harnesses = g_list_prepend (t->harnesses, h);
+  test_validate_sdp (t, &offer, &answer);
 
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
   test_webrtc_free (t);
 }
 
@@ -1445,26 +1659,33 @@ GST_START_TEST (test_recvonly_sendonly)
   struct test_webrtc *t = test_webrtc_new ();
   GstWebRTCRTPTransceiverDirection direction;
   GstWebRTCRTPTransceiver *trans;
-  const gchar *expected_offer[] = { "recvonly", "sendonly" };
-  const gchar *expected_answer[] = { "sendonly", "recvonly" };
-  struct validate_sdp offer = { on_sdp_media_direction, expected_offer, NULL };
-  struct validate_sdp answer =
-      { on_sdp_media_direction, expected_answer, NULL };
+  VAL_SDP_INIT (no_duplicate_payloads, on_sdp_media_no_duplicate_payloads,
+      NULL, NULL);
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, &no_duplicate_payloads);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup, &count);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &count);
+  const gchar *expected_offer_direction[] = { "recvonly", "sendonly" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
   GstCaps *caps;
   GstHarness *h;
   GArray *transceivers;
 
   /* add a transceiver that will only receive an opus stream and check that
    * the created offer is marked as recvonly */
-
-  t->on_negotiation_needed = NULL;
-  t->on_pad_added = _pad_added_fakesink;
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
+  t->on_pad_added = _pad_added_fakesink;
 
   /* setup recvonly transceiver */
   caps = gst_caps_from_string (OPUS_RTP_CAPS (96));
@@ -1481,6 +1702,7 @@ GST_START_TEST (test_recvonly_sendonly)
   t->harnesses = g_list_prepend (t->harnesses, h);
   g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
   fail_unless (transceivers != NULL);
+  fail_unless_equals_int (transceivers->len, 2);
   trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1);
   trans->direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
 
@@ -1491,15 +1713,8 @@ GST_START_TEST (test_recvonly_sendonly)
   add_fake_audio_src_harness (h, 96);
   t->harnesses = g_list_prepend (t->harnesses, h);
 
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
+  test_validate_sdp (t, &offer, &answer);
 
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
   test_webrtc_free (t);
 }
 
@@ -1534,15 +1749,11 @@ GST_START_TEST (test_data_channel_create)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
   gchar *label;
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
 
   fail_if (gst_element_set_state (t->webrtc1,
@@ -1558,10 +1769,8 @@ GST_START_TEST (test_data_channel_create)
   g_signal_connect (channel, "on-error",
       G_CALLBACK (on_channel_error_not_reached), NULL);
 
-  test_webrtc_create_offer (t, t->webrtc1);
+  test_validate_sdp (t, &offer, &offer);
 
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
   g_object_unref (channel);
   g_free (label);
   test_webrtc_free (t);
@@ -1594,14 +1803,10 @@ GST_START_TEST (test_data_channel_remote_notify)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel;
 
@@ -1622,9 +1827,7 @@ GST_START_TEST (test_data_channel_remote_notify)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1637,7 +1840,13 @@ static const gchar *test_string = "GStreamer WebRTC is awesome!";
 static void
 on_message_string (GObject * channel, const gchar * str, struct test_webrtc *t)
 {
-  gchar *expected = g_object_steal_data (channel, "expected");
+  GstWebRTCDataChannelState state;
+  gchar *expected;
+
+  g_object_get (channel, "ready-state", &state, NULL);
+  fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
+
+  expected = g_object_steal_data (channel, "expected");
   g_assert_cmpstr (expected, ==, str);
   g_free (expected);
 
@@ -1653,8 +1862,6 @@ have_data_channel_transfer_string (struct test_webrtc *t, GstElement * element,
 
   g_object_get (our, "ready-state", &state, NULL);
   fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
-  g_object_get (other, "ready-state", &state, NULL);
-  fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
 
   g_object_set_data_full (our, "expected", g_strdup (test_string), g_free);
   g_signal_connect (our, "on-message-string", G_CALLBACK (on_message_string),
@@ -1669,14 +1876,10 @@ GST_START_TEST (test_data_channel_transfer_string)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel_transfer_string;
 
@@ -1697,9 +1900,7 @@ GST_START_TEST (test_data_channel_transfer_string)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1718,7 +1919,13 @@ GST_END_TEST;
 static void
 on_message_data (GObject * channel, GBytes * data, struct test_webrtc *t)
 {
-  GBytes *expected = g_object_steal_data (channel, "expected");
+  GstWebRTCDataChannelState state;
+  GBytes *expected;
+
+  g_object_get (channel, "ready-state", &state, NULL);
+  fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
+
+  expected = g_object_steal_data (channel, "expected");
   g_assert_cmpbytes (data, expected);
   g_bytes_unref (expected);
 
@@ -1735,8 +1942,6 @@ have_data_channel_transfer_data (struct test_webrtc *t, GstElement * element,
 
   g_object_get (our, "ready-state", &state, NULL);
   fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
-  g_object_get (other, "ready-state", &state, NULL);
-  fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
 
   g_object_set_data_full (our, "expected", g_bytes_ref (data),
       (GDestroyNotify) g_bytes_unref);
@@ -1745,20 +1950,17 @@ have_data_channel_transfer_data (struct test_webrtc *t, GstElement * element,
   g_signal_connect (other, "on-error",
       G_CALLBACK (on_channel_error_not_reached), NULL);
   g_signal_emit_by_name (other, "send-data", data);
+  g_bytes_unref (data);
 }
 
 GST_START_TEST (test_data_channel_transfer_data)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel_transfer_data;
 
@@ -1779,9 +1981,7 @@ GST_START_TEST (test_data_channel_transfer_data)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1801,6 +2001,7 @@ have_data_channel_create_data_channel (struct test_webrtc *t,
       &another);
   g_assert_nonnull (another);
   t->data_channel_data = another;
+  t->data_channel_notify = (GDestroyNotify) g_object_unref;
   g_signal_connect (another, "on-error",
       G_CALLBACK (on_channel_error_not_reached), NULL);
 }
@@ -1809,14 +2010,10 @@ GST_START_TEST (test_data_channel_create_after_negotiate)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel_create_data_channel;
 
@@ -1837,9 +2034,7 @@ GST_START_TEST (test_data_channel_create_after_negotiate)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1863,21 +2058,17 @@ have_data_channel_check_low_threshold_emitted (struct test_webrtc *t,
 
   g_signal_connect (our, "on-error", G_CALLBACK (on_channel_error_not_reached),
       NULL);
-  g_signal_emit_by_name (our, "send-string", "DATA");
+  g_signal_emit_by_name (our, "send-string", "A");
 }
 
 GST_START_TEST (test_data_channel_low_threshold)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel_check_low_threshold_emitted;
 
@@ -1898,9 +2089,7 @@ GST_START_TEST (test_data_channel_low_threshold)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1929,7 +2118,8 @@ have_data_channel_transfer_large_data (struct test_webrtc *t,
   for (i = 0; i < size; i++)
     random_data[i] = (guint8) (i & 0xff);
 
-  data = g_bytes_new_static (random_data, size);
+  data = g_bytes_new_with_free_func (random_data, size,
+      (GDestroyNotify) g_free, random_data);
 
   g_object_set_data_full (our, "expected", g_bytes_ref (data),
       (GDestroyNotify) g_bytes_unref);
@@ -1937,20 +2127,17 @@ have_data_channel_transfer_large_data (struct test_webrtc *t,
 
   g_signal_connect (other, "on-error", G_CALLBACK (on_channel_error), t);
   g_signal_emit_by_name (other, "send-data", data);
+  g_bytes_unref (data);
 }
 
 GST_START_TEST (test_data_channel_max_message_size)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
   t->on_data_channel = have_data_channel_transfer_large_data;
 
@@ -1969,9 +2156,7 @@ GST_START_TEST (test_data_channel_max_message_size)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+  test_validate_sdp_full (t, &offer, &offer, 1 << STATE_CUSTOM, FALSE);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -1989,8 +2174,9 @@ _on_ready_state_notify (GObject * channel, GParamSpec * pspec,
   g_object_get (channel, "ready-state", &ready_state, NULL);
 
   if (ready_state == GST_WEBRTC_DATA_CHANNEL_STATE_OPEN) {
-    if (++(*n_ready) >= 2)
+    if (g_atomic_int_add (n_ready, 1) >= 1) {
       test_webrtc_signal_state (t, STATE_CUSTOM);
+    }
   }
 }
 
@@ -1998,16 +2184,12 @@ GST_START_TEST (test_data_channel_pre_negotiated)
 {
   struct test_webrtc *t = test_webrtc_new ();
   GObject *channel1 = NULL, *channel2 = NULL;
-  struct validate_sdp offer = { on_sdp_has_datachannel, NULL, NULL };
-  struct validate_sdp answer = { on_sdp_has_datachannel, NULL, NULL };
+  VAL_SDP_INIT (media_count, _count_num_sdp_media, GUINT_TO_POINTER (1), NULL);
+  VAL_SDP_INIT (offer, on_sdp_has_datachannel, NULL, &media_count);
   GstStructure *s;
   gint n_ready = 0;
 
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
 
   fail_if (gst_element_set_state (t->webrtc1,
@@ -2030,9 +2212,7 @@ GST_START_TEST (test_data_channel_pre_negotiated)
   fail_if (gst_element_set_state (t->webrtc2,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless (t->state == STATE_ANSWER_CREATED);
+  test_validate_sdp_full (t, &offer, &offer, 0, FALSE);
 
   t->data_channel_data = &n_ready;
 
@@ -2056,56 +2236,63 @@ GST_START_TEST (test_data_channel_pre_negotiated)
 
 GST_END_TEST;
 
-typedef struct
+static void
+_count_non_rejected_media (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * sd, gpointer user_data)
 {
-  guint num_media;
-  guint num_active_media;
-  const gchar **bundled;
-  const gchar **bundled_only;
-} BundleCheckData;
+  guint expected = GPOINTER_TO_UINT (user_data);
+  guint non_rejected_media;
+  guint i;
+
+  non_rejected_media = 0;
+
+  for (i = 0; i < gst_sdp_message_medias_len (sd->sdp); i++) {
+    const GstSDPMedia *media = gst_sdp_message_get_media (sd->sdp, i);
+
+    if (gst_sdp_media_get_port (media) != 0)
+      non_rejected_media += 1;
+  }
+
+  fail_unless_equals_int (non_rejected_media, expected);
+}
 
 static void
-_check_bundled_sdp_media (struct test_webrtc *t, GstElement * element,
+_check_bundle_tag (struct test_webrtc *t, GstElement * element,
     GstWebRTCSessionDescription * sd, gpointer user_data)
 {
   gchar **bundled = NULL;
-  BundleCheckData *data = (BundleCheckData *) user_data;
+  GStrv expected = user_data;
   guint i;
-  guint active_media;
-
-  fail_unless_equals_int (gst_sdp_message_medias_len (sd->sdp),
-      data->num_media);
 
-  fail_unless (_parse_bundle (sd->sdp, &bundled));
+  fail_unless (_parse_bundle (sd->sdp, &bundled, NULL));
 
   if (!bundled) {
-    fail_unless_equals_int (g_strv_length ((GStrv) data->bundled), 0);
+    fail_unless_equals_int (g_strv_length (expected), 0);
   } else {
-    fail_unless_equals_int (g_strv_length (bundled),
-        g_strv_length ((GStrv) data->bundled));
+    fail_unless_equals_int (g_strv_length (bundled), g_strv_length (expected));
   }
 
-  for (i = 0; data->bundled[i]; i++) {
-    fail_unless (g_strv_contains ((const gchar **) bundled, data->bundled[i]));
+  for (i = 0; i < g_strv_length (expected); i++) {
+    fail_unless (g_strv_contains ((const gchar **) bundled, expected[i]));
   }
 
-  active_media = 0;
+  g_strfreev (bundled);
+}
+
+static void
+_check_bundle_only_media (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * sd, gpointer user_data)
+{
+  gchar **expected_bundle_only = user_data;
+  guint i;
 
   for (i = 0; i < gst_sdp_message_medias_len (sd->sdp); i++) {
     const GstSDPMedia *media = gst_sdp_message_get_media (sd->sdp, i);
     const gchar *mid = gst_sdp_media_get_attribute_val (media, "mid");
 
-    if (g_strv_contains ((const gchar **) data->bundled_only, mid))
+    if (g_strv_contains ((const gchar **) expected_bundle_only, mid))
       fail_unless (_media_has_attribute_key (media, "bundle-only"));
-
-    if (gst_sdp_media_get_port (media) != 0)
-      active_media += 1;
   }
-
-  fail_unless_equals_int (active_media, data->num_active_media);
-
-  if (bundled)
-    g_strfreev (bundled);
 }
 
 GST_START_TEST (test_bundle_audio_video_max_bundle_max_bundle)
@@ -2114,52 +2301,51 @@ GST_START_TEST (test_bundle_audio_video_max_bundle_max_bundle)
   const gchar *bundle[] = { "audio0", "video1", NULL };
   const gchar *offer_bundle_only[] = { "video1", NULL };
   const gchar *answer_bundle_only[] = { NULL };
+
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  VAL_SDP_INIT (bundle_tag, _check_bundle_tag, bundle, &payloads);
+  VAL_SDP_INIT (offer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (1), &bundle_tag);
+  VAL_SDP_INIT (answer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (2), &bundle_tag);
+  VAL_SDP_INIT (offer_bundle, _check_bundle_only_media, &offer_bundle_only,
+      &offer_non_reject);
+  VAL_SDP_INIT (answer_bundle, _check_bundle_only_media, &answer_bundle_only,
+      &answer_non_reject);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &offer_bundle);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &answer_bundle);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
   /* We set a max-bundle policy on the offering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, and they should be marked
    * as bundle-only
    */
-  BundleCheckData offer_data = {
-    2,
-    1,
-    bundle,
-    offer_bundle_only,
-  };
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-bundle");
   /* We also set a max-bundle policy on the answering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, but need not be marked
    * as bundle-only.
    */
-  BundleCheckData answer_data = {
-    2,
-    2,
-    bundle,
-    answer_bundle_only,
-  };
-  struct validate_sdp offer = { _check_bundled_sdp_media, &offer_data, NULL };
-  struct validate_sdp answer = { _check_bundled_sdp_media, &answer_data, NULL };
-
-  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
-      "max-bundle");
   gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
       "max-bundle");
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
 
   test_webrtc_free (t);
 }
@@ -2171,52 +2357,46 @@ GST_START_TEST (test_bundle_audio_video_max_compat_max_bundle)
   struct test_webrtc *t = create_audio_video_test ();
   const gchar *bundle[] = { "audio0", "video1", NULL };
   const gchar *bundle_only[] = { NULL };
+
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (bundle_tag, _check_bundle_tag, bundle, &count);
+  VAL_SDP_INIT (count_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (2), &bundle_tag);
+  VAL_SDP_INIT (bundle_sdp, _check_bundle_only_media, &bundle_only,
+      &count_non_reject);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &bundle_sdp);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &bundle_sdp);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
   /* We set a max-compat policy on the offering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, and they should *not* be marked
    * as bundle-only
    */
-  BundleCheckData offer_data = {
-    2,
-    2,
-    bundle,
-    bundle_only,
-  };
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-compat");
   /* We set a max-bundle policy on the answering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, but need not be marked
    * as bundle-only.
    */
-  BundleCheckData answer_data = {
-    2,
-    2,
-    bundle,
-    bundle_only,
-  };
-  struct validate_sdp offer = { _check_bundled_sdp_media, &offer_data, NULL };
-  struct validate_sdp answer = { _check_bundled_sdp_media, &answer_data, NULL };
-
-  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
-      "max-compat");
   gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
       "max-bundle");
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
 
   test_webrtc_free (t);
 }
@@ -2226,55 +2406,55 @@ GST_END_TEST;
 GST_START_TEST (test_bundle_audio_video_max_bundle_none)
 {
   struct test_webrtc *t = create_audio_video_test ();
-  const gchar *offer_bundle[] = { "audio0", "video1", NULL };
+  const gchar *offer_mid[] = { "audio0", "video1", NULL };
   const gchar *offer_bundle_only[] = { "video1", NULL };
-  const gchar *answer_bundle[] = { NULL };
+  const gchar *answer_mid[] = { NULL };
   const gchar *answer_bundle_only[] = { NULL };
+
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  VAL_SDP_INIT (count_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (1), &payloads);
+  VAL_SDP_INIT (offer_bundle_tag, _check_bundle_tag, offer_mid,
+      &count_non_reject);
+  VAL_SDP_INIT (answer_bundle_tag, _check_bundle_tag, answer_mid,
+      &count_non_reject);
+  VAL_SDP_INIT (offer_bundle, _check_bundle_only_media, &offer_bundle_only,
+      &offer_bundle_tag);
+  VAL_SDP_INIT (answer_bundle, _check_bundle_only_media, &answer_bundle_only,
+      &answer_bundle_tag);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &offer_bundle);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &answer_bundle);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
   /* We set a max-bundle policy on the offering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, and they should be marked
    * as bundle-only
    */
-  BundleCheckData offer_data = {
-    2,
-    1,
-    offer_bundle,
-    offer_bundle_only,
-  };
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-bundle");
   /* We set a none policy on the answering webrtcbin,
    * this means that the answer should contain no bundled
    * medias, and as the bundle-policy of the offering webrtcbin
    * is set to max-bundle, only one media should be active.
    */
-  BundleCheckData answer_data = {
-    2,
-    1,
-    answer_bundle,
-    answer_bundle_only,
-  };
-  struct validate_sdp offer = { _check_bundled_sdp_media, &offer_data, NULL };
-  struct validate_sdp answer = { _check_bundled_sdp_media, &answer_data, NULL };
-
-  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
-      "max-bundle");
   gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy", "none");
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
-  fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-  fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
-
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
 
   test_webrtc_free (t);
 }
@@ -2284,47 +2464,56 @@ GST_END_TEST;
 GST_START_TEST (test_bundle_audio_video_data)
 {
   struct test_webrtc *t = create_audio_video_test ();
-  const gchar *bundle[] = { "audio0", "video1", "application2", NULL };
+  const gchar *mids[] = { "audio0", "video1", "application2", NULL };
   const gchar *offer_bundle_only[] = { "video1", "application2", NULL };
   const gchar *answer_bundle_only[] = { NULL };
   GObject *channel = NULL;
+
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (3),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  VAL_SDP_INIT (bundle_tag, _check_bundle_tag, mids, &payloads);
+  VAL_SDP_INIT (offer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (1), &bundle_tag);
+  VAL_SDP_INIT (answer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (3), &bundle_tag);
+  VAL_SDP_INIT (offer_bundle, _check_bundle_only_media, &offer_bundle_only,
+      &offer_non_reject);
+  VAL_SDP_INIT (answer_bundle, _check_bundle_only_media, &answer_bundle_only,
+      &answer_non_reject);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &offer_bundle);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &answer_bundle);
+  const gchar *expected_offer_direction[] =
+      { "sendrecv", "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] =
+      { "recvonly", "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
   /* We set a max-bundle policy on the offering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, and they should be marked
    * as bundle-only
    */
-  BundleCheckData offer_data = {
-    3,
-    1,
-    bundle,
-    offer_bundle_only,
-  };
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-bundle");
   /* We also set a max-bundle policy on the answering webrtcbin,
    * this means that all the offered medias should be part
    * of the group:BUNDLE attribute, but need not be marked
    * as bundle-only.
    */
-  BundleCheckData answer_data = {
-    3,
-    3,
-    bundle,
-    answer_bundle_only,
-  };
-  struct validate_sdp offer = { _check_bundled_sdp_media, &offer_data, NULL };
-  struct validate_sdp answer = { _check_bundled_sdp_media, &answer_data, NULL };
-
-  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
-      "max-bundle");
   gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
       "max-bundle");
 
-  t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
-  t->on_ice_candidate = NULL;
-
   fail_if (gst_element_set_state (t->webrtc1,
           GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
   fail_if (gst_element_set_state (t->webrtc2,
@@ -2333,10 +2522,7 @@ GST_START_TEST (test_bundle_audio_video_data)
   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
       &channel);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_validate_sdp (t, &offer, &answer);
 
   g_object_unref (channel);
   test_webrtc_free (t);
@@ -2347,40 +2533,832 @@ GST_END_TEST;
 GST_START_TEST (test_duplicate_nego)
 {
   struct test_webrtc *t = create_audio_video_test ();
-  const gchar *expected_offer[] = { "sendrecv", "sendrecv" };
-  const gchar *expected_answer[] = { "sendrecv", "recvonly" };
-  struct validate_sdp offer = { on_sdp_media_direction, expected_offer, NULL };
-  struct validate_sdp answer =
-      { on_sdp_media_direction, expected_answer, NULL };
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendrecv", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
   GstHarness *h;
+  guint negotiation_flag = 0;
 
   /* check that negotiating twice succeeds */
 
+  t->on_negotiation_needed = on_negotiation_needed_hit;
+  t->negotiation_data = &negotiation_flag;
+
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  test_validate_sdp (t, &offer, &answer);
+  fail_unless_equals_int (negotiation_flag, 1);
+
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_dual_audio)
+{
+  struct test_webrtc *t = create_audio_test ();
+  guint media_format_count[] = { 1, 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendrecv", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  GstHarness *h;
+  GstWebRTCRTPTransceiver *trans;
+  GArray *transceivers;
+
+  /* test that each mline gets a unique transceiver even with the same caps */
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  t->on_negotiation_needed = NULL;
+  test_validate_sdp (t, &offer, &answer);
+
+  g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
+  fail_unless (transceivers != NULL);
+  fail_unless_equals_int (2, transceivers->len);
+
+  trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 0);
+  fail_unless (trans != NULL);
+  fail_unless_equals_int (trans->mline, 0);
+
+  trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1);
+  fail_unless (trans != NULL);
+  fail_unless_equals_int (trans->mline, 1);
+
+  g_array_unref (transceivers);
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+static void
+sdp_increasing_session_version (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  GstWebRTCSessionDescription *previous;
+  const GstSDPOrigin *our_origin, *previous_origin;
+  const gchar *prop;
+  guint64 our_v, previous_v;
+
+  prop =
+      TEST_SDP_IS_LOCAL (t, element,
+      desc) ? "current-local-description" : "current-remote-description";
+  g_object_get (element, prop, &previous, NULL);
+
+  our_origin = gst_sdp_message_get_origin (desc->sdp);
+  previous_origin = gst_sdp_message_get_origin (previous->sdp);
+
+  our_v = g_ascii_strtoull (our_origin->sess_version, NULL, 10);
+  previous_v = g_ascii_strtoull (previous_origin->sess_version, NULL, 10);
+
+  ck_assert_int_lt (previous_v, our_v);
+
+  gst_webrtc_session_description_free (previous);
+}
+
+static void
+sdp_equal_session_id (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  GstWebRTCSessionDescription *previous;
+  const GstSDPOrigin *our_origin, *previous_origin;
+  const gchar *prop;
+
+  prop =
+      TEST_SDP_IS_LOCAL (t, element,
+      desc) ? "current-local-description" : "current-remote-description";
+  g_object_get (element, prop, &previous, NULL);
+
+  our_origin = gst_sdp_message_get_origin (desc->sdp);
+  previous_origin = gst_sdp_message_get_origin (previous->sdp);
+
+  fail_unless_equals_string (previous_origin->sess_id, our_origin->sess_id);
+  gst_webrtc_session_description_free (previous);
+}
+
+static void
+sdp_media_equal_attribute (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, GstWebRTCSessionDescription * previous,
+    const gchar * attr)
+{
+  guint i, n;
+
+  n = MIN (gst_sdp_message_medias_len (previous->sdp),
+      gst_sdp_message_medias_len (desc->sdp));
+
+  for (i = 0; i < n; i++) {
+    const GstSDPMedia *our_media, *other_media;
+    const gchar *our_mid, *other_mid;
+
+    our_media = gst_sdp_message_get_media (desc->sdp, i);
+    other_media = gst_sdp_message_get_media (previous->sdp, i);
+
+    our_mid = gst_sdp_media_get_attribute_val (our_media, attr);
+    other_mid = gst_sdp_media_get_attribute_val (other_media, attr);
+
+    fail_unless_equals_string (our_mid, other_mid);
+  }
+}
+
+static void
+sdp_media_equal_mid (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  GstWebRTCSessionDescription *previous;
+  const gchar *prop;
+
+  prop =
+      TEST_SDP_IS_LOCAL (t, element,
+      desc) ? "current-local-description" : "current-remote-description";
+  g_object_get (element, prop, &previous, NULL);
+
+  sdp_media_equal_attribute (t, element, desc, previous, "mid");
+
+  gst_webrtc_session_description_free (previous);
+}
+
+static void
+sdp_media_equal_ice_params (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  GstWebRTCSessionDescription *previous;
+  const gchar *prop;
+
+  prop =
+      TEST_SDP_IS_LOCAL (t, element,
+      desc) ? "current-local-description" : "current-remote-description";
+  g_object_get (element, prop, &previous, NULL);
+
+  sdp_media_equal_attribute (t, element, desc, previous, "ice-ufrag");
+  sdp_media_equal_attribute (t, element, desc, previous, "ice-pwd");
+
+  gst_webrtc_session_description_free (previous);
+}
+
+static void
+sdp_media_equal_fingerprint (struct test_webrtc *t, GstElement * element,
+    GstWebRTCSessionDescription * desc, gpointer user_data)
+{
+  GstWebRTCSessionDescription *previous;
+  const gchar *prop;
+
+  prop =
+      TEST_SDP_IS_LOCAL (t, element,
+      desc) ? "current-local-description" : "current-remote-description";
+  g_object_get (element, prop, &previous, NULL);
+
+  sdp_media_equal_attribute (t, element, desc, previous, "fingerprint");
+
+  gst_webrtc_session_description_free (previous);
+}
+
+GST_START_TEST (test_renego_add_stream)
+{
+  struct test_webrtc *t = create_audio_video_test ();
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] =
+      { "sendrecv", "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] =
+      { "sendrecv", "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, NULL);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  GstHarness *h;
+
+  /* negotiate an AV stream and then renegotiate an extra stream */
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  test_validate_sdp (t, &offer, &answer);
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_2", NULL);
+  add_fake_audio_src_harness (h, 98);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  media_formats.next = &renego_fingerprint;
+  count.user_data = GUINT_TO_POINTER (3);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_renego_stream_add_data_channel)
+{
+  struct test_webrtc *t = create_audio_video_test ();
+
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { "sendrecv", "sendrecv", NULL };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendrecv", "recvonly", NULL };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, NULL);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  GObject *channel;
+  GstHarness *h;
+
+  /* negotiate an AV stream and then renegotiate a data channel */
   h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
   add_fake_audio_src_harness (h, 96);
   t->harnesses = g_list_prepend (t->harnesses, h);
 
+  test_validate_sdp (t, &offer, &answer);
+
+  g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
+      &channel);
+
+  media_formats.next = &renego_fingerprint;
+  count.user_data = GUINT_TO_POINTER (3);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  g_object_unref (channel);
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_renego_data_channel_add_stream)
+{
+  struct test_webrtc *t = test_webrtc_new ();
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (1),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { NULL, "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { NULL, "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, NULL);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  GObject *channel;
+  GstHarness *h;
+
+  /* negotiate an data channel and then renegotiate to add a av stream */
   t->on_negotiation_needed = NULL;
-  t->offer_data = &offer;
-  t->on_offer_created = _check_validate_sdp;
-  t->answer_data = &answer;
-  t->on_answer_created = _check_validate_sdp;
   t->on_ice_candidate = NULL;
+  t->on_pad_added = _pad_added_fakesink;
 
   fail_if (gst_element_set_state (t->webrtc1,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
   fail_if (gst_element_set_state (t->webrtc2,
-          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE);
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
 
-  test_webrtc_create_offer (t, t->webrtc1);
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
+      &channel);
 
-  test_webrtc_signal_state (t, STATE_NEGOTIATION_NEEDED);
+  test_validate_sdp_full (t, &offer, &answer, 0, FALSE);
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
+  add_fake_audio_src_harness (h, 97);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  media_formats.next = &renego_fingerprint;
+  count.user_data = GUINT_TO_POINTER (2);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp_full (t, &offer, &answer, 0, FALSE);
+
+  g_object_unref (channel);
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+
+GST_START_TEST (test_renego_stream_data_channel_add_stream)
+{
+  struct test_webrtc *t = test_webrtc_new ();
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { "sendrecv", NULL, "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "recvonly", NULL, "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, NULL);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  GObject *channel;
+  GstHarness *h;
+
+  /* Negotiate a stream and a data channel, then renogotiate with a new stream */
+  t->on_negotiation_needed = NULL;
+  t->on_ice_candidate = NULL;
+  t->on_pad_added = _pad_added_fakesink;
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 97);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  fail_if (gst_element_set_state (t->webrtc1,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
+  fail_if (gst_element_set_state (t->webrtc2,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE);
+
+  g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
+      &channel);
+
+  test_validate_sdp_full (t, &offer, &answer, 0, FALSE);
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_2", NULL);
+  add_fake_audio_src_harness (h, 97);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  media_formats.next = &renego_fingerprint;
+  count.user_data = GUINT_TO_POINTER (3);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp_full (t, &offer, &answer, 0, FALSE);
+
+  g_object_unref (channel);
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_bundle_renego_add_stream)
+{
+  struct test_webrtc *t = create_audio_video_test ();
+  const gchar *bundle[] = { "audio0", "video1", "audio2", NULL };
+  const gchar *offer_bundle_only[] = { "video1", "audio2", NULL };
+  const gchar *answer_bundle_only[] = { NULL };
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] =
+      { "sendrecv", "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] =
+      { "sendrecv", "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, &payloads);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  VAL_SDP_INIT (bundle_tag, _check_bundle_tag, bundle, &renego_fingerprint);
+  VAL_SDP_INIT (offer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (1), &bundle_tag);
+  VAL_SDP_INIT (answer_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (3), &bundle_tag);
+  VAL_SDP_INIT (offer_bundle_only_sdp, _check_bundle_only_media,
+      &offer_bundle_only, &offer_non_reject);
+  VAL_SDP_INIT (answer_bundle_only_sdp, _check_bundle_only_media,
+      &answer_bundle_only, &answer_non_reject);
+  GstHarness *h;
+
+  /* We set a max-bundle policy on the offering webrtcbin,
+   * this means that all the offered medias should be part
+   * of the group:BUNDLE attribute, and they should be marked
+   * as bundle-only
+   */
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-bundle");
+  /* We also set a max-bundle policy on the answering webrtcbin,
+   * this means that all the offered medias should be part
+   * of the group:BUNDLE attribute, but need not be marked
+   * as bundle-only.
+   */
+  gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
+      "max-bundle");
+
+  /* negotiate an AV stream and then renegotiate an extra stream */
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  test_validate_sdp (t, &offer, &answer);
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_2", NULL);
+  add_fake_audio_src_harness (h, 98);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  offer_setup.next = &offer_bundle_only_sdp;
+  answer_setup.next = &answer_bundle_only_sdp;
+  count.user_data = GUINT_TO_POINTER (3);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_bundle_max_compat_max_bundle_renego_add_stream)
+{
+  struct test_webrtc *t = create_audio_video_test ();
+  const gchar *bundle[] = { "audio0", "video1", "audio2", NULL };
+  const gchar *bundle_only[] = { NULL };
+  guint media_format_count[] = { 1, 1, 1 };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (2),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", "actpass", "actpass" };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", "active", "active" };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] =
+      { "sendrecv", "sendrecv", "sendrecv" };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] =
+      { "sendrecv", "recvonly", "recvonly" };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+
+  VAL_SDP_INIT (renego_mid, sdp_media_equal_mid, NULL, NULL);
+  VAL_SDP_INIT (renego_ice_params, sdp_media_equal_ice_params, NULL,
+      &renego_mid);
+  VAL_SDP_INIT (renego_sess_id, sdp_equal_session_id, NULL, &renego_ice_params);
+  VAL_SDP_INIT (renego_sess_ver, sdp_increasing_session_version, NULL,
+      &renego_sess_id);
+  VAL_SDP_INIT (renego_fingerprint, sdp_media_equal_fingerprint, NULL,
+      &renego_sess_ver);
+  VAL_SDP_INIT (bundle_tag, _check_bundle_tag, bundle, &renego_fingerprint);
+  VAL_SDP_INIT (count_non_reject, _count_non_rejected_media,
+      GUINT_TO_POINTER (3), &bundle_tag);
+  VAL_SDP_INIT (bundle_sdp, _check_bundle_only_media, &bundle_only,
+      &count_non_reject);
+  GstHarness *h;
+
+  /* We set a max-compat policy on the offering webrtcbin,
+   * this means that all the offered medias should be part
+   * of the group:BUNDLE attribute, and they should *not* be marked
+   * as bundle-only
+   */
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-compat");
+  /* We set a max-bundle policy on the answering webrtcbin,
+   * this means that all the offered medias should be part
+   * of the group:BUNDLE attribute, but need not be marked
+   * as bundle-only.
+   */
+  gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
+      "max-bundle");
+
+  /* negotiate an AV stream and then renegotiate an extra stream */
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  test_validate_sdp (t, &offer, &answer);
+
+  h = gst_harness_new_with_element (t->webrtc1, "sink_2", NULL);
+  add_fake_audio_src_harness (h, 98);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  media_formats.next = &bundle_sdp;
+  count.user_data = GUINT_TO_POINTER (3);
+
+  /* renegotiate! */
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_renego_transceiver_set_direction)
+{
+  struct test_webrtc *t = create_audio_test ();
+  guint media_format_count[] = { 1, };
+  VAL_SDP_INIT (media_formats, on_sdp_media_count_formats,
+      media_format_count, NULL);
+  VAL_SDP_INIT (count, _count_num_sdp_media, GUINT_TO_POINTER (1),
+      &media_formats);
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, &count);
+  const gchar *expected_offer_setup[] = { "actpass", };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &payloads);
+  const gchar *expected_answer_setup[] = { "active", };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &payloads);
+  const gchar *expected_offer_direction[] = { "sendrecv", };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendrecv", };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  GstWebRTCRTPTransceiver *transceiver;
+  GstHarness *h;
+  GstPad *pad;
+
+  /* negotiate an AV stream and then change the transceiver direction */
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_audio_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+
+  test_validate_sdp (t, &offer, &answer);
+
+  /* renegotiate an inactive transceiver! */
+  pad = gst_element_get_static_pad (t->webrtc1, "sink_0");
+  g_object_get (pad, "transceiver", &transceiver, NULL);
+  fail_unless (transceiver != NULL);
+  g_object_set (transceiver, "direction",
+      GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_INACTIVE, NULL);
+  expected_offer_direction[0] = "inactive";
+  expected_answer_direction[0] = "inactive";
+
+  /* TODO: also validate EOS events from the inactive change */
+
+  test_webrtc_reset_negotiation (t);
+  test_validate_sdp (t, &offer, &answer);
+
+  gst_object_unref (pad);
+  gst_object_unref (transceiver);
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+static void
+offer_remove_last_media (struct test_webrtc *t, GstElement * element,
+    GstPromise * promise, gpointer user_data)
+{
+  guint i, n;
+  GstSDPMessage *new, *old;
+  const GstSDPOrigin *origin;
+  const GstSDPConnection *conn;
+
+  old = t->offer_desc->sdp;
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_new (&new));
+
+  origin = gst_sdp_message_get_origin (old);
+  conn = gst_sdp_message_get_connection (old);
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_version (new,
+          gst_sdp_message_get_version (old)));
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_origin (new,
+          origin->username, origin->sess_id, origin->sess_version,
+          origin->nettype, origin->addrtype, origin->addr));
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_session_name (new,
+          gst_sdp_message_get_session_name (old)));
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_information (new,
+          gst_sdp_message_get_information (old)));
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_uri (new,
+          gst_sdp_message_get_uri (old)));
+  fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_set_connection (new,
+          conn->nettype, conn->addrtype, conn->address, conn->ttl,
+          conn->addr_number));
+
+  n = gst_sdp_message_attributes_len (old);
+  for (i = 0; i < n; i++) {
+    const GstSDPAttribute *a = gst_sdp_message_get_attribute (old, i);
+    fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_add_attribute (new,
+            a->key, a->value));
+  }
+
+  n = gst_sdp_message_medias_len (old);
+  fail_unless (n > 0);
+  for (i = 0; i < n - 1; i++) {
+    const GstSDPMedia *m = gst_sdp_message_get_media (old, i);
+    GstSDPMedia *new_m;
+
+    fail_unless_equals_int (GST_SDP_OK, gst_sdp_media_copy (m, &new_m));
+    fail_unless_equals_int (GST_SDP_OK, gst_sdp_message_add_media (new, new_m));
+    gst_sdp_media_init (new_m);
+    gst_sdp_media_free (new_m);
+  }
+
+  gst_webrtc_session_description_free (t->offer_desc);
+  t->offer_desc = gst_webrtc_session_description_new (GST_WEBRTC_SDP_TYPE_OFFER,
+      new);
+}
+
+static void
+offer_set_produced_error (struct test_webrtc *t, GstElement * element,
+    GstPromise * promise, gpointer user_data)
+{
+  const GstStructure *reply;
+  GError *error = NULL;
+
+  reply = gst_promise_get_reply (promise);
+  fail_unless (gst_structure_get (reply, "error", G_TYPE_ERROR, &error, NULL));
+  GST_INFO ("error produced: %s", error->message);
+  g_clear_error (&error);
+
+  test_webrtc_signal_state_unlocked (t, STATE_CUSTOM);
+}
+
+GST_START_TEST (test_renego_lose_media_fails)
+{
+  struct test_webrtc *t = create_audio_video_test ();
+  VAL_SDP_INIT (offer, _count_num_sdp_media, GUINT_TO_POINTER (2), NULL);
+  VAL_SDP_INIT (answer, _count_num_sdp_media, GUINT_TO_POINTER (2), NULL);
+
+  /* check that removing an m=line will produce an error */
+
+  test_validate_sdp (t, &offer, &answer);
+
+  test_webrtc_reset_negotiation (t);
+
+  t->on_offer_created = offer_remove_last_media;
+  t->on_offer_set = offer_set_produced_error;
+  t->on_answer_created = NULL;
 
   test_webrtc_create_offer (t, t->webrtc1);
-  test_webrtc_wait_for_answer_error_eos (t);
-  fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
+  test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
+
+  test_webrtc_free (t);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_bundle_codec_preferences_rtx_no_duplicate_payloads)
+{
+  struct test_webrtc *t = test_webrtc_new ();
+  GstWebRTCRTPTransceiverDirection direction;
+  GstWebRTCRTPTransceiver *trans;
+  guint offer_media_format_count[] = { 2, };
+  guint answer_media_format_count[] = { 1, };
+  VAL_SDP_INIT (payloads, on_sdp_media_no_duplicate_payloads, NULL, NULL);
+  VAL_SDP_INIT (offer_media_formats, on_sdp_media_count_formats,
+      offer_media_format_count, &payloads);
+  VAL_SDP_INIT (answer_media_formats, on_sdp_media_count_formats,
+      answer_media_format_count, &payloads);
+  const gchar *expected_offer_setup[] = { "actpass", };
+  VAL_SDP_INIT (offer_setup, on_sdp_media_setup, expected_offer_setup,
+      &offer_media_formats);
+  const gchar *expected_answer_setup[] = { "active", };
+  VAL_SDP_INIT (answer_setup, on_sdp_media_setup, expected_answer_setup,
+      &answer_media_formats);
+  const gchar *expected_offer_direction[] = { "recvonly", };
+  VAL_SDP_INIT (offer, on_sdp_media_direction, expected_offer_direction,
+      &offer_setup);
+  const gchar *expected_answer_direction[] = { "sendonly", };
+  VAL_SDP_INIT (answer, on_sdp_media_direction, expected_answer_direction,
+      &answer_setup);
+  GstCaps *caps;
+  GstHarness *h;
+
+  /* add a transceiver that will only receive an opus stream and check that
+   * the created offer is marked as recvonly */
+  t->on_negotiation_needed = NULL;
+  t->on_ice_candidate = NULL;
+  t->on_pad_added = _pad_added_fakesink;
+
+  gst_util_set_object_arg (G_OBJECT (t->webrtc1), "bundle-policy",
+      "max-bundle");
+  gst_util_set_object_arg (G_OBJECT (t->webrtc2), "bundle-policy",
+      "max-bundle");
+
+  /* setup recvonly transceiver */
+  caps = gst_caps_from_string (VP8_RTP_CAPS (96));
+  direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY;
+  g_signal_emit_by_name (t->webrtc1, "add-transceiver", direction, caps,
+      &trans);
+  g_object_set (GST_OBJECT (trans), "do-nack", TRUE, NULL);
+  gst_caps_unref (caps);
+  fail_unless (trans != NULL);
+  gst_object_unref (trans);
+
+  /* setup sendonly peer */
+  h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
+  add_fake_video_src_harness (h, 96);
+  t->harnesses = g_list_prepend (t->harnesses, h);
+  test_validate_sdp (t, &offer, &answer);
+
   test_webrtc_free (t);
 }
 
@@ -2409,9 +3387,9 @@ webrtcbin_suite (void)
     tcase_add_test (tc, test_sdp_no_media);
     tcase_add_test (tc, test_session_stats);
     tcase_add_test (tc, test_audio);
+    tcase_add_test (tc, test_ice_port_restriction);
     tcase_add_test (tc, test_audio_video);
     tcase_add_test (tc, test_media_direction);
-    tcase_add_test (tc, test_media_setup);
     tcase_add_test (tc, test_add_transceiver);
     tcase_add_test (tc, test_get_transceivers);
     tcase_add_test (tc, test_add_recvonly_transceiver);
@@ -2420,7 +3398,15 @@ webrtcbin_suite (void)
     tcase_add_test (tc, test_bundle_audio_video_max_bundle_max_bundle);
     tcase_add_test (tc, test_bundle_audio_video_max_bundle_none);
     tcase_add_test (tc, test_bundle_audio_video_max_compat_max_bundle);
+    tcase_add_test (tc, test_dual_audio);
     tcase_add_test (tc, test_duplicate_nego);
+    tcase_add_test (tc, test_renego_add_stream);
+    tcase_add_test (tc, test_bundle_renego_add_stream);
+    tcase_add_test (tc, test_bundle_max_compat_max_bundle_renego_add_stream);
+    tcase_add_test (tc, test_renego_transceiver_set_direction);
+    tcase_add_test (tc, test_renego_lose_media_fails);
+    tcase_add_test (tc,
+        test_bundle_codec_preferences_rtx_no_duplicate_payloads);
     if (sctpenc && sctpdec) {
       tcase_add_test (tc, test_data_channel_create);
       tcase_add_test (tc, test_data_channel_remote_notify);
@@ -2431,9 +3417,12 @@ webrtcbin_suite (void)
       tcase_add_test (tc, test_data_channel_max_message_size);
       tcase_add_test (tc, test_data_channel_pre_negotiated);
       tcase_add_test (tc, test_bundle_audio_video_data);
+      tcase_add_test (tc, test_renego_stream_add_data_channel);
+      tcase_add_test (tc, test_renego_data_channel_add_stream);
+      tcase_add_test (tc, test_renego_stream_data_channel_add_stream);
     } else {
       GST_WARNING ("Some required elements were not found. "
-          "All datachannel are disabled. sctpenc %p, sctpdec %p", sctpenc,
+          "All datachannel tests are disabled. sctpenc %p, sctpdec %p", sctpenc,
           sctpdec);
     }
   } else {