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.
20
/* $Id: gstappsink.c,v 1.6 2008/01/29 05:18:33 bjacques Exp $ */
23
#include "gnashconfig.h"
27
#include <gst/base/gstbasesink.h>
28
#include <gst/gstbuffer.h>
32
#include "gstappsink.h"
35
GST_DEBUG_CATEGORY (app_sink_debug);
36
#define GST_CAT_DEFAULT app_sink_debug
38
static const GstElementDetails app_sink_details =
39
GST_ELEMENT_DETAILS ("AppSink",
41
"Allow the application to get access to raw buffer",
42
"David Schleef <ds@schleef.org>, Wim Taymans <wim.taymans@gmail.com");
65
static GstStaticPadTemplate gst_app_sink_template =
66
GST_STATIC_PAD_TEMPLATE ("sink",
71
static void gst_app_sink_dispose (GObject * object);
72
static void gst_app_sink_finalize (GObject * object);
74
static void gst_app_sink_set_property (GObject * object, guint prop_id,
75
const GValue * value, GParamSpec * pspec);
76
static void gst_app_sink_get_property (GObject * object, guint prop_id,
77
GValue * value, GParamSpec * pspec);
79
static gboolean gst_app_sink_start (GstBaseSink * psink);
80
static gboolean gst_app_sink_stop (GstBaseSink * psink);
81
static gboolean gst_app_sink_event (GstBaseSink * sink, GstEvent * event);
82
static GstFlowReturn gst_app_sink_preroll (GstBaseSink * psink,
84
static GstFlowReturn gst_app_sink_render (GstBaseSink * psink,
86
static GstCaps *gst_app_sink_getcaps (GstBaseSink * psink);
88
static guint gst_app_sink_signals[LAST_SIGNAL] = { 0 };
90
GST_BOILERPLATE (GstAppSink, gst_app_sink, GstBaseSink, GST_TYPE_BASE_SINK);
93
gst_app_marshal_OBJECT__VOID (GClosure * closure,
94
GValue * return_value,
96
const GValue * param_values,
97
gpointer invocation_hint, gpointer marshal_data)
99
typedef GstBuffer *(*GMarshalFunc_OBJECT__VOID) (gpointer data1,
101
register GMarshalFunc_OBJECT__VOID callback;
102
register GCClosure *cc = (GCClosure *) closure;
103
register gpointer data1, data2;
106
g_return_if_fail (return_value != NULL);
107
g_return_if_fail (n_param_values == 1);
109
if (G_CCLOSURE_SWAP_DATA (closure)) {
110
data1 = closure->data;
111
data2 = g_value_peek_pointer (param_values + 0);
113
data1 = g_value_peek_pointer (param_values + 0);
114
data2 = closure->data;
117
(GMarshalFunc_OBJECT__VOID) (marshal_data ? marshal_data : cc->callback);
119
v_return = callback (data1, data2);
121
gst_value_take_buffer (return_value, v_return);
125
gst_app_sink_base_init (gpointer g_class)
127
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
129
GST_DEBUG_CATEGORY_INIT (app_sink_debug, "appsink", 0, "appsink element");
131
gst_element_class_set_details (element_class, &app_sink_details);
133
gst_element_class_add_pad_template (element_class,
134
gst_static_pad_template_get (&gst_app_sink_template));
138
gst_app_sink_class_init (GstAppSinkClass * klass)
140
GObjectClass *gobject_class = (GObjectClass *) klass;
141
GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
143
gobject_class->dispose = gst_app_sink_dispose;
144
gobject_class->finalize = gst_app_sink_finalize;
146
gobject_class->set_property = gst_app_sink_set_property;
147
gobject_class->get_property = gst_app_sink_get_property;
149
g_object_class_install_property (gobject_class, PROP_CAPS,
150
g_param_spec_boxed ("caps", "Caps",
151
"The caps of the sink pad", GST_TYPE_CAPS, G_PARAM_READWRITE));
153
g_object_class_install_property (gobject_class, PROP_EOS,
154
g_param_spec_boolean ("eos", "EOS",
155
"Check if the sink is EOS", TRUE, G_PARAM_READABLE));
159
* @appsink: the appsink element that emited the signal
161
* Signal that the end-of-stream has been reached.
163
gst_app_sink_signals[SIGNAL_EOS] =
164
g_signal_new ("eos", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
165
G_STRUCT_OFFSET (GstAppSinkClass, eos),
166
NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
168
* GstAppSink::new-preroll:
169
* @appsink: the appsink element that emited the signal
170
* @buffer: the buffer that caused the preroll
172
* Signal that a new preroll buffer is available.
174
gst_app_sink_signals[SIGNAL_NEW_PREROLL] =
175
g_signal_new ("new-preroll", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176
G_STRUCT_OFFSET (GstAppSinkClass, new_preroll),
177
NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 0,
180
* GstAppSink::new-buffer:
181
* @appsink: the appsink element that emited the signal
182
* @buffer: the buffer that is available
184
* Signal that a new buffer is available.
186
gst_app_sink_signals[SIGNAL_NEW_BUFFER] =
187
g_signal_new ("new-buffer", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
188
G_STRUCT_OFFSET (GstAppSinkClass, new_buffer),
189
NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 0,
193
* GstAppSink::pull-preroll:
194
* @appsink: the appsink element to emit this signal on
196
* Get the last preroll buffer on @appsink.
198
gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
199
g_signal_new ("pull-preroll", G_TYPE_FROM_CLASS (klass),
200
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstAppSinkClass, pull_preroll), NULL,
201
NULL, gst_app_marshal_OBJECT__VOID, GST_TYPE_BUFFER, 0, G_TYPE_NONE);
203
* GstAppSink::pull-buffer:
204
* @appsink: the appsink element to emit this signal on
206
* Get the next buffer buffer on @appsink.
208
gst_app_sink_signals[SIGNAL_PULL_PREROLL] =
209
g_signal_new ("pull-buffer", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
210
G_STRUCT_OFFSET (GstAppSinkClass, pull_buffer),
211
NULL, NULL, gst_app_marshal_OBJECT__VOID, GST_TYPE_BUFFER, 0,
214
basesink_class->start = gst_app_sink_start;
215
basesink_class->stop = gst_app_sink_stop;
216
basesink_class->event = gst_app_sink_event;
217
basesink_class->preroll = gst_app_sink_preroll;
218
basesink_class->render = gst_app_sink_render;
219
basesink_class->get_caps = gst_app_sink_getcaps;
221
klass->pull_preroll = gst_app_sink_pull_preroll;
222
klass->pull_buffer = gst_app_sink_pull_buffer;
226
gst_app_sink_init (GstAppSink * appsink, GstAppSinkClass * klass)
228
appsink->mutex = g_mutex_new ();
229
appsink->cond = g_cond_new ();
230
appsink->queue = g_queue_new ();
234
gst_app_sink_dispose (GObject * obj)
236
GstAppSink *appsink = GST_APP_SINK (obj);
239
gst_caps_unref (appsink->caps);
240
appsink->caps = NULL;
242
if (appsink->preroll) {
243
gst_buffer_unref (appsink->preroll);
244
appsink->preroll = NULL;
246
g_queue_foreach (appsink->queue, (GFunc) gst_mini_object_unref, NULL);
248
G_OBJECT_CLASS (parent_class)->dispose (obj);
252
gst_app_sink_finalize (GObject * obj)
254
GstAppSink *appsink = GST_APP_SINK (obj);
256
g_mutex_free (appsink->mutex);
257
g_cond_free (appsink->cond);
258
g_queue_free (appsink->queue);
260
G_OBJECT_CLASS (parent_class)->finalize (obj);
264
gst_app_sink_set_property (GObject * object, guint prop_id,
265
const GValue * value, GParamSpec * pspec)
267
GstAppSink *appsink = GST_APP_SINK (object);
271
gst_app_sink_set_caps (appsink, gst_value_get_caps (value));
274
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280
gst_app_sink_get_property (GObject * object, guint prop_id, GValue * value,
283
GstAppSink *appsink = GST_APP_SINK (object);
290
caps = gst_app_sink_get_caps (appsink);
291
gst_value_set_caps (value, caps);
293
gst_caps_unref (caps);
297
g_value_set_boolean (value, gst_app_sink_is_eos (appsink));
300
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306
gst_app_sink_flush_unlocked (GstAppSink * appsink)
310
GST_DEBUG_OBJECT (appsink, "flushing appsink");
311
appsink->is_eos = FALSE;
312
gst_buffer_replace (&appsink->preroll, NULL);
313
while ((buffer = g_queue_pop_head (appsink->queue)))
314
gst_buffer_unref (buffer);
315
g_cond_signal (appsink->cond);
319
gst_app_sink_start (GstBaseSink * psink)
321
GstAppSink *appsink = GST_APP_SINK (psink);
323
g_mutex_lock (appsink->mutex);
324
appsink->is_eos = FALSE;
325
appsink->started = TRUE;
326
GST_DEBUG_OBJECT (appsink, "starting");
327
g_mutex_unlock (appsink->mutex);
333
gst_app_sink_stop (GstBaseSink * psink)
335
GstAppSink *appsink = GST_APP_SINK (psink);
337
g_mutex_lock (appsink->mutex);
338
GST_DEBUG_OBJECT (appsink, "stopping");
339
appsink->started = FALSE;
340
gst_app_sink_flush_unlocked (appsink);
341
g_mutex_unlock (appsink->mutex);
347
gst_app_sink_event (GstBaseSink * sink, GstEvent * event)
349
GstAppSink *appsink = GST_APP_SINK (sink);
351
switch (event->type) {
353
g_mutex_lock (appsink->mutex);
354
GST_DEBUG_OBJECT (appsink, "receiving EOS");
355
appsink->is_eos = TRUE;
356
g_cond_signal (appsink->cond);
357
g_mutex_unlock (appsink->mutex);
359
case GST_EVENT_FLUSH_START:
361
case GST_EVENT_FLUSH_STOP:
362
g_mutex_lock (appsink->mutex);
363
GST_DEBUG_OBJECT (appsink, "received FLUSH_STOP");
364
gst_app_sink_flush_unlocked (appsink);
365
g_mutex_unlock (appsink->mutex);
374
gst_app_sink_preroll (GstBaseSink * psink, GstBuffer * buffer)
376
GstAppSink *appsink = GST_APP_SINK (psink);
378
g_mutex_lock (appsink->mutex);
379
GST_DEBUG_OBJECT (appsink, "setting preroll buffer %p", buffer);
380
gst_buffer_replace (&appsink->preroll, buffer);
381
g_cond_signal (appsink->cond);
382
g_mutex_unlock (appsink->mutex);
388
gst_app_sink_render (GstBaseSink * psink, GstBuffer * buffer)
390
GstAppSink *appsink = GST_APP_SINK (psink);
392
g_mutex_lock (appsink->mutex);
393
GST_DEBUG_OBJECT (appsink, "pushing render buffer %p on queue", buffer);
394
g_queue_push_tail (appsink->queue, gst_buffer_ref (buffer));
395
g_cond_signal (appsink->cond);
396
g_mutex_unlock (appsink->mutex);
402
gst_app_sink_getcaps (GstBaseSink * psink)
406
GstAppSink *appsink = GST_APP_SINK (psink);
408
GST_OBJECT_LOCK (appsink);
409
if ((caps = appsink->caps))
411
GST_DEBUG_OBJECT (appsink, "got caps %" GST_PTR_FORMAT, caps);
412
GST_OBJECT_UNLOCK (appsink);
419
plugin_init (GstPlugin * plugin)
421
GST_DEBUG_CATEGORY_INIT (app_sink_debug, "appsink", 0, "Application sink");
423
if (!gst_element_register (plugin, "appsink", GST_RANK_PRIMARY,
424
gst_app_sink_get_type ()))
430
GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR, GST_VERSION_MINOR,
431
"appsink", "Element application sink",
432
plugin_init, VERSION, "LGPL", "Gnash's internal copy of gstappsink", "Gnash")
440
* gst_app_sink_set_caps:
441
* @appsink: a #GstAppSink
444
* Set the capabilities on the appsink element. This function takes
445
* a copy of the caps structure. After calling this method, the sink will only
446
* accept caps that match @caps. If @caps is non-fixed, you must check the caps
447
* on the buffers to get the actual used caps.
450
gst_app_sink_set_caps (GstAppSink * appsink, const GstCaps * caps)
454
g_return_if_fail (appsink != NULL);
455
g_return_if_fail (GST_IS_APP_SINK (appsink));
457
GST_OBJECT_LOCK (appsink);
458
GST_DEBUG_OBJECT (appsink, "setting caps to %" GST_PTR_FORMAT, caps);
461
appsink->caps = gst_caps_copy (caps);
463
appsink->caps = NULL;
465
gst_caps_unref (old);
466
GST_OBJECT_UNLOCK (appsink);
470
* gst_app_sink_get_caps:
471
* @appsink: a #GstAppSink
473
* Get the configured caps on @appsink.
475
* Returns: the #GstCaps accepted by the sink. gst_caps_unref() after usage.
478
gst_app_sink_get_caps (GstAppSink * appsink)
482
g_return_val_if_fail (appsink != NULL, NULL);
483
g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
485
GST_OBJECT_LOCK (appsink);
486
if ((caps = appsink->caps))
488
GST_DEBUG_OBJECT (appsink, "getting caps of %" GST_PTR_FORMAT, caps);
489
GST_OBJECT_UNLOCK (appsink);
495
* gst_app_sink_is_eos:
496
* @appsink: a #GstAppSink
498
* Check if @appsink is EOS, which is when no more buffers can be pulled because
499
* an EOS event was received.
501
* This function also returns %TRUE when the appsink is not in the PAUSED or
504
* Returns: %TRUE if no more buffers can be pulled and the appsink is EOS.
507
gst_app_sink_is_eos (GstAppSink * appsink)
511
g_return_val_if_fail (appsink != NULL, FALSE);
512
g_return_val_if_fail (GST_IS_APP_SINK (appsink), FALSE);
514
g_mutex_lock (appsink->mutex);
515
if (!appsink->started)
518
if (appsink->is_eos && g_queue_is_empty (appsink->queue)) {
519
GST_DEBUG_OBJECT (appsink, "we are EOS and the queue is empty");
522
GST_DEBUG_OBJECT (appsink, "we are not yet EOS");
525
g_mutex_unlock (appsink->mutex);
531
GST_DEBUG_OBJECT (appsink, "we are stopped, return TRUE");
532
g_mutex_unlock (appsink->mutex);
538
* gst_app_sink_pull_preroll:
539
* @appsink: a #GstAppSink
541
* Get the last preroll buffer in @appsink. This was the buffer that caused the
542
* appsink to preroll in the PAUSED state. This buffer can be pulled many times
543
* and remains available to the application even after EOS.
545
* This function is typically used when dealing with a pipeline in the PAUSED
546
* state. Calling this function after doing a seek will give the buffer right
547
* after the seek position.
549
* Note that the preroll buffer will also be returned as the first buffer
550
* when calling gst_app_sink_pull_buffer().
552
* If an EOS event was received before any buffers, this function returns
553
* %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
555
* This function blocks until a preroll buffer or EOS is received or the appsink
556
* element is set to the READY/NULL state.
558
* Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
561
gst_app_sink_pull_preroll (GstAppSink * appsink)
563
GstBuffer *buf = NULL;
565
g_return_val_if_fail (appsink != NULL, NULL);
566
g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
568
g_mutex_lock (appsink->mutex);
571
GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
572
if (!appsink->started)
575
if (appsink->preroll != NULL)
581
/* nothing to return, wait */
582
GST_DEBUG_OBJECT (appsink, "waiting for the preroll buffer");
583
g_cond_wait (appsink->cond, appsink->mutex);
585
buf = gst_buffer_ref (appsink->preroll);
586
GST_DEBUG_OBJECT (appsink, "we have the preroll buffer %p", buf);
587
g_mutex_unlock (appsink->mutex);
591
/* special conditions */
594
GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
595
g_mutex_unlock (appsink->mutex);
600
GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
601
g_mutex_unlock (appsink->mutex);
607
* gst_app_sink_pull_buffer:
608
* @appsink: a #GstAppSink
610
* This function blocks until a buffer or EOS becomes available or the appsink
611
* element is set to the READY/NULL state.
613
* This function will only return buffers when the appsink is in the PLAYING
614
* state. All rendered buffers will be put in a queue so that the application
615
* can pull buffers at its own rate. Note that when the application does not
616
* pull buffers fast enough, the queued buffers could consume a lot of memory,
617
* especially when dealing with raw video frames.
619
* If an EOS event was received before any buffers, this function returns
620
* %NULL. Use gst_app_sink_is_eos () to check for the EOS condition.
622
* Returns: a #GstBuffer or NULL when the appsink is stopped or EOS.
625
gst_app_sink_pull_buffer (GstAppSink * appsink)
627
GstBuffer *buf = NULL;
629
g_return_val_if_fail (appsink != NULL, NULL);
630
g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
632
g_mutex_lock (appsink->mutex);
635
GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
636
if (!appsink->started)
639
if (!g_queue_is_empty (appsink->queue))
645
/* nothing to return, wait */
646
GST_DEBUG_OBJECT (appsink, "waiting for a buffer");
647
g_cond_wait (appsink->cond, appsink->mutex);
649
buf = g_queue_pop_head (appsink->queue);
650
GST_DEBUG_OBJECT (appsink, "we have a buffer %p", buf);
651
g_mutex_unlock (appsink->mutex);
655
/* special conditions */
658
GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
659
g_mutex_unlock (appsink->mutex);
664
GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
665
g_mutex_unlock (appsink->mutex);
672
* gst_app_sink_pull_buffer_timed:
673
* @appsink: a #GstAppSink
675
* This function blocks until a buffer or EOS becomes available or the appsink
676
* element is set to the READY/NULL state, or if one second elapses.
678
* This function will only return buffers when the appsink is in the PLAYING
679
* state. All rendered buffers will be put in a queue so that the application
680
* can pull buffers at its own rate.
682
* If an EOS event was received before any buffers, or if one second passes and
683
* no data arrives, this function returns * %NULL. Use gst_app_sink_is_eos ()
684
* to check for the EOS condition.
686
* Returns: a #GstBuffer or NULL when the appsink is stopped or EOS, or one
690
gst_app_sink_pull_buffer_timed (GstAppSink * appsink)
692
GstBuffer *buf = NULL;
694
g_return_val_if_fail (appsink != NULL, NULL);
695
g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
697
g_mutex_lock (appsink->mutex);
700
GST_DEBUG_OBJECT (appsink, "trying to grab a buffer");
701
if (!appsink->started)
704
if (!g_queue_is_empty (appsink->queue))
710
/* nothing to return, wait */
711
GST_DEBUG_OBJECT (appsink, "waiting for a buffer");
714
g_get_current_time(&cur_time);
715
cur_time.tv_sec++; // Add one second
717
if (!g_cond_timed_wait(appsink->cond, appsink->mutex, &cur_time))
721
buf = g_queue_pop_head (appsink->queue);
722
GST_DEBUG_OBJECT (appsink, "we have a buffer %p", buf);
723
g_mutex_unlock (appsink->mutex);
727
/* special conditions */
730
GST_DEBUG_OBJECT (appsink, "we timed out, return NULL");
731
g_mutex_unlock (appsink->mutex);
736
GST_DEBUG_OBJECT (appsink, "we are EOS, return NULL");
737
g_mutex_unlock (appsink->mutex);
742
GST_DEBUG_OBJECT (appsink, "we are stopped, return NULL");
743
g_mutex_unlock (appsink->mutex);
751
* gst_app_sink_peek_buffer:
752
* @appsink: a #GstAppSink
754
* This function peeks the queue to see if at least one buffer is in the queue.
756
* Returns: TRUE if there is a buffer in the queue.
759
gst_app_sink_peek_buffer (GstAppSink * appsink)
761
g_return_val_if_fail (appsink != NULL, NULL);
762
g_return_val_if_fail (GST_IS_APP_SINK (appsink), NULL);
764
g_mutex_lock (appsink->mutex);
766
gboolean got_data = !g_queue_is_empty(appsink->queue);
768
g_mutex_unlock (appsink->mutex);