caps: improve _do_simplify
[platform/upstream/gstreamer.git] / tests / check / gst / gstevent.c
1 /* GStreamer
2  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3  *
4  * gstevent.c: Unit test for event handling
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include <gst/check/gstcheck.h>
24
25 GST_START_TEST (create_events)
26 {
27   GstEvent *event, *event2;
28   GstStructure *structure;
29
30   /* FLUSH_START */
31   {
32     event = gst_event_new_flush_start ();
33     fail_if (event == NULL);
34     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_START);
35     fail_unless (GST_EVENT_IS_UPSTREAM (event));
36     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
37     fail_if (GST_EVENT_IS_SERIALIZED (event));
38     gst_event_unref (event);
39   }
40   /* FLUSH_STOP */
41   {
42     gboolean reset_time;
43
44     event = gst_event_new_flush_stop (TRUE);
45     fail_if (event == NULL);
46     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP);
47     fail_unless (GST_EVENT_IS_UPSTREAM (event));
48     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
49     fail_unless (GST_EVENT_IS_SERIALIZED (event));
50
51     gst_event_parse_flush_stop (event, &reset_time);
52     fail_unless (reset_time == TRUE);
53     gst_event_unref (event);
54   }
55   /* EOS */
56   {
57     event = gst_event_new_eos ();
58     fail_if (event == NULL);
59     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_EOS);
60     fail_if (GST_EVENT_IS_UPSTREAM (event));
61     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
62     fail_unless (GST_EVENT_IS_SERIALIZED (event));
63     gst_event_unref (event);
64   }
65   /* GAP */
66   {
67     GstClockTime ts = 0, dur = 0;
68
69     ASSERT_CRITICAL (gst_event_new_gap (GST_CLOCK_TIME_NONE, GST_SECOND));
70
71     event = gst_event_new_gap (90 * GST_SECOND, GST_SECOND);
72     fail_if (event == NULL);
73     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
74     fail_if (GST_EVENT_IS_UPSTREAM (event));
75     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
76     fail_unless (GST_EVENT_IS_SERIALIZED (event));
77     gst_event_parse_gap (event, &ts, NULL);
78     fail_unless_equals_int64 (ts, 90 * GST_SECOND);
79     gst_event_parse_gap (event, &ts, &dur);
80     fail_unless_equals_int64 (dur, GST_SECOND);
81     gst_event_unref (event);
82   }
83   /* SEGMENT */
84   {
85     GstSegment segment, parsed;
86
87     gst_segment_init (&segment, GST_FORMAT_TIME);
88     segment.rate = 0.5;
89     segment.applied_rate = 1.0;
90     segment.start = 1;
91     segment.stop = G_MAXINT64;
92     segment.time = 0xdeadbeef;
93
94     event = gst_event_new_segment (&segment);
95     fail_if (event == NULL);
96     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
97     fail_if (GST_EVENT_IS_UPSTREAM (event));
98     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
99     fail_unless (GST_EVENT_IS_SERIALIZED (event));
100
101     gst_event_copy_segment (event, &parsed);
102     fail_unless (parsed.rate == 0.5);
103     fail_unless (parsed.applied_rate == 1.0);
104     fail_unless (parsed.format == GST_FORMAT_TIME);
105     fail_unless (parsed.start == 1);
106     fail_unless (parsed.stop == G_MAXINT64);
107     fail_unless (parsed.time == 0xdeadbeef);
108
109     gst_event_unref (event);
110   }
111
112   /* STREAM CONFIG */
113   {
114     GstStreamConfigFlags flags = 0x987654;
115     GstBuffer *buf, *cd, *sh1, *sh2;
116     gpointer dummy;
117
118     event = gst_event_new_stream_config (GST_STREAM_CONFIG_FLAG_NONE);
119
120     gst_event_parse_stream_config (event, &flags);
121     fail_unless_equals_int (flags, GST_STREAM_CONFIG_FLAG_NONE);
122
123     fail_unless_equals_int (gst_event_get_n_stream_config_headers (event), 0);
124
125     /* set buf to something random but guaranteed to be non-NULL */
126     buf = (GstBuffer *) & dummy;
127     gst_event_parse_stream_config_setup_data (event, &buf);
128     fail_unless (buf == NULL);
129
130     buf = (GstBuffer *) & dummy;
131     gst_event_parse_nth_stream_config_header (event, 0, &buf);
132     fail_unless (buf == NULL);
133
134     buf = (GstBuffer *) & dummy;
135     gst_event_parse_nth_stream_config_header (event, 98416, &buf);
136     fail_unless (buf == NULL);
137
138     ASSERT_CRITICAL (gst_event_set_stream_config_setup_data (event, NULL));
139     ASSERT_CRITICAL (gst_event_add_stream_config_header (event, NULL));
140
141     cd = gst_buffer_new_wrapped_full ((gpointer) "SetMeUpScottie", NULL, 0, 14);
142     gst_event_set_stream_config_setup_data (event, cd);
143     gst_buffer_unref (cd);
144
145     buf = (GstBuffer *) & dummy;
146     gst_event_parse_nth_stream_config_header (event, 0, &buf);
147     fail_unless (buf == NULL);
148     gst_event_parse_stream_config_setup_data (event, &buf);
149     fail_unless (buf == cd);
150     fail_unless (GST_IS_BUFFER (buf));
151
152     gst_event_unref (event);
153
154     event = gst_event_new_stream_config (GST_STREAM_CONFIG_FLAG_NONE);
155     fail_unless_equals_int (gst_event_get_n_stream_config_headers (event), 0);
156     sh1 = gst_buffer_new_wrapped_full ((gpointer) "Strea", NULL, 0, 5);
157     gst_event_add_stream_config_header (event, sh1);
158     gst_buffer_unref (sh1);
159     fail_unless_equals_int (gst_event_get_n_stream_config_headers (event), 1);
160     sh2 = gst_buffer_new_wrapped_full ((gpointer) "mHeader", NULL, 0, 7);
161     gst_event_add_stream_config_header (event, sh2);
162     gst_buffer_unref (sh2);
163     fail_unless_equals_int (gst_event_get_n_stream_config_headers (event), 2);
164
165     buf = (GstBuffer *) & dummy;
166     gst_event_parse_nth_stream_config_header (event, 1, &buf);
167     fail_unless (buf == sh2);
168     fail_unless (GST_IS_BUFFER (buf));
169
170     buf = (GstBuffer *) & dummy;
171     gst_event_parse_nth_stream_config_header (event, 0, &buf);
172     fail_unless (buf == sh1);
173     fail_unless (GST_IS_BUFFER (buf));
174
175     gst_event_unref (event);
176   }
177
178   /* TAGS */
179   {
180     GstTagList *taglist = gst_tag_list_new_empty ();
181     GstTagList *tl2 = NULL;
182
183     event = gst_event_new_tag (taglist);
184     fail_if (taglist == NULL);
185     fail_if (event == NULL);
186     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_TAG);
187     fail_if (GST_EVENT_IS_UPSTREAM (event));
188     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
189     fail_unless (GST_EVENT_IS_SERIALIZED (event));
190
191     gst_event_parse_tag (event, &tl2);
192     fail_unless (taglist == tl2);
193     gst_event_unref (event);
194   }
195
196   /* QOS */
197   {
198     GstQOSType t1 = GST_QOS_TYPE_THROTTLE, t2;
199     gdouble p1 = 1.0, p2;
200     GstClockTimeDiff ctd1 = G_GINT64_CONSTANT (10), ctd2;
201     GstClockTime ct1 = G_GUINT64_CONSTANT (20), ct2;
202
203     event = gst_event_new_qos (t1, p1, ctd1, ct1);
204     fail_if (event == NULL);
205     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
206     fail_unless (GST_EVENT_IS_UPSTREAM (event));
207     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
208     fail_if (GST_EVENT_IS_SERIALIZED (event));
209
210     gst_event_parse_qos (event, &t2, &p2, &ctd2, &ct2);
211     fail_unless (p1 == p2);
212     fail_unless (ctd1 == ctd2);
213     fail_unless (ct1 == ct2);
214     gst_event_parse_qos (event, &t2, &p2, &ctd2, &ct2);
215     fail_unless (t2 == GST_QOS_TYPE_THROTTLE);
216     fail_unless (p1 == p2);
217     fail_unless (ctd1 == ctd2);
218     fail_unless (ct1 == ct2);
219     gst_event_unref (event);
220
221     ctd1 = G_GINT64_CONSTANT (-10);
222     event = gst_event_new_qos (t1, p1, ctd1, ct1);
223     gst_event_parse_qos (event, &t2, &p2, &ctd2, &ct2);
224     fail_unless (t2 == GST_QOS_TYPE_THROTTLE);
225     gst_event_unref (event);
226
227     event = gst_event_new_qos (t1, p1, ctd1, ct1);
228     gst_event_parse_qos (event, &t2, &p2, &ctd2, &ct2);
229     fail_unless (t2 == GST_QOS_TYPE_THROTTLE);
230     fail_unless (p1 == p2);
231     fail_unless (ctd1 == ctd2);
232     fail_unless (ct1 == ct2);
233     gst_event_unref (event);
234   }
235
236   /* SEEK */
237   {
238     gdouble rate;
239     GstFormat format;
240     GstSeekFlags flags;
241     GstSeekType cur_type, stop_type;
242     gint64 cur, stop;
243
244     event = gst_event_new_seek (0.5, GST_FORMAT_BYTES,
245         GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
246         GST_SEEK_TYPE_SET, 1, GST_SEEK_TYPE_NONE, 0xdeadbeef);
247
248     fail_if (event == NULL);
249     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
250     fail_unless (GST_EVENT_IS_UPSTREAM (event));
251     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
252     fail_if (GST_EVENT_IS_SERIALIZED (event));
253
254     gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
255         &stop_type, &stop);
256     fail_unless (rate == 0.5);
257     fail_unless (format == GST_FORMAT_BYTES);
258     fail_unless (flags == (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE));
259     fail_unless (cur_type == GST_SEEK_TYPE_SET);
260     fail_unless (cur == 1);
261     fail_unless (stop_type == GST_SEEK_TYPE_NONE);
262     fail_unless (stop == 0xdeadbeef);
263
264     gst_event_unref (event);
265   }
266
267   /* NAVIGATION */
268   {
269     structure = gst_structure_new ("application/x-gst-navigation", "event",
270         G_TYPE_STRING, "key-press", "key", G_TYPE_STRING, "mon", NULL);
271     fail_if (structure == NULL);
272     event = gst_event_new_navigation (structure);
273     fail_if (event == NULL);
274     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION);
275     fail_unless (GST_EVENT_IS_UPSTREAM (event));
276     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
277     fail_if (GST_EVENT_IS_SERIALIZED (event));
278
279     fail_unless (gst_event_get_structure (event) == structure);
280     gst_event_unref (event);
281   }
282
283   /* Custom event types */
284   {
285     structure = gst_structure_new_empty ("application/x-custom");
286     fail_if (structure == NULL);
287     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
288     fail_if (event == NULL);
289     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM);
290     fail_unless (GST_EVENT_IS_UPSTREAM (event));
291     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
292     fail_if (GST_EVENT_IS_SERIALIZED (event));
293     fail_unless (gst_event_get_structure (event) == structure);
294     fail_unless (gst_event_has_name (event, "application/x-custom"));
295     gst_event_unref (event);
296
297     /* Decided not to test the other custom enum types, as they
298      * only differ by the value of the enum passed to gst_event_new_custom
299      */
300   }
301
302   /* Event copying */
303   {
304     structure = gst_structure_new_empty ("application/x-custom");
305     fail_if (structure == NULL);
306     event = gst_event_new_custom (GST_EVENT_CUSTOM_BOTH, structure);
307
308     fail_if (event == NULL);
309     event2 = gst_event_copy (event);
310     fail_if (event2 == NULL);
311     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_TYPE (event2));
312     fail_unless (gst_event_has_name (event, "application/x-custom"));
313
314     /* The structure should have been duplicated */
315     fail_if (gst_event_get_structure (event) ==
316         gst_event_get_structure (event2));
317
318     gst_event_unref (event);
319     gst_event_unref (event2);
320   }
321
322   /* Make events writable */
323   {
324     structure = gst_structure_new_empty ("application/x-custom");
325     fail_if (structure == NULL);
326     event = gst_event_new_custom (GST_EVENT_CUSTOM_BOTH, structure);
327     /* ref the event so that it becomes non-writable */
328     gst_event_ref (event);
329     gst_event_ref (event);
330     /* this should fail if the structure isn't writable */
331     ASSERT_CRITICAL (gst_structure_remove_all_fields ((GstStructure *)
332             gst_event_get_structure (event)));
333     fail_unless (gst_event_has_name (event, "application/x-custom"));
334
335     /* now make writable */
336     event2 =
337         GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
338     fail_unless (event != event2);
339     /* this fail if the structure isn't writable */
340     gst_structure_remove_all_fields ((GstStructure *)
341         gst_event_get_structure (event2));
342     fail_unless (gst_event_has_name (event2, "application/x-custom"));
343
344     gst_event_unref (event);
345     gst_event_unref (event);
346     gst_event_unref (event2);
347   }
348 }
349
350 GST_END_TEST;
351
352 static GTimeVal sent_event_time;
353 static GstEvent *got_event_before_q, *got_event_after_q;
354 static GTimeVal got_event_time;
355
356 static GstPadProbeReturn
357 event_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
358 {
359   GstMiniObject *data = GST_PAD_PROBE_INFO_DATA (info);
360   gboolean before_q = (gboolean) GPOINTER_TO_INT (user_data);
361
362   GST_DEBUG ("event probe called %p", data);
363
364   fail_unless (GST_IS_EVENT (data));
365
366   if (before_q) {
367     switch (GST_EVENT_TYPE (GST_EVENT (data))) {
368       case GST_EVENT_CUSTOM_UPSTREAM:
369       case GST_EVENT_CUSTOM_BOTH:
370       case GST_EVENT_CUSTOM_BOTH_OOB:
371         if (got_event_before_q != NULL)
372           break;
373         gst_event_ref ((GstEvent *) data);
374         g_get_current_time (&got_event_time);
375         got_event_before_q = GST_EVENT (data);
376         break;
377       default:
378         break;
379     }
380   } else {
381     switch (GST_EVENT_TYPE (GST_EVENT (data))) {
382       case GST_EVENT_CUSTOM_DOWNSTREAM:
383       case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
384       case GST_EVENT_CUSTOM_BOTH:
385       case GST_EVENT_CUSTOM_BOTH_OOB:
386         if (got_event_after_q != NULL)
387           break;
388         gst_event_ref ((GstEvent *) data);
389         g_get_current_time (&got_event_time);
390         got_event_after_q = GST_EVENT (data);
391         break;
392       default:
393         break;
394     }
395   }
396
397   return GST_PAD_PROBE_OK;
398 }
399
400
401 typedef struct
402 {
403   GMutex *lock;
404   GCond *cond;
405   gboolean signaled;
406 } SignalData;
407
408 static void
409 signal_data_init (SignalData * data)
410 {
411   GST_DEBUG ("init %p", data);
412   data->lock = g_mutex_new ();
413   data->cond = g_cond_new ();
414   data->signaled = FALSE;
415 }
416
417 static void
418 signal_data_cleanup (SignalData * data)
419 {
420   GST_DEBUG ("free %p", data);
421   g_mutex_free (data->lock);
422   g_cond_free (data->cond);
423 }
424
425 static void
426 signal_data_signal (SignalData * data)
427 {
428   g_mutex_lock (data->lock);
429   data->signaled = TRUE;
430   g_cond_broadcast (data->cond);
431   GST_DEBUG ("signaling %p", data);
432   g_mutex_unlock (data->lock);
433 }
434
435 static void
436 signal_data_wait (SignalData * data)
437 {
438   g_mutex_lock (data->lock);
439   GST_DEBUG ("signal wait %p", data);
440   while (!data->signaled)
441     g_cond_wait (data->cond, data->lock);
442   GST_DEBUG ("signal wait done %p", data);
443   g_mutex_unlock (data->lock);
444 }
445
446 static GstPadProbeReturn
447 signal_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
448 {
449   SignalData *data = (SignalData *) user_data;
450
451   GST_DEBUG ("signal called %p", data);
452   signal_data_signal (data);
453   GST_DEBUG ("signal done %p", data);
454
455   return GST_PAD_PROBE_OK;
456 }
457
458 static void test_event
459     (GstBin * pipeline, GstEventType type, GstPad * pad,
460     gboolean expect_before_q, GstPad * fake_srcpad)
461 {
462   GstEvent *event;
463   GstPad *peer;
464   gint i;
465   SignalData data;
466   gulong id;
467
468   got_event_before_q = got_event_after_q = NULL;
469
470   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
471   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
472       GST_CLOCK_TIME_NONE);
473
474   GST_DEBUG ("test event called");
475
476   event = gst_event_new_custom (type,
477       gst_structure_new_empty ("application/x-custom"));
478   g_get_current_time (&sent_event_time);
479   got_event_time.tv_sec = 0;
480   got_event_time.tv_usec = 0;
481
482   signal_data_init (&data);
483
484   /* We block the pad so the stream lock is released and we can send the event */
485   id = gst_pad_add_probe (fake_srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
486       signal_blocked, &data, NULL);
487   fail_unless (id != 0);
488
489   signal_data_wait (&data);
490
491   /* We send on the peer pad, since the pad is blocked */
492   GST_DEBUG ("sending event %p", event);
493   fail_unless ((peer = gst_pad_get_peer (pad)) != NULL);
494   gst_pad_send_event (peer, event);
495   gst_object_unref (peer);
496
497   gst_pad_remove_probe (fake_srcpad, id);
498
499   if (expect_before_q) {
500     /* Wait up to 5 seconds for the event to appear */
501     for (i = 0; i < 500; i++) {
502       g_usleep (G_USEC_PER_SEC / 100);
503       if (got_event_before_q != NULL)
504         break;
505     }
506     fail_if (got_event_before_q == NULL,
507         "Expected event failed to appear upstream of the queue "
508         "within 5 seconds");
509     fail_unless (GST_EVENT_TYPE (got_event_before_q) == type);
510   } else {
511     /* Wait up to 10 seconds for the event to appear */
512     for (i = 0; i < 1000; i++) {
513       g_usleep (G_USEC_PER_SEC / 100);
514       if (got_event_after_q != NULL)
515         break;
516     }
517     fail_if (got_event_after_q == NULL,
518         "Expected event failed to appear after the queue within 10 seconds");
519     fail_unless (GST_EVENT_TYPE (got_event_after_q) == type);
520   }
521
522   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
523   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
524       GST_CLOCK_TIME_NONE);
525
526   if (got_event_before_q)
527     gst_event_unref (got_event_before_q);
528   if (got_event_after_q)
529     gst_event_unref (got_event_after_q);
530
531   got_event_before_q = got_event_after_q = NULL;
532
533   signal_data_cleanup (&data);
534 }
535
536 static gint64
537 timediff (GTimeVal * end, GTimeVal * start)
538 {
539   return (end->tv_sec - start->tv_sec) * G_USEC_PER_SEC +
540       (end->tv_usec - start->tv_usec);
541 }
542
543 GST_START_TEST (send_custom_events)
544 {
545   /* Run some tests on custom events. Checking for serialisation and whatnot.
546    * pipeline is fakesrc ! queue ! fakesink */
547   GstBin *pipeline;
548   GstElement *fakesrc, *fakesink, *queue;
549   GstPad *srcpad, *sinkpad;
550
551   fail_if ((pipeline = (GstBin *) gst_pipeline_new ("testpipe")) == NULL);
552   fail_if ((fakesrc = gst_element_factory_make ("fakesrc", NULL)) == NULL);
553   fail_if ((fakesink = gst_element_factory_make ("fakesink", NULL)) == NULL);
554   fail_if ((queue = gst_element_factory_make ("queue", NULL)) == NULL);
555
556   gst_bin_add_many (pipeline, fakesrc, queue, fakesink, NULL);
557   fail_unless (gst_element_link_many (fakesrc, queue, fakesink, NULL));
558
559   g_object_set (G_OBJECT (fakesink), "sync", FALSE, NULL);
560
561   /* Send 100 buffers per sec */
562   g_object_set (G_OBJECT (fakesrc), "silent", TRUE, "datarate", 100,
563       "sizemax", 1, "sizetype", 2, NULL);
564   g_object_set (G_OBJECT (queue), "max-size-buffers", 0, "max-size-time",
565       (guint64) GST_SECOND, "max-size-bytes", 0, NULL);
566   g_object_set (G_OBJECT (fakesink), "silent", TRUE, "sync", TRUE, NULL);
567
568   /* add pad-probes to faksrc.src and fakesink.sink */
569   fail_if ((srcpad = gst_element_get_static_pad (fakesrc, "src")) == NULL);
570   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_BOTH,
571       event_probe, GINT_TO_POINTER (TRUE), NULL);
572
573   fail_if ((sinkpad = gst_element_get_static_pad (fakesink, "sink")) == NULL);
574   gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_EVENT_BOTH,
575       event_probe, GINT_TO_POINTER (FALSE), NULL);
576
577   /* Upstream events */
578   test_event (pipeline, GST_EVENT_CUSTOM_UPSTREAM, sinkpad, TRUE, srcpad);
579   fail_unless (timediff (&got_event_time,
580           &sent_event_time) < G_USEC_PER_SEC / 2,
581       "GST_EVENT_CUSTOM_UP took too long to reach source: %"
582       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
583
584   test_event (pipeline, GST_EVENT_CUSTOM_BOTH, sinkpad, TRUE, srcpad);
585   fail_unless (timediff (&got_event_time,
586           &sent_event_time) < G_USEC_PER_SEC / 2,
587       "GST_EVENT_CUSTOM_BOTH took too long to reach source: %"
588       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
589
590   test_event (pipeline, GST_EVENT_CUSTOM_BOTH_OOB, sinkpad, TRUE, srcpad);
591   fail_unless (timediff (&got_event_time,
592           &sent_event_time) < G_USEC_PER_SEC / 2,
593       "GST_EVENT_CUSTOM_BOTH_OOB took too long to reach source: %"
594       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
595
596   /* Out of band downstream events */
597   test_event (pipeline, GST_EVENT_CUSTOM_DOWNSTREAM_OOB, srcpad, FALSE, srcpad);
598   fail_unless (timediff (&got_event_time,
599           &sent_event_time) < G_USEC_PER_SEC / 2,
600       "GST_EVENT_CUSTOM_DS_OOB took too long to reach source: %"
601       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
602
603   test_event (pipeline, GST_EVENT_CUSTOM_BOTH_OOB, srcpad, FALSE, srcpad);
604   fail_unless (timediff (&got_event_time,
605           &sent_event_time) < G_USEC_PER_SEC / 2,
606       "GST_EVENT_CUSTOM_BOTH_OOB took too long to reach source: %"
607       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
608
609   /* In-band downstream events are expected to take at least 1 second
610    * to traverse the queue */
611   test_event (pipeline, GST_EVENT_CUSTOM_DOWNSTREAM, srcpad, FALSE, srcpad);
612   fail_unless (timediff (&got_event_time,
613           &sent_event_time) >= G_USEC_PER_SEC / 2,
614       "GST_EVENT_CUSTOM_DS arrived too quickly for an in-band event: %"
615       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
616
617   test_event (pipeline, GST_EVENT_CUSTOM_BOTH, srcpad, FALSE, srcpad);
618   fail_unless (timediff (&got_event_time,
619           &sent_event_time) >= G_USEC_PER_SEC / 2,
620       "GST_EVENT_CUSTOM_BOTH arrived too quickly for an in-band event: %"
621       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
622
623   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
624   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
625       GST_CLOCK_TIME_NONE);
626
627   gst_object_unref (sinkpad);
628   gst_object_unref (srcpad);
629   gst_object_unref (pipeline);
630 }
631
632 GST_END_TEST;
633
634 static Suite *
635 gst_event_suite (void)
636 {
637   Suite *s = suite_create ("GstEvent");
638   TCase *tc_chain = tcase_create ("events");
639
640   tcase_set_timeout (tc_chain, 20);
641
642   suite_add_tcase (s, tc_chain);
643   tcase_add_test (tc_chain, create_events);
644   tcase_add_test (tc_chain, send_custom_events);
645   return s;
646 }
647
648 GST_CHECK_MAIN (gst_event);