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:
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 += (duration / ABS (basesink->segment_rate));
528 /* then update the current segment */
529 basesink->segment_rate = rate;
530 basesink->segment_start = segment_start;
531 basesink->segment_stop = segment_stop;
532 basesink->segment_time = segment_time;
534 GST_DEBUG_OBJECT (basesink,
535 "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
536 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
538 GST_TIME_ARGS (basesink->segment_start),
539 GST_TIME_ARGS (basesink->segment_stop),
540 GST_TIME_ARGS (basesink->segment_time),
541 GST_TIME_ARGS (basesink->segment_accum));
548 basesink->events_queued++;
550 GstBuffer *buf = GST_BUFFER (obj);
552 if (!basesink->have_newsegment) {
553 GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
554 ("Received buffer without a new-segment. Cannot sync to clock."),
555 ("Received buffer without a new-segment. Cannot sync to clock."));
556 basesink->have_newsegment = TRUE;
557 /* this means this sink will not be able to sync to the clock */
558 basesink->segment_start = -1;
559 basesink->segment_stop = -1;
562 /* check if the buffer needs to be dropped */
564 GstClockTime start = -1, end = -1;
566 /* we don't use the subclassed method as it may not return
567 * valid values for our purpose here */
568 gst_base_sink_get_times (basesink, buf, &start, &end);
570 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
571 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
572 GST_TIME_ARGS (end));
574 /* need to drop if the timestamp is not between segment_start and
575 * segment_stop. we check if the complete sample is outside of the
576 * range since the sink might be able to clip the sample. */
577 if (GST_CLOCK_TIME_IS_VALID (end) &&
578 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
579 if (end <= basesink->segment_start) {
580 GST_DEBUG_OBJECT (basesink,
581 "buffer end %" GST_TIME_FORMAT " <= segment start %"
582 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
583 GST_TIME_ARGS (basesink->segment_start));
587 if (GST_CLOCK_TIME_IS_VALID (start) &&
588 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
589 if (basesink->segment_stop <= start) {
590 GST_DEBUG_OBJECT (basesink,
591 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
592 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
593 GST_TIME_ARGS (basesink->segment_stop));
598 basesink->preroll_queued++;
599 basesink->buffers_queued++;
601 GST_DEBUG_OBJECT (basesink,
602 "now %d preroll, %d buffers, %d events on queue",
603 basesink->preroll_queued,
604 basesink->buffers_queued, basesink->events_queued);
606 if (basesink->playing_async)
609 /* check if we are prerolling */
610 if (!basesink->need_preroll)
613 /* there is a buffer queued */
614 if (basesink->buffers_queued == 1) {
615 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
617 /* if it's a buffer, we need to call the preroll method */
618 if (GST_IS_BUFFER (obj)) {
619 GstBaseSinkClass *bclass;
622 bclass = GST_BASE_SINK_GET_CLASS (basesink);
625 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
629 length = basesink->preroll_queued;
630 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
636 basesink->have_preroll = TRUE;
637 /* we are prerolling */
638 GST_PREROLL_UNLOCK (pad);
640 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
641 * inside the STREAM_LOCK */
642 t = GST_STREAM_UNLOCK_FULL (pad);
643 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
645 GST_WARNING ("STREAM_LOCK should have been locked !!");
646 g_warning ("STREAM_LOCK should have been locked !!");
649 /* now we commit our state, this will also automatically proceed to
650 * the next pending state. */
652 if ((task = GST_PAD_TASK (pad))) {
653 while (!GST_STATE_TRYLOCK (basesink)) {
654 GST_DEBUG_OBJECT (basesink,
655 "state change happening, checking shutdown");
657 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
662 GST_STATE_LOCK (basesink);
664 GST_DEBUG_OBJECT (basesink, "commit state");
665 gst_element_commit_state (GST_ELEMENT (basesink));
666 GST_STATE_UNLOCK (basesink);
668 /* reacquire stream lock, pad could be flushing now */
669 /* FIXME in glib, if t==0, the lock is still taken... hmmm.. bug #317802 */
671 GST_STREAM_LOCK_FULL (pad, t);
673 /* and wait if needed */
674 GST_PREROLL_LOCK (pad);
677 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
681 /* it is possible that the application set the state to PLAYING
682 * now in which case we don't need to block anymore. */
683 if (!basesink->need_preroll)
687 length = basesink->preroll_queued;
689 /* FIXME: a pad probe could have made us lose the buffer, according
690 * to one of the python tests */
692 GST_ERROR_OBJECT (basesink,
693 "preroll_queued dropped from 1 to 0 while committing state change");
695 g_assert (length <= 1);
698 /* see if we need to block now. We cannot block on events, only
699 * on buffers, the reason is that events can be sent from the
700 * application thread and we don't want to block there. */
701 if (length > basesink->preroll_queue_max_len && !have_event) {
702 /* block until the state changes, or we get a flush, or something */
703 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
704 GST_PREROLL_WAIT (pad);
705 GST_DEBUG_OBJECT (basesink, "done preroll");
708 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
712 GST_PREROLL_UNLOCK (pad);
720 GST_DEBUG_OBJECT (basesink, "no preroll needed");
721 /* maybe it was another sink that blocked in preroll, need to check for
723 basesink->have_preroll = FALSE;
724 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
725 GST_PREROLL_UNLOCK (pad);
733 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
735 gst_buffer_unref (buf);
736 GST_PREROLL_UNLOCK (pad);
745 basesink->have_preroll = FALSE;
746 basesink->playing_async = FALSE;
748 /* handle buffer first */
749 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
751 /* unroll locks, commit state, reacquire stream lock */
752 GST_PREROLL_UNLOCK (pad);
753 t = GST_STREAM_UNLOCK_FULL (pad);
754 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
756 GST_WARNING ("STREAM_LOCK should have been locked !!");
757 g_warning ("STREAM_LOCK should have been locked !!");
759 GST_STATE_LOCK (basesink);
760 GST_DEBUG_OBJECT (basesink, "commit state");
761 gst_element_commit_state (GST_ELEMENT (basesink));
762 GST_STATE_UNLOCK (basesink);
764 GST_STREAM_LOCK_FULL (pad, t);
771 GST_DEBUG_OBJECT (basesink, "task is stopped");
772 return GST_FLOW_WRONG_STATE;
777 gst_base_sink_preroll_queue_flush (basesink, pad);
778 GST_PREROLL_UNLOCK (pad);
779 GST_DEBUG_OBJECT (basesink, "pad is flushing");
780 return GST_FLOW_WRONG_STATE;
786 GST_DEBUG_OBJECT (basesink, "preroll failed");
787 gst_base_sink_preroll_queue_flush (basesink, pad);
788 GST_PREROLL_UNLOCK (pad);
790 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
791 * inside the STREAM_LOCK */
792 t = GST_STREAM_UNLOCK_FULL (pad);
793 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
795 GST_WARNING ("STREAM_LOCK should have been locked !!");
796 g_warning ("STREAM_LOCK should have been locked !!");
799 /* now we abort our state */
800 GST_STATE_LOCK (basesink);
801 GST_DEBUG_OBJECT (basesink, "abort state");
802 gst_element_abort_state (GST_ELEMENT (basesink));
803 GST_STATE_UNLOCK (basesink);
805 /* reacquire stream lock, pad could be flushing now */
807 GST_STREAM_LOCK_FULL (pad, t);
809 return GST_FLOW_ERROR;
814 gst_base_sink_event (GstPad * pad, GstEvent * event)
816 GstBaseSink *basesink;
817 gboolean result = TRUE;
818 GstBaseSinkClass *bclass;
820 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
822 bclass = GST_BASE_SINK_GET_CLASS (basesink);
824 GST_DEBUG_OBJECT (basesink, "event %p", event);
826 switch (GST_EVENT_TYPE (event)) {
831 GST_STREAM_LOCK (pad);
832 /* EOS also finishes the preroll */
834 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
835 GST_STREAM_UNLOCK (pad);
838 case GST_EVENT_NEWSEGMENT:
842 GST_STREAM_LOCK (pad);
844 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
845 GST_STREAM_UNLOCK (pad);
848 case GST_EVENT_FLUSH_START:
849 /* make sure we are not blocked on the clock also clear any pending
852 bclass->event (basesink, event);
855 basesink->flushing = TRUE;
856 if (basesink->clock_id) {
857 gst_clock_id_unschedule (basesink->clock_id);
859 GST_UNLOCK (basesink);
861 GST_PREROLL_LOCK (pad);
862 /* we need preroll after the flush */
863 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
864 basesink->need_preroll = TRUE;
865 /* unlock from a possible state change/preroll */
866 gst_base_sink_preroll_queue_flush (basesink, pad);
867 GST_PREROLL_UNLOCK (pad);
869 /* and we need to commit our state again on the next
870 * prerolled buffer */
871 GST_STATE_LOCK (basesink);
872 GST_STREAM_LOCK (pad);
873 gst_element_lost_state (GST_ELEMENT (basesink));
874 GST_STREAM_UNLOCK (pad);
875 GST_STATE_UNLOCK (basesink);
876 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
877 gst_event_unref (event);
879 case GST_EVENT_FLUSH_STOP:
881 bclass->event (basesink, event);
883 /* now we are completely unblocked and the _chain method
885 GST_STREAM_LOCK (pad);
887 basesink->flushing = FALSE;
888 GST_UNLOCK (basesink);
889 /* we need new segment info after the flush. */
890 basesink->segment_start = -1;
891 basesink->segment_stop = -1;
892 basesink->current_start = -1;
893 basesink->current_end = -1;
894 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
895 GST_TIME_ARGS (basesink->segment_accum));
896 basesink->segment_accum = 0;
897 GST_STREAM_UNLOCK (pad);
899 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
900 gst_event_unref (event);
903 gst_event_unref (event);
906 gst_object_unref (basesink);
911 /* default implementation to calculate the start and end
912 * timestamps on a buffer, subclasses can override
915 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
916 GstClockTime * start, GstClockTime * end)
918 GstClockTime timestamp, duration;
920 timestamp = GST_BUFFER_TIMESTAMP (buffer);
921 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
923 /* get duration to calculate end time */
924 duration = GST_BUFFER_DURATION (buffer);
925 if (GST_CLOCK_TIME_IS_VALID (duration)) {
926 *end = timestamp + duration;
932 /* with STREAM_LOCK and LOCK*/
933 static GstClockReturn
934 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
939 /* no need to attempt a clock wait if we are flushing */
940 if (basesink->flushing) {
941 return GST_CLOCK_UNSCHEDULED;
944 /* clock_id should be NULL outside of this function */
945 g_assert (basesink->clock_id == NULL);
946 g_assert (GST_CLOCK_TIME_IS_VALID (time));
948 id = gst_clock_new_single_shot_id (basesink->clock, time);
950 basesink->clock_id = id;
951 /* release the object lock while waiting */
952 GST_UNLOCK (basesink);
954 ret = gst_clock_id_wait (id, NULL);
957 gst_clock_id_unref (id);
958 basesink->clock_id = NULL;
963 /* perform synchronisation on a buffer
965 * 1) check if we have a clock, if not, do nothing
966 * 2) calculate the start and end time of the buffer
967 * 3) create a single shot notification to wait on
968 * the clock, save the entry so we can unlock it
969 * 4) wait on the clock, this blocks
970 * 5) unref the clockid again
972 static GstClockReturn
973 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
975 GstClockReturn result = GST_CLOCK_OK;
976 GstClockTime start, end;
977 GstClockTimeDiff stream_start, stream_end;
978 GstBaseSinkClass *bclass;
979 gboolean start_valid, end_valid;
981 bclass = GST_BASE_SINK_GET_CLASS (basesink);
984 if (bclass->get_times)
985 bclass->get_times (basesink, buffer, &start, &end);
987 start_valid = GST_CLOCK_TIME_IS_VALID (start);
988 end_valid = GST_CLOCK_TIME_IS_VALID (start);
990 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
991 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
993 /* if we don't have a timestamp, we don't sync */
997 /* save last times seen. */
998 basesink->current_start = start;
1000 basesink->current_end = end;
1002 basesink->current_end = start;
1004 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
1005 /* check if not outside of the segment range, start is
1006 * always valid here. */
1007 if (start > basesink->segment_stop)
1008 goto out_of_segment;
1011 /* bring timestamp to stream time using last segment offset. */
1012 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1013 /* check if not outside of the segment range */
1014 if (end_valid && end < basesink->segment_start)
1015 goto out_of_segment;
1017 stream_start = (gint64) start - basesink->segment_start;
1018 stream_end = (gint64) end - basesink->segment_start;
1020 stream_start = (gint64) start;
1021 stream_end = (gint64) end;
1024 /* correct for rate */
1025 if (basesink->segment_rate != 0.0) {
1026 stream_start /= ABS (basesink->segment_rate);
1028 stream_end /= ABS (basesink->segment_rate);
1031 stream_start += basesink->segment_accum;
1033 stream_end += basesink->segment_accum;
1035 /* now do clocking */
1036 if (basesink->clock && basesink->sync) {
1037 GstClockTime base_time;
1039 GST_LOCK (basesink);
1041 base_time = GST_ELEMENT (basesink)->base_time;
1043 GST_LOG_OBJECT (basesink,
1044 "waiting for clock, base time %" GST_TIME_FORMAT,
1045 GST_TIME_ARGS (base_time));
1047 /* also save end_time of this buffer so that we can wait
1050 basesink->end_time = stream_end + base_time;
1052 basesink->end_time = GST_CLOCK_TIME_NONE;
1054 result = gst_base_sink_wait (basesink, stream_start + base_time);
1056 GST_UNLOCK (basesink);
1058 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1066 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1067 return GST_CLOCK_UNSCHEDULED;
1074 * 2) render the event
1075 * 3) unref the event
1077 static inline gboolean
1078 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1080 GstBaseSinkClass *bclass;
1083 switch (GST_EVENT_TYPE (event)) {
1085 GST_LOCK (basesink);
1086 if (basesink->clock) {
1087 /* wait for last buffer to finish if we have a valid end time */
1088 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1089 gst_base_sink_wait (basesink, basesink->end_time);
1090 basesink->end_time = GST_CLOCK_TIME_NONE;
1093 GST_UNLOCK (basesink);
1099 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1101 ret = bclass->event (basesink, event);
1105 switch (GST_EVENT_TYPE (event)) {
1107 GST_PREROLL_LOCK (basesink->sinkpad);
1108 /* if we are still EOS, we can post the EOS message */
1109 if (basesink->eos) {
1110 /* ok, now we can post the message */
1111 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1112 gst_element_post_message (GST_ELEMENT (basesink),
1113 gst_message_new_eos (GST_OBJECT (basesink)));
1114 basesink->eos_queued = FALSE;
1116 GST_PREROLL_UNLOCK (basesink->sinkpad);
1122 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1123 gst_event_unref (event);
1130 * 1) first sync on the buffer
1131 * 2) render the buffer
1132 * 3) unref the buffer
1134 static inline GstFlowReturn
1135 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1137 GstFlowReturn ret = GST_FLOW_OK;
1138 GstClockReturn status;
1140 status = gst_base_sink_do_sync (basesink, buf);
1142 case GST_CLOCK_EARLY:
1143 GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1144 /* fallthrough for now */
1147 GstBaseSinkClass *bclass;
1149 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1151 ret = bclass->render (basesink, buf);
1155 GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1159 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1160 gst_buffer_unref (buf);
1165 static GstFlowReturn
1166 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1168 GstBaseSink *basesink;
1169 GstFlowReturn result;
1171 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1173 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1175 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1176 GST_DEBUG_PAD_NAME (pad));
1178 result = GST_FLOW_UNEXPECTED;
1182 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1185 gst_object_unref (basesink);
1191 gst_base_sink_loop (GstPad * pad)
1193 GstBaseSink *basesink;
1194 GstBuffer *buf = NULL;
1195 GstFlowReturn result;
1197 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1199 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1201 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1202 if (result != GST_FLOW_OK)
1205 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1206 if (result != GST_FLOW_OK)
1209 gst_object_unref (basesink);
1216 gst_base_sink_event (pad, gst_event_new_eos ());
1217 gst_object_unref (basesink);
1218 gst_pad_pause_task (pad);
1224 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1226 gboolean result = FALSE;
1227 GstBaseSinkClass *bclass;
1229 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1231 /* step 1, unblock clock sync (if any) or any other blocking thing */
1232 GST_PREROLL_LOCK (pad);
1233 GST_LOCK (basesink);
1234 if (basesink->clock_id) {
1235 gst_clock_id_unschedule (basesink->clock_id);
1237 GST_UNLOCK (basesink);
1239 /* unlock any subclasses */
1241 bclass->unlock (basesink);
1243 /* flush out the data thread if it's locked in finish_preroll */
1244 GST_DEBUG_OBJECT (basesink,
1245 "flushing out data thread, need preroll to FALSE");
1246 basesink->need_preroll = FALSE;
1247 gst_base_sink_preroll_queue_flush (basesink, pad);
1248 GST_PREROLL_SIGNAL (pad);
1249 GST_PREROLL_UNLOCK (pad);
1251 /* step 2, make sure streaming finishes */
1252 result = gst_pad_stop_task (pad);
1258 gst_base_sink_activate (GstPad * pad)
1260 gboolean result = FALSE;
1261 GstBaseSink *basesink;
1263 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1265 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1267 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1268 && gst_pad_activate_pull (pad, TRUE)) {
1269 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1272 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1273 if (gst_pad_activate_push (pad, TRUE)) {
1274 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1280 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1283 gst_object_unref (basesink);
1289 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1292 GstBaseSink *basesink;
1294 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1297 if (!basesink->can_activate_push) {
1299 basesink->pad_mode = GST_ACTIVATE_NONE;
1302 basesink->pad_mode = GST_ACTIVATE_PUSH;
1305 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1306 g_warning ("Internal GStreamer activation error!!!");
1309 result = gst_base_sink_deactivate (basesink, pad);
1310 basesink->pad_mode = GST_ACTIVATE_NONE;
1314 gst_object_unref (basesink);
1319 /* this won't get called until we implement an activate function */
1321 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1323 gboolean result = FALSE;
1324 GstBaseSink *basesink;
1326 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1329 if (!basesink->can_activate_pull) {
1331 basesink->pad_mode = GST_ACTIVATE_NONE;
1333 GstPad *peer = gst_pad_get_peer (pad);
1335 if (G_UNLIKELY (peer == NULL)) {
1336 g_warning ("Trying to activate pad in pull mode, but no peer");
1338 basesink->pad_mode = GST_ACTIVATE_NONE;
1340 if (gst_pad_activate_pull (peer, TRUE)) {
1341 basesink->have_newsegment = TRUE;
1342 basesink->segment_start = basesink->segment_stop = 0;
1344 /* set the pad mode before starting the task so that it's in the
1345 correct state for the new thread... */
1346 basesink->pad_mode = GST_ACTIVATE_PULL;
1348 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1350 /* but if starting the thread fails, set it back */
1352 basesink->pad_mode = GST_ACTIVATE_NONE;
1354 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1356 basesink->pad_mode = GST_ACTIVATE_NONE;
1358 gst_object_unref (peer);
1362 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1363 g_warning ("Internal GStreamer activation error!!!");
1366 basesink->have_newsegment = FALSE;
1367 result = gst_base_sink_deactivate (basesink, pad);
1368 basesink->pad_mode = GST_ACTIVATE_NONE;
1372 gst_object_unref (basesink);
1378 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1381 GstBaseSink *basesink = GST_BASE_SINK (element);
1385 pad = basesink->sinkpad;
1386 gst_object_ref (pad);
1387 GST_UNLOCK (element);
1389 result = gst_pad_push_event (pad, event);
1391 gst_object_unref (pad);
1397 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1400 gboolean res = FALSE;
1402 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1403 res = gst_pad_query (peer, query);
1404 gst_object_unref (peer);
1410 gst_base_sink_query (GstElement * element, GstQuery * query)
1412 gboolean res = FALSE;
1414 GstBaseSink *basesink = GST_BASE_SINK (element);
1416 switch (GST_QUERY_TYPE (query)) {
1417 case GST_QUERY_POSITION:
1418 res = gst_base_sink_peer_query (basesink, query);
1420 case GST_QUERY_LATENCY:
1422 case GST_QUERY_JITTER:
1424 case GST_QUERY_RATE:
1425 //gst_query_set_rate (query, basesink->segment_rate);
1428 case GST_QUERY_SEEKING:
1429 res = gst_base_sink_peer_query (basesink, query);
1431 case GST_QUERY_SEGMENT:
1433 gst_query_set_segment (query, basesink->segment_rate,
1434 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1435 basesink->segment_time);
1438 case GST_QUERY_CONVERT:
1439 res = gst_base_sink_peer_query (basesink, query);
1441 case GST_QUERY_FORMATS:
1442 res = gst_base_sink_peer_query (basesink, query);
1450 static GstStateChangeReturn
1451 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1453 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1454 GstBaseSink *basesink = GST_BASE_SINK (element);
1455 GstBaseSinkClass *bclass;
1457 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1459 switch (transition) {
1460 case GST_STATE_CHANGE_NULL_TO_READY:
1462 if (!bclass->start (basesink))
1465 case GST_STATE_CHANGE_READY_TO_PAUSED:
1466 /* need to complete preroll before this state change completes, there
1467 * is no data flow in READY so we can safely assume we need to preroll. */
1468 basesink->offset = 0;
1469 GST_PREROLL_LOCK (basesink->sinkpad);
1470 basesink->have_preroll = FALSE;
1471 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1472 basesink->need_preroll = TRUE;
1473 GST_PREROLL_UNLOCK (basesink->sinkpad);
1474 basesink->have_newsegment = FALSE;
1475 basesink->segment_rate = 1.0;
1476 basesink->segment_start = 0;
1477 basesink->segment_stop = 0;
1478 basesink->segment_time = 0;
1479 basesink->segment_accum = 0;
1480 ret = GST_STATE_CHANGE_ASYNC;
1482 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1483 GST_PREROLL_LOCK (basesink->sinkpad);
1484 /* if we have EOS, we should empty the queue now as there will
1485 * be no more data received in the chain function.
1486 * FIXME, this could block the state change function too long when
1487 * we are pushing and syncing the buffers, better start a new
1488 * thread to do this. */
1489 if (basesink->eos) {
1490 gboolean do_eos = !basesink->eos_queued;
1492 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1493 basesink->need_preroll = FALSE;
1495 /* need to post EOS message here if it was not in the preroll queue we
1498 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1499 gst_element_post_message (GST_ELEMENT (basesink),
1500 gst_message_new_eos (GST_OBJECT (basesink)));
1502 } else if (!basesink->have_preroll) {
1503 /* don't need preroll, but do queue a commit_state */
1504 GST_DEBUG_OBJECT (basesink,
1505 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1506 basesink->need_preroll = FALSE;
1507 basesink->playing_async = TRUE;
1508 ret = GST_STATE_CHANGE_ASYNC;
1509 /* we know it's not waiting, no need to signal */
1511 /* don't need the preroll anymore */
1512 basesink->need_preroll = FALSE;
1513 GST_DEBUG_OBJECT (basesink,
1514 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1515 /* now let it play */
1516 GST_PREROLL_SIGNAL (basesink->sinkpad);
1518 GST_PREROLL_UNLOCK (basesink->sinkpad);
1525 GstStateChangeReturn bret;
1527 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1528 if (bret == GST_STATE_CHANGE_FAILURE)
1529 goto activate_failed;
1532 switch (transition) {
1533 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1535 GstBaseSinkClass *bclass;
1537 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1539 GST_PREROLL_LOCK (basesink->sinkpad);
1540 GST_LOCK (basesink);
1541 /* unlock clock wait if any */
1542 if (basesink->clock_id) {
1543 gst_clock_id_unschedule (basesink->clock_id);
1545 GST_UNLOCK (basesink);
1547 basesink->playing_async = FALSE;
1549 /* unlock any subclasses */
1551 bclass->unlock (basesink);
1553 /* if we don't have a preroll buffer and we have not received EOS,
1554 * we need to wait for a preroll */
1555 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1556 basesink->have_preroll, basesink->eos);
1557 if (!basesink->have_preroll && !basesink->eos
1558 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1559 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1560 basesink->need_preroll = TRUE;
1561 ret = GST_STATE_CHANGE_ASYNC;
1563 GST_PREROLL_UNLOCK (basesink->sinkpad);
1566 case GST_STATE_CHANGE_PAUSED_TO_READY:
1568 case GST_STATE_CHANGE_READY_TO_NULL:
1570 if (!bclass->stop (basesink)) {
1571 GST_WARNING ("failed to stop");
1583 GST_DEBUG_OBJECT (basesink, "failed to start");
1584 return GST_STATE_CHANGE_FAILURE;
1588 GST_DEBUG_OBJECT (basesink,
1589 "element failed to change states -- activation problem?");
1590 return GST_STATE_CHANGE_FAILURE;