2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransformc, #GstBaseSource
27 * This class is for elements that do output operations.
30 * <listitem><para>one sinkpad</para></listitem>
31 * <listitem><para>handles state changes</para></listitem>
32 * <listitem><para>pull/push mode</para></listitem>
33 * <listitem><para>handles seeking/query</para></listitem>
34 * <listitem><para>handles preroll</para></listitem>
35 * <listitem><para>EOS handling</para></listitem>
43 #include "gstbasesink.h"
44 #include <gst/gstmarshal.h>
46 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
47 #define GST_CAT_DEFAULT gst_base_sink_debug
49 /* BaseSink signals and properties */
57 /* FIXME, need to figure out a better way to handle the pull mode */
58 #define DEFAULT_SIZE 1024
59 #define DEFAULT_HAS_LOOP FALSE
60 #define DEFAULT_HAS_CHAIN TRUE
67 PROP_PREROLL_QUEUE_LEN
70 static GstElementClass *parent_class = NULL;
72 static void gst_base_sink_base_init (gpointer g_class);
73 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
74 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
75 static void gst_base_sink_finalize (GObject * object);
78 gst_base_sink_get_type (void)
80 static GType base_sink_type = 0;
82 if (!base_sink_type) {
83 static const GTypeInfo base_sink_info = {
84 sizeof (GstBaseSinkClass),
85 (GBaseInitFunc) gst_base_sink_base_init,
87 (GClassInitFunc) gst_base_sink_class_init,
92 (GInstanceInitFunc) gst_base_sink_init,
95 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
96 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
98 return base_sink_type;
101 static void gst_base_sink_set_clock (GstElement * element, GstClock * clock);
103 static void gst_base_sink_set_property (GObject * object, guint prop_id,
104 const GValue * value, GParamSpec * pspec);
105 static void gst_base_sink_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec);
108 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
109 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
110 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
111 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
112 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
113 GstClockTime * start, GstClockTime * end);
115 static GstElementStateReturn gst_base_sink_change_state (GstElement * element);
117 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
118 static void gst_base_sink_loop (GstPad * pad);
119 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
120 static gboolean gst_base_sink_activate_push (GstPad * pad, gboolean active);
121 static gboolean gst_base_sink_activate_pull (GstPad * pad, gboolean active);
122 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
123 static inline GstFlowReturn gst_base_sink_handle_buffer (GstBaseSink * basesink,
125 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
129 gst_base_sink_base_init (gpointer g_class)
131 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
136 gst_base_sink_class_init (GstBaseSinkClass * klass)
138 GObjectClass *gobject_class;
139 GstElementClass *gstelement_class;
141 gobject_class = (GObjectClass *) klass;
142 gstelement_class = (GstElementClass *) klass;
144 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
146 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
147 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
148 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
150 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
151 g_param_spec_boolean ("has-loop", "has-loop",
152 "Enable loop-based operation", DEFAULT_HAS_LOOP,
153 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
154 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_CHAIN,
155 g_param_spec_boolean ("has-chain", "has-chain",
156 "Enable chain-based operation", DEFAULT_HAS_CHAIN,
157 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
158 /* FIXME, this next value should be configured using an event from the
159 * upstream element */
160 g_object_class_install_property (G_OBJECT_CLASS (klass),
161 PROP_PREROLL_QUEUE_LEN,
162 g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
163 "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
164 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
166 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_base_sink_set_clock);
167 gstelement_class->change_state =
168 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
170 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
171 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
172 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
173 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
177 gst_base_sink_pad_getcaps (GstPad * pad)
179 GstBaseSinkClass *bclass;
181 GstCaps *caps = NULL;
183 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
184 bclass = GST_BASE_SINK_GET_CLASS (bsink);
185 if (bclass->get_caps)
186 caps = bclass->get_caps (bsink);
189 GstPadTemplate *pad_template;
192 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
193 if (pad_template != NULL) {
194 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
197 gst_object_unref (bsink);
203 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
205 GstBaseSinkClass *bclass;
207 gboolean res = FALSE;
209 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
210 bclass = GST_BASE_SINK_GET_CLASS (bsink);
212 if (bclass->set_caps)
213 res = bclass->set_caps (bsink, caps);
215 gst_object_unref (bsink);
221 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
222 GstCaps * caps, GstBuffer ** buf)
224 GstBaseSinkClass *bclass;
226 GstFlowReturn result = GST_FLOW_OK;
228 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
229 bclass = GST_BASE_SINK_GET_CLASS (bsink);
231 if (bclass->buffer_alloc)
232 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
236 gst_object_unref (bsink);
242 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
244 GstPadTemplate *pad_template;
247 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
248 g_return_if_fail (pad_template != NULL);
250 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
252 gst_pad_set_getcaps_function (basesink->sinkpad,
253 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
254 gst_pad_set_setcaps_function (basesink->sinkpad,
255 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
256 gst_pad_set_bufferalloc_function (basesink->sinkpad,
257 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
258 gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
260 basesink->pad_mode = GST_ACTIVATE_NONE;
261 GST_PAD_TASK (basesink->sinkpad) = NULL;
262 basesink->preroll_queue = g_queue_new ();
264 GST_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
268 gst_base_sink_finalize (GObject * object)
270 GstBaseSink *basesink;
272 basesink = GST_BASE_SINK (object);
274 g_queue_free (basesink->preroll_queue);
276 G_OBJECT_CLASS (parent_class)->finalize (object);
280 gst_base_sink_set_pad_functions (GstBaseSink * this, GstPad * pad)
282 gst_pad_set_activatepush_function (pad,
283 GST_DEBUG_FUNCPTR (gst_base_sink_activate_push));
284 gst_pad_set_activatepull_function (pad,
285 GST_DEBUG_FUNCPTR (gst_base_sink_activate_pull));
286 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_base_sink_event));
289 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_base_sink_chain));
291 gst_pad_set_chain_function (pad, NULL);
295 gst_base_sink_set_all_pad_functions (GstBaseSink * this)
299 for (l = GST_ELEMENT_PADS (this); l; l = l->next)
300 gst_base_sink_set_pad_functions (this, (GstPad *) l->data);
304 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
308 sink = GST_BASE_SINK (element);
314 gst_base_sink_set_property (GObject * object, guint prop_id,
315 const GValue * value, GParamSpec * pspec)
319 sink = GST_BASE_SINK (object);
324 sink->has_loop = g_value_get_boolean (value);
325 gst_base_sink_set_all_pad_functions (sink);
330 sink->has_chain = g_value_get_boolean (value);
331 gst_base_sink_set_all_pad_functions (sink);
334 case PROP_PREROLL_QUEUE_LEN:
335 /* preroll lock necessary to serialize with finish_preroll */
336 GST_PREROLL_LOCK (sink->sinkpad);
337 sink->preroll_queue_max_len = g_value_get_uint (value);
338 GST_PREROLL_UNLOCK (sink->sinkpad);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
352 sink = GST_BASE_SINK (object);
357 g_value_set_boolean (value, sink->has_loop);
360 g_value_set_boolean (value, sink->has_chain);
362 case PROP_PREROLL_QUEUE_LEN:
363 g_value_set_uint (value, sink->preroll_queue_max_len);
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gst_base_sink_get_caps (GstBaseSink * sink)
379 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
385 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
386 GstCaps * caps, GstBuffer ** buf)
392 /* with PREROLL_LOCK */
394 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
397 GQueue *q = basesink->preroll_queue;
403 GST_DEBUG ("emptying queue");
404 while ((obj = g_queue_pop_head (q))) {
407 is_buffer = GST_IS_BUFFER (obj);
409 basesink->preroll_queued--;
410 basesink->buffers_queued--;
412 switch (GST_EVENT_TYPE (obj)) {
414 basesink->preroll_queued--;
419 basesink->events_queued--;
421 /* we release the preroll lock while pushing so that we
422 * can still flush it while blocking on the clock or
423 * inside the element. */
424 GST_PREROLL_UNLOCK (pad);
427 GST_DEBUG ("popped buffer %p", obj);
428 ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER (obj));
430 GST_DEBUG ("popped event %p", obj);
431 gst_base_sink_handle_event (basesink, GST_EVENT (obj));
435 GST_PREROLL_LOCK (pad);
437 GST_DEBUG ("queue empty");
442 /* with PREROLL_LOCK */
444 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
447 GQueue *q = basesink->preroll_queue;
449 GST_DEBUG ("flushing queue %p", basesink);
451 while ((obj = g_queue_pop_head (q))) {
452 GST_DEBUG ("popped %p", obj);
453 gst_mini_object_unref (obj);
456 /* we can't have EOS anymore now */
457 basesink->eos = FALSE;
458 basesink->preroll_queued = 0;
459 basesink->buffers_queued = 0;
460 basesink->events_queued = 0;
461 basesink->have_preroll = FALSE;
462 /* and signal any waiters now */
463 GST_PREROLL_SIGNAL (pad);
466 /* with STREAM_LOCK */
468 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
474 GST_PREROLL_LOCK (pad);
475 /* push object on the queue */
476 GST_DEBUG ("push on queue %p", basesink, obj);
477 g_queue_push_tail (basesink->preroll_queue, obj);
479 have_event = GST_IS_EVENT (obj);
481 GstEvent *event = GST_EVENT (obj);
483 switch (GST_EVENT_TYPE (obj)) {
485 basesink->preroll_queued++;
486 basesink->eos = TRUE;
488 case GST_EVENT_NEWSEGMENT:
493 /* the newsegment event is needed to bring the buffer timestamps to the
495 gst_event_parse_newsegment (event, &rate, &format,
496 &basesink->discont_start, &basesink->discont_stop, NULL);
498 if (format != GST_FORMAT_TIME) {
499 /* this means this sink will not be able to sync to the clock */
500 basesink->discont_start = 0;
501 basesink->discont_stop = 0;
503 basesink->have_discont = TRUE;
505 GST_DEBUG ("received DISCONT %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
506 GST_TIME_ARGS (basesink->discont_start),
507 GST_TIME_ARGS (basesink->discont_stop));
513 basesink->events_queued++;
515 if (!basesink->have_discont) {
516 GST_ELEMENT_ERROR (basesink, STREAM, STOPPED,
517 ("received buffer without a discont"),
518 ("received buffer without a discont"));
520 basesink->preroll_queued++;
521 basesink->buffers_queued++;
523 GST_DEBUG ("now %d preroll, %d buffers, %d events on queue",
524 basesink->preroll_queued,
525 basesink->buffers_queued, basesink->events_queued);
527 /* check if we are prerolling */
528 if (!basesink->need_preroll)
531 /* there is a buffer queued */
532 if (basesink->buffers_queued == 1) {
533 GST_DEBUG ("do preroll %p", obj);
535 /* if it's a buffer, we need to call the preroll method */
536 if (GST_IS_BUFFER (obj)) {
537 GstBaseSinkClass *bclass;
540 bclass = GST_BASE_SINK_GET_CLASS (basesink);
543 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
547 length = basesink->preroll_queued;
548 GST_DEBUG ("prerolled length %d", length);
553 basesink->have_preroll = TRUE;
554 /* we are prerolling */
555 GST_PREROLL_UNLOCK (pad);
557 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
558 * inside the STREAM_LOCK */
559 t = GST_STREAM_UNLOCK_FULL (pad);
560 GST_DEBUG ("released stream lock %d times", t);
562 GST_WARNING ("STREAM_LOCK should have been locked !!");
563 g_warning ("STREAM_LOCK should have been locked !!");
566 /* now we commit our state */
567 GST_STATE_LOCK (basesink);
568 GST_DEBUG ("commit state %p >", basesink);
569 gst_element_commit_state (GST_ELEMENT (basesink));
570 GST_STATE_UNLOCK (basesink);
572 /* reacquire stream lock, pad could be flushing now */
573 /* FIXME in glib, if t==0, the lock is still taken... hmmm */
575 GST_STREAM_LOCK_FULL (pad, t);
577 /* and wait if needed */
578 GST_PREROLL_LOCK (pad);
581 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
585 /* it is possible that the application set the state to PLAYING
586 * now in which case we don't need to block anymore. */
587 if (!basesink->need_preroll)
590 length = basesink->preroll_queued;
592 g_assert (length == 1);
595 /* see if we need to block now. We cannot block on events, only
596 * on buffers, the reason is that events can be sent from the
597 * application thread and we don't want to block there. */
598 if (length > basesink->preroll_queue_max_len && !have_event) {
599 /* block until the state changes, or we get a flush, or something */
600 GST_DEBUG ("element %s waiting to finish preroll",
601 GST_ELEMENT_NAME (basesink));
602 GST_PREROLL_WAIT (pad);
603 GST_DEBUG ("done preroll");
606 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
610 GST_PREROLL_UNLOCK (pad);
618 GST_DEBUG ("no preroll needed");
619 /* maybe it was another sink that blocked in preroll, need to check for
621 basesink->have_preroll = FALSE;
622 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
623 GST_PREROLL_UNLOCK (pad);
630 gst_base_sink_preroll_queue_flush (basesink, pad);
631 GST_PREROLL_UNLOCK (pad);
632 GST_DEBUG ("pad is flushing");
633 return GST_FLOW_WRONG_STATE;
639 GST_DEBUG ("preroll failed");
640 gst_base_sink_preroll_queue_flush (basesink, pad);
641 GST_PREROLL_UNLOCK (pad);
643 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
644 * inside the STREAM_LOCK */
645 t = GST_STREAM_UNLOCK_FULL (pad);
646 GST_DEBUG ("released stream lock %d times", t);
648 GST_WARNING ("STREAM_LOCK should have been locked !!");
649 g_warning ("STREAM_LOCK should have been locked !!");
652 /* now we abort our state */
653 GST_STATE_LOCK (basesink);
654 GST_DEBUG ("abort state %p >", basesink);
655 gst_element_abort_state (GST_ELEMENT (basesink));
656 GST_STATE_UNLOCK (basesink);
658 /* reacquire stream lock, pad could be flushing now */
660 GST_STREAM_LOCK_FULL (pad, t);
662 return GST_FLOW_ERROR;
667 gst_base_sink_event (GstPad * pad, GstEvent * event)
669 GstBaseSink *basesink;
670 gboolean result = TRUE;
671 GstBaseSinkClass *bclass;
673 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
675 bclass = GST_BASE_SINK_GET_CLASS (basesink);
677 GST_DEBUG ("event %p", event);
679 switch (GST_EVENT_TYPE (event)) {
684 GST_STREAM_LOCK (pad);
685 /* EOS also finishes the preroll */
687 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
688 GST_STREAM_UNLOCK (pad);
691 case GST_EVENT_NEWSEGMENT:
695 GST_STREAM_LOCK (pad);
697 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
698 GST_STREAM_UNLOCK (pad);
701 case GST_EVENT_FLUSH_START:
702 /* make sure we are not blocked on the clock also clear any pending
705 bclass->event (basesink, event);
707 GST_PREROLL_LOCK (pad);
708 /* we need preroll after the flush */
709 basesink->need_preroll = TRUE;
710 /* unlock from a possible state change/preroll */
711 gst_base_sink_preroll_queue_flush (basesink, pad);
714 if (basesink->clock_id) {
715 gst_clock_id_unschedule (basesink->clock_id);
717 GST_UNLOCK (basesink);
718 GST_PREROLL_UNLOCK (pad);
720 /* and we need to commit our state again on the next
721 * prerolled buffer */
722 GST_STATE_LOCK (basesink);
723 GST_STREAM_LOCK (pad);
724 gst_element_lost_state (GST_ELEMENT (basesink));
725 GST_STREAM_UNLOCK (pad);
726 GST_STATE_UNLOCK (basesink);
727 GST_DEBUG ("event unref %p %p", basesink, event);
728 gst_event_unref (event);
730 case GST_EVENT_FLUSH_STOP:
732 bclass->event (basesink, event);
734 /* now we are completely unblocked and the _chain method
736 GST_STREAM_LOCK (pad);
737 GST_STREAM_UNLOCK (pad);
739 GST_DEBUG ("event unref %p %p", basesink, event);
740 gst_event_unref (event);
743 gst_event_unref (event);
746 gst_object_unref (basesink);
751 /* default implementation to calculate the start and end
752 * timestamps on a buffer, subclasses can override
755 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
756 GstClockTime * start, GstClockTime * end)
758 GstClockTime timestamp, duration;
760 timestamp = GST_BUFFER_TIMESTAMP (buffer);
761 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
762 GstClockTimeDiff diff;
764 /* bring timestamp to stream time using last
766 if ((diff = timestamp - basesink->discont_start) < 0)
769 /* get duration to calculate end time */
770 duration = GST_BUFFER_DURATION (buffer);
771 if (GST_CLOCK_TIME_IS_VALID (duration)) {
772 *end = diff + duration;
780 *start = GST_CLOCK_TIME_NONE;
781 *end = GST_CLOCK_TIME_NONE;
785 /* perform synchronisation on a buffer
787 * 1) check if we have a clock, if not, do nothing
788 * 2) calculate the start and end time of the buffer
789 * 3) create a single shot notification to wait on
790 * the clock, save the entry so we can unlock it
791 * 4) wait on the clock, this blocks
792 * 5) unref the clockid again
795 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
797 gboolean result = TRUE;
799 if (basesink->clock) {
800 GstClockTime start, end;
801 GstBaseSinkClass *bclass;
803 bclass = GST_BASE_SINK_GET_CLASS (basesink);
805 if (bclass->get_times)
806 bclass->get_times (basesink, buffer, &start, &end);
808 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
809 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
811 if (GST_CLOCK_TIME_IS_VALID (start)) {
813 GstClockTime base_time;
816 base_time = GST_ELEMENT (basesink)->base_time;
818 GST_LOG_OBJECT (basesink,
819 "waiting for clock, base time %" GST_TIME_FORMAT,
820 GST_TIME_ARGS (base_time));
821 /* save clock id so that we can unlock it if needed */
822 basesink->clock_id = gst_clock_new_single_shot_id (basesink->clock,
824 basesink->end_time = end;
825 GST_UNLOCK (basesink);
827 ret = gst_clock_id_wait (basesink->clock_id, NULL);
830 if (basesink->clock_id) {
831 gst_clock_id_unref (basesink->clock_id);
832 basesink->clock_id = NULL;
834 GST_UNLOCK (basesink);
836 GST_LOG_OBJECT (basesink, "clock entry done: %d", ret);
837 if (ret == GST_CLOCK_UNSCHEDULED)
847 * 2) render the event
850 static inline gboolean
851 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
853 GstBaseSinkClass *bclass;
856 switch (GST_EVENT_TYPE (event)) {
859 if (basesink->clock) {
860 /* wait for last buffer to finish if we have a valid end time */
861 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
862 basesink->clock_id = gst_clock_new_single_shot_id (basesink->clock,
863 basesink->end_time + GST_ELEMENT (basesink)->base_time);
864 GST_UNLOCK (basesink);
866 gst_clock_id_wait (basesink->clock_id, NULL);
869 if (basesink->clock_id) {
870 gst_clock_id_unref (basesink->clock_id);
871 basesink->clock_id = NULL;
873 basesink->end_time = GST_CLOCK_TIME_NONE;
876 GST_UNLOCK (basesink);
882 bclass = GST_BASE_SINK_GET_CLASS (basesink);
884 ret = bclass->event (basesink, event);
888 switch (GST_EVENT_TYPE (event)) {
890 GST_PREROLL_LOCK (basesink->sinkpad);
891 /* if we are still EOS, we can post the EOS message */
893 /* ok, now we can post the message */
894 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
895 gst_element_post_message (GST_ELEMENT (basesink),
896 gst_message_new_eos (GST_OBJECT (basesink)));
898 GST_PREROLL_UNLOCK (basesink->sinkpad);
904 GST_DEBUG ("event unref %p %p", basesink, event);
905 gst_event_unref (event);
912 * 1) first sync on the buffer
913 * 2) render the buffer
914 * 3) unref the buffer
916 static inline GstFlowReturn
917 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
919 GstBaseSinkClass *bclass;
922 gst_base_sink_do_sync (basesink, buf);
924 bclass = GST_BASE_SINK_GET_CLASS (basesink);
926 ret = bclass->render (basesink, buf);
930 GST_DEBUG ("buffer unref after render %p", basesink, buf);
931 gst_buffer_unref (buf);
937 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
939 GstBaseSink *basesink;
940 GstFlowReturn result;
942 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
944 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
946 gst_object_unref (basesink);
951 /* FIXME, not all sinks can operate in pull mode
954 gst_base_sink_loop (GstPad * pad)
956 GstBaseSink *basesink;
957 GstBuffer *buf = NULL;
958 GstFlowReturn result;
960 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
962 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
964 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
965 if (result != GST_FLOW_OK)
968 result = gst_base_sink_chain (pad, buf);
969 if (result != GST_FLOW_OK)
972 gst_object_unref (basesink);
979 gst_object_unref (basesink);
980 gst_pad_pause_task (pad);
986 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
988 gboolean result = FALSE;
989 GstBaseSinkClass *bclass;
991 bclass = GST_BASE_SINK_GET_CLASS (basesink);
993 /* step 1, unblock clock sync (if any) or any other blocking thing */
994 GST_PREROLL_LOCK (pad);
996 if (basesink->clock_id) {
997 gst_clock_id_unschedule (basesink->clock_id);
999 GST_UNLOCK (basesink);
1001 /* unlock any subclasses */
1003 bclass->unlock (basesink);
1005 /* flush out the data thread if it's locked in finish_preroll */
1006 basesink->need_preroll = FALSE;
1007 gst_base_sink_preroll_queue_flush (basesink, pad);
1008 GST_PREROLL_SIGNAL (pad);
1009 GST_PREROLL_UNLOCK (pad);
1011 /* step 2, make sure streaming finishes */
1012 result = gst_pad_stop_task (pad);
1018 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1020 gboolean result = FALSE;
1021 GstBaseSink *basesink;
1023 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1026 if (!basesink->has_chain)
1030 result = gst_base_sink_deactivate (basesink, pad);
1032 basesink->pad_mode = GST_ACTIVATE_PUSH;
1035 gst_object_unref (basesink);
1040 /* this won't get called until we implement an activate function */
1042 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1044 gboolean result = FALSE;
1045 GstBaseSink *basesink;
1047 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1050 /* if we have a scheduler we can start the task */
1051 if (!basesink->has_loop)
1054 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop, pad);
1056 result = gst_base_sink_deactivate (basesink, pad);
1059 gst_object_unref (basesink);
1064 static GstElementStateReturn
1065 gst_base_sink_change_state (GstElement * element)
1067 GstElementStateReturn ret = GST_STATE_SUCCESS;
1068 GstBaseSink *basesink = GST_BASE_SINK (element);
1069 GstElementState transition = GST_STATE_TRANSITION (element);
1070 GstBaseSinkClass *bclass;
1072 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1074 switch (transition) {
1075 case GST_STATE_NULL_TO_READY:
1077 if (!bclass->start (basesink))
1080 case GST_STATE_READY_TO_PAUSED:
1081 /* need to complete preroll before this state change completes, there
1082 * is no data flow in READY so we can safely assume we need to preroll. */
1083 basesink->offset = 0;
1084 GST_PREROLL_LOCK (basesink->sinkpad);
1085 basesink->have_preroll = FALSE;
1086 basesink->need_preroll = TRUE;
1087 GST_PREROLL_UNLOCK (basesink->sinkpad);
1088 basesink->have_discont = FALSE;
1089 basesink->discont_start = 0;
1090 basesink->discont_stop = 0;
1091 ret = GST_STATE_ASYNC;
1093 case GST_STATE_PAUSED_TO_PLAYING:
1095 GST_PREROLL_LOCK (basesink->sinkpad);
1096 /* if we have EOS, we should empty the queue now as there will
1097 * be no more data received in the chain function.
1098 * FIXME, this could block the state change function too long when
1099 * we are pushing and syncing the buffers, better start a new
1100 * thread to do this. */
1101 if (basesink->eos) {
1102 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1104 /* don't need the preroll anymore */
1105 basesink->need_preroll = FALSE;
1106 /* now let it play */
1107 GST_PREROLL_SIGNAL (basesink->sinkpad);
1108 GST_PREROLL_UNLOCK (basesink->sinkpad);
1115 GST_ELEMENT_CLASS (parent_class)->change_state (element);
1117 switch (transition) {
1118 case GST_STATE_PLAYING_TO_PAUSED:
1120 GstBaseSinkClass *bclass;
1122 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1124 GST_PREROLL_LOCK (basesink->sinkpad);
1125 GST_LOCK (basesink);
1126 /* unlock clock wait if any */
1127 if (basesink->clock_id) {
1128 gst_clock_id_unschedule (basesink->clock_id);
1130 GST_UNLOCK (basesink);
1132 /* unlock any subclasses */
1134 bclass->unlock (basesink);
1136 /* if we don't have a preroll buffer and we have not received EOS,
1137 * we need to wait for a preroll */
1138 GST_DEBUG ("have_preroll: %d, EOS: %d", basesink->have_preroll,
1140 if (!basesink->have_preroll && !basesink->eos) {
1141 basesink->need_preroll = TRUE;
1142 ret = GST_STATE_ASYNC;
1144 GST_PREROLL_UNLOCK (basesink->sinkpad);
1147 case GST_STATE_PAUSED_TO_READY:
1149 case GST_STATE_READY_TO_NULL:
1151 if (!bclass->stop (basesink)) {
1152 GST_WARNING ("failed to stop");
1164 GST_DEBUG ("failed to start");
1165 return GST_STATE_FAILURE;