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_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 format = GST_FORMAT_TIME;
493 if (format != GST_FORMAT_TIME) {
494 GST_DEBUG_OBJECT (basesink,
495 "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
496 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT,
497 format, segment_start, segment_stop, segment_time);
499 /* this means this sink will not be able to clip or drop samples
500 * and timestamps have to start from 0. */
501 basesink->segment_start = -1;
502 basesink->segment_stop = -1;
503 basesink->segment_time = -1;
504 goto done_newsegment;
506 /* check if we really have a new segment or the previous one is
509 /* the new segment has to be aligned with the old segment.
510 * We first update the accumulated time of the previous
511 * segment. the accumulated time is used when syncing to the
512 * clock. A flush event sets the accumulated time back to 0
514 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
515 duration = basesink->segment_stop - basesink->segment_start;
516 } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
517 /* else use last seen timestamp as segment stop */
518 duration = basesink->current_end - basesink->segment_start;
523 duration = segment_start - basesink->segment_start;
526 /* use previous rate to calculate duration */
527 basesink->segment_accum += gst_gdouble_to_guint64 (
528 (gst_guint64_to_gdouble (duration) / ABS (basesink->segment_rate)));
529 /* then update the current segment */
530 basesink->segment_rate = rate;
531 basesink->segment_start = segment_start;
532 basesink->segment_stop = segment_stop;
533 basesink->segment_time = segment_time;
535 GST_DEBUG_OBJECT (basesink,
536 "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
537 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
539 GST_TIME_ARGS (basesink->segment_start),
540 GST_TIME_ARGS (basesink->segment_stop),
541 GST_TIME_ARGS (basesink->segment_time),
542 GST_TIME_ARGS (basesink->segment_accum));
549 basesink->events_queued++;
551 GstBuffer *buf = GST_BUFFER (obj);
553 if (!basesink->have_newsegment) {
554 GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
555 ("Received buffer without a new-segment. Cannot sync to clock."),
556 ("Received buffer without a new-segment. Cannot sync to clock."));
557 basesink->have_newsegment = TRUE;
558 /* this means this sink will not be able to sync to the clock */
559 basesink->segment_start = -1;
560 basesink->segment_stop = -1;
563 /* check if the buffer needs to be dropped */
565 GstClockTime start = -1, end = -1;
567 /* we don't use the subclassed method as it may not return
568 * valid values for our purpose here */
569 gst_base_sink_get_times (basesink, buf, &start, &end);
571 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
572 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
573 GST_TIME_ARGS (end));
575 /* need to drop if the timestamp is not between segment_start and
576 * segment_stop. we check if the complete sample is outside of the
577 * range since the sink might be able to clip the sample. */
578 if (GST_CLOCK_TIME_IS_VALID (end) &&
579 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
580 if (end <= basesink->segment_start) {
581 GST_DEBUG_OBJECT (basesink,
582 "buffer end %" GST_TIME_FORMAT " <= segment start %"
583 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
584 GST_TIME_ARGS (basesink->segment_start));
588 if (GST_CLOCK_TIME_IS_VALID (start) &&
589 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
590 if (basesink->segment_stop <= start) {
591 GST_DEBUG_OBJECT (basesink,
592 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
593 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
594 GST_TIME_ARGS (basesink->segment_stop));
599 basesink->preroll_queued++;
600 basesink->buffers_queued++;
602 GST_DEBUG_OBJECT (basesink,
603 "now %d preroll, %d buffers, %d events on queue",
604 basesink->preroll_queued,
605 basesink->buffers_queued, basesink->events_queued);
607 if (basesink->playing_async)
610 /* check if we are prerolling */
611 if (!basesink->need_preroll)
614 /* there is a buffer queued */
615 if (basesink->buffers_queued == 1) {
616 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
618 /* if it's a buffer, we need to call the preroll method */
619 if (GST_IS_BUFFER (obj)) {
620 GstBaseSinkClass *bclass;
623 bclass = GST_BASE_SINK_GET_CLASS (basesink);
626 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
630 length = basesink->preroll_queued;
631 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
637 basesink->have_preroll = TRUE;
638 /* we are prerolling */
639 GST_PREROLL_UNLOCK (pad);
641 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
642 * inside the STREAM_LOCK */
643 t = GST_STREAM_UNLOCK_FULL (pad);
644 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
646 GST_WARNING ("STREAM_LOCK should have been locked !!");
647 g_warning ("STREAM_LOCK should have been locked !!");
650 /* now we commit our state, this will also automatically proceed to
651 * the next pending state. */
653 if ((task = GST_PAD_TASK (pad))) {
654 while (!GST_STATE_TRYLOCK (basesink)) {
655 GST_DEBUG_OBJECT (basesink,
656 "state change happening, checking shutdown");
658 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
663 GST_STATE_LOCK (basesink);
665 GST_DEBUG_OBJECT (basesink, "commit state");
666 gst_element_commit_state (GST_ELEMENT (basesink));
667 GST_STATE_UNLOCK (basesink);
669 /* reacquire stream lock, pad could be flushing now */
670 /* FIXME in glib, if t==0, the lock is still taken... hmmm.. bug #317802 */
672 GST_STREAM_LOCK_FULL (pad, t);
674 /* and wait if needed */
675 GST_PREROLL_LOCK (pad);
678 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
682 /* it is possible that the application set the state to PLAYING
683 * now in which case we don't need to block anymore. */
684 if (!basesink->need_preroll)
688 length = basesink->preroll_queued;
690 /* FIXME: a pad probe could have made us lose the buffer, according
691 * to one of the python tests */
693 GST_ERROR_OBJECT (basesink,
694 "preroll_queued dropped from 1 to 0 while committing state change");
696 g_assert (length <= 1);
699 /* see if we need to block now. We cannot block on events, only
700 * on buffers, the reason is that events can be sent from the
701 * application thread and we don't want to block there. */
702 if (length > basesink->preroll_queue_max_len && !have_event) {
703 /* block until the state changes, or we get a flush, or something */
704 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
705 GST_PREROLL_WAIT (pad);
706 GST_DEBUG_OBJECT (basesink, "done preroll");
709 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
713 GST_PREROLL_UNLOCK (pad);
721 GST_DEBUG_OBJECT (basesink, "no preroll needed");
722 /* maybe it was another sink that blocked in preroll, need to check for
724 basesink->have_preroll = FALSE;
725 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
726 GST_PREROLL_UNLOCK (pad);
734 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
736 gst_buffer_unref (buf);
737 GST_PREROLL_UNLOCK (pad);
746 basesink->have_preroll = FALSE;
747 basesink->playing_async = FALSE;
749 /* handle buffer first */
750 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
752 /* unroll locks, commit state, reacquire stream lock */
753 GST_PREROLL_UNLOCK (pad);
754 t = GST_STREAM_UNLOCK_FULL (pad);
755 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
757 GST_WARNING ("STREAM_LOCK should have been locked !!");
758 g_warning ("STREAM_LOCK should have been locked !!");
760 GST_STATE_LOCK (basesink);
761 GST_DEBUG_OBJECT (basesink, "commit state");
762 gst_element_commit_state (GST_ELEMENT (basesink));
763 GST_STATE_UNLOCK (basesink);
765 GST_STREAM_LOCK_FULL (pad, t);
772 GST_DEBUG_OBJECT (basesink, "task is stopped");
773 return GST_FLOW_WRONG_STATE;
778 gst_base_sink_preroll_queue_flush (basesink, pad);
779 GST_PREROLL_UNLOCK (pad);
780 GST_DEBUG_OBJECT (basesink, "pad is flushing");
781 return GST_FLOW_WRONG_STATE;
787 GST_DEBUG_OBJECT (basesink, "preroll failed");
788 gst_base_sink_preroll_queue_flush (basesink, pad);
789 GST_PREROLL_UNLOCK (pad);
791 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
792 * inside the STREAM_LOCK */
793 t = GST_STREAM_UNLOCK_FULL (pad);
794 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
796 GST_WARNING ("STREAM_LOCK should have been locked !!");
797 g_warning ("STREAM_LOCK should have been locked !!");
800 /* now we abort our state */
801 GST_STATE_LOCK (basesink);
802 GST_DEBUG_OBJECT (basesink, "abort state");
803 gst_element_abort_state (GST_ELEMENT (basesink));
804 GST_STATE_UNLOCK (basesink);
806 /* reacquire stream lock, pad could be flushing now */
808 GST_STREAM_LOCK_FULL (pad, t);
810 return GST_FLOW_ERROR;
815 gst_base_sink_event (GstPad * pad, GstEvent * event)
817 GstBaseSink *basesink;
818 gboolean result = TRUE;
819 GstBaseSinkClass *bclass;
821 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
823 bclass = GST_BASE_SINK_GET_CLASS (basesink);
825 GST_DEBUG_OBJECT (basesink, "event %p", event);
827 switch (GST_EVENT_TYPE (event)) {
832 GST_STREAM_LOCK (pad);
833 /* EOS also finishes the preroll */
835 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
836 GST_STREAM_UNLOCK (pad);
839 case GST_EVENT_NEWSEGMENT:
843 GST_STREAM_LOCK (pad);
845 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
846 GST_STREAM_UNLOCK (pad);
849 case GST_EVENT_FLUSH_START:
850 /* make sure we are not blocked on the clock also clear any pending
853 bclass->event (basesink, event);
856 basesink->flushing = TRUE;
857 if (basesink->clock_id) {
858 gst_clock_id_unschedule (basesink->clock_id);
860 GST_UNLOCK (basesink);
862 GST_PREROLL_LOCK (pad);
863 /* we need preroll after the flush */
864 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
865 basesink->need_preroll = TRUE;
866 /* unlock from a possible state change/preroll */
867 gst_base_sink_preroll_queue_flush (basesink, pad);
868 GST_PREROLL_UNLOCK (pad);
870 /* and we need to commit our state again on the next
871 * prerolled buffer */
872 GST_STATE_LOCK (basesink);
873 GST_STREAM_LOCK (pad);
874 gst_element_lost_state (GST_ELEMENT (basesink));
875 GST_STREAM_UNLOCK (pad);
876 GST_STATE_UNLOCK (basesink);
877 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
878 gst_event_unref (event);
880 case GST_EVENT_FLUSH_STOP:
882 bclass->event (basesink, event);
884 /* now we are completely unblocked and the _chain method
886 GST_STREAM_LOCK (pad);
888 basesink->flushing = FALSE;
889 GST_UNLOCK (basesink);
890 /* we need new segment info after the flush. */
891 basesink->segment_start = -1;
892 basesink->segment_stop = -1;
893 basesink->current_start = -1;
894 basesink->current_end = -1;
895 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
896 GST_TIME_ARGS (basesink->segment_accum));
897 basesink->segment_accum = 0;
898 GST_STREAM_UNLOCK (pad);
900 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
901 gst_event_unref (event);
904 gst_event_unref (event);
907 gst_object_unref (basesink);
912 /* default implementation to calculate the start and end
913 * timestamps on a buffer, subclasses can override
916 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
917 GstClockTime * start, GstClockTime * end)
919 GstClockTime timestamp, duration;
921 timestamp = GST_BUFFER_TIMESTAMP (buffer);
922 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
924 /* get duration to calculate end time */
925 duration = GST_BUFFER_DURATION (buffer);
926 if (GST_CLOCK_TIME_IS_VALID (duration)) {
927 *end = timestamp + duration;
933 /* with STREAM_LOCK and LOCK*/
934 static GstClockReturn
935 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
940 /* no need to attempt a clock wait if we are flushing */
941 if (basesink->flushing) {
942 return GST_CLOCK_UNSCHEDULED;
945 /* clock_id should be NULL outside of this function */
946 g_assert (basesink->clock_id == NULL);
947 g_assert (GST_CLOCK_TIME_IS_VALID (time));
949 id = gst_clock_new_single_shot_id (basesink->clock, time);
951 basesink->clock_id = id;
952 /* release the object lock while waiting */
953 GST_UNLOCK (basesink);
955 ret = gst_clock_id_wait (id, NULL);
958 gst_clock_id_unref (id);
959 basesink->clock_id = NULL;
964 /* perform synchronisation on a buffer
966 * 1) check if we have a clock, if not, do nothing
967 * 2) calculate the start and end time of the buffer
968 * 3) create a single shot notification to wait on
969 * the clock, save the entry so we can unlock it
970 * 4) wait on the clock, this blocks
971 * 5) unref the clockid again
973 static GstClockReturn
974 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
976 GstClockReturn result = GST_CLOCK_OK;
977 GstClockTime start, end;
978 GstClockTimeDiff stream_start, stream_end;
979 GstBaseSinkClass *bclass;
980 gboolean start_valid, end_valid;
982 bclass = GST_BASE_SINK_GET_CLASS (basesink);
985 if (bclass->get_times)
986 bclass->get_times (basesink, buffer, &start, &end);
988 start_valid = GST_CLOCK_TIME_IS_VALID (start);
989 end_valid = GST_CLOCK_TIME_IS_VALID (start);
991 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
992 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
994 /* if we don't have a timestamp, we don't sync */
998 /* save last times seen. */
999 basesink->current_start = start;
1001 basesink->current_end = end;
1003 basesink->current_end = start;
1005 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
1006 /* check if not outside of the segment range, start is
1007 * always valid here. */
1008 if (start > basesink->segment_stop)
1009 goto out_of_segment;
1012 /* bring timestamp to stream time using last segment offset. */
1013 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1014 /* check if not outside of the segment range */
1015 if (end_valid && end < basesink->segment_start)
1016 goto out_of_segment;
1018 stream_start = (gint64) start - basesink->segment_start;
1019 stream_end = (gint64) end - basesink->segment_start;
1021 stream_start = (gint64) start;
1022 stream_end = (gint64) end;
1025 /* correct for rate */
1026 if (basesink->segment_rate != 0.0) {
1027 stream_start /= ABS (basesink->segment_rate);
1029 stream_end /= ABS (basesink->segment_rate);
1032 stream_start += basesink->segment_accum;
1034 stream_end += basesink->segment_accum;
1036 /* now do clocking */
1037 if (basesink->clock && basesink->sync) {
1038 GstClockTime base_time;
1040 GST_LOCK (basesink);
1042 base_time = GST_ELEMENT (basesink)->base_time;
1044 GST_LOG_OBJECT (basesink,
1045 "waiting for clock, base time %" GST_TIME_FORMAT,
1046 GST_TIME_ARGS (base_time));
1048 /* also save end_time of this buffer so that we can wait
1051 basesink->end_time = stream_end + base_time;
1053 basesink->end_time = GST_CLOCK_TIME_NONE;
1055 result = gst_base_sink_wait (basesink, stream_start + base_time);
1057 GST_UNLOCK (basesink);
1059 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1067 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1068 return GST_CLOCK_UNSCHEDULED;
1075 * 2) render the event
1076 * 3) unref the event
1078 static inline gboolean
1079 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1081 GstBaseSinkClass *bclass;
1084 switch (GST_EVENT_TYPE (event)) {
1086 GST_LOCK (basesink);
1087 if (basesink->clock) {
1088 /* wait for last buffer to finish if we have a valid end time */
1089 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1090 gst_base_sink_wait (basesink, basesink->end_time);
1091 basesink->end_time = GST_CLOCK_TIME_NONE;
1094 GST_UNLOCK (basesink);
1100 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1102 ret = bclass->event (basesink, event);
1106 switch (GST_EVENT_TYPE (event)) {
1108 GST_PREROLL_LOCK (basesink->sinkpad);
1109 /* if we are still EOS, we can post the EOS message */
1110 if (basesink->eos) {
1111 /* ok, now we can post the message */
1112 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1113 gst_element_post_message (GST_ELEMENT (basesink),
1114 gst_message_new_eos (GST_OBJECT (basesink)));
1115 basesink->eos_queued = FALSE;
1117 GST_PREROLL_UNLOCK (basesink->sinkpad);
1123 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1124 gst_event_unref (event);
1131 * 1) first sync on the buffer
1132 * 2) render the buffer
1133 * 3) unref the buffer
1135 static inline GstFlowReturn
1136 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1138 GstFlowReturn ret = GST_FLOW_OK;
1139 GstClockReturn status;
1141 status = gst_base_sink_do_sync (basesink, buf);
1143 case GST_CLOCK_EARLY:
1144 GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1145 /* fallthrough for now */
1148 GstBaseSinkClass *bclass;
1150 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1152 ret = bclass->render (basesink, buf);
1156 GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1160 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1161 gst_buffer_unref (buf);
1166 static GstFlowReturn
1167 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1169 GstBaseSink *basesink;
1170 GstFlowReturn result;
1172 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1174 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1176 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1177 GST_DEBUG_PAD_NAME (pad));
1179 result = GST_FLOW_UNEXPECTED;
1183 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1186 gst_object_unref (basesink);
1192 gst_base_sink_loop (GstPad * pad)
1194 GstBaseSink *basesink;
1195 GstBuffer *buf = NULL;
1196 GstFlowReturn result;
1198 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1200 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1202 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1203 if (result != GST_FLOW_OK)
1206 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1207 if (result != GST_FLOW_OK)
1210 gst_object_unref (basesink);
1217 gst_base_sink_event (pad, gst_event_new_eos ());
1218 gst_object_unref (basesink);
1219 gst_pad_pause_task (pad);
1225 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1227 gboolean result = FALSE;
1228 GstBaseSinkClass *bclass;
1230 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1232 /* step 1, unblock clock sync (if any) or any other blocking thing */
1233 GST_PREROLL_LOCK (pad);
1234 GST_LOCK (basesink);
1235 if (basesink->clock_id) {
1236 gst_clock_id_unschedule (basesink->clock_id);
1238 GST_UNLOCK (basesink);
1240 /* unlock any subclasses */
1242 bclass->unlock (basesink);
1244 /* flush out the data thread if it's locked in finish_preroll */
1245 GST_DEBUG_OBJECT (basesink,
1246 "flushing out data thread, need preroll to FALSE");
1247 basesink->need_preroll = FALSE;
1248 gst_base_sink_preroll_queue_flush (basesink, pad);
1249 GST_PREROLL_SIGNAL (pad);
1250 GST_PREROLL_UNLOCK (pad);
1252 /* step 2, make sure streaming finishes */
1253 result = gst_pad_stop_task (pad);
1259 gst_base_sink_activate (GstPad * pad)
1261 gboolean result = FALSE;
1262 GstBaseSink *basesink;
1264 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1266 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1268 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1269 && gst_pad_activate_pull (pad, TRUE)) {
1270 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1273 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1274 if (gst_pad_activate_push (pad, TRUE)) {
1275 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1281 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1284 gst_object_unref (basesink);
1290 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1293 GstBaseSink *basesink;
1295 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1298 if (!basesink->can_activate_push) {
1300 basesink->pad_mode = GST_ACTIVATE_NONE;
1303 basesink->pad_mode = GST_ACTIVATE_PUSH;
1306 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1307 g_warning ("Internal GStreamer activation error!!!");
1310 result = gst_base_sink_deactivate (basesink, pad);
1311 basesink->pad_mode = GST_ACTIVATE_NONE;
1315 gst_object_unref (basesink);
1320 /* this won't get called until we implement an activate function */
1322 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1324 gboolean result = FALSE;
1325 GstBaseSink *basesink;
1327 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1330 if (!basesink->can_activate_pull) {
1332 basesink->pad_mode = GST_ACTIVATE_NONE;
1334 GstPad *peer = gst_pad_get_peer (pad);
1336 if (G_UNLIKELY (peer == NULL)) {
1337 g_warning ("Trying to activate pad in pull mode, but no peer");
1339 basesink->pad_mode = GST_ACTIVATE_NONE;
1341 if (gst_pad_activate_pull (peer, TRUE)) {
1342 basesink->have_newsegment = TRUE;
1343 basesink->segment_start = basesink->segment_stop = 0;
1345 /* set the pad mode before starting the task so that it's in the
1346 correct state for the new thread... */
1347 basesink->pad_mode = GST_ACTIVATE_PULL;
1349 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1351 /* but if starting the thread fails, set it back */
1353 basesink->pad_mode = GST_ACTIVATE_NONE;
1355 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1357 basesink->pad_mode = GST_ACTIVATE_NONE;
1359 gst_object_unref (peer);
1363 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1364 g_warning ("Internal GStreamer activation error!!!");
1367 basesink->have_newsegment = FALSE;
1368 result = gst_base_sink_deactivate (basesink, pad);
1369 basesink->pad_mode = GST_ACTIVATE_NONE;
1373 gst_object_unref (basesink);
1379 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1382 GstBaseSink *basesink = GST_BASE_SINK (element);
1386 pad = basesink->sinkpad;
1387 gst_object_ref (pad);
1388 GST_UNLOCK (element);
1390 result = gst_pad_push_event (pad, event);
1392 gst_object_unref (pad);
1398 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1401 gboolean res = FALSE;
1403 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1404 res = gst_pad_query (peer, query);
1405 gst_object_unref (peer);
1411 gst_base_sink_query (GstElement * element, GstQuery * query)
1413 gboolean res = FALSE;
1415 GstBaseSink *basesink = GST_BASE_SINK (element);
1417 switch (GST_QUERY_TYPE (query)) {
1418 case GST_QUERY_POSITION:
1419 res = gst_base_sink_peer_query (basesink, query);
1421 case GST_QUERY_LATENCY:
1423 case GST_QUERY_JITTER:
1425 case GST_QUERY_RATE:
1426 //gst_query_set_rate (query, basesink->segment_rate);
1429 case GST_QUERY_SEEKING:
1430 res = gst_base_sink_peer_query (basesink, query);
1432 case GST_QUERY_SEGMENT:
1434 gst_query_set_segment (query, basesink->segment_rate,
1435 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1436 basesink->segment_time);
1439 case GST_QUERY_CONVERT:
1440 res = gst_base_sink_peer_query (basesink, query);
1442 case GST_QUERY_FORMATS:
1443 res = gst_base_sink_peer_query (basesink, query);
1451 static GstStateChangeReturn
1452 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1454 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1455 GstBaseSink *basesink = GST_BASE_SINK (element);
1456 GstBaseSinkClass *bclass;
1458 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1460 switch (transition) {
1461 case GST_STATE_CHANGE_NULL_TO_READY:
1463 if (!bclass->start (basesink))
1466 case GST_STATE_CHANGE_READY_TO_PAUSED:
1467 /* need to complete preroll before this state change completes, there
1468 * is no data flow in READY so we can safely assume we need to preroll. */
1469 basesink->offset = 0;
1470 GST_PREROLL_LOCK (basesink->sinkpad);
1471 basesink->have_preroll = FALSE;
1472 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1473 basesink->need_preroll = TRUE;
1474 GST_PREROLL_UNLOCK (basesink->sinkpad);
1475 basesink->have_newsegment = FALSE;
1476 basesink->segment_rate = 1.0;
1477 basesink->segment_start = 0;
1478 basesink->segment_stop = 0;
1479 basesink->segment_time = 0;
1480 basesink->segment_accum = 0;
1481 ret = GST_STATE_CHANGE_ASYNC;
1483 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1484 GST_PREROLL_LOCK (basesink->sinkpad);
1485 /* if we have EOS, we should empty the queue now as there will
1486 * be no more data received in the chain function.
1487 * FIXME, this could block the state change function too long when
1488 * we are pushing and syncing the buffers, better start a new
1489 * thread to do this. */
1490 if (basesink->eos) {
1491 gboolean do_eos = !basesink->eos_queued;
1493 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1494 basesink->need_preroll = FALSE;
1496 /* need to post EOS message here if it was not in the preroll queue we
1499 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1500 gst_element_post_message (GST_ELEMENT (basesink),
1501 gst_message_new_eos (GST_OBJECT (basesink)));
1503 } else if (!basesink->have_preroll) {
1504 /* don't need preroll, but do queue a commit_state */
1505 GST_DEBUG_OBJECT (basesink,
1506 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1507 basesink->need_preroll = FALSE;
1508 basesink->playing_async = TRUE;
1509 ret = GST_STATE_CHANGE_ASYNC;
1510 /* we know it's not waiting, no need to signal */
1512 /* don't need the preroll anymore */
1513 basesink->need_preroll = FALSE;
1514 GST_DEBUG_OBJECT (basesink,
1515 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1516 /* now let it play */
1517 GST_PREROLL_SIGNAL (basesink->sinkpad);
1519 GST_PREROLL_UNLOCK (basesink->sinkpad);
1526 GstStateChangeReturn bret;
1528 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1529 if (bret == GST_STATE_CHANGE_FAILURE)
1530 goto activate_failed;
1533 switch (transition) {
1534 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1536 GstBaseSinkClass *bclass;
1538 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1540 GST_PREROLL_LOCK (basesink->sinkpad);
1541 GST_LOCK (basesink);
1542 /* unlock clock wait if any */
1543 if (basesink->clock_id) {
1544 gst_clock_id_unschedule (basesink->clock_id);
1546 GST_UNLOCK (basesink);
1548 basesink->playing_async = FALSE;
1550 /* unlock any subclasses */
1552 bclass->unlock (basesink);
1554 /* if we don't have a preroll buffer and we have not received EOS,
1555 * we need to wait for a preroll */
1556 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1557 basesink->have_preroll, basesink->eos);
1558 if (!basesink->have_preroll && !basesink->eos
1559 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1560 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1561 basesink->need_preroll = TRUE;
1562 ret = GST_STATE_CHANGE_ASYNC;
1564 GST_PREROLL_UNLOCK (basesink->sinkpad);
1567 case GST_STATE_CHANGE_PAUSED_TO_READY:
1569 case GST_STATE_CHANGE_READY_TO_NULL:
1571 if (!bclass->stop (basesink)) {
1572 GST_WARNING ("failed to stop");
1584 GST_DEBUG_OBJECT (basesink, "failed to start");
1585 return GST_STATE_CHANGE_FAILURE;
1589 GST_DEBUG_OBJECT (basesink,
1590 "element failed to change states -- activation problem?");
1591 return GST_STATE_CHANGE_FAILURE;