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 #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_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:
470 gint64 segment_start;
473 /* the newsegment event is needed to bring the buffer timestamps to the
474 * stream time and to drop samples outside of the playback segment. */
475 gst_event_parse_newsegment (event, &basesink->segment_rate, &format,
476 &segment_start, &segment_stop, &basesink->segment_base);
478 basesink->have_newsegment = TRUE;
480 /* any other format with 0 also gives time 0, the other values are
481 * invalid as time though. */
482 if (format != GST_FORMAT_TIME && segment_start == 0) {
483 format = GST_FORMAT_TIME;
485 basesink->segment_base = -1;
488 if (format != GST_FORMAT_TIME) {
489 GST_DEBUG_OBJECT (basesink,
490 "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
491 " -- %" G_GINT64_FORMAT ", base %" G_GINT64_FORMAT,
492 format, basesink->segment_start, basesink->segment_stop,
493 basesink->segment_base);
495 /* this means this sink will not be able to clip or drop samples
496 * and timestamps have to start from 0. */
497 basesink->segment_start = -1;
498 basesink->segment_stop = -1;
499 basesink->segment_base = -1;
500 goto done_newsegment;
502 /* check if we really have a new segment or the previous one is
504 if (basesink->segment_start != segment_start) {
505 /* the new segment has to be aligned with the old segment.
506 * We first update the accumulated time of the previous
507 * segment. the accumulated time is used when syncing to the
508 * clock. A flush event sets the accumulated time back to 0
510 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
511 basesink->segment_accum +=
512 basesink->segment_stop - basesink->segment_start;
513 } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
514 /* else use last seen timestamp as segment stop */
515 basesink->segment_accum +=
516 basesink->current_end - basesink->segment_start;
518 basesink->segment_accum = 0;
522 basesink->segment_start = segment_start;
523 basesink->segment_stop = segment_stop;
525 GST_DEBUG_OBJECT (basesink,
526 "received DISCONT %" GST_TIME_FORMAT " -- %"
527 GST_TIME_FORMAT ", base %" GST_TIME_FORMAT ", accum %"
529 GST_TIME_ARGS (basesink->segment_start),
530 GST_TIME_ARGS (basesink->segment_stop),
531 GST_TIME_ARGS (basesink->segment_base),
532 GST_TIME_ARGS (basesink->segment_accum));
539 basesink->events_queued++;
541 GstBuffer *buf = GST_BUFFER (obj);
543 if (!basesink->have_newsegment) {
544 GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
545 ("Received buffer without a new-segment. Cannot sync to clock."),
546 ("Received buffer without a new-segment. Cannot sync to clock."));
547 basesink->have_newsegment = TRUE;
548 /* this means this sink will not be able to sync to the clock */
549 basesink->segment_start = -1;
550 basesink->segment_stop = -1;
553 /* check if the buffer needs to be dropped */
555 GstClockTime start = -1, end = -1;
557 /* we don't use the subclassed method as it may not return
558 * valid values for our purpose here */
559 gst_base_sink_get_times (basesink, buf, &start, &end);
561 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
562 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
563 GST_TIME_ARGS (end));
565 /* need to drop if the timestamp is not between segment_start and
566 * segment_stop. we check if the complete sample is outside of the
567 * range since the sink might be able to clip the sample. */
568 if (GST_CLOCK_TIME_IS_VALID (end) &&
569 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
570 if (end <= basesink->segment_start) {
571 GST_DEBUG_OBJECT (basesink,
572 "buffer end %" GST_TIME_FORMAT " <= segment start %"
573 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
574 GST_TIME_ARGS (basesink->segment_start));
578 if (GST_CLOCK_TIME_IS_VALID (start) &&
579 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
580 if (basesink->segment_stop <= start) {
581 GST_DEBUG_OBJECT (basesink,
582 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
583 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
584 GST_TIME_ARGS (basesink->segment_stop));
589 basesink->preroll_queued++;
590 basesink->buffers_queued++;
592 GST_DEBUG_OBJECT (basesink,
593 "now %d preroll, %d buffers, %d events on queue",
594 basesink->preroll_queued,
595 basesink->buffers_queued, basesink->events_queued);
597 if (basesink->playing_async)
600 /* check if we are prerolling */
601 if (!basesink->need_preroll)
604 /* there is a buffer queued */
605 if (basesink->buffers_queued == 1) {
606 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
608 /* if it's a buffer, we need to call the preroll method */
609 if (GST_IS_BUFFER (obj)) {
610 GstBaseSinkClass *bclass;
613 bclass = GST_BASE_SINK_GET_CLASS (basesink);
616 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
620 length = basesink->preroll_queued;
621 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
627 basesink->have_preroll = TRUE;
628 /* we are prerolling */
629 GST_PREROLL_UNLOCK (pad);
631 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
632 * inside the STREAM_LOCK */
633 t = GST_STREAM_UNLOCK_FULL (pad);
634 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
636 GST_WARNING ("STREAM_LOCK should have been locked !!");
637 g_warning ("STREAM_LOCK should have been locked !!");
640 /* now we commit our state, this will also automatically proceed to
641 * the next pending state. */
643 if ((task = GST_PAD_TASK (pad))) {
644 while (!GST_STATE_TRYLOCK (basesink)) {
645 GST_DEBUG_OBJECT (basesink,
646 "state change happening, checking shutdown");
648 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
653 GST_STATE_LOCK (basesink);
655 GST_DEBUG_OBJECT (basesink, "commit state");
656 gst_element_commit_state (GST_ELEMENT (basesink));
657 GST_STATE_UNLOCK (basesink);
659 /* reacquire stream lock, pad could be flushing now */
660 /* FIXME in glib, if t==0, the lock is still taken... hmmm.. bug #317802 */
662 GST_STREAM_LOCK_FULL (pad, t);
664 /* and wait if needed */
665 GST_PREROLL_LOCK (pad);
668 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
672 /* it is possible that the application set the state to PLAYING
673 * now in which case we don't need to block anymore. */
674 if (!basesink->need_preroll)
678 length = basesink->preroll_queued;
680 /* FIXME: a pad probe could have made us lose the buffer, according
681 * to one of the python tests */
683 GST_ERROR_OBJECT (basesink,
684 "preroll_queued dropped from 1 to 0 while committing state change");
686 g_assert (length <= 1);
689 /* see if we need to block now. We cannot block on events, only
690 * on buffers, the reason is that events can be sent from the
691 * application thread and we don't want to block there. */
692 if (length > basesink->preroll_queue_max_len && !have_event) {
693 /* block until the state changes, or we get a flush, or something */
694 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
695 GST_PREROLL_WAIT (pad);
696 GST_DEBUG_OBJECT (basesink, "done preroll");
699 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
703 GST_PREROLL_UNLOCK (pad);
711 GST_DEBUG_OBJECT (basesink, "no preroll needed");
712 /* maybe it was another sink that blocked in preroll, need to check for
714 basesink->have_preroll = FALSE;
715 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
716 GST_PREROLL_UNLOCK (pad);
724 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
726 gst_buffer_unref (buf);
727 GST_PREROLL_UNLOCK (pad);
736 basesink->have_preroll = FALSE;
737 basesink->playing_async = FALSE;
739 /* handle buffer first */
740 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
742 /* unroll locks, commit state, reacquire stream lock */
743 GST_PREROLL_UNLOCK (pad);
744 t = GST_STREAM_UNLOCK_FULL (pad);
745 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
747 GST_WARNING ("STREAM_LOCK should have been locked !!");
748 g_warning ("STREAM_LOCK should have been locked !!");
750 GST_STATE_LOCK (basesink);
751 GST_DEBUG_OBJECT (basesink, "commit state");
752 gst_element_commit_state (GST_ELEMENT (basesink));
753 GST_STATE_UNLOCK (basesink);
755 GST_STREAM_LOCK_FULL (pad, t);
762 GST_DEBUG_OBJECT (basesink, "task is stopped");
763 return GST_FLOW_WRONG_STATE;
768 gst_base_sink_preroll_queue_flush (basesink, pad);
769 GST_PREROLL_UNLOCK (pad);
770 GST_DEBUG_OBJECT (basesink, "pad is flushing");
771 return GST_FLOW_WRONG_STATE;
777 GST_DEBUG_OBJECT (basesink, "preroll failed");
778 gst_base_sink_preroll_queue_flush (basesink, pad);
779 GST_PREROLL_UNLOCK (pad);
781 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
782 * inside the STREAM_LOCK */
783 t = GST_STREAM_UNLOCK_FULL (pad);
784 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
786 GST_WARNING ("STREAM_LOCK should have been locked !!");
787 g_warning ("STREAM_LOCK should have been locked !!");
790 /* now we abort our state */
791 GST_STATE_LOCK (basesink);
792 GST_DEBUG_OBJECT (basesink, "abort state");
793 gst_element_abort_state (GST_ELEMENT (basesink));
794 GST_STATE_UNLOCK (basesink);
796 /* reacquire stream lock, pad could be flushing now */
798 GST_STREAM_LOCK_FULL (pad, t);
800 return GST_FLOW_ERROR;
805 gst_base_sink_event (GstPad * pad, GstEvent * event)
807 GstBaseSink *basesink;
808 gboolean result = TRUE;
809 GstBaseSinkClass *bclass;
811 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
813 bclass = GST_BASE_SINK_GET_CLASS (basesink);
815 GST_DEBUG_OBJECT (basesink, "event %p", event);
817 switch (GST_EVENT_TYPE (event)) {
822 GST_STREAM_LOCK (pad);
823 /* EOS also finishes the preroll */
825 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
826 GST_STREAM_UNLOCK (pad);
829 case GST_EVENT_NEWSEGMENT:
833 GST_STREAM_LOCK (pad);
835 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
836 GST_STREAM_UNLOCK (pad);
839 case GST_EVENT_FLUSH_START:
840 /* make sure we are not blocked on the clock also clear any pending
843 bclass->event (basesink, event);
846 basesink->flushing = TRUE;
847 if (basesink->clock_id) {
848 gst_clock_id_unschedule (basesink->clock_id);
850 GST_UNLOCK (basesink);
852 GST_PREROLL_LOCK (pad);
853 /* we need preroll after the flush */
854 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
855 basesink->need_preroll = TRUE;
856 /* unlock from a possible state change/preroll */
857 gst_base_sink_preroll_queue_flush (basesink, pad);
858 GST_PREROLL_UNLOCK (pad);
860 /* and we need to commit our state again on the next
861 * prerolled buffer */
862 GST_STATE_LOCK (basesink);
863 GST_STREAM_LOCK (pad);
864 gst_element_lost_state (GST_ELEMENT (basesink));
865 GST_STREAM_UNLOCK (pad);
866 GST_STATE_UNLOCK (basesink);
867 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
868 gst_event_unref (event);
870 case GST_EVENT_FLUSH_STOP:
872 bclass->event (basesink, event);
874 /* now we are completely unblocked and the _chain method
876 GST_STREAM_LOCK (pad);
878 basesink->flushing = FALSE;
879 GST_UNLOCK (basesink);
880 /* we need new segment info after the flush. */
881 basesink->segment_start = -1;
882 basesink->segment_stop = -1;
883 basesink->current_start = -1;
884 basesink->current_end = -1;
885 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
886 GST_TIME_ARGS (basesink->segment_accum));
887 basesink->segment_accum = 0;
888 GST_STREAM_UNLOCK (pad);
890 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
891 gst_event_unref (event);
894 gst_event_unref (event);
897 gst_object_unref (basesink);
902 /* default implementation to calculate the start and end
903 * timestamps on a buffer, subclasses can override
906 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
907 GstClockTime * start, GstClockTime * end)
909 GstClockTime timestamp, duration;
911 timestamp = GST_BUFFER_TIMESTAMP (buffer);
912 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
914 /* get duration to calculate end time */
915 duration = GST_BUFFER_DURATION (buffer);
916 if (GST_CLOCK_TIME_IS_VALID (duration)) {
917 *end = timestamp + duration;
923 /* with STREAM_LOCK and LOCK*/
924 static GstClockReturn
925 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
930 /* no need to attempt a clock wait if we are flushing */
931 if (basesink->flushing) {
932 return GST_CLOCK_UNSCHEDULED;
935 /* clock_id should be NULL outside of this function */
936 g_assert (basesink->clock_id == NULL);
937 g_assert (GST_CLOCK_TIME_IS_VALID (time));
939 id = gst_clock_new_single_shot_id (basesink->clock, time);
941 basesink->clock_id = id;
942 /* release the object lock while waiting */
943 GST_UNLOCK (basesink);
945 ret = gst_clock_id_wait (id, NULL);
948 gst_clock_id_unref (id);
949 basesink->clock_id = NULL;
954 /* perform synchronisation on a buffer
956 * 1) check if we have a clock, if not, do nothing
957 * 2) calculate the start and end time of the buffer
958 * 3) create a single shot notification to wait on
959 * the clock, save the entry so we can unlock it
960 * 4) wait on the clock, this blocks
961 * 5) unref the clockid again
963 static GstClockReturn
964 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
966 GstClockReturn result = GST_CLOCK_OK;
967 GstClockTime start, end;
968 GstClockTimeDiff stream_start, stream_end;
969 GstBaseSinkClass *bclass;
970 gboolean start_valid, end_valid;
972 bclass = GST_BASE_SINK_GET_CLASS (basesink);
975 if (bclass->get_times)
976 bclass->get_times (basesink, buffer, &start, &end);
978 start_valid = GST_CLOCK_TIME_IS_VALID (start);
979 end_valid = GST_CLOCK_TIME_IS_VALID (start);
981 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
982 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
984 /* if we don't have a timestamp, we don't sync */
988 /* save last times seen. */
989 basesink->current_start = start;
991 basesink->current_end = end;
993 basesink->current_end = start;
995 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
996 /* check if not outside of the segment range, start is
997 * always valid here. */
998 if (start > basesink->segment_stop)
1002 /* bring timestamp to stream time using last segment offset. */
1003 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1004 /* check if not outside of the segment range */
1005 if (end_valid && end < basesink->segment_start)
1006 goto out_of_segment;
1008 stream_start = (gint64) start - basesink->segment_start;
1009 stream_end = (gint64) end - basesink->segment_start;
1011 stream_start = (gint64) start;
1012 stream_end = (gint64) end;
1015 stream_start += basesink->segment_accum;
1017 stream_end += basesink->segment_accum;
1019 /* now do clocking */
1020 if (basesink->clock && basesink->sync) {
1021 GstClockTime base_time;
1023 GST_LOCK (basesink);
1025 base_time = GST_ELEMENT (basesink)->base_time;
1027 GST_LOG_OBJECT (basesink,
1028 "waiting for clock, base time %" GST_TIME_FORMAT,
1029 GST_TIME_ARGS (base_time));
1031 /* also save end_time of this buffer so that we can wait
1034 basesink->end_time = stream_end + base_time;
1036 basesink->end_time = GST_CLOCK_TIME_NONE;
1038 result = gst_base_sink_wait (basesink, stream_start + base_time);
1040 GST_UNLOCK (basesink);
1042 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1050 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1051 return GST_CLOCK_UNSCHEDULED;
1058 * 2) render the event
1059 * 3) unref the event
1061 static inline gboolean
1062 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1064 GstBaseSinkClass *bclass;
1067 switch (GST_EVENT_TYPE (event)) {
1069 GST_LOCK (basesink);
1070 if (basesink->clock) {
1071 /* wait for last buffer to finish if we have a valid end time */
1072 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1073 gst_base_sink_wait (basesink, basesink->end_time);
1074 basesink->end_time = GST_CLOCK_TIME_NONE;
1077 GST_UNLOCK (basesink);
1083 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1085 ret = bclass->event (basesink, event);
1089 switch (GST_EVENT_TYPE (event)) {
1091 GST_PREROLL_LOCK (basesink->sinkpad);
1092 /* if we are still EOS, we can post the EOS message */
1093 if (basesink->eos) {
1094 /* ok, now we can post the message */
1095 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1096 gst_element_post_message (GST_ELEMENT (basesink),
1097 gst_message_new_eos (GST_OBJECT (basesink)));
1098 basesink->eos_queued = FALSE;
1100 GST_PREROLL_UNLOCK (basesink->sinkpad);
1106 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1107 gst_event_unref (event);
1114 * 1) first sync on the buffer
1115 * 2) render the buffer
1116 * 3) unref the buffer
1118 static inline GstFlowReturn
1119 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1121 GstFlowReturn ret = GST_FLOW_OK;
1122 GstClockReturn status;
1124 status = gst_base_sink_do_sync (basesink, buf);
1126 case GST_CLOCK_EARLY:
1127 GST_DEBUG_OBJECT (basesink, "buffer too late!");
1128 /* fallthrough for now */
1131 GstBaseSinkClass *bclass;
1133 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1135 ret = bclass->render (basesink, buf);
1142 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1143 gst_buffer_unref (buf);
1148 static GstFlowReturn
1149 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1151 GstBaseSink *basesink;
1152 GstFlowReturn result;
1154 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1156 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1158 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1159 GST_DEBUG_PAD_NAME (pad));
1161 result = GST_FLOW_UNEXPECTED;
1165 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1168 gst_object_unref (basesink);
1174 gst_base_sink_loop (GstPad * pad)
1176 GstBaseSink *basesink;
1177 GstBuffer *buf = NULL;
1178 GstFlowReturn result;
1180 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1182 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1184 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1185 if (result != GST_FLOW_OK)
1188 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1189 if (result != GST_FLOW_OK)
1192 gst_object_unref (basesink);
1199 gst_base_sink_event (pad, gst_event_new_eos ());
1200 gst_object_unref (basesink);
1201 gst_pad_pause_task (pad);
1207 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1209 gboolean result = FALSE;
1210 GstBaseSinkClass *bclass;
1212 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1214 /* step 1, unblock clock sync (if any) or any other blocking thing */
1215 GST_PREROLL_LOCK (pad);
1216 GST_LOCK (basesink);
1217 if (basesink->clock_id) {
1218 gst_clock_id_unschedule (basesink->clock_id);
1220 GST_UNLOCK (basesink);
1222 /* unlock any subclasses */
1224 bclass->unlock (basesink);
1226 /* flush out the data thread if it's locked in finish_preroll */
1227 GST_DEBUG_OBJECT (basesink,
1228 "flushing out data thread, need preroll to FALSE");
1229 basesink->need_preroll = FALSE;
1230 gst_base_sink_preroll_queue_flush (basesink, pad);
1231 GST_PREROLL_SIGNAL (pad);
1232 GST_PREROLL_UNLOCK (pad);
1234 /* step 2, make sure streaming finishes */
1235 result = gst_pad_stop_task (pad);
1241 gst_base_sink_activate (GstPad * pad)
1243 gboolean result = FALSE;
1244 GstBaseSink *basesink;
1246 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1248 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1250 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1251 && gst_pad_activate_pull (pad, TRUE)) {
1252 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1255 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1256 if (gst_pad_activate_push (pad, TRUE)) {
1257 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1263 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1266 gst_object_unref (basesink);
1272 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1275 GstBaseSink *basesink;
1277 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1280 if (!basesink->can_activate_push) {
1282 basesink->pad_mode = GST_ACTIVATE_NONE;
1285 basesink->pad_mode = GST_ACTIVATE_PUSH;
1288 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1289 g_warning ("Internal GStreamer activation error!!!");
1292 result = gst_base_sink_deactivate (basesink, pad);
1293 basesink->pad_mode = GST_ACTIVATE_NONE;
1297 gst_object_unref (basesink);
1302 /* this won't get called until we implement an activate function */
1304 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1306 gboolean result = FALSE;
1307 GstBaseSink *basesink;
1309 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1312 if (!basesink->can_activate_pull) {
1314 basesink->pad_mode = GST_ACTIVATE_NONE;
1316 GstPad *peer = gst_pad_get_peer (pad);
1318 if (G_UNLIKELY (peer == NULL)) {
1319 g_warning ("Trying to activate pad in pull mode, but no peer");
1321 basesink->pad_mode = GST_ACTIVATE_NONE;
1323 if (gst_pad_activate_pull (peer, TRUE)) {
1324 basesink->have_newsegment = TRUE;
1325 basesink->segment_start = basesink->segment_stop = 0;
1327 /* set the pad mode before starting the task so that it's in the
1328 correct state for the new thread... */
1329 basesink->pad_mode = GST_ACTIVATE_PULL;
1331 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1333 /* but if starting the thread fails, set it back */
1335 basesink->pad_mode = GST_ACTIVATE_NONE;
1337 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1339 basesink->pad_mode = GST_ACTIVATE_NONE;
1341 gst_object_unref (peer);
1345 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1346 g_warning ("Internal GStreamer activation error!!!");
1349 basesink->have_newsegment = FALSE;
1350 result = gst_base_sink_deactivate (basesink, pad);
1351 basesink->pad_mode = GST_ACTIVATE_NONE;
1355 gst_object_unref (basesink);
1361 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1364 GstBaseSink *basesink = GST_BASE_SINK (element);
1368 pad = basesink->sinkpad;
1369 gst_object_ref (pad);
1370 GST_UNLOCK (element);
1372 result = gst_pad_push_event (pad, event);
1374 gst_object_unref (pad);
1380 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1383 gboolean res = FALSE;
1385 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1386 res = gst_pad_query (peer, query);
1387 gst_object_unref (peer);
1393 gst_base_sink_query (GstElement * element, GstQuery * query)
1395 gboolean res = FALSE;
1397 GstBaseSink *basesink = GST_BASE_SINK (element);
1399 switch (GST_QUERY_TYPE (query)) {
1400 case GST_QUERY_POSITION:
1401 res = gst_base_sink_peer_query (basesink, query);
1403 case GST_QUERY_LATENCY:
1405 case GST_QUERY_JITTER:
1407 case GST_QUERY_RATE:
1408 //gst_query_set_rate (query, basesink->segment_rate);
1411 case GST_QUERY_SEEKING:
1412 res = gst_base_sink_peer_query (basesink, query);
1414 case GST_QUERY_SEGMENT:
1416 gst_query_set_segment (query, basesink->segment_rate,
1417 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1418 basesink->segment_base);
1421 case GST_QUERY_CONVERT:
1422 res = gst_base_sink_peer_query (basesink, query);
1424 case GST_QUERY_FORMATS:
1425 res = gst_base_sink_peer_query (basesink, query);
1433 static GstStateChangeReturn
1434 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1436 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1437 GstBaseSink *basesink = GST_BASE_SINK (element);
1438 GstBaseSinkClass *bclass;
1440 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1442 switch (transition) {
1443 case GST_STATE_CHANGE_NULL_TO_READY:
1445 if (!bclass->start (basesink))
1448 case GST_STATE_CHANGE_READY_TO_PAUSED:
1449 /* need to complete preroll before this state change completes, there
1450 * is no data flow in READY so we can safely assume we need to preroll. */
1451 basesink->offset = 0;
1452 GST_PREROLL_LOCK (basesink->sinkpad);
1453 basesink->have_preroll = FALSE;
1454 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1455 basesink->need_preroll = TRUE;
1456 GST_PREROLL_UNLOCK (basesink->sinkpad);
1457 basesink->have_newsegment = FALSE;
1458 basesink->segment_rate = 1.0;
1459 basesink->segment_start = 0;
1460 basesink->segment_stop = 0;
1461 ret = GST_STATE_CHANGE_ASYNC;
1463 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1464 GST_PREROLL_LOCK (basesink->sinkpad);
1465 /* if we have EOS, we should empty the queue now as there will
1466 * be no more data received in the chain function.
1467 * FIXME, this could block the state change function too long when
1468 * we are pushing and syncing the buffers, better start a new
1469 * thread to do this. */
1470 if (basesink->eos) {
1471 gboolean do_eos = !basesink->eos_queued;
1473 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1475 /* need to post EOS message here if it was not in the preroll queue we
1478 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1479 gst_element_post_message (GST_ELEMENT (basesink),
1480 gst_message_new_eos (GST_OBJECT (basesink)));
1482 } else if (!basesink->have_preroll) {
1483 /* don't need preroll, but do queue a commit_state */
1484 GST_DEBUG_OBJECT (basesink,
1485 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1486 basesink->need_preroll = FALSE;
1487 basesink->playing_async = TRUE;
1488 ret = GST_STATE_CHANGE_ASYNC;
1489 /* we know it's not waiting, no need to signal */
1491 /* don't need the preroll anymore */
1492 basesink->need_preroll = FALSE;
1493 GST_DEBUG_OBJECT (basesink,
1494 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1495 /* now let it play */
1496 GST_PREROLL_SIGNAL (basesink->sinkpad);
1498 GST_PREROLL_UNLOCK (basesink->sinkpad);
1505 GstStateChangeReturn bret;
1507 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1508 if (bret == GST_STATE_CHANGE_FAILURE)
1509 goto activate_failed;
1512 switch (transition) {
1513 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1515 GstBaseSinkClass *bclass;
1517 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1519 GST_PREROLL_LOCK (basesink->sinkpad);
1520 GST_LOCK (basesink);
1521 /* unlock clock wait if any */
1522 if (basesink->clock_id) {
1523 gst_clock_id_unschedule (basesink->clock_id);
1525 GST_UNLOCK (basesink);
1527 basesink->playing_async = FALSE;
1529 /* unlock any subclasses */
1531 bclass->unlock (basesink);
1533 /* if we don't have a preroll buffer and we have not received EOS,
1534 * we need to wait for a preroll */
1535 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1536 basesink->have_preroll, basesink->eos);
1537 if (!basesink->have_preroll && !basesink->eos
1538 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1539 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1540 basesink->need_preroll = TRUE;
1541 ret = GST_STATE_CHANGE_ASYNC;
1543 GST_PREROLL_UNLOCK (basesink->sinkpad);
1546 case GST_STATE_CHANGE_PAUSED_TO_READY:
1548 case GST_STATE_CHANGE_READY_TO_NULL:
1550 if (!bclass->stop (basesink)) {
1551 GST_WARNING ("failed to stop");
1563 GST_DEBUG_OBJECT (basesink, "failed to start");
1564 return GST_STATE_CHANGE_FAILURE;
1568 GST_DEBUG_OBJECT (basesink,
1569 "element failed to change states -- activation problem?");
1570 return GST_STATE_CHANGE_FAILURE;