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 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 gboolean gst_base_sink_send_event (GstElement * element,
110 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
112 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
113 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
114 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
115 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
116 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
117 GstClockTime * start, GstClockTime * end);
119 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
120 GstStateChange transition);
122 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
123 static void gst_base_sink_loop (GstPad * pad);
124 static gboolean gst_base_sink_activate (GstPad * pad);
125 static gboolean gst_base_sink_activate_push (GstPad * pad, gboolean active);
126 static gboolean gst_base_sink_activate_pull (GstPad * pad, gboolean active);
127 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
128 static inline GstFlowReturn gst_base_sink_handle_buffer (GstBaseSink * basesink,
130 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
134 gst_base_sink_base_init (gpointer g_class)
136 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
141 gst_base_sink_class_init (GstBaseSinkClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
146 gobject_class = (GObjectClass *) klass;
147 gstelement_class = (GstElementClass *) klass;
149 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
151 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
152 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
153 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
155 /* FIXME, this next value should be configured using an event from the
156 * upstream element */
157 g_object_class_install_property (G_OBJECT_CLASS (klass),
158 PROP_PREROLL_QUEUE_LEN,
159 g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
160 "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
161 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
162 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
163 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
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);
169 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
170 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
172 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
173 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
174 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
175 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
179 gst_base_sink_pad_getcaps (GstPad * pad)
181 GstBaseSinkClass *bclass;
183 GstCaps *caps = NULL;
185 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
186 bclass = GST_BASE_SINK_GET_CLASS (bsink);
187 if (bclass->get_caps)
188 caps = bclass->get_caps (bsink);
191 GstPadTemplate *pad_template;
194 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
195 if (pad_template != NULL) {
196 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
199 gst_object_unref (bsink);
205 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
207 GstBaseSinkClass *bclass;
209 gboolean res = FALSE;
211 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
212 bclass = GST_BASE_SINK_GET_CLASS (bsink);
214 if (bclass->set_caps)
215 res = bclass->set_caps (bsink, caps);
217 gst_object_unref (bsink);
223 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
224 GstCaps * caps, GstBuffer ** buf)
226 GstBaseSinkClass *bclass;
228 GstFlowReturn result = GST_FLOW_OK;
230 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
231 bclass = GST_BASE_SINK_GET_CLASS (bsink);
233 if (bclass->buffer_alloc)
234 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
236 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
238 gst_object_unref (bsink);
244 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
246 GstPadTemplate *pad_template;
249 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
250 g_return_if_fail (pad_template != NULL);
252 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
254 gst_pad_set_getcaps_function (basesink->sinkpad,
255 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
256 gst_pad_set_setcaps_function (basesink->sinkpad,
257 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
258 gst_pad_set_bufferalloc_function (basesink->sinkpad,
259 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
260 gst_pad_set_activate_function (basesink->sinkpad,
261 GST_DEBUG_FUNCPTR (gst_base_sink_activate));
262 gst_pad_set_activatepush_function (basesink->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_base_sink_activate_push));
264 gst_pad_set_activatepull_function (basesink->sinkpad,
265 GST_DEBUG_FUNCPTR (gst_base_sink_activate_pull));
266 gst_pad_set_event_function (basesink->sinkpad,
267 GST_DEBUG_FUNCPTR (gst_base_sink_event));
268 gst_pad_set_chain_function (basesink->sinkpad,
269 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
270 gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
272 basesink->pad_mode = GST_ACTIVATE_NONE;
273 GST_PAD_TASK (basesink->sinkpad) = NULL;
274 basesink->preroll_queue = g_queue_new ();
276 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
277 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
279 basesink->sync = DEFAULT_SYNC;
281 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
285 gst_base_sink_finalize (GObject * object)
287 GstBaseSink *basesink;
289 basesink = GST_BASE_SINK (object);
291 g_queue_free (basesink->preroll_queue);
293 G_OBJECT_CLASS (parent_class)->finalize (object);
297 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
301 sink = GST_BASE_SINK (element);
307 gst_base_sink_set_property (GObject * object, guint prop_id,
308 const GValue * value, GParamSpec * pspec)
310 GstBaseSink *sink = GST_BASE_SINK (object);
313 case PROP_PREROLL_QUEUE_LEN:
314 /* preroll lock necessary to serialize with finish_preroll */
315 GST_PREROLL_LOCK (sink->sinkpad);
316 sink->preroll_queue_max_len = g_value_get_uint (value);
317 GST_PREROLL_UNLOCK (sink->sinkpad);
320 sink->sync = g_value_get_boolean (value);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
332 GstBaseSink *sink = GST_BASE_SINK (object);
336 case PROP_PREROLL_QUEUE_LEN:
337 g_value_set_uint (value, sink->preroll_queue_max_len);
340 g_value_set_boolean (value, sink->sync);
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 gst_base_sink_get_caps (GstBaseSink * sink)
356 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
362 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
363 GstCaps * caps, GstBuffer ** buf)
369 /* with PREROLL_LOCK */
371 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
374 GQueue *q = basesink->preroll_queue;
380 GST_DEBUG_OBJECT (basesink, "emptying queue");
381 while ((obj = g_queue_pop_head (q))) {
384 is_buffer = GST_IS_BUFFER (obj);
386 basesink->preroll_queued--;
387 basesink->buffers_queued--;
389 switch (GST_EVENT_TYPE (obj)) {
391 basesink->preroll_queued--;
396 basesink->events_queued--;
398 /* we release the preroll lock while pushing so that we
399 * can still flush it while blocking on the clock or
400 * inside the element. */
401 GST_PREROLL_UNLOCK (pad);
404 GST_DEBUG_OBJECT (basesink, "popped buffer %p", obj);
405 ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER (obj));
407 GST_DEBUG_OBJECT (basesink, "popped event %p", obj);
408 gst_base_sink_handle_event (basesink, GST_EVENT (obj));
412 GST_PREROLL_LOCK (pad);
414 GST_DEBUG_OBJECT (basesink, "queue empty");
419 /* with PREROLL_LOCK */
421 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
424 GQueue *q = basesink->preroll_queue;
426 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
428 while ((obj = g_queue_pop_head (q))) {
429 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
430 gst_mini_object_unref (obj);
433 /* we can't have EOS anymore now */
434 basesink->eos = FALSE;
435 basesink->eos_queued = FALSE;
436 basesink->preroll_queued = 0;
437 basesink->buffers_queued = 0;
438 basesink->events_queued = 0;
439 basesink->have_preroll = FALSE;
440 /* and signal any waiters now */
441 GST_PREROLL_SIGNAL (pad);
444 /* with STREAM_LOCK */
446 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
452 GST_PREROLL_LOCK (pad);
453 /* push object on the queue */
454 GST_DEBUG_OBJECT (basesink, "push %p on preroll_queue", obj);
455 g_queue_push_tail (basesink->preroll_queue, obj);
457 have_event = GST_IS_EVENT (obj);
459 GstEvent *event = GST_EVENT (obj);
461 switch (GST_EVENT_TYPE (obj)) {
463 basesink->preroll_queued++;
464 basesink->eos = TRUE;
465 basesink->eos_queued = TRUE;
467 case GST_EVENT_NEWSEGMENT:
472 gint64 segment_start;
475 GstClockTime duration;
478 /* the newsegment event is needed to bring the buffer timestamps to the
479 * stream time and to drop samples outside of the playback segment. */
480 gst_event_parse_newsegment (event, &update, &rate, &format,
481 &segment_start, &segment_stop, &segment_time);
483 basesink->have_newsegment = TRUE;
485 /* any other format with 0 also gives time 0, the other values are
486 * invalid as time though. */
487 if (format != GST_FORMAT_TIME && segment_start == 0) {
488 GST_DEBUG_OBJECT (basesink,
489 "non-time newsegment with start 0, coaxing into FORMAT_TIME");
490 format = GST_FORMAT_TIME;
495 if (format != GST_FORMAT_TIME) {
496 GST_DEBUG_OBJECT (basesink,
497 "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
498 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT,
499 format, segment_start, segment_stop, segment_time);
501 /* this means this sink will not be able to clip or drop samples
502 * and timestamps have to start from 0. */
503 basesink->segment_start = -1;
504 basesink->segment_stop = -1;
505 basesink->segment_time = -1;
506 goto done_newsegment;
508 /* check if we really have a new segment or the previous one is
511 /* the new segment has to be aligned with the old segment.
512 * We first update the accumulated time of the previous
513 * segment. the accumulated time is used when syncing to the
514 * clock. A flush event sets the accumulated time back to 0
516 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
517 duration = basesink->segment_stop - basesink->segment_start;
518 } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
519 /* else use last seen timestamp as segment stop */
520 duration = basesink->current_end - basesink->segment_start;
525 duration = segment_start - basesink->segment_start;
528 /* use previous rate to calculate duration */
529 basesink->segment_accum += gst_gdouble_to_guint64 (
530 (gst_guint64_to_gdouble (duration) / ABS (basesink->segment_rate)));
531 /* then update the current segment */
532 basesink->segment_rate = rate;
533 basesink->segment_start = segment_start;
534 basesink->segment_stop = segment_stop;
535 basesink->segment_time = segment_time;
537 GST_DEBUG_OBJECT (basesink,
538 "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
539 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
541 GST_TIME_ARGS (basesink->segment_start),
542 GST_TIME_ARGS (basesink->segment_stop),
543 GST_TIME_ARGS (basesink->segment_time),
544 GST_TIME_ARGS (basesink->segment_accum));
551 basesink->events_queued++;
553 GstBuffer *buf = GST_BUFFER (obj);
555 if (!basesink->have_newsegment) {
556 GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
557 ("Received buffer without a new-segment. Cannot sync to clock."),
558 ("Received buffer without a new-segment. Cannot sync to clock."));
559 basesink->have_newsegment = TRUE;
560 /* this means this sink will not be able to sync to the clock */
561 basesink->segment_start = -1;
562 basesink->segment_stop = -1;
565 /* check if the buffer needs to be dropped */
567 GstClockTime start = -1, end = -1;
569 /* we don't use the subclassed method as it may not return
570 * valid values for our purpose here */
571 gst_base_sink_get_times (basesink, buf, &start, &end);
573 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
574 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
575 GST_TIME_ARGS (end));
577 /* need to drop if the timestamp is not between segment_start and
578 * segment_stop. we check if the complete sample is outside of the
579 * range since the sink might be able to clip the sample. */
580 if (GST_CLOCK_TIME_IS_VALID (end) &&
581 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
582 if (end <= basesink->segment_start) {
583 GST_DEBUG_OBJECT (basesink,
584 "buffer end %" GST_TIME_FORMAT " <= segment start %"
585 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
586 GST_TIME_ARGS (basesink->segment_start));
590 if (GST_CLOCK_TIME_IS_VALID (start) &&
591 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
592 if (basesink->segment_stop <= start) {
593 GST_DEBUG_OBJECT (basesink,
594 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
595 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
596 GST_TIME_ARGS (basesink->segment_stop));
601 basesink->preroll_queued++;
602 basesink->buffers_queued++;
604 GST_DEBUG_OBJECT (basesink,
605 "now %d preroll, %d buffers, %d events on queue",
606 basesink->preroll_queued,
607 basesink->buffers_queued, basesink->events_queued);
609 if (basesink->playing_async)
612 /* check if we are prerolling */
613 if (!basesink->need_preroll)
616 /* there is a buffer queued */
617 if (basesink->buffers_queued == 1) {
618 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
620 /* if it's a buffer, we need to call the preroll method */
621 if (GST_IS_BUFFER (obj)) {
622 GstBaseSinkClass *bclass;
625 bclass = GST_BASE_SINK_GET_CLASS (basesink);
628 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
632 length = basesink->preroll_queued;
633 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
639 basesink->have_preroll = TRUE;
640 /* we are prerolling */
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_OBJECT (basesink, "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 commit our state, this will also automatically proceed to
653 * the next pending state. */
655 if ((task = GST_PAD_TASK (pad))) {
656 while (!GST_STATE_TRYLOCK (basesink)) {
657 GST_DEBUG_OBJECT (basesink,
658 "state change happening, checking shutdown");
660 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
665 GST_STATE_LOCK (basesink);
667 GST_DEBUG_OBJECT (basesink, "commit state");
668 gst_element_commit_state (GST_ELEMENT (basesink));
669 GST_STATE_UNLOCK (basesink);
671 /* reacquire stream lock, pad could be flushing now */
672 /* FIXME in glib, if t==0, the lock is still taken... hmmm.. bug #317802 */
674 GST_STREAM_LOCK_FULL (pad, t);
676 /* and wait if needed */
677 GST_PREROLL_LOCK (pad);
680 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
684 /* it is possible that the application set the state to PLAYING
685 * now in which case we don't need to block anymore. */
686 if (!basesink->need_preroll)
690 length = basesink->preroll_queued;
692 /* FIXME: a pad probe could have made us lose the buffer, according
693 * to one of the python tests */
695 GST_ERROR_OBJECT (basesink,
696 "preroll_queued dropped from 1 to 0 while committing state change");
698 g_assert (length <= 1);
701 /* see if we need to block now. We cannot block on events, only
702 * on buffers, the reason is that events can be sent from the
703 * application thread and we don't want to block there. */
704 if (length > basesink->preroll_queue_max_len && !have_event) {
705 /* block until the state changes, or we get a flush, or something */
706 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
707 GST_PREROLL_WAIT (pad);
708 GST_DEBUG_OBJECT (basesink, "done preroll");
711 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
715 GST_PREROLL_UNLOCK (pad);
723 GST_DEBUG_OBJECT (basesink, "no preroll needed");
724 /* maybe it was another sink that blocked in preroll, need to check for
726 basesink->have_preroll = FALSE;
727 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
728 GST_PREROLL_UNLOCK (pad);
736 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
738 gst_buffer_unref (buf);
739 GST_PREROLL_UNLOCK (pad);
748 basesink->have_preroll = FALSE;
749 basesink->playing_async = FALSE;
751 /* handle buffer first */
752 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
754 /* unroll locks, commit state, reacquire stream lock */
755 GST_PREROLL_UNLOCK (pad);
756 t = GST_STREAM_UNLOCK_FULL (pad);
757 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
759 GST_WARNING ("STREAM_LOCK should have been locked !!");
760 g_warning ("STREAM_LOCK should have been locked !!");
762 GST_STATE_LOCK (basesink);
763 GST_DEBUG_OBJECT (basesink, "commit state");
764 gst_element_commit_state (GST_ELEMENT (basesink));
765 GST_STATE_UNLOCK (basesink);
767 GST_STREAM_LOCK_FULL (pad, t);
774 GST_DEBUG_OBJECT (basesink, "task is stopped");
775 return GST_FLOW_WRONG_STATE;
780 gst_base_sink_preroll_queue_flush (basesink, pad);
781 GST_PREROLL_UNLOCK (pad);
782 GST_DEBUG_OBJECT (basesink, "pad is flushing");
783 return GST_FLOW_WRONG_STATE;
789 GST_DEBUG_OBJECT (basesink, "preroll failed");
790 gst_base_sink_preroll_queue_flush (basesink, pad);
791 GST_PREROLL_UNLOCK (pad);
793 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
794 * inside the STREAM_LOCK */
795 t = GST_STREAM_UNLOCK_FULL (pad);
796 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
798 GST_WARNING ("STREAM_LOCK should have been locked !!");
799 g_warning ("STREAM_LOCK should have been locked !!");
802 /* now we abort our state */
803 GST_STATE_LOCK (basesink);
804 GST_DEBUG_OBJECT (basesink, "abort state");
805 gst_element_abort_state (GST_ELEMENT (basesink));
806 GST_STATE_UNLOCK (basesink);
808 /* reacquire stream lock, pad could be flushing now */
810 GST_STREAM_LOCK_FULL (pad, t);
812 return GST_FLOW_ERROR;
817 gst_base_sink_event (GstPad * pad, GstEvent * event)
819 GstBaseSink *basesink;
820 gboolean result = TRUE;
821 GstBaseSinkClass *bclass;
823 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
825 bclass = GST_BASE_SINK_GET_CLASS (basesink);
827 GST_DEBUG_OBJECT (basesink, "event %p", event);
829 switch (GST_EVENT_TYPE (event)) {
834 GST_STREAM_LOCK (pad);
835 /* EOS also finishes the preroll */
837 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
838 GST_STREAM_UNLOCK (pad);
841 case GST_EVENT_NEWSEGMENT:
845 GST_STREAM_LOCK (pad);
847 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
848 GST_STREAM_UNLOCK (pad);
851 case GST_EVENT_FLUSH_START:
852 /* make sure we are not blocked on the clock also clear any pending
855 bclass->event (basesink, event);
858 basesink->flushing = TRUE;
859 if (basesink->clock_id) {
860 gst_clock_id_unschedule (basesink->clock_id);
862 GST_UNLOCK (basesink);
864 GST_PREROLL_LOCK (pad);
865 /* we need preroll after the flush */
866 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
867 basesink->need_preroll = TRUE;
868 /* unlock from a possible state change/preroll */
869 gst_base_sink_preroll_queue_flush (basesink, pad);
870 GST_PREROLL_UNLOCK (pad);
872 /* and we need to commit our state again on the next
873 * prerolled buffer */
874 GST_STATE_LOCK (basesink);
875 GST_STREAM_LOCK (pad);
876 gst_element_lost_state (GST_ELEMENT (basesink));
877 GST_STREAM_UNLOCK (pad);
878 GST_STATE_UNLOCK (basesink);
879 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
880 gst_event_unref (event);
882 case GST_EVENT_FLUSH_STOP:
884 bclass->event (basesink, event);
886 /* now we are completely unblocked and the _chain method
888 GST_STREAM_LOCK (pad);
890 basesink->flushing = FALSE;
891 GST_UNLOCK (basesink);
892 /* we need new segment info after the flush. */
893 basesink->segment_start = -1;
894 basesink->segment_stop = -1;
895 basesink->current_start = -1;
896 basesink->current_end = -1;
897 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
898 GST_TIME_ARGS (basesink->segment_accum));
899 basesink->segment_accum = 0;
900 GST_STREAM_UNLOCK (pad);
902 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
903 gst_event_unref (event);
906 gst_event_unref (event);
909 gst_object_unref (basesink);
914 /* default implementation to calculate the start and end
915 * timestamps on a buffer, subclasses can override
918 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
919 GstClockTime * start, GstClockTime * end)
921 GstClockTime timestamp, duration;
923 timestamp = GST_BUFFER_TIMESTAMP (buffer);
924 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
926 /* get duration to calculate end time */
927 duration = GST_BUFFER_DURATION (buffer);
928 if (GST_CLOCK_TIME_IS_VALID (duration)) {
929 *end = timestamp + duration;
935 /* with STREAM_LOCK and LOCK*/
936 static GstClockReturn
937 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
942 /* no need to attempt a clock wait if we are flushing */
943 if (basesink->flushing) {
944 return GST_CLOCK_UNSCHEDULED;
947 /* clock_id should be NULL outside of this function */
948 g_assert (basesink->clock_id == NULL);
949 g_assert (GST_CLOCK_TIME_IS_VALID (time));
951 id = gst_clock_new_single_shot_id (basesink->clock, time);
953 basesink->clock_id = id;
954 /* release the object lock while waiting */
955 GST_UNLOCK (basesink);
957 ret = gst_clock_id_wait (id, NULL);
960 gst_clock_id_unref (id);
961 basesink->clock_id = NULL;
966 /* perform synchronisation on a buffer
968 * 1) check if we have a clock, if not, do nothing
969 * 2) calculate the start and end time of the buffer
970 * 3) create a single shot notification to wait on
971 * the clock, save the entry so we can unlock it
972 * 4) wait on the clock, this blocks
973 * 5) unref the clockid again
975 static GstClockReturn
976 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
978 GstClockReturn result = GST_CLOCK_OK;
979 GstClockTime start, end;
980 GstClockTimeDiff stream_start, stream_end;
981 GstBaseSinkClass *bclass;
982 gboolean start_valid, end_valid;
984 bclass = GST_BASE_SINK_GET_CLASS (basesink);
987 if (bclass->get_times)
988 bclass->get_times (basesink, buffer, &start, &end);
990 start_valid = GST_CLOCK_TIME_IS_VALID (start);
991 end_valid = GST_CLOCK_TIME_IS_VALID (end);
993 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
994 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
996 /* if we don't have a timestamp, we don't sync */
1000 /* save last times seen. */
1001 basesink->current_start = start;
1003 basesink->current_end = end;
1005 basesink->current_end = start;
1007 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
1008 /* check if not outside of the segment range, start is
1009 * always valid here. */
1010 if (start > basesink->segment_stop)
1011 goto out_of_segment;
1014 /* bring timestamp to stream time using last segment offset. */
1015 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1016 /* check if not outside of the segment range */
1017 if (end_valid && end < basesink->segment_start)
1018 goto out_of_segment;
1020 stream_start = (gint64) start - basesink->segment_start;
1021 stream_end = (gint64) end - basesink->segment_start;
1023 stream_start = (gint64) start;
1024 stream_end = (gint64) end;
1027 /* correct for rate */
1028 if (basesink->segment_rate != 0.0) {
1029 stream_start /= ABS (basesink->segment_rate);
1031 stream_end /= ABS (basesink->segment_rate);
1034 stream_start += basesink->segment_accum;
1036 stream_end += basesink->segment_accum;
1038 /* now do clocking */
1039 if (basesink->clock && basesink->sync) {
1040 GstClockTime base_time;
1042 GST_LOCK (basesink);
1044 base_time = GST_ELEMENT (basesink)->base_time;
1046 GST_LOG_OBJECT (basesink,
1047 "waiting for clock, base time %" GST_TIME_FORMAT,
1048 GST_TIME_ARGS (base_time));
1050 /* also save end_time of this buffer so that we can wait
1053 basesink->end_time = stream_end + base_time;
1055 basesink->end_time = GST_CLOCK_TIME_NONE;
1057 result = gst_base_sink_wait (basesink, stream_start + base_time);
1059 GST_UNLOCK (basesink);
1061 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1069 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1070 return GST_CLOCK_UNSCHEDULED;
1077 * 2) render the event
1078 * 3) unref the event
1080 static inline gboolean
1081 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1083 GstBaseSinkClass *bclass;
1086 switch (GST_EVENT_TYPE (event)) {
1088 GST_LOCK (basesink);
1089 if (basesink->clock) {
1090 /* wait for last buffer to finish if we have a valid end time */
1091 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1092 gst_base_sink_wait (basesink, basesink->end_time);
1093 basesink->end_time = GST_CLOCK_TIME_NONE;
1096 GST_UNLOCK (basesink);
1102 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1104 ret = bclass->event (basesink, event);
1108 switch (GST_EVENT_TYPE (event)) {
1110 GST_PREROLL_LOCK (basesink->sinkpad);
1111 /* if we are still EOS, we can post the EOS message */
1112 if (basesink->eos) {
1113 /* ok, now we can post the message */
1114 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1115 gst_element_post_message (GST_ELEMENT (basesink),
1116 gst_message_new_eos (GST_OBJECT (basesink)));
1117 basesink->eos_queued = FALSE;
1119 GST_PREROLL_UNLOCK (basesink->sinkpad);
1125 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1126 gst_event_unref (event);
1133 * 1) first sync on the buffer
1134 * 2) render the buffer
1135 * 3) unref the buffer
1137 static inline GstFlowReturn
1138 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1140 GstFlowReturn ret = GST_FLOW_OK;
1141 GstClockReturn status;
1143 status = gst_base_sink_do_sync (basesink, buf);
1145 case GST_CLOCK_EARLY:
1146 GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1147 /* fallthrough for now */
1150 GstBaseSinkClass *bclass;
1152 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1154 ret = bclass->render (basesink, buf);
1158 GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1162 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1163 gst_buffer_unref (buf);
1168 static GstFlowReturn
1169 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1171 GstBaseSink *basesink;
1172 GstFlowReturn result;
1174 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1176 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1178 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1179 GST_DEBUG_PAD_NAME (pad));
1181 result = GST_FLOW_UNEXPECTED;
1185 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1188 gst_object_unref (basesink);
1194 gst_base_sink_loop (GstPad * pad)
1196 GstBaseSink *basesink;
1197 GstBuffer *buf = NULL;
1198 GstFlowReturn result;
1200 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1202 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1204 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1205 if (result != GST_FLOW_OK)
1208 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1209 if (result != GST_FLOW_OK)
1212 gst_object_unref (basesink);
1219 gst_base_sink_event (pad, gst_event_new_eos ());
1220 gst_object_unref (basesink);
1221 gst_pad_pause_task (pad);
1227 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1229 gboolean result = FALSE;
1230 GstBaseSinkClass *bclass;
1232 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1234 /* step 1, unblock clock sync (if any) or any other blocking thing */
1235 GST_PREROLL_LOCK (pad);
1236 GST_LOCK (basesink);
1237 if (basesink->clock_id) {
1238 gst_clock_id_unschedule (basesink->clock_id);
1240 GST_UNLOCK (basesink);
1242 /* unlock any subclasses */
1244 bclass->unlock (basesink);
1246 /* flush out the data thread if it's locked in finish_preroll */
1247 GST_DEBUG_OBJECT (basesink,
1248 "flushing out data thread, need preroll to FALSE");
1249 basesink->need_preroll = FALSE;
1250 gst_base_sink_preroll_queue_flush (basesink, pad);
1251 GST_PREROLL_SIGNAL (pad);
1252 GST_PREROLL_UNLOCK (pad);
1254 /* step 2, make sure streaming finishes */
1255 result = gst_pad_stop_task (pad);
1261 gst_base_sink_activate (GstPad * pad)
1263 gboolean result = FALSE;
1264 GstBaseSink *basesink;
1266 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1268 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1270 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1271 && gst_pad_activate_pull (pad, TRUE)) {
1272 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1275 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1276 if (gst_pad_activate_push (pad, TRUE)) {
1277 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1283 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1286 gst_object_unref (basesink);
1292 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1295 GstBaseSink *basesink;
1297 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1300 if (!basesink->can_activate_push) {
1302 basesink->pad_mode = GST_ACTIVATE_NONE;
1305 basesink->pad_mode = GST_ACTIVATE_PUSH;
1308 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1309 g_warning ("Internal GStreamer activation error!!!");
1312 result = gst_base_sink_deactivate (basesink, pad);
1313 basesink->pad_mode = GST_ACTIVATE_NONE;
1317 gst_object_unref (basesink);
1322 /* this won't get called until we implement an activate function */
1324 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1326 gboolean result = FALSE;
1327 GstBaseSink *basesink;
1329 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1332 if (!basesink->can_activate_pull) {
1334 basesink->pad_mode = GST_ACTIVATE_NONE;
1336 GstPad *peer = gst_pad_get_peer (pad);
1338 if (G_UNLIKELY (peer == NULL)) {
1339 g_warning ("Trying to activate pad in pull mode, but no peer");
1341 basesink->pad_mode = GST_ACTIVATE_NONE;
1343 if (gst_pad_activate_pull (peer, TRUE)) {
1344 basesink->have_newsegment = TRUE;
1345 basesink->segment_start = basesink->segment_stop = 0;
1347 /* set the pad mode before starting the task so that it's in the
1348 correct state for the new thread... */
1349 basesink->pad_mode = GST_ACTIVATE_PULL;
1351 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1353 /* but if starting the thread fails, set it back */
1355 basesink->pad_mode = GST_ACTIVATE_NONE;
1357 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1359 basesink->pad_mode = GST_ACTIVATE_NONE;
1361 gst_object_unref (peer);
1365 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1366 g_warning ("Internal GStreamer activation error!!!");
1369 basesink->have_newsegment = FALSE;
1370 result = gst_base_sink_deactivate (basesink, pad);
1371 basesink->pad_mode = GST_ACTIVATE_NONE;
1375 gst_object_unref (basesink);
1381 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1384 GstBaseSink *basesink = GST_BASE_SINK (element);
1388 pad = basesink->sinkpad;
1389 gst_object_ref (pad);
1390 GST_UNLOCK (element);
1392 result = gst_pad_push_event (pad, event);
1394 gst_object_unref (pad);
1400 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1403 gboolean res = FALSE;
1405 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1406 res = gst_pad_query (peer, query);
1407 gst_object_unref (peer);
1413 gst_base_sink_query (GstElement * element, GstQuery * query)
1415 gboolean res = FALSE;
1417 GstBaseSink *basesink = GST_BASE_SINK (element);
1419 switch (GST_QUERY_TYPE (query)) {
1420 case GST_QUERY_POSITION:
1421 res = gst_base_sink_peer_query (basesink, query);
1423 case GST_QUERY_LATENCY:
1425 case GST_QUERY_JITTER:
1427 case GST_QUERY_RATE:
1428 //gst_query_set_rate (query, basesink->segment_rate);
1431 case GST_QUERY_SEEKING:
1432 res = gst_base_sink_peer_query (basesink, query);
1434 case GST_QUERY_SEGMENT:
1436 gst_query_set_segment (query, basesink->segment_rate,
1437 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1438 basesink->segment_time);
1441 case GST_QUERY_CONVERT:
1442 res = gst_base_sink_peer_query (basesink, query);
1444 case GST_QUERY_FORMATS:
1445 res = gst_base_sink_peer_query (basesink, query);
1453 static GstStateChangeReturn
1454 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1456 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1457 GstBaseSink *basesink = GST_BASE_SINK (element);
1458 GstBaseSinkClass *bclass;
1460 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1462 switch (transition) {
1463 case GST_STATE_CHANGE_NULL_TO_READY:
1465 if (!bclass->start (basesink))
1468 case GST_STATE_CHANGE_READY_TO_PAUSED:
1469 /* need to complete preroll before this state change completes, there
1470 * is no data flow in READY so we can safely assume we need to preroll. */
1471 basesink->offset = 0;
1472 GST_PREROLL_LOCK (basesink->sinkpad);
1473 basesink->have_preroll = FALSE;
1474 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1475 basesink->need_preroll = TRUE;
1476 GST_PREROLL_UNLOCK (basesink->sinkpad);
1477 basesink->have_newsegment = FALSE;
1478 basesink->segment_rate = 1.0;
1479 basesink->segment_start = 0;
1480 basesink->segment_stop = 0;
1481 basesink->segment_time = 0;
1482 basesink->segment_accum = 0;
1483 ret = GST_STATE_CHANGE_ASYNC;
1485 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1486 GST_PREROLL_LOCK (basesink->sinkpad);
1487 /* if we have EOS, we should empty the queue now as there will
1488 * be no more data received in the chain function.
1489 * FIXME, this could block the state change function too long when
1490 * we are pushing and syncing the buffers, better start a new
1491 * thread to do this. */
1492 if (basesink->eos) {
1493 gboolean do_eos = !basesink->eos_queued;
1495 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1496 basesink->need_preroll = FALSE;
1498 /* need to post EOS message here if it was not in the preroll queue we
1501 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1502 gst_element_post_message (GST_ELEMENT (basesink),
1503 gst_message_new_eos (GST_OBJECT (basesink)));
1505 } else if (!basesink->have_preroll) {
1506 /* don't need preroll, but do queue a commit_state */
1507 GST_DEBUG_OBJECT (basesink,
1508 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1509 basesink->need_preroll = FALSE;
1510 basesink->playing_async = TRUE;
1511 ret = GST_STATE_CHANGE_ASYNC;
1512 /* we know it's not waiting, no need to signal */
1514 /* don't need the preroll anymore */
1515 basesink->need_preroll = FALSE;
1516 GST_DEBUG_OBJECT (basesink,
1517 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1518 /* now let it play */
1519 GST_PREROLL_SIGNAL (basesink->sinkpad);
1521 GST_PREROLL_UNLOCK (basesink->sinkpad);
1528 GstStateChangeReturn bret;
1530 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1531 if (bret == GST_STATE_CHANGE_FAILURE)
1532 goto activate_failed;
1535 switch (transition) {
1536 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1538 GstBaseSinkClass *bclass;
1540 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1542 GST_PREROLL_LOCK (basesink->sinkpad);
1543 GST_LOCK (basesink);
1544 /* unlock clock wait if any */
1545 if (basesink->clock_id) {
1546 gst_clock_id_unschedule (basesink->clock_id);
1548 GST_UNLOCK (basesink);
1550 basesink->playing_async = FALSE;
1552 /* unlock any subclasses */
1554 bclass->unlock (basesink);
1556 /* if we don't have a preroll buffer and we have not received EOS,
1557 * we need to wait for a preroll */
1558 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1559 basesink->have_preroll, basesink->eos);
1560 if (!basesink->have_preroll && !basesink->eos
1561 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1562 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1563 basesink->need_preroll = TRUE;
1564 ret = GST_STATE_CHANGE_ASYNC;
1566 GST_PREROLL_UNLOCK (basesink->sinkpad);
1569 case GST_STATE_CHANGE_PAUSED_TO_READY:
1571 case GST_STATE_CHANGE_READY_TO_NULL:
1573 if (!bclass->stop (basesink)) {
1574 GST_WARNING ("failed to stop");
1586 GST_DEBUG_OBJECT (basesink, "failed to start");
1587 return GST_STATE_CHANGE_FAILURE;
1591 GST_DEBUG_OBJECT (basesink,
1592 "element failed to change states -- activation problem?");
1593 return GST_STATE_CHANGE_FAILURE;