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.
26 #include "gstbasesink.h"
27 #include <gst/gstmarshal.h>
29 GST_DEBUG_CATEGORY_STATIC (gst_basesink_debug);
30 #define GST_CAT_DEFAULT gst_basesink_debug
32 /* #define DEBUGGING */
34 #define DEBUG(str,args...) g_print (str,##args)
36 #define DEBUG(str,args...)
39 /* BaseSink signals and properties */
47 /* FIXME, need to figure out a better way to handle the pull mode */
48 #define DEFAULT_SIZE 1024
49 #define DEFAULT_HAS_LOOP FALSE
50 #define DEFAULT_HAS_CHAIN TRUE
57 PROP_PREROLL_QUEUE_LEN
60 static GstElementClass *parent_class = NULL;
62 static void gst_basesink_base_init (gpointer g_class);
63 static void gst_basesink_class_init (GstBaseSinkClass * klass);
64 static void gst_basesink_init (GstBaseSink * trans, gpointer g_class);
65 static void gst_basesink_finalize (GObject * object);
68 gst_basesink_get_type (void)
70 static GType basesink_type = 0;
73 static const GTypeInfo basesink_info = {
74 sizeof (GstBaseSinkClass),
75 (GBaseInitFunc) gst_basesink_base_init,
77 (GClassInitFunc) gst_basesink_class_init,
82 (GInstanceInitFunc) gst_basesink_init,
85 basesink_type = g_type_register_static (GST_TYPE_ELEMENT,
86 "GstBaseSink", &basesink_info, G_TYPE_FLAG_ABSTRACT);
91 static void gst_basesink_set_clock (GstElement * element, GstClock * clock);
93 static void gst_basesink_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
95 static void gst_basesink_get_property (GObject * object, guint prop_id,
96 GValue * value, GParamSpec * pspec);
98 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
99 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
100 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
101 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
102 static void gst_basesink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
103 GstClockTime * start, GstClockTime * end);
105 static GstElementStateReturn gst_basesink_change_state (GstElement * element);
107 static GstFlowReturn gst_basesink_chain_unlocked (GstPad * pad,
109 static void gst_basesink_loop (GstPad * pad);
110 static GstFlowReturn gst_basesink_chain (GstPad * pad, GstBuffer * buffer);
111 static gboolean gst_basesink_activate (GstPad * pad, GstActivateMode mode);
112 static gboolean gst_basesink_event (GstPad * pad, GstEvent * event);
113 static inline GstFlowReturn gst_basesink_handle_buffer (GstBaseSink * basesink,
117 gst_basesink_base_init (gpointer g_class)
119 GST_DEBUG_CATEGORY_INIT (gst_basesink_debug, "basesink", 0,
124 gst_basesink_class_init (GstBaseSinkClass * klass)
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
129 gobject_class = (GObjectClass *) klass;
130 gstelement_class = (GstElementClass *) klass;
132 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
134 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_basesink_finalize);
135 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_basesink_set_property);
136 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_basesink_get_property);
138 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
139 g_param_spec_boolean ("has-loop", "has-loop",
140 "Enable loop-based operation", DEFAULT_HAS_LOOP,
141 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
142 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_CHAIN,
143 g_param_spec_boolean ("has-chain", "has-chain",
144 "Enable chain-based operation", DEFAULT_HAS_CHAIN,
145 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
146 /* FIXME, this next value should be configured using an event from the
147 * upstream element */
148 g_object_class_install_property (G_OBJECT_CLASS (klass),
149 PROP_PREROLL_QUEUE_LEN,
150 g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
151 "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
152 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
154 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_basesink_set_clock);
155 gstelement_class->change_state =
156 GST_DEBUG_FUNCPTR (gst_basesink_change_state);
158 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
159 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
160 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
161 klass->get_times = GST_DEBUG_FUNCPTR (gst_basesink_get_times);
165 gst_basesink_pad_getcaps (GstPad * pad)
167 GstBaseSinkClass *bclass;
169 GstCaps *caps = NULL;
171 bsink = GST_BASESINK (GST_PAD_PARENT (pad));
172 bclass = GST_BASESINK_GET_CLASS (bsink);
173 if (bclass->get_caps)
174 caps = bclass->get_caps (bsink);
177 GstPadTemplate *pad_template;
180 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
181 if (pad_template != NULL) {
182 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
190 gst_basesink_pad_setcaps (GstPad * pad, GstCaps * caps)
192 GstBaseSinkClass *bclass;
194 gboolean res = FALSE;
196 bsink = GST_BASESINK (GST_PAD_PARENT (pad));
197 bclass = GST_BASESINK_GET_CLASS (bsink);
199 if (bclass->set_caps)
200 res = bclass->set_caps (bsink, caps);
206 gst_basesink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
207 GstCaps * caps, GstBuffer ** buf)
209 GstBaseSinkClass *bclass;
211 GstFlowReturn result = GST_FLOW_OK;
213 bsink = GST_BASESINK (GST_PAD_PARENT (pad));
214 bclass = GST_BASESINK_GET_CLASS (bsink);
216 if (bclass->buffer_alloc)
217 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
225 gst_basesink_init (GstBaseSink * basesink, gpointer g_class)
227 GstPadTemplate *pad_template;
230 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
231 g_return_if_fail (pad_template != NULL);
233 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
235 gst_pad_set_getcaps_function (basesink->sinkpad,
236 GST_DEBUG_FUNCPTR (gst_basesink_pad_getcaps));
237 gst_pad_set_setcaps_function (basesink->sinkpad,
238 GST_DEBUG_FUNCPTR (gst_basesink_pad_setcaps));
239 gst_pad_set_bufferalloc_function (basesink->sinkpad,
240 GST_DEBUG_FUNCPTR (gst_basesink_pad_buffer_alloc));
241 gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
243 basesink->pad_mode = GST_ACTIVATE_NONE;
244 GST_PAD_TASK (basesink->sinkpad) = NULL;
245 basesink->preroll_queue = g_queue_new ();
247 GST_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
251 gst_basesink_finalize (GObject * object)
253 GstBaseSink *basesink;
255 basesink = GST_BASESINK (object);
257 g_queue_free (basesink->preroll_queue);
259 G_OBJECT_CLASS (parent_class)->finalize (object);
263 gst_basesink_set_pad_functions (GstBaseSink * this, GstPad * pad)
265 gst_pad_set_activate_function (pad,
266 GST_DEBUG_FUNCPTR (gst_basesink_activate));
267 gst_pad_set_event_function (pad, GST_DEBUG_FUNCPTR (gst_basesink_event));
270 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_basesink_chain));
272 gst_pad_set_chain_function (pad, NULL);
275 gst_pad_set_loop_function (pad, GST_DEBUG_FUNCPTR (gst_basesink_loop));
277 gst_pad_set_loop_function (pad, NULL);
281 gst_basesink_set_all_pad_functions (GstBaseSink * this)
285 for (l = GST_ELEMENT_PADS (this); l; l = l->next)
286 gst_basesink_set_pad_functions (this, (GstPad *) l->data);
290 gst_basesink_set_clock (GstElement * element, GstClock * clock)
294 sink = GST_BASESINK (element);
300 gst_basesink_set_property (GObject * object, guint prop_id,
301 const GValue * value, GParamSpec * pspec)
305 sink = GST_BASESINK (object);
310 sink->has_loop = g_value_get_boolean (value);
311 gst_basesink_set_all_pad_functions (sink);
314 sink->has_chain = g_value_get_boolean (value);
315 gst_basesink_set_all_pad_functions (sink);
317 case PROP_PREROLL_QUEUE_LEN:
318 /* preroll lock necessary to serialize with finish_preroll */
319 GST_PREROLL_LOCK (sink->sinkpad);
320 sink->preroll_queue_max_len = g_value_get_uint (value);
321 GST_PREROLL_UNLOCK (sink->sinkpad);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gst_basesink_get_property (GObject * object, guint prop_id, GValue * value,
336 sink = GST_BASESINK (object);
341 g_value_set_boolean (value, sink->has_loop);
344 g_value_set_boolean (value, sink->has_chain);
346 case PROP_PREROLL_QUEUE_LEN:
347 g_value_set_uint (value, sink->preroll_queue_max_len);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 gst_base_sink_get_caps (GstBaseSink * sink)
363 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
369 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
370 GstCaps * caps, GstBuffer ** buf)
376 /* with PREROLL_LOCK */
378 gst_basesink_preroll_queue_push (GstBaseSink * basesink, GstPad * pad,
381 /* if we don't have a buffer we just start blocking */
386 if (basesink->preroll_queue->length == 0) {
387 GstBaseSinkClass *bclass = GST_BASESINK_GET_CLASS (basesink);
389 GST_DEBUG ("preroll buffer with TS: %" GST_TIME_FORMAT,
390 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
392 bclass->preroll (basesink, buffer);
395 if (basesink->preroll_queue->length < basesink->preroll_queue_max_len) {
396 DEBUG ("push %p %p\n", basesink, buffer);
397 g_queue_push_tail (basesink->preroll_queue, buffer);
402 /* block until the state changes, or we get a flush, or something */
403 DEBUG ("block %p %p\n", basesink, buffer);
404 GST_DEBUG ("element %s waiting to finish preroll",
405 GST_ELEMENT_NAME (basesink));
406 basesink->need_preroll = FALSE;
407 basesink->have_preroll = TRUE;
408 GST_PREROLL_WAIT (pad);
409 GST_DEBUG ("done preroll");
410 basesink->have_preroll = FALSE;
413 /* with PREROLL_LOCK */
415 gst_basesink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
418 GQueue *q = basesink->preroll_queue;
424 DEBUG ("empty queue\n");
425 while ((buf = g_queue_pop_head (q))) {
426 DEBUG ("pop %p\n", buf);
427 ret = gst_basesink_handle_buffer (basesink, buf);
429 DEBUG ("queue len %p %d\n", basesink, q->length);
434 /* with PREROLL_LOCK */
436 gst_basesink_preroll_queue_flush (GstBaseSink * basesink)
439 GQueue *q = basesink->preroll_queue;
441 DEBUG ("flush %p\n", basesink);
443 while ((buf = g_queue_pop_head (q))) {
444 DEBUG ("pop %p\n", buf);
445 gst_buffer_unref (buf);
458 /* with STREAM_LOCK */
460 gst_basesink_finish_preroll (GstBaseSink * basesink, GstPad * pad,
465 DEBUG ("finish preroll %p <\n", basesink);
466 /* lock order is important */
467 GST_STATE_LOCK (basesink);
468 GST_PREROLL_LOCK (pad);
469 DEBUG ("finish preroll %p >\n", basesink);
470 if (!basesink->need_preroll)
473 gst_element_commit_state (GST_ELEMENT (basesink));
474 GST_STATE_UNLOCK (basesink);
476 gst_basesink_preroll_queue_push (basesink, pad, buffer);
479 flushing = GST_PAD_IS_FLUSHING (pad);
484 if (basesink->need_preroll)
487 GST_DEBUG ("done preroll");
489 gst_basesink_preroll_queue_empty (basesink, pad);
491 GST_PREROLL_UNLOCK (pad);
493 return PREROLL_PLAYING;
497 /* maybe it was another sink that blocked in preroll, need to check for
499 if (basesink->preroll_queue->length)
500 gst_basesink_preroll_queue_empty (basesink, pad);
501 GST_PREROLL_UNLOCK (pad);
502 GST_STATE_UNLOCK (basesink);
503 return PREROLL_PLAYING;
507 GST_DEBUG ("pad is flushing");
508 GST_PREROLL_UNLOCK (pad);
509 return PREROLL_FLUSHING;
513 GST_PREROLL_UNLOCK (pad);
514 return PREROLL_QUEUEING;
519 gst_basesink_event (GstPad * pad, GstEvent * event)
521 GstBaseSink *basesink;
522 gboolean result = TRUE;
523 GstBaseSinkClass *bclass;
525 basesink = GST_BASESINK (GST_OBJECT_PARENT (pad));
527 bclass = GST_BASESINK_GET_CLASS (basesink);
529 DEBUG ("event %p\n", basesink);
532 bclass->event (basesink, event);
534 switch (GST_EVENT_TYPE (event)) {
539 GST_STREAM_LOCK (pad);
541 /* EOS also finishes the preroll */
542 gst_basesink_finish_preroll (basesink, pad, NULL);
545 need_eos = basesink->eos = TRUE;
546 if (basesink->clock) {
547 /* wait for last buffer to finish if we have a valid end time */
548 if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
549 basesink->clock_id = gst_clock_new_single_shot_id (basesink->clock,
550 basesink->end_time + GST_ELEMENT (basesink)->base_time);
551 GST_UNLOCK (basesink);
553 gst_clock_id_wait (basesink->clock_id, NULL);
556 if (basesink->clock_id) {
557 gst_clock_id_unref (basesink->clock_id);
558 basesink->clock_id = NULL;
560 basesink->end_time = GST_CLOCK_TIME_NONE;
561 need_eos = basesink->eos;
563 GST_UNLOCK (basesink);
565 /* if we are still EOS, we can post the EOS message */
567 /* ok, now we can post the message */
568 gst_element_post_message (GST_ELEMENT (basesink),
569 gst_message_new_eos (GST_OBJECT (basesink)));
572 GST_STREAM_UNLOCK (pad);
575 case GST_EVENT_DISCONTINUOUS:
576 GST_STREAM_LOCK (pad);
577 if (basesink->clock) {
578 //gint64 value = GST_EVENT_DISCONT_OFFSET (event, 0).value;
580 GST_STREAM_UNLOCK (pad);
582 case GST_EVENT_FLUSH:
583 /* make sure we are not blocked on the clock also clear any pending
585 if (!GST_EVENT_FLUSH_DONE (event)) {
587 basesink->eos = FALSE;
588 if (basesink->clock_id) {
589 gst_clock_id_unschedule (basesink->clock_id);
591 GST_UNLOCK (basesink);
593 /* unlock from a possible state change/preroll */
594 GST_PREROLL_LOCK (pad);
595 basesink->need_preroll = TRUE;
596 gst_basesink_preroll_queue_flush (basesink);
597 GST_PREROLL_SIGNAL (pad);
598 GST_PREROLL_UNLOCK (pad);
600 /* now we are completely unblocked and the _chain method
604 result = gst_pad_event_default (pad, event);
611 /* default implementation to calculate the start and end
612 * timestamps on a buffer, subclasses cna override
615 gst_basesink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
616 GstClockTime * start, GstClockTime * end)
618 GstClockTime timestamp, duration;
620 timestamp = GST_BUFFER_TIMESTAMP (buffer);
621 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
622 duration = GST_BUFFER_DURATION (buffer);
623 if (GST_CLOCK_TIME_IS_VALID (duration)) {
624 *end = timestamp + duration;
630 /* perform synchronisation on a buffer
632 * 1) check if we have a clock, if not, do nothing
633 * 2) calculate the start and end time of the buffer
634 * 3) create a single shot notification to wait on
635 * the clock, save the entry so we can unlock it
636 * 4) wait on the clock, this blocks
637 * 5) unref the clockid again
640 gst_basesink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
642 if (basesink->clock) {
644 GstClockTime start, end;
645 GstBaseSinkClass *bclass;
647 bclass = GST_BASESINK_GET_CLASS (basesink);
649 if (bclass->get_times)
650 bclass->get_times (basesink, buffer, &start, &end);
652 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
653 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
655 if (GST_CLOCK_TIME_IS_VALID (start)) {
656 /* save clock id so that we can unlock it if needed */
658 basesink->clock_id = gst_clock_new_single_shot_id (basesink->clock,
659 start + GST_ELEMENT (basesink)->base_time);
660 basesink->end_time = end;
661 GST_UNLOCK (basesink);
663 ret = gst_clock_id_wait (basesink->clock_id, NULL);
666 if (basesink->clock_id) {
667 gst_clock_id_unref (basesink->clock_id);
668 basesink->clock_id = NULL;
670 /* FIXME, don't mess with end_time here */
671 basesink->end_time = GST_CLOCK_TIME_NONE;
672 GST_UNLOCK (basesink);
674 GST_LOG_OBJECT (basesink, "clock entry done: %d", ret);
681 * 1) first sync on the buffer
682 * 2) render the buffer
683 * 3) unref the buffer
685 static inline GstFlowReturn
686 gst_basesink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
688 GstBaseSinkClass *bclass;
691 gst_basesink_do_sync (basesink, buf);
693 bclass = GST_BASESINK_GET_CLASS (basesink);
695 ret = bclass->render (basesink, buf);
699 DEBUG ("unref %p %p\n", basesink, buf);
700 gst_buffer_unref (buf);
706 gst_basesink_chain_unlocked (GstPad * pad, GstBuffer * buf)
708 GstBaseSink *basesink;
709 PrerollReturn result;
711 basesink = GST_BASESINK (GST_OBJECT_PARENT (pad));
713 DEBUG ("chain_unlocked %p\n", basesink);
715 result = gst_basesink_finish_preroll (basesink, pad, buf);
717 DEBUG ("chain_unlocked %p after, result %d\n", basesink, result);
720 case PREROLL_QUEUEING:
722 case PREROLL_PLAYING:
723 return gst_basesink_handle_buffer (basesink, buf);
724 case PREROLL_FLUSHING:
725 gst_buffer_unref (buf);
726 return GST_FLOW_UNEXPECTED;
728 g_assert_not_reached ();
729 return GST_FLOW_ERROR;
734 gst_basesink_chain (GstPad * pad, GstBuffer * buf)
736 GstFlowReturn result;
738 g_assert (GST_BASESINK (GST_OBJECT_PARENT (pad))->pad_mode ==
741 result = gst_basesink_chain_unlocked (pad, buf);
746 /* FIXME, not all sinks can operate in pull mode
749 gst_basesink_loop (GstPad * pad)
751 GstBaseSink *basesink;
752 GstBuffer *buf = NULL;
753 GstFlowReturn result;
755 basesink = GST_BASESINK (GST_OBJECT_PARENT (pad));
757 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
759 result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
760 if (result != GST_FLOW_OK)
763 result = gst_basesink_chain_unlocked (pad, buf);
764 if (result != GST_FLOW_OK)
771 gst_pad_pause_task (pad);
776 gst_basesink_activate (GstPad * pad, GstActivateMode mode)
778 gboolean result = FALSE;
779 GstBaseSink *basesink;
780 GstBaseSinkClass *bclass;
782 basesink = GST_BASESINK (GST_OBJECT_PARENT (pad));
783 bclass = GST_BASESINK_GET_CLASS (basesink);
786 case GST_ACTIVATE_PUSH:
787 g_return_val_if_fail (basesink->has_chain, FALSE);
790 case GST_ACTIVATE_PULL:
791 /* if we have a scheduler we can start the task */
792 g_return_val_if_fail (basesink->has_loop, FALSE);
793 gst_pad_peer_set_active (pad, mode);
795 gst_pad_start_task (pad, (GstTaskFunction) gst_basesink_loop, pad);
797 case GST_ACTIVATE_NONE:
798 /* step 1, unblock clock sync (if any) or any other blocking thing */
800 if (basesink->clock_id) {
801 gst_clock_id_unschedule (basesink->clock_id);
803 GST_UNLOCK (basesink);
805 /* unlock any subclasses */
807 bclass->unlock (basesink);
810 GST_PREROLL_LOCK (pad);
811 GST_PREROLL_SIGNAL (pad);
812 GST_PREROLL_UNLOCK (pad);
814 /* step 2, make sure streaming finishes */
815 result = gst_pad_stop_task (pad);
818 basesink->pad_mode = mode;
823 static GstElementStateReturn
824 gst_basesink_change_state (GstElement * element)
826 GstElementStateReturn ret = GST_STATE_SUCCESS;
827 GstBaseSink *basesink = GST_BASESINK (element);
828 GstElementState transition = GST_STATE_TRANSITION (element);
830 DEBUG ("state change > %p %x\n", basesink, transition);
832 switch (transition) {
833 case GST_STATE_NULL_TO_READY:
835 case GST_STATE_READY_TO_PAUSED:
836 /* need to complete preroll before this state change completes, there
837 * is no data flow in READY so we can safely assume we need to preroll. */
838 basesink->offset = 0;
839 GST_PREROLL_LOCK (basesink->sinkpad);
840 basesink->need_preroll = TRUE;
841 basesink->have_preroll = FALSE;
842 GST_PREROLL_UNLOCK (basesink->sinkpad);
843 ret = GST_STATE_ASYNC;
845 case GST_STATE_PAUSED_TO_PLAYING:
846 GST_PREROLL_LOCK (basesink->sinkpad);
847 if (basesink->have_preroll) {
848 /* now let it play */
849 GST_PREROLL_SIGNAL (basesink->sinkpad);
851 /* FIXME. We do not have a preroll and we don't need it anymore
852 * now, this is a case we want to avoid. One way would be to make
853 * a 'lost state' function that makes get_state return PAUSED with
854 * ASYNC to indicate that we are prerolling again. */
855 basesink->need_preroll = FALSE;
857 GST_PREROLL_UNLOCK (basesink->sinkpad);
863 GST_ELEMENT_CLASS (parent_class)->change_state (element);
865 switch (transition) {
866 case GST_STATE_PLAYING_TO_PAUSED:
870 /* unlock clock wait if any */
872 if (basesink->clock_id) {
873 gst_clock_id_unschedule (basesink->clock_id);
876 GST_UNLOCK (basesink);
878 GST_PREROLL_LOCK (basesink->sinkpad);
879 /* if we don't have a preroll buffer and we have not received EOS,
880 * we need to wait for a preroll */
881 if (!basesink->have_preroll && !eos) {
882 basesink->need_preroll = TRUE;
883 ret = GST_STATE_ASYNC;
885 GST_PREROLL_UNLOCK (basesink->sinkpad);
888 case GST_STATE_PAUSED_TO_READY:
889 /* flush out the data thread if it's locked in finish_preroll */
890 GST_PREROLL_LOCK (basesink->sinkpad);
892 gst_basesink_preroll_queue_flush (basesink);
894 if (basesink->have_preroll)
895 GST_PREROLL_SIGNAL (basesink->sinkpad);
897 basesink->need_preroll = FALSE;
898 basesink->have_preroll = FALSE;
899 GST_PREROLL_UNLOCK (basesink->sinkpad);
901 /* clear EOS state */
902 basesink->eos = FALSE;
904 case GST_STATE_READY_TO_NULL:
910 DEBUG ("state change < %p %x\n", basesink, transition);