Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / gstappsink.cpp
1 /* GStreamer
2  * Copyright (C) 2007 David Schleef <ds@schleef.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #if 1
20 #include "_highgui.h"
21 #include <gst/gst.h>
22 #include <gst/base/gstbasesink.h>
23 #include <gst/gstbuffer.h>
24
25 #include <string.h>
26
27 #include "gstappsink.h"
28
29 GST_DEBUG_CATEGORY (app_sink_debug);
30 #define GST_CAT_DEFAULT app_sink_debug
31
32 static const GstElementDetails app_sink_details =
33 GST_ELEMENT_DETAILS ((gchar*)"AppSink",
34     (gchar*)"Generic/Sink",
35     (gchar*)"Allow the application to get access to raw buffer",
36     (gchar*)"David Schleef <ds@schleef.org>, Wim Taymans <wim.taymans@gmail.com");
37
38 enum
39 {
40   /* signals */
41   SIGNAL_EOS,
42   SIGNAL_NEW_PREROLL,
43   SIGNAL_NEW_BUFFER,
44
45   /* acions */
46   SIGNAL_PULL_PREROLL,
47   SIGNAL_PULL_BUFFER,
48
49   LAST_SIGNAL
50 };
51
52 enum
53 {
54   PROP_0,
55   PROP_CAPS,
56   PROP_EOS
57 };
58
59 static GstStaticPadTemplate gst_app_sink_template =
60 GST_STATIC_PAD_TEMPLATE ("sink",
61     GST_PAD_SINK,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS_ANY);
64
65 static void gst_app_sink_dispose (GObject * object);
66 static void gst_app_sink_finalize (GObject * object);
67
68 static void gst_app_sink_set_property (GObject * object, guint prop_id,
69     const GValue * value, GParamSpec * pspec);
70 static void gst_app_sink_get_property (GObject * object, guint prop_id,
71     GValue * value, GParamSpec * pspec);
72
73 static gboolean gst_app_sink_start (GstBaseSink * psink);
74 static gboolean gst_app_sink_stop (GstBaseSink * psink);
75 static gboolean gst_app_sink_event (GstBaseSink * sink, GstEvent * event);
76 static GstFlowReturn gst_app_sink_preroll (GstBaseSink * psink,
77     GstBuffer * buffer);
78 static GstFlowReturn gst_app_sink_render (GstBaseSink * psink,
79     GstBuffer * buffer);
80 static GstCaps *gst_app_sink_getcaps (GstBaseSink * psink);
81
82 static guint gst_app_sink_signals[LAST_SIGNAL] = { 0 };
83
84 GST_BOILERPLATE (GstAppSink, gst_app_sink, GstBaseSink, GST_TYPE_BASE_SINK);
85
86 static gboolean
87 appsink_plugin_init (GstPlugin * plugin)
88 {
89   GST_DEBUG_CATEGORY_INIT (app_sink_debug, "opencv-appsink", 0, "Application sink");
90
91   if (!gst_element_register (plugin, "opencv-appsink", GST_RANK_PRIMARY,
92           gst_app_sink_get_type ()))
93     return FALSE;
94
95   return TRUE;
96 }
97
98 #undef PACKAGE
99 #define PACKAGE "highgui"
100 GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR, GST_VERSION_MINOR,
101         "opencv-appsink", "Element application sink",
102         appsink_plugin_init, "0.1", "LGPL", "OpenCV's internal copy of gstappsink", "OpenCV")
103
104 void
105 gst_app_marshal_OBJECT__VOID (GClosure * closure,
106     GValue * return_value,
107     guint n_param_values,
108     const GValue * param_values,
109     gpointer invocation_hint, gpointer marshal_data)
110 {
111   typedef GstBuffer *(*GMarshalFunc_OBJECT__VOID) (gpointer data1,
112       gpointer data2);
113   register GMarshalFunc_OBJECT__VOID callback;
114   register GCClosure *cc = (GCClosure *) closure;
115   register gpointer data1, data2;
116   GstBuffer *v_return;
117
118   //printf("appmarshalobject\n");
119
120   g_return_if_fail (return_value != NULL);
121   g_return_if_fail (n_param_values == 1);
122
123   if (G_CCLOSURE_SWAP_DATA (closure)) {
124     data1 = closure->data;
125     data2 = g_value_peek_pointer (param_values + 0);
126   } else {
127     data1 = g_value_peek_pointer (param_values + 0);
128     data2 = closure->data;
129   }
130   callback =
131       (GMarshalFunc_OBJECT__VOID) (marshal_data ? marshal_data : cc->callback);
132
133   v_return = callback (data1, data2);
134
135   gst_value_take_buffer (return_value, v_return);
136 }
137
138 static void
139 gst_app_sink_base_init (gpointer g_class)
140 {
141   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
142
143   //printf("appsinkbaseinit\n");
144
145   GST_DEBUG_CATEGORY_INIT (app_sink_debug, "appsink", 0, "appsink element");
146
147   gst_element_class_set_details (element_class, &app_sink_details);
148
149   gst_element_class_add_pad_template (element_class,
150       gst_static_pad_template_get (&gst_app_sink_template));
151 }
152
153 static void
154 gst_app_sink_class_init (GstAppSinkClass * klass)
155 {
156   GObjectClass *gobject_class = (GObjectClass *) klass;
157   GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
158
159   //printf("appsinkclassinit\n");
160
161   gobject_class->dispose = gst_app_sink_dispose;
162   gobject_class->finalize = gst_app_sink_finalize;
163
164   gobject_class->set_property = gst_app_sink_set_property;
165   gobject_class->get_property = gst_app_sink_get_property;
166
167   g_object_class_install_property (gobject_class, PROP_CAPS,
168       g_param_spec_boxed ("caps", "Caps",
169           "The caps of the sink pad", GST_TYPE_CAPS, (GParamFlags)G_PARAM_READWRITE));
170
171   g_object_class_install_property (gobject_class, PROP_EOS,
172       g_param_spec_boolean ("eos", "EOS",
173           "Check if the sink is EOS", TRUE, G_PARAM_READABLE));
174
175   /**
176    * GstAppSink::eos:
177    * @appsink: the appsink element that emitted the signal
178    *
179    * Signal that the end-of-stream has been reached.
180    */
181   gst_app_sink_signals[SIGNAL_EOS] =
182       g_signal_new ("eos", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
183       G_STRUCT_OFFSET (GstAppSinkClass, eos),
184       NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
185   /**
186    * GstAppSink::new-preroll:
187    * @appsink: the appsink element that emitted the signal
188    * @buffer: the buffer that caused the preroll
189    *
190    * Signal that a new preroll buffer is available.
191    */
192   gst_app_sink_signals[SIGNAL_NEW_PREROLL] =
193       g_signal_new ("new-preroll", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
194       G_STRUCT_OFFSET (GstAppSinkClass, new_preroll),
195       NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
196       GST_TYPE_BUFFER);
197   /**
198    * GstAppSink::new-buffer:
199    * @appsink: the appsink element that emitted the signal
200    * @buffer: the buffer that is available
201    *
202    * Signal that a new buffer is available.
203    */
204   gst_app_sink_signals[SIGNAL_NEW_BUFFER] =
205       g_signal_new ("new-buffer", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
206       G_STRUCT_OFFSET (GstAppSinkClass, new_buffer),
207       NULL, NULL, g_cclosure_marshal_VOID__UINT, G_TYPE_NONE, 1,
208       G_TYPE_UINT);
209
210   /**
211    * GstAppSink::pull-preroll:
212    * @appsink: the appsink element to emit this signal on
213    *
214    * Get the last preroll buffer on @appsink.
215    */
216   gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
217       g_signal_new ("pull-preroll", G_TYPE_FROM_CLASS (klass),
218       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAppSinkClass, pull_preroll), NULL,
219       NULL, gst_app_marshal_OBJECT__VOID, GST_TYPE_BUFFER, 0, G_TYPE_NONE);
220   /**
221    * GstAppSink::pull-buffer:
222    * @appsink: the appsink element to emit this signal on
223    *
224    * Get the next buffer buffer on @appsink.
225    */
226   gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
227       g_signal_new ("pull-buffer", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
228       G_STRUCT_OFFSET (GstAppSinkClass, pull_buffer),
229       NULL, NULL, gst_app_marshal_OBJECT__VOID, GST_TYPE_BUFFER, 0,
230       G_TYPE_NONE);
231
232   basesink_class->start = gst_app_sink_start;
233   basesink_class->stop = gst_app_sink_stop;
234   basesink_class->event = gst_app_sink_event;
235   basesink_class->preroll = gst_app_sink_preroll;
236   basesink_class->render = gst_app_sink_render;
237   basesink_class->get_caps = gst_app_sink_getcaps;
238
239   klass->pull_preroll = gst_app_sink_pull_preroll;
240   klass->pull_buffer = gst_app_sink_pull_buffer;
241 }
242
243 static void
244 gst_app_sink_init (GstAppSink * appsink, GstAppSinkClass * klass)
245 {
246   appsink->mutex = g_mutex_new ();
247   appsink->cond = g_cond_new ();
248   appsink->queue = g_queue_new ();
249 }
250
251 static void
252 gst_app_sink_dispose (GObject * obj)
253 {
254   GstAppSink *appsink = GST_APP_SINK (obj);
255   GstBuffer *buffer;
256
257   //printf("appsinkdispose\n");
258
259   if (appsink->caps) {
260     gst_caps_unref (appsink->caps);
261     appsink->caps = NULL;
262   }
263   if (appsink->preroll) {
264     gst_buffer_unref (appsink->preroll);
265     appsink->preroll = NULL;
266   }
267   g_mutex_lock (appsink->mutex);
268   while ((buffer = (GstBuffer*)g_queue_pop_head (appsink->queue)))
269     gst_buffer_unref (buffer);
270   g_mutex_unlock (appsink->mutex);
271
272   G_OBJECT_CLASS (parent_class)->dispose (obj);
273 }
274
275 static void
276 gst_app_sink_finalize (GObject * obj)
277 {
278   GstAppSink *appsink = GST_APP_SINK (obj);
279
280   g_mutex_free (appsink->mutex);
281   g_cond_free (appsink->cond);
282   g_queue_free (appsink->queue);
283
284   G_OBJECT_CLASS (parent_class)->finalize (obj);
285 }
286
287 static void
288 gst_app_sink_set_property (GObject * object, guint prop_id,
289     const GValue * value, GParamSpec * pspec)
290 {
291   GstAppSink *appsink = GST_APP_SINK (object);
292
293   //printf("appsinksetproperty\n");
294
295   switch (prop_id) {
296     case PROP_CAPS:
297       gst_app_sink_set_caps (appsink, gst_value_get_caps (value));
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302   }
303 }
304
305 static void
306 gst_app_sink_get_property (GObject * object, guint prop_id, GValue * value,
307     GParamSpec * pspec)
308 {
309   GstAppSink *appsink = GST_APP_SINK (object);
310
311   //printf("appsinkgetproperty\n");
312
313   switch (prop_id) {
314     case PROP_CAPS:
315     {
316       GstCaps *caps;
317
318       caps = gst_app_sink_get_caps (appsink);
319       gst_value_set_caps (value, caps);
320       if (caps)
321         gst_caps_unref (caps);
322       break;
323     }
324     case PROP_EOS:
325       g_value_set_boolean (value, gst_app_sink_is_eos (appsink));
326       break;
327     default:
328       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329       break;
330   }
331 }
332
333 static void
334 gst_app_sink_flush_unlocked (GstAppSink * appsink)
335 {
336   GstBuffer *buffer;
337
338   //printf("appsinkflushunlocked\n");
339
340   GST_DEBUG_OBJECT (appsink, "flushing appsink");
341   appsink->is_eos = FALSE;
342   gst_buffer_replace (&appsink->preroll, NULL);
343   while ((buffer = (GstBuffer*)g_queue_pop_head (appsink->queue)))
344     gst_buffer_unref (buffer);
345   g_cond_signal (appsink->cond);
346 }
347
348 static gboolean
349 gst_app_sink_start (GstBaseSink * psink)
350 {
351   GstAppSink *appsink = GST_APP_SINK (psink);
352
353   //printf("appsinkstart\n");
354
355   g_mutex_lock (appsink->mutex);
356   appsink->is_eos = FALSE;
357   appsink->started = TRUE;
358   GST_DEBUG_OBJECT (appsink, "starting");
359   g_mutex_unlock (appsink->mutex);
360
361   return TRUE;
362 }
363
364 static gboolean
365 gst_app_sink_stop (GstBaseSink * psink)
366 {
367   GstAppSink *appsink = GST_APP_SINK (psink);
368
369   //printf("appsinkstop\n");
370
371   g_mutex_lock (appsink->mutex);
372   GST_DEBUG_OBJECT (appsink, "stopping");
373   appsink->started = FALSE;
374   gst_app_sink_flush_unlocked (appsink);
375   g_mutex_unlock (appsink->mutex);
376
377   return TRUE;
378 }
379
380 static gboolean
381 gst_app_sink_event (GstBaseSink * sink, GstEvent * event)
382 {
383   GstAppSink *appsink = GST_APP_SINK (sink);
384
385   //printf("appsinkevent\n");
386
387   switch (event->type) {
388     case GST_EVENT_EOS:
389       g_mutex_lock (appsink->mutex);
390       GST_DEBUG_OBJECT (appsink, "receiving EOS");
391       appsink->is_eos = TRUE;
392       g_cond_signal (appsink->cond);
393       g_mutex_unlock (appsink->mutex);
394       break;
395     case GST_EVENT_FLUSH_START:
396       break;
397     case GST_EVENT_FLUSH_STOP:
398       g_mutex_lock (appsink->mutex);
399       GST_DEBUG_OBJECT (appsink, "received FLUSH_STOP");
400       gst_app_sink_flush_unlocked (appsink);
401       g_mutex_unlock (appsink->mutex);
402       break;
403     default:
404       break;
405   }
406   return TRUE;
407 }
408
409 static GstFlowReturn
410 gst_app_sink_preroll (GstBaseSink * psink, GstBuffer * buffer)
411 {
412   GstAppSink *appsink = GST_APP_SINK (psink);
413
414   //printf("appsinkpreroll\n");
415
416   g_mutex_lock (appsink->mutex);
417   GST_DEBUG_OBJECT (appsink, "setting preroll buffer %p", buffer);
418   gst_buffer_replace (&appsink->preroll, buffer);
419   g_cond_signal (appsink->cond);
420   g_mutex_unlock (appsink->mutex);
421
422   g_signal_emit(psink, gst_app_sink_signals[SIGNAL_NEW_PREROLL], 0, buffer);
423
424   return GST_FLOW_OK;
425 }
426
427 static GstFlowReturn
428 gst_app_sink_render (GstBaseSink * psink, GstBuffer * buffer)
429 {
430   GstAppSink *appsink = GST_APP_SINK (psink);
431
432   g_mutex_lock (appsink->mutex);
433   GST_DEBUG_OBJECT (appsink, "pushing render buffer %p on queue", buffer);
434   g_queue_push_tail (appsink->queue, gst_buffer_ref (buffer));
435   g_cond_signal (appsink->cond);
436 //  printf("appsinkrender, have %d buffers\n", g_queue_get_length(appsink->queue));
437   g_mutex_unlock (appsink->mutex);
438   g_signal_emit(psink, gst_app_sink_signals[SIGNAL_NEW_BUFFER], 0,
439                 g_queue_get_length(appsink->queue));
440
441   return GST_FLOW_OK;
442 }
443
444 static GstCaps *
445 gst_app_sink_getcaps (GstBaseSink * psink)
446 {
447   GstCaps *caps;
448
449   //printf("appsinkgetcaps\n");
450
451   GstAppSink *appsink = GST_APP_SINK (psink);
452
453   GST_OBJECT_LOCK (appsink);
454   if ((caps = appsink->caps))
455     gst_caps_ref (caps);
456   GST_DEBUG_OBJECT (appsink, "got caps %" GST_PTR_FORMAT, caps);
457   GST_OBJECT_UNLOCK (appsink);
458
459   return caps;
460 }
461
462 /* external API */
463
464 /**
465  * gst_app_sink_set_caps:
466  * @appsink: a #GstAppSink
467  * @caps: caps to set
468  *
469  * Set the capabilities on the appsink element.  This function takes
470  * a copy of the caps structure. After calling this method, the sink will only
471  * accept caps that match @caps. If @caps is non-fixed, you must check the caps
472  * on the buffers to get the actual used caps.
473  */
474 void
475 gst_app_sink_set_caps (GstAppSink * appsink, const GstCaps * caps)
476 {
477   GstCaps *old;
478
479   g_return_if_fail (appsink != NULL);
480   g_return_if_fail (GST_IS_APP_SINK (appsink));
481
482   GST_OBJECT_LOCK (appsink);
483   GST_DEBUG_OBJECT (appsink, "setting caps to %" GST_PTR_FORMAT, caps);
484   old = appsink->caps;
485   if (caps)
486     appsink->caps = gst_caps_copy (caps);
487   else
488     appsink->caps = NULL;
489   if (old)
490     gst_caps_unref (old);
491   GST_OBJECT_UNLOCK (appsink);
492 }
493
494 /**
495  * gst_app_sink_get_caps:
496  * @appsink: a #GstAppSink
497  *
498  * Get the configured caps on @appsink.
499  *
500  * Returns: the #GstCaps accepted by the sink. gst_caps_unref() after usage.
501  */
502 GstCaps *
503 gst_app_sink_get_caps (GstAppSink * appsink)
504 {
505   GstCaps *caps;
506
507   g_return_val_if_fail (appsink != NULL, NULL);
508   g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
509
510   GST_OBJECT_LOCK (appsink);
511   if ((caps = appsink->caps))
512     gst_caps_ref (caps);
513   GST_DEBUG_OBJECT (appsink, "getting caps of %" GST_PTR_FORMAT, caps);
514   GST_OBJECT_UNLOCK (appsink);
515
516   return caps;
517 }
518
519 /**
520  * gst_app_sink_is_eos:
521  * @appsink: a #GstAppSink
522  *
523  * Check if @appsink is EOS, which is when no more buffers can be pulled because
524  * an EOS event was received.
525  *
526  * This function also returns %TRUE when the appsink is not in the PAUSED or
527  * PLAYING state.
528  *
529  * Returns: %TRUE if no more buffers can be pulled and the appsink is EOS.
530  */
531 gboolean
532 gst_app_sink_is_eos (GstAppSink * appsink)
533 {
534   gboolean ret;
535
536   g_return_val_if_fail (appsink != NULL, FALSE);
537   g_return_val_if_fail (GST_IS_APP_SINK (appsink), FALSE);
538
539   g_mutex_lock (appsink->mutex);
540   if (!appsink->started)
541     goto not_started;
542
543   if (appsink->is_eos && g_queue_is_empty (appsink->queue)) {
544     GST_DEBUG_OBJECT (appsink, "we are EOS and the queue is empty");
545     ret = TRUE;
546   } else {
547     GST_DEBUG_OBJECT (appsink, "we are not yet EOS");
548     ret = FALSE;
549   }
550   g_mutex_unlock (appsink->mutex);
551
552   return ret;
553
554 not_started:
555   {
556     GST_DEBUG_OBJECT (appsink, "we are stopped, return TRUE");
557     g_mutex_unlock (appsink->mutex);
558     return TRUE;
559   }
560 }
561
562 /**
563  * gst_app_sink_pull_preroll:
564  * @appsink: a #GstAppSink
565  *
566  * Get the last preroll buffer in @appsink. This was the buffer that caused the
567  * appsink to preroll in the PAUSED state. This buffer can be pulled many times
568  * and remains available to the application even after EOS.
569  *
570  * This function is typically used when dealing with a pipeline in the PAUSED
571  * state. Calling this function after doing a seek will give the buffer right
572  * after the seek position.
573  *
574  * Note that the preroll buffer will also be returned as the first buffer
575  * when calling gst_app_sink_pull_buffer().
576  *
577  * If an EOS event was received before any buffers, this function returns
578  * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
579  *
580  * This function blocks until a preroll buffer or EOS is received or the appsink
581  * element is set to the READY/NULL state.
582  *
583  * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
584  */
585 GstBuffer *
586 gst_app_sink_pull_preroll (GstAppSink * appsink)
587 {
588   GstBuffer *buf = NULL;
589
590   //printf("pull_preroll\n");
591
592   g_return_val_if_fail (appsink != NULL, NULL);
593   g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
594
595   g_mutex_lock (appsink->mutex);
596
597   while (TRUE) {
598     GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
599     if (!appsink->started)
600       goto not_started;
601
602     if (appsink->preroll != NULL)
603       break;
604
605     if (appsink->is_eos)
606       goto eos;
607
608     /* nothing to return, wait */
609     GST_DEBUG_OBJECT (appsink, "waiting for the preroll buffer");
610     g_cond_wait (appsink->cond, appsink->mutex);
611   }
612   buf = gst_buffer_ref (appsink->preroll);
613   GST_DEBUG_OBJECT (appsink, "we have the preroll buffer %p", buf);
614   g_mutex_unlock (appsink->mutex);
615
616   return buf;
617
618   /* special conditions */
619 eos:
620   {
621     GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
622     g_mutex_unlock (appsink->mutex);
623     return NULL;
624   }
625 not_started:
626   {
627     GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
628     g_mutex_unlock (appsink->mutex);
629     return NULL;
630   }
631 }
632
633 /**
634  * gst_app_sink_pull_buffer:
635  * @appsink: a #GstAppSink
636  *
637  * This function blocks until a buffer or EOS becomes available or the appsink
638  * element is set to the READY/NULL state.
639  *
640  * This function will only return buffers when the appsink is in the PLAYING
641  * state. All rendered buffers will be put in a queue so that the application
642  * can pull buffers at its own rate. Note that when the application does not
643  * pull buffers fast enough, the queued buffers could consume a lot of memory,
644  * especially when dealing with raw video frames.
645  *
646  * If an EOS event was received before any buffers, this function returns
647  * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
648  *
649  * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
650  */
651 GstBuffer *
652 gst_app_sink_pull_buffer (GstAppSink * appsink)
653 {
654   GstBuffer *buf = NULL;
655
656   //printf("pull_buffer\n");
657
658   g_return_val_if_fail (appsink != NULL, NULL);
659   g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
660
661   g_mutex_lock (appsink->mutex);
662
663   while (TRUE) {
664     GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
665     if (!appsink->started)
666       goto not_started;
667
668     if (!g_queue_is_empty (appsink->queue))
669       break;
670
671     if (appsink->is_eos)
672       goto eos;
673
674     /* nothing to return, wait */
675     GST_DEBUG_OBJECT (appsink, "waiting for a buffer");
676     g_cond_wait (appsink->cond, appsink->mutex);
677   }
678   buf = (GstBuffer*)g_queue_pop_head (appsink->queue);
679   GST_DEBUG_OBJECT (appsink, "we have a buffer %p", buf);
680   g_mutex_unlock (appsink->mutex);
681
682   return buf;
683
684   /* special conditions */
685 eos:
686   {
687     GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
688     g_mutex_unlock (appsink->mutex);
689     return NULL;
690   }
691 not_started:
692   {
693     GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
694     g_mutex_unlock (appsink->mutex);
695     return NULL;
696   }
697 }
698
699 /**
700  * gst_app_sink_peek_buffer:
701  * @appsink: a #GstAppSink
702  *
703  * This function returns a buffer if there is one queued but does not block.
704  *
705  * This function will only return buffers when the appsink is in the PLAYING
706  * state. All rendered buffers will be put in a queue so that the application
707  * can pull buffers at its own rate. Note that when the application does not
708  * pull buffers fast enough, the queued buffers could consume a lot of memory,
709  * especially when dealing with raw video frames.
710  *
711  * If an EOS event was received before any buffers, this function returns
712  * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
713  *
714  * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
715  */
716 GstBuffer *
717 gst_app_sink_peek_buffer (GstAppSink * appsink)
718 {
719   GstBuffer *buf = NULL;
720
721   //printf("pull_buffer\n");
722
723   g_return_val_if_fail (appsink != NULL, NULL);
724   g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
725
726   g_mutex_lock (appsink->mutex);
727
728   GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
729   if (!appsink->started)
730     goto not_started;
731
732   if (g_queue_is_empty (appsink->queue))
733     return NULL;
734
735   if (appsink->is_eos)
736     goto eos;
737
738   /* nothing to return, wait */
739   buf = (GstBuffer*)g_queue_pop_head (appsink->queue);
740   GST_DEBUG_OBJECT (appsink, "we have a buffer %p", buf);
741   g_mutex_unlock (appsink->mutex);
742
743   return buf;
744
745   /* special conditions */
746 eos:
747   {
748     GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
749     g_mutex_unlock (appsink->mutex);
750     return NULL;
751   }
752 not_started:
753   {
754     GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
755     g_mutex_unlock (appsink->mutex);
756     return NULL;
757   }
758 }
759
760 guint
761 gst_app_sink_get_queue_length (GstAppSink * appsink)
762 {
763   return g_queue_get_length (appsink->queue);
764 }
765 #endif