• 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 Suite *
appsink_suite(void)718 appsink_suite (void)
719 {
720   Suite *s = suite_create ("appsink");
721   TCase *tc_chain = tcase_create ("general");
722 
723   suite_add_tcase (s, tc_chain);
724   tcase_add_test (tc_chain, test_non_clients);
725   tcase_add_test (tc_chain, test_handoff_callback);
726   tcase_add_test (tc_chain, test_notify0);
727   tcase_add_test (tc_chain, test_notify1);
728   tcase_add_test (tc_chain, test_buffer_list_fallback);
729   tcase_add_test (tc_chain, test_buffer_list_support);
730   tcase_add_test (tc_chain, test_buffer_list_fallback_signal);
731   tcase_add_test (tc_chain, test_buffer_list_signal);
732   tcase_add_test (tc_chain, test_segment);
733   tcase_add_test (tc_chain, test_pull_with_timeout);
734   tcase_add_test (tc_chain, test_query_drain);
735   tcase_add_test (tc_chain, test_pull_preroll);
736   tcase_add_test (tc_chain, test_do_not_care_preroll);
737   tcase_add_test (tc_chain, test_pull_sample_refcounts);
738 
739   return s;
740 }
741 
742 GST_CHECK_MAIN (appsink);
743