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);
189 gst_poll_add_fd (src->fdset, &fd);
190 gst_poll_fd_ctl_read (src->fdset, &fd, TRUE);
193 if (src->fd != src->new_fd) {
194 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
196 src->fd = src->new_fd;
199 src->uri = g_strdup_printf ("fd://%d", src->fd);
201 if (fstat (src->fd, &stat_results) < 0)
204 if (!S_ISREG (stat_results.st_mode))
207 /* Try a seek of 0 bytes offset to check for seekability */
208 if (lseek (src->fd, 0, SEEK_CUR) < 0)
211 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
212 src->seekable_fd = TRUE;
218 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
219 src->seekable_fd = FALSE;
224 gst_fd_src_start (GstBaseSrc * bsrc)
226 GstFdSrc *src = GST_FD_SRC (bsrc);
230 if ((src->fdset = gst_poll_new (TRUE)) == NULL)
233 gst_fd_src_update_fd (src);
240 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
247 gst_fd_src_stop (GstBaseSrc * bsrc)
249 GstFdSrc *src = GST_FD_SRC (bsrc);
252 gst_poll_free (src->fdset);
260 gst_fd_src_unlock (GstBaseSrc * bsrc)
262 GstFdSrc *src = GST_FD_SRC (bsrc);
264 GST_LOG_OBJECT (src, "Flushing");
265 GST_OBJECT_LOCK (src);
266 gst_poll_set_flushing (src->fdset, TRUE);
267 GST_OBJECT_UNLOCK (src);
273 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
275 GstFdSrc *src = GST_FD_SRC (bsrc);
277 GST_LOG_OBJECT (src, "No longer flushing");
278 GST_OBJECT_LOCK (src);
279 gst_poll_set_flushing (src->fdset, FALSE);
280 GST_OBJECT_UNLOCK (src);
286 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
289 GstFdSrc *src = GST_FD_SRC (object);
293 src->new_fd = g_value_get_int (value);
295 /* If state is ready or below, update the current fd immediately
296 * so it is reflected in get_properties and uri */
297 GST_OBJECT_LOCK (object);
298 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
299 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
300 gst_fd_src_update_fd (src);
302 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
304 GST_OBJECT_UNLOCK (object);
307 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
316 GstFdSrc *src = GST_FD_SRC (object);
320 g_value_set_int (value, src->fd);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
340 src = GST_FD_SRC (psrc);
344 retval = gst_poll_wait (src->fdset, GST_CLOCK_TIME_NONE);
345 } while (retval == -1 && errno == EINTR);
355 blocksize = GST_BASE_SRC (src)->blocksize;
357 /* create the buffer */
358 buf = gst_buffer_new_and_alloc (blocksize);
361 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
362 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
363 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
371 GST_BUFFER_OFFSET (buf) = src->curoffset;
372 GST_BUFFER_SIZE (buf) = readbytes;
373 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
374 src->curoffset += readbytes;
376 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
378 /* we're done, return the buffer */
387 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
388 ("select on file descriptor: %s.", g_strerror (errno)));
389 GST_DEBUG_OBJECT (psrc, "Error during select");
390 return GST_FLOW_ERROR;
394 GST_DEBUG_OBJECT (psrc, "Select stopped");
395 return GST_FLOW_WRONG_STATE;
400 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
401 gst_buffer_unref (buf);
402 return GST_FLOW_UNEXPECTED;
406 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
407 ("read on file descriptor: %s.", g_strerror (errno)));
408 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
409 gst_buffer_unref (buf);
410 return GST_FLOW_ERROR;
415 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
417 GstFdSrc *src = GST_FD_SRC (bsrc);
419 return src->seekable_fd;
423 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
425 GstFdSrc *src = GST_FD_SRC (bsrc);
426 struct stat stat_results;
428 if (!src->seekable_fd) {
429 /* If it isn't seekable, we won't know the length (but fstat will still
430 * succeed, and wrongly say our length is zero. */
434 if (fstat (src->fd, &stat_results) < 0)
437 *size = stat_results.st_size;
449 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
453 GstFdSrc *src = GST_FD_SRC (bsrc);
455 offset = segment->start;
457 /* No need to seek to the current position */
458 if (offset == src->curoffset)
461 res = lseek (src->fd, offset, SEEK_SET);
462 if (G_UNLIKELY (res < 0 || res != offset))
465 segment->last_stop = segment->start;
466 segment->time = segment->start;
471 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
475 /*** GSTURIHANDLER INTERFACE *************************************************/
478 gst_fd_src_uri_get_type (void)
483 gst_fd_src_uri_get_protocols (void)
485 static gchar *protocols[] = { "fd", NULL };
490 gst_fd_src_uri_get_uri (GstURIHandler * handler)
492 GstFdSrc *src = GST_FD_SRC (handler);
498 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
501 GstFdSrc *src = GST_FD_SRC (handler);
504 protocol = gst_uri_get_protocol (uri);
505 if (strcmp (protocol, "fd") != 0) {
511 if (sscanf (uri, "fd://%d", &fd) != 1 || fd < 0)
516 GST_OBJECT_LOCK (src);
517 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
518 gst_fd_src_update_fd (src);
520 GST_OBJECT_UNLOCK (src);
526 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
528 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
530 iface->get_type = gst_fd_src_uri_get_type;
531 iface->get_protocols = gst_fd_src_uri_get_protocols;
532 iface->get_uri = gst_fd_src_uri_get_uri;
533 iface->set_uri = gst_fd_src_uri_set_uri;