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.
31 #include "gstbasesrc.h"
32 #include "gsttypefindhelper.h"
33 #include <gst/gstmarshal.h>
35 #define DEFAULT_BLOCKSIZE 4096
37 GST_DEBUG_CATEGORY_STATIC (gst_basesrc_debug);
38 #define GST_CAT_DEFAULT gst_basesrc_debug
40 /* BaseSrc signals and args */
55 static GstElementClass *parent_class = NULL;
57 static void gst_basesrc_base_init (gpointer g_class);
58 static void gst_basesrc_class_init (GstBaseSrcClass * klass);
59 static void gst_basesrc_init (GstBaseSrc * src, gpointer g_class);
62 gst_basesrc_get_type (void)
64 static GType basesrc_type = 0;
67 static const GTypeInfo basesrc_info = {
68 sizeof (GstBaseSrcClass),
69 (GBaseInitFunc) gst_basesrc_base_init,
71 (GClassInitFunc) gst_basesrc_class_init,
76 (GInstanceInitFunc) gst_basesrc_init,
79 basesrc_type = g_type_register_static (GST_TYPE_ELEMENT,
80 "GstBaseSrc", &basesrc_info, G_TYPE_FLAG_ABSTRACT);
85 static gboolean gst_basesrc_activate (GstPad * pad, GstActivateMode mode);
86 static void gst_basesrc_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_basesrc_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
90 static gboolean gst_basesrc_event_handler (GstPad * pad, GstEvent * event);
92 static gboolean gst_basesrc_query (GstPad * pad, GstQuery * query);
95 static const GstEventMask *gst_basesrc_get_event_mask (GstPad * pad);
98 static gboolean gst_basesrc_unlock (GstBaseSrc * basesrc);
99 static gboolean gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size);
100 static gboolean gst_basesrc_start (GstBaseSrc * basesrc);
101 static gboolean gst_basesrc_stop (GstBaseSrc * basesrc);
103 static GstElementStateReturn gst_basesrc_change_state (GstElement * element);
105 static void gst_basesrc_set_dataflow_funcs (GstBaseSrc * this);
106 static void gst_basesrc_loop (GstPad * pad);
107 static gboolean gst_basesrc_check_get_range (GstPad * pad);
108 static GstFlowReturn gst_basesrc_get_range (GstPad * pad, guint64 offset,
109 guint length, GstBuffer ** buf);
112 gst_basesrc_base_init (gpointer g_class)
114 GST_DEBUG_CATEGORY_INIT (gst_basesrc_debug, "basesrc", 0, "basesrc element");
118 gst_basesrc_class_init (GstBaseSrcClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_basesrc_set_property);
129 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_basesrc_get_property);
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BLOCKSIZE,
132 g_param_spec_ulong ("blocksize", "Block size",
133 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
136 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_LOOP,
137 g_param_spec_boolean ("has-loop", "Has loop function",
138 "True if the element should expose a loop function", TRUE,
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
141 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HAS_GETRANGE,
142 g_param_spec_boolean ("has-getrange", "Has getrange function",
143 "True if the element should expose a getrange function", TRUE,
144 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
146 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_basesrc_change_state);
150 gst_basesrc_init (GstBaseSrc * basesrc, gpointer g_class)
153 GstPadTemplate *pad_template;
156 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
157 g_return_if_fail (pad_template != NULL);
159 pad = gst_pad_new_from_template (pad_template, "src");
161 gst_pad_set_activate_function (pad, gst_basesrc_activate);
162 gst_pad_set_event_function (pad, gst_basesrc_event_handler);
163 gst_pad_set_query_function (pad, gst_basesrc_query);
165 gst_pad_set_checkgetrange_function (pad, gst_basesrc_check_get_range);
167 /* hold ref to pad */
168 basesrc->srcpad = pad;
169 gst_element_add_pad (GST_ELEMENT (basesrc), pad);
171 basesrc->segment_start = -1;
172 basesrc->segment_end = -1;
173 basesrc->blocksize = DEFAULT_BLOCKSIZE;
174 basesrc->clock_id = NULL;
176 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
180 gst_basesrc_set_dataflow_funcs (GstBaseSrc * this)
182 GST_DEBUG ("updating dataflow functions");
185 gst_pad_set_loop_function (this->srcpad, gst_basesrc_loop);
187 gst_pad_set_loop_function (this->srcpad, NULL);
189 if (this->has_getrange)
190 gst_pad_set_getrange_function (this->srcpad, gst_basesrc_get_range);
192 gst_pad_set_getrange_function (this->srcpad, NULL);
196 gst_basesrc_query (GstPad * pad, GstQuery * query)
203 src = GST_BASESRC (GST_PAD_PARENT (pad));
205 switch (GST_QUERY_TYPE (query)) {
206 case GST_QUERY_POSITION:
210 gst_query_parse_position (query, &format, NULL, NULL);
212 case GST_FORMAT_DEFAULT:
213 case GST_FORMAT_BYTES:
214 b = gst_basesrc_get_size (src, &ui64);
215 /* better to make get_size take an int64 */
216 i64 = b ? (gint64) ui64 : -1;
217 gst_query_set_position (query, GST_FORMAT_BYTES, src->offset, i64);
219 case GST_FORMAT_PERCENT:
220 b = gst_basesrc_get_size (src, &ui64);
221 i64 = GST_FORMAT_PERCENT_MAX;
222 i64 *= b ? (src->offset / (gdouble) ui64) : 1.0;
223 gst_query_set_position (query, GST_FORMAT_PERCENT,
224 i64, GST_FORMAT_PERCENT_MAX);
231 case GST_QUERY_SEEKING:
232 gst_query_set_seeking (query, GST_FORMAT_BYTES,
233 src->seekable, src->segment_start, src->segment_end);
236 case GST_QUERY_FORMATS:
237 gst_query_set_formats (query, 3, GST_FORMAT_DEFAULT,
238 GST_FORMAT_BYTES, GST_FORMAT_PERCENT);
241 case GST_QUERY_LATENCY:
242 case GST_QUERY_JITTER:
244 case GST_QUERY_CONVERT:
246 return gst_pad_query_default (pad, query);
251 static const GstEventMask *
252 gst_basesrc_get_event_mask (GstPad * pad)
254 static const GstEventMask masks[] = {
255 {GST_EVENT_SEEK, GST_SEEK_METHOD_CUR | GST_SEEK_METHOD_SET |
256 GST_SEEK_METHOD_END | GST_SEEK_FLAG_FLUSH |
257 GST_SEEK_FLAG_SEGMENT_LOOP},
258 {GST_EVENT_FLUSH, 0},
267 gst_basesrc_do_seek (GstBaseSrc * src, GstEvent * event)
272 format = GST_EVENT_SEEK_FORMAT (event);
274 /* get seek format */
275 if (format == GST_FORMAT_DEFAULT)
276 format = GST_FORMAT_BYTES;
277 /* we can only seek bytes */
278 if (format != GST_FORMAT_BYTES)
281 /* get seek positions */
282 offset = GST_EVENT_SEEK_OFFSET (event);
283 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
285 switch (GST_EVENT_SEEK_METHOD (event)) {
286 case GST_SEEK_METHOD_SET:
289 src->offset = MIN (offset, src->size);
290 src->segment_start = src->offset;
291 src->segment_end = MIN (GST_EVENT_SEEK_ENDOFFSET (event), src->size);
292 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
295 case GST_SEEK_METHOD_CUR:
296 offset += src->offset;
297 src->offset = CLAMP (offset, 0, src->size);
298 src->segment_start = src->offset;
299 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
300 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
303 case GST_SEEK_METHOD_END:
306 offset = src->size + offset;
307 src->offset = MAX (0, offset);
308 src->segment_start = src->offset;
309 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
310 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
317 /* send flush start */
318 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
320 /* unblock streaming thread */
321 gst_basesrc_unlock (src);
323 /* grab streaming lock */
324 GST_STREAM_LOCK (src->srcpad);
327 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
329 /* now send discont */
333 event = gst_event_new_discontinuous (1.0,
335 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
337 gst_pad_push_event (src->srcpad, event);
340 /* and restart the task */
341 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_basesrc_loop,
343 GST_STREAM_UNLOCK (src->srcpad);
345 gst_event_unref (event);
352 GST_DEBUG_OBJECT (src, "seek error");
353 gst_event_unref (event);
359 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
362 GstBaseSrcClass *bclass;
365 src = GST_BASESRC (GST_PAD_PARENT (pad));
366 bclass = GST_BASESRC_GET_CLASS (src);
369 result = bclass->event (src, event);
371 switch (GST_EVENT_TYPE (event)) {
373 return gst_basesrc_do_seek (src, event);
378 format = GST_EVENT_SIZE_FORMAT (event);
379 if (format == GST_FORMAT_DEFAULT)
380 format = GST_FORMAT_BYTES;
381 /* we can only accept bytes */
382 if (format != GST_FORMAT_BYTES)
385 src->blocksize = GST_EVENT_SIZE_VALUE (event);
386 g_object_notify (G_OBJECT (src), "blocksize");
389 case GST_EVENT_FLUSH:
390 /* cancel any blocking getrange */
391 if (!GST_EVENT_FLUSH_DONE (event))
392 gst_basesrc_unlock (src);
397 gst_event_unref (event);
403 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
408 src = GST_BASESRC (object);
412 src->blocksize = g_value_get_ulong (value);
415 src->has_loop = g_value_get_boolean (value);
416 gst_basesrc_set_dataflow_funcs (src);
418 case PROP_HAS_GETRANGE:
419 src->has_getrange = g_value_get_boolean (value);
420 gst_basesrc_set_dataflow_funcs (src);
423 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
434 src = GST_BASESRC (object);
438 g_value_set_ulong (value, src->blocksize);
441 g_value_set_boolean (value, src->has_loop);
443 case PROP_HAS_GETRANGE:
444 g_value_set_boolean (value, src->has_getrange);
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
458 GstBaseSrcClass *bclass;
460 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
461 bclass = GST_BASESRC_GET_CLASS (src);
463 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
470 if (src->size != -1) {
471 if (offset + length > src->size) {
472 if (bclass->get_size)
473 bclass->get_size (src, &src->size);
475 if (offset + length > src->size) {
476 length = src->size - offset;
481 goto unexpected_length;
483 ret = bclass->create (src, offset, length, buf);
490 GST_DEBUG_OBJECT (src, "getrange but not started");
491 return GST_FLOW_WRONG_STATE;
495 GST_DEBUG_OBJECT (src, "no create function");
496 return GST_FLOW_ERROR;
500 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
501 return GST_FLOW_UNEXPECTED;
506 gst_basesrc_check_get_range (GstPad * pad)
510 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
512 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
513 gst_basesrc_start (src);
514 gst_basesrc_stop (src);
517 return src->seekable;
521 gst_basesrc_loop (GstPad * pad)
524 GstBuffer *buf = NULL;
527 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
529 ret = gst_basesrc_get_range (pad, src->offset, src->blocksize, &buf);
530 if (ret != GST_FLOW_OK)
533 src->offset += GST_BUFFER_SIZE (buf);
535 ret = gst_pad_push (pad, buf);
536 if (ret != GST_FLOW_OK)
543 GST_DEBUG_OBJECT (src, "going to EOS");
544 gst_pad_pause_task (pad);
545 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
550 GST_DEBUG_OBJECT (src, "pausing task");
551 gst_pad_pause_task (pad);
557 gst_basesrc_unlock (GstBaseSrc * basesrc)
559 GstBaseSrcClass *bclass;
560 gboolean result = FALSE;
562 GST_DEBUG ("unlock");
563 /* unblock whatever the subclass is doing */
564 bclass = GST_BASESRC_GET_CLASS (basesrc);
566 result = bclass->unlock (basesrc);
568 GST_DEBUG ("unschedule clock");
569 /* and unblock the clock as well, if any */
571 if (basesrc->clock_id) {
572 gst_clock_id_unschedule (basesrc->clock_id);
574 GST_UNLOCK (basesrc);
576 GST_DEBUG ("unlock done");
582 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
584 GstBaseSrcClass *bclass;
585 gboolean result = FALSE;
587 bclass = GST_BASESRC_GET_CLASS (basesrc);
588 if (bclass->get_size)
589 result = bclass->get_size (basesrc, size);
592 basesrc->size = *size;
598 gst_basesrc_is_seekable (GstBaseSrc * basesrc)
600 GstBaseSrcClass *bclass;
602 bclass = GST_BASESRC_GET_CLASS (basesrc);
604 /* check if we can seek */
605 if (bclass->is_seekable)
606 basesrc->seekable = bclass->is_seekable (basesrc);
608 basesrc->seekable = FALSE;
610 return basesrc->seekable;
614 gst_basesrc_start (GstBaseSrc * basesrc)
616 GstBaseSrcClass *bclass;
619 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
622 bclass = GST_BASESRC_GET_CLASS (basesrc);
624 result = bclass->start (basesrc);
629 goto could_not_start;
631 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
633 /* start in the beginning */
635 basesrc->segment_start = 0;
637 /* figure out the size */
638 if (bclass->get_size) {
639 result = bclass->get_size (basesrc, &basesrc->size);
647 GST_DEBUG ("size %d %lld", result, basesrc->size);
649 /* we always run to the end */
650 basesrc->segment_end = -1;
652 /* check if we can seek, updates ->seekable */
653 gst_basesrc_is_seekable (basesrc);
657 if (basesrc->seekable) {
660 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
661 gst_pad_set_caps (basesrc->srcpad, caps);
670 GST_DEBUG_OBJECT (basesrc, "could not start");
676 gst_basesrc_stop (GstBaseSrc * basesrc)
678 GstBaseSrcClass *bclass;
679 gboolean result = TRUE;
681 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
684 bclass = GST_BASESRC_GET_CLASS (basesrc);
686 result = bclass->stop (basesrc);
689 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
695 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
700 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
702 /* prepare subclass first */
704 case GST_ACTIVATE_PUSH:
705 case GST_ACTIVATE_PULL:
706 result = gst_basesrc_start (basesrc);
712 /* if that failed we can stop here */
718 case GST_ACTIVATE_PUSH:
720 gst_pad_start_task (pad, (GstTaskFunction) gst_basesrc_loop, pad);
722 case GST_ACTIVATE_PULL:
723 result = basesrc->seekable;
725 gst_basesrc_stop (basesrc);
727 case GST_ACTIVATE_NONE:
728 /* step 1, unblock clock sync (if any) */
729 gst_basesrc_unlock (basesrc);
731 /* step 2, make sure streaming finishes */
732 result = gst_pad_stop_task (pad);
740 GST_DEBUG_OBJECT (basesrc, "failed to start");
745 static GstElementStateReturn
746 gst_basesrc_change_state (GstElement * element)
749 GstElementStateReturn result = GST_STATE_FAILURE;
750 GstElementState transition;
752 basesrc = GST_BASESRC (element);
754 transition = GST_STATE_TRANSITION (element);
756 switch (transition) {
757 case GST_STATE_NULL_TO_READY:
759 case GST_STATE_READY_TO_PAUSED:
761 case GST_STATE_PAUSED_TO_PLAYING:
767 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
769 switch (transition) {
770 case GST_STATE_PLAYING_TO_PAUSED:
772 case GST_STATE_PAUSED_TO_READY:
773 if (!gst_basesrc_stop (basesrc))
774 result = GST_STATE_FAILURE;
776 case GST_STATE_READY_TO_NULL: