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);
626 basesink->have_preroll = TRUE;
627 /* we are prerolling */
628 GST_PREROLL_UNLOCK (pad);
630 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
631 * inside the STREAM_LOCK */
632 t = GST_STREAM_UNLOCK_FULL (pad);
633 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
635 GST_WARNING ("STREAM_LOCK should have been locked !!");
636 g_warning ("STREAM_LOCK should have been locked !!");
639 /* now we commit our state */
640 GST_STATE_LOCK (basesink);
641 GST_DEBUG_OBJECT (basesink, "commit state");
642 gst_element_commit_state (GST_ELEMENT (basesink));
643 GST_STATE_UNLOCK (basesink);
645 /* reacquire stream lock, pad could be flushing now */
646 /* FIXME in glib, if t==0, the lock is still taken... hmmm.. bug #317802 */
648 GST_STREAM_LOCK_FULL (pad, t);
650 /* and wait if needed */
651 GST_PREROLL_LOCK (pad);
654 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
658 /* it is possible that the application set the state to PLAYING
659 * now in which case we don't need to block anymore. */
660 if (!basesink->need_preroll)
664 length = basesink->preroll_queued;
666 /* FIXME: a pad probe could have made us lose the buffer, according
667 * to one of the python tests */
669 GST_ERROR_OBJECT (basesink,
670 "preroll_queued dropped from 1 to 0 while committing state change");
672 g_assert (length <= 1);
675 /* see if we need to block now. We cannot block on events, only
676 * on buffers, the reason is that events can be sent from the
677 * application thread and we don't want to block there. */
678 if (length > basesink->preroll_queue_max_len && !have_event) {
679 /* block until the state changes, or we get a flush, or something */
680 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
681 GST_PREROLL_WAIT (pad);
682 GST_DEBUG_OBJECT (basesink, "done preroll");
685 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
689 GST_PREROLL_UNLOCK (pad);
697 GST_DEBUG_OBJECT (basesink, "no preroll needed");
698 /* maybe it was another sink that blocked in preroll, need to check for
700 basesink->have_preroll = FALSE;
701 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
702 GST_PREROLL_UNLOCK (pad);
710 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
712 gst_buffer_unref (buf);
713 GST_PREROLL_UNLOCK (pad);
722 basesink->have_preroll = FALSE;
723 basesink->playing_async = FALSE;
725 /* handle buffer first */
726 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
728 /* unroll locks, commit state, reacquire stream lock */
729 GST_PREROLL_UNLOCK (pad);
730 t = GST_STREAM_UNLOCK_FULL (pad);
731 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
733 GST_WARNING ("STREAM_LOCK should have been locked !!");
734 g_warning ("STREAM_LOCK should have been locked !!");
736 GST_STATE_LOCK (basesink);
737 GST_DEBUG_OBJECT (basesink, "commit state");
738 gst_element_commit_state (GST_ELEMENT (basesink));
739 GST_STATE_UNLOCK (basesink);
741 GST_STREAM_LOCK_FULL (pad, t);
748 gst_base_sink_preroll_queue_flush (basesink, pad);
749 GST_PREROLL_UNLOCK (pad);
750 GST_DEBUG_OBJECT (basesink, "pad is flushing");
751 return GST_FLOW_WRONG_STATE;
757 GST_DEBUG_OBJECT (basesink, "preroll failed");
758 gst_base_sink_preroll_queue_flush (basesink, pad);
759 GST_PREROLL_UNLOCK (pad);
761 /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
762 * inside the STREAM_LOCK */
763 t = GST_STREAM_UNLOCK_FULL (pad);
764 GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
766 GST_WARNING ("STREAM_LOCK should have been locked !!");
767 g_warning ("STREAM_LOCK should have been locked !!");
770 /* now we abort our state */
771 GST_STATE_LOCK (basesink);
772 GST_DEBUG_OBJECT (basesink, "abort state");
773 gst_element_abort_state (GST_ELEMENT (basesink));
774 GST_STATE_UNLOCK (basesink);
776 /* reacquire stream lock, pad could be flushing now */
778 GST_STREAM_LOCK_FULL (pad, t);
780 return GST_FLOW_ERROR;
785 gst_base_sink_event (GstPad * pad, GstEvent * event)
787 GstBaseSink *basesink;
788 gboolean result = TRUE;
789 GstBaseSinkClass *bclass;
791 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
793 bclass = GST_BASE_SINK_GET_CLASS (basesink);
795 GST_DEBUG_OBJECT (basesink, "event %p", event);
797 switch (GST_EVENT_TYPE (event)) {
802 GST_STREAM_LOCK (pad);
803 /* EOS also finishes the preroll */
805 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
806 GST_STREAM_UNLOCK (pad);
809 case GST_EVENT_NEWSEGMENT:
813 GST_STREAM_LOCK (pad);
815 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
816 GST_STREAM_UNLOCK (pad);
819 case GST_EVENT_FLUSH_START:
820 /* make sure we are not blocked on the clock also clear any pending
823 bclass->event (basesink, event);
826 basesink->flushing = TRUE;
827 if (basesink->clock_id) {
828 gst_clock_id_unschedule (basesink->clock_id);
830 GST_UNLOCK (basesink);
832 GST_PREROLL_LOCK (pad);
833 /* we need preroll after the flush */
834 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
835 basesink->need_preroll = TRUE;
836 /* unlock from a possible state change/preroll */
837 gst_base_sink_preroll_queue_flush (basesink, pad);
838 GST_PREROLL_UNLOCK (pad);
840 /* and we need to commit our state again on the next
841 * prerolled buffer */
842 GST_STATE_LOCK (basesink);
843 GST_STREAM_LOCK (pad);
844 gst_element_lost_state (GST_ELEMENT (basesink));
845 GST_STREAM_UNLOCK (pad);
846 GST_STATE_UNLOCK (basesink);
847 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
848 gst_event_unref (event);
850 case GST_EVENT_FLUSH_STOP:
852 bclass->event (basesink, event);
854 /* now we are completely unblocked and the _chain method
856 GST_STREAM_LOCK (pad);
858 basesink->flushing = FALSE;
859 GST_UNLOCK (basesink);
860 /* we need new segment info after the flush. */
861 basesink->segment_start = -1;
862 basesink->segment_stop = -1;
863 basesink->current_start = -1;
864 basesink->current_end = -1;
865 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
866 GST_TIME_ARGS (basesink->segment_accum));
867 basesink->segment_accum = 0;
868 GST_STREAM_UNLOCK (pad);
870 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
871 gst_event_unref (event);
874 gst_event_unref (event);
877 gst_object_unref (basesink);
882 /* default implementation to calculate the start and end
883 * timestamps on a buffer, subclasses can override
886 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
887 GstClockTime * start, GstClockTime * end)
889 GstClockTime timestamp, duration;
891 timestamp = GST_BUFFER_TIMESTAMP (buffer);
892 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
894 /* get duration to calculate end time */
895 duration = GST_BUFFER_DURATION (buffer);
896 if (GST_CLOCK_TIME_IS_VALID (duration)) {
897 *end = timestamp + duration;
903 /* with STREAM_LOCK and LOCK*/
904 static GstClockReturn
905 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
910 /* no need to attempt a clock wait if we are flushing */
911 if (basesink->flushing) {
912 return GST_CLOCK_UNSCHEDULED;
915 /* clock_id should be NULL outside of this function */
916 g_assert (basesink->clock_id == NULL);
917 g_assert (GST_CLOCK_TIME_IS_VALID (time));
919 id = gst_clock_new_single_shot_id (basesink->clock, time);
921 basesink->clock_id = id;
922 /* release the object lock while waiting */
923 GST_UNLOCK (basesink);
925 ret = gst_clock_id_wait (id, NULL);
928 gst_clock_id_unref (id);
929 basesink->clock_id = NULL;
934 /* perform synchronisation on a buffer
936 * 1) check if we have a clock, if not, do nothing
937 * 2) calculate the start and end time of the buffer
938 * 3) create a single shot notification to wait on
939 * the clock, save the entry so we can unlock it
940 * 4) wait on the clock, this blocks
941 * 5) unref the clockid again
943 static GstClockReturn
944 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
946 GstClockReturn result = GST_CLOCK_OK;
947 GstClockTime start, end;
948 GstClockTimeDiff stream_start, stream_end;
949 GstBaseSinkClass *bclass;
950 gboolean start_valid, end_valid;
952 bclass = GST_BASE_SINK_GET_CLASS (basesink);
955 if (bclass->get_times)
956 bclass->get_times (basesink, buffer, &start, &end);
958 start_valid = GST_CLOCK_TIME_IS_VALID (start);
959 end_valid = GST_CLOCK_TIME_IS_VALID (start);
961 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
962 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
964 /* if we don't have a timestamp, we don't sync */
968 /* save last times seen. */
969 basesink->current_start = start;
971 basesink->current_end = end;
973 basesink->current_end = start;
975 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
976 /* check if not outside of the segment range, start is
977 * always valid here. */
978 if (start > basesink->segment_stop)
982 /* bring timestamp to stream time using last segment offset. */
983 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
984 /* check if not outside of the segment range */
985 if (end_valid && end < basesink->segment_start)
988 stream_start = (gint64) start - basesink->segment_start;
989 stream_end = (gint64) end - basesink->segment_start;
991 stream_start = (gint64) start;
992 stream_end = (gint64) end;
995 stream_start += basesink->segment_accum;
997 stream_end += basesink->segment_accum;
999 /* now do clocking */
1000 if (basesink->clock && basesink->sync) {
1001 GstClockTime base_time;
1003 GST_LOCK (basesink);
1005 base_time = GST_ELEMENT (basesink)->base_time;
1007 GST_LOG_OBJECT (basesink,
1008 "waiting for clock, base time %" GST_TIME_FORMAT,
1009 GST_TIME_ARGS (base_time));
1011 /* also save end_time of this buffer so that we can wait
1014 basesink->end_time = stream_end + base_time;
1016 basesink->end_time = GST_CLOCK_TIME_NONE;
1018 result = gst_base_sink_wait (basesink, stream_start + base_time);
1020 GST_UNLOCK (basesink);
1022 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1030 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1031 return GST_CLOCK_UNSCHEDULED;
1038 * 2) render the event
1039 * 3) unref the event
1041 static inline gboolean
1042 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1044 GstBaseSinkClass *bclass;
1047 switch (GST_EVENT_TYPE (event)) {
1049 GST_LOCK (basesink);
1050 if (basesink->clock) {
1051 /* wait for last buffer to finish if we have a valid end time */
1052 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1053 gst_base_sink_wait (basesink, basesink->end_time);
1054 basesink->end_time = GST_CLOCK_TIME_NONE;
1057 GST_UNLOCK (basesink);
1063 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1065 ret = bclass->event (basesink, event);
1069 switch (GST_EVENT_TYPE (event)) {
1071 GST_PREROLL_LOCK (basesink->sinkpad);
1072 /* if we are still EOS, we can post the EOS message */
1073 if (basesink->eos) {
1074 /* ok, now we can post the message */
1075 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1076 gst_element_post_message (GST_ELEMENT (basesink),
1077 gst_message_new_eos (GST_OBJECT (basesink)));
1078 basesink->eos_queued = FALSE;
1080 GST_PREROLL_UNLOCK (basesink->sinkpad);
1086 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1087 gst_event_unref (event);
1094 * 1) first sync on the buffer
1095 * 2) render the buffer
1096 * 3) unref the buffer
1098 static inline GstFlowReturn
1099 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1101 GstFlowReturn ret = GST_FLOW_OK;
1102 GstClockReturn status;
1104 status = gst_base_sink_do_sync (basesink, buf);
1106 case GST_CLOCK_EARLY:
1107 GST_DEBUG_OBJECT (basesink, "late frame !");
1108 /* fallthrough for now */
1111 GstBaseSinkClass *bclass;
1113 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1115 ret = bclass->render (basesink, buf);
1122 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1123 gst_buffer_unref (buf);
1128 static GstFlowReturn
1129 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1131 GstBaseSink *basesink;
1132 GstFlowReturn result;
1134 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1136 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1138 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1139 GST_DEBUG_PAD_NAME (pad));
1141 result = GST_FLOW_UNEXPECTED;
1145 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1148 gst_object_unref (basesink);
1154 gst_base_sink_loop (GstPad * pad)
1156 GstBaseSink *basesink;
1157 GstBuffer *buf = NULL;
1158 GstFlowReturn result;
1160 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1162 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1164 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1165 if (result != GST_FLOW_OK)
1168 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1169 if (result != GST_FLOW_OK)
1172 gst_object_unref (basesink);
1179 gst_base_sink_event (pad, gst_event_new_eos ());
1180 gst_object_unref (basesink);
1181 gst_pad_pause_task (pad);
1187 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1189 gboolean result = FALSE;
1190 GstBaseSinkClass *bclass;
1192 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1194 /* step 1, unblock clock sync (if any) or any other blocking thing */
1195 GST_PREROLL_LOCK (pad);
1196 GST_LOCK (basesink);
1197 if (basesink->clock_id) {
1198 gst_clock_id_unschedule (basesink->clock_id);
1200 GST_UNLOCK (basesink);
1202 /* unlock any subclasses */
1204 bclass->unlock (basesink);
1206 /* flush out the data thread if it's locked in finish_preroll */
1207 GST_DEBUG_OBJECT (basesink,
1208 "flushing out data thread, need preroll to FALSE");
1209 basesink->need_preroll = FALSE;
1210 gst_base_sink_preroll_queue_flush (basesink, pad);
1211 GST_PREROLL_SIGNAL (pad);
1212 GST_PREROLL_UNLOCK (pad);
1214 /* step 2, make sure streaming finishes */
1215 result = gst_pad_stop_task (pad);
1221 gst_base_sink_activate (GstPad * pad)
1223 gboolean result = FALSE;
1224 GstBaseSink *basesink;
1226 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1228 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1230 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1231 && gst_pad_activate_pull (pad, TRUE)) {
1232 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1235 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1236 if (gst_pad_activate_push (pad, TRUE)) {
1237 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1243 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1246 gst_object_unref (basesink);
1252 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1255 GstBaseSink *basesink;
1257 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1260 if (!basesink->can_activate_push) {
1262 basesink->pad_mode = GST_ACTIVATE_NONE;
1265 basesink->pad_mode = GST_ACTIVATE_PUSH;
1268 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1269 g_warning ("Internal GStreamer activation error!!!");
1272 result = gst_base_sink_deactivate (basesink, pad);
1273 basesink->pad_mode = GST_ACTIVATE_NONE;
1277 gst_object_unref (basesink);
1282 /* this won't get called until we implement an activate function */
1284 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1286 gboolean result = FALSE;
1287 GstBaseSink *basesink;
1289 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1292 if (!basesink->can_activate_pull) {
1294 basesink->pad_mode = GST_ACTIVATE_NONE;
1296 GstPad *peer = gst_pad_get_peer (pad);
1298 if (G_UNLIKELY (peer == NULL)) {
1299 g_warning ("Trying to activate pad in pull mode, but no peer");
1301 basesink->pad_mode = GST_ACTIVATE_NONE;
1303 if (gst_pad_activate_pull (peer, TRUE)) {
1304 basesink->have_newsegment = TRUE;
1305 basesink->segment_start = basesink->segment_stop = 0;
1307 /* set the pad mode before starting the task so that it's in the
1308 correct state for the new thread... */
1309 basesink->pad_mode = GST_ACTIVATE_PULL;
1311 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1313 /* but if starting the thread fails, set it back */
1315 basesink->pad_mode = GST_ACTIVATE_NONE;
1317 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1319 basesink->pad_mode = GST_ACTIVATE_NONE;
1321 gst_object_unref (peer);
1325 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1326 g_warning ("Internal GStreamer activation error!!!");
1329 basesink->have_newsegment = FALSE;
1330 result = gst_base_sink_deactivate (basesink, pad);
1331 basesink->pad_mode = GST_ACTIVATE_NONE;
1335 gst_object_unref (basesink);
1341 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1344 GstBaseSink *basesink = GST_BASE_SINK (element);
1348 pad = basesink->sinkpad;
1349 gst_object_ref (pad);
1350 GST_UNLOCK (element);
1352 result = gst_pad_push_event (pad, event);
1354 gst_object_unref (pad);
1360 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1363 gboolean res = FALSE;
1365 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1366 res = gst_pad_query (peer, query);
1367 gst_object_unref (peer);
1373 gst_base_sink_query (GstElement * element, GstQuery * query)
1375 gboolean res = FALSE;
1377 GstBaseSink *basesink = GST_BASE_SINK (element);
1379 switch (GST_QUERY_TYPE (query)) {
1380 case GST_QUERY_POSITION:
1381 res = gst_base_sink_peer_query (basesink, query);
1383 case GST_QUERY_LATENCY:
1385 case GST_QUERY_JITTER:
1387 case GST_QUERY_RATE:
1388 //gst_query_set_rate (query, basesink->segment_rate);
1391 case GST_QUERY_SEEKING:
1392 res = gst_base_sink_peer_query (basesink, query);
1394 case GST_QUERY_SEGMENT:
1396 gst_query_set_segment (query, basesink->segment_rate,
1397 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1398 basesink->segment_base);
1401 case GST_QUERY_CONVERT:
1402 res = gst_base_sink_peer_query (basesink, query);
1404 case GST_QUERY_FORMATS:
1405 res = gst_base_sink_peer_query (basesink, query);
1413 static GstStateChangeReturn
1414 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1416 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1417 GstBaseSink *basesink = GST_BASE_SINK (element);
1418 GstBaseSinkClass *bclass;
1420 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1422 switch (transition) {
1423 case GST_STATE_CHANGE_NULL_TO_READY:
1425 if (!bclass->start (basesink))
1428 case GST_STATE_CHANGE_READY_TO_PAUSED:
1429 /* need to complete preroll before this state change completes, there
1430 * is no data flow in READY so we can safely assume we need to preroll. */
1431 basesink->offset = 0;
1432 GST_PREROLL_LOCK (basesink->sinkpad);
1433 basesink->have_preroll = FALSE;
1434 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1435 basesink->need_preroll = TRUE;
1436 GST_PREROLL_UNLOCK (basesink->sinkpad);
1437 basesink->have_newsegment = FALSE;
1438 basesink->segment_rate = 1.0;
1439 basesink->segment_start = 0;
1440 basesink->segment_stop = 0;
1441 ret = GST_STATE_CHANGE_ASYNC;
1443 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1444 GST_PREROLL_LOCK (basesink->sinkpad);
1445 /* if we have EOS, we should empty the queue now as there will
1446 * be no more data received in the chain function.
1447 * FIXME, this could block the state change function too long when
1448 * we are pushing and syncing the buffers, better start a new
1449 * thread to do this. */
1450 if (basesink->eos) {
1451 gboolean do_eos = !basesink->eos_queued;
1453 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1455 /* need to post EOS message here if it was not in the preroll queue we
1458 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1459 gst_element_post_message (GST_ELEMENT (basesink),
1460 gst_message_new_eos (GST_OBJECT (basesink)));
1462 } else if (!basesink->have_preroll) {
1463 /* don't need preroll, but do queue a commit_state */
1464 GST_DEBUG_OBJECT (basesink,
1465 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1466 basesink->need_preroll = FALSE;
1467 basesink->playing_async = TRUE;
1468 ret = GST_STATE_CHANGE_ASYNC;
1469 /* we know it's not waiting, no need to signal */
1471 /* don't need the preroll anymore */
1472 basesink->need_preroll = FALSE;
1473 GST_DEBUG_OBJECT (basesink,
1474 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1475 /* now let it play */
1476 GST_PREROLL_SIGNAL (basesink->sinkpad);
1478 GST_PREROLL_UNLOCK (basesink->sinkpad);
1485 GstStateChangeReturn bret;
1487 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1488 if (bret == GST_STATE_CHANGE_FAILURE)
1489 goto activate_failed;
1492 switch (transition) {
1493 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1495 GstBaseSinkClass *bclass;
1497 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1499 GST_PREROLL_LOCK (basesink->sinkpad);
1500 GST_LOCK (basesink);
1501 /* unlock clock wait if any */
1502 if (basesink->clock_id) {
1503 gst_clock_id_unschedule (basesink->clock_id);
1505 GST_UNLOCK (basesink);
1507 basesink->playing_async = FALSE;
1509 /* unlock any subclasses */
1511 bclass->unlock (basesink);
1513 /* if we don't have a preroll buffer and we have not received EOS,
1514 * we need to wait for a preroll */
1515 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1516 basesink->have_preroll, basesink->eos);
1517 if (!basesink->have_preroll && !basesink->eos) {
1518 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1519 basesink->need_preroll = TRUE;
1520 ret = GST_STATE_CHANGE_ASYNC;
1522 GST_PREROLL_UNLOCK (basesink->sinkpad);
1525 case GST_STATE_CHANGE_PAUSED_TO_READY:
1527 case GST_STATE_CHANGE_READY_TO_NULL:
1529 if (!bclass->stop (basesink)) {
1530 GST_WARNING ("failed to stop");
1542 GST_DEBUG_OBJECT (basesink, "failed to start");
1543 return GST_STATE_CHANGE_FAILURE;
1547 GST_DEBUG_OBJECT (basesink,
1548 "element failed to change states -- activation problem?");
1549 return GST_STATE_CHANGE_FAILURE;