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 gst_base_src_send_discont (GstBaseSrc * src)
384 GST_DEBUG_OBJECT (src, "Sending newsegment from %" G_GINT64_FORMAT
385 " to %" G_GINT64_FORMAT, (gint64) src->segment_start,
386 (gint64) src->segment_end);
387 event = gst_event_new_newsegment (1.0,
389 (gint64) src->segment_start, (gint64) src->segment_end, (gint64) 0);
391 return gst_pad_push_event (src->srcpad, event);
395 gst_base_src_do_seek (GstBaseSrc * src, GstEvent * event)
400 GstSeekType cur_type, stop_type;
403 gst_event_parse_seek (event, &rate, &format, &flags,
404 &cur_type, &cur, &stop_type, &stop);
406 /* get seek format */
407 if (format == GST_FORMAT_DEFAULT)
408 format = GST_FORMAT_BYTES;
409 /* we can only seek bytes */
410 if (format != GST_FORMAT_BYTES)
413 /* get seek positions */
414 src->segment_loop = flags & GST_SEEK_FLAG_SEGMENT;
416 /* send flush start */
417 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
419 /* unblock streaming thread */
420 gst_base_src_unlock (src);
422 /* grab streaming lock */
423 GST_STREAM_LOCK (src->srcpad);
425 /* send flush stop */
426 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
428 /* perform the seek */
430 case GST_SEEK_TYPE_NONE:
432 case GST_SEEK_TYPE_SET:
435 src->offset = MIN (cur, src->size);
436 src->segment_start = src->offset;
438 case GST_SEEK_TYPE_CUR:
439 src->offset = CLAMP (src->offset + cur, 0, src->size);
440 src->segment_start = src->offset;
442 case GST_SEEK_TYPE_END:
445 src->offset = MAX (0, src->size + cur);
446 src->segment_start = src->offset;
453 case GST_SEEK_TYPE_NONE:
455 case GST_SEEK_TYPE_SET:
458 src->segment_end = MIN (stop, src->size);
460 case GST_SEEK_TYPE_CUR:
461 src->segment_end = CLAMP (src->segment_end + stop, 0, src->size);
463 case GST_SEEK_TYPE_END:
466 src->segment_end = src->size + stop;
472 GST_DEBUG_OBJECT (src, "seek pending for segment from %" G_GINT64_FORMAT
473 " to %" G_GINT64_FORMAT, src->segment_start, src->segment_end);
475 /* now make sure the discont will be send */
476 src->need_discont = TRUE;
478 /* and restart the task */
479 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
481 GST_STREAM_UNLOCK (src->srcpad);
483 gst_event_unref (event);
490 GST_DEBUG_OBJECT (src, "seek error");
491 GST_STREAM_UNLOCK (src->srcpad);
492 gst_event_unref (event);
498 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
501 GstBaseSrcClass *bclass;
504 src = GST_BASE_SRC (GST_PAD_PARENT (pad));
505 bclass = GST_BASE_SRC_GET_CLASS (src);
508 result = bclass->event (src, event);
510 switch (GST_EVENT_TYPE (event)) {
512 return gst_base_src_do_seek (src, event);
513 case GST_EVENT_FLUSH_START:
514 /* cancel any blocking getrange */
515 gst_base_src_unlock (src);
517 case GST_EVENT_FLUSH_STOP:
522 gst_event_unref (event);
528 gst_base_src_set_property (GObject * object, guint prop_id,
529 const GValue * value, GParamSpec * pspec)
533 src = GST_BASE_SRC (object);
537 src->blocksize = g_value_get_ulong (value);
540 src->has_loop = g_value_get_boolean (value);
541 gst_base_src_set_dataflow_funcs (src);
543 case PROP_HAS_GETRANGE:
544 src->has_getrange = g_value_get_boolean (value);
545 gst_base_src_set_dataflow_funcs (src);
547 case PROP_NUM_BUFFERS:
548 src->num_buffers = g_value_get_int (value);
551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
557 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
562 src = GST_BASE_SRC (object);
566 g_value_set_ulong (value, src->blocksize);
569 g_value_set_boolean (value, src->has_loop);
571 case PROP_HAS_GETRANGE:
572 g_value_set_boolean (value, src->has_getrange);
574 case PROP_NUM_BUFFERS:
575 g_value_set_int (value, src->num_buffers);
578 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
584 gst_base_src_get_range (GstPad * pad, guint64 offset, guint length,
589 GstBaseSrcClass *bclass;
591 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
592 bclass = GST_BASE_SRC_GET_CLASS (src);
596 while (!src->live_running) {
597 GST_DEBUG ("live source signal waiting");
598 GST_LIVE_SIGNAL (src);
599 GST_DEBUG ("live source waiting for running state");
601 GST_DEBUG ("live source unlocked");
604 GST_LIVE_UNLOCK (src);
607 if (GST_PAD_IS_FLUSHING (pad))
611 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED))
618 if (src->size != -1) {
619 if (offset > src->size)
620 goto unexpected_length;
622 if (offset + length > src->size) {
623 if (bclass->get_size)
624 bclass->get_size (src, &src->size);
626 if (offset + length > src->size) {
627 length = src->size - offset;
632 goto unexpected_length;
634 if (src->num_buffers_left == 0) {
635 goto reached_num_buffers;
637 if (src->num_buffers_left > 0)
638 src->num_buffers_left--;
641 ret = bclass->create (src, offset, length, buf);
648 GST_DEBUG_OBJECT (src, "pad is flushing");
650 return GST_FLOW_WRONG_STATE;
654 GST_DEBUG_OBJECT (src, "getrange but not started");
655 return GST_FLOW_WRONG_STATE;
659 GST_DEBUG_OBJECT (src, "no create function");
660 return GST_FLOW_ERROR;
664 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
665 return GST_FLOW_UNEXPECTED;
669 GST_DEBUG_OBJECT (src, "sent all buffers");
670 return GST_FLOW_UNEXPECTED;
675 gst_base_src_check_get_range (GstPad * pad)
679 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
681 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
682 gst_base_src_start (src);
683 gst_base_src_stop (src);
686 return src->seekable;
690 gst_base_src_loop (GstPad * pad)
693 GstBuffer *buf = NULL;
696 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
698 if (src->need_discont) {
699 /* now send discont */
700 gst_base_src_send_discont (src);
701 src->need_discont = FALSE;
704 ret = gst_base_src_get_range (pad, src->offset, src->blocksize, &buf);
705 if (ret != GST_FLOW_OK)
711 src->offset += GST_BUFFER_SIZE (buf);
713 ret = gst_pad_push (pad, buf);
714 if (ret != GST_FLOW_OK)
721 GST_DEBUG_OBJECT (src, "going to EOS");
722 gst_pad_pause_task (pad);
723 gst_pad_push_event (pad, gst_event_new_eos ());
728 GST_DEBUG_OBJECT (src, "pausing task");
729 gst_pad_pause_task (pad);
730 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
731 /* for fatal errors we post an error message */
732 GST_ELEMENT_ERROR (src, STREAM, STOPPED,
733 ("streaming stopped, reason %s", gst_flow_get_name (ret)),
734 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
735 gst_pad_push_event (pad, gst_event_new_eos ());
741 GST_ELEMENT_ERROR (src, STREAM, STOPPED,
742 ("internal: element returned NULL buffer"),
743 ("internal: element returned NULL buffer"));
744 gst_pad_pause_task (pad);
745 gst_pad_push_event (pad, gst_event_new_eos ());
751 gst_base_src_unlock (GstBaseSrc * basesrc)
753 GstBaseSrcClass *bclass;
754 gboolean result = FALSE;
756 GST_DEBUG ("unlock");
757 /* unblock whatever the subclass is doing */
758 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
760 result = bclass->unlock (basesrc);
762 GST_DEBUG ("unschedule clock");
763 /* and unblock the clock as well, if any */
765 if (basesrc->clock_id) {
766 gst_clock_id_unschedule (basesrc->clock_id);
768 GST_UNLOCK (basesrc);
770 GST_DEBUG ("unlock done");
776 gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size)
778 GstBaseSrcClass *bclass;
779 gboolean result = FALSE;
781 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
782 if (bclass->get_size)
783 result = bclass->get_size (basesrc, size);
786 basesrc->size = *size;
792 gst_base_src_is_seekable (GstBaseSrc * basesrc)
794 GstBaseSrcClass *bclass;
796 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
798 /* check if we can seek */
799 if (bclass->is_seekable)
800 basesrc->seekable = bclass->is_seekable (basesrc);
802 basesrc->seekable = FALSE;
804 return basesrc->seekable;
808 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
811 GstCaps *caps = NULL;
812 GstCaps *peercaps = NULL;
813 gboolean result = FALSE;
815 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc));
816 GST_DEBUG ("caps of src: %" GST_PTR_FORMAT, thiscaps);
817 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
820 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
821 GST_DEBUG ("caps of peer: %" GST_PTR_FORMAT, peercaps);
825 icaps = gst_caps_intersect (thiscaps, peercaps);
826 GST_DEBUG ("intersect: %" GST_PTR_FORMAT, icaps);
827 gst_caps_unref (thiscaps);
828 gst_caps_unref (peercaps);
830 caps = gst_caps_copy_nth (icaps, 0);
831 gst_caps_unref (icaps);
837 caps = gst_caps_make_writable (caps);
838 gst_caps_truncate (caps);
840 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
841 GST_DEBUG ("fixated to: %" GST_PTR_FORMAT, caps);
843 if (gst_caps_is_any (caps)) {
844 gst_caps_unref (caps);
846 } else if (gst_caps_is_fixed (caps)) {
847 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
848 gst_caps_unref (caps);
856 GST_DEBUG ("no negotiation needed");
858 gst_caps_unref (thiscaps);
864 gst_base_src_negotiate (GstBaseSrc * basesrc)
866 GstBaseSrcClass *bclass;
867 gboolean result = TRUE;
869 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
871 if (bclass->negotiate)
872 result = bclass->negotiate (basesrc);
878 gst_base_src_start (GstBaseSrc * basesrc)
880 GstBaseSrcClass *bclass;
883 if (GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
886 GST_DEBUG_OBJECT (basesrc, "starting source");
888 basesrc->num_buffers_left = basesrc->num_buffers;
890 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
892 result = bclass->start (basesrc);
897 goto could_not_start;
899 GST_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
901 /* start in the beginning */
904 /* figure out the size */
905 if (bclass->get_size) {
906 result = bclass->get_size (basesrc, &basesrc->size);
914 GST_DEBUG ("size %d %lld", result, basesrc->size);
916 /* we always run to the end */
917 basesrc->segment_start = 0;
918 basesrc->segment_end = basesrc->size;
919 basesrc->need_discont = TRUE;
921 /* check if we can seek, updates ->seekable */
922 gst_base_src_is_seekable (basesrc);
926 if (basesrc->seekable) {
929 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
930 gst_pad_set_caps (basesrc->srcpad, caps);
931 gst_caps_unref (caps);
935 if (!gst_base_src_negotiate (basesrc))
936 goto could_not_negotiate;
943 GST_DEBUG_OBJECT (basesrc, "could not start");
948 GST_DEBUG_OBJECT (basesrc, "could not negotiate, stopping");
949 GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
950 ("Could not connect source to pipeline"),
951 ("Check your filtered caps, if any"));
952 gst_base_src_stop (basesrc);
958 gst_base_src_stop (GstBaseSrc * basesrc)
960 GstBaseSrcClass *bclass;
961 gboolean result = TRUE;
963 if (!GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
966 GST_DEBUG_OBJECT (basesrc, "stopping source");
968 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
970 result = bclass->stop (basesrc);
973 GST_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
979 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
983 GST_LIVE_LOCK (basesrc);
984 basesrc->live_running = TRUE;
985 GST_LIVE_SIGNAL (basesrc);
986 GST_LIVE_UNLOCK (basesrc);
988 /* step 1, unblock clock sync (if any) */
989 gst_base_src_unlock (basesrc);
991 /* step 2, make sure streaming finishes */
992 result = gst_pad_stop_task (pad);
998 gst_base_src_activate_push (GstPad * pad, gboolean active)
1000 GstBaseSrc *basesrc;
1002 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1004 /* prepare subclass first */
1006 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
1007 if (!gst_base_src_start (basesrc))
1010 return gst_pad_start_task (pad, (GstTaskFunction) gst_base_src_loop, pad);
1012 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1013 return gst_base_src_deactivate (basesrc, pad);
1018 gst_base_src_stop (basesrc);
1019 GST_DEBUG_OBJECT (basesrc, "Failed to start in push mode");
1025 gst_base_src_activate_pull (GstPad * pad, gboolean active)
1027 GstBaseSrc *basesrc;
1029 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
1031 /* prepare subclass first */
1033 GST_DEBUG_OBJECT (basesrc, "Activating in pull mode");
1034 if (!gst_base_src_start (basesrc))
1037 if (!basesrc->seekable) {
1038 gst_base_src_stop (basesrc);
1044 GST_DEBUG_OBJECT (basesrc, "Deactivating in pull mode");
1046 if (!gst_base_src_stop (basesrc))
1049 return gst_base_src_deactivate (basesrc, pad);
1054 gst_base_src_stop (basesrc);
1055 GST_DEBUG_OBJECT (basesrc, "Failed to start in pull mode");
1060 GST_DEBUG_OBJECT (basesrc, "Failed to stop in pull mode");
1065 static GstElementStateReturn
1066 gst_base_src_change_state (GstElement * element)
1068 GstBaseSrc *basesrc;
1069 GstElementStateReturn result = GST_STATE_SUCCESS;
1070 GstElementStateReturn presult;
1071 GstElementState transition;
1073 basesrc = GST_BASE_SRC (element);
1075 transition = GST_STATE_TRANSITION (element);
1077 switch (transition) {
1078 case GST_STATE_NULL_TO_READY:
1080 case GST_STATE_READY_TO_PAUSED:
1081 GST_LIVE_LOCK (element);
1082 if (basesrc->is_live) {
1083 result = GST_STATE_NO_PREROLL;
1084 basesrc->live_running = FALSE;
1086 GST_LIVE_UNLOCK (element);
1088 case GST_STATE_PAUSED_TO_PLAYING:
1089 GST_LIVE_LOCK (element);
1090 if (basesrc->is_live) {
1091 basesrc->live_running = TRUE;
1092 GST_LIVE_SIGNAL (element);
1094 GST_LIVE_UNLOCK (element);
1100 if ((presult = GST_ELEMENT_CLASS (parent_class)->change_state (element)) !=
1101 GST_STATE_SUCCESS) {
1102 gst_base_src_stop (basesrc);
1106 switch (transition) {
1107 case GST_STATE_PLAYING_TO_PAUSED:
1108 GST_LIVE_LOCK (element);
1109 if (basesrc->is_live) {
1110 result = GST_STATE_NO_PREROLL;
1111 basesrc->live_running = FALSE;
1113 GST_LIVE_UNLOCK (element);
1115 case GST_STATE_PAUSED_TO_READY:
1116 if (!gst_base_src_stop (basesrc))
1117 result = GST_STATE_FAILURE;
1119 case GST_STATE_READY_TO_NULL: