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
28 * Read data from a unix file descriptor.
35 #include "gst/gst_private.h"
37 #include <sys/types.h>
39 #include <sys/socket.h>
53 #define DEFAULT_BLOCKSIZE 4096
55 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
60 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
61 #define GST_CAT_DEFAULT gst_fd_src_debug
69 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
72 _do_init (GType fd_src_type)
74 static const GInterfaceInfo urihandler_info = {
75 gst_fd_src_uri_handler_init,
80 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
83 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
86 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
89 static void gst_fd_src_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_fd_src_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
93 static void gst_fd_src_dispose (GObject * obj);
95 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
96 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
97 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
98 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
99 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
100 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
101 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
103 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
106 gst_fd_src_base_init (gpointer g_class)
108 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
110 gst_element_class_set_details_simple (gstelement_class,
111 "Filedescriptor Source",
113 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
114 gst_element_class_add_pad_template (gstelement_class,
115 gst_static_pad_template_get (&srctemplate));
119 gst_fd_src_class_init (GstFdSrcClass * klass)
121 GObjectClass *gobject_class;
122 GstBaseSrcClass *gstbasesrc_class;
123 GstElementClass *gstelement_class;
124 GstPushSrcClass *gstpush_src_class;
126 gobject_class = G_OBJECT_CLASS (klass);
127 gstelement_class = GST_ELEMENT_CLASS (klass);
128 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
129 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
131 parent_class = g_type_class_peek_parent (klass);
133 gobject_class->set_property = gst_fd_src_set_property;
134 gobject_class->get_property = gst_fd_src_get_property;
135 gobject_class->dispose = gst_fd_src_dispose;
137 g_object_class_install_property (gobject_class, PROP_FD,
138 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
139 0, G_MAXINT, 0, G_PARAM_READWRITE));
141 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
142 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
143 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
144 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
145 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
146 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
147 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
149 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
153 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
157 fdsrc->seekable_fd = FALSE;
158 fdsrc->uri = g_strdup_printf ("fd://0");
159 fdsrc->curoffset = 0;
163 gst_fd_src_dispose (GObject * obj)
165 GstFdSrc *src = GST_FD_SRC (obj);
170 G_OBJECT_CLASS (parent_class)->dispose (obj);
174 gst_fd_src_update_fd (GstFdSrc * src)
176 struct stat stat_results;
178 /* we need to always update the fdset since it may not have existed when
179 * gst_fd_src_update_fd() was called earlier */
180 if (src->fdset != NULL) {
181 GstPollFD fd = GST_POLL_FD_INIT;
185 gst_poll_remove_fd (src->fdset, &fd);
188 /* Reset the GstPollFD */
189 gst_poll_fd_init (&fd);
192 gst_poll_add_fd (src->fdset, &fd);
193 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
196 if (src->fd != src->new_fd) {
197 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
199 src->fd = src->new_fd;
202 src->uri = g_strdup_printf ("fd://%d", src->fd);
204 if (fstat (src->fd, &stat_results) < 0)
207 if (!S_ISREG (stat_results.st_mode))
210 /* Try a seek of 0 bytes offset to check for seekability */
211 if (lseek (src->fd, 0, SEEK_CUR) < 0)
214 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
215 src->seekable_fd = TRUE;
221 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
222 src->seekable_fd = FALSE;
227 gst_fd_src_start (GstBaseSrc * bsrc)
229 GstFdSrc *src = GST_FD_SRC (bsrc);
233 if ((src->fdset = gst_poll_new (GST_POLL_MODE_AUTO, TRUE)) == NULL)
236 gst_fd_src_update_fd (src);
243 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
250 gst_fd_src_stop (GstBaseSrc * bsrc)
252 GstFdSrc *src = GST_FD_SRC (bsrc);
255 gst_poll_free (src->fdset);
263 gst_fd_src_unlock (GstBaseSrc * bsrc)
265 GstFdSrc *src = GST_FD_SRC (bsrc);
267 GST_LOG_OBJECT (src, "Flushing");
268 GST_OBJECT_LOCK (src);
269 gst_poll_set_flushing (src->fdset, TRUE);
270 GST_OBJECT_UNLOCK (src);
276 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
278 GstFdSrc *src = GST_FD_SRC (bsrc);
280 GST_LOG_OBJECT (src, "No longer flushing");
281 GST_OBJECT_LOCK (src);
282 gst_poll_set_flushing (src->fdset, FALSE);
283 GST_OBJECT_UNLOCK (src);
289 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
292 GstFdSrc *src = GST_FD_SRC (object);
296 src->new_fd = g_value_get_int (value);
298 /* If state is ready or below, update the current fd immediately
299 * so it is reflected in get_properties and uri */
300 GST_OBJECT_LOCK (object);
301 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
302 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
303 gst_fd_src_update_fd (src);
305 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
307 GST_OBJECT_UNLOCK (object);
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
316 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
319 GstFdSrc *src = GST_FD_SRC (object);
323 g_value_set_int (value, src->fd);
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
343 src = GST_FD_SRC (psrc);
347 retval = gst_poll_wait (src->fdset, GST_CLOCK_TIME_NONE);
348 } while (retval == -1 && errno == EINTR);
358 blocksize = GST_BASE_SRC (src)->blocksize;
360 /* create the buffer */
361 buf = gst_buffer_new_and_alloc (blocksize);
364 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
365 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
366 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
374 GST_BUFFER_OFFSET (buf) = src->curoffset;
375 GST_BUFFER_SIZE (buf) = readbytes;
376 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
377 src->curoffset += readbytes;
379 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
381 /* we're done, return the buffer */
390 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
391 ("select on file descriptor: %s.", g_strerror (errno)));
392 GST_DEBUG_OBJECT (psrc, "Error during select");
393 return GST_FLOW_ERROR;
397 GST_DEBUG_OBJECT (psrc, "Select stopped");
398 return GST_FLOW_WRONG_STATE;
403 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
404 gst_buffer_unref (buf);
405 return GST_FLOW_UNEXPECTED;
409 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
410 ("read on file descriptor: %s.", g_strerror (errno)));
411 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
412 gst_buffer_unref (buf);
413 return GST_FLOW_ERROR;
418 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
420 GstFdSrc *src = GST_FD_SRC (bsrc);
422 return src->seekable_fd;
426 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
428 GstFdSrc *src = GST_FD_SRC (bsrc);
429 struct stat stat_results;
431 if (!src->seekable_fd) {
432 /* If it isn't seekable, we won't know the length (but fstat will still
433 * succeed, and wrongly say our length is zero. */
437 if (fstat (src->fd, &stat_results) < 0)
440 *size = stat_results.st_size;
452 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
456 GstFdSrc *src = GST_FD_SRC (bsrc);
458 offset = segment->start;
460 /* No need to seek to the current position */
461 if (offset == src->curoffset)
464 res = lseek (src->fd, offset, SEEK_SET);
465 if (G_UNLIKELY (res < 0 || res != offset))
468 segment->last_stop = segment->start;
469 segment->time = segment->start;
474 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
478 /*** GSTURIHANDLER INTERFACE *************************************************/
481 gst_fd_src_uri_get_type (void)
486 gst_fd_src_uri_get_protocols (void)
488 static gchar *protocols[] = { "fd", NULL };
493 gst_fd_src_uri_get_uri (GstURIHandler * handler)
495 GstFdSrc *src = GST_FD_SRC (handler);
501 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
504 GstFdSrc *src = GST_FD_SRC (handler);
507 protocol = gst_uri_get_protocol (uri);
508 if (strcmp (protocol, "fd") != 0) {
514 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
519 GST_OBJECT_LOCK (src);
520 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
521 gst_fd_src_update_fd (src);
523 GST_OBJECT_UNLOCK (src);
529 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
531 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
533 iface->get_type = gst_fd_src_uri_get_type;
534 iface->get_protocols = gst_fd_src_uri_get_protocols;
535 iface->get_uri = gst_fd_src_uri_get_uri;
536 iface->set_uri = gst_fd_src_uri_set_uri;