gst/base/gstbasesink.c: Add the beginning of docs here -- have to work on something...
[platform/upstream/gstreamer.git] / gst / base / gstbasesink.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstbasesink.c: Base class for sink elements
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: #GstBaseTransform, #GstBaseSource
26  *
27  * GstBaseSink is the base class for sink elements in GStreamer, such as
28  * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29  * simplified interface to plugin writers. GstBaseSink handles many details for
30  * you, for example preroll, clock synchronization, state changes, activation in
31  * push or pull mode, and queries. In most cases, when writing sink elements,
32  * there is no need to implement class methods from #GstElement or to set
33  * functions on pads, because the GstBaseSink infrastructure is sufficient.
34  *
35  * There is only support in GstBaseSink for one sink pad, which should be named
36  * "sink". A sink implementation (subclass of GstBaseSink) should install a pad
37  * template in its base_init function, like so:
38  * <programlisting>
39  * static void
40  * my_element_base_init (gpointer g_class)
41  * {
42  *   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
43  *   
44  *   // sinktemplate should be a #GstStaticPadTemplate with direction
45  *   // #GST_PAD_SINK and name "sink"
46  *   gst_element_class_add_pad_template (gstelement_class,
47  *       gst_static_pad_template_get (&amp;sinktemplate));
48  *   // see #GstElementDetails
49  *   gst_element_class_set_details (gstelement_class, &amp;details);
50  * }
51  * </programlisting>
52  *
53  * The one method which all subclasses of GstBaseSink must implement is
54  * GstBaseSink::render. This method will be called...
55  * 
56  * preroll()
57  *
58  * event(): mostly useful for file-like sinks (seeking or flushing)
59  *
60  * get_caps/set_caps/buffer_alloc
61  *
62  * start/stop for resource allocation
63  *
64  * unlock if you block on an fd, for example
65  *
66  * get_times i'm sure is for something :P
67  *
68  * provide example of textsink
69  *
70  * admonishment not to try to implement your own sink with prerolling...
71  *
72  * extending via subclassing, setting pad functions, gstelement vmethods.
73  */
74
75 #ifdef HAVE_CONFIG_H
76 #  include "config.h"
77 #endif
78
79 #include "gstbasesink.h"
80 #include <gst/gstmarshal.h>
81 #include <gst/gst-i18n-lib.h>
82
83 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
84 #define GST_CAT_DEFAULT gst_base_sink_debug
85
86 /* BaseSink signals and properties */
87 enum
88 {
89   /* FILL ME */
90   SIGNAL_HANDOFF,
91   LAST_SIGNAL
92 };
93
94 #define DEFAULT_SIZE 1024
95 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
96 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
97
98 #define DEFAULT_SYNC TRUE
99
100 enum
101 {
102   PROP_0,
103   PROP_PREROLL_QUEUE_LEN,
104   PROP_SYNC
105 };
106
107 static GstElementClass *parent_class = NULL;
108
109 static void gst_base_sink_base_init (gpointer g_class);
110 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
111 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
112 static void gst_base_sink_finalize (GObject * object);
113
114 GType
115 gst_base_sink_get_type (void)
116 {
117   static GType base_sink_type = 0;
118
119   if (!base_sink_type) {
120     static const GTypeInfo base_sink_info = {
121       sizeof (GstBaseSinkClass),
122       (GBaseInitFunc) gst_base_sink_base_init,
123       NULL,
124       (GClassInitFunc) gst_base_sink_class_init,
125       NULL,
126       NULL,
127       sizeof (GstBaseSink),
128       0,
129       (GInstanceInitFunc) gst_base_sink_init,
130     };
131
132     base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
133         "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
134   }
135   return base_sink_type;
136 }
137
138 static void gst_base_sink_set_clock (GstElement * element, GstClock * clock);
139
140 static void gst_base_sink_set_property (GObject * object, guint prop_id,
141     const GValue * value, GParamSpec * pspec);
142 static void gst_base_sink_get_property (GObject * object, guint prop_id,
143     GValue * value, GParamSpec * pspec);
144
145 static gboolean gst_base_sink_send_event (GstElement * element,
146     GstEvent * event);
147 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
148
149 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
150 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
151 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
152     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
153 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
154     GstClockTime * start, GstClockTime * end);
155
156 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
157     GstStateChange transition);
158
159 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
160 static void gst_base_sink_loop (GstPad * pad);
161 static gboolean gst_base_sink_activate (GstPad * pad);
162 static gboolean gst_base_sink_activate_push (GstPad * pad, gboolean active);
163 static gboolean gst_base_sink_activate_pull (GstPad * pad, gboolean active);
164 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
165 static inline GstFlowReturn gst_base_sink_handle_buffer (GstBaseSink * basesink,
166     GstBuffer * buf);
167 static inline gboolean gst_base_sink_handle_event (GstBaseSink * basesink,
168     GstEvent * event);
169
170 static void
171 gst_base_sink_base_init (gpointer g_class)
172 {
173   GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
174       "basesink element");
175 }
176
177 static void
178 gst_base_sink_class_init (GstBaseSinkClass * klass)
179 {
180   GObjectClass *gobject_class;
181   GstElementClass *gstelement_class;
182
183   gobject_class = (GObjectClass *) klass;
184   gstelement_class = (GstElementClass *) klass;
185
186   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
187
188   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
189   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
190   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
191
192   /* FIXME, this next value should be configured using an event from the
193    * upstream element */
194   g_object_class_install_property (G_OBJECT_CLASS (klass),
195       PROP_PREROLL_QUEUE_LEN,
196       g_param_spec_uint ("preroll-queue-len", "preroll-queue-len",
197           "Number of buffers to queue during preroll", 0, G_MAXUINT, 0,
198           G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
199   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SYNC,
200       g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
201           G_PARAM_READWRITE));
202
203   gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_base_sink_set_clock);
204   gstelement_class->change_state =
205       GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
206   gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
207   gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
208
209   klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
210   klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
211   klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
212   klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
213 }
214
215 static GstCaps *
216 gst_base_sink_pad_getcaps (GstPad * pad)
217 {
218   GstBaseSinkClass *bclass;
219   GstBaseSink *bsink;
220   GstCaps *caps = NULL;
221
222   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
223   bclass = GST_BASE_SINK_GET_CLASS (bsink);
224   if (bclass->get_caps)
225     caps = bclass->get_caps (bsink);
226
227   if (caps == NULL) {
228     GstPadTemplate *pad_template;
229
230     pad_template =
231         gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
232     if (pad_template != NULL) {
233       caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
234     }
235   }
236   gst_object_unref (bsink);
237
238   return caps;
239 }
240
241 static gboolean
242 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
243 {
244   GstBaseSinkClass *bclass;
245   GstBaseSink *bsink;
246   gboolean res = FALSE;
247
248   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
249   bclass = GST_BASE_SINK_GET_CLASS (bsink);
250
251   if (bclass->set_caps)
252     res = bclass->set_caps (bsink, caps);
253
254   gst_object_unref (bsink);
255
256   return res;
257 }
258
259 static GstFlowReturn
260 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
261     GstCaps * caps, GstBuffer ** buf)
262 {
263   GstBaseSinkClass *bclass;
264   GstBaseSink *bsink;
265   GstFlowReturn result = GST_FLOW_OK;
266
267   bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
268   bclass = GST_BASE_SINK_GET_CLASS (bsink);
269
270   if (bclass->buffer_alloc)
271     result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
272   else
273     *buf = NULL;                /* fallback in gstpad.c will allocate generic buffer */
274
275   gst_object_unref (bsink);
276
277   return result;
278 }
279
280 static void
281 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
282 {
283   GstPadTemplate *pad_template;
284
285   pad_template =
286       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
287   g_return_if_fail (pad_template != NULL);
288
289   basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
290
291   gst_pad_set_getcaps_function (basesink->sinkpad,
292       GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
293   gst_pad_set_setcaps_function (basesink->sinkpad,
294       GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
295   gst_pad_set_bufferalloc_function (basesink->sinkpad,
296       GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
297   gst_pad_set_activate_function (basesink->sinkpad,
298       GST_DEBUG_FUNCPTR (gst_base_sink_activate));
299   gst_pad_set_activatepush_function (basesink->sinkpad,
300       GST_DEBUG_FUNCPTR (gst_base_sink_activate_push));
301   gst_pad_set_activatepull_function (basesink->sinkpad,
302       GST_DEBUG_FUNCPTR (gst_base_sink_activate_pull));
303   gst_pad_set_event_function (basesink->sinkpad,
304       GST_DEBUG_FUNCPTR (gst_base_sink_event));
305   gst_pad_set_chain_function (basesink->sinkpad,
306       GST_DEBUG_FUNCPTR (gst_base_sink_chain));
307   gst_element_add_pad (GST_ELEMENT (basesink), basesink->sinkpad);
308
309   basesink->pad_mode = GST_ACTIVATE_NONE;
310   GST_PAD_TASK (basesink->sinkpad) = NULL;
311   basesink->preroll_queue = g_queue_new ();
312
313   basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
314   basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
315
316   basesink->sync = DEFAULT_SYNC;
317
318   GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
319 }
320
321 static void
322 gst_base_sink_finalize (GObject * object)
323 {
324   GstBaseSink *basesink;
325
326   basesink = GST_BASE_SINK (object);
327
328   g_queue_free (basesink->preroll_queue);
329
330   G_OBJECT_CLASS (parent_class)->finalize (object);
331 }
332
333 static void
334 gst_base_sink_set_clock (GstElement * element, GstClock * clock)
335 {
336   GstBaseSink *sink;
337
338   sink = GST_BASE_SINK (element);
339
340   sink->clock = clock;
341 }
342
343 static void
344 gst_base_sink_set_property (GObject * object, guint prop_id,
345     const GValue * value, GParamSpec * pspec)
346 {
347   GstBaseSink *sink = GST_BASE_SINK (object);
348
349   switch (prop_id) {
350     case PROP_PREROLL_QUEUE_LEN:
351       /* preroll lock necessary to serialize with finish_preroll */
352       GST_PREROLL_LOCK (sink->sinkpad);
353       sink->preroll_queue_max_len = g_value_get_uint (value);
354       GST_PREROLL_UNLOCK (sink->sinkpad);
355       break;
356     case PROP_SYNC:
357       sink->sync = g_value_get_boolean (value);
358       break;
359     default:
360       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361       break;
362   }
363 }
364
365 static void
366 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
367     GParamSpec * pspec)
368 {
369   GstBaseSink *sink = GST_BASE_SINK (object);
370
371   GST_LOCK (sink);
372   switch (prop_id) {
373     case PROP_PREROLL_QUEUE_LEN:
374       g_value_set_uint (value, sink->preroll_queue_max_len);
375       break;
376     case PROP_SYNC:
377       g_value_set_boolean (value, sink->sync);
378       break;
379     default:
380       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381       break;
382   }
383   GST_UNLOCK (sink);
384 }
385
386 static GstCaps *
387 gst_base_sink_get_caps (GstBaseSink * sink)
388 {
389   return NULL;
390 }
391
392 static gboolean
393 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
394 {
395   return TRUE;
396 }
397
398 static GstFlowReturn
399 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
400     GstCaps * caps, GstBuffer ** buf)
401 {
402   *buf = NULL;
403   return GST_FLOW_OK;
404 }
405
406 /* with PREROLL_LOCK */
407 static GstFlowReturn
408 gst_base_sink_preroll_queue_empty (GstBaseSink * basesink, GstPad * pad)
409 {
410   GstMiniObject *obj;
411   GQueue *q = basesink->preroll_queue;
412   GstFlowReturn ret;
413
414   ret = GST_FLOW_OK;
415
416   if (q) {
417     GST_DEBUG_OBJECT (basesink, "emptying queue");
418     while ((obj = g_queue_pop_head (q))) {
419       gboolean is_buffer;
420
421       is_buffer = GST_IS_BUFFER (obj);
422       if (G_LIKELY (is_buffer)) {
423         basesink->preroll_queued--;
424         basesink->buffers_queued--;
425       } else {
426         switch (GST_EVENT_TYPE (obj)) {
427           case GST_EVENT_EOS:
428             basesink->preroll_queued--;
429             break;
430           default:
431             break;
432         }
433         basesink->events_queued--;
434       }
435       /* we release the preroll lock while pushing so that we
436        * can still flush it while blocking on the clock or
437        * inside the element. */
438       GST_PREROLL_UNLOCK (pad);
439
440       if (G_LIKELY (is_buffer)) {
441         GST_DEBUG_OBJECT (basesink, "popped buffer %p", obj);
442         ret = gst_base_sink_handle_buffer (basesink, GST_BUFFER_CAST (obj));
443       } else {
444         GST_DEBUG_OBJECT (basesink, "popped event %p", obj);
445         gst_base_sink_handle_event (basesink, GST_EVENT_CAST (obj));
446         ret = GST_FLOW_OK;
447       }
448
449       GST_PREROLL_LOCK (pad);
450     }
451     GST_DEBUG_OBJECT (basesink, "queue empty");
452   }
453   return ret;
454 }
455
456 /* with PREROLL_LOCK */
457 static void
458 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
459 {
460   GstMiniObject *obj;
461   GQueue *q = basesink->preroll_queue;
462
463   GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
464   if (q) {
465     while ((obj = g_queue_pop_head (q))) {
466       GST_DEBUG_OBJECT (basesink, "popped %p", obj);
467       gst_mini_object_unref (obj);
468     }
469   }
470   /* we can't have EOS anymore now */
471   basesink->eos = FALSE;
472   basesink->eos_queued = FALSE;
473   basesink->preroll_queued = 0;
474   basesink->buffers_queued = 0;
475   basesink->events_queued = 0;
476   basesink->have_preroll = FALSE;
477   /* and signal any waiters now */
478   GST_PREROLL_SIGNAL (pad);
479 }
480
481 /* with PREROLL_LOCK */
482 static gboolean
483 gst_base_sink_commit_state (GstBaseSink * basesink)
484 {
485   /* commit state and proceed to next pending state */
486   {
487     GstState current, next, pending, post_pending;
488     GstMessage *message;
489     gboolean post_paused = FALSE;
490     gboolean post_playing = FALSE;
491
492     GST_LOCK (basesink);
493     current = GST_STATE (basesink);
494     next = GST_STATE_NEXT (basesink);
495     pending = GST_STATE_PENDING (basesink);
496     post_pending = pending;
497
498     switch (pending) {
499       case GST_STATE_PLAYING:
500         basesink->need_preroll = FALSE;
501         post_playing = TRUE;
502         /* post PAUSED too when we were READY */
503         if (current == GST_STATE_READY) {
504           post_paused = TRUE;
505         }
506         break;
507       case GST_STATE_PAUSED:
508         basesink->need_preroll = TRUE;
509         post_paused = TRUE;
510         post_pending = GST_STATE_VOID_PENDING;
511         break;
512       case GST_STATE_READY:
513         goto stopping;
514       default:
515         break;
516     }
517
518     if (pending != GST_STATE_VOID_PENDING) {
519       GST_STATE (basesink) = pending;
520       GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
521       GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
522       GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
523     }
524     GST_UNLOCK (basesink);
525
526     if (post_paused) {
527       message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
528           current, next, post_pending);
529       gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
530     }
531     if (post_playing) {
532       message = gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
533           next, pending, GST_STATE_VOID_PENDING);
534       gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
535     }
536     /* and mark dirty */
537     if (post_paused || post_playing) {
538       gst_element_post_message (GST_ELEMENT_CAST (basesink),
539           gst_message_new_state_dirty (GST_OBJECT_CAST (basesink)));
540     }
541
542     GST_STATE_BROADCAST (basesink);
543   }
544   return TRUE;
545
546 stopping:
547   {
548     /* app is going to READY */
549     GST_UNLOCK (basesink);
550     return FALSE;
551   }
552 }
553
554 /* with STREAM_LOCK */
555 static GstFlowReturn
556 gst_base_sink_handle_object (GstBaseSink * basesink, GstPad * pad,
557     GstMiniObject * obj)
558 {
559   gint length;
560   gboolean have_event;
561
562   GST_PREROLL_LOCK (pad);
563   /* push object on the queue */
564   GST_DEBUG_OBJECT (basesink, "push %p on preroll_queue", obj);
565   g_queue_push_tail (basesink->preroll_queue, obj);
566
567   have_event = GST_IS_EVENT (obj);
568   if (have_event) {
569     GstEvent *event = GST_EVENT (obj);
570
571     switch (GST_EVENT_TYPE (obj)) {
572       case GST_EVENT_EOS:
573         basesink->preroll_queued++;
574         basesink->eos = TRUE;
575         basesink->eos_queued = TRUE;
576         break;
577       case GST_EVENT_NEWSEGMENT:
578       {
579         gboolean update;
580         gdouble rate;
581         GstFormat format;
582         gint64 segment_start;
583         gint64 segment_stop;
584         gint64 segment_time;
585         GstClockTime duration;
586
587
588         /* the newsegment event is needed to bring the buffer timestamps to the
589          * stream time and to drop samples outside of the playback segment. */
590         gst_event_parse_newsegment (event, &update, &rate, &format,
591             &segment_start, &segment_stop, &segment_time);
592
593         basesink->have_newsegment = TRUE;
594
595         /* any other format with 0 also gives time 0, the other values are
596          * invalid as time though. */
597         if (format != GST_FORMAT_TIME && segment_start == 0) {
598           GST_DEBUG_OBJECT (basesink,
599               "non-time newsegment with start 0, coaxing into FORMAT_TIME");
600           format = GST_FORMAT_TIME;
601           segment_stop = -1;
602           segment_time = -1;
603         }
604
605         if (format != GST_FORMAT_TIME) {
606           GST_DEBUG_OBJECT (basesink,
607               "received non time %d NEW_SEGMENT %" G_GINT64_FORMAT
608               " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT,
609               format, segment_start, segment_stop, segment_time);
610
611           /* this means this sink will not be able to clip or drop samples
612            * and timestamps have to start from 0. */
613           basesink->segment_start = -1;
614           basesink->segment_stop = -1;
615           basesink->segment_time = -1;
616           goto done_newsegment;
617         }
618         /* check if we really have a new segment or the previous one is
619          * closed */
620         if (!update) {
621           /* the new segment has to be aligned with the old segment.
622            * We first update the accumulated time of the previous
623            * segment. the accumulated time is used when syncing to the
624            * clock. A flush event sets the accumulated time back to 0
625            */
626           if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
627             duration = basesink->segment_stop - basesink->segment_start;
628           } else if (GST_CLOCK_TIME_IS_VALID (basesink->current_end)) {
629             /* else use last seen timestamp as segment stop */
630             duration = basesink->current_end - basesink->segment_start;
631           } else {
632             duration = 0;
633           }
634         } else {
635           duration = segment_start - basesink->segment_start;
636         }
637
638         /* use previous rate to calculate duration */
639         basesink->segment_accum += gst_gdouble_to_guint64 (
640             (gst_guint64_to_gdouble (duration) / ABS (basesink->segment_rate)));
641         /* then update the current segment */
642         basesink->segment_rate = rate;
643         basesink->segment_start = segment_start;
644         basesink->segment_stop = segment_stop;
645         basesink->segment_time = segment_time;
646
647         GST_DEBUG_OBJECT (basesink,
648             "received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
649             GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
650             GST_TIME_FORMAT,
651             GST_TIME_ARGS (basesink->segment_start),
652             GST_TIME_ARGS (basesink->segment_stop),
653             GST_TIME_ARGS (basesink->segment_time),
654             GST_TIME_ARGS (basesink->segment_accum));
655       done_newsegment:
656         break;
657       }
658       default:
659         break;
660     }
661     basesink->events_queued++;
662   } else {
663     GstBuffer *buf = GST_BUFFER (obj);
664
665     if (!basesink->have_newsegment) {
666       GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
667           (_("Internal data flow problem.")),
668           ("Received buffer without a new-segment. Cannot sync to clock."));
669       basesink->have_newsegment = TRUE;
670       /* this means this sink will not be able to sync to the clock */
671       basesink->segment_start = -1;
672       basesink->segment_stop = -1;
673     }
674
675     /* check if the buffer needs to be dropped */
676     if (TRUE) {
677       GstClockTime start = -1, end = -1;
678
679       /* we don't use the subclassed method as it may not return
680        * valid values for our purpose here */
681       gst_base_sink_get_times (basesink, buf, &start, &end);
682
683       GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
684           ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start),
685           GST_TIME_ARGS (end));
686
687       /* need to drop if the timestamp is not between segment_start and
688        * segment_stop. we check if the complete sample is outside of the
689        * range since the sink might be able to clip the sample. */
690       if (GST_CLOCK_TIME_IS_VALID (end) &&
691           GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
692         if (end <= basesink->segment_start) {
693           GST_DEBUG_OBJECT (basesink,
694               "buffer end %" GST_TIME_FORMAT " <= segment start %"
695               GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (end),
696               GST_TIME_ARGS (basesink->segment_start));
697           goto dropping;
698         }
699       }
700       if (GST_CLOCK_TIME_IS_VALID (start) &&
701           GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
702         if (basesink->segment_stop <= start) {
703           GST_DEBUG_OBJECT (basesink,
704               "buffer start %" GST_TIME_FORMAT " >= segment stop %"
705               GST_TIME_FORMAT ", dropping buffer", GST_TIME_ARGS (start),
706               GST_TIME_ARGS (basesink->segment_stop));
707           goto dropping;
708         }
709       }
710     }
711     basesink->preroll_queued++;
712     basesink->buffers_queued++;
713   }
714
715   GST_DEBUG_OBJECT (basesink,
716       "now %d preroll, %d buffers, %d events on queue",
717       basesink->preroll_queued,
718       basesink->buffers_queued, basesink->events_queued);
719
720   /* check if we are prerolling */
721   if (!basesink->need_preroll)
722     goto no_preroll;
723
724   /* there is a buffer queued */
725   if (basesink->buffers_queued == 1) {
726     GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
727
728     /* if it's a buffer, we need to call the preroll method */
729     if (GST_IS_BUFFER (obj)) {
730       GstBaseSinkClass *bclass;
731       GstFlowReturn pres;
732       GstBuffer *buf = GST_BUFFER (obj);
733
734       GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
735           GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
736
737       bclass = GST_BASE_SINK_GET_CLASS (basesink);
738       if (bclass->preroll)
739         if ((pres = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
740           goto preroll_failed;
741     }
742   }
743   length = basesink->preroll_queued;
744   GST_DEBUG_OBJECT (basesink, "prerolled length %d", length);
745
746   if (length == 1) {
747
748     basesink->have_preroll = TRUE;
749
750     /* commit state */
751     if (!gst_base_sink_commit_state (basesink))
752       goto stopping;
753
754     GST_LOCK (pad);
755     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
756       goto flushing;
757     GST_UNLOCK (pad);
758
759     /* it is possible that commiting the state made us go to PLAYING
760      * now in which case we don't need to block anymore. */
761     if (!basesink->need_preroll)
762       goto no_preroll;
763
764     length = basesink->preroll_queued;
765
766     /* FIXME: a pad probe could have made us lose the buffer, according
767      * to one of the python tests */
768     if (length == 0) {
769       GST_ERROR_OBJECT (basesink,
770           "preroll_queued dropped from 1 to 0 while committing state change");
771     }
772     g_assert (length <= 1);
773   }
774
775   /* see if we need to block now. We cannot block on events, only
776    * on buffers, the reason is that events can be sent from the
777    * application thread and we don't want to block there. */
778   if (length > basesink->preroll_queue_max_len && !have_event) {
779     /* block until the state changes, or we get a flush, or something */
780     GST_DEBUG_OBJECT (basesink, "waiting to finish preroll");
781     GST_PREROLL_WAIT (pad);
782     GST_DEBUG_OBJECT (basesink, "done preroll");
783     GST_LOCK (pad);
784     if (G_UNLIKELY (GST_PAD_IS_FLUSHING (pad)))
785       goto flushing;
786     GST_UNLOCK (pad);
787   }
788   GST_PREROLL_UNLOCK (pad);
789
790   return GST_FLOW_OK;
791
792 no_preroll:
793   {
794     GstFlowReturn ret;
795
796     GST_DEBUG_OBJECT (basesink, "no preroll needed");
797     /* maybe it was another sink that blocked in preroll, need to check for
798        buffers to drain */
799     basesink->have_preroll = FALSE;
800     ret = gst_base_sink_preroll_queue_empty (basesink, pad);
801     GST_PREROLL_UNLOCK (pad);
802
803     return ret;
804   }
805 dropping:
806   {
807     GstBuffer *buf;
808
809     buf = GST_BUFFER (g_queue_pop_tail (basesink->preroll_queue));
810
811     gst_buffer_unref (buf);
812     GST_PREROLL_UNLOCK (pad);
813
814     return GST_FLOW_OK;
815   }
816 flushing:
817   {
818     GST_UNLOCK (pad);
819     gst_base_sink_preroll_queue_flush (basesink, pad);
820     GST_PREROLL_UNLOCK (pad);
821     GST_DEBUG_OBJECT (basesink, "pad is flushing");
822
823     return GST_FLOW_WRONG_STATE;
824   }
825 stopping:
826   {
827     GST_PREROLL_UNLOCK (pad);
828     GST_DEBUG_OBJECT (basesink, "stopping");
829
830     return GST_FLOW_WRONG_STATE;
831   }
832 preroll_failed:
833   {
834     GST_DEBUG_OBJECT (basesink, "preroll failed");
835     gst_base_sink_preroll_queue_flush (basesink, pad);
836
837     GST_DEBUG_OBJECT (basesink, "abort state");
838     gst_element_abort_state (GST_ELEMENT (basesink));
839
840     return GST_FLOW_ERROR;
841   }
842 }
843
844 static gboolean
845 gst_base_sink_event (GstPad * pad, GstEvent * event)
846 {
847   GstBaseSink *basesink;
848   gboolean result = TRUE;
849   GstBaseSinkClass *bclass;
850
851   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
852
853   bclass = GST_BASE_SINK_GET_CLASS (basesink);
854
855   GST_DEBUG_OBJECT (basesink, "event %p", event);
856
857   switch (GST_EVENT_TYPE (event)) {
858     case GST_EVENT_EOS:
859     {
860       GstFlowReturn ret;
861
862       GST_STREAM_LOCK (pad);
863       /* EOS also finishes the preroll */
864       ret =
865           gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
866       GST_STREAM_UNLOCK (pad);
867       break;
868     }
869     case GST_EVENT_NEWSEGMENT:
870     {
871       GstFlowReturn ret;
872
873       GST_STREAM_LOCK (pad);
874       ret =
875           gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (event));
876       GST_STREAM_UNLOCK (pad);
877       break;
878     }
879     case GST_EVENT_FLUSH_START:
880       /* make sure we are not blocked on the clock also clear any pending
881        * eos state. */
882       if (bclass->event)
883         bclass->event (basesink, event);
884
885       GST_LOCK (basesink);
886       basesink->flushing = TRUE;
887       if (basesink->clock_id) {
888         gst_clock_id_unschedule (basesink->clock_id);
889       }
890       GST_UNLOCK (basesink);
891
892       GST_PREROLL_LOCK (pad);
893       /* we need preroll after the flush */
894       GST_DEBUG_OBJECT (basesink, "flushing, need preroll after flush");
895       basesink->need_preroll = TRUE;
896       /* unlock from a possible state change/preroll */
897       gst_base_sink_preroll_queue_flush (basesink, pad);
898       GST_PREROLL_UNLOCK (pad);
899
900       /* and we need to commit our state again on the next
901        * prerolled buffer */
902       GST_STREAM_LOCK (pad);
903       gst_element_lost_state (GST_ELEMENT (basesink));
904       GST_STREAM_UNLOCK (pad);
905       GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
906       gst_event_unref (event);
907       break;
908     case GST_EVENT_FLUSH_STOP:
909       if (bclass->event)
910         bclass->event (basesink, event);
911
912       /* now we are completely unblocked and the _chain method
913        * will return */
914       GST_STREAM_LOCK (pad);
915       GST_LOCK (basesink);
916       basesink->flushing = FALSE;
917       GST_UNLOCK (basesink);
918       /* we need new segment info after the flush. */
919       basesink->segment_start = -1;
920       basesink->segment_stop = -1;
921       basesink->current_start = -1;
922       basesink->current_end = -1;
923       GST_DEBUG_OBJECT (basesink, "reset accum %" GST_TIME_FORMAT,
924           GST_TIME_ARGS (basesink->segment_accum));
925       basesink->segment_accum = 0;
926       GST_STREAM_UNLOCK (pad);
927
928       GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
929       gst_event_unref (event);
930       break;
931     default:
932       gst_event_unref (event);
933       break;
934   }
935   gst_object_unref (basesink);
936
937   return result;
938 }
939
940 /* default implementation to calculate the start and end
941  * timestamps on a buffer, subclasses can override
942  */
943 static void
944 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
945     GstClockTime * start, GstClockTime * end)
946 {
947   GstClockTime timestamp, duration;
948
949   timestamp = GST_BUFFER_TIMESTAMP (buffer);
950   if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
951
952     /* get duration to calculate end time */
953     duration = GST_BUFFER_DURATION (buffer);
954     if (GST_CLOCK_TIME_IS_VALID (duration)) {
955       *end = timestamp + duration;
956     }
957     *start = timestamp;
958   }
959 }
960
961 /* with STREAM_LOCK and LOCK*/
962 static GstClockReturn
963 gst_base_sink_wait (GstBaseSink * basesink, GstClockTime time)
964 {
965   GstClockReturn ret;
966   GstClockID id;
967
968   /* no need to attempt a clock wait if we are flushing */
969   if (basesink->flushing) {
970     return GST_CLOCK_UNSCHEDULED;
971   }
972
973   /* clock_id should be NULL outside of this function */
974   g_assert (basesink->clock_id == NULL);
975   g_assert (GST_CLOCK_TIME_IS_VALID (time));
976
977   id = gst_clock_new_single_shot_id (basesink->clock, time);
978
979   basesink->clock_id = id;
980   /* release the object lock while waiting */
981   GST_UNLOCK (basesink);
982
983   ret = gst_clock_id_wait (id, NULL);
984
985   GST_LOCK (basesink);
986   gst_clock_id_unref (id);
987   basesink->clock_id = NULL;
988
989   return ret;
990 }
991
992 /* perform synchronisation on a buffer
993  *
994  * 1) check if we have a clock, if not, do nothing
995  * 2) calculate the start and end time of the buffer
996  * 3) create a single shot notification to wait on
997  *    the clock, save the entry so we can unlock it
998  * 4) wait on the clock, this blocks
999  * 5) unref the clockid again
1000  */
1001 static GstClockReturn
1002 gst_base_sink_do_sync (GstBaseSink * basesink, GstBuffer * buffer)
1003 {
1004   GstClockReturn result = GST_CLOCK_OK;
1005   GstClockTime start, end;
1006   GstClockTimeDiff stream_start, stream_end;
1007   GstBaseSinkClass *bclass;
1008   gboolean start_valid, end_valid;
1009
1010   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1011
1012   start = end = -1;
1013   if (bclass->get_times)
1014     bclass->get_times (basesink, buffer, &start, &end);
1015
1016   start_valid = GST_CLOCK_TIME_IS_VALID (start);
1017   end_valid = GST_CLOCK_TIME_IS_VALID (end);
1018
1019   GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1020       ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
1021
1022   /* if we don't have a timestamp, we don't sync */
1023   if (!start_valid) {
1024     GST_DEBUG_OBJECT (basesink, "start not valid");
1025     goto done;
1026   }
1027
1028   /* save last times seen. */
1029   basesink->current_start = start;
1030   if (end_valid)
1031     basesink->current_end = end;
1032   else
1033     basesink->current_end = start;
1034
1035   if (GST_CLOCK_TIME_IS_VALID (basesink->segment_stop)) {
1036     /* check if not outside of the segment range, start is
1037      * always valid here. */
1038     if (start > basesink->segment_stop)
1039       goto out_of_segment;
1040   }
1041
1042   /* bring timestamp to stream time using last segment offset. */
1043   if (GST_CLOCK_TIME_IS_VALID (basesink->segment_start)) {
1044     /* check if not outside of the segment range */
1045     if (end_valid && end < basesink->segment_start)
1046       goto out_of_segment;
1047
1048     stream_start = (gint64) start - basesink->segment_start;
1049     stream_end = (gint64) end - basesink->segment_start;
1050   } else {
1051     stream_start = (gint64) start;
1052     stream_end = (gint64) end;
1053   }
1054
1055   /* correct for rate */
1056   if (basesink->segment_rate != 0.0) {
1057     stream_start /= ABS (basesink->segment_rate);
1058     if (end_valid)
1059       stream_end /= ABS (basesink->segment_rate);
1060   }
1061
1062   stream_start += basesink->segment_accum;
1063   if (end_valid)
1064     stream_end += basesink->segment_accum;
1065
1066   /* now do clocking */
1067   if (basesink->clock && basesink->sync) {
1068     GstClockTime base_time;
1069
1070     GST_LOCK (basesink);
1071
1072     base_time = GST_ELEMENT_CAST (basesink)->base_time;
1073
1074     GST_LOG_OBJECT (basesink,
1075         "waiting for clock, base time %" GST_TIME_FORMAT
1076         "stream_start %" GST_TIME_FORMAT,
1077         GST_TIME_ARGS (base_time), GST_TIME_ARGS (stream_start));
1078
1079     /* also save end_time of this buffer so that we can wait
1080      * to signal EOS */
1081     if (end_valid)
1082       basesink->end_time = stream_end + base_time;
1083     else
1084       basesink->end_time = GST_CLOCK_TIME_NONE;
1085
1086     result = gst_base_sink_wait (basesink, stream_start + base_time);
1087
1088     GST_UNLOCK (basesink);
1089
1090     GST_LOG_OBJECT (basesink, "clock entry done: %d", result);
1091   } else {
1092     GST_DEBUG_OBJECT (basesink, "no clock, not syncing");
1093   }
1094
1095 done:
1096   return result;
1097
1098 out_of_segment:
1099   {
1100     GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1101     return GST_CLOCK_UNSCHEDULED;
1102   }
1103 }
1104
1105
1106 /* handle an event
1107  *
1108  * 2) render the event
1109  * 3) unref the event
1110  */
1111 static inline gboolean
1112 gst_base_sink_handle_event (GstBaseSink * basesink, GstEvent * event)
1113 {
1114   GstBaseSinkClass *bclass;
1115   gboolean ret;
1116
1117   switch (GST_EVENT_TYPE (event)) {
1118     case GST_EVENT_EOS:
1119       GST_LOCK (basesink);
1120       if (basesink->clock) {
1121         /* wait for last buffer to finish if we have a valid end time */
1122         if (GST_CLOCK_TIME_IS_VALID (basesink->end_time)) {
1123           gst_base_sink_wait (basesink, basesink->end_time);
1124           basesink->end_time = GST_CLOCK_TIME_NONE;
1125         }
1126       }
1127       GST_UNLOCK (basesink);
1128       break;
1129     default:
1130       break;
1131   }
1132
1133   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1134   if (bclass->event)
1135     ret = bclass->event (basesink, event);
1136   else
1137     ret = TRUE;
1138
1139   switch (GST_EVENT_TYPE (event)) {
1140     case GST_EVENT_EOS:
1141       GST_PREROLL_LOCK (basesink->sinkpad);
1142       /* if we are still EOS, we can post the EOS message */
1143       if (basesink->eos) {
1144         /* ok, now we can post the message */
1145         GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1146         gst_element_post_message (GST_ELEMENT_CAST (basesink),
1147             gst_message_new_eos (GST_OBJECT_CAST (basesink)));
1148         basesink->eos_queued = FALSE;
1149       }
1150       GST_PREROLL_UNLOCK (basesink->sinkpad);
1151       break;
1152     default:
1153       break;
1154   }
1155
1156   GST_DEBUG_OBJECT (basesink, "event unref %p %p", basesink, event);
1157   gst_event_unref (event);
1158
1159   return ret;
1160 }
1161
1162 /* handle a buffer
1163  *
1164  * 1) first sync on the buffer
1165  * 2) render the buffer
1166  * 3) unref the buffer
1167  */
1168 static inline GstFlowReturn
1169 gst_base_sink_handle_buffer (GstBaseSink * basesink, GstBuffer * buf)
1170 {
1171   GstFlowReturn ret = GST_FLOW_OK;
1172   GstClockReturn status;
1173
1174   status = gst_base_sink_do_sync (basesink, buf);
1175   switch (status) {
1176     case GST_CLOCK_EARLY:
1177       GST_DEBUG_OBJECT (basesink, "buffer too late!, rendering anyway");
1178       /* fallthrough for now */
1179     case GST_CLOCK_OK:
1180     {
1181       GstBaseSinkClass *bclass;
1182
1183       bclass = GST_BASE_SINK_GET_CLASS (basesink);
1184       if (bclass->render)
1185         ret = bclass->render (basesink, buf);
1186       break;
1187     }
1188     default:
1189       GST_DEBUG_OBJECT (basesink, "clock returned %d, not rendering", status);
1190       break;
1191   }
1192
1193   GST_DEBUG_OBJECT (basesink, "buffer unref after render %p", basesink, buf);
1194   gst_buffer_unref (buf);
1195
1196   return ret;
1197 }
1198
1199 static GstFlowReturn
1200 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
1201 {
1202   GstBaseSink *basesink;
1203   GstFlowReturn result;
1204
1205   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1206
1207   if (!(basesink->pad_mode == GST_ACTIVATE_PUSH)) {
1208     GST_LOCK (pad);
1209     g_warning ("Push on pad %s:%s, but it was not activated in push mode",
1210         GST_DEBUG_PAD_NAME (pad));
1211     GST_UNLOCK (pad);
1212     result = GST_FLOW_UNEXPECTED;
1213     goto done;
1214   }
1215
1216   result =
1217       gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT_CAST (buf));
1218
1219 done:
1220   gst_object_unref (basesink);
1221
1222   return result;
1223 }
1224
1225 static void
1226 gst_base_sink_loop (GstPad * pad)
1227 {
1228   GstBaseSink *basesink;
1229   GstBuffer *buf = NULL;
1230   GstFlowReturn result;
1231
1232   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1233
1234   g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
1235
1236   result = gst_pad_pull_range (pad, basesink->offset, DEFAULT_SIZE, &buf);
1237   if (result != GST_FLOW_OK)
1238     goto paused;
1239
1240   result = gst_base_sink_handle_object (basesink, pad, GST_MINI_OBJECT (buf));
1241   if (result != GST_FLOW_OK)
1242     goto paused;
1243
1244   gst_object_unref (basesink);
1245
1246   /* default */
1247   return;
1248
1249 paused:
1250   {
1251     gst_base_sink_event (pad, gst_event_new_eos ());
1252     gst_object_unref (basesink);
1253     gst_pad_pause_task (pad);
1254     return;
1255   }
1256 }
1257
1258 static gboolean
1259 gst_base_sink_deactivate (GstBaseSink * basesink, GstPad * pad)
1260 {
1261   gboolean result = FALSE;
1262   GstBaseSinkClass *bclass;
1263
1264   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1265
1266   /* step 1, unblock clock sync (if any) or any other blocking thing */
1267   GST_PREROLL_LOCK (pad);
1268   GST_LOCK (basesink);
1269   if (basesink->clock_id) {
1270     gst_clock_id_unschedule (basesink->clock_id);
1271   }
1272   GST_UNLOCK (basesink);
1273
1274   /* unlock any subclasses */
1275   if (bclass->unlock)
1276     bclass->unlock (basesink);
1277
1278   /* flush out the data thread if it's locked in finish_preroll */
1279   GST_DEBUG_OBJECT (basesink,
1280       "flushing out data thread, need preroll to FALSE");
1281   basesink->need_preroll = FALSE;
1282   gst_base_sink_preroll_queue_flush (basesink, pad);
1283   GST_PREROLL_SIGNAL (pad);
1284   GST_PREROLL_UNLOCK (pad);
1285
1286   /* step 2, make sure streaming finishes */
1287   result = gst_pad_stop_task (pad);
1288
1289   return result;
1290 }
1291
1292 static gboolean
1293 gst_base_sink_activate (GstPad * pad)
1294 {
1295   gboolean result = FALSE;
1296   GstBaseSink *basesink;
1297
1298   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1299
1300   GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
1301
1302   if (basesink->can_activate_pull && gst_pad_check_pull_range (pad)
1303       && gst_pad_activate_pull (pad, TRUE)) {
1304     GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
1305     result = TRUE;
1306   } else {
1307     GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
1308     if (gst_pad_activate_push (pad, TRUE)) {
1309       GST_DEBUG_OBJECT (basesink, "Success activating push mode");
1310       result = TRUE;
1311     }
1312   }
1313
1314   if (!result) {
1315     GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
1316   }
1317
1318   gst_object_unref (basesink);
1319
1320   return result;
1321 }
1322
1323 static gboolean
1324 gst_base_sink_activate_push (GstPad * pad, gboolean active)
1325 {
1326   gboolean result;
1327   GstBaseSink *basesink;
1328
1329   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1330
1331   if (active) {
1332     if (!basesink->can_activate_push) {
1333       result = FALSE;
1334       basesink->pad_mode = GST_ACTIVATE_NONE;
1335     } else {
1336       result = TRUE;
1337       basesink->pad_mode = GST_ACTIVATE_PUSH;
1338     }
1339   } else {
1340     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
1341       g_warning ("Internal GStreamer activation error!!!");
1342       result = FALSE;
1343     } else {
1344       result = gst_base_sink_deactivate (basesink, pad);
1345       basesink->pad_mode = GST_ACTIVATE_NONE;
1346     }
1347   }
1348
1349   gst_object_unref (basesink);
1350
1351   return result;
1352 }
1353
1354 /* this won't get called until we implement an activate function */
1355 static gboolean
1356 gst_base_sink_activate_pull (GstPad * pad, gboolean active)
1357 {
1358   gboolean result = FALSE;
1359   GstBaseSink *basesink;
1360
1361   basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
1362
1363   if (active) {
1364     if (!basesink->can_activate_pull) {
1365       result = FALSE;
1366       basesink->pad_mode = GST_ACTIVATE_NONE;
1367     } else {
1368       GstPad *peer = gst_pad_get_peer (pad);
1369
1370       if (G_UNLIKELY (peer == NULL)) {
1371         g_warning ("Trying to activate pad in pull mode, but no peer");
1372         result = FALSE;
1373         basesink->pad_mode = GST_ACTIVATE_NONE;
1374       } else {
1375         if (gst_pad_activate_pull (peer, TRUE)) {
1376           basesink->have_newsegment = TRUE;
1377           basesink->segment_start = basesink->segment_stop = 0;
1378
1379           /* set the pad mode before starting the task so that it's in the
1380              correct state for the new thread... */
1381           basesink->pad_mode = GST_ACTIVATE_PULL;
1382           result =
1383               gst_pad_start_task (pad, (GstTaskFunction) gst_base_sink_loop,
1384               pad);
1385           /* but if starting the thread fails, set it back */
1386           if (!result)
1387             basesink->pad_mode = GST_ACTIVATE_NONE;
1388         } else {
1389           GST_DEBUG_OBJECT (pad, "Failed to activate peer in pull mode");
1390           result = FALSE;
1391           basesink->pad_mode = GST_ACTIVATE_NONE;
1392         }
1393         gst_object_unref (peer);
1394       }
1395     }
1396   } else {
1397     if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
1398       g_warning ("Internal GStreamer activation error!!!");
1399       result = FALSE;
1400     } else {
1401       basesink->have_newsegment = FALSE;
1402       result = gst_base_sink_deactivate (basesink, pad);
1403       basesink->pad_mode = GST_ACTIVATE_NONE;
1404     }
1405   }
1406
1407   gst_object_unref (basesink);
1408
1409   return result;
1410 }
1411
1412 static gboolean
1413 gst_base_sink_send_event (GstElement * element, GstEvent * event)
1414 {
1415   GstPad *pad;
1416   GstBaseSink *basesink = GST_BASE_SINK (element);
1417   gboolean result;
1418
1419   GST_LOCK (element);
1420   pad = basesink->sinkpad;
1421   gst_object_ref (pad);
1422   GST_UNLOCK (element);
1423
1424   result = gst_pad_push_event (pad, event);
1425
1426   gst_object_unref (pad);
1427
1428   return result;
1429 }
1430
1431 static gboolean
1432 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
1433 {
1434   GstPad *peer;
1435   gboolean res = FALSE;
1436
1437   if ((peer = gst_pad_get_peer (sink->sinkpad))) {
1438     res = gst_pad_query (peer, query);
1439     gst_object_unref (peer);
1440   }
1441   return res;
1442 }
1443
1444 static gboolean
1445 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
1446     gint64 * cur)
1447 {
1448   GstClock *clock;
1449   gboolean res = FALSE;
1450
1451   switch (format) {
1452     case GST_FORMAT_TIME:
1453     {
1454       /* we can answer time format */
1455       GST_LOCK (basesink);
1456       if ((clock = GST_ELEMENT_CLOCK (basesink))) {
1457         GstClockTime now;
1458         gint64 segment_time;
1459
1460         gst_object_ref (clock);
1461         GST_UNLOCK (basesink);
1462
1463         now = gst_clock_get_time (clock);
1464
1465         GST_LOCK (basesink);
1466         if (GST_CLOCK_TIME_IS_VALID (basesink->segment_time))
1467           segment_time = basesink->segment_time;
1468         else
1469           segment_time = 0;
1470
1471         *cur = now - GST_ELEMENT_CAST (basesink)->base_time + segment_time;
1472
1473         GST_DEBUG_OBJECT (basesink,
1474             "now %" GST_TIME_FORMAT " + segment_time %" GST_TIME_FORMAT " = %"
1475             GST_TIME_FORMAT, GST_TIME_ARGS (now),
1476             GST_TIME_ARGS (segment_time), GST_TIME_ARGS (*cur));
1477
1478         gst_object_unref (clock);
1479
1480         res = TRUE;
1481       }
1482       GST_UNLOCK (basesink);
1483     }
1484     default:
1485       break;
1486   }
1487   return res;
1488 }
1489
1490 static gboolean
1491 gst_base_sink_query (GstElement * element, GstQuery * query)
1492 {
1493   gboolean res = FALSE;
1494
1495   GstBaseSink *basesink = GST_BASE_SINK (element);
1496
1497   switch (GST_QUERY_TYPE (query)) {
1498     case GST_QUERY_POSITION:
1499     {
1500       gint64 cur = 0;
1501       GstFormat format;
1502       gboolean eos;
1503
1504       GST_PREROLL_LOCK (basesink->sinkpad);
1505       eos = basesink->eos;
1506       GST_PREROLL_UNLOCK (basesink->sinkpad);
1507
1508       if (eos) {
1509         res = gst_base_sink_peer_query (basesink, query);
1510       } else {
1511         gst_query_parse_position (query, &format, NULL);
1512
1513         GST_DEBUG_OBJECT (basesink, "current position format %d", format);
1514
1515         if ((res = gst_base_sink_get_position (basesink, format, &cur))) {
1516           gst_query_set_position (query, format, cur);
1517         } else {
1518           res = gst_base_sink_peer_query (basesink, query);
1519         }
1520       }
1521       break;
1522     }
1523     case GST_QUERY_DURATION:
1524       res = gst_base_sink_peer_query (basesink, query);
1525       break;
1526     case GST_QUERY_LATENCY:
1527       break;
1528     case GST_QUERY_JITTER:
1529       break;
1530     case GST_QUERY_RATE:
1531       //gst_query_set_rate (query, basesink->segment_rate);
1532       res = TRUE;
1533       break;
1534     case GST_QUERY_SEGMENT:
1535     {
1536       /* FIXME, bring start/stop to stream time */
1537       gst_query_set_segment (query, basesink->segment_rate,
1538           GST_FORMAT_TIME, basesink->segment_start, basesink->segment_stop);
1539       break;
1540     }
1541     case GST_QUERY_SEEKING:
1542     case GST_QUERY_CONVERT:
1543     case GST_QUERY_FORMATS:
1544     default:
1545       res = gst_base_sink_peer_query (basesink, query);
1546       break;
1547   }
1548   return res;
1549 }
1550
1551 static GstStateChangeReturn
1552 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
1553 {
1554   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1555   GstBaseSink *basesink = GST_BASE_SINK (element);
1556   GstBaseSinkClass *bclass;
1557
1558   bclass = GST_BASE_SINK_GET_CLASS (basesink);
1559
1560   switch (transition) {
1561     case GST_STATE_CHANGE_NULL_TO_READY:
1562       if (bclass->start)
1563         if (!bclass->start (basesink))
1564           goto start_failed;
1565       break;
1566     case GST_STATE_CHANGE_READY_TO_PAUSED:
1567       /* need to complete preroll before this state change completes, there
1568        * is no data flow in READY so we can safely assume we need to preroll. */
1569       basesink->offset = 0;
1570       GST_PREROLL_LOCK (basesink->sinkpad);
1571       basesink->have_preroll = FALSE;
1572       GST_DEBUG_OBJECT (basesink, "READY to PAUSED, need preroll to FALSE");
1573       basesink->need_preroll = TRUE;
1574       GST_PREROLL_UNLOCK (basesink->sinkpad);
1575       basesink->have_newsegment = FALSE;
1576       basesink->segment_rate = 1.0;
1577       basesink->segment_start = 0;
1578       basesink->segment_stop = 0;
1579       basesink->segment_time = 0;
1580       basesink->segment_accum = 0;
1581       ret = GST_STATE_CHANGE_ASYNC;
1582       break;
1583     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1584       GST_PREROLL_LOCK (basesink->sinkpad);
1585       /* no preroll needed */
1586       basesink->need_preroll = FALSE;
1587
1588       /* if we have EOS, we should empty the queue now as there will
1589        * be no more data received in the chain function.
1590        * FIXME, this could block the state change function too long when
1591        * we are pushing and syncing the buffers, better start a new
1592        * thread to do this. */
1593       if (basesink->eos) {
1594         gboolean do_eos = !basesink->eos_queued;
1595
1596         gst_base_sink_preroll_queue_empty (basesink, basesink->sinkpad);
1597
1598         /* need to post EOS message here if it was not in the preroll queue we
1599          * just emptied. */
1600         if (do_eos) {
1601           GST_DEBUG_OBJECT (basesink, "Now posting EOS");
1602           gst_element_post_message (GST_ELEMENT_CAST (basesink),
1603               gst_message_new_eos (GST_OBJECT_CAST (basesink)));
1604         }
1605       } else if (!basesink->have_preroll) {
1606         /* queue a commit_state */
1607         basesink->need_preroll = TRUE;
1608         GST_DEBUG_OBJECT (basesink,
1609             "PAUSED to PLAYING, !eos, !have_preroll, need preroll to FALSE");
1610         ret = GST_STATE_CHANGE_ASYNC;
1611         /* we know it's not waiting, no need to signal */
1612       } else {
1613         GST_DEBUG_OBJECT (basesink,
1614             "PAUSED to PLAYING, !eos, have_preroll, need preroll to FALSE");
1615         /* now let it play */
1616         GST_PREROLL_SIGNAL (basesink->sinkpad);
1617       }
1618       GST_PREROLL_UNLOCK (basesink->sinkpad);
1619       break;
1620     default:
1621       break;
1622   }
1623
1624   {
1625     GstStateChangeReturn bret;
1626
1627     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1628     if (bret == GST_STATE_CHANGE_FAILURE)
1629       goto activate_failed;
1630   }
1631
1632   switch (transition) {
1633     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1634     {
1635       GstBaseSinkClass *bclass;
1636
1637       bclass = GST_BASE_SINK_GET_CLASS (basesink);
1638
1639       GST_PREROLL_LOCK (basesink->sinkpad);
1640       GST_LOCK (basesink);
1641       /* unlock clock wait if any */
1642       if (basesink->clock_id) {
1643         gst_clock_id_unschedule (basesink->clock_id);
1644       }
1645       GST_UNLOCK (basesink);
1646
1647       /* unlock any subclasses */
1648       if (bclass->unlock)
1649         bclass->unlock (basesink);
1650
1651       /* if we don't have a preroll buffer and we have not received EOS,
1652        * we need to wait for a preroll */
1653       GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d",
1654           basesink->have_preroll, basesink->eos);
1655       if (!basesink->have_preroll && !basesink->eos
1656           && GST_STATE_PENDING (basesink) == GST_STATE_PAUSED) {
1657         GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, need preroll to TRUE");
1658         basesink->need_preroll = TRUE;
1659         ret = GST_STATE_CHANGE_ASYNC;
1660       }
1661       GST_PREROLL_UNLOCK (basesink->sinkpad);
1662       break;
1663     }
1664     case GST_STATE_CHANGE_PAUSED_TO_READY:
1665       break;
1666     case GST_STATE_CHANGE_READY_TO_NULL:
1667       if (bclass->stop)
1668         if (!bclass->stop (basesink)) {
1669           GST_WARNING ("failed to stop");
1670         }
1671       break;
1672     default:
1673       break;
1674   }
1675
1676   return ret;
1677
1678   /* ERRORS */
1679 start_failed:
1680   {
1681     GST_DEBUG_OBJECT (basesink, "failed to start");
1682     return GST_STATE_CHANGE_FAILURE;
1683   }
1684 activate_failed:
1685   {
1686     GST_DEBUG_OBJECT (basesink,
1687         "element failed to change states -- activation problem?");
1688     return GST_STATE_CHANGE_FAILURE;
1689   }
1690 }