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: #GstBaseTransform, #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 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>
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 */
74 static GstElementClass *parent_class = NULL;
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);
83 gst_base_src_get_type (void)
85 static GType base_src_type = 0;
88 static const GTypeInfo base_src_info = {
89 sizeof (GstBaseSrcClass),
90 (GBaseInitFunc) gst_base_src_base_init,
92 (GClassInitFunc) gst_base_src_class_init,
97 (GInstanceInitFunc) gst_base_src_init,
100 base_src_type = g_type_register_static (GST_TYPE_ELEMENT,
101 "GstBaseSrc", &base_src_info, G_TYPE_FLAG_ABSTRACT);
103 return base_src_type;
105 static GstCaps *gst_base_src_getcaps (GstPad * pad);
106 static gboolean gst_base_src_setcaps (GstPad * pad, GstCaps * caps);
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);
116 static gboolean gst_base_src_query (GstPad * pad, GstQuery * query);
119 static const GstEventMask *gst_base_src_get_event_mask (GstPad * pad);
121 static gboolean gst_base_src_default_negotiate (GstBaseSrc * basesrc);
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);
128 static GstStateChangeReturn gst_base_src_change_state (GstElement * element,
129 GstStateChange transition);
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);
137 gst_base_src_base_init (gpointer g_class)
139 GST_DEBUG_CATEGORY_INIT (gst_base_src_debug, "basesrc", 0, "basesrc element");
143 gst_base_src_class_init (GstBaseSrcClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
151 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
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);
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,
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));
167 gstelement_class->change_state =
168 GST_DEBUG_FUNCPTR (gst_base_src_change_state);
170 klass->negotiate = gst_base_src_default_negotiate;
174 gst_base_src_init (GstBaseSrc * basesrc, gpointer g_class)
177 GstPadTemplate *pad_template;
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;
185 basesrc->can_activate_push = TRUE;
186 basesrc->pad_mode = GST_ACTIVATE_NONE;
189 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
190 g_return_if_fail (pad_template != NULL);
192 GST_DEBUG_OBJECT (basesrc, "creating src pad");
193 pad = gst_pad_new_from_template (pad_template, "src");
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));
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);
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);
223 GST_DEBUG_OBJECT (basesrc, "init done");
227 gst_base_src_finalize (GObject * object)
231 basesrc = GST_BASE_SRC (object);
233 g_mutex_free (basesrc->live_lock);
234 g_cond_free (basesrc->live_cond);
236 G_OBJECT_CLASS (parent_class)->finalize (object);
240 * gst_base_src_set_live:
241 * @src: base source instance
242 * @live: new live-mode
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.
248 gst_base_src_set_live (GstBaseSrc * src, gboolean live)
252 GST_LIVE_UNLOCK (src);
256 * gst_base_src_is_live:
257 * @src: base source instance
259 * Check if an element is in live mode.
261 * Returns: %TRUE if element is in live mode.
264 gst_base_src_is_live (GstBaseSrc * src)
269 result = src->is_live;
270 GST_LIVE_UNLOCK (src);
276 gst_base_src_setcaps (GstPad * pad, GstCaps * caps)
278 GstBaseSrcClass *bclass;
282 bsrc = GST_BASE_SRC (GST_PAD_PARENT (pad));
283 bclass = GST_BASE_SRC_GET_CLASS (bsrc);
285 if (bclass->set_caps)
286 res = bclass->set_caps (bsrc, caps);
292 gst_base_src_getcaps (GstPad * pad)
294 GstBaseSrcClass *bclass;
296 GstCaps *caps = NULL;
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);
304 GstPadTemplate *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));
316 gst_base_src_query (GstPad * pad, GstQuery * query)
323 src = GST_BASE_SRC (GST_PAD_PARENT (pad));
325 switch (GST_QUERY_TYPE (query)) {
326 case GST_QUERY_POSITION:
330 gst_query_parse_position (query, &format, NULL, NULL);
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);
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);
351 case GST_QUERY_SEEKING:
352 gst_query_set_seeking (query, GST_FORMAT_BYTES,
353 src->seekable, src->segment_start, src->segment_end);
356 case GST_QUERY_FORMATS:
357 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
358 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
361 case GST_QUERY_LATENCY:
362 case GST_QUERY_JITTER:
364 case GST_QUERY_CONVERT:
366 return gst_pad_query_default (pad, query);
371 gst_base_src_send_discont (GstBaseSrc * src)
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,
380 (gint64) src->segment_start, (gint64) src->segment_end, (gint64) 0);
382 return gst_pad_push_event (src->srcpad, event);
386 gst_base_src_do_seek (GstBaseSrc * src, GstEvent * event)
391 GstSeekType cur_type, stop_type;
394 gst_event_parse_seek (event, &rate, &format, &flags,
395 &cur_type, &cur, &stop_type, &stop);
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)
404 /* get seek positions */
405 src->segment_loop = flags & GST_SEEK_FLAG_SEGMENT;
407 /* send flush start */
408 gst_pad_push_event (src->srcpad, gst_event_new_flush_start ());
410 /* unblock streaming thread */
411 gst_base_src_unlock (src);
413 /* grab streaming lock */
414 GST_STREAM_LOCK (src->srcpad);
416 /* send flush stop */
417 gst_pad_push_event (src->srcpad, gst_event_new_flush_stop ());
419 /* perform the seek */
421 case GST_SEEK_TYPE_NONE:
423 case GST_SEEK_TYPE_SET:
426 src->offset = MIN (cur, src->size);
427 src->segment_start = src->offset;
429 case GST_SEEK_TYPE_CUR:
430 src->offset = CLAMP (src->offset + cur, 0, src->size);
431 src->segment_start = src->offset;
433 case GST_SEEK_TYPE_END:
436 src->offset = MAX (0, src->size + cur);
437 src->segment_start = src->offset;
444 case GST_SEEK_TYPE_NONE:
446 case GST_SEEK_TYPE_SET:
449 src->segment_end = MIN (stop, src->size);
451 case GST_SEEK_TYPE_CUR:
452 src->segment_end = CLAMP (src->segment_end + stop, 0, src->size);
454 case GST_SEEK_TYPE_END:
457 src->segment_end = src->size + stop;
463 GST_DEBUG_OBJECT (src, "seek pending for segment from %" G_GINT64_FORMAT
464 " to %" G_GINT64_FORMAT, src->segment_start, src->segment_end);
466 /* now make sure the discont will be send */
467 src->need_discont = TRUE;
469 /* and restart the task */
470 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_base_src_loop,
472 GST_STREAM_UNLOCK (src->srcpad);
474 gst_event_unref (event);
481 GST_DEBUG_OBJECT (src, "seek error");
482 GST_STREAM_UNLOCK (src->srcpad);
483 gst_event_unref (event);
489 gst_base_src_event_handler (GstPad * pad, GstEvent * event)
492 GstBaseSrcClass *bclass;
495 src = GST_BASE_SRC (GST_PAD_PARENT (pad));
496 bclass = GST_BASE_SRC_GET_CLASS (src);
499 result = bclass->event (src, event);
501 switch (GST_EVENT_TYPE (event)) {
503 if (!src->seekable) {
504 gst_event_unref (event);
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);
512 case GST_EVENT_FLUSH_STOP:
517 gst_event_unref (event);
523 gst_base_src_set_property (GObject * object, guint prop_id,
524 const GValue * value, GParamSpec * pspec)
528 src = GST_BASE_SRC (object);
532 src->blocksize = g_value_get_ulong (value);
534 case PROP_NUM_BUFFERS:
535 src->num_buffers = g_value_get_int (value);
538 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 gst_base_src_get_property (GObject * object, guint prop_id, GValue * value,
549 src = GST_BASE_SRC (object);
553 g_value_set_ulong (value, src->blocksize);
555 case PROP_NUM_BUFFERS:
556 g_value_set_int (value, src->num_buffers);
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565 gst_base_src_get_range (GstPad * pad, guint64 offset, guint length,
570 GstBaseSrcClass *bclass;
572 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
573 bclass = GST_BASE_SRC_GET_CLASS (src);
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");
582 GST_DEBUG ("live source unlocked");
585 GST_LIVE_UNLOCK (src);
588 if (GST_PAD_IS_FLUSHING (pad))
592 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED))
598 GST_DEBUG_OBJECT (src,
599 "reading offset %" G_GUINT64_FORMAT ", length %u, size %"
600 G_GUINT64_FORMAT, offset, length, src->size);
603 if (src->size != -1) {
604 if (offset > src->size)
605 goto unexpected_length;
607 if (offset + length > src->size) {
608 if (bclass->get_size)
609 bclass->get_size (src, &src->size);
611 if (offset + length > src->size) {
612 length = src->size - offset;
617 goto unexpected_length;
619 if (src->num_buffers_left == 0) {
620 goto reached_num_buffers;
622 if (src->num_buffers_left > 0)
623 src->num_buffers_left--;
626 ret = bclass->create (src, offset, length, buf);
633 GST_DEBUG_OBJECT (src, "pad is flushing");
635 return GST_FLOW_WRONG_STATE;
639 GST_DEBUG_OBJECT (src, "getrange but not started");
640 return GST_FLOW_WRONG_STATE;
644 GST_DEBUG_OBJECT (src, "no create function");
645 return GST_FLOW_ERROR;
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;
655 GST_DEBUG_OBJECT (src, "sent all buffers");
656 return GST_FLOW_UNEXPECTED;
661 gst_base_src_check_get_range (GstPad * pad)
665 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
667 if (!GST_FLAG_IS_SET (src, GST_BASE_SRC_STARTED)) {
668 gst_base_src_start (src);
669 gst_base_src_stop (src);
672 return src->seekable;
676 gst_base_src_loop (GstPad * pad)
679 GstBuffer *buf = NULL;
682 src = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
684 if (src->need_discont) {
685 /* now send discont */
686 gst_base_src_send_discont (src);
687 src->need_discont = FALSE;
690 ret = gst_base_src_get_range (pad, src->offset, src->blocksize, &buf);
691 if (ret != GST_FLOW_OK)
697 src->offset += GST_BUFFER_SIZE (buf);
699 ret = gst_pad_push (pad, buf);
700 if (ret != GST_FLOW_OK)
707 GST_DEBUG_OBJECT (src, "going to EOS");
708 gst_pad_pause_task (pad);
709 gst_pad_push_event (pad, gst_event_new_eos ());
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 ());
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 ());
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. */
740 gst_base_src_unlock (GstBaseSrc * basesrc)
742 GstBaseSrcClass *bclass;
743 gboolean result = FALSE;
745 GST_DEBUG ("unlock");
746 /* unblock whatever the subclass is doing */
747 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
749 result = bclass->unlock (basesrc);
751 GST_DEBUG ("unschedule clock");
752 /* and unblock the clock as well, if any */
754 if (basesrc->clock_id) {
755 gst_clock_id_unschedule (basesrc->clock_id);
757 GST_UNLOCK (basesrc);
759 GST_DEBUG ("unlock done");
765 gst_base_src_get_size (GstBaseSrc * basesrc, guint64 * size)
767 GstBaseSrcClass *bclass;
768 gboolean result = FALSE;
770 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
771 if (bclass->get_size)
772 result = bclass->get_size (basesrc, size);
775 basesrc->size = *size;
781 gst_base_src_is_seekable (GstBaseSrc * basesrc)
783 GstBaseSrcClass *bclass;
785 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
787 /* check if we can seek */
788 if (bclass->is_seekable)
789 basesrc->seekable = bclass->is_seekable (basesrc);
791 basesrc->seekable = FALSE;
793 GST_DEBUG_OBJECT (basesrc, "is seekable: %d", basesrc->seekable);
795 return basesrc->seekable;
799 gst_base_src_default_negotiate (GstBaseSrc * basesrc)
802 GstCaps *caps = NULL;
803 GstCaps *peercaps = NULL;
804 gboolean result = FALSE;
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))
811 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc));
812 GST_DEBUG ("caps of peer: %" GST_PTR_FORMAT, peercaps);
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);
821 caps = gst_caps_copy_nth (icaps, 0);
822 gst_caps_unref (icaps);
828 caps = gst_caps_make_writable (caps);
829 gst_caps_truncate (caps);
831 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
832 GST_DEBUG ("fixated to: %" GST_PTR_FORMAT, caps);
834 if (gst_caps_is_any (caps)) {
835 gst_caps_unref (caps);
837 } else if (gst_caps_is_fixed (caps)) {
838 gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
839 gst_caps_unref (caps);
847 GST_DEBUG ("no negotiation needed");
849 gst_caps_unref (thiscaps);
855 gst_base_src_negotiate (GstBaseSrc * basesrc)
857 GstBaseSrcClass *bclass;
858 gboolean result = TRUE;
860 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
862 if (bclass->negotiate)
863 result = bclass->negotiate (basesrc);
869 gst_base_src_start (GstBaseSrc * basesrc)
871 GstBaseSrcClass *bclass;
874 if (GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
877 GST_DEBUG_OBJECT (basesrc, "starting source");
879 basesrc->num_buffers_left = basesrc->num_buffers;
881 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
883 result = bclass->start (basesrc);
888 goto could_not_start;
890 GST_FLAG_SET (basesrc, GST_BASE_SRC_STARTED);
892 /* start in the beginning */
895 /* figure out the size */
896 if (bclass->get_size) {
897 result = bclass->get_size (basesrc, &basesrc->size);
905 GST_DEBUG ("size %d %lld", result, basesrc->size);
907 /* we always run to the end */
908 basesrc->segment_start = 0;
909 basesrc->segment_end = basesrc->size;
910 basesrc->need_discont = TRUE;
912 /* check if we can seek, updates ->seekable */
913 gst_base_src_is_seekable (basesrc);
917 if (basesrc->seekable) {
920 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
921 gst_pad_set_caps (basesrc->srcpad, caps);
922 gst_caps_unref (caps);
926 if (!gst_base_src_negotiate (basesrc))
927 goto could_not_negotiate;
934 GST_DEBUG_OBJECT (basesrc, "could not start");
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);
949 gst_base_src_stop (GstBaseSrc * basesrc)
951 GstBaseSrcClass *bclass;
952 gboolean result = TRUE;
954 if (!GST_FLAG_IS_SET (basesrc, GST_BASE_SRC_STARTED))
957 GST_DEBUG_OBJECT (basesrc, "stopping source");
959 bclass = GST_BASE_SRC_GET_CLASS (basesrc);
961 result = bclass->stop (basesrc);
964 GST_FLAG_UNSET (basesrc, GST_BASE_SRC_STARTED);
970 gst_base_src_deactivate (GstBaseSrc * basesrc, GstPad * pad)
974 GST_LIVE_LOCK (basesrc);
975 basesrc->live_running = TRUE;
976 GST_LIVE_SIGNAL (basesrc);
977 GST_LIVE_UNLOCK (basesrc);
979 /* step 1, unblock clock sync (if any) */
980 gst_base_src_unlock (basesrc);
982 /* step 2, make sure streaming finishes */
983 result = gst_pad_stop_task (pad);
989 gst_base_src_activate_push (GstPad * pad, gboolean active)
993 basesrc = GST_BASE_SRC (GST_OBJECT_PARENT (pad));
995 /* prepare subclass first */
997 GST_DEBUG_OBJECT (basesrc, "Activating in push mode");
999 if (!basesrc->can_activate_push)
1000 goto no_push_activation;
1002 if (!gst_base_src_start (basesrc))
1005 return gst_pad_start_task (pad, (GstTaskFunction) gst_base_src_loop, pad);
1007 GST_DEBUG_OBJECT (basesrc, "Deactivating in push mode");
1008 return gst_base_src_deactivate (basesrc, pad);
1013 GST_DEBUG_OBJECT (basesrc, "Subclass disabled push-mode activation");
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 GstStateChangeReturn
1066 gst_base_src_change_state (GstElement * element, GstStateChange transition)
1068 GstBaseSrc *basesrc;
1069 GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
1070 GstStateChangeReturn presult;
1072 basesrc = GST_BASE_SRC (element);
1075 switch (transition) {
1076 case GST_STATE_CHANGE_NULL_TO_READY:
1078 case GST_STATE_CHANGE_READY_TO_PAUSED:
1079 GST_LIVE_LOCK (element);
1080 if (basesrc->is_live) {
1081 result = GST_STATE_CHANGE_NO_PREROLL;
1082 basesrc->live_running = FALSE;
1084 GST_LIVE_UNLOCK (element);
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);
1092 GST_LIVE_UNLOCK (element);
1099 GST_ELEMENT_CLASS (parent_class)->change_state (element,
1100 transition)) == GST_STATE_CHANGE_FAILURE)
1103 switch (transition) {
1104 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1105 GST_LIVE_LOCK (element);
1106 if (basesrc->is_live) {
1107 result = GST_STATE_CHANGE_NO_PREROLL;
1108 basesrc->live_running = FALSE;
1110 GST_LIVE_UNLOCK (element);
1112 case GST_STATE_CHANGE_PAUSED_TO_READY:
1113 if (!gst_base_src_stop (basesrc))
1114 result = GST_STATE_CHANGE_FAILURE;
1116 case GST_STATE_CHANGE_READY_TO_NULL:
1127 gst_base_src_stop (basesrc);