]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - libs/gst/base/gstbasesink.c
gst/base/gstbasesink.c: Prepare for doing QOS.
[glsdk/gstreamer0-10.git] / libs / gst / base / gstbasesink.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstbasesink.c:
5  *
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.
10  *
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.
15  *
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.
20  */
22 /**
23  * SECTION:gstbasesink
24  * @short_description: Base class for sink elements
25  * @see_also: #GstBaseTransformc, #GstBaseSource
26  *
27  * This class is for elements that do output operations.
28  *
29  * <itemizedlist>
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>
36  * </itemizedlist>
37  */
39 #ifdef HAVE_CONFIG_H
40 #  include "config.h"
41 #endif
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 */
50 enum
51 {
52   /* FILL ME */
53   SIGNAL_HANDOFF,
54   LAST_SIGNAL
55 };
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
63 enum
64 {
65   PROP_0,
66   PROP_PREROLL_QUEUE_LEN,
67   PROP_SYNC
68 };
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);
77 GType
78 gst_base_sink_get_type (void)
79 {
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,
86       NULL,
87       (GClassInitFunc) gst_base_sink_class_init,
88       NULL,
89       NULL,
90       sizeof (GstBaseSink),
91       0,
92       (GInstanceInitFunc) gst_base_sink_init,
93     };
95     base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
96         "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
97   }
98   return base_sink_type;
99 }
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,
109     GstEvent * event);
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,
129     GstBuffer * buf);
130 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
131     GstEvent * event);
133 static void
134 gst_base_sink_base_init (gpointer g_class)
136   GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
137       "basesink element");
140 static void
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,
164           G_PARAM_READWRITE));
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);
178 static GstCaps *
179 gst_base_sink_pad_getcaps (GstPad * pad)
181   GstBaseSinkClass *bclass;
182   GstBaseSink *bsink;
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);
190   if (caps == NULL) {
191     GstPadTemplate *pad_template;
193     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));
197     }
198   }
199   gst_object_unref (bsink);
201   return caps;
204 static gboolean
205 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
207   GstBaseSinkClass *bclass;
208   GstBaseSink *bsink;
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);
219   return res;
222 static GstFlowReturn
223 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
224     GstCaps * caps, GstBuffer ** buf)
226   GstBaseSinkClass *bclass;
227   GstBaseSink *bsink;
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);
235   else
236     *buf = NULL;                /* fallback in gstpad.c will allocate generic buffer */
238   gst_object_unref (bsink);
240   return result;
243 static void
244 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
246   GstPadTemplate *pad_template;
248   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);
284 static void
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);
296 static void
297 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
299   GstBaseSink *sink;
301   sink = GST_BASE_SINK (element);
303   sink->clock = clock;
306 static void
307 gst_base_sink_set_property (GObject * object, guint prop_id,
308     const GValue * value, GParamSpec * pspec)
310   GstBaseSink *sink = GST_BASE_SINK (object);
312   switch (prop_id) {
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);
318       break;
319     case PROP_SYNC:
320       sink->sync = g_value_get_boolean (value);
321       break;
322     default:
323       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324       break;
325   }
328 static void
329 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
330     GParamSpec * pspec)
332   GstBaseSink *sink = GST_BASE_SINK (object);
334   GST_LOCK (sink);
335   switch (prop_id) {
336     case PROP_PREROLL_QUEUE_LEN:
337       g_value_set_uint (value, sink->preroll_queue_max_len);
338       break;
339     case PROP_SYNC:
340       g_value_set_boolean (value, sink->sync);
341       break;
342     default:
343       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344       break;
345   }
346   GST_UNLOCK (sink);
349 static GstCaps *
350 gst_base_sink_get_caps (GstBaseSink * sink)
352   return NULL;
355 static gboolean
356 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
358   return TRUE;
361 static GstFlowReturn
362 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
363     GstCaps * caps, GstBuffer ** buf)
365   *buf = NULL;
366   return GST_FLOW_OK;
369 /* with PREROLL_LOCK */
370 static GstFlowReturn
371 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
373   GstMiniObject *obj;
374   GQueue *q = basesink->preroll_queue;
375   GstFlowReturn ret;
377   ret = GST_FLOW_OK;
379   if (q) {
380     GST_DEBUG_OBJECT (basesink, "emptying queue");
381     while ((obj = g_queue_pop_head (q))) {
382       gboolean is_buffer;
384       is_buffer = GST_IS_BUFFER (obj);
385       if (is_buffer) {
386         basesink->preroll_queued--;
387         basesink->buffers_queued--;
388       } else {
389         switch (GST_EVENT_TYPE (obj)) {
390           case GST_EVENT_EOS:
391             basesink->preroll_queued--;
392             break;
393           default:
394             break;
395         }
396         basesink->events_queued--;
397       }
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);
403       if (is_buffer) {
404         GST_DEBUG_OBJECT (basesink, "popped buffer %p", obj);
405         ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER (obj));
406       } else {
407         GST_DEBUG_OBJECT (basesink, "popped event %p", obj);
408         gst_base_sink_handle_event (basesink, GST_EVENT (obj));
409         ret = GST_FLOW_OK;
410       }
412       GST_PREROLL_LOCK (pad);
413     }
414     GST_DEBUG_OBJECT (basesink, "queue empty");
415   }
416   return ret;
419 /* with PREROLL_LOCK */
420 static void
421 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
423   GstMiniObject *obj;
424   GQueue *q = basesink->preroll_queue;
426   GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
427   if (q) {
428     while ((obj = g_queue_pop_head (q))) {
429       GST_DEBUG_OBJECT (basesink, "popped %p", obj);
430       gst_mini_object_unref (obj);
431     }
432   }
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 */
445 static GstFlowReturn
446 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
447     GstMiniObject * obj)
449   gint length;
450   gboolean have_event;
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);
458   if (have_event) {
459     GstEvent *event = GST_EVENT (obj);
461     switch (GST_EVENT_TYPE (obj)) {
462       case GST_EVENT_EOS:
463         basesink->preroll_queued++;
464         basesink->eos = TRUE;
465         basesink->eos_queued = TRUE;
466         break;
467       case GST_EVENT_NEWSEGMENT:
468       {
469         GstFormat format;
470         gint64 segment_start;
471         gint64 segment_stop;
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;
484           segment_stop = -1;
485           basesink->segment_base = -1;
486         }
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;
501         }
502         /* check if we really have a new segment or the previous one is
503          * closed */
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
509            */
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;
517           } else {
518             basesink->segment_accum = 0;
519           }
520         }
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 %"
528             GST_TIME_FORMAT,
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));
533       done_newsegment:
534         break;
535       }
536       default:
537         break;
538     }
539     basesink->events_queued++;
540   } else {
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;
551     }
553     /* check if the buffer needs to be dropped */
554     if (TRUE) {
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));
575           goto dropping;
576         }
577       }
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));
585           goto dropping;
586         }
587       }
588     }
589     basesink->preroll_queued++;
590     basesink->buffers_queued++;
591   }
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)
598     goto playing_async;
600   /* check if we are prerolling */
601   if (!basesink->need_preroll)
602     goto no_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;
611       GstFlowReturn pres;
613       bclass = GST_BASE_SINK_GET_CLASS (basesink);
614       if (bclass->preroll)
615         if ((pres =
616                 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
617           goto preroll_failed;
618     }
619   }
620   length = basesink->preroll_queued;
621   GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
623   if (length == 1) {
624     gint t;
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);
634     if (t <= 0) {
635       GST_WARNING ("STREAM_LOCK should have been locked !!");
636       g_warning ("STREAM_LOCK should have been locked !!");
637     }
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 */
647     if (t > 0)
648       GST_STREAM_LOCK_FULL (pad, t);
650     /* and wait if needed */
651     GST_PREROLL_LOCK (pad);
653     GST_LOCK (pad);
654     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
655       goto flushing;
656     GST_UNLOCK (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)
661       goto no_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 */
668     if (length == 0) {
669       GST_ERROR_OBJECT (basesink,
670           "preroll_queued dropped from 1 to 0 while committing state change");
671     }
672     g_assert (length <= 1);
673   }
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");
683   }
684   GST_LOCK (pad);
685   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
686     goto flushing;
687   GST_UNLOCK (pad);
689   GST_PREROLL_UNLOCK (pad);
691   return GST_FLOW_OK;
693 no_preroll:
694   {
695     GstFlowReturn ret;
697     GST_DEBUG_OBJECT (basesink, "no preroll needed");
698     /* maybe it was another sink that blocked in preroll, need to check for
699        buffers to drain */
700     basesink->have_preroll = FALSE;
701     ret = gst_base_sink_preroll_queue_empty (basesink, pad);
702     GST_PREROLL_UNLOCK (pad);
704     return ret;
705   }
706 dropping:
707   {
708     GstBuffer *buf;
710     buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
712     gst_buffer_unref (buf);
713     GST_PREROLL_UNLOCK (pad);
715     return GST_FLOW_OK;
716   }
717 playing_async:
718   {
719     GstFlowReturn ret;
720     gint t;
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);
732     if (t <= 0) {
733       GST_WARNING ("STREAM_LOCK should have been locked !!");
734       g_warning ("STREAM_LOCK should have been locked !!");
735     }
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);
740     if (t > 0)
741       GST_STREAM_LOCK_FULL (pad, t);
743     return ret;
744   }
745 flushing:
746   {
747     GST_UNLOCK (pad);
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;
752   }
753 preroll_failed:
754   {
755     gint t;
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);
765     if (t <= 0) {
766       GST_WARNING ("STREAM_LOCK should have been locked !!");
767       g_warning ("STREAM_LOCK should have been locked !!");
768     }
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 */
777     if (t > 0)
778       GST_STREAM_LOCK_FULL (pad, t);
780     return GST_FLOW_ERROR;
781   }
784 static gboolean
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)) {
798     case GST_EVENT_EOS:
799     {
800       GstFlowReturn ret;
802       GST_STREAM_LOCK (pad);
803       /* EOS also finishes the preroll */
804       ret =
805           gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
806       GST_STREAM_UNLOCK (pad);
807       break;
808     }
809     case GST_EVENT_NEWSEGMENT:
810     {
811       GstFlowReturn ret;
813       GST_STREAM_LOCK (pad);
814       ret =
815           gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
816       GST_STREAM_UNLOCK (pad);
817       break;
818     }
819     case GST_EVENT_FLUSH_START:
820       /* make sure we are not blocked on the clock also clear any pending
821        * eos state. */
822       if (bclass->event)
823         bclass->event (basesink, event);
825       GST_LOCK (basesink);
826       basesink->flushing = TRUE;
827       if (basesink->clock_id) {
828         gst_clock_id_unschedule (basesink->clock_id);
829       }
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);
849       break;
850     case GST_EVENT_FLUSH_STOP:
851       if (bclass->event)
852         bclass->event (basesink, event);
854       /* now we are completely unblocked and the _chain method
855        * will return */
856       GST_STREAM_LOCK (pad);
857       GST_LOCK (basesink);
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);
872       break;
873     default:
874       gst_event_unref (event);
875       break;
876   }
877   gst_object_unref (basesink);
879   return result;
882 /* default implementation to calculate the start and end
883  * timestamps on a buffer, subclasses can override
884  */
885 static void
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;
898     }
899     *start = timestamp;
900   }
903 /* with STREAM_LOCK and LOCK*/
904 static GstClockReturn
905 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
907   GstClockReturn ret;
908   GstClockID id;
910   /* no need to attempt a clock wait if we are flushing */
911   if (basesink->flushing) {
912     return GST_CLOCK_UNSCHEDULED;
913   }
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);
927   GST_LOCK (basesink);
928   gst_clock_id_unref (id);
929   basesink->clock_id = NULL;
931   return ret;
934 /* perform synchronisation on a buffer
935  * 
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
942  */
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);
954   start = end = -1;
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 */
965   if (!start_valid)
966     goto done;
968   /* save last times seen. */
969   basesink->current_start = start;
970   if (end_valid)
971     basesink->current_end = end;
972   else
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)
979       goto out_of_segment;
980   }
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)
986       goto out_of_segment;
988     stream_start = (gint64) start - basesink->segment_start;
989     stream_end = (gint64) end - basesink->segment_start;
990   } else {
991     stream_start = (gint64) start;
992     stream_end = (gint64) end;
993   }
995   stream_start += basesink->segment_accum;
996   if (end_valid)
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
1012      * to signal EOS */
1013     if (end_valid)
1014       basesink->end_time = stream_end + base_time;
1015     else
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);
1023   }
1025 done:
1026   return result;
1028 out_of_segment:
1029   {
1030     GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1031     return GST_CLOCK_UNSCHEDULED;
1032   }
1036 /* handle an event
1037  *
1038  * 2) render the event
1039  * 3) unref the event
1040  */
1041 static inline gboolean
1042 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1044   GstBaseSinkClass *bclass;
1045   gboolean ret;
1047   switch (GST_EVENT_TYPE (event)) {
1048     case GST_EVENT_EOS:
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;
1055         }
1056       }
1057       GST_UNLOCK (basesink);
1058       break;
1059     default:
1060       break;
1061   }
1063   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1064   if (bclass->event)
1065     ret = bclass->event (basesink, event);
1066   else
1067     ret = TRUE;
1069   switch (GST_EVENT_TYPE (event)) {
1070     case GST_EVENT_EOS:
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;
1079       }
1080       GST_PREROLL_UNLOCK (basesink->sinkpad);
1081       break;
1082     default:
1083       break;
1084   }
1086   GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1087   gst_event_unref (event);
1089   return ret;
1092 /* handle a buffer
1093  *
1094  * 1) first sync on the buffer
1095  * 2) render the buffer
1096  * 3) unref the buffer
1097  */
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);
1105   switch (status) {
1106     case GST_CLOCK_EARLY:
1107       GST_DEBUG_OBJECT (basesink, "late frame !");
1108       /* fallthrough for now */
1109     case GST_CLOCK_OK:
1110     {
1111       GstBaseSinkClass *bclass;
1113       bclass = GST_BASE_SINK_GET_CLASS (basesink);
1114       if (bclass->render)
1115         ret = bclass->render (basesink, buf);
1116       break;
1117     }
1118     default:
1119       break;
1120   }
1122   GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1123   gst_buffer_unref (buf);
1125   return ret;
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)) {
1137     GST_LOCK (pad);
1138     g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1139         GST_DEBUG_PAD_NAME (pad));
1140     GST_UNLOCK (pad);
1141     result = GST_FLOW_UNEXPECTED;
1142     goto done;
1143   }
1145   result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1147 done:
1148   gst_object_unref (basesink);
1150   return result;
1153 static void
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)
1166     goto paused;
1168   result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1169   if (result != GST_FLOW_OK)
1170     goto paused;
1172   gst_object_unref (basesink);
1174   /* default */
1175   return;
1177 paused:
1178   {
1179     gst_base_sink_event (pad, gst_event_new_eos ());
1180     gst_object_unref (basesink);
1181     gst_pad_pause_task (pad);
1182     return;
1183   }
1186 static gboolean
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);
1199   }
1200   GST_UNLOCK (basesink);
1202   /* unlock any subclasses */
1203   if (bclass->unlock)
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);
1217   return result;
1220 static gboolean
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");
1233     result = TRUE;
1234   } else {
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");
1238       result = TRUE;
1239     }
1240   }
1242   if (!result) {
1243     GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1244   }
1246   gst_object_unref (basesink);
1248   return result;
1251 static gboolean
1252 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1254   gboolean result;
1255   GstBaseSink *basesink;
1257   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1259   if (active) {
1260     if (!basesink->can_activate_push) {
1261       result = FALSE;
1262       basesink->pad_mode = GST_ACTIVATE_NONE;
1263     } else {
1264       result = TRUE;
1265       basesink->pad_mode = GST_ACTIVATE_PUSH;
1266     }
1267   } else {
1268     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1269       g_warning ("Internal GStreamer activation error!!!");
1270       result = FALSE;
1271     } else {
1272       result = gst_base_sink_deactivate (basesink, pad);
1273       basesink->pad_mode = GST_ACTIVATE_NONE;
1274     }
1275   }
1277   gst_object_unref (basesink);
1279   return result;
1282 /* this won't get called until we implement an activate function */
1283 static gboolean
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));
1291   if (active) {
1292     if (!basesink->can_activate_pull) {
1293       result = FALSE;
1294       basesink->pad_mode = GST_ACTIVATE_NONE;
1295     } else {
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");
1300         result = FALSE;
1301         basesink->pad_mode = GST_ACTIVATE_NONE;
1302       } else {
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;
1310           result =
1311               gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1312               pad);
1313           /* but if starting the thread fails, set it back */
1314           if (!result)
1315             basesink->pad_mode = GST_ACTIVATE_NONE;
1316         } else {
1317           GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1318           result = FALSE;
1319           basesink->pad_mode = GST_ACTIVATE_NONE;
1320         }
1321         gst_object_unref (peer);
1322       }
1323     }
1324   } else {
1325     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1326       g_warning ("Internal GStreamer activation error!!!");
1327       result = FALSE;
1328     } else {
1329       basesink->have_newsegment = FALSE;
1330       result = gst_base_sink_deactivate (basesink, pad);
1331       basesink->pad_mode = GST_ACTIVATE_NONE;
1332     }
1333   }
1335   gst_object_unref (basesink);
1337   return result;
1340 static gboolean
1341 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1343   GstPad *pad;
1344   GstBaseSink *basesink = GST_BASE_SINK (element);
1345   gboolean result;
1347   GST_LOCK (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);
1356   return result;
1359 static gboolean
1360 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1362   GstPad *peer;
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);
1368   }
1369   return res;
1372 static gboolean
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);
1382       break;
1383     case GST_QUERY_LATENCY:
1384       break;
1385     case GST_QUERY_JITTER:
1386       break;
1387     case GST_QUERY_RATE:
1388       //gst_query_set_rate (query, basesink->segment_rate);
1389       res = TRUE;
1390       break;
1391     case GST_QUERY_SEEKING:
1392       res = gst_base_sink_peer_query (basesink, query);
1393       break;
1394     case GST_QUERY_SEGMENT:
1395     {
1396       gst_query_set_segment (query, basesink->segment_rate,
1397           GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1398           basesink->segment_base);
1399       break;
1400     }
1401     case GST_QUERY_CONVERT:
1402       res = gst_base_sink_peer_query (basesink, query);
1403       break;
1404     case GST_QUERY_FORMATS:
1405       res = gst_base_sink_peer_query (basesink, query);
1406       break;
1407     default:
1408       break;
1409   }
1410   return res;
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:
1424       if (bclass->start)
1425         if (!bclass->start (basesink))
1426           goto start_failed;
1427       break;
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;
1442       break;
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
1456          * just emptied. */
1457         if (do_eos) {
1458           GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1459           gst_element_post_message (GST_ELEMENT (basesink),
1460               gst_message_new_eos (GST_OBJECT (basesink)));
1461         }
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 */
1470       } else {
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);
1477       }
1478       GST_PREROLL_UNLOCK (basesink->sinkpad);
1479       break;
1480     default:
1481       break;
1482   }
1484   {
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;
1490   }
1492   switch (transition) {
1493     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1494     {
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);
1504       }
1505       GST_UNLOCK (basesink);
1507       basesink->playing_async = FALSE;
1509       /* unlock any subclasses */
1510       if (bclass->unlock)
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;
1521       }
1522       GST_PREROLL_UNLOCK (basesink->sinkpad);
1523       break;
1524     }
1525     case GST_STATE_CHANGE_PAUSED_TO_READY:
1526       break;
1527     case GST_STATE_CHANGE_READY_TO_NULL:
1528       if (bclass->stop)
1529         if (!bclass->stop (basesink)) {
1530           GST_WARNING ("failed to stop");
1531         }
1532       break;
1533     default:
1534       break;
1535   }
1537   return ret;
1539   /* ERRORS */
1540 start_failed:
1541   {
1542     GST_DEBUG_OBJECT (basesink, "failed to start");
1543     return GST_STATE_CHANGE_FAILURE;
1544   }
1545 activate_failed:
1546   {
1547     GST_DEBUG_OBJECT (basesink,
1548         "element failed to change states -- activation problem?");
1549     return GST_STATE_CHANGE_FAILURE;
1550   }