2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
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.
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.
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.
25 * @short_description: Base class for getrange based source elements
26 * @see_also: #GstBaseTransformc, #GstBaseSink
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
34 * <listitem><para>one sinkpad</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>
50 #include "gstbasesrc.h"
51 #include "gsttypefindhelper.h"
52 #include <gst/gstmarshal.h>
54 #define DEFAULT_BLOCKSIZE 4096
55 #define DEFAULT_NUM_BUFFERS -1
57 GST_DEBUG_CATEGORY_STATIC (gst_base_src_debug);
58 #define GST_CAT_DEFAULT gst_base_src_debug
60 /* BaseSrc signals and args */
76 static GstElementClass *parent_class = NULL;
78 static void gst_base_src_base_init (gpointer g_class);
79 static void gst_base_src_class_init (GstBaseSrcClass * klass);
80 static void gst_base_src_init (GstBaseSrc * src, gpointer g_class);
81 static void gst_base_src_finalize (GObject * object);
85 gst_base_src_get_type (void)
87 static GType base_src_type = 0;
90 static const GTypeInfo base_src_info = {
91 sizeof (GstBaseSrcClass),
92 (GBaseInitFunc) gst_base_src_base_init,
94 (GClassInitFunc) gst_base_src_class_init,
99 (GInstanceInitFunc) gst_base_src_init,
102 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
103 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
105 return base_src_type;
107 static GstCaps *gst_base_src_getcaps (GstPad * pad);
108 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
110 static gboolean gst_base_src_activate_push (GstPad * pad, gboolean active);
111 static gboolean gst_base_src_activate_pull (GstPad * pad, gboolean active);
112 static void gst_base_src_set_property (GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * pspec);
114 static void gst_base_src_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec);
116 static gboolean gst_base_src_event_handler (GstPad * pad, GstEvent * event);
118 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
121 static const GstEventMask *gst_base_src_get_event_mask (GstPad * pad);
123 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
125 static gboolean gst_base_src_unlock (GstBaseSrc * basesrc);
126 static gboolean gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size);
127 static gboolean gst_base_src_start (GstBaseSrc * basesrc);
128 static gboolean gst_base_src_stop (GstBaseSrc * basesrc);
130 static GstElementStateReturn gst_base_src_change_state (GstElement * element);
132 static void gst_base_src_set_dataflow_funcs (GstBaseSrc * this);
133 static void gst_base_src_loop (GstPad * pad);
134 static gboolean gst_base_src_check_get_range (GstPad * pad);
135 static GstFlowReturn gst_base_src_get_range (GstPad * pad, guint64 offset,
136 guint length, GstBuffer ** buf);
139 gst_base_src_base_init (gpointer g_class)
141 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
145 gst_base_src_class_init (GstBaseSrcClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
150 gobject_class = (GObjectClass *) klass;
151 gstelement_class = (GstElementClass *) klass;
153 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
155 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_src_finalize);
156 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_src_set_property);
157 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_src_get_property);
159 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
160 g_param_spec_ulong ("blocksize", "Block size",
161 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
164 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
165 g_param_spec_boolean ("has-loop", "Has loop function",
166 "True if the element should expose a loop function", TRUE,
167 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
169 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
170 g_param_spec_boolean ("has-getrange", "Has getrange function",
171 "True if the element should expose a getrange function", TRUE,
172 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
174 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_NUM_BUFFERS,
175 g_param_spec_int ("num-buffers", "num-buffers",
176 "Number of buffers to output before sending EOS", -1, G_MAXINT,
177 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE));
179 gstelement_class->change_state =
180 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
182 klass->negotiate = gst_base_src_default_negotiate;
186 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
189 GstPadTemplate *pad_template;
191 basesrc->is_live = FALSE;
192 basesrc->live_lock = g_mutex_new ();
193 basesrc->live_cond = g_cond_new ();
194 basesrc->num_buffers = DEFAULT_NUM_BUFFERS;
195 basesrc->num_buffers_left = -1;
198 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
199 g_return_if_fail (pad_template != NULL);
201 pad = gst_pad_new_from_template (pad_template, "src");
203 gst_pad_set_activatepush_function (pad, gst_base_src_activate_push);
204 gst_pad_set_activatepull_function (pad, gst_base_src_activate_pull);
205 gst_pad_set_event_function (pad, gst_base_src_event_handler);
206 gst_pad_set_query_function (pad, gst_base_src_query);
207 gst_pad_set_checkgetrange_function (pad, gst_base_src_check_get_range);
208 gst_pad_set_getcaps_function (pad, gst_base_src_getcaps);
209 gst_pad_set_setcaps_function (pad, gst_base_src_setcaps);
211 /* hold ref to pad */
212 basesrc->srcpad = pad;
213 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
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;
221 GST_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
225 gst_base_src_finalize (GObject * object)
229 basesrc = GST_BASE_SRC (object);
231 g_mutex_free (basesrc->live_lock);
232 g_cond_free (basesrc->live_cond);
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 * gst_base_src_set_live:
239 * @src: base source instance
240 * @live: new live-mode
242 * If the element listens to a live source, the @livemode should
243 * be set to %TRUE. This declares that this source can't seek.
246 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
250 GST_LIVE_UNLOCK (src);
254 * gst_base_src_get_live:
255 * @src: base source instance
257 * Check if an element is in live mode.
259 * Returns: %TRUE if element is in live mode.
262 gst_base_src_is_live (GstBaseSrc * src)
267 result = src->is_live;
268 GST_LIVE_UNLOCK (src);
274 gst_base_src_set_dataflow_funcs (GstBaseSrc * this)
276 GST_DEBUG ("updating dataflow functions");
278 if (this->has_getrange)
279 gst_pad_set_getrange_function (this->srcpad, gst_base_src_get_range);
281 gst_pad_set_getrange_function (this->srcpad, NULL);
285 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
287 GstBaseSrcClass *bclass;
291 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
292 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
294 if (bclass->set_caps)
295 res = bclass->set_caps (bsrc, caps);
301 gst_base_src_getcaps (GstPad * pad)
303 GstBaseSrcClass *bclass;
305 GstCaps *caps = NULL;
307 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
308 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
309 if (bclass->get_caps)
310 caps = bclass->get_caps (bsrc);
313 GstPadTemplate *pad_template;
316 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
317 if (pad_template != NULL) {
318 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
325 gst_base_src_query (GstPad * pad, GstQuery * query)
332 src = GST_BASE_SRC (GST_PAD_PARENT (pad));
334 switch (GST_QUERY_TYPE (query)) {
335 case GST_QUERY_POSITION:
339 gst_query_parse_position (query, &format, NULL, NULL);
341 case GST_FORMAT_DEFAULT:
342 case GST_FORMAT_BYTES:
343 b = gst_base_src_get_size (src, &ui64);
344 /* better to make get_size take an int64 */
345 i64 = b ? (gint64) ui64 : -1;
346 gst_query_set_position (query, GST_FORMAT_BYTES, src->offset, i64);
348 case GST_FORMAT_PERCENT:
349 b = gst_base_src_get_size (src, &ui64);
350 i64 = GST_FORMAT_PERCENT_MAX;
351 i64 *= b ? (src->offset / (gdouble) ui64) : 1.0;
352 gst_query_set_position (query, GST_FORMAT_PERCENT,
353 i64, GST_FORMAT_PERCENT_MAX);
360 case GST_QUERY_SEEKING:
361 gst_query_set_seeking (query, GST_FORMAT_BYTES,
362 src->seekable, src->segment_start, src->segment_end);
365 case GST_QUERY_FORMATS:
366 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
367 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
370 case GST_QUERY_LATENCY:
371 case GST_QUERY_JITTER:
373 case GST_QUERY_CONVERT:
375 return gst_pad_query_default (pad, query);
380 static const GstEventMask *
381 gst_base_src_get_event_mask (GstPad * pad)
383 static const GstEventMask masks[] = {
384 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
385 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
386 GST_SEEK_FLAG_SEGMENT_LOOP},
387 {GST_EVENT_FLUSH, 0},
396 gst_base_src_send_discont (GstBaseSrc * src)
400 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
401 " to %" G_GINT64_FORMAT, (gint64) src->segment_start,
402 (gint64) src->segment_end);
403 event = gst_event_new_newsegment (1.0,
405 (gint64) src->segment_start, (gint64) src->segment_end, (gint64) 0);
407 return gst_pad_push_event (src->srcpad, event);
411 gst_base_src_do_seek (GstBaseSrc * src, GstEvent * event)
416 GstSeekType cur_type, stop_type;
419 gst_event_parse_seek (event, &rate, &format, &flags,
420 &cur_type, &cur, &stop_type, &stop);
422 /* get seek format */
423 if (format == GST_FORMAT_DEFAULT)
424 format = GST_FORMAT_BYTES;
425 /* we can only seek bytes */
426 if (format != GST_FORMAT_BYTES)
429 /* get seek positions */
430 src->segment_loop = flags & GST_SEEK_FLAG_SEGMENT;
432 /* send flush start */
433 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
435 /* unblock streaming thread */
436 gst_base_src_unlock (src);
438 /* grab streaming lock */
439 GST_STREAM_LOCK (src->srcpad);
441 /* send flush stop */
442 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
444 /* perform the seek */
446 case GST_SEEK_TYPE_SET:
449 src->offset = MIN (cur, src->size);
450 src->segment_start = src->offset;
451 src->segment_end = MIN (stop, src->size);
452 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
455 case GST_SEEK_TYPE_CUR:
457 src->offset = CLAMP (cur, 0, src->size);
458 src->segment_start = src->offset;
459 src->segment_end = stop;
460 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
463 case GST_SEEK_TYPE_END:
466 cur = src->size + cur;
467 src->offset = MAX (0, cur);
468 src->segment_start = src->offset;
469 src->segment_end = stop;
470 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
477 /* now make sure the discont will be send */
478 src->need_discont = TRUE;
480 /* and restart the task */
481 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
483 GST_STREAM_UNLOCK (src->srcpad);
485 gst_event_unref (event);
492 GST_DEBUG_OBJECT (src, "seek error");
493 GST_STREAM_UNLOCK (src->srcpad);
494 gst_event_unref (event);
500 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
503 GstBaseSrcClass *bclass;
506 src = GST_BASE_SRC (GST_PAD_PARENT (pad));
507 bclass = GST_BASE_SRC_GET_CLASS (src);
510 result = bclass->event (src, event);
512 switch (GST_EVENT_TYPE (event)) {
514 return gst_base_src_do_seek (src, event);
515 case GST_EVENT_FLUSH_START:
516 /* cancel any blocking getrange */
517 gst_base_src_unlock (src);
519 case GST_EVENT_FLUSH_STOP:
524 gst_event_unref (event);
530 gst_base_src_set_property (GObject * object, guint prop_id,
531 const GValue * value, GParamSpec * pspec)
535 src = GST_BASE_SRC (object);
539 src->blocksize = g_value_get_ulong (value);
542 src->has_loop = g_value_get_boolean (value);
543 gst_base_src_set_dataflow_funcs (src);
545 case PROP_HAS_GETRANGE:
546 src->has_getrange = g_value_get_boolean (value);
547 gst_base_src_set_dataflow_funcs (src);
549 case PROP_NUM_BUFFERS:
550 src->num_buffers = g_value_get_int (value);
553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
564 src = GST_BASE_SRC (object);
568 g_value_set_ulong (value, src->blocksize);
571 g_value_set_boolean (value, src->has_loop);
573 case PROP_HAS_GETRANGE:
574 g_value_set_boolean (value, src->has_getrange);
576 case PROP_NUM_BUFFERS:
577 g_value_set_int (value, src->num_buffers);
580 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
586 gst_base_src_get_range (GstPad * pad, guint64 offset, guint length,
591 GstBaseSrcClass *bclass;
593 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
594 bclass = GST_BASE_SRC_GET_CLASS (src);
598 while (!src->live_running) {
599 GST_DEBUG ("live source signal waiting");
600 GST_LIVE_SIGNAL (src);
601 GST_DEBUG ("live source waiting for running state");
603 GST_DEBUG ("live source unlocked");
606 GST_LIVE_UNLOCK (src);
609 if (GST_PAD_IS_FLUSHING (pad))
613 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED))
620 if (src->size != -1) {
621 if (offset > src->size)
622 goto unexpected_length;
624 if (offset + length > src->size) {
625 if (bclass->get_size)
626 bclass->get_size (src, &src->size);
628 if (offset + length > src->size) {
629 length = src->size - offset;
634 goto unexpected_length;
636 if (src->num_buffers_left == 0) {
637 goto reached_num_buffers;
639 if (src->num_buffers_left > 0)
640 src->num_buffers_left--;
643 ret = bclass->create (src, offset, length, buf);
650 GST_DEBUG_OBJECT (src, "pad is flushing");
652 return GST_FLOW_WRONG_STATE;
656 GST_DEBUG_OBJECT (src, "getrange but not started");
657 return GST_FLOW_WRONG_STATE;
661 GST_DEBUG_OBJECT (src, "no create function");
662 return GST_FLOW_ERROR;
666 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
667 return GST_FLOW_UNEXPECTED;
671 GST_DEBUG_OBJECT (src, "sent all buffers");
672 return GST_FLOW_UNEXPECTED;
677 gst_base_src_check_get_range (GstPad * pad)
681 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
683 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
684 gst_base_src_start (src);
685 gst_base_src_stop (src);
688 return src->seekable;
692 gst_base_src_loop (GstPad * pad)
695 GstBuffer *buf = NULL;
698 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
700 if (src->need_discont) {
701 /* now send discont */
702 gst_base_src_send_discont (src);
703 src->need_discont = FALSE;
706 ret = gst_base_src_get_range (pad, src->offset, src->blocksize, &buf);
707 if (ret != GST_FLOW_OK)
713 src->offset += GST_BUFFER_SIZE (buf);
715 ret = gst_pad_push (pad, buf);
716 if (ret != GST_FLOW_OK)
723 GST_DEBUG_OBJECT (src, "going to EOS");
724 gst_pad_pause_task (pad);
725 gst_pad_push_event (pad, gst_event_new_eos ());
730 GST_DEBUG_OBJECT (src, "pausing task");
731 gst_pad_pause_task (pad);
732 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
733 /* for fatal errors we post an error message */
734 GST_ELEMENT_ERROR (src, STREAM, STOPPED,
735 ("streaming stopped, reason %s", gst_flow_get_name (ret)),
736 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
737 gst_pad_push_event (pad, gst_event_new_eos ());
743 GST_ELEMENT_ERROR (src, STREAM, STOPPED,
744 ("internal: element returned NULL buffer"),
745 ("internal: element returned NULL buffer"));
746 gst_pad_pause_task (pad);
747 gst_pad_push_event (pad, gst_event_new_eos ());
753 gst_base_src_unlock (GstBaseSrc * basesrc)
755 GstBaseSrcClass *bclass;
756 gboolean result = FALSE;
758 GST_DEBUG ("unlock");
759 /* unblock whatever the subclass is doing */
760 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
762 result = bclass->unlock (basesrc);
764 GST_DEBUG ("unschedule clock");
765 /* and unblock the clock as well, if any */
767 if (basesrc->clock_id) {
768 gst_clock_id_unschedule (basesrc->clock_id);
770 GST_UNLOCK (basesrc);
772 GST_DEBUG ("unlock done");
778 gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size)
780 GstBaseSrcClass *bclass;
781 gboolean result = FALSE;
783 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
784 if (bclass->get_size)
785 result = bclass->get_size (basesrc, size);
788 basesrc->size = *size;
794 gst_base_src_is_seekable (GstBaseSrc * basesrc)
796 GstBaseSrcClass *bclass;
798 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
800 /* check if we can seek */
801 if (bclass->is_seekable)
802 basesrc->seekable = bclass->is_seekable (basesrc);
804 basesrc->seekable = FALSE;
806 return basesrc->seekable;
810 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
813 GstCaps *caps = NULL;
814 GstCaps *peercaps = NULL;
815 gboolean result = FALSE;
817 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
818 GST_DEBUG ("caps of src: %" GST_PTR_FORMAT, thiscaps);
819 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
822 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
823 GST_DEBUG ("caps of peer: %" GST_PTR_FORMAT, peercaps);
827 icaps = gst_caps_intersect (thiscaps, peercaps);
828 GST_DEBUG ("intersect: %" GST_PTR_FORMAT, icaps);
829 gst_caps_unref (thiscaps);
830 gst_caps_unref (peercaps);
832 caps = gst_caps_copy_nth (icaps, 0);
833 gst_caps_unref (icaps);
839 caps = gst_caps_make_writable (caps);
840 gst_caps_truncate (caps);
842 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
843 GST_DEBUG ("fixated to: %" GST_PTR_FORMAT, caps);
845 if (gst_caps_is_any (caps)) {
846 gst_caps_unref (caps);
848 } else if (gst_caps_is_fixed (caps)) {
849 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
850 gst_caps_unref (caps);
858 GST_DEBUG ("no negotiation needed");
860 gst_caps_unref (thiscaps);
866 gst_base_src_negotiate (GstBaseSrc * basesrc)
868 GstBaseSrcClass *bclass;
869 gboolean result = TRUE;
871 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
873 if (bclass->negotiate)
874 result = bclass->negotiate (basesrc);
880 gst_base_src_start (GstBaseSrc * basesrc)
882 GstBaseSrcClass *bclass;
885 if (GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
888 GST_DEBUG_OBJECT (basesrc, "starting source");
890 basesrc->num_buffers_left = basesrc->num_buffers;
892 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
894 result = bclass->start (basesrc);
899 goto could_not_start;
901 GST_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
903 /* start in the beginning */
906 /* figure out the size */
907 if (bclass->get_size) {
908 result = bclass->get_size (basesrc, &basesrc->size);
916 GST_DEBUG ("size %d %lld", result, basesrc->size);
918 /* we always run to the end */
919 basesrc->segment_start = 0;
920 basesrc->segment_end = basesrc->size;
921 basesrc->need_discont = TRUE;
923 /* check if we can seek, updates ->seekable */
924 gst_base_src_is_seekable (basesrc);
928 if (basesrc->seekable) {
931 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
932 gst_pad_set_caps (basesrc->srcpad, caps);
933 gst_caps_unref (caps);
937 if (!gst_base_src_negotiate (basesrc))
938 goto could_not_negotiate;
945 GST_DEBUG_OBJECT (basesrc, "could not start");
950 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
951 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
952 ("Could not connect source to pipeline"),
953 ("Check your filtered caps, if any"));
954 gst_base_src_stop (basesrc);
960 gst_base_src_stop (GstBaseSrc * basesrc)
962 GstBaseSrcClass *bclass;
963 gboolean result = TRUE;
965 if (!GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
968 GST_DEBUG_OBJECT (basesrc, "stopping source");
970 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
972 result = bclass->stop (basesrc);
975 GST_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
981 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
985 GST_LIVE_LOCK (basesrc);
986 basesrc->live_running = TRUE;
987 GST_LIVE_SIGNAL (basesrc);
988 GST_LIVE_UNLOCK (basesrc);
990 /* step 1, unblock clock sync (if any) */
991 gst_base_src_unlock (basesrc);
993 /* step 2, make sure streaming finishes */
994 result = gst_pad_stop_task (pad);
1000 gst_base_src_activate_push (GstPad * pad, gboolean active)
1002 GstBaseSrc *basesrc;
1004 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1006 /* prepare subclass first */
1008 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1009 if (!gst_base_src_start (basesrc))
1012 return gst_pad_start_task (pad, (GstTaskFunction) gst_base_src_loop, pad);
1014 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1015 return gst_base_src_deactivate (basesrc, pad);
1020 gst_base_src_stop (basesrc);
1021 GST_DEBUG_OBJECT (basesrc, "Failed to start in push mode");
1027 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1029 GstBaseSrc *basesrc;
1031 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1033 /* prepare subclass first */
1035 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1036 if (!gst_base_src_start (basesrc))
1039 if (!basesrc->seekable) {
1040 gst_base_src_stop (basesrc);
1046 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
1048 if (!gst_base_src_stop (basesrc))
1051 return gst_base_src_deactivate (basesrc, pad);
1056 gst_base_src_stop (basesrc);
1057 GST_DEBUG_OBJECT (basesrc, "Failed to start in pull mode");
1062 GST_DEBUG_OBJECT (basesrc, "Failed to stop in pull mode");
1067 static GstElementStateReturn
1068 gst_base_src_change_state (GstElement * element)
1070 GstBaseSrc *basesrc;
1071 GstElementStateReturn result = GST_STATE_SUCCESS;
1072 GstElementStateReturn presult;
1073 GstElementState transition;
1075 basesrc = GST_BASE_SRC (element);
1077 transition = GST_STATE_TRANSITION (element);
1079 switch (transition) {
1080 case GST_STATE_NULL_TO_READY:
1082 case GST_STATE_READY_TO_PAUSED:
1083 GST_LIVE_LOCK (element);
1084 if (basesrc->is_live) {
1085 result = GST_STATE_NO_PREROLL;
1086 basesrc->live_running = FALSE;
1088 GST_LIVE_UNLOCK (element);
1090 case GST_STATE_PAUSED_TO_PLAYING:
1091 GST_LIVE_LOCK (element);
1092 if (basesrc->is_live) {
1093 basesrc->live_running = TRUE;
1094 GST_LIVE_SIGNAL (element);
1096 GST_LIVE_UNLOCK (element);
1102 if ((presult = GST_ELEMENT_CLASS (parent_class)->change_state (element)) !=
1103 GST_STATE_SUCCESS) {
1104 gst_base_src_stop (basesrc);
1108 switch (transition) {
1109 case GST_STATE_PLAYING_TO_PAUSED:
1110 GST_LIVE_LOCK (element);
1111 if (basesrc->is_live) {
1112 result = GST_STATE_NO_PREROLL;
1113 basesrc->live_running = FALSE;
1115 GST_LIVE_UNLOCK (element);
1117 case GST_STATE_PAUSED_TO_READY:
1118 if (!gst_base_src_stop (basesrc))
1119 result = GST_STATE_FAILURE;
1121 case GST_STATE_READY_TO_NULL: