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>
65 #include <sys/socket.h>
79 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
84 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
85 #define GST_CAT_DEFAULT gst_fd_src_debug
88 #define DEFAULT_TIMEOUT 0
100 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
103 _do_init (GType fd_src_type)
105 static const GInterfaceInfo urihandler_info = {
106 gst_fd_src_uri_handler_init,
111 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
114 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
117 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
120 static void gst_fd_src_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_fd_src_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
124 static void gst_fd_src_dispose (GObject * obj);
126 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
127 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
128 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
129 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
130 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
131 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
132 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
134 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
137 gst_fd_src_base_init (gpointer g_class)
139 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
141 gst_element_class_set_details_simple (gstelement_class,
142 "Filedescriptor Source",
144 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
145 gst_element_class_add_pad_template (gstelement_class,
146 gst_static_pad_template_get (&srctemplate));
150 gst_fd_src_class_init (GstFdSrcClass * klass)
152 GObjectClass *gobject_class;
153 GstBaseSrcClass *gstbasesrc_class;
154 GstElementClass *gstelement_class;
155 GstPushSrcClass *gstpush_src_class;
157 gobject_class = G_OBJECT_CLASS (klass);
158 gstelement_class = GST_ELEMENT_CLASS (klass);
159 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
160 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
162 parent_class = g_type_class_peek_parent (klass);
164 gobject_class->set_property = gst_fd_src_set_property;
165 gobject_class->get_property = gst_fd_src_get_property;
166 gobject_class->dispose = gst_fd_src_dispose;
168 g_object_class_install_property (gobject_class, PROP_FD,
169 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
170 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 * Post a message after timeout microseconds
178 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
179 g_param_spec_uint64 ("timeout", "Timeout",
180 "Post a message after timeout microseconds (0 = disabled)", 0,
181 G_MAXUINT64, DEFAULT_TIMEOUT,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
184 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
185 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
186 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
187 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
188 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
189 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
190 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
192 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
196 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
199 fdsrc->seekable_fd = FALSE;
200 fdsrc->fd = DEFAULT_FD;
201 fdsrc->timeout = DEFAULT_TIMEOUT;
202 fdsrc->uri = g_strdup_printf ("fd://0");
203 fdsrc->curoffset = 0;
207 gst_fd_src_dispose (GObject * obj)
209 GstFdSrc *src = GST_FD_SRC (obj);
214 G_OBJECT_CLASS (parent_class)->dispose (obj);
218 gst_fd_src_update_fd (GstFdSrc * src)
220 struct stat stat_results;
222 /* we need to always update the fdset since it may not have existed when
223 * gst_fd_src_update_fd () was called earlier */
224 if (src->fdset != NULL) {
225 GstPollFD fd = GST_POLL_FD_INIT;
229 gst_poll_remove_fd (src->fdset, &fd);
233 gst_poll_add_fd (src->fdset, &fd);
234 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;
243 src->uri = g_strdup_printf ("fd://%d", src->fd);
245 if (fstat (src->fd, &stat_results) < 0)
248 if (!S_ISREG (stat_results.st_mode))
251 /* Try a seek of 0 bytes offset to check for seekability */
252 if (lseek (src->fd, 0, SEEK_CUR) < 0)
255 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
256 src->seekable_fd = TRUE;
262 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
263 src->seekable_fd = FALSE;
268 gst_fd_src_start (GstBaseSrc * bsrc)
270 GstFdSrc *src = GST_FD_SRC (bsrc);
274 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
277 gst_fd_src_update_fd (src);
284 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
291 gst_fd_src_stop (GstBaseSrc * bsrc)
293 GstFdSrc *src = GST_FD_SRC (bsrc);
296 gst_poll_free (src->fdset);
304 gst_fd_src_unlock (GstBaseSrc * bsrc)
306 GstFdSrc *src = GST_FD_SRC (bsrc);
308 GST_LOG_OBJECT (src, "Flushing");
309 GST_OBJECT_LOCK (src);
310 gst_poll_set_flushing (src->fdset, TRUE);
311 GST_OBJECT_UNLOCK (src);
317 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
319 GstFdSrc *src = GST_FD_SRC (bsrc);
321 GST_LOG_OBJECT (src, "No longer flushing");
322 GST_OBJECT_LOCK (src);
323 gst_poll_set_flushing (src->fdset, FALSE);
324 GST_OBJECT_UNLOCK (src);
330 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
333 GstFdSrc *src = GST_FD_SRC (object);
337 src->new_fd = g_value_get_int (value);
339 /* If state is ready or below, update the current fd immediately
340 * so it is reflected in get_properties and uri */
341 GST_OBJECT_LOCK (object);
342 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
343 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
344 gst_fd_src_update_fd (src);
346 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
348 GST_OBJECT_UNLOCK (object);
351 src->timeout = g_value_get_uint64 (value);
352 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
353 GST_TIME_ARGS (src->timeout));
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
362 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
365 GstFdSrc *src = GST_FD_SRC (object);
369 g_value_set_int (value, src->fd);
372 g_value_set_uint64 (value, src->timeout);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
387 GstClockTime timeout;
394 src = GST_FD_SRC (psrc);
396 if (src->timeout > 0) {
397 timeout = src->timeout * GST_USECOND;
399 timeout = GST_CLOCK_TIME_NONE;
406 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
407 GST_TIME_ARGS (src->timeout));
409 retval = gst_poll_wait (src->fdset, timeout);
410 GST_LOG_OBJECT (src, "poll returned %d", retval);
412 if (G_UNLIKELY (retval == -1)) {
413 if (errno == EINTR || errno == EAGAIN) {
414 /* retry if interrupted */
416 } else if (errno == EBUSY) {
421 } else if (G_UNLIKELY (retval == 0)) {
423 /* timeout, post element message */
424 gst_element_post_message (GST_ELEMENT_CAST (src),
425 gst_message_new_element (GST_OBJECT_CAST (src),
426 gst_structure_new ("GstFdSrcTimeout",
427 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
429 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
432 blocksize = GST_BASE_SRC (src)->blocksize;
434 /* create the buffer */
435 buf = gst_buffer_try_new_and_alloc (blocksize);
436 if (G_UNLIKELY (buf == NULL)) {
437 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
438 return GST_FLOW_ERROR;
442 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
443 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
444 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
452 GST_BUFFER_OFFSET (buf) = src->curoffset;
453 GST_BUFFER_SIZE (buf) = readbytes;
454 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
455 src->curoffset += readbytes;
457 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
459 /* we're done, return the buffer */
468 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
469 ("poll on file descriptor: %s.", g_strerror (errno)));
470 GST_DEBUG_OBJECT (psrc, "Error during poll");
471 return GST_FLOW_ERROR;
475 GST_DEBUG_OBJECT (psrc, "Poll stopped");
476 return GST_FLOW_WRONG_STATE;
481 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
482 gst_buffer_unref (buf);
483 return GST_FLOW_UNEXPECTED;
487 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
488 ("read on file descriptor: %s.", g_strerror (errno)));
489 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
490 gst_buffer_unref (buf);
491 return GST_FLOW_ERROR;
496 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
498 GstFdSrc *src = GST_FD_SRC (bsrc);
500 return src->seekable_fd;
504 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
506 GstFdSrc *src = GST_FD_SRC (bsrc);
507 struct stat stat_results;
509 if (!src->seekable_fd) {
510 /* If it isn't seekable, we won't know the length (but fstat will still
511 * succeed, and wrongly say our length is zero. */
515 if (fstat (src->fd, &stat_results) < 0)
518 *size = stat_results.st_size;
530 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
534 GstFdSrc *src = GST_FD_SRC (bsrc);
536 offset = segment->start;
538 /* No need to seek to the current position */
539 if (offset == src->curoffset)
542 res = lseek (src->fd, offset, SEEK_SET);
543 if (G_UNLIKELY (res < 0 || res != offset))
546 segment->last_stop = segment->start;
547 segment->time = segment->start;
552 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
556 /*** GSTURIHANDLER INTERFACE *************************************************/
559 gst_fd_src_uri_get_type (void)
565 gst_fd_src_uri_get_protocols (void)
567 static gchar *protocols[] = { "fd", NULL };
573 gst_fd_src_uri_get_uri (GstURIHandler * handler)
575 GstFdSrc *src = GST_FD_SRC (handler);
581 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
584 GstFdSrc *src = GST_FD_SRC (handler);
587 protocol = gst_uri_get_protocol (uri);
588 if (strcmp (protocol, "fd") != 0) {
594 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
599 GST_OBJECT_LOCK (src);
600 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
601 gst_fd_src_update_fd (src);
603 GST_OBJECT_UNLOCK (src);
609 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
611 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
613 iface->get_type = gst_fd_src_uri_get_type;
614 iface->get_protocols = gst_fd_src_uri_get_protocols;
615 iface->get_uri = gst_fd_src_uri_get_uri;
616 iface->set_uri = gst_fd_src_uri_set_uri;