• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2009, Axis Communications AB, LUND, SWEDEN
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <gst/check/gstcheck.h>
25 #include <gst/app/gstappsink.h>
26 
27 gint global_testdata;
28 
29 static GstPad *mysrcpad;
30 
31 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
32     GST_PAD_SRC,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("application/x-gst-check")
35     );
36 
37 static GstElement *
setup_appsink(void)38 setup_appsink (void)
39 {
40   GstElement *appsink;
41   GstCaps *caps;
42 
43   GST_DEBUG ("setup_appsink");
44   appsink = gst_check_setup_element ("appsink");
45   mysrcpad = gst_check_setup_src_pad (appsink, &srctemplate);
46   gst_pad_set_active (mysrcpad, TRUE);
47 
48   caps = gst_caps_new_empty_simple ("application/x-gst-check");
49   gst_check_setup_events (mysrcpad, appsink, caps, GST_FORMAT_TIME);
50   gst_caps_unref (caps);
51 
52   return appsink;
53 }
54 
55 static void
cleanup_appsink(GstElement * appsink)56 cleanup_appsink (GstElement * appsink)
57 {
58   GST_DEBUG ("cleanup_appsink");
59 
60   gst_check_teardown_src_pad (appsink);
61   gst_check_teardown_element (appsink);
62 }
63 
64 /* This function does an operation to it's indata argument and returns it.
65  * The exact operation performed doesn't matter. Currently it multiplies with
66  * two, but it could do anything. The idea is to use the function to verify
67  * that the code calling it gets run. */
68 static gint
operate_on_data(gint indata)69 operate_on_data (gint indata)
70 {
71   return indata * 2;
72 }
73 
74 static GstFlowReturn
callback_function(GstAppSink * appsink,gpointer callback_data)75 callback_function (GstAppSink * appsink, gpointer callback_data)
76 {
77   global_testdata = operate_on_data (*((gint *) callback_data));
78 
79   return GST_FLOW_OK;
80 }
81 
82 static void
notify_function(gpointer callback_data)83 notify_function (gpointer callback_data)
84 {
85   global_testdata = operate_on_data (*((gint *) callback_data));
86 }
87 
GST_START_TEST(test_non_clients)88 GST_START_TEST (test_non_clients)
89 {
90   GstElement *sink;
91   GstBuffer *buffer;
92 
93   sink = setup_appsink ();
94 
95   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
96 
97   buffer = gst_buffer_new_and_alloc (4);
98   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
99 
100   GST_DEBUG ("cleaning up appsink");
101   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
102   cleanup_appsink (sink);
103 }
104 
105 GST_END_TEST;
106 
107 /* Verifies that the handoff callback gets run one time when passing a buffer */
GST_START_TEST(test_handoff_callback)108 GST_START_TEST (test_handoff_callback)
109 {
110   GstElement *sink;
111   GstBuffer *buffer;
112   gint testdata;
113   GstAppSinkCallbacks callbacks = { NULL };
114 
115   sink = setup_appsink ();
116 
117   global_testdata = 0;
118   testdata = 5;                 /* Arbitrary value */
119 
120   callbacks.new_sample = callback_function;
121 
122   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
123 
124   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
125 
126   buffer = gst_buffer_new_and_alloc (4);
127   /* Pushing a buffer should run our callback */
128   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
129 
130   testdata = operate_on_data (testdata);
131 
132   /* If both test_data & global_testdata have been operated on, we're happy. */
133   fail_unless (testdata == global_testdata);
134 
135   GST_DEBUG ("cleaning up appsink");
136   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
137   cleanup_appsink (sink);
138 }
139 
140 GST_END_TEST;
141 
142 /* Verifies that the notify function gets executed when the sink is destroyed */
GST_START_TEST(test_notify0)143 GST_START_TEST (test_notify0)
144 {
145   GstElement *sink;
146   gint testdata;
147   GstAppSinkCallbacks callbacks = { NULL };
148 
149   sink = gst_element_factory_make ("appsink", NULL);
150 
151   global_testdata = 0;
152   testdata = 17;                /* Arbitrary value */
153 
154   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
155       &testdata, (*notify_function));
156 
157   GST_DEBUG ("cleaning up appsink");
158   /* Destroying sink should call our notify_function */
159   gst_object_unref (sink);
160 
161   testdata = operate_on_data (testdata);
162 
163   /* If both test_data & global_testdata have been operated on, we're happy. */
164   fail_unless (testdata == global_testdata);
165 }
166 
167 GST_END_TEST;
168 
169 
170 /* Verifies that the notify function gets executed when
171  * gst_app_sink_set_callbacks () gets called */
GST_START_TEST(test_notify1)172 GST_START_TEST (test_notify1)
173 {
174   GstElement *sink;
175   gint testdata;
176   GstAppSinkCallbacks callbacks = { NULL };
177 
178   sink = gst_element_factory_make ("appsink", NULL);
179 
180   global_testdata = 0;
181   testdata = 42;                /* Arbitrary value */
182 
183   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks,
184       &testdata, (*notify_function));
185   /* Setting new callbacks should trigger the destroy of the old data */
186   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &testdata, NULL);
187 
188   testdata = operate_on_data (testdata);
189 
190   /* If both test_data & global_testdata have been operated on, we're happy. */
191   fail_unless (testdata == global_testdata);
192 
193   GST_DEBUG ("cleaning up appsink");
194   gst_object_unref (sink);
195 }
196 
197 GST_END_TEST;
198 
199 static const gint values[] = { 1, 2, 4 };
200 
201 static GstBufferList *
create_buffer_list(void)202 create_buffer_list (void)
203 {
204   guint len;
205   GstBuffer *buffer;
206   GstBufferList *mylist;
207 
208   mylist = gst_buffer_list_new ();
209   fail_if (mylist == NULL);
210 
211   len = gst_buffer_list_length (mylist);
212   fail_if (len != 0);
213 
214   buffer = gst_buffer_new_and_alloc (sizeof (gint));
215   gst_buffer_fill (buffer, 0, &values[0], sizeof (gint));
216   gst_buffer_list_add (mylist, buffer);
217 
218   buffer = gst_buffer_new_and_alloc (sizeof (gint));
219   gst_buffer_fill (buffer, 0, &values[1], sizeof (gint));
220   gst_buffer_list_add (mylist, buffer);
221 
222   buffer = gst_buffer_new_and_alloc (sizeof (gint));
223   gst_buffer_fill (buffer, 0, &values[2], sizeof (gint));
224   gst_buffer_list_add (mylist, buffer);
225 
226   return mylist;
227 }
228 
229 static GstFlowReturn
callback_function_sample_fallback(GstAppSink * appsink,gpointer p_counter)230 callback_function_sample_fallback (GstAppSink * appsink, gpointer p_counter)
231 {
232   GstSample *sample;
233   GstBuffer *buf;
234   gint *p_int_counter = p_counter;
235 
236   sample = gst_app_sink_pull_sample (appsink);
237   buf = gst_sample_get_buffer (sample);
238   fail_unless (GST_IS_BUFFER (buf));
239 
240   /* buffer list has 3 buffers in two groups */
241   switch (*p_int_counter) {
242     case 0:
243       fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
244       gst_check_buffer_data (buf, &values[0], sizeof (gint));
245       break;
246     case 1:
247       fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
248       gst_check_buffer_data (buf, &values[1], sizeof (gint));
249       break;
250     case 2:
251       fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
252       gst_check_buffer_data (buf, &values[2], sizeof (gint));
253       break;
254     default:
255       g_warn_if_reached ();
256       break;
257   }
258 
259   gst_sample_unref (sample);
260 
261   *p_int_counter += 1;
262 
263   return GST_FLOW_OK;
264 }
265 
266 static GstFlowReturn
callback_function_sample(GstAppSink * appsink,gpointer p_counter)267 callback_function_sample (GstAppSink * appsink, gpointer p_counter)
268 {
269   GstSample *sample;
270   GstBufferList *list;
271   gint *p_int_counter = p_counter;
272   guint len;
273   gint i;
274 
275   sample = gst_app_sink_pull_sample (appsink);
276   list = gst_sample_get_buffer_list (sample);
277   fail_unless (GST_IS_BUFFER_LIST (list));
278   len = gst_buffer_list_length (list);
279   fail_unless_equals_int (len, 3);
280 
281   for (i = 0; i < len; i++) {
282     GstBuffer *buf = gst_buffer_list_get (list, i);
283     fail_unless_equals_int (gst_buffer_get_size (buf), sizeof (gint));
284     gst_check_buffer_data (buf, &values[i], sizeof (gint));
285   }
286 
287   gst_sample_unref (sample);
288 
289   *p_int_counter += 1;
290 
291   return GST_FLOW_OK;
292 }
293 
GST_START_TEST(test_buffer_list_fallback)294 GST_START_TEST (test_buffer_list_fallback)
295 {
296   GstElement *sink;
297   GstBufferList *list;
298   GstAppSinkCallbacks callbacks = { NULL };
299   gint counter = 0;
300   gboolean buffer_list_support;
301 
302   sink = setup_appsink ();
303 
304   /* verify that the buffer list support is disabled per default */
305   g_object_get (sink, "buffer-list", &buffer_list_support, NULL);
306   fail_unless (buffer_list_support == FALSE);
307 
308 
309   callbacks.new_sample = callback_function_sample_fallback;
310 
311   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &counter, NULL);
312 
313   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
314 
315   list = create_buffer_list ();
316   fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
317 
318   fail_unless_equals_int (counter, 3);
319 
320   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
321   cleanup_appsink (sink);
322 }
323 
324 GST_END_TEST;
325 
GST_START_TEST(test_buffer_list_support)326 GST_START_TEST (test_buffer_list_support)
327 {
328   GstElement *sink;
329   GstBufferList *list;
330   GstAppSinkCallbacks callbacks = { NULL };
331   gint counter = 0;
332 
333   sink = setup_appsink ();
334 
335   /* enable buffer list support */
336   g_object_set (sink, "buffer-list", TRUE, NULL);
337 
338   callbacks.new_sample = callback_function_sample;
339 
340   gst_app_sink_set_callbacks (GST_APP_SINK (sink), &callbacks, &counter, NULL);
341 
342   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
343 
344   list = create_buffer_list ();
345   fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
346 
347   fail_unless_equals_int (counter, 1);
348 
349   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
350   cleanup_appsink (sink);
351 }
352 
353 GST_END_TEST;
354 
GST_START_TEST(test_buffer_list_fallback_signal)355 GST_START_TEST (test_buffer_list_fallback_signal)
356 {
357   GstElement *sink;
358   GstBufferList *list;
359   gint counter = 0;
360 
361   sink = setup_appsink ();
362 
363   /* C calling convention to the rescue.. */
364   g_signal_connect (sink, "new-sample",
365       G_CALLBACK (callback_function_sample_fallback), &counter);
366 
367   g_object_set (sink, "emit-signals", TRUE, NULL);
368 
369   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
370 
371   list = create_buffer_list ();
372   fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
373 
374   fail_unless_equals_int (counter, 3);
375 
376   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
377   cleanup_appsink (sink);
378 }
379 
380 GST_END_TEST;
381 
GST_START_TEST(test_buffer_list_signal)382 GST_START_TEST (test_buffer_list_signal)
383 {
384   GstElement *sink;
385   GstBufferList *list;
386   gint counter = 0;
387 
388   sink = setup_appsink ();
389 
390   /* enable buffer list support */
391   g_object_set (sink, "buffer-list", TRUE, NULL);
392 
393   /* C calling convention to the rescue.. */
394   g_signal_connect (sink, "new-sample", G_CALLBACK (callback_function_sample),
395       &counter);
396 
397   g_object_set (sink, "emit-signals", TRUE, NULL);
398 
399   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
400 
401   list = create_buffer_list ();
402   fail_unless (gst_pad_push_list (mysrcpad, list) == GST_FLOW_OK);
403 
404   fail_unless_equals_int (counter, 1);
405 
406   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
407   cleanup_appsink (sink);
408 }
409 
410 GST_END_TEST;
411 
GST_START_TEST(test_segment)412 GST_START_TEST (test_segment)
413 {
414   GstElement *sink;
415   GstSegment segment;
416   GstBuffer *buffer;
417   GstSample *pulled_preroll;
418   GstSample *pulled_sample;
419 
420   sink = setup_appsink ();
421 
422   gst_segment_init (&segment, GST_FORMAT_TIME);
423   segment.start = 2 * GST_SECOND;
424   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
425 
426   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
427 
428   buffer = gst_buffer_new_and_alloc (4);
429   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
430 
431   g_signal_emit_by_name (sink, "pull-preroll", &pulled_preroll);
432   fail_unless (gst_segment_is_equal (&segment,
433           gst_sample_get_segment (pulled_preroll)));
434   gst_sample_unref (pulled_preroll);
435 
436   g_signal_emit_by_name (sink, "pull-sample", &pulled_sample);
437   fail_unless (gst_segment_is_equal (&segment,
438           gst_sample_get_segment (pulled_sample)));
439   gst_sample_unref (pulled_sample);
440 
441   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
442   cleanup_appsink (sink);
443 }
444 
445 GST_END_TEST;
446 
GST_START_TEST(test_pull_with_timeout)447 GST_START_TEST (test_pull_with_timeout)
448 {
449   GstElement *sink;
450   GstBuffer *buffer;
451   GstSample *s;
452   guint64 t1, tdiff;
453 
454   sink = setup_appsink ();
455 
456   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
457 
458   /* Check that it actually waits for a bit */
459   t1 = gst_util_get_timestamp ();
460   s = gst_app_sink_try_pull_preroll (GST_APP_SINK (sink), GST_SECOND / 20);
461   tdiff = gst_util_get_timestamp () - t1;
462   GST_LOG ("tdiff: %" GST_TIME_FORMAT, GST_TIME_ARGS (tdiff));
463   fail_unless (s == NULL);
464   fail_unless (tdiff > (GST_SECOND / (20 * 2)));
465 
466   buffer = gst_buffer_new_and_alloc (4);
467   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
468 
469   s = gst_app_sink_try_pull_preroll (GST_APP_SINK (sink), GST_SECOND / 20);
470   fail_unless (s != NULL);
471   gst_sample_unref (s);
472 
473   s = gst_app_sink_try_pull_sample (GST_APP_SINK (sink), 500 * GST_SECOND);
474   fail_unless (s != NULL);
475   gst_sample_unref (s);
476 
477   /* No waiting */
478   s = gst_app_sink_try_pull_sample (GST_APP_SINK (sink), 0);
479   fail_unless (s == NULL);
480 
481   /* Check that it actually waits for a bit */
482   t1 = gst_util_get_timestamp ();
483   s = gst_app_sink_try_pull_sample (GST_APP_SINK (sink), GST_SECOND / 20);
484   tdiff = gst_util_get_timestamp () - t1;
485   GST_LOG ("tdiff: %" GST_TIME_FORMAT, GST_TIME_ARGS (tdiff));
486   fail_unless (s == NULL);
487   fail_unless (tdiff > (GST_SECOND / (20 * 2)));
488 
489   /* No waiting, with buffer pending */
490   buffer = gst_buffer_new_and_alloc (5);
491   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
492   s = gst_app_sink_try_pull_sample (GST_APP_SINK (sink), 0);
493   fail_unless (s != NULL);
494   gst_sample_unref (s);
495 
496   /* With timeout, with buffer pending */
497   buffer = gst_buffer_new_and_alloc (6);
498   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
499   s = gst_app_sink_try_pull_sample (GST_APP_SINK (sink), GST_SECOND / 20);
500   fail_unless (s != NULL);
501   gst_sample_unref (s);
502 
503   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
504   cleanup_appsink (sink);
505 }
506 
507 GST_END_TEST;
508 
GST_START_TEST(test_pull_preroll)509 GST_START_TEST (test_pull_preroll)
510 {
511   GstElement *sink = NULL;
512   GstBuffer *buffer = NULL;
513   GstSample *pulled_preroll = NULL;
514 
515   sink = setup_appsink ();
516 
517   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
518 
519   buffer = gst_buffer_new_and_alloc (4);
520   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
521 
522   pulled_preroll = gst_app_sink_pull_preroll (GST_APP_SINK (sink));
523   fail_unless (pulled_preroll);
524   gst_sample_unref (pulled_preroll);
525 
526   fail_if (gst_app_sink_try_pull_preroll (GST_APP_SINK (sink), 0));
527 
528   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
529   cleanup_appsink (sink);
530 }
531 
532 GST_END_TEST;
533 
GST_START_TEST(test_do_not_care_preroll)534 GST_START_TEST (test_do_not_care_preroll)
535 {
536   GstElement *sink = NULL;
537   GstBuffer *buffer = NULL;
538   GstSample *pulled_sample = NULL;
539 
540   sink = setup_appsink ();
541 
542   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
543 
544   buffer = gst_buffer_new_and_alloc (4);
545   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
546 
547   pulled_sample = gst_app_sink_pull_sample (GST_APP_SINK (sink));
548   fail_unless (pulled_sample);
549   gst_sample_unref (pulled_sample);
550 
551   fail_if (gst_app_sink_try_pull_preroll (GST_APP_SINK (sink), 0));
552 
553   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
554   cleanup_appsink (sink);
555 }
556 
557 GST_END_TEST;
558 
559 typedef struct
560 {
561   GMutex mutex;
562   GCond cond;
563   GstAppSink *appsink;
564   gboolean check;
565 } TestQueryDrainContext;
566 
567 #define TEST_QUERY_DRAIN_CONTEXT_INIT { { 0, }, }
568 
569 static gpointer
my_app_thread(TestQueryDrainContext * ctx)570 my_app_thread (TestQueryDrainContext * ctx)
571 {
572   GstSample *pulled_preroll = NULL;
573   GstSample *pulled_sample = NULL;
574 
575   /* Wait for the query to reach appsink. */
576   g_mutex_lock (&ctx->mutex);
577   while (!ctx->check)
578     g_cond_wait (&ctx->cond, &ctx->mutex);
579   g_mutex_unlock (&ctx->mutex);
580 
581   pulled_preroll = gst_app_sink_pull_preroll (ctx->appsink);
582   fail_unless (pulled_preroll);
583   gst_sample_unref (pulled_preroll);
584 
585   pulled_sample = gst_app_sink_pull_sample (ctx->appsink);
586   fail_unless (pulled_sample);
587   gst_sample_unref (pulled_sample);
588 
589   pulled_sample = gst_app_sink_pull_sample (ctx->appsink);
590   fail_unless (pulled_sample);
591   gst_sample_unref (pulled_sample);
592 
593   return NULL;
594 }
595 
596 static GstPadProbeReturn
query_handler(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)597 query_handler (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
598 {
599   GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
600   TestQueryDrainContext *ctx = (TestQueryDrainContext *) user_data;
601   switch (GST_QUERY_TYPE (query)) {
602     case GST_QUERY_DRAIN:
603     {
604       if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PUSH) {
605         g_mutex_lock (&ctx->mutex);
606         ctx->check = TRUE;
607         g_cond_signal (&ctx->cond);
608         g_mutex_unlock (&ctx->mutex);
609       } else if (GST_PAD_PROBE_INFO_TYPE (info) & GST_PAD_PROBE_TYPE_PULL) {
610         /* Check that there is no pending buffers when drain query is done. */
611         fail_if (gst_app_sink_try_pull_preroll (ctx->appsink, 0));
612         fail_if (gst_app_sink_try_pull_sample (ctx->appsink, 0));
613       }
614       break;
615     }
616     default:
617       break;
618   }
619   return GST_PAD_PROBE_OK;
620 }
621 
GST_START_TEST(test_query_drain)622 GST_START_TEST (test_query_drain)
623 {
624   GstElement *sink = NULL;
625   GstBuffer *buffer = NULL;
626   GstPad *sinkpad = NULL;
627   GThread *thread = NULL;
628   GstQuery *query = NULL;
629   TestQueryDrainContext ctx = TEST_QUERY_DRAIN_CONTEXT_INIT;
630 
631   sink = setup_appsink ();
632 
633   g_mutex_init (&ctx.mutex);
634   g_cond_init (&ctx.cond);
635   ctx.appsink = GST_APP_SINK (sink);
636   ctx.check = FALSE;
637 
638   sinkpad = gst_element_get_static_pad (sink, "sink");
639   gst_pad_add_probe (sinkpad, GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
640       query_handler, (gpointer) & ctx, NULL);
641   gst_object_unref (sinkpad);
642 
643   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
644 
645   buffer = gst_buffer_new_and_alloc (4);
646   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
647 
648   buffer = gst_buffer_new_and_alloc (4);
649   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
650 
651   thread = g_thread_new ("appthread", (GThreadFunc) my_app_thread, &ctx);
652   fail_unless (thread != NULL);
653 
654   query = gst_query_new_drain ();
655   fail_unless (gst_pad_peer_query (mysrcpad, query));
656   gst_query_unref (query);
657 
658   g_thread_join (thread);
659 
660   g_mutex_clear (&ctx.mutex);
661   g_cond_clear (&ctx.cond);
662 
663   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
664   cleanup_appsink (sink);
665 }
666 
667 GST_END_TEST;
668 
GST_START_TEST(test_pull_sample_refcounts)669 GST_START_TEST (test_pull_sample_refcounts)
670 {
671   GstElement *sink;
672   GstBuffer *buffer;
673   GstSample *s1, *s2, *s3;
674 
675   sink = setup_appsink ();
676 
677   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
678 
679   buffer = gst_buffer_new_and_alloc (4);
680   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
681 
682   s1 = gst_app_sink_pull_sample (GST_APP_SINK (sink));
683   fail_unless (s1 != NULL);
684   fail_unless (gst_buffer_get_size (gst_sample_get_buffer (s1)) == 4);
685   gst_sample_unref (s1);
686 
687   buffer = gst_buffer_new_and_alloc (6);
688   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
689   s2 = gst_app_sink_pull_sample (GST_APP_SINK (sink));
690   fail_unless (s2 != NULL);
691   fail_unless (gst_buffer_get_size (gst_sample_get_buffer (s2)) == 6);
692 
693   /* We unreffed s1, appsink should thus reuse the same sample,
694    * avoiding an extra allocation */
695   fail_unless (s1 == s2);
696 
697   buffer = gst_buffer_new_and_alloc (8);
698   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
699   s3 = gst_app_sink_pull_sample (GST_APP_SINK (sink));
700   fail_unless (s3 != NULL);
701   fail_unless (gst_buffer_get_size (gst_sample_get_buffer (s2)) == 6);
702   fail_unless (gst_buffer_get_size (gst_sample_get_buffer (s3)) == 8);
703 
704 
705   /* We didn't unref s2, appsink should thus have created a new sample */
706   fail_unless (s2 != s3);
707 
708   gst_sample_unref (s2);
709   gst_sample_unref (s3);
710 
711   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
712   cleanup_appsink (sink);
713 }
714 
715 GST_END_TEST;
716 
717 static gboolean
new_event_cb(GstAppSink * appsink,gpointer callback_data)718 new_event_cb (GstAppSink * appsink, gpointer callback_data)
719 {
720   guint *new_event_count = callback_data;
721   *new_event_count += 1;
722   return TRUE;
723 }
724 
725 /* Verifies that the event callback is called */
GST_START_TEST(test_event_callback)726 GST_START_TEST (test_event_callback)
727 {
728   GstElement *sink;
729   GstPad *sinkpad;
730   GstBuffer *buffer;
731   guint new_event_count;
732   GstAppSinkCallbacks callbacks = { NULL };
733   GstMiniObject *object;
734   GstAppSink *app_sink;
735 
736   sink = setup_appsink ();
737   app_sink = GST_APP_SINK (sink);
738 
739   callbacks.new_event = new_event_cb;
740 
741   gst_app_sink_set_callbacks (app_sink, &callbacks, &new_event_count, NULL);
742 
743   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
744 
745   /* push a buffer so pending events are pushed */
746   buffer = gst_buffer_new_and_alloc (4);
747   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
748 
749   /* flush pending events from the queue */
750   while ((object = gst_app_sink_try_pull_object (app_sink, 0)))
751     gst_mini_object_unref (object);
752   new_event_count = 0;
753 
754   /* push a buffer */
755   buffer = gst_buffer_new_and_alloc (4);
756   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
757 
758   /* push custom event */
759   sinkpad = gst_element_get_static_pad (sink, "sink");
760   fail_unless (sinkpad);
761   fail_unless (gst_pad_send_event (sinkpad,
762           gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
763               gst_structure_new ("custom", NULL, NULL))));
764   fail_unless_equals_int (new_event_count, 1);
765   gst_object_unref (sinkpad);
766 
767   /* push a second buffer */
768   buffer = gst_buffer_new_and_alloc (4);
769   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
770 
771   /* check if the samples and events are pulled in the right order */
772   object = gst_app_sink_pull_object (app_sink);
773   fail_unless (GST_IS_SAMPLE (object));
774   gst_mini_object_unref (object);
775 
776   object = gst_app_sink_pull_object (app_sink);
777   fail_unless (GST_IS_EVENT (object));
778   fail_unless_equals_int (GST_EVENT_TYPE (object), GST_EVENT_CUSTOM_DOWNSTREAM);
779   gst_mini_object_unref (object);
780 
781   object = gst_app_sink_pull_object (app_sink);
782   fail_unless (GST_IS_SAMPLE (object));
783   gst_mini_object_unref (object);
784 
785   GST_DEBUG ("cleaning up appsink");
786   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
787   cleanup_appsink (sink);
788 }
789 
790 GST_END_TEST;
791 
792 
GST_START_TEST(test_event_signals)793 GST_START_TEST (test_event_signals)
794 {
795   GstElement *sink;
796   GstPad *sinkpad;
797   GstBuffer *buffer;
798   GstMiniObject *object;
799   GstAppSink *app_sink;
800   guint new_event_count = 0;
801 
802   sink = setup_appsink ();
803   app_sink = GST_APP_SINK (sink);
804 
805   g_object_set (sink, "emit-signals", TRUE, NULL);
806 
807   g_signal_connect (sink, "new-serialized-event", G_CALLBACK (new_event_cb),
808       &new_event_count);
809 
810   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
811 
812   /* push a buffer so pending events are pushed */
813   buffer = gst_buffer_new_and_alloc (4);
814   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
815 
816   /* flush pending events from the queue */
817   while ((object = gst_app_sink_try_pull_object (app_sink, 0)))
818     gst_mini_object_unref (object);
819   new_event_count = 0;
820 
821   /* push a buffer */
822   buffer = gst_buffer_new_and_alloc (4);
823   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
824 
825   /* push custom event */
826   sinkpad = gst_element_get_static_pad (sink, "sink");
827   fail_unless (sinkpad);
828   fail_unless (gst_pad_send_event (sinkpad,
829           gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
830               gst_structure_new ("custom", NULL, NULL))));
831   fail_unless_equals_int (new_event_count, 1);
832   gst_object_unref (sinkpad);
833 
834   /* push a second buffer */
835   buffer = gst_buffer_new_and_alloc (4);
836   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
837 
838   /* check if the buffers and events are pulled in the right order */
839   g_signal_emit_by_name (sink, "try-pull-object", GST_CLOCK_TIME_NONE, &object);
840   fail_unless (GST_IS_SAMPLE (object));
841   gst_mini_object_unref (object);
842 
843   g_signal_emit_by_name (sink, "try-pull-object", GST_CLOCK_TIME_NONE, &object);
844   fail_unless (GST_IS_EVENT (object));
845   fail_unless_equals_int (GST_EVENT_TYPE (object), GST_EVENT_CUSTOM_DOWNSTREAM);
846   gst_mini_object_unref (object);
847 
848   g_signal_emit_by_name (sink, "try-pull-object", GST_CLOCK_TIME_NONE, &object);
849   fail_unless (GST_IS_SAMPLE (object));
850   gst_mini_object_unref (object);
851 
852   GST_DEBUG ("cleaning up appsink");
853   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
854   cleanup_appsink (sink);
855 
856 }
857 
858 GST_END_TEST;
859 
860 /* try pulling events when appsink is in PAUSED */
GST_START_TEST(test_event_paused)861 GST_START_TEST (test_event_paused)
862 {
863   GstElement *sink;
864   guint new_event_count = 0;
865   GstAppSinkCallbacks callbacks = { NULL };
866   GstMiniObject *object;
867   GstAppSink *app_sink;
868   GstCaps *caps;
869 
870   sink = setup_appsink ();
871   app_sink = GST_APP_SINK (sink);
872 
873   callbacks.new_event = new_event_cb;
874 
875   gst_app_sink_set_callbacks (app_sink, &callbacks, &new_event_count, NULL);
876 
877   ASSERT_SET_STATE (sink, GST_STATE_PAUSED, GST_STATE_CHANGE_ASYNC);
878 
879   /* push a couple of events while in PAUSED */
880   gst_pad_push_event (mysrcpad, gst_event_new_stream_start ("test"));
881   caps = gst_caps_new_simple ("audio/x-raw", NULL, NULL);
882   gst_pad_push_event (mysrcpad, gst_event_new_caps (caps));
883   gst_caps_unref (caps);
884 
885   fail_unless_equals_int (new_event_count, 2);
886 
887   /* check pulled events */
888   object = gst_app_sink_pull_object (app_sink);
889   fail_unless (GST_IS_EVENT (object));
890   fail_unless_equals_int (GST_EVENT_TYPE (object), GST_EVENT_STREAM_START);
891   gst_mini_object_unref (object);
892 
893   object = gst_app_sink_pull_object (app_sink);
894   fail_unless (GST_IS_EVENT (object));
895   fail_unless_equals_int (GST_EVENT_TYPE (object), GST_EVENT_CAPS);
896   gst_mini_object_unref (object);
897 
898   object = gst_app_sink_try_pull_object (app_sink, 0);
899   fail_if (object);
900 
901   GST_DEBUG ("cleaning up appsink");
902   ASSERT_SET_STATE (sink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
903   cleanup_appsink (sink);
904 }
905 
906 GST_END_TEST;
907 
GST_START_TEST(test_reverse_stepping)908 GST_START_TEST (test_reverse_stepping)
909 {
910   GstElement *pipeline;
911   GstStateChangeReturn state_ret;
912   GstState state = GST_STATE_NULL;
913   gboolean ret;
914   GstEvent *event;
915   GstAppSink *sink;
916   GstSample *sample;
917   GstBuffer *buffer;
918   GstClockTime running_time;
919 
920   pipeline =
921       gst_parse_launch ("videotestsrc name=src ! video/x-raw,framerate=1/1 "
922       "! appsink name=sink max-buffers=1", NULL);
923   fail_unless (pipeline != NULL);
924 
925   sink = (GstAppSink *) gst_bin_get_by_name (GST_BIN (pipeline), "sink");
926   fail_unless (sink != NULL);
927 
928   /* Pause and ensure preroll */
929   state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
930   fail_unless (state_ret != GST_STATE_CHANGE_FAILURE);
931 
932   state_ret =
933       gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
934   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
935   fail_unless (state == GST_STATE_PAUSED);
936 
937   ret = gst_element_seek (pipeline, -1.0, GST_FORMAT_TIME,
938       GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_NONE,
939       -1, GST_SEEK_TYPE_SET, 10 * GST_SECOND);
940   fail_unless (ret != FALSE);
941 
942   state_ret =
943       gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
944   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
945   fail_unless (state == GST_STATE_PAUSED);
946 
947   sample = gst_app_sink_pull_preroll (sink);
948   fail_unless (GST_IS_SAMPLE (sample));
949   buffer = gst_sample_get_buffer (sample);
950   fail_unless (GST_IS_BUFFER (buffer));
951 
952   /* start running time */
953   running_time = GST_BUFFER_PTS (buffer);
954   gst_sample_unref (sample);
955 
956   do {
957     /* timestamp of new preroll buffer should be
958      * "previous running time - buffer duration"
959      */
960     running_time -= GST_SECOND;
961     event = gst_event_new_step (GST_FORMAT_BUFFERS, 1, 1.0, TRUE, FALSE);
962     ret = gst_element_send_event (pipeline, event);
963     fail_unless (ret);
964     state_ret =
965         gst_element_get_state (pipeline, &state, NULL, GST_CLOCK_TIME_NONE);
966     fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
967     fail_unless (state == GST_STATE_PAUSED);
968 
969     sample = gst_app_sink_pull_preroll (sink);
970     /* EOS */
971     if (!sample)
972       break;
973 
974     fail_unless (GST_IS_SAMPLE (sample));
975     buffer = gst_sample_get_buffer (sample);
976     fail_unless (GST_IS_BUFFER (buffer));
977     fail_unless_equals_uint64 (running_time, GST_BUFFER_PTS (buffer));
978     gst_sample_unref (sample);
979   } while (sample);
980 
981   state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
982   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
983 
984   gst_object_unref (sink);
985   gst_object_unref (pipeline);
986 }
987 
988 GST_END_TEST;
989 
990 static void
push_caps_with_type(gint caps_type)991 push_caps_with_type (gint caps_type)
992 {
993   GstCaps *caps;
994 
995   caps =
996       gst_caps_new_simple ("application/x-gst-check", "type", G_TYPE_INT,
997       caps_type, NULL);
998   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_caps (caps)));
999 
1000   gst_caps_unref (caps);
1001 }
1002 
1003 static void
push_buffer_with_number(gint buffer_number)1004 push_buffer_with_number (gint buffer_number)
1005 {
1006   GstBuffer *buffer;
1007 
1008   buffer = gst_buffer_new_and_alloc (sizeof (gint));
1009   gst_buffer_fill (buffer, 0, &buffer_number, sizeof (gint));
1010   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1011 }
1012 
1013 static void
pull_and_check_sample(GstElement * appsink,gint expected_buffer_number,gint expected_caps_type)1014 pull_and_check_sample (GstElement * appsink, gint expected_buffer_number,
1015     gint expected_caps_type)
1016 {
1017   GstSample *sample;
1018   GstCaps *caps;
1019   GstBuffer *buffer;
1020   GstStructure *structure;
1021   gint actual_caps_type;
1022 
1023   sample = gst_app_sink_pull_sample (GST_APP_SINK (appsink));
1024 
1025   caps = gst_sample_get_caps (sample);
1026   fail_unless (structure = gst_caps_get_structure (caps, 0));
1027   fail_unless (gst_structure_get_int (structure, "type", &actual_caps_type));
1028   assert_equals_int (actual_caps_type, expected_caps_type);
1029 
1030   buffer = gst_sample_get_buffer (sample);
1031   gst_check_buffer_data (buffer, &expected_buffer_number, sizeof (gint));
1032 
1033   gst_sample_unref (sample);
1034 }
1035 
GST_START_TEST(test_caps_before_flush_race_condition)1036 GST_START_TEST (test_caps_before_flush_race_condition)
1037 {
1038   GstElement *sink;
1039   GstSegment segment;
1040 
1041   sink = setup_appsink ();
1042 
1043   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
1044 
1045   // Push a series of buffers, and at the end, a new caps event.
1046   push_caps_with_type (1);
1047   push_buffer_with_number (10);
1048   push_buffer_with_number (11);
1049   push_caps_with_type (2);
1050 
1051   pull_and_check_sample (sink, 10, 1);
1052 
1053   // Then, let a flush happen.
1054   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_start ()));
1055   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_stop (TRUE)));
1056   // Sinks downgrade state to PAUSED after a flush, let's up it to PLAYING again to avoid gst_pad_push becoming blocking.
1057   ASSERT_SET_STATE (sink, GST_STATE_PLAYING, GST_STATE_CHANGE_ASYNC);
1058   // A segment must be sent after a flush.
1059   gst_segment_init (&segment, GST_FORMAT_TIME);
1060   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
1061 
1062   // Send a buffer now, and check that when pulled by the appsink user, it didn't come with the wrong old caps.
1063   push_buffer_with_number (20);
1064   pull_and_check_sample (sink, 20, 2);
1065 
1066   cleanup_appsink (sink);
1067 }
1068 
1069 GST_END_TEST;
1070 
1071 static Suite *
appsink_suite(void)1072 appsink_suite (void)
1073 {
1074   Suite *s = suite_create ("appsink");
1075   TCase *tc_chain = tcase_create ("general");
1076 
1077   suite_add_tcase (s, tc_chain);
1078   tcase_add_test (tc_chain, test_non_clients);
1079   tcase_add_test (tc_chain, test_handoff_callback);
1080   tcase_add_test (tc_chain, test_notify0);
1081   tcase_add_test (tc_chain, test_notify1);
1082   tcase_add_test (tc_chain, test_buffer_list_fallback);
1083   tcase_add_test (tc_chain, test_buffer_list_support);
1084   tcase_add_test (tc_chain, test_buffer_list_fallback_signal);
1085   tcase_add_test (tc_chain, test_buffer_list_signal);
1086   tcase_add_test (tc_chain, test_segment);
1087   tcase_add_test (tc_chain, test_pull_with_timeout);
1088   tcase_add_test (tc_chain, test_query_drain);
1089   tcase_add_test (tc_chain, test_pull_preroll);
1090   tcase_add_test (tc_chain, test_do_not_care_preroll);
1091   tcase_add_test (tc_chain, test_pull_sample_refcounts);
1092   tcase_add_test (tc_chain, test_event_callback);
1093   tcase_add_test (tc_chain, test_event_signals);
1094   tcase_add_test (tc_chain, test_event_paused);
1095   tcase_add_test (tc_chain, test_reverse_stepping);
1096   tcase_add_test (tc_chain, test_caps_before_flush_race_condition);
1097 
1098   return s;
1099 }
1100 
1101 GST_CHECK_MAIN (appsink);
1102