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_start = offset;
284 src->segment_end = GST_EVENT_SEEK_ENDOFFSET (event);
285 src->segment_loop = GST_EVENT_SEEK_FLAGS (event) & GST_SEEK_FLAG_SEGMENT_LOOP;
287 /* send flush start */
288 gst_pad_push_event (src->srcpad, gst_event_new_flush (FALSE));
290 /* unblock streaming thread */
291 gst_basesrc_unlock (src);
293 /* grab streaming lock */
294 GST_STREAM_LOCK (src->srcpad);
296 switch (GST_EVENT_SEEK_METHOD (event)) {
297 case GST_SEEK_METHOD_SET:
300 src->offset = MIN (offset, src->size);
301 GST_DEBUG_OBJECT (src, "seek set pending to %" G_GINT64_FORMAT,
304 case GST_SEEK_METHOD_CUR:
305 offset += src->offset;
306 src->offset = CLAMP (offset, 0, src->size);
307 GST_DEBUG_OBJECT (src, "seek cur pending to %" G_GINT64_FORMAT,
310 case GST_SEEK_METHOD_END:
313 offset = src->size + offset;
314 src->offset = MAX (0, offset);
315 GST_DEBUG_OBJECT (src, "seek end pending to %" G_GINT64_FORMAT,
322 gst_pad_push_event (src->srcpad, gst_event_new_flush (TRUE));
324 /* now send discont */
328 event = gst_event_new_discontinuous (1.0,
330 (gint64) src->segment_start, (gint64) src->segment_end, NULL);
332 gst_pad_push_event (src->srcpad, event);
335 /* and restart the task */
336 gst_pad_start_task (src->srcpad, (GstTaskFunction) gst_basesrc_loop,
338 GST_STREAM_UNLOCK (src->srcpad);
340 gst_event_unref (event);
347 GST_DEBUG_OBJECT (src, "seek error");
348 GST_STREAM_UNLOCK (src->srcpad);
349 gst_event_unref (event);
355 gst_basesrc_event_handler (GstPad * pad, GstEvent * event)
358 GstBaseSrcClass *bclass;
361 src = GST_BASESRC (GST_PAD_PARENT (pad));
362 bclass = GST_BASESRC_GET_CLASS (src);
365 result = bclass->event (src, event);
367 switch (GST_EVENT_TYPE (event)) {
369 return gst_basesrc_do_seek (src, event);
374 format = GST_EVENT_SIZE_FORMAT (event);
375 if (format == GST_FORMAT_DEFAULT)
376 format = GST_FORMAT_BYTES;
377 /* we can only accept bytes */
378 if (format != GST_FORMAT_BYTES)
381 src->blocksize = GST_EVENT_SIZE_VALUE (event);
382 g_object_notify (G_OBJECT (src), "blocksize");
385 case GST_EVENT_FLUSH:
386 /* cancel any blocking getrange */
387 if (!GST_EVENT_FLUSH_DONE (event))
388 gst_basesrc_unlock (src);
393 gst_event_unref (event);
399 gst_basesrc_set_property (GObject * object, guint prop_id, const GValue * value,
404 src = GST_BASESRC (object);
408 src->blocksize = g_value_get_ulong (value);
411 src->has_loop = g_value_get_boolean (value);
412 gst_basesrc_set_dataflow_funcs (src);
414 case PROP_HAS_GETRANGE:
415 src->has_getrange = g_value_get_boolean (value);
416 gst_basesrc_set_dataflow_funcs (src);
419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425 gst_basesrc_get_property (GObject * object, guint prop_id, GValue * value,
430 src = GST_BASESRC (object);
434 g_value_set_ulong (value, src->blocksize);
437 g_value_set_boolean (value, src->has_loop);
439 case PROP_HAS_GETRANGE:
440 g_value_set_boolean (value, src->has_getrange);
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gst_basesrc_get_range (GstPad * pad, guint64 offset, guint length,
454 GstBaseSrcClass *bclass;
456 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
457 bclass = GST_BASESRC_GET_CLASS (src);
459 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED))
466 if (src->size != -1) {
467 if (offset + length > src->size) {
468 if (bclass->get_size)
469 bclass->get_size (src, &src->size);
471 if (offset + length > src->size) {
472 length = src->size - offset;
477 goto unexpected_length;
479 ret = bclass->create (src, offset, length, buf);
486 GST_DEBUG_OBJECT (src, "getrange but not started");
487 return GST_FLOW_WRONG_STATE;
491 GST_DEBUG_OBJECT (src, "no create function");
492 return GST_FLOW_ERROR;
496 GST_DEBUG_OBJECT (src, "unexpected length %u", length);
497 return GST_FLOW_UNEXPECTED;
502 gst_basesrc_check_get_range (GstPad * pad)
506 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
508 if (!GST_FLAG_IS_SET (src, GST_BASESRC_STARTED)) {
509 gst_basesrc_start (src);
510 gst_basesrc_stop (src);
513 return src->seekable;
517 gst_basesrc_loop (GstPad * pad)
520 GstBuffer *buf = NULL;
523 src = GST_BASESRC (GST_OBJECT_PARENT (pad));
525 ret = gst_basesrc_get_range (pad, src->offset, src->blocksize, &buf);
526 if (ret != GST_FLOW_OK)
529 src->offset += GST_BUFFER_SIZE (buf);
531 ret = gst_pad_push (pad, buf);
532 if (ret != GST_FLOW_OK)
539 GST_DEBUG_OBJECT (src, "going to EOS");
540 gst_pad_pause_task (pad);
541 gst_pad_push_event (pad, gst_event_new (GST_EVENT_EOS));
546 GST_DEBUG_OBJECT (src, "pausing task");
547 gst_pad_pause_task (pad);
553 gst_basesrc_unlock (GstBaseSrc * basesrc)
555 GstBaseSrcClass *bclass;
556 gboolean result = FALSE;
558 GST_DEBUG ("unlock");
559 /* unblock whatever the subclass is doing */
560 bclass = GST_BASESRC_GET_CLASS (basesrc);
562 result = bclass->unlock (basesrc);
564 GST_DEBUG ("unschedule clock");
565 /* and unblock the clock as well, if any */
567 if (basesrc->clock_id) {
568 gst_clock_id_unschedule (basesrc->clock_id);
570 GST_UNLOCK (basesrc);
572 GST_DEBUG ("unlock done");
578 gst_basesrc_get_size (GstBaseSrc * basesrc, guint64 * size)
580 GstBaseSrcClass *bclass;
581 gboolean result = FALSE;
583 bclass = GST_BASESRC_GET_CLASS (basesrc);
584 if (bclass->get_size)
585 result = bclass->get_size (basesrc, size);
588 basesrc->size = *size;
594 gst_basesrc_is_seekable (GstBaseSrc * basesrc)
596 GstBaseSrcClass *bclass;
598 bclass = GST_BASESRC_GET_CLASS (basesrc);
600 /* check if we can seek */
601 if (bclass->is_seekable)
602 basesrc->seekable = bclass->is_seekable (basesrc);
604 basesrc->seekable = FALSE;
606 return basesrc->seekable;
610 gst_basesrc_start (GstBaseSrc * basesrc)
612 GstBaseSrcClass *bclass;
615 if (GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
618 bclass = GST_BASESRC_GET_CLASS (basesrc);
620 result = bclass->start (basesrc);
625 goto could_not_start;
627 GST_FLAG_SET (basesrc, GST_BASESRC_STARTED);
629 /* start in the beginning */
631 basesrc->segment_start = 0;
633 /* figure out the size */
634 if (bclass->get_size) {
635 result = bclass->get_size (basesrc, &basesrc->size);
643 GST_DEBUG ("size %d %lld", result, basesrc->size);
645 /* we always run to the end */
646 basesrc->segment_end = -1;
648 /* check if we can seek, updates ->seekable */
649 gst_basesrc_is_seekable (basesrc);
653 if (basesrc->seekable) {
656 caps = gst_type_find_helper (basesrc->srcpad, basesrc->size);
657 gst_pad_set_caps (basesrc->srcpad, caps);
666 GST_DEBUG_OBJECT (basesrc, "could not start");
672 gst_basesrc_stop (GstBaseSrc * basesrc)
674 GstBaseSrcClass *bclass;
675 gboolean result = TRUE;
677 if (!GST_FLAG_IS_SET (basesrc, GST_BASESRC_STARTED))
680 bclass = GST_BASESRC_GET_CLASS (basesrc);
682 result = bclass->stop (basesrc);
685 GST_FLAG_UNSET (basesrc, GST_BASESRC_STARTED);
691 gst_basesrc_activate (GstPad * pad, GstActivateMode mode)
696 basesrc = GST_BASESRC (GST_OBJECT_PARENT (pad));
698 /* prepare subclass first */
700 case GST_ACTIVATE_PUSH:
701 case GST_ACTIVATE_PULL:
702 result = gst_basesrc_start (basesrc);
708 /* if that failed we can stop here */
714 case GST_ACTIVATE_PUSH:
716 gst_pad_start_task (pad, (GstTaskFunction) gst_basesrc_loop, pad);
718 case GST_ACTIVATE_PULL:
719 result = basesrc->seekable;
721 gst_basesrc_stop (basesrc);
723 case GST_ACTIVATE_NONE:
724 /* step 1, unblock clock sync (if any) */
725 gst_basesrc_unlock (basesrc);
727 /* step 2, make sure streaming finishes */
728 result = gst_pad_stop_task (pad);
736 GST_DEBUG_OBJECT (basesrc, "failed to start");
741 static GstElementStateReturn
742 gst_basesrc_change_state (GstElement * element)
745 GstElementStateReturn result = GST_STATE_FAILURE;
746 GstElementState transition;
748 basesrc = GST_BASESRC (element);
750 transition = GST_STATE_TRANSITION (element);
752 switch (transition) {
753 case GST_STATE_NULL_TO_READY:
755 case GST_STATE_READY_TO_PAUSED:
757 case GST_STATE_PAUSED_TO_PLAYING:
763 result = GST_ELEMENT_CLASS (parent_class)->change_state (element);
765 switch (transition) {
766 case GST_STATE_PLAYING_TO_PAUSED:
768 case GST_STATE_PAUSED_TO_READY:
769 if (!gst_basesrc_stop (basesrc))
770 result = GST_STATE_FAILURE;
772 case GST_STATE_READY_TO_NULL: