gst/base/gstbasesink.c: Only error is an error.
[platform/upstream/gstreamer.git] / 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  */
21
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  */
38
39 #ifdef HAVE_CONFIG_H
40 #  include "config.h"
41 #endif
42
43 #include "gstbasesink.h"
44 #include <gst/gstmarshal.h>
45
46 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
47 #define GST_CAT_DEFAULT gst_base_sink_debug
48
49 /* BaseSink signals and properties */
50 enum
51 {
52   /* FILL ME */
53   SIGNAL_HANDOFF,
54   LAST_SIGNAL
55 };
56
57 #define DEFAULT_SIZE 1024
58 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
59 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
60
61 #define DEFAULT_SYNC TRUE
62
63 enum
64 {
65   PROP_0,
66   PROP_PREROLL_QUEUE_LEN,
67   PROP_SYNC
68 };
69
70 static GstElementClass *parent_class = NULL;
71
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);
76
77 GType
78 gst_base_sink_get_type (void)
79 {
80   static GType base_sink_type = 0;
81
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     };
94
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 }
100
101 static void gst_base_sink_set_clock (GstElement * element, GstClock * clock);
102
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);
107
108 static gboolean gst_base_sink_send_event (GstElement * element,
109     GstEvent * event);
110 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
111
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);
118
119 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
120     GstStateChange transition);
121
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);
132
133 static void
134 gst_base_sink_base_init (gpointer g_class)
135 {
136   GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
137       "basesink element");
138 }
139
140 static void
141 gst_base_sink_class_init (GstBaseSinkClass * klass)
142 {
143   GObjectClass *gobject_class;
144   GstElementClass *gstelement_class;
145
146   gobject_class = (GObjectClass *) klass;
147   gstelement_class = (GstElementClass *) klass;
148
149   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
150
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);
154
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));
165
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);
171
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);
176 }
177
178 static GstCaps *
179 gst_base_sink_pad_getcaps (GstPad * pad)
180 {
181   GstBaseSinkClass *bclass;
182   GstBaseSink *bsink;
183   GstCaps *caps = NULL;
184
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);
189
190   if (caps == NULL) {
191     GstPadTemplate *pad_template;
192
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);
200
201   return caps;
202 }
203
204 static gboolean
205 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
206 {
207   GstBaseSinkClass *bclass;
208   GstBaseSink *bsink;
209   gboolean res = FALSE;
210
211   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
212   bclass = GST_BASE_SINK_GET_CLASS (bsink);
213
214   if (bclass->set_caps)
215     res = bclass->set_caps (bsink, caps);
216
217   gst_object_unref (bsink);
218
219   return res;
220 }
221
222 static GstFlowReturn
223 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
224     GstCaps * caps, GstBuffer ** buf)
225 {
226   GstBaseSinkClass *bclass;
227   GstBaseSink *bsink;
228   GstFlowReturn result = GST_FLOW_OK;
229
230   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
231   bclass = GST_BASE_SINK_GET_CLASS (bsink);
232
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 */
237
238   gst_object_unref (bsink);
239
240   return result;
241 }
242
243 static void
244 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
245 {
246   GstPadTemplate *pad_template;
247
248   pad_template =
249       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
250   g_return_if_fail (pad_template != NULL);
251
252   basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
253
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);
271
272   basesink->pad_mode = GST_ACTIVATE_NONE;
273   GST_PAD_TASK (basesink->sinkpad) = NULL;
274   basesink->preroll_queue = g_queue_new ();
275
276   basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
277   basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
278
279   basesink->sync = DEFAULT_SYNC;
280
281   GST_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
282 }
283
284 static void
285 gst_base_sink_finalize (GObject * object)
286 {
287   GstBaseSink *basesink;
288
289   basesink = GST_BASE_SINK (object);
290
291   g_queue_free (basesink->preroll_queue);
292
293   G_OBJECT_CLASS (parent_class)->finalize (object);
294 }
295
296 static void
297 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
298 {
299   GstBaseSink *sink;
300
301   sink = GST_BASE_SINK (element);
302
303   sink->clock = clock;
304 }
305
306 static void
307 gst_base_sink_set_property (GObject * object, guint prop_id,
308     const GValue * value, GParamSpec * pspec)
309 {
310   GstBaseSink *sink = GST_BASE_SINK (object);
311
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   }
326 }
327
328 static void
329 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
330     GParamSpec * pspec)
331 {
332   GstBaseSink *sink = GST_BASE_SINK (object);
333
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);
347 }
348
349 static GstCaps *
350 gst_base_sink_get_caps (GstBaseSink * sink)
351 {
352   return NULL;
353 }
354
355 static gboolean
356 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
357 {
358   return TRUE;
359 }
360
361 static GstFlowReturn
362 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
363     GstCaps * caps, GstBuffer ** buf)
364 {
365   *buf = NULL;
366   return GST_FLOW_OK;
367 }
368
369 /* with PREROLL_LOCK */
370 static GstFlowReturn
371 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
372 {
373   GstMiniObject *obj;
374   GQueue *q = basesink->preroll_queue;
375   GstFlowReturn ret;
376
377   ret = GST_FLOW_OK;
378
379   if (q) {
380     GST_DEBUG_OBJECT (basesink, "emptying queue");
381     while ((obj = g_queue_pop_head (q))) {
382       gboolean is_buffer;
383
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);
402
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       }
411
412       GST_PREROLL_LOCK (pad);
413     }
414     GST_DEBUG_OBJECT (basesink, "queue empty");
415   }
416   return ret;
417 }
418
419 /* with PREROLL_LOCK */
420 static void
421 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
422 {
423   GstMiniObject *obj;
424   GQueue *q = basesink->preroll_queue;
425
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->preroll_queued = 0;
436   basesink->buffers_queued = 0;
437   basesink->events_queued = 0;
438   basesink->have_preroll = FALSE;
439   /* and signal any waiters now */
440   GST_PREROLL_SIGNAL (pad);
441 }
442
443 /* with STREAM_LOCK */
444 static GstFlowReturn
445 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
446     GstMiniObject * obj)
447 {
448   gint length;
449   gboolean have_event;
450
451   GST_PREROLL_LOCK (pad);
452   /* push object on the queue */
453   GST_DEBUG_OBJECT (basesink, "push %p on preroll_queue", obj);
454   g_queue_push_tail (basesink->preroll_queue, obj);
455
456   have_event = GST_IS_EVENT (obj);
457   if (have_event) {
458     GstEvent *event = GST_EVENT (obj);
459
460     switch (GST_EVENT_TYPE (obj)) {
461       case GST_EVENT_EOS:
462         basesink->preroll_queued++;
463         basesink->eos = TRUE;
464         break;
465       case GST_EVENT_NEWSEGMENT:
466       {
467         GstFormat format;
468         gint64 segment_start;
469         gint64 segment_stop;
470
471         /* the newsegment event is needed to bring the buffer timestamps to the
472          * stream time and to drop samples outside of the playback segment. */
473         gst_event_parse_newsegment (event, &basesink->segment_rate, &format,
474             &segment_start, &segment_stop, &basesink->segment_base);
475
476         basesink->have_newsegment = TRUE;
477
478         if (format != GST_FORMAT_TIME) {
479           GST_DEBUG_OBJECT (basesink,
480               "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
481               " -- %" G_GINT64_FORMAT ", base %" G_GINT64_FORMAT,
482               format, basesink->segment_start, basesink->segment_stop,
483               basesink->segment_base);
484
485           /* this means this sink will not be able to clip or drop samples
486            * and timestamps have to start from 0. */
487           basesink->segment_start = -1;
488           basesink->segment_stop = -1;
489           basesink->segment_base = -1;
490           goto done_newsegment;
491         }
492         /* check if we really have a new segment or the previous one is
493          * closed */
494         if (basesink->segment_start != segment_start) {
495           /* the new segment has to be aligned with the old segment.
496            * We first update the accumulated time of the previous
497            * segment. the accumulated time is used when syncing to the
498            * clock. A flush event sets the accumulated time back to 0
499            */
500           if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
501             basesink->segment_accum +=
502                 basesink->segment_stop - basesink->segment_start;
503           } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
504             /* else use last seen timestamp as segment stop */
505             basesink->segment_accum +=
506                 basesink->current_end - basesink->segment_start;
507           } else {
508             basesink->segment_accum = 0;
509           }
510         }
511
512         basesink->segment_start = segment_start;
513         basesink->segment_stop = segment_stop;
514
515         GST_DEBUG_OBJECT (basesink,
516             "received DISCONT %" GST_TIME_FORMAT " -- %"
517             GST_TIME_FORMAT ", base %" GST_TIME_FORMAT ", accum %"
518             GST_TIME_FORMAT,
519             GST_TIME_ARGS (basesink->segment_start),
520             GST_TIME_ARGS (basesink->segment_stop),
521             GST_TIME_ARGS (basesink->segment_base),
522             GST_TIME_ARGS (basesink->segment_accum));
523       done_newsegment:
524         break;
525       }
526       default:
527         break;
528     }
529     basesink->events_queued++;
530   } else {
531     GstBuffer *buf = GST_BUFFER (obj);
532
533     if (!basesink->have_newsegment) {
534       GST_ELEMENT_WARNING (basesink, STREAM, STOPPED,
535           ("Received buffer without a new-segment. Cannot sync to clock."),
536           ("Received buffer without a new-segment. Cannot sync to clock."));
537       basesink->have_newsegment = TRUE;
538       /* this means this sink will not be able to sync to the clock */
539       basesink->segment_start = -1;
540       basesink->segment_stop = -1;
541     }
542
543     /* check if the buffer needs to be dropped */
544     if (TRUE) {
545       GstClockTime start = -1, end = -1;
546
547       /* we don't use the subclassed method as it may not return
548        * valid values for our purpose here */
549       gst_base_sink_get_times (basesink, buf, &start, &end);
550
551       GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
552           ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
553           GST_TIME_ARGS (end));
554
555       /* need to drop if the timestamp is not between segment_start and
556        * segment_stop. we check if the complete sample is outside of the
557        * range since the sink might be able to clip the sample. */
558       if (GST_CLOCK_TIME_IS_VALID (end) &&
559           GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
560         if (end <= basesink->segment_start) {
561           GST_DEBUG_OBJECT (basesink,
562               "buffer end %" GST_TIME_FORMAT " <= segment start %"
563               GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
564               GST_TIME_ARGS (basesink->segment_start));
565           goto dropping;
566         }
567       }
568       if (GST_CLOCK_TIME_IS_VALID (start) &&
569           GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
570         if (basesink->segment_stop <= start) {
571           GST_DEBUG_OBJECT (basesink,
572               "buffer start %" GST_TIME_FORMAT " >= segment stop %"
573               GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
574               GST_TIME_ARGS (basesink->segment_stop));
575           goto dropping;
576         }
577       }
578     }
579     basesink->preroll_queued++;
580     basesink->buffers_queued++;
581   }
582   GST_DEBUG_OBJECT (basesink,
583       "now %d preroll, %d buffers, %d events on queue",
584       basesink->preroll_queued,
585       basesink->buffers_queued, basesink->events_queued);
586
587   if (basesink->playing_async)
588     goto playing_async;
589
590   /* check if we are prerolling */
591   if (!basesink->need_preroll)
592     goto no_preroll;
593
594   /* there is a buffer queued */
595   if (basesink->buffers_queued == 1) {
596     GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
597
598     /* if it's a buffer, we need to call the preroll method */
599     if (GST_IS_BUFFER (obj)) {
600       GstBaseSinkClass *bclass;
601       GstFlowReturn pres;
602
603       bclass = GST_BASE_SINK_GET_CLASS (basesink);
604       if (bclass->preroll)
605         if ((pres =
606                 bclass->preroll (basesink, GST_BUFFER (obj))) != GST_FLOW_OK)
607           goto preroll_failed;
608     }
609   }
610   length = basesink->preroll_queued;
611   GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
612
613   if (length == 1) {
614     gint t;
615
616     basesink->have_preroll = TRUE;
617     /* we are prerolling */
618     GST_PREROLL_UNLOCK (pad);
619
620     /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
621      * inside the STREAM_LOCK */
622     t = GST_STREAM_UNLOCK_FULL (pad);
623     GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
624     if (t <= 0) {
625       GST_WARNING ("STREAM_LOCK should have been locked !!");
626       g_warning ("STREAM_LOCK should have been locked !!");
627     }
628
629     /* now we commit our state */
630     GST_STATE_LOCK (basesink);
631     GST_DEBUG_OBJECT (basesink, "commit state");
632     gst_element_commit_state (GST_ELEMENT (basesink));
633     GST_STATE_UNLOCK (basesink);
634
635     /* reacquire stream lock, pad could be flushing now */
636     /* FIXME in glib, if t==0, the lock is still taken... hmmm */
637     if (t > 0)
638       GST_STREAM_LOCK_FULL (pad, t);
639
640     /* and wait if needed */
641     GST_PREROLL_LOCK (pad);
642
643     GST_LOCK (pad);
644     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
645       goto flushing;
646     GST_UNLOCK (pad);
647
648     /* it is possible that the application set the state to PLAYING
649      * now in which case we don't need to block anymore. */
650     if (!basesink->need_preroll)
651       goto no_preroll;
652
653
654     length = basesink->preroll_queued;
655
656     /* FIXME: a pad probe could have made us lose the buffer, according
657      * to one of the python tests */
658     if (length == 0) {
659       GST_ERROR_OBJECT (basesink,
660           "preroll_queued dropped from 1 to 0 while committing state change");
661     }
662     g_assert (length <= 1);
663   }
664
665   /* see if we need to block now. We cannot block on events, only
666    * on buffers, the reason is that events can be sent from the
667    * application thread and we don't want to block there. */
668   if (length > basesink->preroll_queue_max_len && !have_event) {
669     /* block until the state changes, or we get a flush, or something */
670     GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
671     GST_PREROLL_WAIT (pad);
672     GST_DEBUG_OBJECT (basesink, "done preroll");
673   }
674   GST_LOCK (pad);
675   if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
676     goto flushing;
677   GST_UNLOCK (pad);
678
679   GST_PREROLL_UNLOCK (pad);
680
681   return GST_FLOW_OK;
682
683 no_preroll:
684   {
685     GstFlowReturn ret;
686
687     GST_DEBUG_OBJECT (basesink, "no preroll needed");
688     /* maybe it was another sink that blocked in preroll, need to check for
689        buffers to drain */
690     basesink->have_preroll = FALSE;
691     ret = gst_base_sink_preroll_queue_empty (basesink, pad);
692     GST_PREROLL_UNLOCK (pad);
693
694     return ret;
695   }
696 dropping:
697   {
698     GstBuffer *buf;
699
700     buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
701
702     gst_buffer_unref (buf);
703     GST_PREROLL_UNLOCK (pad);
704
705     return GST_FLOW_OK;
706   }
707 playing_async:
708   {
709     GstFlowReturn ret;
710     gint t;
711
712     basesink->have_preroll = FALSE;
713     basesink->playing_async = FALSE;
714
715     /* handle buffer first */
716     ret = gst_base_sink_preroll_queue_empty (basesink, pad);
717
718     /* unroll locks, commit state, reacquire stream lock */
719     GST_PREROLL_UNLOCK (pad);
720     t = GST_STREAM_UNLOCK_FULL (pad);
721     GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
722     if (t <= 0) {
723       GST_WARNING ("STREAM_LOCK should have been locked !!");
724       g_warning ("STREAM_LOCK should have been locked !!");
725     }
726     GST_STATE_LOCK (basesink);
727     GST_DEBUG_OBJECT (basesink, "commit state");
728     gst_element_commit_state (GST_ELEMENT (basesink));
729     GST_STATE_UNLOCK (basesink);
730     if (t > 0)
731       GST_STREAM_LOCK_FULL (pad, t);
732
733     return ret;
734   }
735 flushing:
736   {
737     GST_UNLOCK (pad);
738     gst_base_sink_preroll_queue_flush (basesink, pad);
739     GST_PREROLL_UNLOCK (pad);
740     GST_DEBUG_OBJECT (basesink, "pad is flushing");
741     return GST_FLOW_WRONG_STATE;
742   }
743 preroll_failed:
744   {
745     gint t;
746
747     GST_DEBUG_OBJECT (basesink, "preroll failed");
748     gst_base_sink_preroll_queue_flush (basesink, pad);
749     GST_PREROLL_UNLOCK (pad);
750
751     /* have to release STREAM_LOCK as we cannot take the STATE_LOCK
752      * inside the STREAM_LOCK */
753     t = GST_STREAM_UNLOCK_FULL (pad);
754     GST_DEBUG_OBJECT (basesink, "released stream lock %d times", t);
755     if (t <= 0) {
756       GST_WARNING ("STREAM_LOCK should have been locked !!");
757       g_warning ("STREAM_LOCK should have been locked !!");
758     }
759
760     /* now we abort our state */
761     GST_STATE_LOCK (basesink);
762     GST_DEBUG_OBJECT (basesink, "abort state");
763     gst_element_abort_state (GST_ELEMENT (basesink));
764     GST_STATE_UNLOCK (basesink);
765
766     /* reacquire stream lock, pad could be flushing now */
767     if (t > 0)
768       GST_STREAM_LOCK_FULL (pad, t);
769
770     return GST_FLOW_ERROR;
771   }
772 }
773
774 static gboolean
775 gst_base_sink_event (GstPad * pad, GstEvent * event)
776 {
777   GstBaseSink *basesink;
778   gboolean result = TRUE;
779   GstBaseSinkClass *bclass;
780
781   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
782
783   bclass = GST_BASE_SINK_GET_CLASS (basesink);
784
785   GST_DEBUG_OBJECT (basesink, "event %p", event);
786
787   switch (GST_EVENT_TYPE (event)) {
788     case GST_EVENT_EOS:
789     {
790       GstFlowReturn ret;
791
792       GST_STREAM_LOCK (pad);
793       /* EOS also finishes the preroll */
794       ret =
795           gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
796       GST_STREAM_UNLOCK (pad);
797       break;
798     }
799     case GST_EVENT_NEWSEGMENT:
800     {
801       GstFlowReturn ret;
802
803       GST_STREAM_LOCK (pad);
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_FLUSH_START:
810       /* make sure we are not blocked on the clock also clear any pending
811        * eos state. */
812       if (bclass->event)
813         bclass->event (basesink, event);
814
815       GST_PREROLL_LOCK (pad);
816       /* we need preroll after the flush */
817       GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
818       basesink->need_preroll = TRUE;
819       /* unlock from a possible state change/preroll */
820       gst_base_sink_preroll_queue_flush (basesink, pad);
821
822       GST_LOCK (basesink);
823       if (basesink->clock_id) {
824         gst_clock_id_unschedule (basesink->clock_id);
825       }
826       GST_UNLOCK (basesink);
827       GST_PREROLL_UNLOCK (pad);
828
829       /* and we need to commit our state again on the next
830        * prerolled buffer */
831       GST_STATE_LOCK (basesink);
832       GST_STREAM_LOCK (pad);
833       gst_element_lost_state (GST_ELEMENT (basesink));
834       GST_STREAM_UNLOCK (pad);
835       GST_STATE_UNLOCK (basesink);
836       GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
837       gst_event_unref (event);
838       break;
839     case GST_EVENT_FLUSH_STOP:
840       if (bclass->event)
841         bclass->event (basesink, event);
842
843       /* now we are completely unblocked and the _chain method
844        * will return */
845       GST_STREAM_LOCK (pad);
846       /* we need new segment info after the flush. */
847       basesink->segment_start = -1;
848       basesink->segment_stop = -1;
849       basesink->current_start = -1;
850       basesink->current_end = -1;
851       GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
852           GST_TIME_ARGS (basesink->segment_accum));
853       basesink->segment_accum = 0;
854       GST_STREAM_UNLOCK (pad);
855
856       GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
857       gst_event_unref (event);
858       break;
859     default:
860       gst_event_unref (event);
861       break;
862   }
863   gst_object_unref (basesink);
864
865   return result;
866 }
867
868 /* default implementation to calculate the start and end
869  * timestamps on a buffer, subclasses can override
870  */
871 static void
872 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
873     GstClockTime * start, GstClockTime * end)
874 {
875   GstClockTime timestamp, duration;
876
877   timestamp = GST_BUFFER_TIMESTAMP (buffer);
878   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
879
880     /* get duration to calculate end time */
881     duration = GST_BUFFER_DURATION (buffer);
882     if (GST_CLOCK_TIME_IS_VALID (duration)) {
883       *end = timestamp + duration;
884     }
885     *start = timestamp;
886   }
887 }
888
889 /* with STREAM_LOCK and LOCK*/
890 static GstClockReturn
891 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
892 {
893   GstClockReturn ret;
894
895   /* clock_id should be NULL outside of this function */
896   g_assert (basesink->clock_id == NULL);
897   g_assert (GST_CLOCK_TIME_IS_VALID (time));
898
899   basesink->clock_id = gst_clock_new_single_shot_id (basesink->clock, time);
900
901   /* release the object lock while waiting */
902   GST_UNLOCK (basesink);
903   ret = gst_clock_id_wait (basesink->clock_id, NULL);
904   GST_LOCK (basesink);
905
906   gst_clock_id_unref (basesink->clock_id);
907   basesink->clock_id = NULL;
908
909   return ret;
910 }
911
912 /* perform synchronisation on a buffer
913  * 
914  * 1) check if we have a clock, if not, do nothing
915  * 2) calculate the start and end time of the buffer
916  * 3) create a single shot notification to wait on
917  *    the clock, save the entry so we can unlock it
918  * 4) wait on the clock, this blocks
919  * 5) unref the clockid again
920  */
921 static gboolean
922 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
923 {
924   gboolean result = TRUE;
925   GstClockTime start, end;
926   GstClockTimeDiff stream_start, stream_end;
927   GstBaseSinkClass *bclass;
928   gboolean start_valid, end_valid;
929
930   bclass = GST_BASE_SINK_GET_CLASS (basesink);
931
932   start = end = -1;
933   if (bclass->get_times)
934     bclass->get_times (basesink, buffer, &start, &end);
935
936   start_valid = GST_CLOCK_TIME_IS_VALID (start);
937   end_valid = GST_CLOCK_TIME_IS_VALID (start);
938
939   GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
940       ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
941
942   /* if we don't have a timestamp, we don't sync */
943   if (!start_valid)
944     goto done;
945
946   /* save last times seen. */
947   basesink->current_start = start;
948   if (end_valid)
949     basesink->current_end = end;
950   else
951     basesink->current_end = start;
952
953   if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
954     /* check if not outside of the segment range, start is
955      * always valid here. */
956     if (start > basesink->segment_stop)
957       goto out_of_segment;
958   }
959
960   /* bring timestamp to stream time using last segment offset. */
961   if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
962     /* check if not outside of the segment range */
963     if (end_valid && end < basesink->segment_start)
964       goto out_of_segment;
965
966     stream_start = (gint64) start - basesink->segment_start;
967     stream_end = (gint64) end - basesink->segment_start;
968   } else {
969     stream_start = (gint64) start;
970     stream_end = (gint64) end;
971   }
972
973   stream_start += basesink->segment_accum;
974   if (end_valid)
975     stream_end += basesink->segment_accum;
976
977   /* now do clocking */
978   if (basesink->clock && basesink->sync) {
979     GstClockReturn ret;
980     GstClockTime base_time;
981
982     GST_LOCK (basesink);
983
984     base_time = GST_ELEMENT (basesink)->base_time;
985
986     GST_LOG_OBJECT (basesink,
987         "waiting for clock, base time %" GST_TIME_FORMAT,
988         GST_TIME_ARGS (base_time));
989
990     /* also save end_time of this buffer so that we can wait
991      * to signal EOS */
992     if (end_valid)
993       basesink->end_time = stream_end + base_time;
994     else
995       basesink->end_time = GST_CLOCK_TIME_NONE;
996
997     ret = gst_base_sink_wait (basesink, stream_start + base_time);
998
999     GST_UNLOCK (basesink);
1000
1001     GST_LOG_OBJECT (basesink, "clock entry done: %d", ret);
1002     if (ret == GST_CLOCK_UNSCHEDULED)
1003       result = FALSE;
1004   }
1005
1006 done:
1007   return result;
1008
1009 out_of_segment:
1010   {
1011     GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1012     return FALSE;
1013   }
1014 }
1015
1016
1017 /* handle an event
1018  *
1019  * 2) render the event
1020  * 3) unref the event
1021  */
1022 static inline gboolean
1023 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1024 {
1025   GstBaseSinkClass *bclass;
1026   gboolean ret;
1027
1028   switch (GST_EVENT_TYPE (event)) {
1029     case GST_EVENT_EOS:
1030       GST_LOCK (basesink);
1031       if (basesink->clock) {
1032         /* wait for last buffer to finish if we have a valid end time */
1033         if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1034           gst_base_sink_wait (basesink, basesink->end_time);
1035           basesink->end_time = GST_CLOCK_TIME_NONE;
1036         }
1037       }
1038       GST_UNLOCK (basesink);
1039       break;
1040     default:
1041       break;
1042   }
1043
1044   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1045   if (bclass->event)
1046     ret = bclass->event (basesink, event);
1047   else
1048     ret = TRUE;
1049
1050   switch (GST_EVENT_TYPE (event)) {
1051     case GST_EVENT_EOS:
1052       GST_PREROLL_LOCK (basesink->sinkpad);
1053       /* if we are still EOS, we can post the EOS message */
1054       if (basesink->eos) {
1055         /* ok, now we can post the message */
1056         GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1057         gst_element_post_message (GST_ELEMENT (basesink),
1058             gst_message_new_eos (GST_OBJECT (basesink)));
1059       }
1060       GST_PREROLL_UNLOCK (basesink->sinkpad);
1061       break;
1062     default:
1063       break;
1064   }
1065
1066   GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1067   gst_event_unref (event);
1068
1069   return ret;
1070 }
1071
1072 /* handle a buffer
1073  *
1074  * 1) first sync on the buffer
1075  * 2) render the buffer
1076  * 3) unref the buffer
1077  */
1078 static inline GstFlowReturn
1079 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1080 {
1081   GstFlowReturn ret = GST_FLOW_OK;
1082   gboolean render;
1083
1084   render = gst_base_sink_do_sync (basesink, buf);
1085
1086   if (render) {
1087     GstBaseSinkClass *bclass;
1088
1089     bclass = GST_BASE_SINK_GET_CLASS (basesink);
1090     if (bclass->render)
1091       ret = bclass->render (basesink, buf);
1092   }
1093
1094   GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1095   gst_buffer_unref (buf);
1096
1097   return ret;
1098 }
1099
1100 static GstFlowReturn
1101 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1102 {
1103   GstBaseSink *basesink;
1104   GstFlowReturn result;
1105
1106   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1107
1108   if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1109     GST_LOCK (pad);
1110     g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1111         GST_DEBUG_PAD_NAME (pad));
1112     GST_UNLOCK (pad);
1113     result = GST_FLOW_UNEXPECTED;
1114     goto done;
1115   }
1116
1117   result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1118
1119 done:
1120   gst_object_unref (basesink);
1121
1122   return result;
1123 }
1124
1125 static void
1126 gst_base_sink_loop (GstPad * pad)
1127 {
1128   GstBaseSink *basesink;
1129   GstBuffer *buf = NULL;
1130   GstFlowReturn result;
1131
1132   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1133
1134   g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1135
1136   result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1137   if (result != GST_FLOW_OK)
1138     goto paused;
1139
1140   result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1141   if (result != GST_FLOW_OK)
1142     goto paused;
1143
1144   gst_object_unref (basesink);
1145
1146   /* default */
1147   return;
1148
1149 paused:
1150   {
1151     gst_base_sink_event (pad, gst_event_new_eos ());
1152     gst_object_unref (basesink);
1153     gst_pad_pause_task (pad);
1154     return;
1155   }
1156 }
1157
1158 static gboolean
1159 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1160 {
1161   gboolean result = FALSE;
1162   GstBaseSinkClass *bclass;
1163
1164   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1165
1166   /* step 1, unblock clock sync (if any) or any other blocking thing */
1167   GST_PREROLL_LOCK (pad);
1168   GST_LOCK (basesink);
1169   if (basesink->clock_id) {
1170     gst_clock_id_unschedule (basesink->clock_id);
1171   }
1172   GST_UNLOCK (basesink);
1173
1174   /* unlock any subclasses */
1175   if (bclass->unlock)
1176     bclass->unlock (basesink);
1177
1178   /* flush out the data thread if it's locked in finish_preroll */
1179   GST_DEBUG_OBJECT (basesink,
1180       "flushing out data thread, need preroll to FALSE");
1181   basesink->need_preroll = FALSE;
1182   gst_base_sink_preroll_queue_flush (basesink, pad);
1183   GST_PREROLL_SIGNAL (pad);
1184   GST_PREROLL_UNLOCK (pad);
1185
1186   /* step 2, make sure streaming finishes */
1187   result = gst_pad_stop_task (pad);
1188
1189   return result;
1190 }
1191
1192 static gboolean
1193 gst_base_sink_activate (GstPad * pad)
1194 {
1195   gboolean result = FALSE;
1196   GstBaseSink *basesink;
1197
1198   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1199
1200   GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1201
1202   if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1203       && gst_pad_activate_pull (pad, TRUE)) {
1204     GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1205     result = TRUE;
1206   } else {
1207     GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1208     if (gst_pad_activate_push (pad, TRUE)) {
1209       GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1210       result = TRUE;
1211     }
1212   }
1213
1214   if (!result) {
1215     GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1216   }
1217
1218   gst_object_unref (basesink);
1219
1220   return result;
1221 }
1222
1223 static gboolean
1224 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1225 {
1226   gboolean result;
1227   GstBaseSink *basesink;
1228
1229   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1230
1231   if (active) {
1232     if (!basesink->can_activate_push) {
1233       result = FALSE;
1234       basesink->pad_mode = GST_ACTIVATE_NONE;
1235     } else {
1236       result = TRUE;
1237       basesink->pad_mode = GST_ACTIVATE_PUSH;
1238     }
1239   } else {
1240     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1241       g_warning ("Internal GStreamer activation error!!!");
1242       result = FALSE;
1243     } else {
1244       result = gst_base_sink_deactivate (basesink, pad);
1245       basesink->pad_mode = GST_ACTIVATE_NONE;
1246     }
1247   }
1248
1249   gst_object_unref (basesink);
1250
1251   return result;
1252 }
1253
1254 /* this won't get called until we implement an activate function */
1255 static gboolean
1256 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1257 {
1258   gboolean result = FALSE;
1259   GstBaseSink *basesink;
1260
1261   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1262
1263   if (active) {
1264     if (!basesink->can_activate_pull) {
1265       result = FALSE;
1266       basesink->pad_mode = GST_ACTIVATE_NONE;
1267     } else {
1268       GstPad *peer = gst_pad_get_peer (pad);
1269
1270       if (G_UNLIKELY (peer == NULL)) {
1271         g_warning ("Trying to activate pad in pull mode, but no peer");
1272         result = FALSE;
1273         basesink->pad_mode = GST_ACTIVATE_NONE;
1274       } else {
1275         if (gst_pad_activate_pull (peer, TRUE)) {
1276           basesink->have_newsegment = TRUE;
1277           basesink->segment_start = basesink->segment_stop = 0;
1278
1279           /* set the pad mode before starting the task so that it's in the
1280              correct state for the new thread... */
1281           basesink->pad_mode = GST_ACTIVATE_PULL;
1282           result =
1283               gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1284               pad);
1285           /* but if starting the thread fails, set it back */
1286           if (!result)
1287             basesink->pad_mode = GST_ACTIVATE_NONE;
1288         } else {
1289           GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1290           result = FALSE;
1291           basesink->pad_mode = GST_ACTIVATE_NONE;
1292         }
1293         gst_object_unref (peer);
1294       }
1295     }
1296   } else {
1297     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1298       g_warning ("Internal GStreamer activation error!!!");
1299       result = FALSE;
1300     } else {
1301       basesink->have_newsegment = FALSE;
1302       result = gst_base_sink_deactivate (basesink, pad);
1303       basesink->pad_mode = GST_ACTIVATE_NONE;
1304     }
1305   }
1306
1307   gst_object_unref (basesink);
1308
1309   return result;
1310 }
1311
1312 static gboolean
1313 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1314 {
1315   GstPad *pad;
1316   GstBaseSink *basesink = GST_BASE_SINK (element);
1317   gboolean result;
1318
1319   GST_LOCK (element);
1320   pad = basesink->sinkpad;
1321   gst_object_ref (pad);
1322   GST_UNLOCK (element);
1323
1324   result = gst_pad_push_event (pad, event);
1325
1326   gst_object_unref (pad);
1327
1328   return result;
1329 }
1330
1331 static gboolean
1332 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1333 {
1334   GstPad *peer;
1335   gboolean res = FALSE;
1336
1337   if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1338     res = gst_pad_query (peer, query);
1339     gst_object_unref (peer);
1340   }
1341   return res;
1342 }
1343
1344 static gboolean
1345 gst_base_sink_query (GstElement * element, GstQuery * query)
1346 {
1347   gboolean res = FALSE;
1348
1349   GstBaseSink *basesink = GST_BASE_SINK (element);
1350
1351   switch (GST_QUERY_TYPE (query)) {
1352     case GST_QUERY_POSITION:
1353       res = gst_base_sink_peer_query (basesink, query);
1354       break;
1355     case GST_QUERY_LATENCY:
1356       break;
1357     case GST_QUERY_JITTER:
1358       break;
1359     case GST_QUERY_RATE:
1360       //gst_query_set_rate (query, basesink->segment_rate);
1361       res = TRUE;
1362       break;
1363     case GST_QUERY_SEEKING:
1364       res = gst_base_sink_peer_query (basesink, query);
1365       break;
1366     case GST_QUERY_SEGMENT:
1367     {
1368       gst_query_set_segment (query, basesink->segment_rate,
1369           GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop,
1370           basesink->segment_base);
1371       break;
1372     }
1373     case GST_QUERY_CONVERT:
1374       res = gst_base_sink_peer_query (basesink, query);
1375       break;
1376     case GST_QUERY_FORMATS:
1377       res = gst_base_sink_peer_query (basesink, query);
1378       break;
1379     default:
1380       break;
1381   }
1382   return res;
1383 }
1384
1385 static GstStateChangeReturn
1386 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1387 {
1388   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1389   GstBaseSink *basesink = GST_BASE_SINK (element);
1390   GstBaseSinkClass *bclass;
1391
1392   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1393
1394   switch (transition) {
1395     case GST_STATE_CHANGE_NULL_TO_READY:
1396       if (bclass->start)
1397         if (!bclass->start (basesink))
1398           goto start_failed;
1399       break;
1400     case GST_STATE_CHANGE_READY_TO_PAUSED:
1401       /* need to complete preroll before this state change completes, there
1402        * is no data flow in READY so we can safely assume we need to preroll. */
1403       basesink->offset = 0;
1404       GST_PREROLL_LOCK (basesink->sinkpad);
1405       basesink->have_preroll = FALSE;
1406       GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1407       basesink->need_preroll = TRUE;
1408       GST_PREROLL_UNLOCK (basesink->sinkpad);
1409       basesink->have_newsegment = FALSE;
1410       basesink->segment_rate = 1.0;
1411       basesink->segment_start = 0;
1412       basesink->segment_stop = 0;
1413       ret = GST_STATE_CHANGE_ASYNC;
1414       break;
1415     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1416     {
1417       GST_PREROLL_LOCK (basesink->sinkpad);
1418       /* if we have EOS, we should empty the queue now as there will
1419        * be no more data received in the chain function.
1420        * FIXME, this could block the state change function too long when
1421        * we are pushing and syncing the buffers, better start a new
1422        * thread to do this. */
1423       if (basesink->eos) {
1424         gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1425       } else if (!basesink->have_preroll) {
1426         /* don't need preroll, but do queue a commit_state */
1427         GST_DEBUG_OBJECT (basesink,
1428             "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1429         basesink->need_preroll = FALSE;
1430         basesink->playing_async = TRUE;
1431         ret = GST_STATE_CHANGE_ASYNC;
1432         /* we know it's not waiting, no need to signal */
1433       } else {
1434         /* don't need the preroll anymore */
1435         basesink->need_preroll = FALSE;
1436         GST_DEBUG_OBJECT (basesink,
1437             "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1438         /* now let it play */
1439         GST_PREROLL_SIGNAL (basesink->sinkpad);
1440       }
1441       GST_PREROLL_UNLOCK (basesink->sinkpad);
1442       break;
1443     }
1444     default:
1445       break;
1446   }
1447
1448   {
1449     GstStateChangeReturn bret;
1450
1451     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1452     if (bret == GST_STATE_CHANGE_FAILURE)
1453       goto activate_failed;
1454   }
1455
1456   switch (transition) {
1457     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1458     {
1459       GstBaseSinkClass *bclass;
1460
1461       bclass = GST_BASE_SINK_GET_CLASS (basesink);
1462
1463       GST_PREROLL_LOCK (basesink->sinkpad);
1464       GST_LOCK (basesink);
1465       /* unlock clock wait if any */
1466       if (basesink->clock_id) {
1467         gst_clock_id_unschedule (basesink->clock_id);
1468       }
1469       GST_UNLOCK (basesink);
1470
1471       basesink->playing_async = FALSE;
1472
1473       /* unlock any subclasses */
1474       if (bclass->unlock)
1475         bclass->unlock (basesink);
1476
1477       /* if we don't have a preroll buffer and we have not received EOS,
1478        * we need to wait for a preroll */
1479       GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1480           basesink->have_preroll, basesink->eos);
1481       if (!basesink->have_preroll && !basesink->eos) {
1482         GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1483         basesink->need_preroll = TRUE;
1484         ret = GST_STATE_CHANGE_ASYNC;
1485       }
1486       GST_PREROLL_UNLOCK (basesink->sinkpad);
1487       break;
1488     }
1489     case GST_STATE_CHANGE_PAUSED_TO_READY:
1490       break;
1491     case GST_STATE_CHANGE_READY_TO_NULL:
1492       if (bclass->stop)
1493         if (!bclass->stop (basesink)) {
1494           GST_WARNING ("failed to stop");
1495         }
1496       break;
1497     default:
1498       break;
1499   }
1500
1501   return ret;
1502
1503   /* ERRORS */
1504 start_failed:
1505   {
1506     GST_DEBUG_OBJECT (basesink, "failed to start");
1507     return GST_STATE_CHANGE_FAILURE;
1508   }
1509 activate_failed:
1510   {
1511     GST_DEBUG_OBJECT (basesink,
1512         "element failed to change states -- activation problem?");
1513     return GST_STATE_CHANGE_FAILURE;
1514   }
1515 }