tests/webrtcbin: add a couple more dependant element checks
[platform/upstream/gstreamer.git] / tests / check / elements / webrtcbin.c
1 /* GStreamer
2  *
3  * Unit tests for webrtcbin
4  *
5  * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gst/check/gstcheck.h>
30 #include <gst/check/gstharness.h>
31 #include <gst/webrtc/webrtc.h>
32
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"
35
36 typedef enum
37 {
38   STATE_NEW,
39   STATE_NEGOTATION_NEEDED,
40   STATE_OFFER_CREATED,
41   STATE_ANSWER_CREATED,
42   STATE_EOS,
43   STATE_ERROR,
44   STATE_CUSTOM,
45 } TestState;
46
47 /* basic premise of this is that webrtc1 and webrtc2 are attempting to connect
48  * to each other in various configurations */
49 struct test_webrtc;
50 struct test_webrtc
51 {
52   GList *harnesses;
53   GThread *thread;
54   GMainLoop *loop;
55   GstBus *bus1;
56   GstBus *bus2;
57   GstElement *webrtc1;
58   GstElement *webrtc2;
59   GMutex lock;
60   GCond cond;
61   TestState state;
62   guint offerror;
63   gpointer user_data;
64   GDestroyNotify data_notify;
65 /* *INDENT-OFF* */
66   void      (*on_negotiation_needed)    (struct test_webrtc * t,
67                                          GstElement * element,
68                                          gpointer user_data);
69   gpointer negotiation_data;
70   GDestroyNotify negotiation_notify;
71   void      (*on_ice_candidate)         (struct test_webrtc * t,
72                                          GstElement * element,
73                                          guint mlineindex,
74                                          gchar * candidate,
75                                          GstElement * other,
76                                          gpointer user_data);
77   gpointer ice_candidate_data;
78   GDestroyNotify ice_candidate_notify;
79   GstWebRTCSessionDescription * (*on_offer_created)     (struct test_webrtc * t,
80                                                          GstElement * element,
81                                                          GstPromise * promise,
82                                                          gpointer user_data);
83   gpointer offer_data;
84   GDestroyNotify offer_notify;
85   GstWebRTCSessionDescription * (*on_answer_created)    (struct test_webrtc * t,
86                                                          GstElement * element,
87                                                          GstPromise * promise,
88                                                          gpointer user_data);
89   gpointer data_channel_data;
90   GDestroyNotify data_channel_notify;
91   void       (*on_data_channel)                         (struct test_webrtc * t,
92                                                          GstElement * element,
93                                                          GObject *data_channel,
94                                                          gpointer user_data);
95   gpointer answer_data;
96   GDestroyNotify answer_notify;
97   void      (*on_pad_added)             (struct test_webrtc * t,
98                                          GstElement * element,
99                                          GstPad * pad,
100                                          gpointer user_data);
101   gpointer pad_added_data;
102   GDestroyNotify pad_added_notify;
103   void      (*bus_message)              (struct test_webrtc * t,
104                                          GstBus * bus,
105                                          GstMessage * msg,
106                                          gpointer user_data);
107   gpointer bus_data;
108   GDestroyNotify bus_notify;
109 /* *INDENT-ON* */
110 };
111
112 static void
113 _on_answer_received (GstPromise * promise, gpointer user_data)
114 {
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;
120   gchar *desc;
121
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);
127   g_free (desc);
128
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);
133   }
134   gst_promise_unref (promise);
135
136   g_signal_emit_by_name (answerer, "set-local-description", answer, NULL);
137   g_signal_emit_by_name (offeror, "set-remote-description", answer, NULL);
138
139   t->state = STATE_ANSWER_CREATED;
140   g_cond_broadcast (&t->cond);
141   g_mutex_unlock (&t->lock);
142
143   gst_webrtc_session_description_free (answer);
144 }
145
146 static void
147 _on_offer_received (GstPromise * promise, gpointer user_data)
148 {
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;
154   gchar *desc;
155
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);
161   g_free (desc);
162
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);
167   }
168   gst_promise_unref (promise);
169
170   g_signal_emit_by_name (offeror, "set-local-description", offer, NULL);
171   g_signal_emit_by_name (answerer, "set-remote-description", offer, NULL);
172
173   promise = gst_promise_new_with_change_func (_on_answer_received, t, NULL);
174   g_signal_emit_by_name (answerer, "create-answer", NULL, promise);
175
176   t->state = STATE_OFFER_CREATED;
177   g_cond_broadcast (&t->cond);
178   g_mutex_unlock (&t->lock);
179
180   gst_webrtc_session_description_free (offer);
181 }
182
183 static gboolean
184 _bus_watch (GstBus * bus, GstMessage * msg, struct test_webrtc *t)
185 {
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;
192
193         gst_message_parse_state_changed (msg, &old, &new, &pending);
194
195         {
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);
201           g_free (dump_name);
202         }
203       }
204       break;
205     case GST_MESSAGE_ERROR:{
206       GError *err = NULL;
207       gchar *dbg_info = NULL;
208
209       {
210         gchar *dump_name;
211         dump_name =
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);
215         g_free (dump_name);
216         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);
220         g_free (dump_name);
221       }
222
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");
227       g_error_free (err);
228       g_free (dbg_info);
229       t->state = STATE_ERROR;
230       g_cond_broadcast (&t->cond);
231       break;
232     }
233     case GST_MESSAGE_EOS:{
234       {
235         gchar *dump_name;
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);
239         g_free (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);
243         g_free (dump_name);
244       }
245       GST_INFO ("EOS received\n");
246       t->state = STATE_EOS;
247       g_cond_broadcast (&t->cond);
248       break;
249     }
250     default:
251       break;
252   }
253
254   if (t->bus_message)
255     t->bus_message (t, bus, msg, t->bus_data);
256   g_mutex_unlock (&t->lock);
257
258   return TRUE;
259 }
260
261 static void
262 _on_negotiation_needed (GstElement * webrtc, struct test_webrtc *t)
263 {
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);
271 }
272
273 static void
274 _on_ice_candidate (GstElement * webrtc, guint mlineindex, gchar * candidate,
275     struct test_webrtc *t)
276 {
277   GstElement *other;
278
279   g_mutex_lock (&t->lock);
280   other = webrtc == t->webrtc1 ? t->webrtc2 : t->webrtc1;
281
282   if (t->on_ice_candidate)
283     t->on_ice_candidate (t, webrtc, mlineindex, candidate, other,
284         t->ice_candidate_data);
285
286   g_signal_emit_by_name (other, "add-ice-candidate", mlineindex, candidate);
287   g_mutex_unlock (&t->lock);
288 }
289
290 static void
291 _on_pad_added (GstElement * webrtc, GstPad * new_pad, struct test_webrtc *t)
292 {
293   g_mutex_lock (&t->lock);
294   if (t->on_pad_added)
295     t->on_pad_added (t, webrtc, new_pad, t->pad_added_data);
296   g_mutex_unlock (&t->lock);
297 }
298
299 static void
300 _on_data_channel (GstElement * webrtc, GObject * data_channel,
301     struct test_webrtc *t)
302 {
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);
307 }
308
309 static void
310 _pad_added_not_reached (struct test_webrtc *t, GstElement * element,
311     GstPad * pad, gpointer user_data)
312 {
313   g_assert_not_reached ();
314 }
315
316 static void
317 _ice_candidate_not_reached (struct test_webrtc *t, GstElement * element,
318     guint mlineindex, gchar * candidate, GstElement * other, gpointer user_data)
319 {
320   g_assert_not_reached ();
321 }
322
323 static void
324 _negotiation_not_reached (struct test_webrtc *t, GstElement * element,
325     gpointer user_data)
326 {
327   g_assert_not_reached ();
328 }
329
330 static void
331 _bus_no_errors (struct test_webrtc *t, GstBus * bus, GstMessage * msg,
332     gpointer user_data)
333 {
334   switch (GST_MESSAGE_TYPE (msg)) {
335     case GST_MESSAGE_ERROR:{
336       g_assert_not_reached ();
337       break;
338     }
339     default:
340       break;
341   }
342 }
343
344 static GstWebRTCSessionDescription *
345 _offer_answer_not_reached (struct test_webrtc *t, GstElement * element,
346     GstPromise * promise, gpointer user_data)
347 {
348   g_assert_not_reached ();
349 }
350
351 static void
352 _on_data_channel_not_reached (struct test_webrtc *t, GstElement * element,
353     GObject * data_channel, gpointer user_data)
354 {
355   g_assert_not_reached ();
356 }
357
358 static void
359 _broadcast (struct test_webrtc *t)
360 {
361   g_mutex_lock (&t->lock);
362   g_cond_broadcast (&t->cond);
363   g_mutex_unlock (&t->lock);
364 }
365
366 static gboolean
367 _unlock_create_thread (GMutex * lock)
368 {
369   g_mutex_unlock (lock);
370   return G_SOURCE_REMOVE;
371 }
372
373 static gpointer
374 _bus_thread (struct test_webrtc *t)
375 {
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);
380
381   g_main_loop_run (t->loop);
382
383   g_mutex_lock (&t->lock);
384   g_main_loop_unref (t->loop);
385   t->loop = NULL;
386   g_cond_broadcast (&t->cond);
387   g_mutex_unlock (&t->lock);
388
389   return NULL;
390 }
391
392 static void
393 element_added_disable_sync (GstBin * bin, GstBin * sub_bin,
394     GstElement * element, gpointer user_data)
395 {
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);
401 }
402
403 static struct test_webrtc *
404 test_webrtc_new (void)
405 {
406   struct test_webrtc *ret = g_new0 (struct test_webrtc, 1);
407
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;
415
416   g_mutex_init (&ret->lock);
417   g_cond_init (&ret->cond);
418
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);
426
427   gst_element_set_bus (ret->webrtc1, ret->bus1);
428   gst_element_set_bus (ret->webrtc2, ret->bus2);
429
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);
456
457   ret->thread = g_thread_new ("test-webrtc", (GThreadFunc) _bus_thread, ret);
458
459   g_mutex_lock (&ret->lock);
460   while (!ret->loop)
461     g_cond_wait (&ret->cond, &ret->lock);
462   g_mutex_unlock (&ret->lock);
463
464   return ret;
465 }
466
467 static void
468 test_webrtc_free (struct test_webrtc *t)
469 {
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);
474
475   g_main_loop_quit (t->loop);
476   g_mutex_lock (&t->lock);
477   while (t->loop)
478     g_cond_wait (&t->cond, &t->lock);
479   g_mutex_unlock (&t->lock);
480
481   g_thread_join (t->thread);
482
483   gst_bus_remove_watch (t->bus1);
484   gst_bus_remove_watch (t->bus2);
485
486   gst_bus_set_flushing (t->bus1, TRUE);
487   gst_bus_set_flushing (t->bus2, TRUE);
488
489   gst_object_unref (t->bus1);
490   gst_object_unref (t->bus2);
491
492   g_list_free_full (t->harnesses, (GDestroyNotify) gst_harness_teardown);
493
494   if (t->data_notify)
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);
500   if (t->offer_notify)
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);
508
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));
513
514   gst_object_unref (t->webrtc1);
515   gst_object_unref (t->webrtc2);
516
517   g_mutex_clear (&t->lock);
518   g_cond_clear (&t->cond);
519
520   g_free (t);
521 }
522
523 static void
524 test_webrtc_create_offer (struct test_webrtc *t, GstElement * webrtc)
525 {
526   GstPromise *promise;
527
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);
531 }
532
533 static void
534 test_webrtc_wait_for_state_mask (struct test_webrtc *t, TestState state)
535 {
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);
540   }
541   GST_INFO ("have test state 0x%x, current 0x%x", state, 1 << t->state);
542   g_mutex_unlock (&t->lock);
543 }
544
545 static void
546 test_webrtc_wait_for_answer_error_eos (struct test_webrtc *t)
547 {
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);
553 }
554
555 static void
556 test_webrtc_signal_state_unlocked (struct test_webrtc *t, TestState state)
557 {
558   t->state = state;
559   g_cond_broadcast (&t->cond);
560 }
561
562 static void
563 test_webrtc_signal_state (struct test_webrtc *t, TestState state)
564 {
565   g_mutex_lock (&t->lock);
566   test_webrtc_signal_state_unlocked (t, state);
567   g_mutex_unlock (&t->lock);
568 }
569
570 #if 0
571 static void
572 test_webrtc_wait_for_ice_gathering_complete (struct test_webrtc *t)
573 {
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);
583   }
584   g_mutex_unlock (&t->lock);
585 }
586
587 static void
588 test_webrtc_wait_for_ice_connection (struct test_webrtc *t,
589     GstWebRTCICEConnectionState states)
590 {
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);
601   }
602   g_mutex_unlock (&t->lock);
603 }
604 #endif
605 static void
606 _pad_added_fakesink (struct test_webrtc *t, GstElement * element,
607     GstPad * pad, gpointer user_data)
608 {
609   GstHarness *h;
610
611   if (GST_PAD_DIRECTION (pad) != GST_PAD_SRC)
612     return;
613
614   h = gst_harness_new_with_element (element, NULL, "src_%u");
615   gst_harness_add_sink_parse (h, "fakesink async=false sync=false");
616
617   t->harnesses = g_list_prepend (t->harnesses, h);
618 }
619
620 static GstWebRTCSessionDescription *
621 _count_num_sdp_media (struct test_webrtc *t, GstElement * element,
622     GstPromise * promise, gpointer user_data)
623 {
624   GstWebRTCSessionDescription *offer = NULL;
625   guint expected = GPOINTER_TO_UINT (user_data);
626   const GstStructure *reply;
627   const gchar *field;
628
629   field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
630
631   reply = gst_promise_get_reply (promise);
632   gst_structure_get (reply, field,
633       GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
634
635   fail_unless_equals_int (gst_sdp_message_medias_len (offer->sdp), expected);
636
637   return offer;
638 }
639
640 GST_START_TEST (test_sdp_no_media)
641 {
642   struct test_webrtc *t = test_webrtc_new ();
643
644   /* check that a no stream connection creates 0 media sections */
645
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;
650
651   test_webrtc_create_offer (t, t->webrtc1);
652
653   test_webrtc_wait_for_answer_error_eos (t);
654   fail_unless (t->state == STATE_ANSWER_CREATED);
655   test_webrtc_free (t);
656 }
657
658 GST_END_TEST;
659
660 static void
661 add_fake_audio_src_harness (GstHarness * h, gint pt)
662 {
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);
668 }
669
670 static void
671 add_fake_video_src_harness (GstHarness * h, gint pt)
672 {
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);
678 }
679
680 static struct test_webrtc *
681 create_audio_test (void)
682 {
683   struct test_webrtc *t = test_webrtc_new ();
684   GstHarness *h;
685
686   t->on_negotiation_needed = NULL;
687   t->on_pad_added = _pad_added_fakesink;
688
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);
692
693   return t;
694 }
695
696 GST_START_TEST (test_audio)
697 {
698   struct test_webrtc *t = create_audio_test ();
699
700   /* check that a single stream connection creates the associated number
701    * of media sections */
702
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;
708
709   test_webrtc_create_offer (t, t->webrtc1);
710
711   test_webrtc_wait_for_answer_error_eos (t);
712   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
713   test_webrtc_free (t);
714 }
715
716 GST_END_TEST;
717
718 static struct test_webrtc *
719 create_audio_video_test (void)
720 {
721   struct test_webrtc *t = test_webrtc_new ();
722   GstHarness *h;
723
724   t->on_negotiation_needed = NULL;
725   t->on_pad_added = _pad_added_fakesink;
726
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);
730
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);
734
735   return t;
736 }
737
738 GST_START_TEST (test_audio_video)
739 {
740   struct test_webrtc *t = create_audio_video_test ();
741
742   /* check that a dual stream connection creates the associated number
743    * of media sections */
744
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;
750
751   test_webrtc_create_offer (t, t->webrtc1);
752
753   test_webrtc_wait_for_answer_error_eos (t);
754   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
755   test_webrtc_free (t);
756 }
757
758 GST_END_TEST;
759
760 typedef void (*ValidateSDPFunc) (struct test_webrtc * t, GstElement * element,
761     GstWebRTCSessionDescription * desc, gpointer user_data);
762
763 struct validate_sdp
764 {
765   ValidateSDPFunc validate;
766   gpointer user_data;
767 };
768
769 static GstWebRTCSessionDescription *
770 validate_sdp (struct test_webrtc *t, GstElement * element,
771     GstPromise * promise, gpointer user_data)
772 {
773   struct validate_sdp *validate = user_data;
774   GstWebRTCSessionDescription *offer = NULL;
775   const GstStructure *reply;
776   const gchar *field;
777
778   field = t->offerror == 1 && t->webrtc1 == element ? "offer" : "answer";
779
780   reply = gst_promise_get_reply (promise);
781   gst_structure_get (reply, field,
782       GST_TYPE_WEBRTC_SESSION_DESCRIPTION, &offer, NULL);
783
784   validate->validate (t, element, offer, validate->user_data);
785
786   return offer;
787 }
788
789 static void
790 on_sdp_media_direction (struct test_webrtc *t, GstElement * element,
791     GstWebRTCSessionDescription * desc, gpointer user_data)
792 {
793   gchar **expected_directions = user_data;
794   int i;
795
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;
799     int j;
800
801     for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
802       const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
803
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);
824       }
825     }
826     fail_unless (have_direction, "no direction attribute in media %u", j);
827   }
828 }
829
830 GST_START_TEST (test_media_direction)
831 {
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 };
837   GstHarness *h;
838
839   /* check the default media directions for transceivers */
840
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);
844
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;
850
851   test_webrtc_create_offer (t, t->webrtc1);
852
853   test_webrtc_wait_for_answer_error_eos (t);
854   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
855   test_webrtc_free (t);
856 }
857
858 GST_END_TEST;
859
860 static void
861 on_sdp_media_payload_types (struct test_webrtc *t, GstElement * element,
862     GstWebRTCSessionDescription * desc, gpointer user_data)
863 {
864   const GstSDPMedia *vmedia;
865   guint j;
866
867   fail_unless_equals_int (gst_sdp_message_medias_len (desc->sdp), 2);
868
869   vmedia = gst_sdp_message_get_media (desc->sdp, 1);
870
871   for (j = 0; j < gst_sdp_media_attributes_len (vmedia); j++) {
872     const GstSDPAttribute *attr = gst_sdp_media_get_attribute (vmedia, j);
873
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");
885       }
886     }
887   }
888 }
889
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)
893 {
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;
898
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;
904
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,
909       NULL);
910   g_array_unref (transceivers);
911
912   test_webrtc_create_offer (t, t->webrtc1);
913
914   test_webrtc_wait_for_answer_error_eos (t);
915   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
916   test_webrtc_free (t);
917 }
918
919 GST_END_TEST;
920
921 static void
922 on_sdp_media_setup (struct test_webrtc *t, GstElement * element,
923     GstWebRTCSessionDescription * desc, gpointer user_data)
924 {
925   gchar **expected_setup = user_data;
926   int i;
927
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;
931     int j;
932
933     for (j = 0; j < gst_sdp_media_attributes_len (media); j++) {
934       const GstSDPAttribute *attr = gst_sdp_media_get_attribute (media, j);
935
936       if (g_strcmp0 (attr->key, "setup") == 0) {
937         fail_unless (have_setup == FALSE,
938             "duplicate/multiple setup for media %u", j);
939         have_setup = TRUE;
940         fail_unless (g_strcmp0 (attr->value, expected_setup[i]) == 0);
941       }
942     }
943     fail_unless (have_setup, "no setup attribute in media %u", j);
944   }
945 }
946
947 GST_START_TEST (test_media_setup)
948 {
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 };
954
955   /* check the default dtls setup negotiation values */
956
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;
962
963   test_webrtc_create_offer (t, t->webrtc1);
964
965   test_webrtc_wait_for_answer_error_eos (t);
966   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
967   test_webrtc_free (t);
968 }
969
970 GST_END_TEST;
971
972 GST_START_TEST (test_no_nice_elements_request_pad)
973 {
974   struct test_webrtc *t = test_webrtc_new ();
975   GstPluginFeature *nicesrc, *nicesink;
976   GstRegistry *registry;
977   GstPad *pad;
978
979   /* check that the absence of libnice elements posts an error on the bus
980    * when requesting a pad */
981
982   registry = gst_registry_get ();
983   nicesrc = gst_registry_lookup_feature (registry, "nicesrc");
984   nicesink = gst_registry_lookup_feature (registry, "nicesink");
985
986   if (nicesrc)
987     gst_registry_remove_feature (registry, nicesrc);
988   if (nicesink)
989     gst_registry_remove_feature (registry, nicesink);
990
991   t->bus_message = NULL;
992
993   pad = gst_element_get_request_pad (t->webrtc1, "sink_0");
994   fail_unless (pad == NULL);
995
996   test_webrtc_wait_for_answer_error_eos (t);
997   fail_unless_equals_int (STATE_ERROR, t->state);
998   test_webrtc_free (t);
999
1000   if (nicesrc)
1001     gst_registry_add_feature (registry, nicesrc);
1002   if (nicesink)
1003     gst_registry_add_feature (registry, nicesink);
1004 }
1005
1006 GST_END_TEST;
1007
1008 GST_START_TEST (test_no_nice_elements_state_change)
1009 {
1010   struct test_webrtc *t = test_webrtc_new ();
1011   GstPluginFeature *nicesrc, *nicesink;
1012   GstRegistry *registry;
1013
1014   /* check that the absence of libnice elements posts an error on the bus */
1015
1016   registry = gst_registry_get ();
1017   nicesrc = gst_registry_lookup_feature (registry, "nicesrc");
1018   nicesink = gst_registry_lookup_feature (registry, "nicesink");
1019
1020   if (nicesrc)
1021     gst_registry_remove_feature (registry, nicesrc);
1022   if (nicesink)
1023     gst_registry_remove_feature (registry, nicesink);
1024
1025   t->bus_message = NULL;
1026   gst_element_set_state (t->webrtc1, GST_STATE_READY);
1027
1028   test_webrtc_wait_for_answer_error_eos (t);
1029   fail_unless_equals_int (STATE_ERROR, t->state);
1030   test_webrtc_free (t);
1031
1032   if (nicesrc)
1033     gst_registry_add_feature (registry, nicesrc);
1034   if (nicesink)
1035     gst_registry_add_feature (registry, nicesink);
1036 }
1037
1038 GST_END_TEST;
1039
1040 static void
1041 validate_rtc_stats (const GstStructure * s)
1042 {
1043   GstWebRTCStatsType type = 0;
1044   double ts = 0.;
1045   gchar *id = NULL;
1046
1047   fail_unless (gst_structure_get (s, "type", GST_TYPE_WEBRTC_STATS_TYPE, &type,
1048           NULL));
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);
1054
1055   g_free (id);
1056 }
1057
1058 static void
1059 validate_codec_stats (const GstStructure * s)
1060 {
1061   guint pt = 0, clock_rate = 0;
1062
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,
1065           NULL));
1066   fail_unless (pt >= 0 && pt <= 127);
1067   fail_unless (clock_rate >= 0);
1068 }
1069
1070 static void
1071 validate_rtc_stream_stats (const GstStructure * s, const GstStructure * stats)
1072 {
1073   gchar *codec_id, *transport_id;
1074   GstStructure *codec, *transport;
1075
1076   fail_unless (gst_structure_get (s, "codec-id", G_TYPE_STRING, &codec_id,
1077           NULL));
1078   fail_unless (gst_structure_get (s, "transport-id", G_TYPE_STRING,
1079           &transport_id, NULL));
1080
1081   fail_unless (gst_structure_get (stats, codec_id, GST_TYPE_STRUCTURE, &codec,
1082           NULL));
1083   fail_unless (gst_structure_get (stats, transport_id, GST_TYPE_STRUCTURE,
1084           &transport, NULL));
1085
1086   fail_unless (codec != NULL);
1087   fail_unless (transport != NULL);
1088
1089   gst_structure_free (transport);
1090   gst_structure_free (codec);
1091
1092   g_free (codec_id);
1093   g_free (transport_id);
1094 }
1095
1096 static void
1097 validate_inbound_rtp_stats (const GstStructure * s, const GstStructure * stats)
1098 {
1099   guint ssrc, fir, pli, nack;
1100   gint packets_lost;
1101   guint64 packets_received, bytes_received;
1102   double jitter;
1103   gchar *remote_id;
1104   GstStructure *remote;
1105
1106   validate_rtc_stream_stats (s, stats);
1107
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,
1118           NULL));
1119   fail_unless (gst_structure_get (s, "remote-id", G_TYPE_STRING, &remote_id,
1120           NULL));
1121   fail_unless (gst_structure_get (stats, remote_id, GST_TYPE_STRUCTURE, &remote,
1122           NULL));
1123   fail_unless (remote != NULL);
1124
1125   gst_structure_free (remote);
1126   g_free (remote_id);
1127 }
1128
1129 static void
1130 validate_remote_inbound_rtp_stats (const GstStructure * s,
1131     const GstStructure * stats)
1132 {
1133   guint ssrc;
1134   gint packets_lost;
1135   double jitter, rtt;
1136   gchar *local_id;
1137   GstStructure *local;
1138
1139   validate_rtc_stream_stats (s, stats);
1140
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,
1144           NULL));
1145   fail_unless (gst_structure_get (s, "round-trip-time", G_TYPE_DOUBLE, &rtt,
1146           NULL));
1147   fail_unless (gst_structure_get (s, "local-id", G_TYPE_STRING, &local_id,
1148           NULL));
1149   fail_unless (gst_structure_get (stats, local_id, GST_TYPE_STRUCTURE, &local,
1150           NULL));
1151   fail_unless (local != NULL);
1152
1153   gst_structure_free (local);
1154   g_free (local_id);
1155 }
1156
1157 static void
1158 validate_outbound_rtp_stats (const GstStructure * s, const GstStructure * stats)
1159 {
1160   guint ssrc, fir, pli, nack;
1161   guint64 packets_sent, bytes_sent;
1162   gchar *remote_id;
1163   GstStructure *remote;
1164
1165   validate_rtc_stream_stats (s, stats);
1166
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,
1174           NULL));
1175   fail_unless (gst_structure_get (s, "remote-id", G_TYPE_STRING, &remote_id,
1176           NULL));
1177   fail_unless (gst_structure_get (stats, remote_id, GST_TYPE_STRUCTURE, &remote,
1178           NULL));
1179   fail_unless (remote != NULL);
1180
1181   gst_structure_free (remote);
1182   g_free (remote_id);
1183 }
1184
1185 static void
1186 validate_remote_outbound_rtp_stats (const GstStructure * s,
1187     const GstStructure * stats)
1188 {
1189   guint ssrc;
1190   gchar *local_id;
1191   GstStructure *local;
1192
1193   validate_rtc_stream_stats (s, stats);
1194
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,
1197           NULL));
1198   fail_unless (gst_structure_get (stats, local_id, GST_TYPE_STRUCTURE, &local,
1199           NULL));
1200   fail_unless (local != NULL);
1201
1202   gst_structure_free (local);
1203   g_free (local_id);
1204 }
1205
1206 static gboolean
1207 validate_stats_foreach (GQuark field_id, const GValue * value,
1208     const GstStructure * stats)
1209 {
1210   const gchar *field = g_quark_to_string (field_id);
1211   GstWebRTCStatsType type;
1212   const GstStructure *s;
1213
1214   fail_unless (GST_VALUE_HOLDS_STRUCTURE (value));
1215
1216   s = gst_value_get_structure (value);
1217
1218   GST_INFO ("validating field %s %" GST_PTR_FORMAT, field, s);
1219
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) {
1241   } else {
1242     g_assert_not_reached ();
1243   }
1244
1245   return TRUE;
1246 }
1247
1248 static void
1249 validate_stats (const GstStructure * stats)
1250 {
1251   gst_structure_foreach (stats,
1252       (GstStructureForeachFunc) validate_stats_foreach, (gpointer) stats);
1253 }
1254
1255 static void
1256 _on_stats (GstPromise * promise, gpointer user_data)
1257 {
1258   struct test_webrtc *t = user_data;
1259   const GstStructure *reply = gst_promise_get_reply (promise);
1260   int i;
1261
1262   validate_stats (reply);
1263   i = GPOINTER_TO_INT (t->user_data);
1264   i++;
1265   t->user_data = GINT_TO_POINTER (i);
1266   if (i >= 2)
1267     test_webrtc_signal_state (t, STATE_CUSTOM);
1268
1269   gst_promise_unref (promise);
1270 }
1271
1272 GST_START_TEST (test_session_stats)
1273 {
1274   struct test_webrtc *t = test_webrtc_new ();
1275   GstPromise *p;
1276
1277   /* test that the stats generated without any streams are sane */
1278
1279   t->on_offer_created = NULL;
1280   t->on_answer_created = NULL;
1281
1282   test_webrtc_create_offer (t, t->webrtc1);
1283
1284   test_webrtc_wait_for_answer_error_eos (t);
1285   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1286
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);
1291
1292   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1293
1294   test_webrtc_free (t);
1295 }
1296
1297 GST_END_TEST;
1298
1299 GST_START_TEST (test_add_transceiver)
1300 {
1301   struct test_webrtc *t = test_webrtc_new ();
1302   GstWebRTCRTPTransceiverDirection direction;
1303   GstWebRTCRTPTransceiver *trans;
1304
1305   direction = GST_WEBRTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV;
1306   g_signal_emit_by_name (t->webrtc1, "add-transceiver", direction, NULL,
1307       &trans);
1308   fail_unless (trans != NULL);
1309   fail_unless_equals_int (direction, trans->direction);
1310
1311   gst_object_unref (trans);
1312
1313   test_webrtc_free (t);
1314 }
1315
1316 GST_END_TEST;
1317
1318 GST_START_TEST (test_get_transceivers)
1319 {
1320   struct test_webrtc *t = create_audio_test ();
1321   GstWebRTCRTPTransceiver *trans;
1322   GArray *transceivers;
1323
1324   g_signal_emit_by_name (t->webrtc1, "get-transceivers", &transceivers);
1325   fail_unless (transceivers != NULL);
1326   fail_unless_equals_int (1, transceivers->len);
1327
1328   trans = g_array_index (transceivers, GstWebRTCRTPTransceiver *, 0);
1329   fail_unless (trans != NULL);
1330
1331   g_array_unref (transceivers);
1332
1333   test_webrtc_free (t);
1334 }
1335
1336 GST_END_TEST;
1337
1338 GST_START_TEST (test_add_recvonly_transceiver)
1339 {
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 };
1347   GstCaps *caps;
1348   GstHarness *h;
1349
1350   /* add a transceiver that will only receive an opus stream and check that
1351    * the created offer is marked as recvonly */
1352
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;
1360
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,
1365       &trans);
1366   gst_caps_unref (caps);
1367   fail_unless (trans != NULL);
1368   gst_object_unref (trans);
1369
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);
1374
1375   test_webrtc_create_offer (t, t->webrtc1);
1376
1377   test_webrtc_wait_for_answer_error_eos (t);
1378   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1379   test_webrtc_free (t);
1380 }
1381
1382 GST_END_TEST;
1383
1384 GST_START_TEST (test_recvonly_sendonly)
1385 {
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 };
1393   GstCaps *caps;
1394   GstHarness *h;
1395   GArray *transceivers;
1396
1397   /* add a transceiver that will only receive an opus stream and check that
1398    * the created offer is marked as recvonly */
1399
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;
1407
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,
1412       &trans);
1413   gst_caps_unref (caps);
1414   fail_unless (trans != NULL);
1415   gst_object_unref (trans);
1416
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;
1425
1426   g_array_unref (transceivers);
1427
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);
1432
1433   test_webrtc_create_offer (t, t->webrtc1);
1434
1435   test_webrtc_wait_for_answer_error_eos (t);
1436   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1437   test_webrtc_free (t);
1438 }
1439
1440 GST_END_TEST;
1441
1442 static gboolean
1443 _message_media_is_datachannel (const GstSDPMessage * msg, guint media_id)
1444 {
1445   const GstSDPMedia *media;
1446
1447   if (!msg)
1448     return FALSE;
1449
1450   if (gst_sdp_message_medias_len (msg) <= media_id)
1451     return FALSE;
1452
1453   media = gst_sdp_message_get_media (msg, media_id);
1454
1455   if (g_strcmp0 (gst_sdp_media_get_media (media), "application") != 0)
1456     return FALSE;
1457
1458   if (gst_sdp_media_formats_len (media) != 1)
1459     return FALSE;
1460
1461   if (g_strcmp0 (gst_sdp_media_get_format (media, 0),
1462           "webrtc-datachannel") != 0)
1463     return FALSE;
1464
1465   return TRUE;
1466 }
1467
1468 static void
1469 on_sdp_has_datachannel (struct test_webrtc *t, GstElement * element,
1470     GstWebRTCSessionDescription * desc, gpointer user_data)
1471 {
1472   gboolean have_data_channel = FALSE;
1473   int i;
1474
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;
1480     }
1481   }
1482
1483   fail_unless_equals_int (TRUE, have_data_channel);
1484 }
1485
1486 static void
1487 on_channel_error_not_reached (GObject * channel, GError * error,
1488     gpointer user_data)
1489 {
1490   g_assert_not_reached ();
1491 }
1492
1493 GST_START_TEST (test_data_channel_create)
1494 {
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 };
1499   gchar *label;
1500
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;
1507
1508   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1509       &channel);
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);
1515
1516   test_webrtc_create_offer (t, t->webrtc1);
1517
1518   test_webrtc_wait_for_answer_error_eos (t);
1519   fail_unless_equals_int (STATE_ANSWER_CREATED, t->state);
1520   g_object_unref (channel);
1521   g_free (label);
1522   test_webrtc_free (t);
1523 }
1524
1525 GST_END_TEST;
1526
1527 static void
1528 have_data_channel (struct test_webrtc *t, GstElement * element,
1529     GObject * our, gpointer user_data)
1530 {
1531   GObject *other = user_data;
1532   gchar *our_label, *other_label;
1533
1534   g_signal_connect (our, "on-error", G_CALLBACK (on_channel_error_not_reached),
1535       NULL);
1536
1537   g_object_get (our, "label", &our_label, NULL);
1538   g_object_get (other, "label", &other_label, NULL);
1539
1540   g_assert_cmpstr (our_label, ==, other_label);
1541
1542   g_free (our_label);
1543   g_free (other_label);
1544
1545   test_webrtc_signal_state_unlocked (t, STATE_CUSTOM);
1546 }
1547
1548 GST_START_TEST (test_data_channel_remote_notify)
1549 {
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 };
1554
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;
1562
1563   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1564       &channel);
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);
1569
1570   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1571   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1572
1573   test_webrtc_create_offer (t, t->webrtc1);
1574
1575   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1576
1577   g_object_unref (channel);
1578   test_webrtc_free (t);
1579 }
1580
1581 GST_END_TEST;
1582
1583 static const gchar *test_string = "GStreamer WebRTC is awesome!";
1584
1585 static void
1586 on_message_string (GObject * channel, const gchar * str, struct test_webrtc *t)
1587 {
1588   gchar *expected = g_object_steal_data (channel, "expected");
1589   g_assert_cmpstr (expected, ==, str);
1590   g_free (expected);
1591
1592   test_webrtc_signal_state (t, STATE_CUSTOM);
1593 }
1594
1595 static void
1596 have_data_channel_transfer_string (struct test_webrtc *t, GstElement * element,
1597     GObject * our, gpointer user_data)
1598 {
1599   GObject *other = user_data;
1600   GstWebRTCDataChannelState state;
1601
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);
1606
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),
1609       t);
1610
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);
1614 }
1615
1616 GST_START_TEST (test_data_channel_transfer_string)
1617 {
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 };
1622
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;
1630
1631   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1632       &channel);
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);
1637
1638   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1639   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1640
1641   test_webrtc_create_offer (t, t->webrtc1);
1642
1643   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1644
1645   g_object_unref (channel);
1646   test_webrtc_free (t);
1647 }
1648
1649 GST_END_TEST;
1650
1651 #define g_assert_cmpbytes(b1, b2)                       \
1652     G_STMT_START {                                      \
1653       gsize l1, l2;                                     \
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);                 \
1657     } G_STMT_END;
1658
1659 static void
1660 on_message_data (GObject * channel, GBytes * data, struct test_webrtc *t)
1661 {
1662   GBytes *expected = g_object_steal_data (channel, "expected");
1663   g_assert_cmpbytes (data, expected);
1664   g_bytes_unref (expected);
1665
1666   test_webrtc_signal_state (t, STATE_CUSTOM);
1667 }
1668
1669 static void
1670 have_data_channel_transfer_data (struct test_webrtc *t, GstElement * element,
1671     GObject * our, gpointer user_data)
1672 {
1673   GObject *other = user_data;
1674   GBytes *data = g_bytes_new_static (test_string, strlen (test_string));
1675   GstWebRTCDataChannelState state;
1676
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);
1681
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);
1685
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);
1689 }
1690
1691 GST_START_TEST (test_data_channel_transfer_data)
1692 {
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 };
1697
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;
1705
1706   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1707       &channel);
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);
1712
1713   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1714   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1715
1716   test_webrtc_create_offer (t, t->webrtc1);
1717
1718   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1719
1720   g_object_unref (channel);
1721   test_webrtc_free (t);
1722 }
1723
1724 GST_END_TEST;
1725
1726 static void
1727 have_data_channel_create_data_channel (struct test_webrtc *t,
1728     GstElement * element, GObject * our, gpointer user_data)
1729 {
1730   GObject *another;
1731
1732   t->on_data_channel = have_data_channel_transfer_string;
1733
1734   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1735       &another);
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);
1740 }
1741
1742 GST_START_TEST (test_data_channel_create_after_negotiate)
1743 {
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 };
1748
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;
1756
1757   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "prev-label", NULL,
1758       &channel);
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);
1763
1764   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1765   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1766
1767   test_webrtc_create_offer (t, t->webrtc1);
1768
1769   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1770
1771   g_object_unref (channel);
1772   test_webrtc_free (t);
1773 }
1774
1775 GST_END_TEST;
1776
1777 static void
1778 on_buffered_amount_low_emitted (GObject * channel, struct test_webrtc *t)
1779 {
1780   test_webrtc_signal_state (t, STATE_CUSTOM);
1781 }
1782
1783 static void
1784 have_data_channel_check_low_threshold_emitted (struct test_webrtc *t,
1785     GstElement * element, GObject * our, gpointer user_data)
1786 {
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);
1790
1791   g_signal_connect (our, "on-error", G_CALLBACK (on_channel_error_not_reached),
1792       NULL);
1793   g_signal_emit_by_name (our, "send-string", "DATA");
1794 }
1795
1796 GST_START_TEST (test_data_channel_low_threshold)
1797 {
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 };
1802
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;
1810
1811   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1812       &channel);
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);
1817
1818   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1819   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1820
1821   test_webrtc_create_offer (t, t->webrtc1);
1822
1823   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1824
1825   g_object_unref (channel);
1826   test_webrtc_free (t);
1827 }
1828
1829 GST_END_TEST;
1830
1831 static void
1832 on_channel_error (GObject * channel, GError * error, struct test_webrtc *t)
1833 {
1834   g_assert_nonnull (error);
1835
1836   test_webrtc_signal_state (t, STATE_CUSTOM);
1837 }
1838
1839 static void
1840 have_data_channel_transfer_large_data (struct test_webrtc *t,
1841     GstElement * element, GObject * our, gpointer user_data)
1842 {
1843   GObject *other = user_data;
1844   const gsize size = 1024 * 1024;
1845   guint8 *random_data = g_new (guint8, size);
1846   GBytes *data;
1847   gsize i;
1848
1849   for (i = 0; i < size; i++)
1850     random_data[i] = (guint8) (i & 0xff);
1851
1852   data = g_bytes_new_static (random_data, size);
1853
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);
1857
1858   g_signal_connect (other, "on-error", G_CALLBACK (on_channel_error), t);
1859   g_signal_emit_by_name (other, "send-data", data);
1860 }
1861
1862 GST_START_TEST (test_data_channel_max_message_size)
1863 {
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 };
1868
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;
1876
1877   g_signal_emit_by_name (t->webrtc1, "create-data-channel", "label", NULL,
1878       &channel);
1879   g_assert_nonnull (channel);
1880   t->data_channel_data = channel;
1881
1882   gst_element_set_state (t->webrtc1, GST_STATE_PLAYING);
1883   gst_element_set_state (t->webrtc2, GST_STATE_PLAYING);
1884
1885   test_webrtc_create_offer (t, t->webrtc1);
1886
1887   test_webrtc_wait_for_state_mask (t, 1 << STATE_CUSTOM);
1888
1889   g_object_unref (channel);
1890   test_webrtc_free (t);
1891 }
1892
1893 GST_END_TEST;
1894
1895 static Suite *
1896 webrtcbin_suite (void)
1897 {
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;
1903
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");
1911
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);
1934     } else {
1935       GST_WARNING ("Some required elements were not found. "
1936           "All datachannel are disabled. sctpenc %p, sctpdec %p", sctpenc,
1937           sctpdec);
1938     }
1939   } else {
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,
1943         dtlssrtpdec);
1944   }
1945
1946   if (nicesrc)
1947     gst_object_unref (nicesrc);
1948   if (nicesink)
1949     gst_object_unref (nicesink);
1950   if (dtlssrtpdec)
1951     gst_object_unref (dtlssrtpdec);
1952   if (dtlssrtpenc)
1953     gst_object_unref (dtlssrtpenc);
1954   if (sctpenc)
1955     gst_object_unref (sctpenc);
1956   if (sctpdec)
1957     gst_object_unref (sctpdec);
1958
1959   suite_add_tcase (s, tc);
1960
1961   return s;
1962 }
1963
1964 GST_CHECK_MAIN (webrtcbin);