2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSource
27 * GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. GstBaseSink handles many details for
30 * you, for example preroll, clock synchronization, state changes, activation in
31 * push or pull mode, and queries. In most cases, when writing sink elements,
32 * there is no need to implement class methods from #GstElement or to set
33 * functions on pads, because the GstBaseSink infrastructure is sufficient.
35 * There is only support in GstBaseSink for one sink pad, which should be named
36 * "sink". A sink implementation (subclass of GstBaseSink) should install a pad
37 * template in its base_init function, like so:
40 * my_element_base_init (gpointer g_class)
42 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
44 * // sinktemplate should be a #GstStaticPadTemplate with direction
45 * // #GST_PAD_SINK and name "sink"
46 * gst_element_class_add_pad_template (gstelement_class,
47 * gst_static_pad_template_get (&sinktemplate));
48 * // see #GstElementDetails
49 * gst_element_class_set_details (gstelement_class, &details);
53 * The one method which all subclasses of GstBaseSink must implement is
54 * GstBaseSink::render. This method will be called...
58 * event(): mostly useful for file-like sinks (seeking or flushing)
60 * get_caps/set_caps/buffer_alloc
62 * start/stop for resource allocation
64 * unlock if you block on an fd, for example
66 * get_times i'm sure is for something :P
68 * provide example of textsink
70 * admonishment not to try to implement your own sink with prerolling...
72 * extending via subclassing, setting pad functions, gstelement vmethods.
79 #include "gstbasesink.h"
80 #include <gst/gstmarshal.h>
81 #include <gst/gst-i18n-lib.h>
83 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
84 #define GST_CAT_DEFAULT gst_base_sink_debug
86 /* BaseSink signals and properties */
94 #define DEFAULT_SIZE 1024
95 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
96 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
98 #define DEFAULT_SYNC TRUE
103 PROP_PREROLL_QUEUE_LEN,
107 static GstElementClass *parent_class = NULL;
109 static void gst_base_sink_base_init (gpointer g_class);
110 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
111 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
112 static void gst_base_sink_finalize (GObject * object);
115 gst_base_sink_get_type (void)
117 static GType base_sink_type = 0;
119 if (!base_sink_type) {
120 static const GTypeInfo base_sink_info = {
121 sizeof (GstBaseSinkClass),
122 (GBaseInitFunc) gst_base_sink_base_init,
124 (GClassInitFunc) gst_base_sink_class_init,
127 sizeof (GstBaseSink),
129 (GInstanceInitFunc) gst_base_sink_init,
132 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
133 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
135 return base_sink_type;
138 static void gst_base_sink_set_clock (GstElement * element, GstClock * clock);
140 static void gst_base_sink_set_property (GObject * object, guint prop_id,
141 const GValue * value, GParamSpec * pspec);
142 static void gst_base_sink_get_property (GObject * object, guint prop_id,
143 GValue * value, GParamSpec * pspec);
145 static gboolean gst_base_sink_send_event (GstElement * element,
147 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
149 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
150 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
151 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
152 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
153 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
154 GstClockTime * start, GstClockTime * end);
156 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
157 GstStateChange transition);
159 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
160 static void gst_base_sink_loop (GstPad * pad);
161 static gboolean gst_base_sink_activate (GstPad * pad);
162 static gboolean gst_base_sink_activate_push (GstPad * pad, gboolean active);
163 static gboolean gst_base_sink_activate_pull (GstPad * pad, gboolean active);
164 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
165 static inline GstFlowReturn gst_base_sink_handle_buffer (GstBaseSink * basesink,
167 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
171 gst_base_sink_base_init (gpointer g_class)
173 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
178 gst_base_sink_class_init (GstBaseSinkClass * klass)
180 GObjectClass *gobject_class;
181 GstElementClass *gstelement_class;
183 gobject_class = (GObjectClass *) klass;
184 gstelement_class = (GstElementClass *) klass;
186 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
188 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
189 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
190 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
192 /* FIXME, this next value should be configured using an event from the
193 * upstream element */
194 g_object_class_install_property (G_OBJECT_CLASS (klass),
195 PROP_PREROLL_QUEUE_LEN,
196 g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
197 "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
198 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
199 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
200 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
203 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_base_sink_set_clock);
204 gstelement_class->change_state =
205 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
206 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
207 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
209 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
210 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
211 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
212 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
216 gst_base_sink_pad_getcaps (GstPad * pad)
218 GstBaseSinkClass *bclass;
220 GstCaps *caps = NULL;
222 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
223 bclass = GST_BASE_SINK_GET_CLASS (bsink);
224 if (bclass->get_caps)
225 caps = bclass->get_caps (bsink);
228 GstPadTemplate *pad_template;
231 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
232 if (pad_template != NULL) {
233 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
236 gst_object_unref (bsink);
242 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
244 GstBaseSinkClass *bclass;
246 gboolean res = FALSE;
248 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
249 bclass = GST_BASE_SINK_GET_CLASS (bsink);
251 if (bclass->set_caps)
252 res = bclass->set_caps (bsink, caps);
254 gst_object_unref (bsink);
260 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
261 GstCaps * caps, GstBuffer ** buf)
263 GstBaseSinkClass *bclass;
265 GstFlowReturn result = GST_FLOW_OK;
267 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
268 bclass = GST_BASE_SINK_GET_CLASS (bsink);
270 if (bclass->buffer_alloc)
271 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
273 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
275 gst_object_unref (bsink);
281 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
283 GstPadTemplate *pad_template;
286 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
287 g_return_if_fail (pad_template != NULL);
289 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
291 gst_pad_set_getcaps_function (basesink->sinkpad,
292 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
293 gst_pad_set_setcaps_function (basesink->sinkpad,
294 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
295 gst_pad_set_bufferalloc_function (basesink->sinkpad,
296 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
297 gst_pad_set_activate_function (basesink->sinkpad,
298 GST_DEBUG_FUNCPTR (gst_base_sink_activate));
299 gst_pad_set_activatepush_function (basesink->sinkpad,
300 GST_DEBUG_FUNCPTR (gst_base_sink_activate_push));
301 gst_pad_set_activatepull_function (basesink->sinkpad,
302 GST_DEBUG_FUNCPTR (gst_base_sink_activate_pull));
303 gst_pad_set_event_function (basesink->sinkpad,
304 GST_DEBUG_FUNCPTR (gst_base_sink_event));
305 gst_pad_set_chain_function (basesink->sinkpad,
306 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
307 gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
309 basesink->pad_mode = GST_ACTIVATE_NONE;
310 GST_PAD_TASK (basesink->sinkpad) = NULL;
311 basesink->preroll_queue = g_queue_new ();
313 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
314 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
316 basesink->sync = DEFAULT_SYNC;
318 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
322 gst_base_sink_finalize (GObject * object)
324 GstBaseSink *basesink;
326 basesink = GST_BASE_SINK (object);
328 g_queue_free (basesink->preroll_queue);
330 G_OBJECT_CLASS (parent_class)->finalize (object);
334 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
338 sink = GST_BASE_SINK (element);
344 gst_base_sink_set_property (GObject * object, guint prop_id,
345 const GValue * value, GParamSpec * pspec)
347 GstBaseSink *sink = GST_BASE_SINK (object);
350 case PROP_PREROLL_QUEUE_LEN:
351 /* preroll lock necessary to serialize with finish_preroll */
352 GST_PREROLL_LOCK (sink->sinkpad);
353 sink->preroll_queue_max_len = g_value_get_uint (value);
354 GST_PREROLL_UNLOCK (sink->sinkpad);
357 sink->sync = g_value_get_boolean (value);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
369 GstBaseSink *sink = GST_BASE_SINK (object);
373 case PROP_PREROLL_QUEUE_LEN:
374 g_value_set_uint (value, sink->preroll_queue_max_len);
377 g_value_set_boolean (value, sink->sync);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gst_base_sink_get_caps (GstBaseSink * sink)
393 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
399 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
400 GstCaps * caps, GstBuffer ** buf)
406 /* with PREROLL_LOCK */
408 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
411 GQueue *q = basesink->preroll_queue;
417 GST_DEBUG_OBJECT (basesink, "emptying queue");
418 while ((obj = g_queue_pop_head (q))) {
421 is_buffer = GST_IS_BUFFER (obj);
422 if (G_LIKELY (is_buffer)) {
423 basesink->preroll_queued--;
424 basesink->buffers_queued--;
426 switch (GST_EVENT_TYPE (obj)) {
428 basesink->preroll_queued--;
433 basesink->events_queued--;
435 /* we release the preroll lock while pushing so that we
436 * can still flush it while blocking on the clock or
437 * inside the element. */
438 GST_PREROLL_UNLOCK (pad);
440 if (G_LIKELY (is_buffer)) {
441 GST_DEBUG_OBJECT (basesink, "popped buffer %p", obj);
442 ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER_CAST (obj));
444 GST_DEBUG_OBJECT (basesink, "popped event %p", obj);
445 gst_base_sink_handle_event (basesink, GST_EVENT_CAST (obj));
449 GST_PREROLL_LOCK (pad);
451 GST_DEBUG_OBJECT (basesink, "queue empty");
456 /* with PREROLL_LOCK */
458 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
461 GQueue *q = basesink->preroll_queue;
463 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
465 while ((obj = g_queue_pop_head (q))) {
466 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
467 gst_mini_object_unref (obj);
470 /* we can't have EOS anymore now */
471 basesink->eos = FALSE;
472 basesink->eos_queued = FALSE;
473 basesink->preroll_queued = 0;
474 basesink->buffers_queued = 0;
475 basesink->events_queued = 0;
476 basesink->have_preroll = FALSE;
477 /* and signal any waiters now */
478 GST_PREROLL_SIGNAL (pad);
481 /* with PREROLL_LOCK */
483 gst_base_sink_commit_state (GstBaseSink * basesink)
485 /* commit state and proceed to next pending state */
487 GstState current, next, pending, post_pending;
489 gboolean post_paused = FALSE;
490 gboolean post_playing = FALSE;
493 current = GST_STATE (basesink);
494 next = GST_STATE_NEXT (basesink);
495 pending = GST_STATE_PENDING (basesink);
496 post_pending = pending;
499 case GST_STATE_PLAYING:
500 basesink->need_preroll = FALSE;
502 /* post PAUSED too when we were READY */
503 if (current == GST_STATE_READY) {
507 case GST_STATE_PAUSED:
508 basesink->need_preroll = TRUE;
510 post_pending = GST_STATE_VOID_PENDING;
512 case GST_STATE_READY:
518 if (pending != GST_STATE_VOID_PENDING) {
519 GST_STATE (basesink) = pending;
520 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
521 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
522 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
524 GST_UNLOCK (basesink);
527 message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
528 current, next, post_pending);
529 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
532 message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
533 next, pending, GST_STATE_VOID_PENDING);
534 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
537 if (post_paused || post_playing) {
538 gst_element_post_message (GST_ELEMENT_CAST (basesink),
539 gst_message_new_state_dirty (GST_OBJECT_CAST (basesink)));
542 GST_STATE_BROADCAST (basesink);
548 /* app is going to READY */
549 GST_UNLOCK (basesink);
554 /* with STREAM_LOCK */
556 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
562 GST_PREROLL_LOCK (pad);
563 /* push object on the queue */
564 GST_DEBUG_OBJECT (basesink, "push %p on preroll_queue", obj);
565 g_queue_push_tail (basesink->preroll_queue, obj);
567 have_event = GST_IS_EVENT (obj);
569 GstEvent *event = GST_EVENT (obj);
571 switch (GST_EVENT_TYPE (obj)) {
573 basesink->preroll_queued++;
574 basesink->eos = TRUE;
575 basesink->eos_queued = TRUE;
577 case GST_EVENT_NEWSEGMENT:
582 gint64 segment_start;
585 GstClockTime duration;
588 /* the newsegment event is needed to bring the buffer timestamps to the
589 * stream time and to drop samples outside of the playback segment. */
590 gst_event_parse_newsegment (event, &update, &rate, &format,
591 &segment_start, &segment_stop, &segment_time);
593 basesink->have_newsegment = TRUE;
595 /* any other format with 0 also gives time 0, the other values are
596 * invalid as time though. */
597 if (format != GST_FORMAT_TIME && segment_start == 0) {
598 GST_DEBUG_OBJECT (basesink,
599 "non-time newsegment with start 0, coaxing into FORMAT_TIME");
600 format = GST_FORMAT_TIME;
605 if (format != GST_FORMAT_TIME) {
606 GST_DEBUG_OBJECT (basesink,
607 "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
608 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT,
609 format, segment_start, segment_stop, segment_time);
611 /* this means this sink will not be able to clip or drop samples
612 * and timestamps have to start from 0. */
613 basesink->segment_start = -1;
614 basesink->segment_stop = -1;
615 basesink->segment_time = -1;
616 goto done_newsegment;
618 /* check if we really have a new segment or the previous one is
621 /* the new segment has to be aligned with the old segment.
622 * We first update the accumulated time of the previous
623 * segment. the accumulated time is used when syncing to the
624 * clock. A flush event sets the accumulated time back to 0
626 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
627 duration = basesink->segment_stop - basesink->segment_start;
628 } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
629 /* else use last seen timestamp as segment stop */
630 duration = basesink->current_end - basesink->segment_start;
635 duration = segment_start - basesink->segment_start;
638 /* use previous rate to calculate duration */
639 basesink->segment_accum += gst_gdouble_to_guint64 (
640 (gst_guint64_to_gdouble (duration) / ABS (basesink->segment_rate)));
641 /* then update the current segment */
642 basesink->segment_rate = rate;
643 basesink->segment_start = segment_start;
644 basesink->segment_stop = segment_stop;
645 basesink->segment_time = segment_time;
647 GST_DEBUG_OBJECT (basesink,
648 "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
649 GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
651 GST_TIME_ARGS (basesink->segment_start),
652 GST_TIME_ARGS (basesink->segment_stop),
653 GST_TIME_ARGS (basesink->segment_time),
654 GST_TIME_ARGS (basesink->segment_accum));
661 basesink->events_queued++;
663 GstBuffer *buf = GST_BUFFER (obj);
665 if (!basesink->have_newsegment) {
666 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
667 (_("Internal data flow problem.")),
668 ("Received buffer without a new-segment. Cannot sync to clock."));
669 basesink->have_newsegment = TRUE;
670 /* this means this sink will not be able to sync to the clock */
671 basesink->segment_start = -1;
672 basesink->segment_stop = -1;
675 /* check if the buffer needs to be dropped */
677 GstClockTime start = -1, end = -1;
679 /* we don't use the subclassed method as it may not return
680 * valid values for our purpose here */
681 gst_base_sink_get_times (basesink, buf, &start, &end);
683 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
684 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
685 GST_TIME_ARGS (end));
687 /* need to drop if the timestamp is not between segment_start and
688 * segment_stop. we check if the complete sample is outside of the
689 * range since the sink might be able to clip the sample. */
690 if (GST_CLOCK_TIME_IS_VALID (end) &&
691 GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
692 if (end <= basesink->segment_start) {
693 GST_DEBUG_OBJECT (basesink,
694 "buffer end %" GST_TIME_FORMAT " <= segment start %"
695 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
696 GST_TIME_ARGS (basesink->segment_start));
700 if (GST_CLOCK_TIME_IS_VALID (start) &&
701 GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
702 if (basesink->segment_stop <= start) {
703 GST_DEBUG_OBJECT (basesink,
704 "buffer start %" GST_TIME_FORMAT " >= segment stop %"
705 GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
706 GST_TIME_ARGS (basesink->segment_stop));
711 basesink->preroll_queued++;
712 basesink->buffers_queued++;
715 GST_DEBUG_OBJECT (basesink,
716 "now %d preroll, %d buffers, %d events on queue",
717 basesink->preroll_queued,
718 basesink->buffers_queued, basesink->events_queued);
720 /* check if we are prerolling */
721 if (!basesink->need_preroll)
724 /* there is a buffer queued */
725 if (basesink->buffers_queued == 1) {
726 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
728 /* if it's a buffer, we need to call the preroll method */
729 if (GST_IS_BUFFER (obj)) {
730 GstBaseSinkClass *bclass;
732 GstBuffer *buf = GST_BUFFER (obj);
734 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
735 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
737 bclass = GST_BASE_SINK_GET_CLASS (basesink);
739 if ((pres = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
743 length = basesink->preroll_queued;
744 GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
748 basesink->have_preroll = TRUE;
751 if (!gst_base_sink_commit_state (basesink))
755 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
759 /* it is possible that commiting the state made us go to PLAYING
760 * now in which case we don't need to block anymore. */
761 if (!basesink->need_preroll)
764 length = basesink->preroll_queued;
766 /* FIXME: a pad probe could have made us lose the buffer, according
767 * to one of the python tests */
769 GST_ERROR_OBJECT (basesink,
770 "preroll_queued dropped from 1 to 0 while committing state change");
772 g_assert (length <= 1);
775 /* see if we need to block now. We cannot block on events, only
776 * on buffers, the reason is that events can be sent from the
777 * application thread and we don't want to block there. */
778 if (length > basesink->preroll_queue_max_len && !have_event) {
779 /* block until the state changes, or we get a flush, or something */
780 GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
781 GST_PREROLL_WAIT (pad);
782 GST_DEBUG_OBJECT (basesink, "done preroll");
784 if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
788 GST_PREROLL_UNLOCK (pad);
796 GST_DEBUG_OBJECT (basesink, "no preroll needed");
797 /* maybe it was another sink that blocked in preroll, need to check for
799 basesink->have_preroll = FALSE;
800 ret = gst_base_sink_preroll_queue_empty (basesink, pad);
801 GST_PREROLL_UNLOCK (pad);
809 buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
811 gst_buffer_unref (buf);
812 GST_PREROLL_UNLOCK (pad);
819 gst_base_sink_preroll_queue_flush (basesink, pad);
820 GST_PREROLL_UNLOCK (pad);
821 GST_DEBUG_OBJECT (basesink, "pad is flushing");
823 return GST_FLOW_WRONG_STATE;
827 GST_PREROLL_UNLOCK (pad);
828 GST_DEBUG_OBJECT (basesink, "stopping");
830 return GST_FLOW_WRONG_STATE;
834 GST_DEBUG_OBJECT (basesink, "preroll failed");
835 gst_base_sink_preroll_queue_flush (basesink, pad);
837 GST_DEBUG_OBJECT (basesink, "abort state");
838 gst_element_abort_state (GST_ELEMENT (basesink));
840 return GST_FLOW_ERROR;
845 gst_base_sink_event (GstPad * pad, GstEvent * event)
847 GstBaseSink *basesink;
848 gboolean result = TRUE;
849 GstBaseSinkClass *bclass;
851 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
853 bclass = GST_BASE_SINK_GET_CLASS (basesink);
855 GST_DEBUG_OBJECT (basesink, "event %p", event);
857 switch (GST_EVENT_TYPE (event)) {
862 GST_STREAM_LOCK (pad);
863 /* EOS also finishes the preroll */
865 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
866 GST_STREAM_UNLOCK (pad);
869 case GST_EVENT_NEWSEGMENT:
873 GST_STREAM_LOCK (pad);
875 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
876 GST_STREAM_UNLOCK (pad);
879 case GST_EVENT_FLUSH_START:
880 /* make sure we are not blocked on the clock also clear any pending
883 bclass->event (basesink, event);
886 basesink->flushing = TRUE;
887 if (basesink->clock_id) {
888 gst_clock_id_unschedule (basesink->clock_id);
890 GST_UNLOCK (basesink);
892 GST_PREROLL_LOCK (pad);
893 /* we need preroll after the flush */
894 GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
895 basesink->need_preroll = TRUE;
896 /* unlock from a possible state change/preroll */
897 gst_base_sink_preroll_queue_flush (basesink, pad);
898 GST_PREROLL_UNLOCK (pad);
900 /* and we need to commit our state again on the next
901 * prerolled buffer */
902 GST_STREAM_LOCK (pad);
903 gst_element_lost_state (GST_ELEMENT (basesink));
904 GST_STREAM_UNLOCK (pad);
905 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
906 gst_event_unref (event);
908 case GST_EVENT_FLUSH_STOP:
910 bclass->event (basesink, event);
912 /* now we are completely unblocked and the _chain method
914 GST_STREAM_LOCK (pad);
916 basesink->flushing = FALSE;
917 GST_UNLOCK (basesink);
918 /* we need new segment info after the flush. */
919 basesink->segment_start = -1;
920 basesink->segment_stop = -1;
921 basesink->current_start = -1;
922 basesink->current_end = -1;
923 GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
924 GST_TIME_ARGS (basesink->segment_accum));
925 basesink->segment_accum = 0;
926 GST_STREAM_UNLOCK (pad);
928 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
929 gst_event_unref (event);
932 gst_event_unref (event);
935 gst_object_unref (basesink);
940 /* default implementation to calculate the start and end
941 * timestamps on a buffer, subclasses can override
944 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
945 GstClockTime * start, GstClockTime * end)
947 GstClockTime timestamp, duration;
949 timestamp = GST_BUFFER_TIMESTAMP (buffer);
950 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
952 /* get duration to calculate end time */
953 duration = GST_BUFFER_DURATION (buffer);
954 if (GST_CLOCK_TIME_IS_VALID (duration)) {
955 *end = timestamp + duration;
961 /* with STREAM_LOCK and LOCK*/
962 static GstClockReturn
963 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
968 /* no need to attempt a clock wait if we are flushing */
969 if (basesink->flushing) {
970 return GST_CLOCK_UNSCHEDULED;
973 /* clock_id should be NULL outside of this function */
974 g_assert (basesink->clock_id == NULL);
975 g_assert (GST_CLOCK_TIME_IS_VALID (time));
977 id = gst_clock_new_single_shot_id (basesink->clock, time);
979 basesink->clock_id = id;
980 /* release the object lock while waiting */
981 GST_UNLOCK (basesink);
983 ret = gst_clock_id_wait (id, NULL);
986 gst_clock_id_unref (id);
987 basesink->clock_id = NULL;
992 /* perform synchronisation on a buffer
994 * 1) check if we have a clock, if not, do nothing
995 * 2) calculate the start and end time of the buffer
996 * 3) create a single shot notification to wait on
997 * the clock, save the entry so we can unlock it
998 * 4) wait on the clock, this blocks
999 * 5) unref the clockid again
1001 static GstClockReturn
1002 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
1004 GstClockReturn result = GST_CLOCK_OK;
1005 GstClockTime start, end;
1006 GstClockTimeDiff stream_start, stream_end;
1007 GstBaseSinkClass *bclass;
1008 gboolean start_valid, end_valid;
1010 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1013 if (bclass->get_times)
1014 bclass->get_times (basesink, buffer, &start, &end);
1016 start_valid = GST_CLOCK_TIME_IS_VALID (start);
1017 end_valid = GST_CLOCK_TIME_IS_VALID (end);
1019 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1020 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
1022 /* if we don't have a timestamp, we don't sync */
1024 GST_DEBUG_OBJECT (basesink, "start not valid");
1028 /* save last times seen. */
1029 basesink->current_start = start;
1031 basesink->current_end = end;
1033 basesink->current_end = start;
1035 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
1036 /* check if not outside of the segment range, start is
1037 * always valid here. */
1038 if (start > basesink->segment_stop)
1039 goto out_of_segment;
1042 /* bring timestamp to stream time using last segment offset. */
1043 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1044 /* check if not outside of the segment range */
1045 if (end_valid && end < basesink->segment_start)
1046 goto out_of_segment;
1048 stream_start = (gint64) start - basesink->segment_start;
1049 stream_end = (gint64) end - basesink->segment_start;
1051 stream_start = (gint64) start;
1052 stream_end = (gint64) end;
1055 /* correct for rate */
1056 if (basesink->segment_rate != 0.0) {
1057 stream_start /= ABS (basesink->segment_rate);
1059 stream_end /= ABS (basesink->segment_rate);
1062 stream_start += basesink->segment_accum;
1064 stream_end += basesink->segment_accum;
1066 /* now do clocking */
1067 if (basesink->clock && basesink->sync) {
1068 GstClockTime base_time;
1070 GST_LOCK (basesink);
1072 base_time = GST_ELEMENT_CAST (basesink)->base_time;
1074 GST_LOG_OBJECT (basesink,
1075 "waiting for clock, base time %" GST_TIME_FORMAT
1076 "stream_start %" GST_TIME_FORMAT,
1077 GST_TIME_ARGS (base_time), GST_TIME_ARGS (stream_start));
1079 /* also save end_time of this buffer so that we can wait
1082 basesink->end_time = stream_end + base_time;
1084 basesink->end_time = GST_CLOCK_TIME_NONE;
1086 result = gst_base_sink_wait (basesink, stream_start + base_time);
1088 GST_UNLOCK (basesink);
1090 GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1092 GST_DEBUG_OBJECT (basesink, "no clock, not syncing");
1100 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1101 return GST_CLOCK_UNSCHEDULED;
1108 * 2) render the event
1109 * 3) unref the event
1111 static inline gboolean
1112 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1114 GstBaseSinkClass *bclass;
1117 switch (GST_EVENT_TYPE (event)) {
1119 GST_LOCK (basesink);
1120 if (basesink->clock) {
1121 /* wait for last buffer to finish if we have a valid end time */
1122 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1123 gst_base_sink_wait (basesink, basesink->end_time);
1124 basesink->end_time = GST_CLOCK_TIME_NONE;
1127 GST_UNLOCK (basesink);
1133 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1135 ret = bclass->event (basesink, event);
1139 switch (GST_EVENT_TYPE (event)) {
1141 GST_PREROLL_LOCK (basesink->sinkpad);
1142 /* if we are still EOS, we can post the EOS message */
1143 if (basesink->eos) {
1144 /* ok, now we can post the message */
1145 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1146 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1147 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
1148 basesink->eos_queued = FALSE;
1150 GST_PREROLL_UNLOCK (basesink->sinkpad);
1156 GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1157 gst_event_unref (event);
1164 * 1) first sync on the buffer
1165 * 2) render the buffer
1166 * 3) unref the buffer
1168 static inline GstFlowReturn
1169 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1171 GstFlowReturn ret = GST_FLOW_OK;
1172 GstClockReturn status;
1174 status = gst_base_sink_do_sync (basesink, buf);
1176 case GST_CLOCK_EARLY:
1177 GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1178 /* fallthrough for now */
1181 GstBaseSinkClass *bclass;
1183 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1185 ret = bclass->render (basesink, buf);
1189 GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1193 GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1194 gst_buffer_unref (buf);
1199 static GstFlowReturn
1200 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1202 GstBaseSink *basesink;
1203 GstFlowReturn result;
1205 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1207 if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1209 g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1210 GST_DEBUG_PAD_NAME (pad));
1212 result = GST_FLOW_UNEXPECTED;
1217 gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT_CAST (buf));
1220 gst_object_unref (basesink);
1226 gst_base_sink_loop (GstPad * pad)
1228 GstBaseSink *basesink;
1229 GstBuffer *buf = NULL;
1230 GstFlowReturn result;
1232 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1234 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1236 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1237 if (result != GST_FLOW_OK)
1240 result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1241 if (result != GST_FLOW_OK)
1244 gst_object_unref (basesink);
1251 gst_base_sink_event (pad, gst_event_new_eos ());
1252 gst_object_unref (basesink);
1253 gst_pad_pause_task (pad);
1259 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1261 gboolean result = FALSE;
1262 GstBaseSinkClass *bclass;
1264 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1266 /* step 1, unblock clock sync (if any) or any other blocking thing */
1267 GST_PREROLL_LOCK (pad);
1268 GST_LOCK (basesink);
1269 if (basesink->clock_id) {
1270 gst_clock_id_unschedule (basesink->clock_id);
1272 GST_UNLOCK (basesink);
1274 /* unlock any subclasses */
1276 bclass->unlock (basesink);
1278 /* flush out the data thread if it's locked in finish_preroll */
1279 GST_DEBUG_OBJECT (basesink,
1280 "flushing out data thread, need preroll to FALSE");
1281 basesink->need_preroll = FALSE;
1282 gst_base_sink_preroll_queue_flush (basesink, pad);
1283 GST_PREROLL_SIGNAL (pad);
1284 GST_PREROLL_UNLOCK (pad);
1286 /* step 2, make sure streaming finishes */
1287 result = gst_pad_stop_task (pad);
1293 gst_base_sink_activate (GstPad * pad)
1295 gboolean result = FALSE;
1296 GstBaseSink *basesink;
1298 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1300 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1302 if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1303 && gst_pad_activate_pull (pad, TRUE)) {
1304 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1307 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1308 if (gst_pad_activate_push (pad, TRUE)) {
1309 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1315 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1318 gst_object_unref (basesink);
1324 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1327 GstBaseSink *basesink;
1329 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1332 if (!basesink->can_activate_push) {
1334 basesink->pad_mode = GST_ACTIVATE_NONE;
1337 basesink->pad_mode = GST_ACTIVATE_PUSH;
1340 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1341 g_warning ("Internal GStreamer activation error!!!");
1344 result = gst_base_sink_deactivate (basesink, pad);
1345 basesink->pad_mode = GST_ACTIVATE_NONE;
1349 gst_object_unref (basesink);
1354 /* this won't get called until we implement an activate function */
1356 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1358 gboolean result = FALSE;
1359 GstBaseSink *basesink;
1361 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1364 if (!basesink->can_activate_pull) {
1366 basesink->pad_mode = GST_ACTIVATE_NONE;
1368 GstPad *peer = gst_pad_get_peer (pad);
1370 if (G_UNLIKELY (peer == NULL)) {
1371 g_warning ("Trying to activate pad in pull mode, but no peer");
1373 basesink->pad_mode = GST_ACTIVATE_NONE;
1375 if (gst_pad_activate_pull (peer, TRUE)) {
1376 basesink->have_newsegment = TRUE;
1377 basesink->segment_start = basesink->segment_stop = 0;
1379 /* set the pad mode before starting the task so that it's in the
1380 correct state for the new thread... */
1381 basesink->pad_mode = GST_ACTIVATE_PULL;
1383 gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1385 /* but if starting the thread fails, set it back */
1387 basesink->pad_mode = GST_ACTIVATE_NONE;
1389 GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1391 basesink->pad_mode = GST_ACTIVATE_NONE;
1393 gst_object_unref (peer);
1397 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1398 g_warning ("Internal GStreamer activation error!!!");
1401 basesink->have_newsegment = FALSE;
1402 result = gst_base_sink_deactivate (basesink, pad);
1403 basesink->pad_mode = GST_ACTIVATE_NONE;
1407 gst_object_unref (basesink);
1413 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1416 GstBaseSink *basesink = GST_BASE_SINK (element);
1420 pad = basesink->sinkpad;
1421 gst_object_ref (pad);
1422 GST_UNLOCK (element);
1424 result = gst_pad_push_event (pad, event);
1426 gst_object_unref (pad);
1432 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1435 gboolean res = FALSE;
1437 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1438 res = gst_pad_query (peer, query);
1439 gst_object_unref (peer);
1445 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
1449 gboolean res = FALSE;
1452 case GST_FORMAT_TIME:
1454 /* we can answer time format */
1455 GST_LOCK (basesink);
1456 if ((clock = GST_ELEMENT_CLOCK (basesink))) {
1458 gint64 segment_time;
1460 gst_object_ref (clock);
1461 GST_UNLOCK (basesink);
1463 now = gst_clock_get_time (clock);
1465 GST_LOCK (basesink);
1466 if (GST_CLOCK_TIME_IS_VALID (basesink->segment_time))
1467 segment_time = basesink->segment_time;
1471 *cur = now - GST_ELEMENT_CAST (basesink)->base_time + segment_time;
1473 GST_DEBUG_OBJECT (basesink,
1474 "now %" GST_TIME_FORMAT " + segment_time %" GST_TIME_FORMAT " = %"
1475 GST_TIME_FORMAT, GST_TIME_ARGS (now),
1476 GST_TIME_ARGS (segment_time), GST_TIME_ARGS (*cur));
1478 gst_object_unref (clock);
1482 GST_UNLOCK (basesink);
1491 gst_base_sink_query (GstElement * element, GstQuery * query)
1493 gboolean res = FALSE;
1495 GstBaseSink *basesink = GST_BASE_SINK (element);
1497 switch (GST_QUERY_TYPE (query)) {
1498 case GST_QUERY_POSITION:
1504 GST_PREROLL_LOCK (basesink->sinkpad);
1505 eos = basesink->eos;
1506 GST_PREROLL_UNLOCK (basesink->sinkpad);
1509 res = gst_base_sink_peer_query (basesink, query);
1511 gst_query_parse_position (query, &format, NULL);
1513 GST_DEBUG_OBJECT (basesink, "current position format %d", format);
1515 if ((res = gst_base_sink_get_position (basesink, format, &cur))) {
1516 gst_query_set_position (query, format, cur);
1518 res = gst_base_sink_peer_query (basesink, query);
1523 case GST_QUERY_DURATION:
1524 res = gst_base_sink_peer_query (basesink, query);
1526 case GST_QUERY_LATENCY:
1528 case GST_QUERY_JITTER:
1530 case GST_QUERY_RATE:
1531 //gst_query_set_rate (query, basesink->segment_rate);
1534 case GST_QUERY_SEGMENT:
1536 /* FIXME, bring start/stop to stream time */
1537 gst_query_set_segment (query, basesink->segment_rate,
1538 GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop);
1541 case GST_QUERY_SEEKING:
1542 case GST_QUERY_CONVERT:
1543 case GST_QUERY_FORMATS:
1545 res = gst_base_sink_peer_query (basesink, query);
1551 static GstStateChangeReturn
1552 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1554 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1555 GstBaseSink *basesink = GST_BASE_SINK (element);
1556 GstBaseSinkClass *bclass;
1558 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1560 switch (transition) {
1561 case GST_STATE_CHANGE_NULL_TO_READY:
1563 if (!bclass->start (basesink))
1566 case GST_STATE_CHANGE_READY_TO_PAUSED:
1567 /* need to complete preroll before this state change completes, there
1568 * is no data flow in READY so we can safely assume we need to preroll. */
1569 basesink->offset = 0;
1570 GST_PREROLL_LOCK (basesink->sinkpad);
1571 basesink->have_preroll = FALSE;
1572 GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1573 basesink->need_preroll = TRUE;
1574 GST_PREROLL_UNLOCK (basesink->sinkpad);
1575 basesink->have_newsegment = FALSE;
1576 basesink->segment_rate = 1.0;
1577 basesink->segment_start = 0;
1578 basesink->segment_stop = 0;
1579 basesink->segment_time = 0;
1580 basesink->segment_accum = 0;
1581 ret = GST_STATE_CHANGE_ASYNC;
1583 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1584 GST_PREROLL_LOCK (basesink->sinkpad);
1585 /* no preroll needed */
1586 basesink->need_preroll = FALSE;
1588 /* if we have EOS, we should empty the queue now as there will
1589 * be no more data received in the chain function.
1590 * FIXME, this could block the state change function too long when
1591 * we are pushing and syncing the buffers, better start a new
1592 * thread to do this. */
1593 if (basesink->eos) {
1594 gboolean do_eos = !basesink->eos_queued;
1596 gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1598 /* need to post EOS message here if it was not in the preroll queue we
1601 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1602 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1603 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
1605 } else if (!basesink->have_preroll) {
1606 /* queue a commit_state */
1607 basesink->need_preroll = TRUE;
1608 GST_DEBUG_OBJECT (basesink,
1609 "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1610 ret = GST_STATE_CHANGE_ASYNC;
1611 /* we know it's not waiting, no need to signal */
1613 GST_DEBUG_OBJECT (basesink,
1614 "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1615 /* now let it play */
1616 GST_PREROLL_SIGNAL (basesink->sinkpad);
1618 GST_PREROLL_UNLOCK (basesink->sinkpad);
1625 GstStateChangeReturn bret;
1627 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1628 if (bret == GST_STATE_CHANGE_FAILURE)
1629 goto activate_failed;
1632 switch (transition) {
1633 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1635 GstBaseSinkClass *bclass;
1637 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1639 GST_PREROLL_LOCK (basesink->sinkpad);
1640 GST_LOCK (basesink);
1641 /* unlock clock wait if any */
1642 if (basesink->clock_id) {
1643 gst_clock_id_unschedule (basesink->clock_id);
1645 GST_UNLOCK (basesink);
1647 /* unlock any subclasses */
1649 bclass->unlock (basesink);
1651 /* if we don't have a preroll buffer and we have not received EOS,
1652 * we need to wait for a preroll */
1653 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1654 basesink->have_preroll, basesink->eos);
1655 if (!basesink->have_preroll && !basesink->eos
1656 && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1657 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1658 basesink->need_preroll = TRUE;
1659 ret = GST_STATE_CHANGE_ASYNC;
1661 GST_PREROLL_UNLOCK (basesink->sinkpad);
1664 case GST_STATE_CHANGE_PAUSED_TO_READY:
1666 case GST_STATE_CHANGE_READY_TO_NULL:
1668 if (!bclass->stop (basesink)) {
1669 GST_WARNING ("failed to stop");
1681 GST_DEBUG_OBJECT (basesink, "failed to start");
1682 return GST_STATE_CHANGE_FAILURE;
1686 GST_DEBUG_OBJECT (basesink,
1687 "element failed to change states -- activation problem?");
1688 return GST_STATE_CHANGE_FAILURE;