And correct even more valid sparse warnings.
[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     event = gst_event_new_flush_stop ();
43     fail_if (event == NULL);
44     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP);
45     fail_unless (GST_EVENT_IS_UPSTREAM (event));
46     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
47     fail_unless (GST_EVENT_IS_SERIALIZED (event));
48     gst_event_unref (event);
49   }
50   /* EOS */
51   {
52     event = gst_event_new_eos ();
53     fail_if (event == NULL);
54     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_EOS);
55     fail_if (GST_EVENT_IS_UPSTREAM (event));
56     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
57     fail_unless (GST_EVENT_IS_SERIALIZED (event));
58     gst_event_unref (event);
59   }
60   /* NEWSEGMENT */
61   {
62     gdouble rate, applied_rate;
63     GstFormat format;
64     gint64 start, end, base;
65     gboolean update;
66
67     event =
68         gst_event_new_new_segment (FALSE, 0.5, GST_FORMAT_TIME, 1, G_MAXINT64,
69         0xdeadbeef);
70     fail_if (event == NULL);
71     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT);
72     fail_if (GST_EVENT_IS_UPSTREAM (event));
73     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
74     fail_unless (GST_EVENT_IS_SERIALIZED (event));
75
76     gst_event_parse_new_segment (event, &update, &rate, &format, &start, &end,
77         &base);
78     fail_unless (update == FALSE);
79     fail_unless (rate == 0.5);
80     fail_unless (format == GST_FORMAT_TIME);
81     fail_unless (start == 1);
82     fail_unless (end == G_MAXINT64);
83     fail_unless (base == 0xdeadbeef);
84
85     /* Check that the new segment was created with applied_rate of 1.0 */
86     gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
87         &format, &start, &end, &base);
88
89     fail_unless (update == FALSE);
90     fail_unless (rate == 0.5);
91     fail_unless (applied_rate == 1.0);
92     fail_unless (format == GST_FORMAT_TIME);
93     fail_unless (start == 1);
94     fail_unless (end == G_MAXINT64);
95
96     gst_event_unref (event);
97
98     event =
99         gst_event_new_new_segment_full (TRUE, 0.75, 0.5, GST_FORMAT_BYTES, 0,
100         G_MAXINT64 - 1, 0xdeadbeef);
101
102     fail_if (event == NULL);
103     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT);
104     fail_if (GST_EVENT_IS_UPSTREAM (event));
105     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
106     fail_unless (GST_EVENT_IS_SERIALIZED (event));
107
108     gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
109         &format, &start, &end, &base);
110
111     fail_unless (update == TRUE);
112     fail_unless (rate == 0.75);
113     fail_unless (applied_rate == 0.5);
114     fail_unless (format == GST_FORMAT_BYTES);
115     fail_unless (start == 0);
116     fail_unless (end == (G_MAXINT64 - 1));
117
118     gst_event_unref (event);
119   }
120
121   /* TAGS */
122   {
123     GstTagList *taglist = gst_tag_list_new ();
124     GstTagList *tl2 = NULL;
125
126     event = gst_event_new_tag (taglist);
127     fail_if (taglist == NULL);
128     fail_if (event == NULL);
129     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_TAG);
130     fail_if (GST_EVENT_IS_UPSTREAM (event));
131     fail_unless (GST_EVENT_IS_DOWNSTREAM (event));
132     fail_unless (GST_EVENT_IS_SERIALIZED (event));
133
134     gst_event_parse_tag (event, &tl2);
135     fail_unless (taglist == tl2);
136     gst_event_unref (event);
137   }
138
139   /* QOS */
140   {
141     gdouble p1 = 1.0, p2;
142     GstClockTimeDiff ctd1 = G_GINT64_CONSTANT (10), ctd2;
143     GstClockTime ct1 = G_GUINT64_CONSTANT (20), ct2;
144
145     event = gst_event_new_qos (p1, ctd1, ct1);
146     fail_if (event == NULL);
147     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_QOS);
148     fail_unless (GST_EVENT_IS_UPSTREAM (event));
149     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
150     fail_if (GST_EVENT_IS_SERIALIZED (event));
151
152     gst_event_parse_qos (event, &p2, &ctd2, &ct2);
153     fail_unless (p1 == p2);
154     fail_unless (ctd1 == ctd2);
155     fail_unless (ct1 == ct2);
156     gst_event_unref (event);
157   }
158
159   /* SEEK */
160   {
161     gdouble rate;
162     GstFormat format;
163     GstSeekFlags flags;
164     GstSeekType cur_type, stop_type;
165     gint64 cur, stop;
166
167     event = gst_event_new_seek (0.5, GST_FORMAT_BYTES,
168         GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
169         GST_SEEK_TYPE_SET, 1, GST_SEEK_TYPE_NONE, 0xdeadbeef);
170
171     fail_if (event == NULL);
172     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);
173     fail_unless (GST_EVENT_IS_UPSTREAM (event));
174     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
175     fail_if (GST_EVENT_IS_SERIALIZED (event));
176
177     gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
178         &stop_type, &stop);
179     fail_unless (rate == 0.5);
180     fail_unless (format == GST_FORMAT_BYTES);
181     fail_unless (flags == (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE));
182     fail_unless (cur_type == GST_SEEK_TYPE_SET);
183     fail_unless (cur == 1);
184     fail_unless (stop_type == GST_SEEK_TYPE_NONE);
185     fail_unless (stop == 0xdeadbeef);
186
187     gst_event_unref (event);
188   }
189
190   /* NAVIGATION */
191   {
192     structure = gst_structure_new ("application/x-gst-navigation", "event",
193         G_TYPE_STRING, "key-press", "key", G_TYPE_STRING, "mon", NULL);
194     fail_if (structure == NULL);
195     event = gst_event_new_navigation (structure);
196     fail_if (event == NULL);
197     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION);
198     fail_unless (GST_EVENT_IS_UPSTREAM (event));
199     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
200     fail_if (GST_EVENT_IS_SERIALIZED (event));
201
202     fail_unless (gst_event_get_structure (event) == structure);
203     gst_event_unref (event);
204   }
205
206   /* Custom event types */
207   {
208     structure = gst_structure_empty_new ("application/x-custom");
209     fail_if (structure == NULL);
210     event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, structure);
211     fail_if (event == NULL);
212     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_UPSTREAM);
213     fail_unless (GST_EVENT_IS_UPSTREAM (event));
214     fail_if (GST_EVENT_IS_DOWNSTREAM (event));
215     fail_if (GST_EVENT_IS_SERIALIZED (event));
216     fail_unless (gst_event_get_structure (event) == structure);
217     gst_event_unref (event);
218
219     /* Decided not to test the other custom enum types, as they
220      * only differ by the value of the enum passed to gst_event_new_custom
221      */
222   }
223
224   /* Event copying */
225   {
226     structure = gst_structure_empty_new ("application/x-custom");
227     fail_if (structure == NULL);
228     event = gst_event_new_custom (GST_EVENT_CUSTOM_BOTH, structure);
229
230     fail_if (event == NULL);
231     event2 = gst_event_copy (event);
232     fail_if (event2 == NULL);
233     fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_TYPE (event2));
234
235     /* The structure should have been duplicated */
236     fail_if (gst_event_get_structure (event) ==
237         gst_event_get_structure (event2));
238
239     gst_event_unref (event);
240     gst_event_unref (event2);
241   }
242
243   /* Make events writable */
244   {
245     structure = gst_structure_empty_new ("application/x-custom");
246     fail_if (structure == NULL);
247     event = gst_event_new_custom (GST_EVENT_CUSTOM_BOTH, structure);
248     /* ref the event so that it becomes non-writable */
249     gst_event_ref (event);
250     gst_event_ref (event);
251     /* this should fail if the structure isn't writable */
252     ASSERT_CRITICAL (gst_structure_remove_all_fields ((GstStructure *)
253             gst_event_get_structure (event)));
254
255     /* now make writable */
256     event2 =
257         GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
258     fail_unless (event != event2);
259     /* this fail if the structure isn't writable */
260     gst_structure_remove_all_fields ((GstStructure *)
261         gst_event_get_structure (event2));
262
263     gst_event_unref (event);
264     gst_event_unref (event);
265     gst_event_unref (event2);
266   }
267 }
268
269 GST_END_TEST;
270
271 static GTimeVal sent_event_time;
272 static GstEvent *got_event_before_q, *got_event_after_q;
273 static GTimeVal got_event_time;
274
275 static gboolean
276 event_probe (GstPad * pad, GstMiniObject ** data, gpointer user_data)
277 {
278   gboolean before_q = (gboolean) GPOINTER_TO_INT (user_data);
279
280   fail_unless (GST_IS_EVENT (data));
281
282   GST_DEBUG ("event probe called");
283
284   if (before_q) {
285     switch (GST_EVENT_TYPE (GST_EVENT (data))) {
286       case GST_EVENT_CUSTOM_UPSTREAM:
287       case GST_EVENT_CUSTOM_BOTH:
288       case GST_EVENT_CUSTOM_BOTH_OOB:
289         if (got_event_before_q != NULL)
290           break;
291         gst_event_ref ((GstEvent *) data);
292         g_get_current_time (&got_event_time);
293         got_event_before_q = GST_EVENT (data);
294         break;
295       default:
296         break;
297     }
298   } else {
299     switch (GST_EVENT_TYPE (GST_EVENT (data))) {
300       case GST_EVENT_CUSTOM_DOWNSTREAM:
301       case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
302       case GST_EVENT_CUSTOM_BOTH:
303       case GST_EVENT_CUSTOM_BOTH_OOB:
304         if (got_event_after_q != NULL)
305           break;
306         gst_event_ref ((GstEvent *) data);
307         g_get_current_time (&got_event_time);
308         got_event_after_q = GST_EVENT (data);
309         break;
310       default:
311         break;
312     }
313   }
314
315   return TRUE;
316 }
317
318 static void test_event
319     (GstBin * pipeline, GstEventType type, GstPad * pad,
320     gboolean expect_before_q, GstPad * fake_srcpad)
321 {
322   GstEvent *event;
323   GstPad *peer;
324   gint i;
325
326   got_event_before_q = got_event_after_q = NULL;
327
328   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
329   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
330       GST_CLOCK_TIME_NONE);
331
332   GST_DEBUG ("test event called");
333
334   event = gst_event_new_custom (type,
335       gst_structure_empty_new ("application/x-custom"));
336   g_get_current_time (&sent_event_time);
337   got_event_time.tv_sec = 0;
338   got_event_time.tv_usec = 0;
339
340   /* We block the pad so the stream lock is released and we can send the event */
341   fail_unless (gst_pad_set_blocked (fake_srcpad, TRUE) == TRUE);
342
343   /* We send on the peer pad, since the pad is blocked */
344   fail_unless ((peer = gst_pad_get_peer (pad)) != NULL);
345   gst_pad_send_event (peer, event);
346   gst_object_unref (peer);
347
348   fail_unless (gst_pad_set_blocked (fake_srcpad, FALSE) == TRUE);
349
350   if (expect_before_q) {
351     /* Wait up to 5 seconds for the event to appear */
352     for (i = 0; i < 500; i++) {
353       g_usleep (G_USEC_PER_SEC / 100);
354       if (got_event_before_q != NULL)
355         break;
356     }
357     fail_if (got_event_before_q == NULL,
358         "Expected event failed to appear upstream of the queue "
359         "within 5 seconds");
360     fail_unless (GST_EVENT_TYPE (got_event_before_q) == type);
361   } else {
362     /* Wait up to 10 seconds for the event to appear */
363     for (i = 0; i < 1000; i++) {
364       g_usleep (G_USEC_PER_SEC / 100);
365       if (got_event_after_q != NULL)
366         break;
367     }
368     fail_if (got_event_after_q == NULL,
369         "Expected event failed to appear after the queue within 10 seconds");
370     fail_unless (GST_EVENT_TYPE (got_event_after_q) == type);
371   }
372
373   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
374   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
375       GST_CLOCK_TIME_NONE);
376
377   if (got_event_before_q)
378     gst_event_unref (got_event_before_q);
379   if (got_event_after_q)
380     gst_event_unref (got_event_after_q);
381
382   got_event_before_q = got_event_after_q = NULL;
383 }
384
385 static gint64
386 timediff (GTimeVal * end, GTimeVal * start)
387 {
388   return (end->tv_sec - start->tv_sec) * G_USEC_PER_SEC +
389       (end->tv_usec - start->tv_usec);
390 }
391
392 GST_START_TEST (send_custom_events)
393 {
394   /* Run some tests on custom events. Checking for serialisation and whatnot.
395    * pipeline is fakesrc ! queue ! fakesink */
396   GstBin *pipeline;
397   GstElement *fakesrc, *fakesink, *queue;
398   GstPad *srcpad, *sinkpad;
399
400   fail_if ((pipeline = (GstBin *) gst_pipeline_new ("testpipe")) == NULL);
401   fail_if ((fakesrc = gst_element_factory_make ("fakesrc", NULL)) == NULL);
402   fail_if ((fakesink = gst_element_factory_make ("fakesink", NULL)) == NULL);
403   fail_if ((queue = gst_element_factory_make ("queue", NULL)) == NULL);
404
405   gst_bin_add_many (pipeline, fakesrc, queue, fakesink, NULL);
406   fail_unless (gst_element_link_many (fakesrc, queue, fakesink, NULL));
407
408   g_object_set (G_OBJECT (fakesink), "sync", FALSE, NULL);
409
410   /* Send 100 buffers per sec */
411   g_object_set (G_OBJECT (fakesrc), "silent", TRUE, "datarate", 100,
412       "sizemax", 1, "sizetype", 2, NULL);
413   g_object_set (G_OBJECT (queue), "max-size-buffers", 0, "max-size-time",
414       (guint64) GST_SECOND, "max-size-bytes", 0, NULL);
415   g_object_set (G_OBJECT (fakesink), "silent", TRUE, "sync", TRUE, NULL);
416
417   /* add pad-probes to faksrc.src and fakesink.sink */
418   fail_if ((srcpad = gst_element_get_pad (fakesrc, "src")) == NULL);
419   gst_pad_add_event_probe (srcpad, (GCallback) event_probe,
420       GINT_TO_POINTER (TRUE));
421
422   fail_if ((sinkpad = gst_element_get_pad (fakesink, "sink")) == NULL);
423   gst_pad_add_event_probe (sinkpad, (GCallback) event_probe,
424       GINT_TO_POINTER (FALSE));
425
426   /* Upstream events */
427   test_event (pipeline, GST_EVENT_CUSTOM_UPSTREAM, sinkpad, TRUE, srcpad);
428   fail_unless (timediff (&got_event_time,
429           &sent_event_time) < G_USEC_PER_SEC / 2,
430       "GST_EVENT_CUSTOM_UP took too long to reach source: %"
431       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
432
433   test_event (pipeline, GST_EVENT_CUSTOM_BOTH, sinkpad, TRUE, srcpad);
434   fail_unless (timediff (&got_event_time,
435           &sent_event_time) < G_USEC_PER_SEC / 2,
436       "GST_EVENT_CUSTOM_BOTH took too long to reach source: %"
437       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
438
439   test_event (pipeline, GST_EVENT_CUSTOM_BOTH_OOB, sinkpad, TRUE, srcpad);
440   fail_unless (timediff (&got_event_time,
441           &sent_event_time) < G_USEC_PER_SEC / 2,
442       "GST_EVENT_CUSTOM_BOTH_OOB took too long to reach source: %"
443       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
444
445   /* Out of band downstream events */
446   test_event (pipeline, GST_EVENT_CUSTOM_DOWNSTREAM_OOB, srcpad, FALSE, srcpad);
447   fail_unless (timediff (&got_event_time,
448           &sent_event_time) < G_USEC_PER_SEC / 2,
449       "GST_EVENT_CUSTOM_DS_OOB took too long to reach source: %"
450       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
451
452   test_event (pipeline, GST_EVENT_CUSTOM_BOTH_OOB, srcpad, FALSE, srcpad);
453   fail_unless (timediff (&got_event_time,
454           &sent_event_time) < G_USEC_PER_SEC / 2,
455       "GST_EVENT_CUSTOM_BOTH_OOB took too long to reach source: %"
456       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
457
458   /* In-band downstream events are expected to take at least 1 second
459    * to traverse the the queue */
460   test_event (pipeline, GST_EVENT_CUSTOM_DOWNSTREAM, srcpad, FALSE, srcpad);
461   fail_unless (timediff (&got_event_time,
462           &sent_event_time) >= G_USEC_PER_SEC / 2,
463       "GST_EVENT_CUSTOM_DS arrived too quickly for an in-band event: %"
464       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
465
466   test_event (pipeline, GST_EVENT_CUSTOM_BOTH, srcpad, FALSE, srcpad);
467   fail_unless (timediff (&got_event_time,
468           &sent_event_time) >= G_USEC_PER_SEC / 2,
469       "GST_EVENT_CUSTOM_BOTH arrived too quickly for an in-band event: %"
470       G_GINT64_FORMAT " us", timediff (&got_event_time, &sent_event_time));
471
472   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
473   gst_element_get_state (GST_ELEMENT (pipeline), NULL, NULL,
474       GST_CLOCK_TIME_NONE);
475
476   gst_object_unref (pipeline);
477 }
478
479 GST_END_TEST;
480
481 static Suite *
482 gst_event_suite (void)
483 {
484   Suite *s = suite_create ("GstEvent");
485   TCase *tc_chain = tcase_create ("events");
486
487   tcase_set_timeout (tc_chain, 20);
488
489   suite_add_tcase (s, tc_chain);
490   tcase_add_test (tc_chain, create_events);
491   tcase_add_test (tc_chain, send_custom_events);
492   return s;
493 }
494
495 GST_CHECK_MAIN (gst_event);