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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 * SECTION:element-fdsrc
25 * @see_also: #GstFdSink
27 * Read data from a unix file descriptor.
29 * To generate data, enter some data on the console folowed by enter.
30 * The above mentioned pipeline should dump data packets to the console.
32 * If the #GstFdSrc:timeout property is set to a value bigger than 0, fdsrc will
33 * generate an element message named
34 * <classname>"GstFdSrcTimeout"</classname>
35 * if no data was recieved in the given timeout.
36 * The message's structure contains one field:
41 * <classname>"timeout"</classname>: the timeout in microseconds that
42 * expired when waiting for data.
48 * <title>Example launch line</title>
50 * echo "Hello GStreamer" | gst-launch -v fdsrc ! fakesink dump=true
51 * ]| A simple pipeline to read from the standard input and dump the data
52 * with a fakesink as hex ascii block.
55 * Last reviewed on 2008-06-20 (0.10.21)
61 #include "gst/gst_private.h"
63 #include <sys/types.h>
66 #include <io.h> /* lseek, open, close, read */
68 #define lseek _lseeki64
74 #ifdef HAVE_SYS_SOCKET_H
75 #include <sys/socket.h>
86 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
93 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
98 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
99 #define GST_CAT_DEFAULT gst_fd_src_debug
102 #define DEFAULT_TIMEOUT 0
114 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
117 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_src_uri_handler_init); \
118 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
119 #define gst_fd_src_parent_class parent_class
120 G_DEFINE_TYPE_WITH_CODE (GstFdSrc, gst_fd_src, GST_TYPE_PUSH_SRC, _do_init);
122 static void gst_fd_src_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_fd_src_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
126 static void gst_fd_src_dispose (GObject * obj);
128 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
129 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
130 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
131 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
132 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
133 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
134 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
135 static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
137 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
140 gst_fd_src_class_init (GstFdSrcClass * klass)
142 GObjectClass *gobject_class;
143 GstElementClass *gstelement_class;
144 GstBaseSrcClass *gstbasesrc_class;
145 GstPushSrcClass *gstpush_src_class;
147 gobject_class = G_OBJECT_CLASS (klass);
148 gstelement_class = GST_ELEMENT_CLASS (klass);
149 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
150 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
152 gobject_class->set_property = gst_fd_src_set_property;
153 gobject_class->get_property = gst_fd_src_get_property;
154 gobject_class->dispose = gst_fd_src_dispose;
156 g_object_class_install_property (gobject_class, PROP_FD,
157 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
158 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162 * Post a message after timeout microseconds
164 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
165 g_param_spec_uint64 ("timeout", "Timeout",
166 "Post a message after timeout microseconds (0 = disabled)", 0,
167 G_MAXUINT64, DEFAULT_TIMEOUT,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 gst_element_class_set_static_metadata (gstelement_class,
171 "Filedescriptor Source",
173 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
174 gst_element_class_add_pad_template (gstelement_class,
175 gst_static_pad_template_get (&srctemplate));
177 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
178 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
179 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
180 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
181 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
182 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
183 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
184 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
186 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
190 gst_fd_src_init (GstFdSrc * fdsrc)
192 fdsrc->new_fd = DEFAULT_FD;
193 fdsrc->seekable_fd = FALSE;
196 fdsrc->timeout = DEFAULT_TIMEOUT;
197 fdsrc->uri = g_strdup_printf ("fd://0");
198 fdsrc->curoffset = 0;
202 gst_fd_src_dispose (GObject * obj)
204 GstFdSrc *src = GST_FD_SRC (obj);
209 G_OBJECT_CLASS (parent_class)->dispose (obj);
213 gst_fd_src_update_fd (GstFdSrc * src, guint64 size)
215 struct stat stat_results;
217 GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
220 /* we need to always update the fdset since it may not have existed when
221 * gst_fd_src_update_fd () was called earlier */
222 if (src->fdset != NULL) {
223 GstPollFD fd = GST_POLL_FD_INIT;
227 /* this will log a harmless warning, if it was never added */
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);
237 if (src->fd != src->new_fd) {
238 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
240 src->fd = src->new_fd;
242 GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
246 src->uri = g_strdup_printf ("fd://%d", src->fd);
248 if (fstat (src->fd, &stat_results) < 0)
251 if (!S_ISREG (stat_results.st_mode))
254 /* Try a seek of 0 bytes offset to check for seekability */
255 if (lseek (src->fd, 0, SEEK_CUR) < 0)
258 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
259 src->seekable_fd = TRUE;
261 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), TRUE);
267 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
268 src->seekable_fd = FALSE;
269 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), FALSE);
274 gst_fd_src_start (GstBaseSrc * bsrc)
276 GstFdSrc *src = GST_FD_SRC (bsrc);
280 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
283 gst_fd_src_update_fd (src, -1);
290 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
297 gst_fd_src_stop (GstBaseSrc * bsrc)
299 GstFdSrc *src = GST_FD_SRC (bsrc);
302 gst_poll_free (src->fdset);
310 gst_fd_src_unlock (GstBaseSrc * bsrc)
312 GstFdSrc *src = GST_FD_SRC (bsrc);
314 GST_LOG_OBJECT (src, "Flushing");
315 GST_OBJECT_LOCK (src);
316 gst_poll_set_flushing (src->fdset, TRUE);
317 GST_OBJECT_UNLOCK (src);
323 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
325 GstFdSrc *src = GST_FD_SRC (bsrc);
327 GST_LOG_OBJECT (src, "No longer flushing");
328 GST_OBJECT_LOCK (src);
329 gst_poll_set_flushing (src->fdset, FALSE);
330 GST_OBJECT_UNLOCK (src);
336 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
339 GstFdSrc *src = GST_FD_SRC (object);
343 src->new_fd = g_value_get_int (value);
345 /* If state is ready or below, update the current fd immediately
346 * so it is reflected in get_properties and uri */
347 GST_OBJECT_LOCK (object);
348 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
349 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
350 gst_fd_src_update_fd (src, -1);
352 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
354 GST_OBJECT_UNLOCK (object);
357 src->timeout = g_value_get_uint64 (value);
358 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
359 GST_TIME_ARGS (src->timeout));
362 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
371 GstFdSrc *src = GST_FD_SRC (object);
375 g_value_set_int (value, src->fd);
378 g_value_set_uint64 (value, src->timeout);
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
396 GstClockTime timeout;
401 src = GST_FD_SRC (psrc);
404 if (src->timeout > 0) {
405 timeout = src->timeout * GST_USECOND;
407 timeout = GST_CLOCK_TIME_NONE;
413 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
414 GST_TIME_ARGS (src->timeout));
416 retval = gst_poll_wait (src->fdset, timeout);
417 GST_LOG_OBJECT (src, "poll returned %d", retval);
419 if (G_UNLIKELY (retval == -1)) {
420 if (errno == EINTR || errno == EAGAIN) {
421 /* retry if interrupted */
423 } else if (errno == EBUSY) {
428 } else if (G_UNLIKELY (retval == 0)) {
430 /* timeout, post element message */
431 gst_element_post_message (GST_ELEMENT_CAST (src),
432 gst_message_new_element (GST_OBJECT_CAST (src),
433 gst_structure_new ("GstFdSrcTimeout",
434 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
436 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
439 blocksize = GST_BASE_SRC (src)->blocksize;
441 /* create the buffer */
442 buf = gst_buffer_new_allocate (NULL, blocksize, NULL);
443 if (G_UNLIKELY (buf == NULL))
446 gst_buffer_map (buf, &info, GST_MAP_WRITE);
449 readbytes = read (src->fd, info.data, blocksize);
450 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
451 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
456 gst_buffer_unmap (buf, &info);
457 gst_buffer_resize (buf, 0, readbytes);
462 GST_BUFFER_OFFSET (buf) = src->curoffset;
463 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
464 src->curoffset += readbytes;
466 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
468 /* we're done, return the buffer */
477 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
478 ("poll on file descriptor: %s.", g_strerror (errno)));
479 GST_DEBUG_OBJECT (psrc, "Error during poll");
480 return GST_FLOW_ERROR;
484 GST_DEBUG_OBJECT (psrc, "Poll stopped");
485 return GST_FLOW_FLUSHING;
490 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
491 return GST_FLOW_ERROR;
495 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
496 gst_buffer_unref (buf);
501 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
502 ("read on file descriptor: %s.", g_strerror (errno)));
503 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
504 gst_buffer_unmap (buf, &info);
505 gst_buffer_unref (buf);
506 return GST_FLOW_ERROR;
511 gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
513 gboolean ret = FALSE;
514 GstFdSrc *src = GST_FD_SRC (basesrc);
516 switch (GST_QUERY_TYPE (query)) {
518 gst_query_set_uri (query, src->uri);
527 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
533 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
535 GstFdSrc *src = GST_FD_SRC (bsrc);
537 return src->seekable_fd;
541 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
543 GstFdSrc *src = GST_FD_SRC (bsrc);
544 struct stat stat_results;
546 if (src->size != -1) {
551 if (!src->seekable_fd) {
552 /* If it isn't seekable, we won't know the length (but fstat will still
553 * succeed, and wrongly say our length is zero. */
557 if (fstat (src->fd, &stat_results) < 0)
560 *size = stat_results.st_size;
572 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
576 GstFdSrc *src = GST_FD_SRC (bsrc);
578 offset = segment->start;
580 /* No need to seek to the current position */
581 if (offset == src->curoffset)
584 res = lseek (src->fd, offset, SEEK_SET);
585 if (G_UNLIKELY (res < 0 || res != offset))
588 segment->position = segment->start;
589 segment->time = segment->start;
594 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
598 /*** GSTURIHANDLER INTERFACE *************************************************/
601 gst_fd_src_uri_get_type (GType type)
606 static const gchar *const *
607 gst_fd_src_uri_get_protocols (GType type)
609 static const gchar *protocols[] = { "fd", NULL };
615 gst_fd_src_uri_get_uri (GstURIHandler * handler)
617 GstFdSrc *src = GST_FD_SRC (handler);
619 /* FIXME: make thread-safe */
620 return g_strdup (src->uri);
624 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
628 GstFdSrc *src = GST_FD_SRC (handler);
630 guint64 size = (guint64) - 1;
632 GST_INFO_OBJECT (src, "checking uri %s", uri);
634 protocol = gst_uri_get_protocol (uri);
635 if (strcmp (protocol, "fd") != 0) {
641 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
644 if ((q = g_strstr_len (uri, -1, "?"))) {
647 GST_INFO_OBJECT (src, "found ?");
649 if ((sp = g_strstr_len (q, -1, "size="))) {
650 if (sscanf (sp, "size=%" G_GUINT64_FORMAT, &size) != 1) {
651 GST_INFO_OBJECT (src, "parsing size failed");
654 GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
661 GST_OBJECT_LOCK (src);
662 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
663 gst_fd_src_update_fd (src, size);
665 GST_OBJECT_UNLOCK (src);
671 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
673 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
675 iface->get_type = gst_fd_src_uri_get_type;
676 iface->get_protocols = gst_fd_src_uri_get_protocols;
677 iface->get_uri = gst_fd_src_uri_get_uri;
678 iface->set_uri = gst_fd_src_uri_set_uri;