3 * Unit tests for webrtcbin
5 * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
29 #include <gst/check/gstcheck.h>
30 #include <gst/check/gstharness.h>
31 #include <gst/webrtc/webrtc.h>
33 #define OPUS_RTP_CAPS(pt) "application/x-rtp,payload=" G_STRINGIFY(pt) ",encoding-name=OPUS,media=audio,clock-rate=48000,ssrc=(uint)3384078950"
34 #define VP8_RTP_CAPS(pt) "application/x-rtp,payload=" G_STRINGIFY(pt) ",encoding-name=VP8,media=video,clock-rate=90000,ssrc=(uint)3484078950"
39 STATE_NEGOTATION_NEEDED,
47 /* basic premise of this is that webrtc1 and webrtc2 are attempting to connect
48 * to each other in various configurations */
64 GDestroyNotify data_notify;
66 void (*on_negotiation_needed) (struct test_webrtc * t,
69 gpointer negotiation_data;
70 GDestroyNotify negotiation_notify;
71 void (*on_ice_candidate) (struct test_webrtc * t,
77 gpointer ice_candidate_data;
78 GDestroyNotify ice_candidate_notify;
79 GstWebRTCSessionDescription * (*on_offer_created) (struct test_webrtc * t,
84 GDestroyNotify offer_notify;
85 GstWebRTCSessionDescription * (*on_answer_created) (struct test_webrtc * t,
89 gpointer data_channel_data;
90 GDestroyNotify data_channel_notify;
91 void (*on_data_channel) (struct test_webrtc * t,
93 GObject *data_channel,
96 GDestroyNotify answer_notify;
97 void (*on_pad_added) (struct test_webrtc * t,
101 gpointer pad_added_data;
102 GDestroyNotify pad_added_notify;
103 void (*bus_message) (struct test_webrtc * t,
108 GDestroyNotify bus_notify;
113 _on_answer_received (GstPromise * promise, gpointer user_data)
115 struct test_webrtc *t = user_data;
116 GstElement *offeror = t->offerror == 1 ? t->webrtc1 : t->webrtc2;
117 GstElement *answerer = t->offerror == 2 ? t->webrtc1 : t->webrtc2;
118 const GstStructure *reply;
119 GstWebRTCSessionDescription *answer = NULL;
122 reply = gst_promise_get_reply (promise);
123 gst_structure_get (reply, "answer",
124 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &answer, NULL);
125 desc = gst_sdp_message_as_text (answer->sdp);
126 GST_INFO ("Created Answer: %s", desc);
129 g_mutex_lock (&t->lock);
130 if (t->on_answer_created) {
131 gst_webrtc_session_description_free (answer);
132 answer = t->on_answer_created (t, answerer, promise, t->answer_data);
134 gst_promise_unref (promise);
136 g_signal_emit_by_name (answerer, "set-local-description", answer, NULL);
137 g_signal_emit_by_name (offeror, "set-remote-description", answer, NULL);
139 t->state = STATE_ANSWER_CREATED;
140 g_cond_broadcast (&t->cond);
141 g_mutex_unlock (&t->lock);
143 gst_webrtc_session_description_free (answer);
147 _on_offer_received (GstPromise * promise, gpointer user_data)
149 struct test_webrtc *t = user_data;
150 GstElement *offeror = t->offerror == 1 ? t->webrtc1 : t->webrtc2;
151 GstElement *answerer = t->offerror == 2 ? t->webrtc1 : t->webrtc2;
152 const GstStructure *reply;
153 GstWebRTCSessionDescription *offer = NULL;
156 reply = gst_promise_get_reply (promise);
157 gst_structure_get (reply, "offer",
158 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
159 desc = gst_sdp_message_as_text (offer->sdp);
160 GST_INFO ("Created offer: %s", desc);
163 g_mutex_lock (&t->lock);
164 if (t->on_offer_created) {
165 gst_webrtc_session_description_free (offer);
166 offer = t->on_offer_created (t, offeror, promise, t->offer_data);
168 gst_promise_unref (promise);
170 g_signal_emit_by_name (offeror, "set-local-description", offer, NULL);
171 g_signal_emit_by_name (answerer, "set-remote-description", offer, NULL);
173 promise = gst_promise_new_with_change_func (_on_answer_received, t, NULL);
174 g_signal_emit_by_name (answerer, "create-answer", NULL, promise);
176 t->state = STATE_OFFER_CREATED;
177 g_cond_broadcast (&t->cond);
178 g_mutex_unlock (&t->lock);
180 gst_webrtc_session_description_free (offer);
184 _bus_watch (GstBus * bus, GstMessage * msg, struct test_webrtc *t)
186 g_mutex_lock (&t->lock);
187 switch (GST_MESSAGE_TYPE (msg)) {
188 case GST_MESSAGE_STATE_CHANGED:
189 if (GST_ELEMENT (msg->src) == t->webrtc1
190 || GST_ELEMENT (msg->src) == t->webrtc2) {
191 GstState old, new, pending;
193 gst_message_parse_state_changed (msg, &old, &new, &pending);
196 gchar *dump_name = g_strconcat ("%s-state_changed-",
197 GST_OBJECT_NAME (msg->src), gst_element_state_get_name (old), "_",
198 gst_element_state_get_name (new), NULL);
199 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (msg->src),
200 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
205 case GST_MESSAGE_ERROR:{
207 gchar *dbg_info = NULL;
212 g_strconcat ("%s-error", GST_OBJECT_NAME (t->webrtc1), NULL);
213 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (t->webrtc1),
214 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
217 g_strconcat ("%s-error", GST_OBJECT_NAME (t->webrtc2), NULL);
218 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (t->webrtc2),
219 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
223 gst_message_parse_error (msg, &err, &dbg_info);
224 GST_WARNING ("ERROR from element %s: %s\n",
225 GST_OBJECT_NAME (msg->src), err->message);
226 GST_WARNING ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
229 t->state = STATE_ERROR;
230 g_cond_broadcast (&t->cond);
233 case GST_MESSAGE_EOS:{
236 dump_name = g_strconcat ("%s-eos", GST_OBJECT_NAME (t->webrtc1), NULL);
237 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (t->webrtc1),
238 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
240 dump_name = g_strconcat ("%s-eos", GST_OBJECT_NAME (t->webrtc2), NULL);
241 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (t->webrtc2),
242 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
245 GST_INFO ("EOS received\n");
246 t->state = STATE_EOS;
247 g_cond_broadcast (&t->cond);
255 t->bus_message (t, bus, msg, t->bus_data);
256 g_mutex_unlock (&t->lock);
262 _on_negotiation_needed (GstElement * webrtc, struct test_webrtc *t)
264 g_mutex_lock (&t->lock);
265 if (t->on_negotiation_needed)
266 t->on_negotiation_needed (t, webrtc, t->negotiation_data);
267 if (t->state == STATE_NEW)
268 t->state = STATE_NEGOTATION_NEEDED;
269 g_cond_broadcast (&t->cond);
270 g_mutex_unlock (&t->lock);
274 _on_ice_candidate (GstElement * webrtc, guint mlineindex, gchar * candidate,
275 struct test_webrtc *t)
279 g_mutex_lock (&t->lock);
280 other = webrtc == t->webrtc1 ? t->webrtc2 : t->webrtc1;
282 if (t->on_ice_candidate)
283 t->on_ice_candidate (t, webrtc, mlineindex, candidate, other,
284 t->ice_candidate_data);
286 g_signal_emit_by_name (other, "add-ice-candidate", mlineindex, candidate);
287 g_mutex_unlock (&t->lock);
291 _on_pad_added (GstElement * webrtc, GstPad * new_pad, struct test_webrtc *t)
293 g_mutex_lock (&t->lock);
295 t->on_pad_added (t, webrtc, new_pad, t->pad_added_data);
296 g_mutex_unlock (&t->lock);
300 _on_data_channel (GstElement * webrtc, GObject * data_channel,
301 struct test_webrtc *t)
303 g_mutex_lock (&t->lock);
304 if (t->on_data_channel)
305 t->on_data_channel (t, webrtc, data_channel, t->data_channel_data);
306 g_mutex_unlock (&t->lock);
310 _pad_added_not_reached (struct test_webrtc *t, GstElement * element,
311 GstPad * pad, gpointer user_data)
313 g_assert_not_reached ();
317 _ice_candidate_not_reached (struct test_webrtc *t, GstElement * element,
318 guint mlineindex, gchar * candidate, GstElement * other, gpointer user_data)
320 g_assert_not_reached ();
324 _negotiation_not_reached (struct test_webrtc *t, GstElement * element,
327 g_assert_not_reached ();
331 _bus_no_errors (struct test_webrtc *t, GstBus * bus, GstMessage * msg,
334 switch (GST_MESSAGE_TYPE (msg)) {
335 case GST_MESSAGE_ERROR:{
336 g_assert_not_reached ();
344 static GstWebRTCSessionDescription *
345 _offer_answer_not_reached (struct test_webrtc *t, GstElement * element,
346 GstPromise * promise, gpointer user_data)
348 g_assert_not_reached ();
352 _on_data_channel_not_reached (struct test_webrtc *t, GstElement * element,
353 GObject * data_channel, gpointer user_data)
355 g_assert_not_reached ();
359 _broadcast (struct test_webrtc *t)
361 g_mutex_lock (&t->lock);
362 g_cond_broadcast (&t->cond);
363 g_mutex_unlock (&t->lock);
367 _unlock_create_thread (GMutex * lock)
369 g_mutex_unlock (lock);
370 return G_SOURCE_REMOVE;
374 _bus_thread (struct test_webrtc *t)
376 g_mutex_lock (&t->lock);
377 t->loop = g_main_loop_new (NULL, FALSE);
378 g_idle_add ((GSourceFunc) _unlock_create_thread, &t->lock);
379 g_cond_broadcast (&t->cond);
381 g_main_loop_run (t->loop);
383 g_mutex_lock (&t->lock);
384 g_main_loop_unref (t->loop);
386 g_cond_broadcast (&t->cond);
387 g_mutex_unlock (&t->lock);
393 element_added_disable_sync (GstBin * bin, GstBin * sub_bin,
394 GstElement * element, gpointer user_data)
396 GObjectClass *class = G_OBJECT_GET_CLASS (element);
397 if (g_object_class_find_property (class, "async"))
398 g_object_set (element, "async", FALSE, NULL);
399 if (g_object_class_find_property (class, "sync"))
400 g_object_set (element, "sync", FALSE, NULL);
403 static struct test_webrtc *
404 test_webrtc_new (void)
406 struct test_webrtc *ret = g_new0 (struct test_webrtc, 1);
408 ret->on_negotiation_needed = _negotiation_not_reached;
409 ret->on_ice_candidate = _ice_candidate_not_reached;
410 ret->on_pad_added = _pad_added_not_reached;
411 ret->on_offer_created = _offer_answer_not_reached;
412 ret->on_answer_created = _offer_answer_not_reached;
413 ret->on_data_channel = _on_data_channel_not_reached;
414 ret->bus_message = _bus_no_errors;
416 g_mutex_init (&ret->lock);
417 g_cond_init (&ret->cond);
419 ret->bus1 = gst_bus_new ();
420 ret->bus2 = gst_bus_new ();
421 gst_bus_add_watch (ret->bus1, (GstBusFunc) _bus_watch, ret);
422 gst_bus_add_watch (ret->bus2, (GstBusFunc) _bus_watch, ret);
423 ret->webrtc1 = gst_element_factory_make ("webrtcbin", NULL);
424 ret->webrtc2 = gst_element_factory_make ("webrtcbin", NULL);
425 fail_unless (ret->webrtc1 != NULL && ret->webrtc2 != NULL);
427 gst_element_set_bus (ret->webrtc1, ret->bus1);
428 gst_element_set_bus (ret->webrtc2, ret->bus2);
430 g_signal_connect (ret->webrtc1, "deep-element-added",
431 G_CALLBACK (element_added_disable_sync), NULL);
432 g_signal_connect (ret->webrtc2, "deep-element-added",
433 G_CALLBACK (element_added_disable_sync), NULL);
434 g_signal_connect (ret->webrtc1, "on-negotiation-needed",
435 G_CALLBACK (_on_negotiation_needed), ret);
436 g_signal_connect (ret->webrtc2, "on-negotiation-needed",
437 G_CALLBACK (_on_negotiation_needed), ret);
438 g_signal_connect (ret->webrtc1, "on-ice-candidate",
439 G_CALLBACK (_on_ice_candidate), ret);
440 g_signal_connect (ret->webrtc2, "on-ice-candidate",
441 G_CALLBACK (_on_ice_candidate), ret);
442 g_signal_connect (ret->webrtc1, "on-data-channel",
443 G_CALLBACK (_on_data_channel), ret);
444 g_signal_connect (ret->webrtc2, "on-data-channel",
445 G_CALLBACK (_on_data_channel), ret);
446 g_signal_connect (ret->webrtc1, "pad-added", G_CALLBACK (_on_pad_added), ret);
447 g_signal_connect (ret->webrtc2, "pad-added", G_CALLBACK (_on_pad_added), ret);
448 g_signal_connect_swapped (ret->webrtc1, "notify::ice-gathering-state",
449 G_CALLBACK (_broadcast), ret);
450 g_signal_connect_swapped (ret->webrtc2, "notify::ice-gathering-state",
451 G_CALLBACK (_broadcast), ret);
452 g_signal_connect_swapped (ret->webrtc1, "notify::ice-connection-state",
453 G_CALLBACK (_broadcast), ret);
454 g_signal_connect_swapped (ret->webrtc2, "notify::ice-connection-state",
455 G_CALLBACK (_broadcast), ret);
457 ret->thread = g_thread_new ("test-webrtc", (GThreadFunc) _bus_thread, ret);
459 g_mutex_lock (&ret->lock);
461 g_cond_wait (&ret->cond, &ret->lock);
462 g_mutex_unlock (&ret->lock);
468 test_webrtc_free (struct test_webrtc *t)
470 /* Otherwise while one webrtcbin is being destroyed, the other could
471 * generate a signal that calls into the destroyed webrtcbin */
472 g_signal_handlers_disconnect_by_data (t->webrtc1, t);
473 g_signal_handlers_disconnect_by_data (t->webrtc2, t);
475 g_main_loop_quit (t->loop);
476 g_mutex_lock (&t->lock);
478 g_cond_wait (&t->cond, &t->lock);
479 g_mutex_unlock (&t->lock);
481 g_thread_join (t->thread);
483 gst_bus_remove_watch (t->bus1);
484 gst_bus_remove_watch (t->bus2);
486 gst_bus_set_flushing (t->bus1, TRUE);
487 gst_bus_set_flushing (t->bus2, TRUE);
489 gst_object_unref (t->bus1);
490 gst_object_unref (t->bus2);
492 g_list_free_full (t->harnesses, (GDestroyNotify) gst_harness_teardown);
495 t->data_notify (t->user_data);
496 if (t->negotiation_notify)
497 t->negotiation_notify (t->negotiation_data);
498 if (t->ice_candidate_notify)
499 t->ice_candidate_notify (t->ice_candidate_data);
501 t->offer_notify (t->offer_data);
502 if (t->answer_notify)
503 t->answer_notify (t->answer_data);
504 if (t->pad_added_notify)
505 t->pad_added_notify (t->pad_added_data);
506 if (t->data_channel_notify)
507 t->data_channel_notify (t->data_channel_data);
509 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
510 gst_element_set_state (t->webrtc1, GST_STATE_NULL));
511 fail_unless_equals_int (GST_STATE_CHANGE_SUCCESS,
512 gst_element_set_state (t->webrtc2, GST_STATE_NULL));
514 gst_object_unref (t->webrtc1);
515 gst_object_unref (t->webrtc2);
517 g_mutex_clear (&t->lock);
518 g_cond_clear (&t->cond);
524 test_webrtc_create_offer (struct test_webrtc *t, GstElement * webrtc)
528 t->offerror = webrtc == t->webrtc1 ? 1 : 2;
529 promise = gst_promise_new_with_change_func (_on_offer_received, t, NULL);
530 g_signal_emit_by_name (webrtc, "create-offer", NULL, promise);
534 test_webrtc_wait_for_state_mask (struct test_webrtc *t, TestState state)
536 g_mutex_lock (&t->lock);
537 while (((1 << t->state) & state) == 0) {
538 GST_INFO ("test state 0x%x, current 0x%x", state, (1 << t->state));
539 g_cond_wait (&t->cond, &t->lock);
541 GST_INFO ("have test state 0x%x, current 0x%x", state, 1 << t->state);
542 g_mutex_unlock (&t->lock);
546 test_webrtc_wait_for_answer_error_eos (struct test_webrtc *t)
548 TestState states = 0;
549 states |= (1 << STATE_ANSWER_CREATED);
550 states |= (1 << STATE_EOS);
551 states |= (1 << STATE_ERROR);
552 test_webrtc_wait_for_state_mask (t, states);
556 test_webrtc_signal_state_unlocked (struct test_webrtc *t, TestState state)
559 g_cond_broadcast (&t->cond);
563 test_webrtc_signal_state (struct test_webrtc *t, TestState state)
565 g_mutex_lock (&t->lock);
566 test_webrtc_signal_state_unlocked (t, state);
567 g_mutex_unlock (&t->lock);
572 test_webrtc_wait_for_ice_gathering_complete (struct test_webrtc *t)
574 GstWebRTCICEGatheringState ice_state1, ice_state2;
575 g_mutex_lock (&t->lock);
576 g_object_get (t->webrtc1, "ice-gathering-state", &ice_state1, NULL);
577 g_object_get (t->webrtc2, "ice-gathering-state", &ice_state2, NULL);
578 while (ice_state1 != GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE &&
579 ice_state2 != GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE) {
580 g_cond_wait (&t->cond, &t->lock);
581 g_object_get (t->webrtc1, "ice-gathering-state", &ice_state1, NULL);
582 g_object_get (t->webrtc2, "ice-gathering-state", &ice_state2, NULL);
584 g_mutex_unlock (&t->lock);
588 test_webrtc_wait_for_ice_connection (struct test_webrtc *t,
589 GstWebRTCICEConnectionState states)
591 GstWebRTCICEConnectionState ice_state1, ice_state2, current;
592 g_mutex_lock (&t->lock);
593 g_object_get (t->webrtc1, "ice-connection-state", &ice_state1, NULL);
594 g_object_get (t->webrtc2, "ice-connection-state", &ice_state2, NULL);
595 current = (1 << ice_state1) | (1 << ice_state2);
596 while ((current & states) == 0 || (current & ~states)) {
597 g_cond_wait (&t->cond, &t->lock);
598 g_object_get (t->webrtc1, "ice-connection-state", &ice_state1, NULL);
599 g_object_get (t->webrtc2, "ice-connection-state", &ice_state2, NULL);
600 current = (1 << ice_state1) | (1 << ice_state2);
602 g_mutex_unlock (&t->lock);
606 _pad_added_fakesink (struct test_webrtc *t, GstElement * element,
607 GstPad * pad, gpointer user_data)
611 if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
614 h = gst_harness_new_with_element (element, NULL, "src_%u");
615 gst_harness_add_sink_parse (h, "fakesink async=false sync=false");
617 t->harnesses = g_list_prepend (t->harnesses, h);
620 static GstWebRTCSessionDescription *
621 _count_num_sdp_media (struct test_webrtc *t, GstElement * element,
622 GstPromise * promise, gpointer user_data)
624 GstWebRTCSessionDescription *offer = NULL;
625 guint expected = GPOINTER_TO_UINT (user_data);
626 const GstStructure *reply;
629 field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
631 reply = gst_promise_get_reply (promise);
632 gst_structure_get (reply, field,
633 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
635 fail_unless_equals_int (gst_sdp_message_medias_len (offer->sdp), expected);
640 GST_START_TEST (test_sdp_no_media)
642 struct test_webrtc *t = test_webrtc_new ();
644 /* check that a no stream connection creates 0 media sections */
646 t->offer_data = GUINT_TO_POINTER (0);
647 t->on_offer_created = _count_num_sdp_media;
648 t->answer_data = GUINT_TO_POINTER (0);
649 t->on_answer_created = _count_num_sdp_media;
651 test_webrtc_create_offer (t, t->webrtc1);
653 test_webrtc_wait_for_answer_error_eos (t);
654 fail_unless (t->state == STATE_ANSWER_CREATED);
655 test_webrtc_free (t);
661 add_fake_audio_src_harness (GstHarness * h, gint pt)
663 GstCaps *caps = gst_caps_from_string (OPUS_RTP_CAPS (pt));
664 GstStructure *s = gst_caps_get_structure (caps, 0);
665 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
666 gst_harness_set_src_caps (h, caps);
667 gst_harness_add_src_parse (h, "fakesrc is-live=true", TRUE);
671 add_fake_video_src_harness (GstHarness * h, gint pt)
673 GstCaps *caps = gst_caps_from_string (VP8_RTP_CAPS (pt));
674 GstStructure *s = gst_caps_get_structure (caps, 0);
675 gst_structure_set (s, "payload", G_TYPE_INT, pt, NULL);
676 gst_harness_set_src_caps (h, caps);
677 gst_harness_add_src_parse (h, "fakesrc is-live=true", TRUE);
680 static struct test_webrtc *
681 create_audio_test (void)
683 struct test_webrtc *t = test_webrtc_new ();
686 t->on_negotiation_needed = NULL;
687 t->on_pad_added = _pad_added_fakesink;
689 h = gst_harness_new_with_element (t->webrtc1, "sink_0", NULL);
690 add_fake_audio_src_harness (h, 96);
691 t->harnesses = g_list_prepend (t->harnesses, h);
696 GST_START_TEST (test_audio)
698 struct test_webrtc *t = create_audio_test ();
700 /* check that a single stream connection creates the associated number
701 * of media sections */
703 t->offer_data = GUINT_TO_POINTER (1);
704 t->on_offer_created = _count_num_sdp_media;
705 t->answer_data = GUINT_TO_POINTER (1);
706 t->on_answer_created = _count_num_sdp_media;
707 t->on_ice_candidate = NULL;
709 test_webrtc_create_offer (t, t->webrtc1);
711 test_webrtc_wait_for_answer_error_eos (t);
712 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
713 test_webrtc_free (t);
718 static struct test_webrtc *
719 create_audio_video_test (void)
721 struct test_webrtc *t = test_webrtc_new ();
724 t->on_negotiation_needed = NULL;
725 t->on_pad_added = _pad_added_fakesink;
727 h = gst_harness_new_with_element (t->webrtc1, "sink_0", NULL);
728 add_fake_audio_src_harness (h, 96);
729 t->harnesses = g_list_prepend (t->harnesses, h);
731 h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
732 add_fake_video_src_harness (h, 97);
733 t->harnesses = g_list_prepend (t->harnesses, h);
738 GST_START_TEST (test_audio_video)
740 struct test_webrtc *t = create_audio_video_test ();
742 /* check that a dual stream connection creates the associated number
743 * of media sections */
745 t->offer_data = GUINT_TO_POINTER (2);
746 t->on_offer_created = _count_num_sdp_media;
747 t->answer_data = GUINT_TO_POINTER (2);
748 t->on_answer_created = _count_num_sdp_media;
749 t->on_ice_candidate = NULL;
751 test_webrtc_create_offer (t, t->webrtc1);
753 test_webrtc_wait_for_answer_error_eos (t);
754 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
755 test_webrtc_free (t);
760 typedef void (*ValidateSDPFunc) (struct test_webrtc * t, GstElement * element,
761 GstWebRTCSessionDescription * desc, gpointer user_data);
765 ValidateSDPFunc validate;
769 static GstWebRTCSessionDescription *
770 validate_sdp (struct test_webrtc *t, GstElement * element,
771 GstPromise * promise, gpointer user_data)
773 struct validate_sdp *validate = user_data;
774 GstWebRTCSessionDescription *offer = NULL;
775 const GstStructure *reply;
778 field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
780 reply = gst_promise_get_reply (promise);
781 gst_structure_get (reply, field,
782 GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
784 validate->validate (t, element, offer, validate->user_data);
790 on_sdp_media_direction (struct test_webrtc *t, GstElement * element,
791 GstWebRTCSessionDescription * desc, gpointer user_data)
793 gchar **expected_directions = user_data;
796 for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
797 const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
798 gboolean have_direction = FALSE;
801 for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
802 const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
804 if (g_strcmp0 (attr->key, "inactive") == 0) {
805 fail_unless (have_direction == FALSE,
806 "duplicate/multiple directions for media %u", j);
807 have_direction = TRUE;
808 fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
809 } else if (g_strcmp0 (attr->key, "sendonly") == 0) {
810 fail_unless (have_direction == FALSE,
811 "duplicate/multiple directions for media %u", j);
812 have_direction = TRUE;
813 fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
814 } else if (g_strcmp0 (attr->key, "recvonly") == 0) {
815 fail_unless (have_direction == FALSE,
816 "duplicate/multiple directions for media %u", j);
817 have_direction = TRUE;
818 fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
819 } else if (g_strcmp0 (attr->key, "sendrecv") == 0) {
820 fail_unless (have_direction == FALSE,
821 "duplicate/multiple directions for media %u", j);
822 have_direction = TRUE;
823 fail_unless (g_strcmp0 (attr->key, expected_directions[i]) == 0);
826 fail_unless (have_direction, "no direction attribute in media %u", j);
830 GST_START_TEST (test_media_direction)
832 struct test_webrtc *t = create_audio_video_test ();
833 const gchar *expected_offer[] = { "sendrecv", "sendrecv" };
834 const gchar *expected_answer[] = { "sendrecv", "recvonly" };
835 struct validate_sdp offer = { on_sdp_media_direction, expected_offer };
836 struct validate_sdp answer = { on_sdp_media_direction, expected_answer };
839 /* check the default media directions for transceivers */
841 h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
842 add_fake_audio_src_harness (h, 96);
843 t->harnesses = g_list_prepend (t->harnesses, h);
845 t->offer_data = &offer;
846 t->on_offer_created = validate_sdp;
847 t->answer_data = &answer;
848 t->on_answer_created = validate_sdp;
849 t->on_ice_candidate = NULL;
851 test_webrtc_create_offer (t, t->webrtc1);
853 test_webrtc_wait_for_answer_error_eos (t);
854 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
855 test_webrtc_free (t);
861 on_sdp_media_payload_types (struct test_webrtc *t, GstElement * element,
862 GstWebRTCSessionDescription * desc, gpointer user_data)
864 const GstSDPMedia *vmedia;
867 fail_unless_equals_int (gst_sdp_message_medias_len (desc->sdp), 2);
869 vmedia = gst_sdp_message_get_media (desc->sdp, 1);
871 for (j = 0; j < gst_sdp_media_attributes_len (vmedia); j++) {
872 const GstSDPAttribute *attr = gst_sdp_media_get_attribute (vmedia, j);
874 if (!g_strcmp0 (attr->key, "rtpmap")) {
875 if (g_str_has_prefix (attr->value, "97")) {
876 fail_unless_equals_string (attr->value, "97 VP8/90000");
877 } else if (g_str_has_prefix (attr->value, "96")) {
878 fail_unless_equals_string (attr->value, "96 red/90000");
879 } else if (g_str_has_prefix (attr->value, "98")) {
880 fail_unless_equals_string (attr->value, "98 ulpfec/90000");
881 } else if (g_str_has_prefix (attr->value, "99")) {
882 fail_unless_equals_string (attr->value, "99 rtx/90000");
883 } else if (g_str_has_prefix (attr->value, "100")) {
884 fail_unless_equals_string (attr->value, "100 rtx/90000");
890 /* In this test we verify that webrtcbin will pick available payload
891 * types when it needs to, in that example for RTX and FEC */
892 GST_START_TEST (test_payload_types)
894 struct test_webrtc *t = create_audio_video_test ();
895 struct validate_sdp offer = { on_sdp_media_payload_types, NULL };
896 GstWebRTCRTPTransceiver *trans;
897 GArray *transceivers;
899 t->offer_data = &offer;
900 t->on_offer_created = validate_sdp;
901 t->on_ice_candidate = NULL;
902 /* We don't really care about the answer here */
903 t->on_answer_created = NULL;
905 g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
906 fail_unless_equals_int (transceivers->len, 2);
907 trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1);
908 g_object_set (trans, "fec-type", GST_WEBRTC_FEC_TYPE_ULP_RED, "do-nack", TRUE,
910 g_array_unref (transceivers);
912 test_webrtc_create_offer (t, t->webrtc1);
914 test_webrtc_wait_for_answer_error_eos (t);
915 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
916 test_webrtc_free (t);
922 on_sdp_media_setup (struct test_webrtc *t, GstElement * element,
923 GstWebRTCSessionDescription * desc, gpointer user_data)
925 gchar **expected_setup = user_data;
928 for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
929 const GstSDPMedia *media = gst_sdp_message_get_media (desc->sdp, i);
930 gboolean have_setup = FALSE;
933 for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
934 const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
936 if (g_strcmp0 (attr->key, "setup") == 0) {
937 fail_unless (have_setup == FALSE,
938 "duplicate/multiple setup for media %u", j);
940 fail_unless (g_strcmp0 (attr->value, expected_setup[i]) == 0);
943 fail_unless (have_setup, "no setup attribute in media %u", j);
947 GST_START_TEST (test_media_setup)
949 struct test_webrtc *t = create_audio_test ();
950 const gchar *expected_offer[] = { "actpass" };
951 const gchar *expected_answer[] = { "active" };
952 struct validate_sdp offer = { on_sdp_media_setup, expected_offer };
953 struct validate_sdp answer = { on_sdp_media_setup, expected_answer };
955 /* check the default dtls setup negotiation values */
957 t->offer_data = &offer;
958 t->on_offer_created = validate_sdp;
959 t->answer_data = &answer;
960 t->on_answer_created = validate_sdp;
961 t->on_ice_candidate = NULL;
963 test_webrtc_create_offer (t, t->webrtc1);
965 test_webrtc_wait_for_answer_error_eos (t);
966 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
967 test_webrtc_free (t);
972 GST_START_TEST (test_no_nice_elements_request_pad)
974 struct test_webrtc *t = test_webrtc_new ();
975 GstPluginFeature *nicesrc, *nicesink;
976 GstRegistry *registry;
979 /* check that the absence of libnice elements posts an error on the bus
980 * when requesting a pad */
982 registry = gst_registry_get ();
983 nicesrc = gst_registry_lookup_feature (registry, "nicesrc");
984 nicesink = gst_registry_lookup_feature (registry, "nicesink");
987 gst_registry_remove_feature (registry, nicesrc);
989 gst_registry_remove_feature (registry, nicesink);
991 t->bus_message = NULL;
993 pad = gst_element_get_request_pad (t->webrtc1, "sink_0");
994 fail_unless (pad == NULL);
996 test_webrtc_wait_for_answer_error_eos (t);
997 fail_unless_equals_int (STATE_ERROR, t->state);
998 test_webrtc_free (t);
1001 gst_registry_add_feature (registry, nicesrc);
1003 gst_registry_add_feature (registry, nicesink);
1008 GST_START_TEST (test_no_nice_elements_state_change)
1010 struct test_webrtc *t = test_webrtc_new ();
1011 GstPluginFeature *nicesrc, *nicesink;
1012 GstRegistry *registry;
1014 /* check that the absence of libnice elements posts an error on the bus */
1016 registry = gst_registry_get ();
1017 nicesrc = gst_registry_lookup_feature (registry, "nicesrc");
1018 nicesink = gst_registry_lookup_feature (registry, "nicesink");
1021 gst_registry_remove_feature (registry, nicesrc);
1023 gst_registry_remove_feature (registry, nicesink);
1025 t->bus_message = NULL;
1026 gst_element_set_state (t->webrtc1, GST_STATE_READY);
1028 test_webrtc_wait_for_answer_error_eos (t);
1029 fail_unless_equals_int (STATE_ERROR, t->state);
1030 test_webrtc_free (t);
1033 gst_registry_add_feature (registry, nicesrc);
1035 gst_registry_add_feature (registry, nicesink);
1041 validate_rtc_stats (const GstStructure * s)
1043 GstWebRTCStatsType type = 0;
1047 fail_unless (gst_structure_get (s, "type", GST_TYPE_WEBRTC_STATS_TYPE, &type,
1049 fail_unless (gst_structure_get (s, "id", G_TYPE_STRING, &id, NULL));
1050 fail_unless (gst_structure_get (s, "timestamp", G_TYPE_DOUBLE, &ts, NULL));
1051 fail_unless (type != 0);
1052 fail_unless (ts != 0.);
1053 fail_unless (id != NULL);
1059 validate_codec_stats (const GstStructure * s)
1061 guint pt = 0, clock_rate = 0;
1063 fail_unless (gst_structure_get (s, "payload-type", G_TYPE_UINT, &pt, NULL));
1064 fail_unless (gst_structure_get (s, "clock-rate", G_TYPE_UINT, &clock_rate,
1066 fail_unless (pt >= 0 && pt <= 127);
1067 fail_unless (clock_rate >= 0);
1071 validate_rtc_stream_stats (const GstStructure * s, const GstStructure * stats)
1073 gchar *codec_id, *transport_id;
1074 GstStructure *codec, *transport;
1076 fail_unless (gst_structure_get (s, "codec-id", G_TYPE_STRING, &codec_id,
1078 fail_unless (gst_structure_get (s, "transport-id", G_TYPE_STRING,
1079 &transport_id, NULL));
1081 fail_unless (gst_structure_get (stats, codec_id, GST_TYPE_STRUCTURE, &codec,
1083 fail_unless (gst_structure_get (stats, transport_id, GST_TYPE_STRUCTURE,
1086 fail_unless (codec != NULL);
1087 fail_unless (transport != NULL);
1089 gst_structure_free (transport);
1090 gst_structure_free (codec);
1093 g_free (transport_id);
1097 validate_inbound_rtp_stats (const GstStructure * s, const GstStructure * stats)
1099 guint ssrc, fir, pli, nack;
1101 guint64 packets_received, bytes_received;
1104 GstStructure *remote;
1106 validate_rtc_stream_stats (s, stats);
1108 fail_unless (gst_structure_get (s, "ssrc", G_TYPE_UINT, &ssrc, NULL));
1109 fail_unless (gst_structure_get (s, "fir-count", G_TYPE_UINT, &fir, NULL));
1110 fail_unless (gst_structure_get (s, "pli-count", G_TYPE_UINT, &pli, NULL));
1111 fail_unless (gst_structure_get (s, "nack-count", G_TYPE_UINT, &nack, NULL));
1112 fail_unless (gst_structure_get (s, "packets-received", G_TYPE_UINT64,
1113 &packets_received, NULL));
1114 fail_unless (gst_structure_get (s, "bytes-received", G_TYPE_UINT64,
1115 &bytes_received, NULL));
1116 fail_unless (gst_structure_get (s, "jitter", G_TYPE_DOUBLE, &jitter, NULL));
1117 fail_unless (gst_structure_get (s, "packets-lost", G_TYPE_INT, &packets_lost,
1119 fail_unless (gst_structure_get (s, "remote-id", G_TYPE_STRING, &remote_id,
1121 fail_unless (gst_structure_get (stats, remote_id, GST_TYPE_STRUCTURE, &remote,
1123 fail_unless (remote != NULL);
1125 gst_structure_free (remote);
1130 validate_remote_inbound_rtp_stats (const GstStructure * s,
1131 const GstStructure * stats)
1137 GstStructure *local;
1139 validate_rtc_stream_stats (s, stats);
1141 fail_unless (gst_structure_get (s, "ssrc", G_TYPE_UINT, &ssrc, NULL));
1142 fail_unless (gst_structure_get (s, "jitter", G_TYPE_DOUBLE, &jitter, NULL));
1143 fail_unless (gst_structure_get (s, "packets-lost", G_TYPE_INT, &packets_lost,
1145 fail_unless (gst_structure_get (s, "round-trip-time", G_TYPE_DOUBLE, &rtt,
1147 fail_unless (gst_structure_get (s, "local-id", G_TYPE_STRING, &local_id,
1149 fail_unless (gst_structure_get (stats, local_id, GST_TYPE_STRUCTURE, &local,
1151 fail_unless (local != NULL);
1153 gst_structure_free (local);
1158 validate_outbound_rtp_stats (const GstStructure * s, const GstStructure * stats)
1160 guint ssrc, fir, pli, nack;
1161 guint64 packets_sent, bytes_sent;
1163 GstStructure *remote;
1165 validate_rtc_stream_stats (s, stats);
1167 fail_unless (gst_structure_get (s, "ssrc", G_TYPE_UINT, &ssrc, NULL));
1168 fail_unless (gst_structure_get (s, "fir-count", G_TYPE_UINT, &fir, NULL));
1169 fail_unless (gst_structure_get (s, "pli-count", G_TYPE_UINT, &pli, NULL));
1170 fail_unless (gst_structure_get (s, "nack-count", G_TYPE_UINT, &nack, NULL));
1171 fail_unless (gst_structure_get (s, "packets-sent", G_TYPE_UINT64,
1172 &packets_sent, NULL));
1173 fail_unless (gst_structure_get (s, "bytes-sent", G_TYPE_UINT64, &bytes_sent,
1175 fail_unless (gst_structure_get (s, "remote-id", G_TYPE_STRING, &remote_id,
1177 fail_unless (gst_structure_get (stats, remote_id, GST_TYPE_STRUCTURE, &remote,
1179 fail_unless (remote != NULL);
1181 gst_structure_free (remote);
1186 validate_remote_outbound_rtp_stats (const GstStructure * s,
1187 const GstStructure * stats)
1191 GstStructure *local;
1193 validate_rtc_stream_stats (s, stats);
1195 fail_unless (gst_structure_get (s, "ssrc", G_TYPE_UINT, &ssrc, NULL));
1196 fail_unless (gst_structure_get (s, "local-id", G_TYPE_STRING, &local_id,
1198 fail_unless (gst_structure_get (stats, local_id, GST_TYPE_STRUCTURE, &local,
1200 fail_unless (local != NULL);
1202 gst_structure_free (local);
1207 validate_stats_foreach (GQuark field_id, const GValue * value,
1208 const GstStructure * stats)
1210 const gchar *field = g_quark_to_string (field_id);
1211 GstWebRTCStatsType type;
1212 const GstStructure *s;
1214 fail_unless (GST_VALUE_HOLDS_STRUCTURE (value));
1216 s = gst_value_get_structure (value);
1218 GST_INFO ("validating field %s %" GST_PTR_FORMAT, field, s);
1220 validate_rtc_stats (s);
1221 gst_structure_get (s, "type", GST_TYPE_WEBRTC_STATS_TYPE, &type, NULL);
1222 if (type == GST_WEBRTC_STATS_CODEC) {
1223 validate_codec_stats (s);
1224 } else if (type == GST_WEBRTC_STATS_INBOUND_RTP) {
1225 validate_inbound_rtp_stats (s, stats);
1226 } else if (type == GST_WEBRTC_STATS_OUTBOUND_RTP) {
1227 validate_outbound_rtp_stats (s, stats);
1228 } else if (type == GST_WEBRTC_STATS_REMOTE_INBOUND_RTP) {
1229 validate_remote_inbound_rtp_stats (s, stats);
1230 } else if (type == GST_WEBRTC_STATS_REMOTE_OUTBOUND_RTP) {
1231 validate_remote_outbound_rtp_stats (s, stats);
1232 } else if (type == GST_WEBRTC_STATS_CSRC) {
1233 } else if (type == GST_WEBRTC_STATS_PEER_CONNECTION) {
1234 } else if (type == GST_WEBRTC_STATS_DATA_CHANNEL) {
1235 } else if (type == GST_WEBRTC_STATS_STREAM) {
1236 } else if (type == GST_WEBRTC_STATS_TRANSPORT) {
1237 } else if (type == GST_WEBRTC_STATS_CANDIDATE_PAIR) {
1238 } else if (type == GST_WEBRTC_STATS_LOCAL_CANDIDATE) {
1239 } else if (type == GST_WEBRTC_STATS_REMOTE_CANDIDATE) {
1240 } else if (type == GST_WEBRTC_STATS_CERTIFICATE) {
1242 g_assert_not_reached ();
1249 validate_stats (const GstStructure * stats)
1251 gst_structure_foreach (stats,
1252 (GstStructureForeachFunc) validate_stats_foreach, (gpointer) stats);
1256 _on_stats (GstPromise * promise, gpointer user_data)
1258 struct test_webrtc *t = user_data;
1259 const GstStructure *reply = gst_promise_get_reply (promise);
1262 validate_stats (reply);
1263 i = GPOINTER_TO_INT (t->user_data);
1265 t->user_data = GINT_TO_POINTER (i);
1267 test_webrtc_signal_state (t, STATE_CUSTOM);
1269 gst_promise_unref (promise);
1272 GST_START_TEST (test_session_stats)
1274 struct test_webrtc *t = test_webrtc_new ();
1277 /* test that the stats generated without any streams are sane */
1279 t->on_offer_created = NULL;
1280 t->on_answer_created = NULL;
1282 test_webrtc_create_offer (t, t->webrtc1);
1284 test_webrtc_wait_for_answer_error_eos (t);
1285 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1287 p = gst_promise_new_with_change_func (_on_stats, t, NULL);
1288 g_signal_emit_by_name (t->webrtc1, "get-stats", NULL, p);
1289 p = gst_promise_new_with_change_func (_on_stats, t, NULL);
1290 g_signal_emit_by_name (t->webrtc2, "get-stats", NULL, p);
1292 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1294 test_webrtc_free (t);
1299 GST_START_TEST (test_add_transceiver)
1301 struct test_webrtc *t = test_webrtc_new ();
1302 GstWebRTCRTPTransceiverDirection direction;
1303 GstWebRTCRTPTransceiver *trans;
1305 direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV;
1306 g_signal_emit_by_name (t->webrtc1, "add-transceiver", direction, NULL,
1308 fail_unless (trans != NULL);
1309 fail_unless_equals_int (direction, trans->direction);
1311 gst_object_unref (trans);
1313 test_webrtc_free (t);
1318 GST_START_TEST (test_get_transceivers)
1320 struct test_webrtc *t = create_audio_test ();
1321 GstWebRTCRTPTransceiver *trans;
1322 GArray *transceivers;
1324 g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
1325 fail_unless (transceivers != NULL);
1326 fail_unless_equals_int (1, transceivers->len);
1328 trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 0);
1329 fail_unless (trans != NULL);
1331 g_array_unref (transceivers);
1333 test_webrtc_free (t);
1338 GST_START_TEST (test_add_recvonly_transceiver)
1340 struct test_webrtc *t = test_webrtc_new ();
1341 GstWebRTCRTPTransceiverDirection direction;
1342 GstWebRTCRTPTransceiver *trans;
1343 const gchar *expected_offer[] = { "recvonly" };
1344 const gchar *expected_answer[] = { "sendonly" };
1345 struct validate_sdp offer = { on_sdp_media_direction, expected_offer };
1346 struct validate_sdp answer = { on_sdp_media_direction, expected_answer };
1350 /* add a transceiver that will only receive an opus stream and check that
1351 * the created offer is marked as recvonly */
1353 t->on_pad_added = _pad_added_fakesink;
1354 t->on_negotiation_needed = NULL;
1355 t->offer_data = &offer;
1356 t->on_offer_created = validate_sdp;
1357 t->answer_data = &answer;
1358 t->on_answer_created = validate_sdp;
1359 t->on_ice_candidate = NULL;
1361 /* setup recvonly transceiver */
1362 caps = gst_caps_from_string (OPUS_RTP_CAPS (96));
1363 direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY;
1364 g_signal_emit_by_name (t->webrtc1, "add-transceiver", direction, caps,
1366 gst_caps_unref (caps);
1367 fail_unless (trans != NULL);
1368 gst_object_unref (trans);
1370 /* setup sendonly peer */
1371 h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
1372 add_fake_audio_src_harness (h, 96);
1373 t->harnesses = g_list_prepend (t->harnesses, h);
1375 test_webrtc_create_offer (t, t->webrtc1);
1377 test_webrtc_wait_for_answer_error_eos (t);
1378 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1379 test_webrtc_free (t);
1384 GST_START_TEST (test_recvonly_sendonly)
1386 struct test_webrtc *t = test_webrtc_new ();
1387 GstWebRTCRTPTransceiverDirection direction;
1388 GstWebRTCRTPTransceiver *trans;
1389 const gchar *expected_offer[] = { "recvonly", "sendonly" };
1390 const gchar *expected_answer[] = { "sendonly", "recvonly" };
1391 struct validate_sdp offer = { on_sdp_media_direction, expected_offer };
1392 struct validate_sdp answer = { on_sdp_media_direction, expected_answer };
1395 GArray *transceivers;
1397 /* add a transceiver that will only receive an opus stream and check that
1398 * the created offer is marked as recvonly */
1400 t->on_pad_added = _pad_added_fakesink;
1401 t->on_negotiation_needed = NULL;
1402 t->offer_data = &offer;
1403 t->on_offer_created = validate_sdp;
1404 t->answer_data = &answer;
1405 t->on_answer_created = validate_sdp;
1406 t->on_ice_candidate = NULL;
1408 /* setup recvonly transceiver */
1409 caps = gst_caps_from_string (OPUS_RTP_CAPS (96));
1410 direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY;
1411 g_signal_emit_by_name (t->webrtc1, "add-transceiver", direction, caps,
1413 gst_caps_unref (caps);
1414 fail_unless (trans != NULL);
1415 gst_object_unref (trans);
1417 /* setup sendonly stream */
1418 h = gst_harness_new_with_element (t->webrtc1, "sink_1", NULL);
1419 add_fake_audio_src_harness (h, 96);
1420 t->harnesses = g_list_prepend (t->harnesses, h);
1421 g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
1422 fail_unless (transceivers != NULL);
1423 trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 1);
1424 trans->direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY;
1426 g_array_unref (transceivers);
1428 /* setup sendonly peer */
1429 h = gst_harness_new_with_element (t->webrtc2, "sink_0", NULL);
1430 add_fake_audio_src_harness (h, 96);
1431 t->harnesses = g_list_prepend (t->harnesses, h);
1433 test_webrtc_create_offer (t, t->webrtc1);
1435 test_webrtc_wait_for_answer_error_eos (t);
1436 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1437 test_webrtc_free (t);
1443 _message_media_is_datachannel (const GstSDPMessage * msg, guint media_id)
1445 const GstSDPMedia *media;
1450 if (gst_sdp_message_medias_len (msg) <= media_id)
1453 media = gst_sdp_message_get_media (msg, media_id);
1455 if (g_strcmp0 (gst_sdp_media_get_media (media), "application") != 0)
1458 if (gst_sdp_media_formats_len (media) != 1)
1461 if (g_strcmp0 (gst_sdp_media_get_format (media, 0),
1462 "webrtc-datachannel") != 0)
1469 on_sdp_has_datachannel (struct test_webrtc *t, GstElement * element,
1470 GstWebRTCSessionDescription * desc, gpointer user_data)
1472 gboolean have_data_channel = FALSE;
1475 for (i = 0; i < gst_sdp_message_medias_len (desc->sdp); i++) {
1476 if (_message_media_is_datachannel (desc->sdp, i)) {
1477 /* there should only be one data channel m= section */
1478 fail_unless_equals_int (FALSE, have_data_channel);
1479 have_data_channel = TRUE;
1483 fail_unless_equals_int (TRUE, have_data_channel);
1487 on_channel_error_not_reached (GObject * channel, GError * error,
1490 g_assert_not_reached ();
1493 GST_START_TEST (test_data_channel_create)
1495 struct test_webrtc *t = test_webrtc_new ();
1496 GObject *channel = NULL;
1497 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1498 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1501 t->on_negotiation_needed = NULL;
1502 t->offer_data = &offer;
1503 t->on_offer_created = validate_sdp;
1504 t->answer_data = &answer;
1505 t->on_answer_created = validate_sdp;
1506 t->on_ice_candidate = NULL;
1508 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1510 g_assert_nonnull (channel);
1511 g_object_get (channel, "label", &label, NULL);
1512 g_assert_cmpstr (label, ==, "label");
1513 g_signal_connect (channel, "on-error",
1514 G_CALLBACK (on_channel_error_not_reached), NULL);
1516 test_webrtc_create_offer (t, t->webrtc1);
1518 test_webrtc_wait_for_answer_error_eos (t);
1519 fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1520 g_object_unref (channel);
1522 test_webrtc_free (t);
1528 have_data_channel (struct test_webrtc *t, GstElement * element,
1529 GObject * our, gpointer user_data)
1531 GObject *other = user_data;
1532 gchar *our_label, *other_label;
1534 g_signal_connect (our, "on-error", G_CALLBACK (on_channel_error_not_reached),
1537 g_object_get (our, "label", &our_label, NULL);
1538 g_object_get (other, "label", &other_label, NULL);
1540 g_assert_cmpstr (our_label, ==, other_label);
1543 g_free (other_label);
1545 test_webrtc_signal_state_unlocked (t, STATE_CUSTOM);
1548 GST_START_TEST (test_data_channel_remote_notify)
1550 struct test_webrtc *t = test_webrtc_new ();
1551 GObject *channel = NULL;
1552 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1553 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1555 t->on_negotiation_needed = NULL;
1556 t->offer_data = &offer;
1557 t->on_offer_created = validate_sdp;
1558 t->answer_data = &answer;
1559 t->on_answer_created = validate_sdp;
1560 t->on_ice_candidate = NULL;
1561 t->on_data_channel = have_data_channel;
1563 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1565 g_assert_nonnull (channel);
1566 t->data_channel_data = channel;
1567 g_signal_connect (channel, "on-error",
1568 G_CALLBACK (on_channel_error_not_reached), NULL);
1570 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1571 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1573 test_webrtc_create_offer (t, t->webrtc1);
1575 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1577 g_object_unref (channel);
1578 test_webrtc_free (t);
1583 static const gchar *test_string = "GStreamer WebRTC is awesome!";
1586 on_message_string (GObject * channel, const gchar * str, struct test_webrtc *t)
1588 gchar *expected = g_object_steal_data (channel, "expected");
1589 g_assert_cmpstr (expected, ==, str);
1592 test_webrtc_signal_state (t, STATE_CUSTOM);
1596 have_data_channel_transfer_string (struct test_webrtc *t, GstElement * element,
1597 GObject * our, gpointer user_data)
1599 GObject *other = user_data;
1600 GstWebRTCDataChannelState state;
1602 g_object_get (our, "ready-state", &state, NULL);
1603 fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
1604 g_object_get (other, "ready-state", &state, NULL);
1605 fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
1607 g_object_set_data_full (our, "expected", g_strdup (test_string), g_free);
1608 g_signal_connect (our, "on-message-string", G_CALLBACK (on_message_string),
1611 g_signal_connect (other, "on-error",
1612 G_CALLBACK (on_channel_error_not_reached), NULL);
1613 g_signal_emit_by_name (other, "send-string", test_string);
1616 GST_START_TEST (test_data_channel_transfer_string)
1618 struct test_webrtc *t = test_webrtc_new ();
1619 GObject *channel = NULL;
1620 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1621 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1623 t->on_negotiation_needed = NULL;
1624 t->offer_data = &offer;
1625 t->on_offer_created = validate_sdp;
1626 t->answer_data = &answer;
1627 t->on_answer_created = validate_sdp;
1628 t->on_ice_candidate = NULL;
1629 t->on_data_channel = have_data_channel_transfer_string;
1631 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1633 g_assert_nonnull (channel);
1634 t->data_channel_data = channel;
1635 g_signal_connect (channel, "on-error",
1636 G_CALLBACK (on_channel_error_not_reached), NULL);
1638 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1639 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1641 test_webrtc_create_offer (t, t->webrtc1);
1643 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1645 g_object_unref (channel);
1646 test_webrtc_free (t);
1651 #define g_assert_cmpbytes(b1, b2) \
1654 const guint8 *d1 = g_bytes_get_data (b1, &l1); \
1655 const guint8 *d2 = g_bytes_get_data (b2, &l2); \
1656 g_assert_cmpmem (d1, l1, d2, l2); \
1660 on_message_data (GObject * channel, GBytes * data, struct test_webrtc *t)
1662 GBytes *expected = g_object_steal_data (channel, "expected");
1663 g_assert_cmpbytes (data, expected);
1664 g_bytes_unref (expected);
1666 test_webrtc_signal_state (t, STATE_CUSTOM);
1670 have_data_channel_transfer_data (struct test_webrtc *t, GstElement * element,
1671 GObject * our, gpointer user_data)
1673 GObject *other = user_data;
1674 GBytes *data = g_bytes_new_static (test_string, strlen (test_string));
1675 GstWebRTCDataChannelState state;
1677 g_object_get (our, "ready-state", &state, NULL);
1678 fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
1679 g_object_get (other, "ready-state", &state, NULL);
1680 fail_unless_equals_int (GST_WEBRTC_DATA_CHANNEL_STATE_OPEN, state);
1682 g_object_set_data_full (our, "expected", g_bytes_ref (data),
1683 (GDestroyNotify) g_bytes_unref);
1684 g_signal_connect (our, "on-message-data", G_CALLBACK (on_message_data), t);
1686 g_signal_connect (other, "on-error",
1687 G_CALLBACK (on_channel_error_not_reached), NULL);
1688 g_signal_emit_by_name (other, "send-data", data);
1691 GST_START_TEST (test_data_channel_transfer_data)
1693 struct test_webrtc *t = test_webrtc_new ();
1694 GObject *channel = NULL;
1695 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1696 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1698 t->on_negotiation_needed = NULL;
1699 t->offer_data = &offer;
1700 t->on_offer_created = validate_sdp;
1701 t->answer_data = &answer;
1702 t->on_answer_created = validate_sdp;
1703 t->on_ice_candidate = NULL;
1704 t->on_data_channel = have_data_channel_transfer_data;
1706 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1708 g_assert_nonnull (channel);
1709 t->data_channel_data = channel;
1710 g_signal_connect (channel, "on-error",
1711 G_CALLBACK (on_channel_error_not_reached), NULL);
1713 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1714 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1716 test_webrtc_create_offer (t, t->webrtc1);
1718 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1720 g_object_unref (channel);
1721 test_webrtc_free (t);
1727 have_data_channel_create_data_channel (struct test_webrtc *t,
1728 GstElement * element, GObject * our, gpointer user_data)
1732 t->on_data_channel = have_data_channel_transfer_string;
1734 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1736 g_assert_nonnull (another);
1737 t->data_channel_data = another;
1738 g_signal_connect (another, "on-error",
1739 G_CALLBACK (on_channel_error_not_reached), NULL);
1742 GST_START_TEST (test_data_channel_create_after_negotiate)
1744 struct test_webrtc *t = test_webrtc_new ();
1745 GObject *channel = NULL;
1746 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1747 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1749 t->on_negotiation_needed = NULL;
1750 t->offer_data = &offer;
1751 t->on_offer_created = validate_sdp;
1752 t->answer_data = &answer;
1753 t->on_answer_created = validate_sdp;
1754 t->on_ice_candidate = NULL;
1755 t->on_data_channel = have_data_channel_create_data_channel;
1757 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "prev-label", NULL,
1759 g_assert_nonnull (channel);
1760 t->data_channel_data = channel;
1761 g_signal_connect (channel, "on-error",
1762 G_CALLBACK (on_channel_error_not_reached), NULL);
1764 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1765 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1767 test_webrtc_create_offer (t, t->webrtc1);
1769 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1771 g_object_unref (channel);
1772 test_webrtc_free (t);
1778 on_buffered_amount_low_emitted (GObject * channel, struct test_webrtc *t)
1780 test_webrtc_signal_state (t, STATE_CUSTOM);
1784 have_data_channel_check_low_threshold_emitted (struct test_webrtc *t,
1785 GstElement * element, GObject * our, gpointer user_data)
1787 g_signal_connect (our, "on-buffered-amount-low",
1788 G_CALLBACK (on_buffered_amount_low_emitted), t);
1789 g_object_set (our, "buffered-amount-low-threshold", 1, NULL);
1791 g_signal_connect (our, "on-error", G_CALLBACK (on_channel_error_not_reached),
1793 g_signal_emit_by_name (our, "send-string", "DATA");
1796 GST_START_TEST (test_data_channel_low_threshold)
1798 struct test_webrtc *t = test_webrtc_new ();
1799 GObject *channel = NULL;
1800 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1801 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1803 t->on_negotiation_needed = NULL;
1804 t->offer_data = &offer;
1805 t->on_offer_created = validate_sdp;
1806 t->answer_data = &answer;
1807 t->on_answer_created = validate_sdp;
1808 t->on_ice_candidate = NULL;
1809 t->on_data_channel = have_data_channel_check_low_threshold_emitted;
1811 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1813 g_assert_nonnull (channel);
1814 t->data_channel_data = channel;
1815 g_signal_connect (channel, "on-error",
1816 G_CALLBACK (on_channel_error_not_reached), NULL);
1818 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1819 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1821 test_webrtc_create_offer (t, t->webrtc1);
1823 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1825 g_object_unref (channel);
1826 test_webrtc_free (t);
1832 on_channel_error (GObject * channel, GError * error, struct test_webrtc *t)
1834 g_assert_nonnull (error);
1836 test_webrtc_signal_state (t, STATE_CUSTOM);
1840 have_data_channel_transfer_large_data (struct test_webrtc *t,
1841 GstElement * element, GObject * our, gpointer user_data)
1843 GObject *other = user_data;
1844 const gsize size = 1024 * 1024;
1845 guint8 *random_data = g_new (guint8, size);
1849 for (i = 0; i < size; i++)
1850 random_data[i] = (guint8) (i & 0xff);
1852 data = g_bytes_new_static (random_data, size);
1854 g_object_set_data_full (our, "expected", g_bytes_ref (data),
1855 (GDestroyNotify) g_bytes_unref);
1856 g_signal_connect (our, "on-message-data", G_CALLBACK (on_message_data), t);
1858 g_signal_connect (other, "on-error", G_CALLBACK (on_channel_error), t);
1859 g_signal_emit_by_name (other, "send-data", data);
1862 GST_START_TEST (test_data_channel_max_message_size)
1864 struct test_webrtc *t = test_webrtc_new ();
1865 GObject *channel = NULL;
1866 struct validate_sdp offer = { on_sdp_has_datachannel, NULL };
1867 struct validate_sdp answer = { on_sdp_has_datachannel, NULL };
1869 t->on_negotiation_needed = NULL;
1870 t->offer_data = &offer;
1871 t->on_offer_created = validate_sdp;
1872 t->answer_data = &answer;
1873 t->on_answer_created = validate_sdp;
1874 t->on_ice_candidate = NULL;
1875 t->on_data_channel = have_data_channel_transfer_large_data;
1877 g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1879 g_assert_nonnull (channel);
1880 t->data_channel_data = channel;
1882 gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1883 gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1885 test_webrtc_create_offer (t, t->webrtc1);
1887 test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1889 g_object_unref (channel);
1890 test_webrtc_free (t);
1896 webrtcbin_suite (void)
1898 Suite *s = suite_create ("webrtcbin");
1899 TCase *tc = tcase_create ("general");
1900 GstPluginFeature *nicesrc, *nicesink, *dtlssrtpdec, *dtlssrtpenc;
1901 GstPluginFeature *sctpenc, *sctpdec;
1902 GstRegistry *registry;
1904 registry = gst_registry_get ();
1905 nicesrc = gst_registry_lookup_feature (registry, "nicesrc");
1906 nicesink = gst_registry_lookup_feature (registry, "nicesink");
1907 dtlssrtpenc = gst_registry_lookup_feature (registry, "dtlssrtpenc");
1908 dtlssrtpdec = gst_registry_lookup_feature (registry, "dtlssrtpdec");
1909 sctpenc = gst_registry_lookup_feature (registry, "sctpenc");
1910 sctpdec = gst_registry_lookup_feature (registry, "sctpdec");
1912 tcase_add_test (tc, test_sdp_no_media);
1913 tcase_add_test (tc, test_no_nice_elements_request_pad);
1914 tcase_add_test (tc, test_no_nice_elements_state_change);
1915 tcase_add_test (tc, test_session_stats);
1916 if (nicesrc && nicesink && dtlssrtpenc && dtlssrtpdec) {
1917 tcase_add_test (tc, test_audio);
1918 tcase_add_test (tc, test_audio_video);
1919 tcase_add_test (tc, test_media_direction);
1920 tcase_add_test (tc, test_media_setup);
1921 tcase_add_test (tc, test_add_transceiver);
1922 tcase_add_test (tc, test_get_transceivers);
1923 tcase_add_test (tc, test_add_recvonly_transceiver);
1924 tcase_add_test (tc, test_recvonly_sendonly);
1925 tcase_add_test (tc, test_payload_types);
1926 if (sctpenc && sctpdec) {
1927 tcase_add_test (tc, test_data_channel_create);
1928 tcase_add_test (tc, test_data_channel_remote_notify);
1929 tcase_add_test (tc, test_data_channel_transfer_string);
1930 tcase_add_test (tc, test_data_channel_transfer_data);
1931 tcase_add_test (tc, test_data_channel_create_after_negotiate);
1932 tcase_add_test (tc, test_data_channel_low_threshold);
1933 tcase_add_test (tc, test_data_channel_max_message_size);
1935 GST_WARNING ("Some required elements were not found. "
1936 "All datachannel are disabled. sctpenc %p, sctpdec %p", sctpenc,
1940 GST_WARNING ("Some required elements were not found. "
1941 "All media tests are disabled. nicesrc %p, nicesink %p, "
1942 "dtlssrtpenc %p, dtlssrtpdec %p", nicesrc, nicesink, dtlssrtpenc,
1947 gst_object_unref (nicesrc);
1949 gst_object_unref (nicesink);
1951 gst_object_unref (dtlssrtpdec);
1953 gst_object_unref (dtlssrtpenc);
1955 gst_object_unref (sctpenc);
1957 gst_object_unref (sctpdec);
1959 suite_add_tcase (s, tc);
1964 GST_CHECK_MAIN (webrtcbin);