Git init
[framework/multimedia/gstreamer0.10.git] / tests / check / elements / selector.c
1 /* GStreamer
2  *
3  * Unit test for selector plugin
4  * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
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 #include <gst/check/gstcheck.h>
23
24 #define NUM_SELECTOR_PADS 4
25 #define NUM_INPUT_BUFFERS 4     // buffers to send per each selector pad
26
27 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
28     GST_PAD_SINK,
29     GST_PAD_ALWAYS,
30     GST_STATIC_CAPS_ANY);
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
32     GST_PAD_SRC,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS_ANY);
35
36 /* Data probe cb to drop everything but count buffers and events */
37 static gboolean
38 probe_cb (GstPad * pad, GstMiniObject * obj, gpointer user_data)
39 {
40   gint count = 0;
41   const gchar *count_type = NULL;
42
43   GST_LOG_OBJECT (pad, "got data");
44
45   if (GST_IS_BUFFER (obj)) {
46     count_type = "buffer_count";
47   } else if (GST_IS_EVENT (obj)) {
48     count_type = "event_count";
49   } else {
50     g_assert_not_reached ();
51   }
52
53   /* increment and store count */
54   count = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), count_type));
55   count++;
56   g_object_set_data (G_OBJECT (pad), count_type, GINT_TO_POINTER (count));
57
58   /* drop everything */
59   return FALSE;
60 }
61
62 /* Create and link output pad: selector:src%d ! output_pad */
63 static GstPad *
64 setup_output_pad (GstElement * element, GstStaticPadTemplate * tmpl)
65 {
66   GstPad *srcpad = NULL, *output_pad = NULL;
67   gulong probe_id = 0;
68
69   if (tmpl == NULL)
70     tmpl = &sinktemplate;
71
72   /* create output_pad */
73   output_pad = gst_pad_new_from_static_template (tmpl, "sink");
74   fail_if (output_pad == NULL, "Could not create a output_pad");
75
76   /* add probe */
77   probe_id = gst_pad_add_data_probe (output_pad, G_CALLBACK (probe_cb), NULL);
78   g_object_set_data (G_OBJECT (output_pad), "probe_id",
79       GINT_TO_POINTER (probe_id));
80
81   /* request src pad */
82   srcpad = gst_element_get_request_pad (element, "src%d");
83   fail_if (srcpad == NULL, "Could not get source pad from %s",
84       GST_ELEMENT_NAME (element));
85
86   /* link pads and activate */
87   fail_unless (gst_pad_link (srcpad, output_pad) == GST_PAD_LINK_OK,
88       "Could not link %s source and output pad", GST_ELEMENT_NAME (element));
89
90   gst_pad_set_active (output_pad, TRUE);
91
92   GST_DEBUG_OBJECT (output_pad, "set up %" GST_PTR_FORMAT " ! %" GST_PTR_FORMAT,
93       srcpad, output_pad);
94
95   gst_object_unref (srcpad);
96   ASSERT_OBJECT_REFCOUNT (srcpad, "srcpad", 1);
97
98   return output_pad;
99 }
100
101 /* Clean up output/input pad and respective selector request pad */
102 static void
103 cleanup_pad (GstPad * pad, GstElement * element)
104 {
105   GstPad *selpad = NULL;
106   guint probe_id = 0;
107
108   fail_if (pad == NULL, "pad doesn't exist");
109
110   /* remove probe if necessary */
111   probe_id = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pad), "probe_id"));
112   if (probe_id)
113     gst_pad_remove_data_probe (pad, probe_id);
114
115   /* unlink */
116   selpad = gst_pad_get_peer (pad);
117   if (GST_PAD_DIRECTION (selpad) == GST_PAD_SRC) {
118     gst_pad_unlink (selpad, pad);
119   } else {
120     gst_pad_unlink (pad, selpad);
121   }
122
123   /* caps could have been set, make sure they get unset */
124   gst_pad_set_caps (pad, NULL);
125
126   GST_DEBUG_OBJECT (pad, "clean up %" GST_PTR_FORMAT " and  %" GST_PTR_FORMAT,
127       selpad, pad);
128
129   /* cleanup the pad */
130   gst_pad_set_active (pad, FALSE);
131   ASSERT_OBJECT_REFCOUNT (pad, "pad", 1);
132   gst_object_unref (pad);
133
134   /* cleanup selector pad, reffed by this function (_get_peer) and creator */
135   gst_element_release_request_pad (element, selpad);
136   gst_object_unref (selpad);
137 }
138
139 /* Duplicate and push given buffer many times to all input_pads */
140 static void
141 push_input_buffers (GList * input_pads, GstBuffer * buf, gint num_buffers)
142 {
143   GstBuffer *buf_in = NULL;
144   GList *l = input_pads;
145   GstPad *input_pad;
146   gint i = 0;
147
148   while (l != NULL) {
149     input_pad = l->data;
150     GST_DEBUG_OBJECT (input_pad, "pushing %d buffers to %" GST_PTR_FORMAT,
151         num_buffers, input_pad);
152     for (i = 0; i < num_buffers; i++) {
153       buf_in = gst_buffer_copy (buf);
154       fail_unless (gst_pad_push (input_pad, buf_in) == GST_FLOW_OK,
155           "pushing buffer failed");
156     }
157     l = g_list_next (l);
158   }
159 }
160
161 /* Check that received buffers count match to expected buffers */
162 static void
163 count_output_buffers (GList * output_pads, gint expected_buffers)
164 {
165   gint count = 0;
166   GList *l = output_pads;
167   GstPad *output_pad = NULL;
168
169   while (l != NULL) {
170     output_pad = l->data;
171     count =
172         GPOINTER_TO_INT (g_object_get_data (G_OBJECT (output_pad),
173             "buffer_count"));
174     GST_DEBUG_OBJECT (output_pad, "received %d buffers", count);
175     fail_unless (count == expected_buffers,
176         "received/expected buffer count doesn't match %d/%d", count,
177         expected_buffers);
178     count =
179         GPOINTER_TO_INT (g_object_get_data (G_OBJECT (output_pad),
180             "event_count"));
181     GST_DEBUG_OBJECT (output_pad, "received %d events", count);
182     l = g_list_next (l);
183   }
184 }
185
186 /* Set selector active pad */
187 static void
188 selector_set_active_pad (GstElement * elem, GstPad * selpad)
189 {
190   gchar *padname = NULL;
191
192   if (selpad) {
193     padname = gst_pad_get_name (selpad);
194   }
195
196   g_object_set (G_OBJECT (elem), "active-pad", selpad, NULL);
197   GST_DEBUG_OBJECT (elem, "activated selector pad: %s", GST_STR_NULL (padname));
198   g_free (padname);
199 }
200
201 /* Push buffers and switch for each selector pad */
202 static void
203 push_switched_buffers (GList * input_pads,
204     GstElement * elem, GList * peer_pads, gint num_buffers)
205 {
206   GstBuffer *buf = NULL;
207   GstCaps *caps = NULL;
208   GList *l = peer_pads;
209   GstPad *selpad = NULL;
210
211   /* setup dummy buffer */
212   caps = gst_caps_from_string ("application/x-unknown");
213   buf = gst_buffer_new_and_alloc (1);
214   gst_buffer_set_caps (buf, caps);
215   gst_caps_unref (caps);
216
217   while (l != NULL) {
218     /* set selector pad */
219     selpad = gst_pad_get_peer (GST_PAD (l->data));
220     selector_set_active_pad (elem, selpad);
221     if (selpad) {
222       gst_object_unref (selpad);
223     }
224     /* push buffers */
225     push_input_buffers (input_pads, buf, num_buffers);
226     /* switch to next selector pad */
227     l = g_list_next (l);
228   }
229
230   /* cleanup buffer */
231   gst_buffer_unref (buf);
232 }
233
234 /* Create output-selector with given number of src pads and switch
235    given number of input buffers to each src pad.
236  */
237 static void
238 run_output_selector_buffer_count (gint num_output_pads,
239     gint num_buffers_per_output)
240 {
241   /* setup input_pad ! selector ! output_pads */
242   gint i = 0;
243   GList *output_pads = NULL, *input_pads = NULL;
244   GstElement *sel = gst_check_setup_element ("output-selector");
245   GstPad *input_pad = gst_check_setup_src_pad (sel, &srctemplate, NULL);
246
247   input_pads = g_list_append (input_pads, input_pad);
248   gst_pad_set_active (input_pad, TRUE);
249   for (i = 0; i < num_output_pads; i++) {
250     output_pads = g_list_append (output_pads, setup_output_pad (sel, NULL));
251   }
252
253   /* run the test */
254   fail_unless (gst_element_set_state (sel,
255           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
256       "could not set to playing");
257   push_switched_buffers (input_pads, sel, output_pads, num_buffers_per_output);
258   count_output_buffers (output_pads, num_buffers_per_output);
259   fail_unless (gst_element_set_state (sel,
260           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
261
262   /* cleanup input_pad, selector and output_pads */
263   gst_pad_set_active (input_pad, FALSE);
264   gst_check_teardown_src_pad (sel);
265   g_list_foreach (output_pads, (GFunc) cleanup_pad, sel);
266   g_list_free (output_pads);
267   g_list_free (input_pads);
268   gst_check_teardown_element (sel);
269 }
270
271 /* Create and link input pad: input_pad ! selector:sink%d */
272 static GstPad *
273 setup_input_pad (GstElement * element)
274 {
275   GstPad *sinkpad = NULL, *input_pad = NULL;
276
277   /* create input_pad */
278   input_pad = gst_pad_new_from_static_template (&srctemplate, "src");
279   fail_if (input_pad == NULL, "Could not create a input_pad");
280
281   /* request sink pad */
282   sinkpad = gst_element_get_request_pad (element, "sink%d");
283   fail_if (sinkpad == NULL, "Could not get sink pad from %s",
284       GST_ELEMENT_NAME (element));
285
286   /* link pads and activate */
287   fail_unless (gst_pad_link (input_pad, sinkpad) == GST_PAD_LINK_OK,
288       "Could not link input_pad and %s sink", GST_ELEMENT_NAME (element));
289
290   gst_pad_set_active (input_pad, TRUE);
291
292   GST_DEBUG_OBJECT (input_pad, "set up %" GST_PTR_FORMAT " ! %" GST_PTR_FORMAT,
293       input_pad, sinkpad);
294
295   gst_object_unref (sinkpad);
296   ASSERT_OBJECT_REFCOUNT (sinkpad, "sinkpad", 1);
297
298   return input_pad;
299 }
300
301 /* Create input-selector with given number of sink pads and switch
302    given number of input buffers to each sink pad.
303  */
304 static void
305 run_input_selector_buffer_count (gint num_input_pads,
306     gint num_buffers_per_input)
307 {
308   /* set up input_pads ! selector ! output_pad */
309   gint i = 0, probe_id = 0;
310   GList *input_pads = NULL, *output_pads = NULL;
311   GstElement *sel = gst_check_setup_element ("input-selector");
312   GstPad *output_pad = gst_check_setup_sink_pad (sel, &sinktemplate, NULL);
313
314   output_pads = g_list_append (output_pads, output_pad);
315   gst_pad_set_active (output_pad, TRUE);
316   for (i = 0; i < num_input_pads; i++) {
317     input_pads = g_list_append (input_pads, setup_input_pad (sel));
318   }
319   /* add probe */
320   probe_id = gst_pad_add_data_probe (output_pad, G_CALLBACK (probe_cb), NULL);
321   g_object_set_data (G_OBJECT (output_pad), "probe_id",
322       GINT_TO_POINTER (probe_id));
323
324   /* run the test */
325   fail_unless (gst_element_set_state (sel,
326           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
327       "could not set to playing");
328   push_switched_buffers (input_pads, sel, input_pads, num_buffers_per_input);
329   count_output_buffers (output_pads, (num_input_pads * num_buffers_per_input));
330   fail_unless (gst_element_set_state (sel,
331           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
332
333   /* clean up */
334   gst_pad_remove_data_probe (output_pad, probe_id);
335   gst_pad_set_active (output_pad, FALSE);
336   gst_check_teardown_sink_pad (sel);
337   GST_DEBUG ("setting selector pad to NULL");
338   selector_set_active_pad (sel, NULL);  // unref input-selector active pad
339   g_list_foreach (input_pads, (GFunc) cleanup_pad, sel);
340   g_list_free (input_pads);
341   g_list_free (output_pads);
342   gst_check_teardown_element (sel);
343 }
344
345 /* Push buffers to input pad and check the 
346    amount of buffers arrived to output pads */
347 GST_START_TEST (test_output_selector_buffer_count);
348 {
349   gint i, j;
350
351   for (i = 0; i < NUM_SELECTOR_PADS; i++) {
352     for (j = 0; j < NUM_INPUT_BUFFERS; j++) {
353       run_output_selector_buffer_count (i, j);
354     }
355   }
356 }
357
358 GST_END_TEST;
359
360 /* Push buffers to input pads and check the 
361    amount of buffers arrived to output pad */
362 GST_START_TEST (test_input_selector_buffer_count);
363 {
364   gint i, j;
365
366   for (i = 0; i < NUM_SELECTOR_PADS; i++) {
367     for (j = 0; j < NUM_INPUT_BUFFERS; j++) {
368       run_input_selector_buffer_count (i, j);
369     }
370   }
371 }
372
373 GST_END_TEST;
374
375
376 GST_START_TEST (test_output_selector_no_srcpad_negotiation);
377 {
378   GstElement *sel;
379   GstCaps *caps;
380   GstPad *pad;
381   gint i;
382
383   sel = gst_element_factory_make ("output-selector", NULL);
384   fail_unless (sel != NULL);
385
386   pad = gst_element_get_static_pad (sel, "sink");
387   fail_unless (pad != NULL);
388
389   fail_unless (gst_element_set_state (sel,
390           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
391       "could not set to playing");
392
393   for (i = 0; i <= 2; i++) {
394
395     /* regardless of pad-negotiation-mode, getcaps should return ANY and
396      * setcaps should accept any caps when there are no srcpads */
397     g_object_set (sel, "pad-negotiation-mode", i, NULL);
398
399     caps = gst_pad_get_caps (pad);
400     fail_unless (gst_caps_is_any (caps));
401
402     gst_caps_unref (caps);
403
404     caps = gst_caps_new_simple ("mymedia/mycaps", NULL);
405     fail_unless (gst_pad_set_caps (pad, caps));
406     gst_caps_unref (caps);
407   }
408
409   fail_unless (gst_element_set_state (sel,
410           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
411   gst_object_unref (pad);
412   gst_object_unref (sel);
413 }
414
415 GST_END_TEST;
416
417
418 GstElement *sel;
419 GstPad *input_pad;
420 GList *output_pads = NULL;      /* list of sinkpads linked to output-selector */
421 #define OUTPUT_SELECTOR_NUM_PADS 2
422
423 static GstStaticPadTemplate sinktmpl_nego_a = GST_STATIC_PAD_TEMPLATE ("sink",
424     GST_PAD_SINK,
425     GST_PAD_ALWAYS,
426     GST_STATIC_CAPS ("format/abc; format/xyz"));
427 static GstStaticPadTemplate sinktmpl_nego_b = GST_STATIC_PAD_TEMPLATE ("sink",
428     GST_PAD_SINK,
429     GST_PAD_ALWAYS,
430     GST_STATIC_CAPS ("format/abc"));
431
432 static void
433 setup_output_selector (void)
434 {
435   sel = gst_check_setup_element ("output-selector");
436   input_pad = gst_check_setup_src_pad (sel, &srctemplate, NULL);
437   gst_pad_set_active (input_pad, TRUE);
438
439   output_pads = g_list_append (output_pads, setup_output_pad (sel,
440           &sinktmpl_nego_a));
441   output_pads = g_list_append (output_pads, setup_output_pad (sel,
442           &sinktmpl_nego_b));
443 }
444
445 static void
446 teardown_output_selector (void)
447 {
448   gst_pad_set_active (input_pad, FALSE);
449   gst_object_unref (input_pad);
450   gst_check_teardown_src_pad (sel);
451   g_list_foreach (output_pads, (GFunc) cleanup_pad, sel);
452   g_list_free (output_pads);
453   gst_check_teardown_element (sel);
454   output_pads = NULL;
455 }
456
457 GST_START_TEST (test_output_selector_getcaps_none);
458 {
459   GList *walker;
460
461   /* set pad negotiation mode to none */
462   g_object_set (sel, "pad-negotiation-mode", 0, NULL);
463
464   fail_unless (gst_element_set_state (sel,
465           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
466       "could not set to playing");
467
468   for (walker = output_pads; walker; walker = g_list_next (walker)) {
469     GstCaps *caps;
470     GstPad *pad;
471
472     pad = gst_pad_get_peer ((GstPad *) walker->data);
473
474     g_object_set (sel, "active-pad", pad, NULL);
475
476     caps = gst_pad_peer_get_caps (input_pad);
477
478     /* in 'none' mode, the getcaps returns the template, which is ANY */
479     g_assert (gst_caps_is_any (caps));
480     gst_caps_unref (caps);
481     gst_object_unref (pad);
482   }
483
484   fail_unless (gst_element_set_state (sel,
485           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
486
487 }
488
489 GST_END_TEST;
490
491
492 GST_START_TEST (test_output_selector_getcaps_all);
493 {
494   GList *walker;
495   GstCaps *expected;
496
497   /* set pad negotiation mode to 'all' */
498   g_object_set (sel, "pad-negotiation-mode", 1, NULL);
499
500   fail_unless (gst_element_set_state (sel,
501           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
502       "could not set to playing");
503
504   /* in 'all' mode, the intersection of the srcpad caps should be returned on
505    * the sinkpad's getcaps */
506   expected = gst_caps_new_simple ("format/abc", NULL);
507
508   for (walker = output_pads; walker; walker = g_list_next (walker)) {
509     GstCaps *caps;
510     GstPad *pad;
511
512     pad = gst_pad_get_peer ((GstPad *) walker->data);
513
514     g_object_set (sel, "active-pad", pad, NULL);
515
516     caps = gst_pad_peer_get_caps (input_pad);
517
518     g_assert (gst_caps_is_equal (caps, expected));
519     gst_caps_unref (caps);
520     gst_object_unref (pad);
521   }
522   gst_caps_unref (expected);
523
524   fail_unless (gst_element_set_state (sel,
525           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
526
527 }
528
529 GST_END_TEST;
530
531
532 GST_START_TEST (test_output_selector_getcaps_active);
533 {
534   GList *walker;
535   GstCaps *expected;
536
537   /* set pad negotiation mode to 'active' */
538   g_object_set (sel, "pad-negotiation-mode", 2, NULL);
539
540   fail_unless (gst_element_set_state (sel,
541           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
542       "could not set to playing");
543
544   for (walker = output_pads; walker; walker = g_list_next (walker)) {
545     GstCaps *caps;
546     GstPad *pad;
547
548     pad = gst_pad_get_peer ((GstPad *) walker->data);
549
550     g_object_set (sel, "active-pad", pad, NULL);
551
552     /* in 'active' mode, the active srcpad peer's caps should be returned on
553      * the sinkpad's getcaps */
554
555     expected = gst_pad_template_get_caps (gst_pad_get_pad_template ((GstPad *)
556             walker->data));
557     caps = gst_pad_peer_get_caps (input_pad);
558
559     g_assert (gst_caps_is_equal (caps, expected));
560     gst_caps_unref (caps);
561     gst_object_unref (pad);
562   }
563
564   fail_unless (gst_element_set_state (sel,
565           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
566
567 }
568
569 GST_END_TEST;
570
571
572 static Suite *
573 selector_suite (void)
574 {
575   Suite *s = suite_create ("selector");
576   TCase *tc_chain = tcase_create ("general");
577
578   suite_add_tcase (s, tc_chain);
579   tcase_add_test (tc_chain, test_output_selector_buffer_count);
580   tcase_add_test (tc_chain, test_input_selector_buffer_count);
581   tcase_add_test (tc_chain, test_output_selector_no_srcpad_negotiation);
582
583   tc_chain = tcase_create ("output-selector-negotiation");
584   tcase_add_checked_fixture (tc_chain, setup_output_selector,
585       teardown_output_selector);
586   suite_add_tcase (s, tc_chain);
587   tcase_add_test (tc_chain, test_output_selector_getcaps_none);
588   tcase_add_test (tc_chain, test_output_selector_getcaps_all);
589   tcase_add_test (tc_chain, test_output_selector_getcaps_active);
590
591   return s;
592 }
593
594 GST_CHECK_MAIN (selector);