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 * @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 _do_init (GType fd_src_type)
119 static const GInterfaceInfo urihandler_info = {
120 gst_fd_src_uri_handler_init,
125 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
128 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
131 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
134 static void gst_fd_src_set_property (GObject * object, guint prop_id,
135 const GValue * value, GParamSpec * pspec);
136 static void gst_fd_src_get_property (GObject * object, guint prop_id,
137 GValue * value, GParamSpec * pspec);
138 static void gst_fd_src_dispose (GObject * obj);
140 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
141 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
142 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
143 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
144 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
145 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
146 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
147 static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
149 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
152 gst_fd_src_base_init (gpointer g_class)
154 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
156 gst_element_class_set_details_simple (gstelement_class,
157 "Filedescriptor Source",
159 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
160 gst_element_class_add_pad_template (gstelement_class,
161 gst_static_pad_template_get (&srctemplate));
165 gst_fd_src_class_init (GstFdSrcClass * klass)
167 GObjectClass *gobject_class;
168 GstBaseSrcClass *gstbasesrc_class;
169 GstPushSrcClass *gstpush_src_class;
171 gobject_class = G_OBJECT_CLASS (klass);
172 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
173 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
175 parent_class = g_type_class_peek_parent (klass);
177 gobject_class->set_property = gst_fd_src_set_property;
178 gobject_class->get_property = gst_fd_src_get_property;
179 gobject_class->dispose = gst_fd_src_dispose;
181 g_object_class_install_property (gobject_class, PROP_FD,
182 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
183 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 * Post a message after timeout microseconds
191 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
192 g_param_spec_uint64 ("timeout", "Timeout",
193 "Post a message after timeout microseconds (0 = disabled)", 0,
194 G_MAXUINT64, DEFAULT_TIMEOUT,
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
198 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
199 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
200 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
201 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
202 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
203 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
204 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
206 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
210 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
212 fdsrc->new_fd = DEFAULT_FD;
213 fdsrc->seekable_fd = FALSE;
216 fdsrc->timeout = DEFAULT_TIMEOUT;
217 fdsrc->uri = g_strdup_printf ("fd://0");
218 fdsrc->curoffset = 0;
222 gst_fd_src_dispose (GObject * obj)
224 GstFdSrc *src = GST_FD_SRC (obj);
229 G_OBJECT_CLASS (parent_class)->dispose (obj);
233 gst_fd_src_update_fd (GstFdSrc * src, guint64 size)
235 struct stat stat_results;
237 GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
240 /* we need to always update the fdset since it may not have existed when
241 * gst_fd_src_update_fd () was called earlier */
242 if (src->fdset != NULL) {
243 GstPollFD fd = GST_POLL_FD_INIT;
247 /* this will log a harmless warning, if it was never added */
248 gst_poll_remove_fd (src->fdset, &fd);
252 gst_poll_add_fd (src->fdset, &fd);
253 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
257 if (src->fd != src->new_fd) {
258 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
260 src->fd = src->new_fd;
262 GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
266 src->uri = g_strdup_printf ("fd://%d", src->fd);
268 if (fstat (src->fd, &stat_results) < 0)
271 if (!S_ISREG (stat_results.st_mode))
274 /* Try a seek of 0 bytes offset to check for seekability */
275 if (lseek (src->fd, 0, SEEK_CUR) < 0)
278 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
279 src->seekable_fd = TRUE;
285 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
286 src->seekable_fd = FALSE;
291 gst_fd_src_start (GstBaseSrc * bsrc)
293 GstFdSrc *src = GST_FD_SRC (bsrc);
297 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
300 gst_fd_src_update_fd (src, -1);
307 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
314 gst_fd_src_stop (GstBaseSrc * bsrc)
316 GstFdSrc *src = GST_FD_SRC (bsrc);
319 gst_poll_free (src->fdset);
327 gst_fd_src_unlock (GstBaseSrc * bsrc)
329 GstFdSrc *src = GST_FD_SRC (bsrc);
331 GST_LOG_OBJECT (src, "Flushing");
332 GST_OBJECT_LOCK (src);
333 gst_poll_set_flushing (src->fdset, TRUE);
334 GST_OBJECT_UNLOCK (src);
340 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
342 GstFdSrc *src = GST_FD_SRC (bsrc);
344 GST_LOG_OBJECT (src, "No longer flushing");
345 GST_OBJECT_LOCK (src);
346 gst_poll_set_flushing (src->fdset, FALSE);
347 GST_OBJECT_UNLOCK (src);
353 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
356 GstFdSrc *src = GST_FD_SRC (object);
360 src->new_fd = g_value_get_int (value);
362 /* If state is ready or below, update the current fd immediately
363 * so it is reflected in get_properties and uri */
364 GST_OBJECT_LOCK (object);
365 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
366 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
367 gst_fd_src_update_fd (src, -1);
369 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
371 GST_OBJECT_UNLOCK (object);
374 src->timeout = g_value_get_uint64 (value);
375 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
376 GST_TIME_ARGS (src->timeout));
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
388 GstFdSrc *src = GST_FD_SRC (object);
392 g_value_set_int (value, src->fd);
395 g_value_set_uint64 (value, src->timeout);
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
410 GstClockTime timeout;
417 src = GST_FD_SRC (psrc);
419 if (src->timeout > 0) {
420 timeout = src->timeout * GST_USECOND;
422 timeout = GST_CLOCK_TIME_NONE;
429 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
430 GST_TIME_ARGS (src->timeout));
432 retval = gst_poll_wait (src->fdset, timeout);
433 GST_LOG_OBJECT (src, "poll returned %d", retval);
435 if (G_UNLIKELY (retval == -1)) {
436 if (errno == EINTR || errno == EAGAIN) {
437 /* retry if interrupted */
439 } else if (errno == EBUSY) {
444 } else if (G_UNLIKELY (retval == 0)) {
446 /* timeout, post element message */
447 gst_element_post_message (GST_ELEMENT_CAST (src),
448 gst_message_new_element (GST_OBJECT_CAST (src),
449 gst_structure_new ("GstFdSrcTimeout",
450 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
452 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
455 blocksize = GST_BASE_SRC (src)->blocksize;
457 /* create the buffer */
458 buf = gst_buffer_try_new_and_alloc (blocksize);
459 if (G_UNLIKELY (buf == NULL)) {
460 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
461 return GST_FLOW_ERROR;
465 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
466 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
467 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
475 GST_BUFFER_OFFSET (buf) = src->curoffset;
476 GST_BUFFER_SIZE (buf) = readbytes;
477 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
478 src->curoffset += readbytes;
480 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
482 /* we're done, return the buffer */
491 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
492 ("poll on file descriptor: %s.", g_strerror (errno)));
493 GST_DEBUG_OBJECT (psrc, "Error during poll");
494 return GST_FLOW_ERROR;
498 GST_DEBUG_OBJECT (psrc, "Poll stopped");
499 return GST_FLOW_WRONG_STATE;
504 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
505 gst_buffer_unref (buf);
506 return GST_FLOW_UNEXPECTED;
510 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
511 ("read on file descriptor: %s.", g_strerror (errno)));
512 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
513 gst_buffer_unref (buf);
514 return GST_FLOW_ERROR;
519 gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
521 gboolean ret = FALSE;
522 GstFdSrc *src = GST_FD_SRC (basesrc);
524 switch (GST_QUERY_TYPE (query)) {
526 gst_query_set_uri (query, src->uri);
535 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
541 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
543 GstFdSrc *src = GST_FD_SRC (bsrc);
545 return src->seekable_fd;
549 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
551 GstFdSrc *src = GST_FD_SRC (bsrc);
552 struct stat stat_results;
554 if (src->size != -1) {
559 if (!src->seekable_fd) {
560 /* If it isn't seekable, we won't know the length (but fstat will still
561 * succeed, and wrongly say our length is zero. */
565 if (fstat (src->fd, &stat_results) < 0)
568 *size = stat_results.st_size;
580 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
584 GstFdSrc *src = GST_FD_SRC (bsrc);
586 offset = segment->start;
588 /* No need to seek to the current position */
589 if (offset == src->curoffset)
592 res = lseek (src->fd, offset, SEEK_SET);
593 if (G_UNLIKELY (res < 0 || res != offset))
596 segment->last_stop = segment->start;
597 segment->time = segment->start;
602 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
606 /*** GSTURIHANDLER INTERFACE *************************************************/
609 gst_fd_src_uri_get_type (void)
615 gst_fd_src_uri_get_protocols (void)
617 static gchar *protocols[] = { (char *) "fd", NULL };
623 gst_fd_src_uri_get_uri (GstURIHandler * handler)
625 GstFdSrc *src = GST_FD_SRC (handler);
631 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
634 GstFdSrc *src = GST_FD_SRC (handler);
638 GST_INFO_OBJECT (src, "checking uri %s", uri);
640 protocol = gst_uri_get_protocol (uri);
641 if (strcmp (protocol, "fd") != 0) {
647 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
650 if ((q = g_strstr_len (uri, -1, "?"))) {
653 GST_INFO_OBJECT (src, "found ?");
655 if ((sp = g_strstr_len (q, -1, "size="))) {
656 if (sscanf (sp, "size=%" G_GUINT64_FORMAT, &size) != 1) {
657 GST_INFO_OBJECT (src, "parsing size failed");
660 GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
667 GST_OBJECT_LOCK (src);
668 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
669 gst_fd_src_update_fd (src, size);
671 GST_OBJECT_UNLOCK (src);
677 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
679 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
681 iface->get_type = gst_fd_src_uri_get_type;
682 iface->get_protocols = gst_fd_src_uri_get_protocols;
683 iface->get_uri = gst_fd_src_uri_get_uri;
684 iface->set_uri = gst_fd_src_uri_set_uri;