2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2005 Philippe Khalaf <burger@speedy.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include "gst/gst_private.h"
29 #include <sys/types.h>
41 #define DEFAULT_BLOCKSIZE 4096
43 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
48 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
49 #define GST_CAT_DEFAULT gst_fdsrc_debug
51 GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS ("Disk Source",
53 "Synchronous read from a file",
54 "Erik Walthinsen <omega@cse.ogi.edu>");
57 /* FdSrc signals and args */
72 static guint gst_fdsrc_signals[LAST_SIGNAL] = { 0 };
74 #define _do_init(bla) \
75 GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
77 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_PUSH_SRC,
80 static void gst_fdsrc_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_fdsrc_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstStateChangeReturn gst_fdsrc_change_state (GstElement * element,
86 GstStateChange transition);
88 static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
91 gst_fdsrc_base_init (gpointer g_class)
93 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
95 gst_element_class_add_pad_template (gstelement_class,
96 gst_static_pad_template_get (&srctemplate));
97 gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
100 gst_fdsrc_class_init (GstFdSrcClass * klass)
102 GObjectClass *gobject_class;
103 GstBaseSrcClass *gstbasesrc_class;
104 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
105 GstPushSrcClass *gstpush_src_class;
107 gobject_class = G_OBJECT_CLASS (klass);
108 gstbasesrc_class = (GstBaseSrcClass *) klass;
109 gstpush_src_class = (GstPushSrcClass *) klass;
111 parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
113 gobject_class->set_property = gst_fdsrc_set_property;
114 gobject_class->get_property = gst_fdsrc_get_property;
116 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
117 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
118 0, G_MAXINT, 0, G_PARAM_READWRITE));
119 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BLOCKSIZE,
120 g_param_spec_ulong ("blocksize", "Block size",
121 "Size in bytes to read per buffer", 1, G_MAXULONG, DEFAULT_BLOCKSIZE,
123 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TIMEOUT,
124 g_param_spec_uint64 ("timeout", "Timeout", "Read timeout in nanoseconds",
125 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
127 gst_fdsrc_signals[SIGNAL_TIMEOUT] =
128 g_signal_new ("timeout", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
129 G_STRUCT_OFFSET (GstFdSrcClass, timeout), NULL, NULL,
130 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
132 gstelement_class->change_state = gst_fdsrc_change_state;
134 gstpush_src_class->create = gst_fdsrc_create;
139 gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
141 // TODO set live only if it's actually a live source
142 gst_base_src_set_live (GST_BASE_SRC (fdsrc), TRUE);
145 fdsrc->curoffset = 0;
146 fdsrc->blocksize = DEFAULT_BLOCKSIZE;
151 static GstStateChangeReturn
152 gst_fdsrc_change_state (GstElement * element, GstStateChange transition)
154 GstFdSrc *src = GST_FDSRC (element);
156 switch (transition) {
157 case GST_STATE_CHANGE_NULL_TO_READY:
159 case GST_STATE_CHANGE_READY_TO_NULL:
161 case GST_STATE_CHANGE_READY_TO_PAUSED:
164 case GST_STATE_CHANGE_PAUSED_TO_READY:
170 if (GST_ELEMENT_CLASS (parent_class)->change_state)
171 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
173 return GST_STATE_CHANGE_SUCCESS;
178 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
183 g_return_if_fail (GST_IS_FDSRC (object));
185 src = GST_FDSRC (object);
189 src->fd = g_value_get_int (value);
192 src->blocksize = g_value_get_ulong (value);
195 src->timeout = g_value_get_uint64 (value);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
209 g_return_if_fail (GST_IS_FDSRC (object));
211 src = GST_FDSRC (object);
215 g_value_set_ulong (value, src->blocksize);
218 g_value_set_int (value, src->fd);
221 g_value_set_uint64 (value, src->timeout);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
238 struct timeval t, *tp = &t;
242 src = GST_FDSRC (psrc);
244 /* create the buffer */
245 buf = gst_buffer_new_and_alloc (src->blocksize);
249 FD_SET (src->fd, &readfds);
251 if (src->timeout != 0) {
252 GST_TIME_TO_TIMEVAL (src->timeout, t);
257 retval = select (src->fd + 1, &readfds, NULL, NULL, tp);
258 } while (retval == -1 && errno == EINTR); /* retry if interrupted */
261 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
262 ("select on file descriptor: %s.", g_strerror (errno)));
263 GST_DEBUG_OBJECT (psrc, "Error during select");
264 return GST_FLOW_ERROR;
265 } else if (retval == 0) {
266 g_signal_emit (G_OBJECT (src), gst_fdsrc_signals[SIGNAL_TIMEOUT], 0);
267 GST_DEBUG_OBJECT (psrc, "Timeout in select");
268 return GST_FLOW_ERROR;
273 readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
274 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
277 GST_BUFFER_OFFSET (buf) = src->curoffset;
278 GST_BUFFER_SIZE (buf) = readbytes;
279 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
280 src->curoffset += readbytes;
282 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
284 /* we're done, return the buffer */
287 } else if (readbytes == 0) {
288 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
289 return GST_FLOW_ERROR;
291 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
292 ("read on file descriptor: %s.", g_strerror (errno)));
293 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
294 return GST_FLOW_ERROR;