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: #GstBaseTransform, #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 #define DEFAULT_SIZE 1024
58 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
59 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
61 #define DEFAULT_SYNC TRUE
66 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 GstStateChangeReturn do_playing (GstBaseSink * sink);
103 static void gst_base_sink_set_clock (GstElement * element, GstClock * clock);
105 static void gst_base_sink_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_base_sink_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
110 static gboolean gst_base_sink_send_event (GstElement * element,
112 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
114 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
115 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
116 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
117 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
118 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
119 GstClockTime * start, GstClockTime * end);
121 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
122 GstStateChange transition);
124 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
125 static void gst_base_sink_loop (GstPad * pad);
126 static gboolean gst_base_sink_activate (GstPad * pad);
127 static gboolean gst_base_sink_activate_push (GstPad * pad, gboolean active);
128 static gboolean gst_base_sink_activate_pull (GstPad * pad, gboolean active);
129 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
130 static inline GstFlowReturn gst_base_sink_handle_buffer (GstBaseSink * basesink,
132 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
136 gst_base_sink_base_init (gpointer g_class)
138 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
143 gst_base_sink_class_init (GstBaseSinkClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
151 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
153 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
154 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
155 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
157 /* FIXME, this next value should be configured using an event from the
158 * upstream element */
159 g_object_class_install_property (G_OBJECT_CLASS (klass),
160 PROP_PREROLL_QUEUE_LEN,
161 g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
162 "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
163 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
164 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
165 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
168 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_base_sink_set_clock);
169 gstelement_class->change_state =
170 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
171 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
172 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
174 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
175 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
176 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
177 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
181 gst_base_sink_pad_getcaps (GstPad * pad)
183 GstBaseSinkClass *bclass;
185 GstCaps *caps = NULL;
187 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
188 bclass = GST_BASE_SINK_GET_CLASS (bsink);
189 if (bclass->get_caps)
190 caps = bclass->get_caps (bsink);
193 GstPadTemplate *pad_template;
196 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
197 if (pad_template != NULL) {
198 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
201 gst_object_unref (bsink);
207 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
209 GstBaseSinkClass *bclass;
211 gboolean res = FALSE;
213 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
214 bclass = GST_BASE_SINK_GET_CLASS (bsink);
216 if (bclass->set_caps)
217 res = bclass->set_caps (bsink, caps);
219 gst_object_unref (bsink);
225 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
226 GstCaps * caps, GstBuffer ** buf)
228 GstBaseSinkClass *bclass;
230 GstFlowReturn result = GST_FLOW_OK;
232 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
233 bclass = GST_BASE_SINK_GET_CLASS (bsink);
235 if (bclass->buffer_alloc)
236 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
238 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
240 gst_object_unref (bsink);
246 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
248 GstPadTemplate *pad_template;
251 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
252 g_return_if_fail (pad_template != NULL);
254 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
256 gst_pad_set_getcaps_function (basesink->sinkpad,
257 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
258 gst_pad_set_setcaps_function (basesink->sinkpad,
259 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
260 gst_pad_set_bufferalloc_function (basesink->sinkpad,
261 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
262 gst_pad_set_activate_function (basesink->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_base_sink_activate));
264 gst_pad_set_activatepush_function (basesink->sinkpad,
265 GST_DEBUG_FUNCPTR (gst_base_sink_activate_push));
266 gst_pad_set_activatepull_function (basesink->sinkpad,
267 GST_DEBUG_FUNCPTR (gst_base_sink_activate_pull));
268 gst_pad_set_event_function (basesink->sinkpad,
269 GST_DEBUG_FUNCPTR (gst_base_sink_event));
270 gst_pad_set_chain_function (basesink->sinkpad,
271 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
272 gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
274 basesink->pad_mode = GST_ACTIVATE_NONE;
275 GST_PAD_TASK (basesink->sinkpad) = NULL;
276 basesink->preroll_queue = g_queue_new ();
278 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
279 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
281 basesink->sync = DEFAULT_SYNC;
283 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
287 gst_base_sink_finalize (GObject * object)
289 GstBaseSink *basesink;
291 basesink = GST_BASE_SINK (object);
293 g_queue_free (basesink->preroll_queue);
295 G_OBJECT_CLASS (parent_class)->finalize (object);
299 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
303 sink = GST_BASE_SINK (element);
309 gst_base_sink_set_property (GObject * object, guint prop_id,
310 const GValue * value, GParamSpec * pspec)
312 GstBaseSink *sink = GST_BASE_SINK (object);
315 case PROP_PREROLL_QUEUE_LEN:
316 /* preroll lock necessary to serialize with finish_preroll */
317 GST_PREROLL_LOCK (sink->sinkpad);
318 sink->preroll_queue_max_len = g_value_get_uint (value);
319 GST_PREROLL_UNLOCK (sink->sinkpad);
322 sink->sync = g_value_get_boolean (value);
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
334 GstBaseSink *sink = GST_BASE_SINK (object);
338 case PROP_PREROLL_QUEUE_LEN:
339 g_value_set_uint (value, sink->preroll_queue_max_len);
342 g_value_set_boolean (value, sink->sync);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
352 gst_base_sink_get_caps (GstBaseSink * sink)
358 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
364 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
365 GstCaps * caps, GstBuffer ** buf)
371 /* with PREROLL_LOCK */
373 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
376 GQueue *q = basesink->preroll_queue;
382 GST_DEBUG_OBJECT (basesink, "emptying queue");
383 while ((obj = g_queue_pop_head (q))) {
386 is_buffer = GST_IS_BUFFER (obj);
388 basesink->preroll_queued--;
389 basesink->buffers_queued--;
391 switch (GST_EVENT_TYPE (obj)) {
393 basesink->preroll_queued--;
398 basesink->events_queued--;
400 /* we release the preroll lock while pushing so that we
401 * can still flush it while blocking on the clock or
402 * inside the element. */
403 GST_PREROLL_UNLOCK (pad);
406 GST_DEBUG_OBJECT (basesink, "popped buffer %p", obj);
407 ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER (obj));
409 GST_DEBUG_OBJECT (basesink, "popped event %p", obj);
410 gst_base_sink_handle_event (basesink, GST_EVENT (obj));
414 GST_PREROLL_LOCK (pad);
416 GST_DEBUG_OBJECT (basesink, "queue empty");
421 /* with PREROLL_LOCK */
423 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
426 GQueue *q = basesink->preroll_queue;
428 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
430 while ((obj = g_queue_pop_head (q))) {
431 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
432 gst_mini_object_unref (obj);
435 /* we can't have EOS anymore now */
436 basesink->eos = FALSE;
437 basesink->eos_queued = FALSE;
438 basesink->preroll_queued = 0;
439 basesink->buffers_queued = 0;
440 basesink->events_queued = 0;
441 basesink->have_preroll = FALSE;
442 /* and signal any waiters now */
443 GST_PREROLL_SIGNAL (pad);
447 gst_base_sink_commit_state (GstBaseSink * basesink)
449 /* commit state and proceed to next pending state */
451 GstState current, next, pending;
453 gboolean post_paused = FALSE;
454 gboolean post_playing = FALSE;
457 current = GST_STATE (basesink);
458 next = GST_STATE_NEXT (basesink);
459 pending = GST_STATE_PENDING (basesink);
462 case GST_STATE_PLAYING:
463 do_playing (basesink);
466 case GST_STATE_PAUSED:
467 basesink->need_preroll = TRUE;
470 case GST_STATE_READY:
476 if (pending != GST_STATE_VOID_PENDING) {
477 GST_STATE (basesink) = pending;
478 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
479 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
480 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
482 pending = GST_STATE_VOID_PENDING;
484 GST_UNLOCK (basesink);
487 message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
488 current, next, GST_STATE_VOID_PENDING);
489 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
492 message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
493 next, pending, GST_STATE_VOID_PENDING);
494 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
497 if (post_paused || post_playing) {
498 gst_element_post_message (GST_ELEMENT_CAST (basesink),
499 gst_message_new_state_dirty (GST_OBJECT_CAST (basesink)));
502 GST_STATE_BROADCAST (basesink);
508 /* app is going to READY */
509 GST_UNLOCK (basesink);
514 /* with STREAM_LOCK */
516 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
522 GST_PREROLL_LOCK (pad);
523 /* push object on the queue */
524 GST_DEBUG_OBJECT (basesink, "push %p on preroll_queue", obj);
525 g_queue_push_tail (basesink->preroll_queue, obj);
527 have_event = GST_IS_EVENT (obj);
529 GstEvent *event = GST_EVENT (obj);
531 switch (GST_EVENT_TYPE (obj)) {
533 basesink->preroll_queued++;
534 basesink->eos = TRUE;
535 basesink->eos_queued = TRUE;
537 case GST_EVENT_NEWSEGMENT:
542 gint64 segment_start;
545 GstClockTime duration;
548 /* the newsegment event is needed to bring the buffer timestamps to the
549 * stream time and to drop samples outside of the playback segment. */
550 gst_event_parse_newsegment (event, &update, &rate, &format,
551 &segment_start, &segment_stop, &segment_time);
553 basesink->have_newsegment = TRUE;
555 /* any other format with 0 also gives time 0, the other values are
556 * invalid as time though. */
557 if (format != GST_FORMAT_TIME && segment_start == 0) {
558 GST_DEBUG_OBJECT (basesink,
559 "non-time newsegment with start 0, coaxing into FORMAT_TIME");
560 format = GST_FORMAT_TIME;
565 if (format != GST_FORMAT_TIME) {
566 GST_DEBUG_OBJECT (basesink,
567 "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
568 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT,
569 format, segment_start, segment_stop, segment_time);
571 /* this means this sink will not be able to clip or drop samples
572 * and timestamps have to start from 0. */
573 basesink->segment_start = -1;
574 basesink->segment_stop = -1;
575 basesink->segment_time = -1;
576 goto done_newsegment;
578 /* check if we really have a new segment or the previous one is
581 /* the new segment has to be aligned with the old segment.
582 * We first update the accumulated time of the previous
583 * segment. the accumulated time is used when syncing to the
584 * clock. A flush event sets the accumulated time back to 0
586 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
587 duration = basesink->segment_stop - basesink->segment_start;
588 } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
589 /* else use last seen timestamp as segment stop */
590 duration = basesink->current_end - basesink->segment_start;
595 duration = segment_start - basesink->segment_start;
598 /* use previous rate to calculate duration */
599 basesink->segment_accum += gst_gdouble_to_guint64 (
600 (gst_guint64_to_gdouble (duration) / ABS (basesink->segment_rate)));
601 /* then update the current segment */
602 basesink->segment_rate = rate;
603 basesink->segment_start = segment_start;
604 basesink->segment_stop = segment_stop;
605 basesink->segment_time = segment_time;
607 GST_DEBUG_OBJECT (basesink,
608 "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
609 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
611 GST_TIME_ARGS (basesink->segment_start),
612 GST_TIME_ARGS (basesink->segment_stop),
613 GST_TIME_ARGS (basesink->segment_time),
614 GST_TIME_ARGS (basesink->segment_accum));
621 basesink->events_queued++;
623 GstBuffer *buf = GST_BUFFER (obj);
625 if (!basesink->have_newsegment) {
626 GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
627 ("Received buffer without a new-segment. Cannot sync to clock."),
628 ("Received buffer without a new-segment. Cannot sync to clock."));
629 basesink->have_newsegment = TRUE;
630 /* this means this sink will not be able to sync to the clock */
631 basesink->segment_start = -1;
632 basesink->segment_stop = -1;
635 /* check if the buffer needs to be dropped */
637 GstClockTime start = -1, end = -1;
639 /* we don't use the subclassed method as it may not return
640 * valid values for our purpose here */
641 gst_base_sink_get_times (basesink, buf, &start, &end);
643 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
644 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
645 GST_TIME_ARGS (end));
647 /* need to drop if the timestamp is not between segment_start and
648 * segment_stop. we check if the complete sample is outside of the
649 * range since the sink might be able to clip the sample. */
650 if (GST_CLOCK_TIME_IS_VALID (end) &&
651 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
652 if (end <= basesink->segment_start) {
653 GST_DEBUG_OBJECT (basesink,
654 "buffer end %" GST_TIME_FORMAT " <= segment start %"
655 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
656 GST_TIME_ARGS (basesink->segment_start));
660 if (GST_CLOCK_TIME_IS_VALID (start) &&
661 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
662 if (basesink->segment_stop <= start) {
663 GST_DEBUG_OBJECT (basesink,
664 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
665 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
666 GST_TIME_ARGS (basesink->segment_stop));
671 basesink->preroll_queued++;
672 basesink->buffers_queued++;
675 GST_DEBUG_OBJECT (basesink,
676 "now %d preroll, %d buffers, %d events on queue",
677 basesink->preroll_queued,
678 basesink->buffers_queued, basesink->events_queued);
680 /* check if we are prerolling */
681 if (!basesink->need_preroll)
684 /* there is a buffer queued */
685 if (basesink->buffers_queued == 1) {
686 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
688 /* if it's a buffer, we need to call the preroll method */
689 if (GST_IS_BUFFER (obj)) {
690 GstBaseSinkClass *bclass;
693 bclass = GST_BASE_SINK_GET_CLASS (basesink);
696 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
700 length = basesink->preroll_queued;
701 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
705 basesink->have_preroll = TRUE;
708 if (!gst_base_sink_commit_state (basesink))
712 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
716 /* it is possible that commiting the state made us go to PLAYING
717 * now in which case we don't need to block anymore. */
718 if (!basesink->need_preroll)
721 length = basesink->preroll_queued;
723 /* FIXME: a pad probe could have made us lose the buffer, according
724 * to one of the python tests */
726 GST_ERROR_OBJECT (basesink,
727 "preroll_queued dropped from 1 to 0 while committing state change");
729 g_assert (length <= 1);
732 /* see if we need to block now. We cannot block on events, only
733 * on buffers, the reason is that events can be sent from the
734 * application thread and we don't want to block there. */
735 if (length > basesink->preroll_queue_max_len && !have_event) {
736 /* block until the state changes, or we get a flush, or something */
737 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
738 GST_PREROLL_WAIT (pad);
739 GST_DEBUG_OBJECT (basesink, "done preroll");
741 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
745 GST_PREROLL_UNLOCK (pad);
753 GST_DEBUG_OBJECT (basesink, "no preroll needed");
754 /* maybe it was another sink that blocked in preroll, need to check for
756 basesink->have_preroll = FALSE;
757 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
758 GST_PREROLL_UNLOCK (pad);
766 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
768 gst_buffer_unref (buf);
769 GST_PREROLL_UNLOCK (pad);
776 gst_base_sink_preroll_queue_flush (basesink, pad);
777 GST_PREROLL_UNLOCK (pad);
778 GST_DEBUG_OBJECT (basesink, "pad is flushing");
780 return GST_FLOW_WRONG_STATE;
784 GST_PREROLL_UNLOCK (pad);
785 GST_DEBUG_OBJECT (basesink, "stopping");
787 return GST_FLOW_WRONG_STATE;
791 GST_DEBUG_OBJECT (basesink, "preroll failed");
792 gst_base_sink_preroll_queue_flush (basesink, pad);
794 GST_DEBUG_OBJECT (basesink, "abort state");
795 gst_element_abort_state (GST_ELEMENT (basesink));
797 return GST_FLOW_ERROR;
802 gst_base_sink_event (GstPad * pad, GstEvent * event)
804 GstBaseSink *basesink;
805 gboolean result = TRUE;
806 GstBaseSinkClass *bclass;
808 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
810 bclass = GST_BASE_SINK_GET_CLASS (basesink);
812 GST_DEBUG_OBJECT (basesink, "event %p", event);
814 switch (GST_EVENT_TYPE (event)) {
819 GST_STREAM_LOCK (pad);
820 /* EOS also finishes the preroll */
822 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
823 GST_STREAM_UNLOCK (pad);
826 case GST_EVENT_NEWSEGMENT:
830 GST_STREAM_LOCK (pad);
832 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
833 GST_STREAM_UNLOCK (pad);
836 case GST_EVENT_FLUSH_START:
837 /* make sure we are not blocked on the clock also clear any pending
840 bclass->event (basesink, event);
843 basesink->flushing = TRUE;
844 if (basesink->clock_id) {
845 gst_clock_id_unschedule (basesink->clock_id);
847 GST_UNLOCK (basesink);
849 GST_PREROLL_LOCK (pad);
850 /* we need preroll after the flush */
851 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
852 basesink->need_preroll = TRUE;
853 /* unlock from a possible state change/preroll */
854 gst_base_sink_preroll_queue_flush (basesink, pad);
855 GST_PREROLL_UNLOCK (pad);
857 /* and we need to commit our state again on the next
858 * prerolled buffer */
859 GST_STREAM_LOCK (pad);
860 gst_element_lost_state (GST_ELEMENT (basesink));
861 GST_STREAM_UNLOCK (pad);
862 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
863 gst_event_unref (event);
865 case GST_EVENT_FLUSH_STOP:
867 bclass->event (basesink, event);
869 /* now we are completely unblocked and the _chain method
871 GST_STREAM_LOCK (pad);
873 basesink->flushing = FALSE;
874 GST_UNLOCK (basesink);
875 /* we need new segment info after the flush. */
876 basesink->segment_start = -1;
877 basesink->segment_stop = -1;
878 basesink->current_start = -1;
879 basesink->current_end = -1;
880 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
881 GST_TIME_ARGS (basesink->segment_accum));
882 basesink->segment_accum = 0;
883 GST_STREAM_UNLOCK (pad);
885 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
886 gst_event_unref (event);
889 gst_event_unref (event);
892 gst_object_unref (basesink);
897 /* default implementation to calculate the start and end
898 * timestamps on a buffer, subclasses can override
901 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
902 GstClockTime * start, GstClockTime * end)
904 GstClockTime timestamp, duration;
906 timestamp = GST_BUFFER_TIMESTAMP (buffer);
907 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
909 /* get duration to calculate end time */
910 duration = GST_BUFFER_DURATION (buffer);
911 if (GST_CLOCK_TIME_IS_VALID (duration)) {
912 *end = timestamp + duration;
918 /* with STREAM_LOCK and LOCK*/
919 static GstClockReturn
920 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
925 /* no need to attempt a clock wait if we are flushing */
926 if (basesink->flushing) {
927 return GST_CLOCK_UNSCHEDULED;
930 /* clock_id should be NULL outside of this function */
931 g_assert (basesink->clock_id == NULL);
932 g_assert (GST_CLOCK_TIME_IS_VALID (time));
934 id = gst_clock_new_single_shot_id (basesink->clock, time);
936 basesink->clock_id = id;
937 /* release the object lock while waiting */
938 GST_UNLOCK (basesink);
940 ret = gst_clock_id_wait (id, NULL);
943 gst_clock_id_unref (id);
944 basesink->clock_id = NULL;
949 /* perform synchronisation on a buffer
951 * 1) check if we have a clock, if not, do nothing
952 * 2) calculate the start and end time of the buffer
953 * 3) create a single shot notification to wait on
954 * the clock, save the entry so we can unlock it
955 * 4) wait on the clock, this blocks
956 * 5) unref the clockid again
958 static GstClockReturn
959 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
961 GstClockReturn result = GST_CLOCK_OK;
962 GstClockTime start, end;
963 GstClockTimeDiff stream_start, stream_end;
964 GstBaseSinkClass *bclass;
965 gboolean start_valid, end_valid;
967 bclass = GST_BASE_SINK_GET_CLASS (basesink);
970 if (bclass->get_times)
971 bclass->get_times (basesink, buffer, &start, &end);
973 start_valid = GST_CLOCK_TIME_IS_VALID (start);
974 end_valid = GST_CLOCK_TIME_IS_VALID (end);
976 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
977 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
979 /* if we don't have a timestamp, we don't sync */
983 /* save last times seen. */
984 basesink->current_start = start;
986 basesink->current_end = end;
988 basesink->current_end = start;
990 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
991 /* check if not outside of the segment range, start is
992 * always valid here. */
993 if (start > basesink->segment_stop)
997 /* bring timestamp to stream time using last segment offset. */
998 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
999 /* check if not outside of the segment range */
1000 if (end_valid && end < basesink->segment_start)
1001 goto out_of_segment;
1003 stream_start = (gint64) start - basesink->segment_start;
1004 stream_end = (gint64) end - basesink->segment_start;
1006 stream_start = (gint64) start;
1007 stream_end = (gint64) end;
1010 /* correct for rate */
1011 if (basesink->segment_rate != 0.0) {
1012 stream_start /= ABS (basesink->segment_rate);
1014 stream_end /= ABS (basesink->segment_rate);
1017 stream_start += basesink->segment_accum;
1019 stream_end += basesink->segment_accum;
1021 /* now do clocking */
1022 if (basesink->clock && basesink->sync) {
1023 GstClockTime base_time;
1025 GST_LOCK (basesink);
1027 base_time = GST_ELEMENT (basesink)->base_time;
1029 GST_LOG_OBJECT (basesink,
1030 "waiting for clock, base time %" GST_TIME_FORMAT,
1031 GST_TIME_ARGS (base_time));
1033 /* also save end_time of this buffer so that we can wait
1036 basesink->end_time = stream_end + base_time;
1038 basesink->end_time = GST_CLOCK_TIME_NONE;
1040 result = gst_base_sink_wait (basesink, stream_start + base_time);
1042 GST_UNLOCK (basesink);
1044 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1052 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1053 return GST_CLOCK_UNSCHEDULED;
1060 * 2) render the event
1061 * 3) unref the event
1063 static inline gboolean
1064 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1066 GstBaseSinkClass *bclass;
1069 switch (GST_EVENT_TYPE (event)) {
1071 GST_LOCK (basesink);
1072 if (basesink->clock) {
1073 /* wait for last buffer to finish if we have a valid end time */
1074 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1075 gst_base_sink_wait (basesink, basesink->end_time);
1076 basesink->end_time = GST_CLOCK_TIME_NONE;
1079 GST_UNLOCK (basesink);
1085 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1087 ret = bclass->event (basesink, event);
1091 switch (GST_EVENT_TYPE (event)) {
1093 GST_PREROLL_LOCK (basesink->sinkpad);
1094 /* if we are still EOS, we can post the EOS message */
1095 if (basesink->eos) {
1096 /* ok, now we can post the message */
1097 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1098 gst_element_post_message (GST_ELEMENT (basesink),
1099 gst_message_new_eos (GST_OBJECT (basesink)));
1100 basesink->eos_queued = FALSE;
1102 GST_PREROLL_UNLOCK (basesink->sinkpad);
1108 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1109 gst_event_unref (event);
1116 * 1) first sync on the buffer
1117 * 2) render the buffer
1118 * 3) unref the buffer
1120 static inline GstFlowReturn
1121 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1123 GstFlowReturn ret = GST_FLOW_OK;
1124 GstClockReturn status;
1126 status = gst_base_sink_do_sync (basesink, buf);
1128 case GST_CLOCK_EARLY:
1129 GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1130 /* fallthrough for now */
1133 GstBaseSinkClass *bclass;
1135 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1137 ret = bclass->render (basesink, buf);
1141 GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1145 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1146 gst_buffer_unref (buf);
1151 static GstFlowReturn
1152 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1154 GstBaseSink *basesink;
1155 GstFlowReturn result;
1157 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1159 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1161 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1162 GST_DEBUG_PAD_NAME (pad));
1164 result = GST_FLOW_UNEXPECTED;
1168 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1171 gst_object_unref (basesink);
1177 gst_base_sink_loop (GstPad * pad)
1179 GstBaseSink *basesink;
1180 GstBuffer *buf = NULL;
1181 GstFlowReturn result;
1183 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1185 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1187 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1188 if (result != GST_FLOW_OK)
1191 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1192 if (result != GST_FLOW_OK)
1195 gst_object_unref (basesink);
1202 gst_base_sink_event (pad, gst_event_new_eos ());
1203 gst_object_unref (basesink);
1204 gst_pad_pause_task (pad);
1210 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1212 gboolean result = FALSE;
1213 GstBaseSinkClass *bclass;
1215 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1217 /* step 1, unblock clock sync (if any) or any other blocking thing */
1218 GST_PREROLL_LOCK (pad);
1219 GST_LOCK (basesink);
1220 if (basesink->clock_id) {
1221 gst_clock_id_unschedule (basesink->clock_id);
1223 GST_UNLOCK (basesink);
1225 /* unlock any subclasses */
1227 bclass->unlock (basesink);
1229 /* flush out the data thread if it's locked in finish_preroll */
1230 GST_DEBUG_OBJECT (basesink,
1231 "flushing out data thread, need preroll to FALSE");
1232 basesink->need_preroll = FALSE;
1233 gst_base_sink_preroll_queue_flush (basesink, pad);
1234 GST_PREROLL_SIGNAL (pad);
1235 GST_PREROLL_UNLOCK (pad);
1237 /* step 2, make sure streaming finishes */
1238 result = gst_pad_stop_task (pad);
1244 gst_base_sink_activate (GstPad * pad)
1246 gboolean result = FALSE;
1247 GstBaseSink *basesink;
1249 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1251 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1253 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1254 && gst_pad_activate_pull (pad, TRUE)) {
1255 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1258 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1259 if (gst_pad_activate_push (pad, TRUE)) {
1260 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1266 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1269 gst_object_unref (basesink);
1275 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1278 GstBaseSink *basesink;
1280 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1283 if (!basesink->can_activate_push) {
1285 basesink->pad_mode = GST_ACTIVATE_NONE;
1288 basesink->pad_mode = GST_ACTIVATE_PUSH;
1291 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1292 g_warning ("Internal GStreamer activation error!!!");
1295 result = gst_base_sink_deactivate (basesink, pad);
1296 basesink->pad_mode = GST_ACTIVATE_NONE;
1300 gst_object_unref (basesink);
1305 /* this won't get called until we implement an activate function */
1307 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1309 gboolean result = FALSE;
1310 GstBaseSink *basesink;
1312 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1315 if (!basesink->can_activate_pull) {
1317 basesink->pad_mode = GST_ACTIVATE_NONE;
1319 GstPad *peer = gst_pad_get_peer (pad);
1321 if (G_UNLIKELY (peer == NULL)) {
1322 g_warning ("Trying to activate pad in pull mode, but no peer");
1324 basesink->pad_mode = GST_ACTIVATE_NONE;
1326 if (gst_pad_activate_pull (peer, TRUE)) {
1327 basesink->have_newsegment = TRUE;
1328 basesink->segment_start = basesink->segment_stop = 0;
1330 /* set the pad mode before starting the task so that it's in the
1331 correct state for the new thread... */
1332 basesink->pad_mode = GST_ACTIVATE_PULL;
1334 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1336 /* but if starting the thread fails, set it back */
1338 basesink->pad_mode = GST_ACTIVATE_NONE;
1340 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1342 basesink->pad_mode = GST_ACTIVATE_NONE;
1344 gst_object_unref (peer);
1348 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1349 g_warning ("Internal GStreamer activation error!!!");
1352 basesink->have_newsegment = FALSE;
1353 result = gst_base_sink_deactivate (basesink, pad);
1354 basesink->pad_mode = GST_ACTIVATE_NONE;
1358 gst_object_unref (basesink);
1364 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1367 GstBaseSink *basesink = GST_BASE_SINK (element);
1371 pad = basesink->sinkpad;
1372 gst_object_ref (pad);
1373 GST_UNLOCK (element);
1375 result = gst_pad_push_event (pad, event);
1377 gst_object_unref (pad);
1383 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1386 gboolean res = FALSE;
1388 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1389 res = gst_pad_query (peer, query);
1390 gst_object_unref (peer);
1396 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
1400 gboolean res = FALSE;
1403 case GST_FORMAT_TIME:
1405 /* we can answer time format */
1406 GST_LOCK (basesink);
1407 if ((clock = GST_ELEMENT_CLOCK (basesink))) {
1410 gst_object_ref (clock);
1411 GST_UNLOCK (basesink);
1413 now = gst_clock_get_time (clock);
1415 GST_LOCK (basesink);
1417 now - GST_ELEMENT_CAST (basesink)->base_time +
1418 basesink->segment_time;
1420 GST_DEBUG_OBJECT (basesink,
1421 "now %" GST_TIME_FORMAT " + segment_time %" GST_TIME_FORMAT " = %"
1422 GST_TIME_FORMAT, GST_TIME_ARGS (now),
1423 GST_TIME_ARGS (basesink->segment_time), GST_TIME_ARGS (*cur));
1425 gst_object_unref (clock);
1430 GST_UNLOCK (basesink);
1439 gst_base_sink_query (GstElement * element, GstQuery * query)
1441 gboolean res = FALSE;
1443 GstBaseSink *basesink = GST_BASE_SINK (element);
1445 switch (GST_QUERY_TYPE (query)) {
1446 case GST_QUERY_POSITION:
1451 gst_query_parse_position (query, &format, NULL);
1453 GST_DEBUG_OBJECT (basesink, "current position format %d", format);
1455 if ((res = gst_base_sink_get_position (basesink, format, &cur))) {
1456 gst_query_set_position (query, format, cur);
1458 res = gst_base_sink_peer_query (basesink, query);
1462 case GST_QUERY_DURATION:
1463 res = gst_base_sink_peer_query (basesink, query);
1465 case GST_QUERY_LATENCY:
1467 case GST_QUERY_JITTER:
1469 case GST_QUERY_RATE:
1470 //gst_query_set_rate (query, basesink->segment_rate);
1473 case GST_QUERY_SEGMENT:
1475 /* FIXME, bring start/stop to stream time */
1476 gst_query_set_segment (query, basesink->segment_rate,
1477 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop);
1480 case GST_QUERY_SEEKING:
1481 case GST_QUERY_CONVERT:
1482 case GST_QUERY_FORMATS:
1484 res = gst_base_sink_peer_query (basesink, query);
1490 /* with PREROLL_LOCK */
1491 static GstStateChangeReturn
1492 do_playing (GstBaseSink * basesink)
1494 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1496 /* no preroll needed */
1497 basesink->need_preroll = FALSE;
1499 /* if we have EOS, we should empty the queue now as there will
1500 * be no more data received in the chain function.
1501 * FIXME, this could block the state change function too long when
1502 * we are pushing and syncing the buffers, better start a new
1503 * thread to do this. */
1504 if (basesink->eos) {
1505 gboolean do_eos = !basesink->eos_queued;
1507 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1509 /* need to post EOS message here if it was not in the preroll queue we
1512 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1513 gst_element_post_message (GST_ELEMENT (basesink),
1514 gst_message_new_eos (GST_OBJECT (basesink)));
1516 } else if (!basesink->have_preroll) {
1517 /* don't need preroll, but do queue a commit_state */
1518 basesink->need_preroll = TRUE;
1519 GST_DEBUG_OBJECT (basesink,
1520 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1521 ret = GST_STATE_CHANGE_ASYNC;
1522 /* we know it's not waiting, no need to signal */
1524 /* don't need the preroll anymore */
1525 GST_DEBUG_OBJECT (basesink,
1526 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1527 /* now let it play */
1528 GST_PREROLL_SIGNAL (basesink->sinkpad);
1534 static GstStateChangeReturn
1535 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1537 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1538 GstBaseSink *basesink = GST_BASE_SINK (element);
1539 GstBaseSinkClass *bclass;
1541 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1543 switch (transition) {
1544 case GST_STATE_CHANGE_NULL_TO_READY:
1546 if (!bclass->start (basesink))
1549 case GST_STATE_CHANGE_READY_TO_PAUSED:
1550 /* need to complete preroll before this state change completes, there
1551 * is no data flow in READY so we can safely assume we need to preroll. */
1552 basesink->offset = 0;
1553 GST_PREROLL_LOCK (basesink->sinkpad);
1554 basesink->have_preroll = FALSE;
1555 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1556 basesink->need_preroll = TRUE;
1557 GST_PREROLL_UNLOCK (basesink->sinkpad);
1558 basesink->have_newsegment = FALSE;
1559 basesink->segment_rate = 1.0;
1560 basesink->segment_start = 0;
1561 basesink->segment_stop = 0;
1562 basesink->segment_time = 0;
1563 basesink->segment_accum = 0;
1564 ret = GST_STATE_CHANGE_ASYNC;
1566 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1567 GST_PREROLL_LOCK (basesink->sinkpad);
1568 ret = do_playing (basesink);
1569 GST_PREROLL_UNLOCK (basesink->sinkpad);
1576 GstStateChangeReturn bret;
1578 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1579 if (bret == GST_STATE_CHANGE_FAILURE)
1580 goto activate_failed;
1583 switch (transition) {
1584 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1586 GstBaseSinkClass *bclass;
1588 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1590 GST_PREROLL_LOCK (basesink->sinkpad);
1591 GST_LOCK (basesink);
1592 /* unlock clock wait if any */
1593 if (basesink->clock_id) {
1594 gst_clock_id_unschedule (basesink->clock_id);
1596 GST_UNLOCK (basesink);
1598 /* unlock any subclasses */
1600 bclass->unlock (basesink);
1602 /* if we don't have a preroll buffer and we have not received EOS,
1603 * we need to wait for a preroll */
1604 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1605 basesink->have_preroll, basesink->eos);
1606 if (!basesink->have_preroll && !basesink->eos
1607 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1608 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1609 basesink->need_preroll = TRUE;
1610 ret = GST_STATE_CHANGE_ASYNC;
1612 GST_PREROLL_UNLOCK (basesink->sinkpad);
1615 case GST_STATE_CHANGE_PAUSED_TO_READY:
1617 case GST_STATE_CHANGE_READY_TO_NULL:
1619 if (!bclass->stop (basesink)) {
1620 GST_WARNING ("failed to stop");
1632 GST_DEBUG_OBJECT (basesink, "failed to start");
1633 return GST_STATE_CHANGE_FAILURE;
1637 GST_DEBUG_OBJECT (basesink,
1638 "element failed to change states -- activation problem?");
1639 return GST_STATE_CHANGE_FAILURE;