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 followed 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 <classname>"GstFdSrcTimeout"</classname>
34 * if no data was received 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-1.0 -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.
59 #include "gst/gst_private.h"
61 #include <sys/types.h>
64 #include <io.h> /* lseek, open, close, read */
66 #define lseek _lseeki64
72 #ifdef HAVE_SYS_SOCKET_H
73 #include <sys/socket.h>
84 #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
91 #ifdef __BIONIC__ /* Android */
98 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
101 GST_STATIC_CAPS_ANY);
103 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
104 #define GST_CAT_DEFAULT gst_fd_src_debug
107 #define DEFAULT_TIMEOUT 0
119 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
122 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_src_uri_handler_init); \
123 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
124 #define gst_fd_src_parent_class parent_class
125 G_DEFINE_TYPE_WITH_CODE (GstFdSrc, gst_fd_src, GST_TYPE_PUSH_SRC, _do_init);
127 static void gst_fd_src_set_property (GObject * object, guint prop_id,
128 const GValue * value, GParamSpec * pspec);
129 static void gst_fd_src_get_property (GObject * object, guint prop_id,
130 GValue * value, GParamSpec * pspec);
131 static void gst_fd_src_dispose (GObject * obj);
133 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
134 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
135 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
136 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
137 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
138 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
139 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
140 static gboolean gst_fd_src_query (GstBaseSrc * src, GstQuery * query);
142 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
145 gst_fd_src_class_init (GstFdSrcClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
149 GstBaseSrcClass *gstbasesrc_class;
150 GstPushSrcClass *gstpush_src_class;
152 gobject_class = G_OBJECT_CLASS (klass);
153 gstelement_class = GST_ELEMENT_CLASS (klass);
154 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
155 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
157 gobject_class->set_property = gst_fd_src_set_property;
158 gobject_class->get_property = gst_fd_src_get_property;
159 gobject_class->dispose = gst_fd_src_dispose;
161 g_object_class_install_property (gobject_class, PROP_FD,
162 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
163 0, G_MAXINT, DEFAULT_FD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 * Post a message after timeout microseconds
169 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TIMEOUT,
170 g_param_spec_uint64 ("timeout", "Timeout",
171 "Post a message after timeout microseconds (0 = disabled)", 0,
172 G_MAXUINT64, DEFAULT_TIMEOUT,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 gst_element_class_set_static_metadata (gstelement_class,
176 "Filedescriptor Source",
178 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
179 gst_element_class_add_pad_template (gstelement_class,
180 gst_static_pad_template_get (&srctemplate));
182 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
183 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
184 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
185 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
186 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
187 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
188 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
189 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_fd_src_query);
191 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
195 gst_fd_src_init (GstFdSrc * fdsrc)
197 fdsrc->new_fd = DEFAULT_FD;
198 fdsrc->seekable_fd = FALSE;
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, guint64 size)
220 struct stat stat_results;
222 GST_DEBUG_OBJECT (src, "fdset %p, old_fd %d, new_fd %d", src->fdset, src->fd,
225 /* we need to always update the fdset since it may not have existed when
226 * gst_fd_src_update_fd () was called earlier */
227 if (src->fdset != NULL) {
228 GstPollFD fd = GST_POLL_FD_INIT;
232 /* this will log a harmless warning, if it was never added */
233 gst_poll_remove_fd (src->fdset, &fd);
237 gst_poll_add_fd (src->fdset, &fd);
238 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
242 if (src->fd != src->new_fd) {
243 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
245 src->fd = src->new_fd;
247 GST_INFO_OBJECT (src, "Setting size to fd %" G_GUINT64_FORMAT, size);
251 src->uri = g_strdup_printf ("fd://%d", src->fd);
253 if (fstat (src->fd, &stat_results) < 0)
256 if (!S_ISREG (stat_results.st_mode))
259 /* Try a seek of 0 bytes offset to check for seekability */
260 if (lseek (src->fd, 0, SEEK_CUR) < 0)
263 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
264 src->seekable_fd = TRUE;
266 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), TRUE);
272 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
273 src->seekable_fd = FALSE;
274 gst_base_src_set_dynamic_size (GST_BASE_SRC (src), FALSE);
279 gst_fd_src_start (GstBaseSrc * bsrc)
281 GstFdSrc *src = GST_FD_SRC (bsrc);
285 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
288 gst_fd_src_update_fd (src, -1);
295 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
302 gst_fd_src_stop (GstBaseSrc * bsrc)
304 GstFdSrc *src = GST_FD_SRC (bsrc);
307 gst_poll_free (src->fdset);
315 gst_fd_src_unlock (GstBaseSrc * bsrc)
317 GstFdSrc *src = GST_FD_SRC (bsrc);
319 GST_LOG_OBJECT (src, "Flushing");
320 GST_OBJECT_LOCK (src);
321 gst_poll_set_flushing (src->fdset, TRUE);
322 GST_OBJECT_UNLOCK (src);
328 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
330 GstFdSrc *src = GST_FD_SRC (bsrc);
332 GST_LOG_OBJECT (src, "No longer flushing");
333 GST_OBJECT_LOCK (src);
334 gst_poll_set_flushing (src->fdset, FALSE);
335 GST_OBJECT_UNLOCK (src);
341 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
344 GstFdSrc *src = GST_FD_SRC (object);
348 src->new_fd = g_value_get_int (value);
350 /* If state is ready or below, update the current fd immediately
351 * so it is reflected in get_properties and uri */
352 GST_OBJECT_LOCK (object);
353 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
354 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
355 gst_fd_src_update_fd (src, -1);
357 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
359 GST_OBJECT_UNLOCK (object);
362 src->timeout = g_value_get_uint64 (value);
363 GST_DEBUG_OBJECT (src, "poll timeout set to %" GST_TIME_FORMAT,
364 GST_TIME_ARGS (src->timeout));
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
376 GstFdSrc *src = GST_FD_SRC (object);
380 g_value_set_int (value, src->fd);
383 g_value_set_uint64 (value, src->timeout);
386 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
392 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
401 GstClockTime timeout;
406 src = GST_FD_SRC (psrc);
409 if (src->timeout > 0) {
410 timeout = src->timeout * GST_USECOND;
412 timeout = GST_CLOCK_TIME_NONE;
418 GST_LOG_OBJECT (src, "doing poll, timeout %" GST_TIME_FORMAT,
419 GST_TIME_ARGS (src->timeout));
421 retval = gst_poll_wait (src->fdset, timeout);
422 GST_LOG_OBJECT (src, "poll returned %d", retval);
424 if (G_UNLIKELY (retval == -1)) {
425 if (errno == EINTR || errno == EAGAIN) {
426 /* retry if interrupted */
428 } else if (errno == EBUSY) {
433 } else if (G_UNLIKELY (retval == 0)) {
435 /* timeout, post element message */
436 gst_element_post_message (GST_ELEMENT_CAST (src),
437 gst_message_new_element (GST_OBJECT_CAST (src),
438 gst_structure_new ("GstFdSrcTimeout",
439 "timeout", G_TYPE_UINT64, src->timeout, NULL)));
441 } while (G_UNLIKELY (try_again)); /* retry if interrupted or timeout */
444 blocksize = GST_BASE_SRC (src)->blocksize;
446 /* create the buffer */
447 buf = gst_buffer_new_allocate (NULL, blocksize, NULL);
448 if (G_UNLIKELY (buf == NULL))
451 gst_buffer_map (buf, &info, GST_MAP_WRITE);
454 readbytes = read (src->fd, info.data, blocksize);
455 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
456 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
461 gst_buffer_unmap (buf, &info);
462 gst_buffer_resize (buf, 0, readbytes);
467 GST_BUFFER_OFFSET (buf) = src->curoffset;
468 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
469 src->curoffset += readbytes;
471 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
473 /* we're done, return the buffer */
482 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
483 ("poll on file descriptor: %s.", g_strerror (errno)));
484 GST_DEBUG_OBJECT (psrc, "Error during poll");
485 return GST_FLOW_ERROR;
489 GST_DEBUG_OBJECT (psrc, "Poll stopped");
490 return GST_FLOW_FLUSHING;
495 GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", blocksize);
496 return GST_FLOW_ERROR;
500 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
501 gst_buffer_unref (buf);
506 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
507 ("read on file descriptor: %s.", g_strerror (errno)));
508 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
509 gst_buffer_unmap (buf, &info);
510 gst_buffer_unref (buf);
511 return GST_FLOW_ERROR;
516 gst_fd_src_query (GstBaseSrc * basesrc, GstQuery * query)
518 gboolean ret = FALSE;
519 GstFdSrc *src = GST_FD_SRC (basesrc);
521 switch (GST_QUERY_TYPE (query)) {
523 gst_query_set_uri (query, src->uri);
532 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
538 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
540 GstFdSrc *src = GST_FD_SRC (bsrc);
542 return src->seekable_fd;
546 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
548 GstFdSrc *src = GST_FD_SRC (bsrc);
549 struct stat stat_results;
551 if (src->size != -1) {
556 if (!src->seekable_fd) {
557 /* If it isn't seekable, we won't know the length (but fstat will still
558 * succeed, and wrongly say our length is zero. */
562 if (fstat (src->fd, &stat_results) < 0)
565 *size = stat_results.st_size;
577 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
581 GstFdSrc *src = GST_FD_SRC (bsrc);
583 offset = segment->start;
585 /* No need to seek to the current position */
586 if (offset == src->curoffset)
589 res = lseek (src->fd, offset, SEEK_SET);
590 if (G_UNLIKELY (res < 0 || res != offset))
593 segment->position = segment->start;
594 segment->time = segment->start;
599 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
603 /*** GSTURIHANDLER INTERFACE *************************************************/
606 gst_fd_src_uri_get_type (GType type)
611 static const gchar *const *
612 gst_fd_src_uri_get_protocols (GType type)
614 static const gchar *protocols[] = { "fd", NULL };
620 gst_fd_src_uri_get_uri (GstURIHandler * handler)
622 GstFdSrc *src = GST_FD_SRC (handler);
624 /* FIXME: make thread-safe */
625 return g_strdup (src->uri);
629 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
633 GstFdSrc *src = GST_FD_SRC (handler);
635 guint64 size = (guint64) - 1;
637 GST_INFO_OBJECT (src, "checking uri %s", uri);
639 protocol = gst_uri_get_protocol (uri);
640 if (strcmp (protocol, "fd") != 0) {
641 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
642 "Wrong protocol for fdsrc in uri: '%s'", uri);
648 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0) {
649 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
650 "Bad file descriptor number in uri: '%s'", uri);
654 if ((q = g_strstr_len (uri, -1, "?"))) {
655 gchar *sp, *end = NULL;
657 GST_INFO_OBJECT (src, "found ?");
659 if ((sp = g_strstr_len (q, -1, "size="))) {
660 sp += strlen ("size=");
661 size = g_ascii_strtoull (sp, &end, 10);
662 if ((size == 0 && errno == EINVAL) || size == G_MAXUINT64 || end == sp) {
663 GST_INFO_OBJECT (src, "parsing size failed");
666 GST_INFO_OBJECT (src, "found size %" G_GUINT64_FORMAT, size);
673 GST_OBJECT_LOCK (src);
674 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
675 gst_fd_src_update_fd (src, size);
677 GST_OBJECT_UNLOCK (src);
683 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
685 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
687 iface->get_type = gst_fd_src_uri_get_type;
688 iface->get_protocols = gst_fd_src_uri_get_protocols;
689 iface->get_uri = gst_fd_src_uri_get_uri;
690 iface->set_uri = gst_fd_src_uri_set_uri;