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 (check
142 * for seekable fd) */
143 gst_base_src_set_live (GST_BASE_SRC (fdsrc), TRUE);
146 fdsrc->curoffset = 0;
147 fdsrc->blocksize = DEFAULT_BLOCKSIZE;
152 static GstStateChangeReturn
153 gst_fdsrc_change_state (GstElement * element, GstStateChange transition)
155 GstFdSrc *src = GST_FDSRC (element);
157 switch (transition) {
158 case GST_STATE_CHANGE_NULL_TO_READY:
160 case GST_STATE_CHANGE_READY_TO_NULL:
162 case GST_STATE_CHANGE_READY_TO_PAUSED:
165 case GST_STATE_CHANGE_PAUSED_TO_READY:
171 if (GST_ELEMENT_CLASS (parent_class)->change_state)
172 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
174 return GST_STATE_CHANGE_SUCCESS;
179 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
184 g_return_if_fail (GST_IS_FDSRC (object));
186 src = GST_FDSRC (object);
190 src->fd = g_value_get_int (value);
193 src->blocksize = g_value_get_ulong (value);
196 src->timeout = g_value_get_uint64 (value);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
210 g_return_if_fail (GST_IS_FDSRC (object));
212 src = GST_FDSRC (object);
216 g_value_set_ulong (value, src->blocksize);
219 g_value_set_int (value, src->fd);
222 g_value_set_uint64 (value, src->timeout);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 #define SELECT_TIMEOUT (GST_SECOND / 20)
233 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
238 GstClockTime timeout;
242 struct timeval t, *tp = &t;
246 src = GST_FDSRC (psrc);
250 FD_SET (src->fd, &readfds);
252 if (src->timeout != 0) {
253 timeout = MIN (SELECT_TIMEOUT, src->timeout);
255 timeout = SELECT_TIMEOUT;
259 GST_TIME_TO_TIMEVAL (timeout, t);
260 if (src->timeout != 0)
261 timeout -= MIN (timeout, SELECT_TIMEOUT);
263 retval = select (src->fd + 1, &readfds, NULL, NULL, tp);
265 /* Check whether the element got shutdown before full timeout */
267 if (GST_PAD_IS_FLUSHING (GST_BASE_SRC_PAD (src))) {
268 GST_DEBUG_OBJECT (src, "Shutting down with no buffer.");
269 return GST_FLOW_WRONG_STATE;
272 } while ((retval == -1 && errno == EINTR) || /* retry if interrupted */
273 (retval == 0 && timeout > 0)); /* Retry on incomplete timeout */
276 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
277 ("select on file descriptor: %s.", g_strerror (errno)));
278 GST_DEBUG_OBJECT (psrc, "Error during select");
279 return GST_FLOW_ERROR;
280 } else if (retval == 0) {
281 g_signal_emit (G_OBJECT (src), gst_fdsrc_signals[SIGNAL_TIMEOUT], 0);
282 GST_DEBUG_OBJECT (psrc, "Timeout in select");
283 return GST_FLOW_ERROR;
287 /* create the buffer */
288 buf = gst_buffer_new_and_alloc (src->blocksize);
291 readbytes = read (src->fd, GST_BUFFER_DATA (buf), src->blocksize);
292 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
295 GST_BUFFER_OFFSET (buf) = src->curoffset;
296 GST_BUFFER_SIZE (buf) = readbytes;
297 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
298 src->curoffset += readbytes;
300 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
302 /* we're done, return the buffer */
305 } else if (readbytes == 0) {
306 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
307 gst_buffer_unref (buf);
308 return GST_FLOW_ERROR;
310 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
311 ("read on file descriptor: %s.", g_strerror (errno)));
312 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
313 gst_buffer_unref (buf);
314 return GST_FLOW_ERROR;