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.
24 * SECTION:element-fdsrc
25 * @short_description: read from a unix file descriptor
26 * @see_also: #GstFdSink
30 * Read data from a unix file descriptor.
32 * <title>Examples</title>
34 * Here is a simple pipeline to read from the standard input and dump the data
37 * gst-launch -v fdsrc ! fakesink dump=1
39 * To generate data, enter some data on the console folowed by enter.
40 * The above mentioned pipeline should dump data packets to the console.
43 * If the <link linkend="GstFdSrc--timeout">timeout property</link> is set to a
44 * value bigger than 0, fdsrc will generate an element message named
45 * <classname>"GstFdSrcTimeout"</classname>
46 * if no data was recieved in the given timeout.
47 * The message's structure contains one field:
52 * <classname>"timeout"</classname>: the timeout in microseconds that
53 * expired when waiting for data.
59 * Last reviewed on 2008-06-20 (0.10.21)
67 #include "gst/gst_private.h"
69 #include <sys/types.h>
71 #include <sys/socket.h>
85 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
91 #define GST_CAT_DEFAULT gst_fd_src_debug
94 #define DEFAULT_TIMEOUT 0
106 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
109 _do_init (GType fd_src_type)
111 static const GInterfaceInfo urihandler_info = {
112 gst_fd_src_uri_handler_init,
117 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
120 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
123 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
126 static void gst_fd_src_set_property (GObject * object, guint prop_id,
127 const GValue * value, GParamSpec * pspec);
128 static void gst_fd_src_get_property (GObject * object, guint prop_id,
129 GValue * value, GParamSpec * pspec);
130 static void gst_fd_src_dispose (GObject * obj);
132 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
133 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
134 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
135 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
136 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
137 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
138 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
140 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
143 gst_fd_src_base_init (gpointer g_class)
145 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
147 gst_element_class_set_details_simple (gstelement_class,
148 "Filedescriptor Source",
150 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
151 gst_element_class_add_pad_template (gstelement_class,
152 gst_static_pad_template_get (&srctemplate));
156 gst_fd_src_class_init (GstFdSrcClass * klass)
158 GObjectClass *gobject_class;
159 GstBaseSrcClass *gstbasesrc_class;
160 GstElementClass *gstelement_class;
161 GstPushSrcClass *gstpush_src_class;
163 gobject_class = G_OBJECT_CLASS (klass);
164 gstelement_class = GST_ELEMENT_CLASS (klass);
165 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
166 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
168 parent_class = g_type_class_peek_parent (klass);
170 gobject_class->set_property = gst_fd_src_set_property;
171 gobject_class->get_property = gst_fd_src_get_property;
172 gobject_class->dispose = gst_fd_src_dispose;
174 g_object_class_install_property (gobject_class, PROP_FD,
175 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
176 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
178 g_param_spec_uint64 ("timeout", "Timeout",
179 "Post a message after timeout microseconds (0 = disabled)", 0,
180 G_MAXUINT64, DEFAULT_TIMEOUT,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
184 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
185 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
186 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
187 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
188 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
189 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
191 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
195 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
198 fdsrc->seekable_fd = FALSE;
199 fdsrc->fd = DEFAULT_FD;
200 fdsrc->timeout = DEFAULT_TIMEOUT;
201 fdsrc->uri = g_strdup_printf ("fd://0");
202 fdsrc->curoffset = 0;
206 gst_fd_src_dispose (GObject * obj)
208 GstFdSrc *src = GST_FD_SRC (obj);
213 G_OBJECT_CLASS (parent_class)->dispose (obj);
217 gst_fd_src_update_fd (GstFdSrc * src)
219 struct stat stat_results;
221 /* we need to always update the fdset since it may not have existed when
222 * gst_fd_src_update_fd () was called earlier */
223 if (src->fdset != NULL) {
224 GstPollFD fd = GST_POLL_FD_INIT;
228 gst_poll_remove_fd (src->fdset, &fd);
232 gst_poll_add_fd (src->fdset, &fd);
233 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
236 if (src->fd != src->new_fd) {
237 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
239 src->fd = src->new_fd;
242 src->uri = g_strdup_printf ("fd://%d", src->fd);
244 if (fstat (src->fd, &stat_results) < 0)
247 if (!S_ISREG (stat_results.st_mode))
250 /* Try a seek of 0 bytes offset to check for seekability */
251 if (lseek (src->fd, 0, SEEK_CUR) < 0)
254 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
255 src->seekable_fd = TRUE;
261 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
262 src->seekable_fd = FALSE;
267 gst_fd_src_start (GstBaseSrc * bsrc)
269 GstFdSrc *src = GST_FD_SRC (bsrc);
273 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
276 gst_fd_src_update_fd (src);
283 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
290 gst_fd_src_stop (GstBaseSrc * bsrc)
292 GstFdSrc *src = GST_FD_SRC (bsrc);
295 gst_poll_free (src->fdset);
303 gst_fd_src_unlock (GstBaseSrc * bsrc)
305 GstFdSrc *src = GST_FD_SRC (bsrc);
307 GST_LOG_OBJECT (src, "Flushing");
308 GST_OBJECT_LOCK (src);
309 gst_poll_set_flushing (src->fdset, TRUE);
310 GST_OBJECT_UNLOCK (src);
316 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
318 GstFdSrc *src = GST_FD_SRC (bsrc);
320 GST_LOG_OBJECT (src, "No longer flushing");
321 GST_OBJECT_LOCK (src);
322 gst_poll_set_flushing (src->fdset, FALSE);
323 GST_OBJECT_UNLOCK (src);
329 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
332 GstFdSrc *src = GST_FD_SRC (object);
336 src->new_fd = g_value_get_int (value);
338 /* If state is ready or below, update the current fd immediately
339 * so it is reflected in get_properties and uri */
340 GST_OBJECT_LOCK (object);
341 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
342 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
343 gst_fd_src_update_fd (src);
345 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
347 GST_OBJECT_UNLOCK (object);
350 src->timeout = g_value_get_uint64 (value);
351 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
352 GST_TIME_ARGS (src->timeout));
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
364 GstFdSrc *src = GST_FD_SRC (object);
368 g_value_set_int (value, src->fd);
371 g_value_set_uint64 (value, src->timeout);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
386 GstClockTime timeout;
393 src = GST_FD_SRC (psrc);
395 if (src->timeout > 0) {
396 timeout = src->timeout * GST_USECOND;
398 timeout = GST_CLOCK_TIME_NONE;
405 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
406 GST_TIME_ARGS (src->timeout));
408 retval = gst_poll_wait (src->fdset, timeout);
409 GST_LOG_OBJECT (src, "poll returned %d", retval);
411 if (G_UNLIKELY (retval == -1)) {
412 if (errno == EINTR || errno == EAGAIN) {
413 /* retry if interrupted */
415 } else if (errno == EBUSY) {
420 } else if (G_UNLIKELY (retval == 0)) {
422 /* timeout, post element message */
423 gst_element_post_message (GST_ELEMENT_CAST (src),
424 gst_message_new_element (GST_OBJECT_CAST (src),
425 gst_structure_new ("GstFdSrcTimeout",
426 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
428 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
431 blocksize = GST_BASE_SRC (src)->blocksize;
433 /* create the buffer */
434 buf = gst_buffer_new_and_alloc (blocksize);
437 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
438 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
439 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
447 GST_BUFFER_OFFSET (buf) = src->curoffset;
448 GST_BUFFER_SIZE (buf) = readbytes;
449 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
450 src->curoffset += readbytes;
452 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
454 /* we're done, return the buffer */
463 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
464 ("poll on file descriptor: %s.", g_strerror (errno)));
465 GST_DEBUG_OBJECT (psrc, "Error during poll");
466 return GST_FLOW_ERROR;
470 GST_DEBUG_OBJECT (psrc, "Poll stopped");
471 return GST_FLOW_WRONG_STATE;
476 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
477 gst_buffer_unref (buf);
478 return GST_FLOW_UNEXPECTED;
482 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
483 ("read on file descriptor: %s.", g_strerror (errno)));
484 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
485 gst_buffer_unref (buf);
486 return GST_FLOW_ERROR;
491 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
493 GstFdSrc *src = GST_FD_SRC (bsrc);
495 return src->seekable_fd;
499 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
501 GstFdSrc *src = GST_FD_SRC (bsrc);
502 struct stat stat_results;
504 if (!src->seekable_fd) {
505 /* If it isn't seekable, we won't know the length (but fstat will still
506 * succeed, and wrongly say our length is zero. */
510 if (fstat (src->fd, &stat_results) < 0)
513 *size = stat_results.st_size;
525 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
529 GstFdSrc *src = GST_FD_SRC (bsrc);
531 offset = segment->start;
533 /* No need to seek to the current position */
534 if (offset == src->curoffset)
537 res = lseek (src->fd, offset, SEEK_SET);
538 if (G_UNLIKELY (res < 0 || res != offset))
541 segment->last_stop = segment->start;
542 segment->time = segment->start;
547 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
551 /*** GSTURIHANDLER INTERFACE *************************************************/
554 gst_fd_src_uri_get_type (void)
559 gst_fd_src_uri_get_protocols (void)
561 static gchar *protocols[] = { "fd", NULL };
566 gst_fd_src_uri_get_uri (GstURIHandler * handler)
568 GstFdSrc *src = GST_FD_SRC (handler);
574 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
577 GstFdSrc *src = GST_FD_SRC (handler);
580 protocol = gst_uri_get_protocol (uri);
581 if (strcmp (protocol, "fd") != 0) {
587 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
592 GST_OBJECT_LOCK (src);
593 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
594 gst_fd_src_update_fd (src);
596 GST_OBJECT_UNLOCK (src);
602 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
604 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
606 iface->get_type = gst_fd_src_uri_get_type;
607 iface->get_protocols = gst_fd_src_uri_get_protocols;
608 iface->get_uri = gst_fd_src_uri_get_uri;
609 iface->set_uri = gst_fd_src_uri_set_uri;