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));
180 * Post a message after timeout microseconds
184 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
185 g_param_spec_uint64 ("timeout", "Timeout",
186 "Post a message after timeout microseconds (0 = disabled)", 0,
187 G_MAXUINT64, DEFAULT_TIMEOUT,
188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
191 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
192 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
193 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
194 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
195 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
196 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
198 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
202 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
205 fdsrc->seekable_fd = FALSE;
206 fdsrc->fd = DEFAULT_FD;
207 fdsrc->timeout = DEFAULT_TIMEOUT;
208 fdsrc->uri = g_strdup_printf ("fd://0");
209 fdsrc->curoffset = 0;
213 gst_fd_src_dispose (GObject * obj)
215 GstFdSrc *src = GST_FD_SRC (obj);
220 G_OBJECT_CLASS (parent_class)->dispose (obj);
224 gst_fd_src_update_fd (GstFdSrc * src)
226 struct stat stat_results;
228 /* we need to always update the fdset since it may not have existed when
229 * gst_fd_src_update_fd () was called earlier */
230 if (src->fdset != NULL) {
231 GstPollFD fd = GST_POLL_FD_INIT;
235 gst_poll_remove_fd (src->fdset, &fd);
239 gst_poll_add_fd (src->fdset, &fd);
240 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
243 if (src->fd != src->new_fd) {
244 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
246 src->fd = src->new_fd;
249 src->uri = g_strdup_printf ("fd://%d", src->fd);
251 if (fstat (src->fd, &stat_results) < 0)
254 if (!S_ISREG (stat_results.st_mode))
257 /* Try a seek of 0 bytes offset to check for seekability */
258 if (lseek (src->fd, 0, SEEK_CUR) < 0)
261 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
262 src->seekable_fd = TRUE;
268 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
269 src->seekable_fd = 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);
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);
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)
393 GstClockTime timeout;
400 src = GST_FD_SRC (psrc);
402 if (src->timeout > 0) {
403 timeout = src->timeout * GST_USECOND;
405 timeout = GST_CLOCK_TIME_NONE;
412 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
413 GST_TIME_ARGS (src->timeout));
415 retval = gst_poll_wait (src->fdset, timeout);
416 GST_LOG_OBJECT (src, "poll returned %d", retval);
418 if (G_UNLIKELY (retval == -1)) {
419 if (errno == EINTR || errno == EAGAIN) {
420 /* retry if interrupted */
422 } else if (errno == EBUSY) {
427 } else if (G_UNLIKELY (retval == 0)) {
429 /* timeout, post element message */
430 gst_element_post_message (GST_ELEMENT_CAST (src),
431 gst_message_new_element (GST_OBJECT_CAST (src),
432 gst_structure_new ("GstFdSrcTimeout",
433 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
435 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
438 blocksize = GST_BASE_SRC (src)->blocksize;
440 /* create the buffer */
441 buf = gst_buffer_new_and_alloc (blocksize);
444 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
445 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
446 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
454 GST_BUFFER_OFFSET (buf) = src->curoffset;
455 GST_BUFFER_SIZE (buf) = readbytes;
456 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
457 src->curoffset += readbytes;
459 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
461 /* we're done, return the buffer */
470 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
471 ("poll on file descriptor: %s.", g_strerror (errno)));
472 GST_DEBUG_OBJECT (psrc, "Error during poll");
473 return GST_FLOW_ERROR;
477 GST_DEBUG_OBJECT (psrc, "Poll stopped");
478 return GST_FLOW_WRONG_STATE;
483 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
484 gst_buffer_unref (buf);
485 return GST_FLOW_UNEXPECTED;
489 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
490 ("read on file descriptor: %s.", g_strerror (errno)));
491 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
492 gst_buffer_unref (buf);
493 return GST_FLOW_ERROR;
498 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
500 GstFdSrc *src = GST_FD_SRC (bsrc);
502 return src->seekable_fd;
506 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
508 GstFdSrc *src = GST_FD_SRC (bsrc);
509 struct stat stat_results;
511 if (!src->seekable_fd) {
512 /* If it isn't seekable, we won't know the length (but fstat will still
513 * succeed, and wrongly say our length is zero. */
517 if (fstat (src->fd, &stat_results) < 0)
520 *size = stat_results.st_size;
532 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
536 GstFdSrc *src = GST_FD_SRC (bsrc);
538 offset = segment->start;
540 /* No need to seek to the current position */
541 if (offset == src->curoffset)
544 res = lseek (src->fd, offset, SEEK_SET);
545 if (G_UNLIKELY (res < 0 || res != offset))
548 segment->last_stop = segment->start;
549 segment->time = segment->start;
554 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
558 /*** GSTURIHANDLER INTERFACE *************************************************/
561 gst_fd_src_uri_get_type (void)
566 gst_fd_src_uri_get_protocols (void)
568 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;