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