2 * Copyright (C) 2007 David Schleef <ds@schleef.org>
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.
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.
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.
25 #include <gst/base/gstbasesink.h>
26 #include <gst/gstbuffer.h>
30 #include "gstappsink.h"
33 GST_DEBUG_CATEGORY (app_sink_debug);
34 #define GST_CAT_DEFAULT app_sink_debug
36 static const GstElementDetails app_sink_details =
37 GST_ELEMENT_DETAILS ("AppSink",
39 "Allow the application to get access to raw buffer",
40 "David Schleef <ds@schleef.org>, Wim Taymans <wim.taymans@gmail.com");
56 #define DEFAULT_PROP_EOS TRUE
57 #define DEFAULT_PROP_EMIT_SIGNALS TRUE
58 #define DEFAULT_PROP_MAX_BUFFERS 0
71 static GstStaticPadTemplate gst_app_sink_template =
72 GST_STATIC_PAD_TEMPLATE ("sink",
77 static void gst_app_sink_dispose (GObject * object);
78 static void gst_app_sink_finalize (GObject * object);
80 static void gst_app_sink_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_app_sink_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static gboolean gst_app_sink_start (GstBaseSink * psink);
86 static gboolean gst_app_sink_stop (GstBaseSink * psink);
87 static gboolean gst_app_sink_event (GstBaseSink * sink, GstEvent * event);
88 static GstFlowReturn gst_app_sink_preroll (GstBaseSink * psink,
90 static GstFlowReturn gst_app_sink_render (GstBaseSink * psink,
92 static GstCaps *gst_app_sink_getcaps (GstBaseSink * psink);
94 static guint gst_app_sink_signals[LAST_SIGNAL] = { 0 };
96 GST_BOILERPLATE (GstAppSink, gst_app_sink, GstBaseSink, GST_TYPE_BASE_SINK);
99 gst_app_marshal_OBJECT__VOID (GClosure * closure,
100 GValue * return_value,
101 guint n_param_values,
102 const GValue * param_values,
103 gpointer invocation_hint, gpointer marshal_data)
105 typedef GstBuffer *(*GMarshalFunc_OBJECT__VOID) (gpointer data1,
107 register GMarshalFunc_OBJECT__VOID callback;
108 register GCClosure *cc = (GCClosure *) closure;
109 register gpointer data1, data2;
112 g_return_if_fail (return_value != NULL);
113 g_return_if_fail (n_param_values == 1);
115 if (G_CCLOSURE_SWAP_DATA (closure)) {
116 data1 = closure->data;
117 data2 = g_value_peek_pointer (param_values + 0);
119 data1 = g_value_peek_pointer (param_values + 0);
120 data2 = closure->data;
123 (GMarshalFunc_OBJECT__VOID) (marshal_data ? marshal_data : cc->callback);
125 v_return = callback (data1, data2);
127 gst_value_take_buffer (return_value, v_return);
131 gst_app_sink_base_init (gpointer g_class)
133 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
135 GST_DEBUG_CATEGORY_INIT (app_sink_debug, "appsink", 0, "appsink element");
137 gst_element_class_set_details (element_class, &app_sink_details);
139 gst_element_class_add_pad_template (element_class,
140 gst_static_pad_template_get (&gst_app_sink_template));
144 gst_app_sink_class_init (GstAppSinkClass * klass)
146 GObjectClass *gobject_class = (GObjectClass *) klass;
147 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
149 gobject_class->dispose = gst_app_sink_dispose;
150 gobject_class->finalize = gst_app_sink_finalize;
152 gobject_class->set_property = gst_app_sink_set_property;
153 gobject_class->get_property = gst_app_sink_get_property;
155 g_object_class_install_property (gobject_class, PROP_CAPS,
156 g_param_spec_boxed ("caps", "Caps",
157 "The caps of the sink pad", GST_TYPE_CAPS,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 g_object_class_install_property (gobject_class, PROP_EOS,
161 g_param_spec_boolean ("eos", "EOS",
162 "Check if the sink is EOS or not started", DEFAULT_PROP_EOS,
163 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
165 g_object_class_install_property (gobject_class, PROP_EMIT_SIGNALS,
166 g_param_spec_boolean ("emit-signals", "Emit signals",
167 "Emit new-preroll and new-buffer signals", DEFAULT_PROP_EMIT_SIGNALS,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class, PROP_MAX_BUFFERS,
171 g_param_spec_uint ("max-buffers", "Max Buffers",
172 "Control the maximum buffers to queue internally (0 = unlimited)",
173 0, G_MAXUINT, DEFAULT_PROP_MAX_BUFFERS,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 * @appsink: the appsink element that emited the signal
180 * Signal that the end-of-stream has been reached. This signal is emited from
181 * the steaming thread.
183 gst_app_sink_signals[SIGNAL_EOS] =
184 g_signal_new ("eos", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
185 G_STRUCT_OFFSET (GstAppSinkClass, eos),
186 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
188 * GstAppSink::new-preroll:
189 * @appsink: the appsink element that emited the signal
191 * Signal that a new preroll buffer is available.
193 * This signal is emited from the steaming thread and only when the
194 * "emit-signals" property is %TRUE.
196 * The new preroll buffer can be retrieved with the "pull-preroll" action
197 * signal or gst_app_sink_pull_preroll() either from this signal callback
198 * or from any other thread.
200 * Note that this signal is only emited when the "emit-signals" property is
201 * set to %TRUE, which it is not by default for performance reasons.
203 gst_app_sink_signals[SIGNAL_NEW_PREROLL] =
204 g_signal_new ("new-preroll", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
205 G_STRUCT_OFFSET (GstAppSinkClass, new_preroll),
206 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
208 * GstAppSink::new-buffer:
209 * @appsink: the appsink element that emited the signal
211 * Signal that a new buffer is available.
213 * This signal is emited from the steaming thread and only when the
214 * "emit-signals" property is %TRUE.
216 * The new preroll buffer can be retrieved with the "pull-buffer" action
217 * signal or gst_app_sink_pull_buffer() either from this signal callback
218 * or from any other thread.
220 * Note that this signal is only emited when the "emit-signals" property is
221 * set to %TRUE, which it is not by default for performance reasons.
223 gst_app_sink_signals[SIGNAL_NEW_BUFFER] =
224 g_signal_new ("new-buffer", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
225 G_STRUCT_OFFSET (GstAppSinkClass, new_buffer),
226 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
229 * GstAppSink::pull-preroll:
230 * @appsink: the appsink element to emit this signal on
232 * Get the last preroll buffer in @appsink. This was the buffer that caused the
233 * appsink to preroll in the PAUSED state. This buffer can be pulled many times
234 * and remains available to the application even after EOS.
236 * This function is typically used when dealing with a pipeline in the PAUSED
237 * state. Calling this function after doing a seek will give the buffer right
238 * after the seek position.
240 * Note that the preroll buffer will also be returned as the first buffer
241 * when calling gst_app_sink_pull_buffer() or the "pull-buffer" action signal.
243 * If an EOS event was received before any buffers, this function returns
244 * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
246 * This function blocks until a preroll buffer or EOS is received or the appsink
247 * element is set to the READY/NULL state.
249 * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
251 gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
252 g_signal_new ("pull-preroll", G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstAppSinkClass,
254 pull_preroll), NULL, NULL, gst_app_marshal_OBJECT__VOID,
255 GST_TYPE_BUFFER, 0, G_TYPE_NONE);
257 * GstAppSink::pull-buffer:
258 * @appsink: the appsink element to emit this signal on
260 * This function blocks until a buffer or EOS becomes available or the appsink
261 * element is set to the READY/NULL state.
263 * This function will only return buffers when the appsink is in the PLAYING
264 * state. All rendered buffers will be put in a queue so that the application
265 * can pull buffers at its own rate. Note that when the application does not
266 * pull buffers fast enough, the queued buffers could consume a lot of memory,
267 * especially when dealing with raw video frames.
269 * If an EOS event was received before any buffers, this function returns
270 * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
272 * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
274 gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
275 g_signal_new ("pull-buffer", G_TYPE_FROM_CLASS (klass),
276 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstAppSinkClass,
277 pull_buffer), NULL, NULL, gst_app_marshal_OBJECT__VOID,
278 GST_TYPE_BUFFER, 0, G_TYPE_NONE);
280 basesink_class->start = gst_app_sink_start;
281 basesink_class->stop = gst_app_sink_stop;
282 basesink_class->event = gst_app_sink_event;
283 basesink_class->preroll = gst_app_sink_preroll;
284 basesink_class->render = gst_app_sink_render;
285 basesink_class->get_caps = gst_app_sink_getcaps;
287 klass->pull_preroll = gst_app_sink_pull_preroll;
288 klass->pull_buffer = gst_app_sink_pull_buffer;
292 gst_app_sink_init (GstAppSink * appsink, GstAppSinkClass * klass)
294 appsink->mutex = g_mutex_new ();
295 appsink->cond = g_cond_new ();
296 appsink->queue = g_queue_new ();
298 appsink->emit_signals = DEFAULT_PROP_EMIT_SIGNALS;
299 appsink->max_buffers = DEFAULT_PROP_MAX_BUFFERS;
303 gst_app_sink_dispose (GObject * obj)
305 GstAppSink *appsink = GST_APP_SINK (obj);
309 gst_caps_unref (appsink->caps);
310 appsink->caps = NULL;
312 if (appsink->preroll) {
313 gst_buffer_unref (appsink->preroll);
314 appsink->preroll = NULL;
316 g_mutex_lock (appsink->mutex);
317 while ((buffer = g_queue_pop_head (appsink->queue)))
318 gst_buffer_unref (buffer);
319 g_mutex_unlock (appsink->mutex);
321 G_OBJECT_CLASS (parent_class)->dispose (obj);
325 gst_app_sink_finalize (GObject * obj)
327 GstAppSink *appsink = GST_APP_SINK (obj);
329 g_mutex_free (appsink->mutex);
330 g_cond_free (appsink->cond);
331 g_queue_free (appsink->queue);
333 G_OBJECT_CLASS (parent_class)->finalize (obj);
337 gst_app_sink_set_property (GObject * object, guint prop_id,
338 const GValue * value, GParamSpec * pspec)
340 GstAppSink *appsink = GST_APP_SINK (object);
344 gst_app_sink_set_caps (appsink, gst_value_get_caps (value));
346 case PROP_EMIT_SIGNALS:
347 gst_app_sink_set_emit_signals (appsink, g_value_get_boolean (value));
349 case PROP_MAX_BUFFERS:
350 gst_app_sink_set_max_buffers (appsink, g_value_get_uint (value));
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 gst_app_sink_get_property (GObject * object, guint prop_id, GValue * value,
362 GstAppSink *appsink = GST_APP_SINK (object);
369 caps = gst_app_sink_get_caps (appsink);
370 gst_value_set_caps (value, caps);
372 gst_caps_unref (caps);
376 g_value_set_boolean (value, gst_app_sink_is_eos (appsink));
378 case PROP_EMIT_SIGNALS:
379 g_value_set_boolean (value, gst_app_sink_get_emit_signals (appsink));
381 case PROP_MAX_BUFFERS:
382 g_value_set_uint (value, gst_app_sink_get_max_buffers (appsink));
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gst_app_sink_flush_unlocked (GstAppSink * appsink)
395 GST_DEBUG_OBJECT (appsink, "flushing appsink");
396 appsink->is_eos = FALSE;
397 gst_buffer_replace (&appsink->preroll, NULL);
398 while ((buffer = g_queue_pop_head (appsink->queue)))
399 gst_buffer_unref (buffer);
400 g_cond_signal (appsink->cond);
404 gst_app_sink_start (GstBaseSink * psink)
406 GstAppSink *appsink = GST_APP_SINK (psink);
408 g_mutex_lock (appsink->mutex);
409 appsink->is_eos = FALSE;
410 appsink->started = TRUE;
411 GST_DEBUG_OBJECT (appsink, "starting");
412 g_mutex_unlock (appsink->mutex);
418 gst_app_sink_stop (GstBaseSink * psink)
420 GstAppSink *appsink = GST_APP_SINK (psink);
422 g_mutex_lock (appsink->mutex);
423 GST_DEBUG_OBJECT (appsink, "stopping");
424 appsink->started = FALSE;
425 gst_app_sink_flush_unlocked (appsink);
426 g_mutex_unlock (appsink->mutex);
432 gst_app_sink_event (GstBaseSink * sink, GstEvent * event)
434 GstAppSink *appsink = GST_APP_SINK (sink);
436 switch (event->type) {
439 g_mutex_lock (appsink->mutex);
440 GST_DEBUG_OBJECT (appsink, "receiving EOS");
441 appsink->is_eos = TRUE;
442 g_cond_signal (appsink->cond);
443 g_mutex_unlock (appsink->mutex);
446 g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_EOS], 0);
448 case GST_EVENT_FLUSH_START:
450 case GST_EVENT_FLUSH_STOP:
451 g_mutex_lock (appsink->mutex);
452 GST_DEBUG_OBJECT (appsink, "received FLUSH_STOP");
453 gst_app_sink_flush_unlocked (appsink);
454 g_mutex_unlock (appsink->mutex);
463 gst_app_sink_preroll (GstBaseSink * psink, GstBuffer * buffer)
465 GstAppSink *appsink = GST_APP_SINK (psink);
468 g_mutex_lock (appsink->mutex);
469 GST_DEBUG_OBJECT (appsink, "setting preroll buffer %p", buffer);
470 gst_buffer_replace (&appsink->preroll, buffer);
471 g_cond_signal (appsink->cond);
472 emit = appsink->emit_signals;
473 g_mutex_unlock (appsink->mutex);
476 g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_NEW_PREROLL], 0);
482 gst_app_sink_render (GstBaseSink * psink, GstBuffer * buffer)
484 GstAppSink *appsink = GST_APP_SINK (psink);
487 g_mutex_lock (appsink->mutex);
488 GST_DEBUG_OBJECT (appsink, "pushing render buffer %p on queue", buffer);
490 while (appsink->max_buffers > 0 &&
491 appsink->queue->length >= appsink->max_buffers) {
492 /* FIXME, do proper unlocking when flushing */
493 g_cond_wait (appsink->cond, appsink->mutex);
494 if (!appsink->started)
497 g_queue_push_tail (appsink->queue, gst_buffer_ref (buffer));
498 g_cond_signal (appsink->cond);
499 emit = appsink->emit_signals;
500 g_mutex_unlock (appsink->mutex);
503 g_signal_emit (appsink, gst_app_sink_signals[SIGNAL_NEW_BUFFER], 0);
509 GST_DEBUG_OBJECT (appsink, "we stopped");
510 g_mutex_unlock (appsink->mutex);
511 return GST_FLOW_WRONG_STATE;
516 gst_app_sink_getcaps (GstBaseSink * psink)
520 GstAppSink *appsink = GST_APP_SINK (psink);
522 GST_OBJECT_LOCK (appsink);
523 if ((caps = appsink->caps))
525 GST_DEBUG_OBJECT (appsink, "got caps %" GST_PTR_FORMAT, caps);
526 GST_OBJECT_UNLOCK (appsink);
534 * gst_app_sink_set_caps:
535 * @appsink: a #GstAppSink
538 * Set the capabilities on the appsink element. This function takes
539 * a copy of the caps structure. After calling this method, the sink will only
540 * accept caps that match @caps. If @caps is non-fixed, you must check the caps
541 * on the buffers to get the actual used caps.
544 gst_app_sink_set_caps (GstAppSink * appsink, const GstCaps * caps)
548 g_return_if_fail (appsink != NULL);
549 g_return_if_fail (GST_IS_APP_SINK (appsink));
551 GST_OBJECT_LOCK (appsink);
552 GST_DEBUG_OBJECT (appsink, "setting caps to %" GST_PTR_FORMAT, caps);
555 appsink->caps = gst_caps_copy (caps);
557 appsink->caps = NULL;
559 gst_caps_unref (old);
560 GST_OBJECT_UNLOCK (appsink);
564 * gst_app_sink_get_caps:
565 * @appsink: a #GstAppSink
567 * Get the configured caps on @appsink.
569 * Returns: the #GstCaps accepted by the sink. gst_caps_unref() after usage.
572 gst_app_sink_get_caps (GstAppSink * appsink)
576 g_return_val_if_fail (appsink != NULL, NULL);
577 g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
579 GST_OBJECT_LOCK (appsink);
580 if ((caps = appsink->caps))
582 GST_DEBUG_OBJECT (appsink, "getting caps of %" GST_PTR_FORMAT, caps);
583 GST_OBJECT_UNLOCK (appsink);
589 * gst_app_sink_is_eos:
590 * @appsink: a #GstAppSink
592 * Check if @appsink is EOS, which is when no more buffers can be pulled because
593 * an EOS event was received.
595 * This function also returns %TRUE when the appsink is not in the PAUSED or
598 * Returns: %TRUE if no more buffers can be pulled and the appsink is EOS.
601 gst_app_sink_is_eos (GstAppSink * appsink)
605 g_return_val_if_fail (appsink != NULL, FALSE);
606 g_return_val_if_fail (GST_IS_APP_SINK (appsink), FALSE);
608 g_mutex_lock (appsink->mutex);
609 if (!appsink->started)
612 if (appsink->is_eos && g_queue_is_empty (appsink->queue)) {
613 GST_DEBUG_OBJECT (appsink, "we are EOS and the queue is empty");
616 GST_DEBUG_OBJECT (appsink, "we are not yet EOS");
619 g_mutex_unlock (appsink->mutex);
625 GST_DEBUG_OBJECT (appsink, "we are stopped, return TRUE");
626 g_mutex_unlock (appsink->mutex);
632 * gst_app_sink_set_emit_signals:
633 * @appsink: a #GstAppSink
634 * @emit: the new state
636 * Make appsink emit the "new-preroll" and "new-buffer" signals. This option is
637 * by default disabled because signal emission is expensive and unneeded when
638 * the application prefers to operate in pull mode.
641 gst_app_sink_set_emit_signals (GstAppSink * appsink, gboolean emit)
643 g_return_if_fail (GST_IS_APP_SINK (appsink));
645 g_mutex_lock (appsink->mutex);
646 appsink->emit_signals = emit;
647 g_mutex_unlock (appsink->mutex);
651 * gst_app_sink_get_emit_signals:
652 * @appsink: a #GstAppSink
654 * Check if appsink will emit the "new-preroll" and "new-buffer" signals.
656 * Returns: %TRUE if @appsink is emiting the "new-preroll" and "new-buffer"
660 gst_app_sink_get_emit_signals (GstAppSink * appsink)
664 g_return_val_if_fail (GST_IS_APP_SINK (appsink), FALSE);
666 g_mutex_lock (appsink->mutex);
667 result = appsink->emit_signals;
668 g_mutex_unlock (appsink->mutex);
674 * gst_app_sink_set_max_buffers:
675 * @appsink: a #GstAppSink
676 * @max: the maximum number of buffers to queue
678 * Set the maximum amount of buffers that can be queued in @appsink. After this
679 * amount of buffers are queued in appsink, any more buffers will block upstream
680 * elements until a buffer is pulled from @appsink.
683 gst_app_sink_set_max_buffers (GstAppSink * appsink, guint max)
685 g_return_if_fail (GST_IS_APP_SINK (appsink));
687 g_mutex_lock (appsink->mutex);
688 if (max != appsink->max_buffers) {
689 appsink->max_buffers = max;
690 /* signal the change */
691 g_cond_signal (appsink->cond);
693 g_mutex_unlock (appsink->mutex);
697 * gst_app_sink_get_max_buffers:
698 * @appsink: a #GstAppSink
700 * Get the maximum amount of buffers that can be queued in @appsink.
702 * Returns: The maximum amount of buffers that can be queued.
705 gst_app_sink_get_max_buffers (GstAppSink * appsink)
709 g_return_val_if_fail (GST_IS_APP_SINK (appsink), 0);
711 g_mutex_lock (appsink->mutex);
712 result = appsink->max_buffers;
713 g_mutex_unlock (appsink->mutex);
719 * gst_app_sink_pull_preroll:
720 * @appsink: a #GstAppSink
722 * Get the last preroll buffer in @appsink. This was the buffer that caused the
723 * appsink to preroll in the PAUSED state. This buffer can be pulled many times
724 * and remains available to the application even after EOS.
726 * This function is typically used when dealing with a pipeline in the PAUSED
727 * state. Calling this function after doing a seek will give the buffer right
728 * after the seek position.
730 * Note that the preroll buffer will also be returned as the first buffer
731 * when calling gst_app_sink_pull_buffer().
733 * If an EOS event was received before any buffers, this function returns
734 * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
736 * This function blocks until a preroll buffer or EOS is received or the appsink
737 * element is set to the READY/NULL state.
739 * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
742 gst_app_sink_pull_preroll (GstAppSink * appsink)
744 GstBuffer *buf = NULL;
746 g_return_val_if_fail (appsink != NULL, NULL);
747 g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
749 g_mutex_lock (appsink->mutex);
752 GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
753 if (!appsink->started)
756 if (appsink->preroll != NULL)
762 /* nothing to return, wait */
763 GST_DEBUG_OBJECT (appsink, "waiting for the preroll buffer");
764 g_cond_wait (appsink->cond, appsink->mutex);
766 buf = gst_buffer_ref (appsink->preroll);
767 GST_DEBUG_OBJECT (appsink, "we have the preroll buffer %p", buf);
768 g_mutex_unlock (appsink->mutex);
772 /* special conditions */
775 GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
776 g_mutex_unlock (appsink->mutex);
781 GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
782 g_mutex_unlock (appsink->mutex);
788 * gst_app_sink_pull_buffer:
789 * @appsink: a #GstAppSink
791 * This function blocks until a buffer or EOS becomes available or the appsink
792 * element is set to the READY/NULL state.
794 * This function will only return buffers when the appsink is in the PLAYING
795 * state. All rendered buffers will be put in a queue so that the application
796 * can pull buffers at its own rate. Note that when the application does not
797 * pull buffers fast enough, the queued buffers could consume a lot of memory,
798 * especially when dealing with raw video frames.
800 * If an EOS event was received before any buffers, this function returns
801 * %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
803 * Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
806 gst_app_sink_pull_buffer (GstAppSink * appsink)
808 GstBuffer *buf = NULL;
810 g_return_val_if_fail (appsink != NULL, NULL);
811 g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
813 g_mutex_lock (appsink->mutex);
816 GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
817 if (!appsink->started)
820 if (!g_queue_is_empty (appsink->queue))
826 /* nothing to return, wait */
827 GST_DEBUG_OBJECT (appsink, "waiting for a buffer");
828 g_cond_wait (appsink->cond, appsink->mutex);
830 buf = g_queue_pop_head (appsink->queue);
831 GST_DEBUG_OBJECT (appsink, "we have a buffer %p", buf);
832 g_cond_signal (appsink->cond);
833 g_mutex_unlock (appsink->mutex);
837 /* special conditions */
840 GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
841 g_mutex_unlock (appsink->mutex);
846 GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
847 g_mutex_unlock (appsink->mutex);