renamed GST_FLAGS macros to GST_OBJECT_FLAGS moved bitshift from macro to enum definition
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstbasesrc.c:
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstbasesrc
25  * @short_description: Base class for getrange based source elements
26  * @see_also: #GstBaseTransform, #GstBaseSink
27  *
28  * This class is mostly useful for elements that do byte based
29  * access to a random access resource, like files.
30  * If random access is not possible, the live-mode should be set
31  * to TRUE.
32  *
33  * <itemizedlist>
34  *   <listitem><para>one source pad</para></listitem>
35  *   <listitem><para>handles state changes</para></listitem>
36  *   <listitem><para>does flushing</para></listitem>
37  *   <listitem><para>preroll with optional preview</para></listitem>
38  *   <listitem><para>pull/push mode</para></listitem>
39  *   <listitem><para>EOS handling</para></listitem>
40  * </itemizedlist>
41  */
42
43 #include <stdlib.h>
44 #include <string.h>
45
46 #ifdef HAVE_CONFIG_H
47 #  include "config.h"
48 #endif
49
50 #include "gstbasesrc.h"
51 #include "gsttypefindhelper.h"
52 #include <gst/gstmarshal.h>
53
54 #define DEFAULT_BLOCKSIZE       4096
55 #define DEFAULT_NUM_BUFFERS     -1
56
57 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
58 #define GST_CAT_DEFAULT gst_base_src_debug
59
60 /* BaseSrc signals and args */
61 enum
62 {
63   /* FILL ME */
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   PROP_0,
70   PROP_BLOCKSIZE,
71   PROP_NUM_BUFFERS,
72 };
73
74 static GstElementClass *parent_class = NULL;
75
76 static void gst_base_src_base_init (gpointer g_class);
77 static void gst_base_src_class_init (GstBaseSrcClass * klass);
78 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
79 static void gst_base_src_finalize (GObject * object);
80
81
82 GType
83 gst_base_src_get_type (void)
84 {
85   static GType base_src_type = 0;
86
87   if (!base_src_type) {
88     static const GTypeInfo base_src_info = {
89       sizeof (GstBaseSrcClass),
90       (GBaseInitFunc) gst_base_src_base_init,
91       NULL,
92       (GClassInitFunc) gst_base_src_class_init,
93       NULL,
94       NULL,
95       sizeof (GstBaseSrc),
96       0,
97       (GInstanceInitFunc) gst_base_src_init,
98     };
99
100     base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
101         "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
102   }
103   return base_src_type;
104 }
105 static GstCaps *gst_base_src_getcaps (GstPad * pad);
106 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
107
108 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
109 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
110 static void gst_base_src_set_property (GObject * object, guint prop_id,
111     const GValue * value, GParamSpec * pspec);
112 static void gst_base_src_get_property (GObject * object, guint prop_id,
113     GValue * value, GParamSpec * pspec);
114 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
115
116 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
117
118 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
119 static gboolean gst_base_src_default_newsegment (GstBaseSrc * src);
120
121 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
122 static gboolean gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size);
123 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
124 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
125
126 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
127     GstStateChange transition);
128
129 static void gst_base_src_loop (GstPad * pad);
130 static gboolean gst_base_src_check_get_range (GstPad * pad);
131 static GstFlowReturn gst_base_src_get_range (GstPad * pad, guint64 offset,
132     guint length, GstBuffer ** buf);
133
134 static void
135 gst_base_src_base_init (gpointer g_class)
136 {
137   GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
138 }
139
140 static void
141 gst_base_src_class_init (GstBaseSrcClass * 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_src_finalize);
152   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
153   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
154
155   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
156       g_param_spec_ulong ("blocksize", "Block size",
157           "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
158           G_PARAM_READWRITE));
159
160   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NUM_BUFFERS,
161       g_param_spec_int ("num-buffers", "num-buffers",
162           "Number of buffers to output before sending EOS", -1, G_MAXINT,
163           DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
164
165   gstelement_class->change_state =
166       GST_DEBUG_FUNCPTR (gst_base_src_change_state);
167
168   klass->negotiate = gst_base_src_default_negotiate;
169   klass->newsegment = gst_base_src_default_newsegment;
170 }
171
172 static void
173 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
174 {
175   GstPad *pad;
176   GstPadTemplate *pad_template;
177
178   basesrc->is_live = FALSE;
179   basesrc->live_lock = g_mutex_new ();
180   basesrc->live_cond = g_cond_new ();
181   basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
182   basesrc->num_buffers_left = -1;
183
184   basesrc->can_activate_push = TRUE;
185   basesrc->pad_mode = GST_ACTIVATE_NONE;
186
187   pad_template =
188       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
189   g_return_if_fail (pad_template != NULL);
190
191   GST_DEBUG_OBJECT (basesrc, "creating src pad");
192   pad = gst_pad_new_from_template (pad_template, "src");
193
194   GST_DEBUG_OBJECT (basesrc, "setting functions on src pad");
195   gst_pad_set_activatepush_function (pad,
196       GST_DEBUG_FUNCPTR (gst_base_src_activate_push));
197   gst_pad_set_activatepull_function (pad,
198       GST_DEBUG_FUNCPTR (gst_base_src_activate_pull));
199   gst_pad_set_event_function (pad,
200       GST_DEBUG_FUNCPTR (gst_base_src_event_handler));
201   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_query));
202   gst_pad_set_checkgetrange_function (pad,
203       GST_DEBUG_FUNCPTR (gst_base_src_check_get_range));
204   gst_pad_set_getrange_function (pad,
205       GST_DEBUG_FUNCPTR (gst_base_src_get_range));
206   gst_pad_set_getcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_getcaps));
207   gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_base_src_setcaps));
208
209   /* hold pointer to pad */
210   basesrc->srcpad = pad;
211   GST_DEBUG_OBJECT (basesrc, "adding src pad");
212   gst_element_add_pad (GST_ELEMENT (basesrc), pad);
213
214   basesrc->segment_start = -1;
215   basesrc->segment_end = -1;
216   basesrc->need_newsegment = TRUE;
217   basesrc->blocksize = DEFAULT_BLOCKSIZE;
218   basesrc->clock_id = NULL;
219
220   GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
221
222   GST_DEBUG_OBJECT (basesrc, "init done");
223 }
224
225 static void
226 gst_base_src_finalize (GObject * object)
227 {
228   GstBaseSrc *basesrc;
229
230   basesrc = GST_BASE_SRC (object);
231
232   g_mutex_free (basesrc->live_lock);
233   g_cond_free (basesrc->live_cond);
234
235   G_OBJECT_CLASS (parent_class)->finalize (object);
236 }
237
238 /**
239  * gst_base_src_set_live:
240  * @src: base source instance
241  * @live: new live-mode
242  *
243  * If the element listens to a live source, the @livemode should
244  * be set to %TRUE. This declares that this source can't seek.
245  */
246 void
247 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
248 {
249   GST_LIVE_LOCK (src);
250   src->is_live = live;
251   GST_LIVE_UNLOCK (src);
252 }
253
254 /**
255  * gst_base_src_is_live:
256  * @src: base source instance
257  *
258  * Check if an element is in live mode.
259  *
260  * Returns: %TRUE if element is in live mode.
261  */
262 gboolean
263 gst_base_src_is_live (GstBaseSrc * src)
264 {
265   gboolean result;
266
267   GST_LIVE_LOCK (src);
268   result = src->is_live;
269   GST_LIVE_UNLOCK (src);
270
271   return result;
272 }
273
274 static gboolean
275 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
276 {
277   GstBaseSrcClass *bclass;
278   GstBaseSrc *bsrc;
279   gboolean res = TRUE;
280
281   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
282   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
283
284   if (bclass->set_caps)
285     res = bclass->set_caps (bsrc, caps);
286
287   return res;
288 }
289
290 static GstCaps *
291 gst_base_src_getcaps (GstPad * pad)
292 {
293   GstBaseSrcClass *bclass;
294   GstBaseSrc *bsrc;
295   GstCaps *caps = NULL;
296
297   bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
298   bclass = GST_BASE_SRC_GET_CLASS (bsrc);
299   if (bclass->get_caps)
300     caps = bclass->get_caps (bsrc);
301
302   if (caps == NULL) {
303     GstPadTemplate *pad_template;
304
305     pad_template =
306         gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
307     if (pad_template != NULL) {
308       caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
309     }
310   }
311   return caps;
312 }
313
314 static gboolean
315 gst_base_src_query (GstPad * pad, GstQuery * query)
316 {
317   gboolean b;
318   guint64 ui64;
319   gint64 i64;
320   GstBaseSrc *src;
321
322   src = GST_BASE_SRC (GST_PAD_PARENT (pad));
323
324   switch (GST_QUERY_TYPE (query)) {
325     case GST_QUERY_POSITION:
326     {
327       GstFormat format;
328
329       gst_query_parse_position (query, &format, NULL, NULL);
330       switch (format) {
331         case GST_FORMAT_DEFAULT:
332         case GST_FORMAT_BYTES:
333           b = gst_base_src_get_size (src, &ui64);
334           /* better to make get_size take an int64 */
335           i64 = b ? (gint64) ui64 : -1;
336           gst_query_set_position (query, GST_FORMAT_BYTES, src->offset, i64);
337           return TRUE;
338         case GST_FORMAT_PERCENT:
339           b = gst_base_src_get_size (src, &ui64);
340           i64 = GST_FORMAT_PERCENT_MAX;
341           i64 *= b ? (src->offset / gst_guint64_to_gdouble (ui64)) : 1.0;
342           gst_query_set_position (query, GST_FORMAT_PERCENT,
343               i64, GST_FORMAT_PERCENT_MAX);
344           return TRUE;
345         default:
346           return FALSE;
347       }
348     }
349
350     case GST_QUERY_SEEKING:
351       gst_query_set_seeking (query, GST_FORMAT_BYTES,
352           src->seekable, src->segment_start, src->segment_end);
353       return TRUE;
354
355     case GST_QUERY_FORMATS:
356       gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
357           GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
358       return TRUE;
359
360     case GST_QUERY_LATENCY:
361     case GST_QUERY_JITTER:
362     case GST_QUERY_RATE:
363     case GST_QUERY_CONVERT:
364     default:
365       return gst_pad_query_default (pad, query);
366   }
367 }
368
369 static gboolean
370 gst_base_src_default_newsegment (GstBaseSrc * src)
371 {
372   GstEvent *event;
373
374   GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
375       " to %" G_GINT64_FORMAT, (gint64) src->segment_start,
376       (gint64) src->segment_end);
377   event = gst_event_new_newsegment (FALSE, 1.0,
378       GST_FORMAT_BYTES,
379       (gint64) src->segment_start, (gint64) src->segment_end, (gint64) 0);
380
381   return gst_pad_push_event (src->srcpad, event);
382 }
383
384 static gboolean
385 gst_base_src_newsegment (GstBaseSrc * src)
386 {
387   GstBaseSrcClass *bclass;
388   gboolean result = FALSE;
389
390   bclass = GST_BASE_SRC_GET_CLASS (src);
391
392   if (bclass->newsegment)
393     result = bclass->newsegment (src);
394
395   return result;
396 }
397
398 static gboolean
399 gst_base_src_do_seek (GstBaseSrc * src, GstEvent * event)
400 {
401   gdouble rate;
402   GstFormat format;
403   GstSeekFlags flags;
404   GstSeekType cur_type, stop_type;
405   gint64 cur, stop;
406
407   gst_event_parse_seek (event, &rate, &format, &flags,
408       &cur_type, &cur, &stop_type, &stop);
409
410   /* get seek format */
411   if (format == GST_FORMAT_DEFAULT)
412     format = GST_FORMAT_BYTES;
413   /* we can only seek bytes */
414   if (format != GST_FORMAT_BYTES)
415     return FALSE;
416
417   /* get seek positions */
418   src->segment_loop = flags & GST_SEEK_FLAG_SEGMENT;
419
420   /* send flush start */
421   gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
422
423   /* unblock streaming thread */
424   gst_base_src_unlock (src);
425
426   /* grab streaming lock */
427   GST_STREAM_LOCK (src->srcpad);
428
429   /* send flush stop */
430   gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
431
432   /* perform the seek */
433   switch (cur_type) {
434     case GST_SEEK_TYPE_NONE:
435       break;
436     case GST_SEEK_TYPE_SET:
437       if (cur < 0)
438         goto error;
439       src->offset = MIN (cur, src->size);
440       src->segment_start = src->offset;
441       break;
442     case GST_SEEK_TYPE_CUR:
443       src->offset = CLAMP (src->offset + cur, 0, src->size);
444       src->segment_start = src->offset;
445       break;
446     case GST_SEEK_TYPE_END:
447       if (cur > 0)
448         goto error;
449       src->offset = MAX (0, src->size + cur);
450       src->segment_start = src->offset;
451       break;
452     default:
453       goto error;
454   }
455
456   switch (stop_type) {
457     case GST_SEEK_TYPE_NONE:
458       break;
459     case GST_SEEK_TYPE_SET:
460       if (stop < 0)
461         goto error;
462       src->segment_end = MIN (stop, src->size);
463       break;
464     case GST_SEEK_TYPE_CUR:
465       src->segment_end = CLAMP (src->segment_end + stop, 0, src->size);
466       break;
467     case GST_SEEK_TYPE_END:
468       if (stop > 0)
469         goto error;
470       src->segment_end = src->size + stop;
471       break;
472     default:
473       goto error;
474   }
475
476   GST_DEBUG_OBJECT (src, "seek pending for segment from %" G_GINT64_FORMAT
477       " to %" G_GINT64_FORMAT, src->segment_start, src->segment_end);
478
479   /* now make sure the newsegment will be send */
480   src->need_newsegment = TRUE;
481
482   /* and restart the task */
483   gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
484       src->srcpad);
485   GST_STREAM_UNLOCK (src->srcpad);
486
487   gst_event_unref (event);
488
489   return TRUE;
490
491   /* ERROR */
492 error:
493   {
494     GST_DEBUG_OBJECT (src, "seek error");
495     GST_STREAM_UNLOCK (src->srcpad);
496     gst_event_unref (event);
497     return FALSE;
498   }
499 }
500
501 static gboolean
502 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
503 {
504   GstBaseSrc *src;
505   GstBaseSrcClass *bclass;
506   gboolean result;
507
508   src = GST_BASE_SRC (GST_PAD_PARENT (pad));
509   bclass = GST_BASE_SRC_GET_CLASS (src);
510
511   if (bclass->event)
512     result = bclass->event (src, event);
513
514   switch (GST_EVENT_TYPE (event)) {
515     case GST_EVENT_SEEK:
516       if (!src->seekable) {
517         gst_event_unref (event);
518         return FALSE;
519       }
520       return gst_base_src_do_seek (src, event);
521     case GST_EVENT_FLUSH_START:
522       /* cancel any blocking getrange */
523       gst_base_src_unlock (src);
524       break;
525     case GST_EVENT_FLUSH_STOP:
526       break;
527     default:
528       break;
529   }
530   gst_event_unref (event);
531
532   return TRUE;
533 }
534
535 static void
536 gst_base_src_set_property (GObject * object, guint prop_id,
537     const GValue * value, GParamSpec * pspec)
538 {
539   GstBaseSrc *src;
540
541   src = GST_BASE_SRC (object);
542
543   switch (prop_id) {
544     case PROP_BLOCKSIZE:
545       src->blocksize = g_value_get_ulong (value);
546       break;
547     case PROP_NUM_BUFFERS:
548       src->num_buffers = g_value_get_int (value);
549       break;
550     default:
551       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
552       break;
553   }
554 }
555
556 static void
557 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
558     GParamSpec * pspec)
559 {
560   GstBaseSrc *src;
561
562   src = GST_BASE_SRC (object);
563
564   switch (prop_id) {
565     case PROP_BLOCKSIZE:
566       g_value_set_ulong (value, src->blocksize);
567       break;
568     case PROP_NUM_BUFFERS:
569       g_value_set_int (value, src->num_buffers);
570       break;
571     default:
572       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573       break;
574   }
575 }
576
577 static GstFlowReturn
578 gst_base_src_get_range (GstPad * pad, guint64 offset, guint length,
579     GstBuffer ** buf)
580 {
581   GstFlowReturn ret;
582   GstBaseSrc *src;
583   GstBaseSrcClass *bclass;
584
585   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
586   bclass = GST_BASE_SRC_GET_CLASS (src);
587
588   GST_LIVE_LOCK (src);
589   if (src->is_live) {
590     while (!src->live_running) {
591       GST_DEBUG ("live source signal waiting");
592       GST_LIVE_SIGNAL (src);
593       GST_DEBUG ("live source waiting for running state");
594       GST_LIVE_WAIT (src);
595       GST_DEBUG ("live source unlocked");
596     }
597   }
598   GST_LIVE_UNLOCK (src);
599
600   GST_LOCK (pad);
601   if (GST_PAD_IS_FLUSHING (pad))
602     goto flushing;
603   GST_UNLOCK (pad);
604
605   if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED))
606     goto not_started;
607
608   if (!bclass->create)
609     goto no_function;
610
611   GST_DEBUG_OBJECT (src,
612       "reading offset %" G_GUINT64_FORMAT ", length %u, size %"
613       G_GUINT64_FORMAT, offset, length, src->size);
614
615   /* check size */
616   if (src->size != -1) {
617     if (offset > src->size)
618       goto unexpected_length;
619
620     if (offset + length > src->size) {
621       if (bclass->get_size)
622         bclass->get_size (src, &src->size);
623
624       if (offset + length > src->size) {
625         length = src->size - offset;
626       }
627     }
628   }
629   if (length == 0)
630     goto unexpected_length;
631
632   if (src->num_buffers_left == 0) {
633     goto reached_num_buffers;
634   } else {
635     if (src->num_buffers_left > 0)
636       src->num_buffers_left--;
637   }
638
639   ret = bclass->create (src, offset, length, buf);
640
641   return ret;
642
643   /* ERROR */
644 flushing:
645   {
646     GST_DEBUG_OBJECT (src, "pad is flushing");
647     GST_UNLOCK (pad);
648     return GST_FLOW_WRONG_STATE;
649   }
650 not_started:
651   {
652     GST_DEBUG_OBJECT (src, "getrange but not started");
653     return GST_FLOW_WRONG_STATE;
654   }
655 no_function:
656   {
657     GST_DEBUG_OBJECT (src, "no create function");
658     return GST_FLOW_ERROR;
659   }
660 unexpected_length:
661   {
662     GST_DEBUG_OBJECT (src, "unexpected length %u (offset=%" G_GUINT64_FORMAT
663         ", size=%" G_GUINT64_FORMAT ")", length, offset, src->size);
664     return GST_FLOW_UNEXPECTED;
665   }
666 reached_num_buffers:
667   {
668     GST_DEBUG_OBJECT (src, "sent all buffers");
669     return GST_FLOW_UNEXPECTED;
670   }
671 }
672
673 static gboolean
674 gst_base_src_check_get_range (GstPad * pad)
675 {
676   GstBaseSrc *src;
677
678   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
679
680   if (!GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
681     gst_base_src_start (src);
682     gst_base_src_stop (src);
683   }
684
685   return src->seekable;
686 }
687
688 static void
689 gst_base_src_loop (GstPad * pad)
690 {
691   GstBaseSrc *src;
692   GstBuffer *buf = NULL;
693   GstFlowReturn ret;
694
695   src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
696
697   if (src->need_newsegment) {
698     /* now send newsegment */
699     gst_base_src_newsegment (src);
700     src->need_newsegment = FALSE;
701   }
702
703   ret = gst_base_src_get_range (pad, src->offset, src->blocksize, &buf);
704   if (ret != GST_FLOW_OK)
705     goto eos;
706
707   if (buf == NULL)
708     goto error;
709
710   src->offset += GST_BUFFER_SIZE (buf);
711
712   ret = gst_pad_push (pad, buf);
713   if (ret != GST_FLOW_OK)
714     goto pause;
715
716   return;
717
718 eos:
719   {
720     GST_DEBUG_OBJECT (src, "going to EOS");
721     gst_pad_pause_task (pad);
722     gst_pad_push_event (pad, gst_event_new_eos ());
723     return;
724   }
725 pause:
726   {
727     GST_DEBUG_OBJECT (src, "pausing task");
728     gst_pad_pause_task (pad);
729     if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
730       /* for fatal errors we post an error message */
731       GST_ELEMENT_ERROR (src, STREAM, STOPPED,
732           ("streaming stopped, reason %s", gst_flow_get_name (ret)),
733           ("streaming stopped, reason %s", gst_flow_get_name (ret)));
734       gst_pad_push_event (pad, gst_event_new_eos ());
735     }
736     return;
737   }
738 error:
739   {
740     GST_ELEMENT_ERROR (src, STREAM, STOPPED,
741         ("internal: element returned NULL buffer"),
742         ("internal: element returned NULL buffer"));
743     gst_pad_pause_task (pad);
744     gst_pad_push_event (pad, gst_event_new_eos ());
745     return;
746   }
747 }
748
749 /* this will always be called between start() and stop(). So you can rely on
750    resources allocated by start() and freed from stop(). This needs to be added
751    to the docs at some point. */
752 static gboolean
753 gst_base_src_unlock (GstBaseSrc * basesrc)
754 {
755   GstBaseSrcClass *bclass;
756   gboolean result = FALSE;
757
758   GST_DEBUG ("unlock");
759   /* unblock whatever the subclass is doing */
760   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
761   if (bclass->unlock)
762     result = bclass->unlock (basesrc);
763
764   GST_DEBUG ("unschedule clock");
765   /* and unblock the clock as well, if any */
766   GST_LOCK (basesrc);
767   if (basesrc->clock_id) {
768     gst_clock_id_unschedule (basesrc->clock_id);
769   }
770   GST_UNLOCK (basesrc);
771
772   GST_DEBUG ("unlock done");
773
774   return result;
775 }
776
777 static gboolean
778 gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size)
779 {
780   GstBaseSrcClass *bclass;
781   gboolean result = FALSE;
782
783   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
784   if (bclass->get_size)
785     result = bclass->get_size (basesrc, size);
786
787   if (result)
788     basesrc->size = *size;
789
790   return result;
791 }
792
793 static gboolean
794 gst_base_src_is_seekable (GstBaseSrc * basesrc)
795 {
796   GstBaseSrcClass *bclass;
797
798   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
799
800   /* check if we can seek */
801   if (bclass->is_seekable)
802     basesrc->seekable = bclass->is_seekable (basesrc);
803   else
804     basesrc->seekable = FALSE;
805
806   GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
807
808   return basesrc->seekable;
809 }
810
811 static gboolean
812 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
813 {
814   GstCaps *thiscaps;
815   GstCaps *caps = NULL;
816   GstCaps *peercaps = NULL;
817   gboolean result = FALSE;
818
819   thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
820   GST_DEBUG ("caps of src: %" GST_PTR_FORMAT, thiscaps);
821   if (thiscaps == NULL || gst_caps_is_any (thiscaps))
822     goto no_nego_needed;
823
824   peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
825   GST_DEBUG ("caps of peer: %" GST_PTR_FORMAT, peercaps);
826   if (peercaps) {
827     GstCaps *icaps;
828
829     icaps = gst_caps_intersect (thiscaps, peercaps);
830     GST_DEBUG ("intersect: %" GST_PTR_FORMAT, icaps);
831     gst_caps_unref (thiscaps);
832     gst_caps_unref (peercaps);
833     if (icaps) {
834       caps = gst_caps_copy_nth (icaps, 0);
835       gst_caps_unref (icaps);
836     }
837   } else {
838     caps = thiscaps;
839   }
840   if (caps) {
841     caps = gst_caps_make_writable (caps);
842     gst_caps_truncate (caps);
843
844     gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
845     GST_DEBUG ("fixated to: %" GST_PTR_FORMAT, caps);
846
847     if (gst_caps_is_any (caps)) {
848       gst_caps_unref (caps);
849       result = TRUE;
850     } else if (gst_caps_is_fixed (caps)) {
851       gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
852       gst_caps_unref (caps);
853       result = TRUE;
854     }
855   }
856   return result;
857
858 no_nego_needed:
859   {
860     GST_DEBUG ("no negotiation needed");
861     if (thiscaps)
862       gst_caps_unref (thiscaps);
863     return TRUE;
864   }
865 }
866
867 static gboolean
868 gst_base_src_negotiate (GstBaseSrc * basesrc)
869 {
870   GstBaseSrcClass *bclass;
871   gboolean result = TRUE;
872
873   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
874
875   if (bclass->negotiate)
876     result = bclass->negotiate (basesrc);
877
878   return result;
879 }
880
881 static gboolean
882 gst_base_src_start (GstBaseSrc * basesrc)
883 {
884   GstBaseSrcClass *bclass;
885   gboolean result;
886
887   if (GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
888     return TRUE;
889
890   GST_DEBUG_OBJECT (basesrc, "starting source");
891
892   basesrc->num_buffers_left = basesrc->num_buffers;
893
894   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
895   if (bclass->start)
896     result = bclass->start (basesrc);
897   else
898     result = TRUE;
899
900   if (!result)
901     goto could_not_start;
902
903   GST_OBJECT_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
904
905   /* start in the beginning */
906   basesrc->offset = 0;
907
908   /* figure out the size */
909   if (bclass->get_size) {
910     result = bclass->get_size (basesrc, &basesrc->size);
911     if (result == FALSE)
912       basesrc->size = -1;
913   } else {
914     result = FALSE;
915     basesrc->size = -1;
916   }
917
918   GST_DEBUG ("size %d %lld", result, basesrc->size);
919
920   /* we always run to the end */
921   basesrc->segment_start = 0;
922   basesrc->segment_end = basesrc->size;
923   basesrc->need_newsegment = TRUE;
924
925   /* check if we can seek, updates ->seekable */
926   gst_base_src_is_seekable (basesrc);
927
928   /* run typefind */
929 #if 0
930   if (basesrc->seekable) {
931     GstCaps *caps;
932
933     caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
934     gst_pad_set_caps (basesrc->srcpad, caps);
935     gst_caps_unref (caps);
936   }
937 #endif
938
939   if (!gst_base_src_negotiate (basesrc))
940     goto could_not_negotiate;
941
942   return TRUE;
943
944   /* ERROR */
945 could_not_start:
946   {
947     GST_DEBUG_OBJECT (basesrc, "could not start");
948     return FALSE;
949   }
950 could_not_negotiate:
951   {
952     GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
953     GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
954         ("Could not connect source to pipeline"),
955         ("Check your filtered caps, if any"));
956     gst_base_src_stop (basesrc);
957     return FALSE;
958   }
959 }
960
961 static gboolean
962 gst_base_src_stop (GstBaseSrc * basesrc)
963 {
964   GstBaseSrcClass *bclass;
965   gboolean result = TRUE;
966
967   if (!GST_OBJECT_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
968     return TRUE;
969
970   GST_DEBUG_OBJECT (basesrc, "stopping source");
971
972   bclass = GST_BASE_SRC_GET_CLASS (basesrc);
973   if (bclass->stop)
974     result = bclass->stop (basesrc);
975
976   if (result)
977     GST_OBJECT_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
978
979   return result;
980 }
981
982 static gboolean
983 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
984 {
985   gboolean result;
986
987   GST_LIVE_LOCK (basesrc);
988   basesrc->live_running = TRUE;
989   GST_LIVE_SIGNAL (basesrc);
990   GST_LIVE_UNLOCK (basesrc);
991
992   /* step 1, unblock clock sync (if any) */
993   gst_base_src_unlock (basesrc);
994
995   /* step 2, make sure streaming finishes */
996   result = gst_pad_stop_task (pad);
997
998   return result;
999 }
1000
1001 static gboolean
1002 gst_base_src_activate_push (GstPad * pad, gboolean active)
1003 {
1004   GstBaseSrc *basesrc;
1005
1006   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1007
1008   /* prepare subclass first */
1009   if (active) {
1010     GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1011
1012     if (!basesrc->can_activate_push)
1013       goto no_push_activation;
1014
1015     if (!gst_base_src_start (basesrc))
1016       goto error_start;
1017
1018     return gst_pad_start_task (pad, (GstTaskFunction) gst_base_src_loop, pad);
1019   } else {
1020     GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1021     return gst_base_src_deactivate (basesrc, pad);
1022   }
1023
1024 no_push_activation:
1025   {
1026     GST_DEBUG_OBJECT (basesrc, "Subclass disabled push-mode activation");
1027     return FALSE;
1028   }
1029 error_start:
1030   {
1031     gst_base_src_stop (basesrc);
1032     GST_DEBUG_OBJECT (basesrc, "Failed to start in push mode");
1033     return FALSE;
1034   }
1035 }
1036
1037 static gboolean
1038 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1039 {
1040   GstBaseSrc *basesrc;
1041
1042   basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1043
1044   /* prepare subclass first */
1045   if (active) {
1046     GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1047     if (!gst_base_src_start (basesrc))
1048       goto error_start;
1049
1050     if (!basesrc->seekable) {
1051       gst_base_src_stop (basesrc);
1052       return FALSE;
1053     }
1054
1055     return TRUE;
1056   } else {
1057     GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
1058
1059     if (!gst_base_src_stop (basesrc))
1060       goto error_stop;
1061
1062     return gst_base_src_deactivate (basesrc, pad);
1063   }
1064
1065 error_start:
1066   {
1067     gst_base_src_stop (basesrc);
1068     GST_DEBUG_OBJECT (basesrc, "Failed to start in pull mode");
1069     return FALSE;
1070   }
1071 error_stop:
1072   {
1073     GST_DEBUG_OBJECT (basesrc, "Failed to stop in pull mode");
1074     return FALSE;
1075   }
1076 }
1077
1078 static GstStateChangeReturn
1079 gst_base_src_change_state (GstElement * element, GstStateChange transition)
1080 {
1081   GstBaseSrc *basesrc;
1082   GstStateChangeReturn result;
1083   gboolean no_preroll = FALSE;
1084
1085   basesrc = GST_BASE_SRC (element);
1086
1087
1088   switch (transition) {
1089     case GST_STATE_CHANGE_NULL_TO_READY:
1090       break;
1091     case GST_STATE_CHANGE_READY_TO_PAUSED:
1092       GST_LIVE_LOCK (element);
1093       if (basesrc->is_live) {
1094         no_preroll = TRUE;
1095         basesrc->live_running = FALSE;
1096       }
1097       GST_LIVE_UNLOCK (element);
1098       break;
1099     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1100       GST_LIVE_LOCK (element);
1101       if (basesrc->is_live) {
1102         basesrc->live_running = TRUE;
1103         GST_LIVE_SIGNAL (element);
1104       }
1105       GST_LIVE_UNLOCK (element);
1106       break;
1107     default:
1108       break;
1109   }
1110
1111   if ((result =
1112           GST_ELEMENT_CLASS (parent_class)->change_state (element,
1113               transition)) == GST_STATE_CHANGE_FAILURE)
1114     goto failure;
1115
1116   switch (transition) {
1117     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1118       GST_LIVE_LOCK (element);
1119       if (basesrc->is_live) {
1120         no_preroll = TRUE;
1121         basesrc->live_running = FALSE;
1122       }
1123       GST_LIVE_UNLOCK (element);
1124       break;
1125     case GST_STATE_CHANGE_PAUSED_TO_READY:
1126       if (!gst_base_src_stop (basesrc))
1127         result = GST_STATE_CHANGE_FAILURE;
1128       break;
1129     case GST_STATE_CHANGE_READY_TO_NULL:
1130       break;
1131     default:
1132       break;
1133   }
1134
1135   if (no_preroll && result == GST_STATE_CHANGE_SUCCESS)
1136     return GST_STATE_CHANGE_NO_PREROLL;
1137   else
1138     return result;
1139
1140   /* ERRORS */
1141 failure:
1142   {
1143     gst_base_src_stop (basesrc);
1144     return result;
1145   }
1146 }