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 /* the select call is also performed on the control sockets, that way
54 * we can send special commands to unblock the select call */
55 #define CONTROL_STOP 'S' /* stop the select call */
56 #define CONTROL_SOCKETS(src) src->control_sock
57 #define WRITE_SOCKET(src) src->control_sock[1]
58 #define READ_SOCKET(src) src->control_sock[0]
60 #define SEND_COMMAND(src, command) \
62 unsigned char c; c = command; \
63 write (WRITE_SOCKET(src), &c, 1); \
66 #define READ_COMMAND(src, command, res) \
68 res = read(READ_SOCKET(src), &command, 1); \
71 #define DEFAULT_BLOCKSIZE 4096
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
78 GST_DEBUG_CATEGORY_STATIC (gst_fd_src_debug);
79 #define GST_CAT_DEFAULT gst_fd_src_debug
87 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
90 _do_init (GType fd_src_type)
92 static const GInterfaceInfo urihandler_info = {
93 gst_fd_src_uri_handler_init,
98 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
101 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
104 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstPushSrc, GST_TYPE_PUSH_SRC,
107 static void gst_fd_src_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_fd_src_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
111 static void gst_fd_src_dispose (GObject * obj);
113 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
114 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
115 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
116 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
117 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
118 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
119 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
121 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
124 gst_fd_src_base_init (gpointer g_class)
126 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
128 gst_element_class_set_details_simple (gstelement_class,
129 "Filedescriptor Source",
131 "Read from a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
132 gst_element_class_add_pad_template (gstelement_class,
133 gst_static_pad_template_get (&srctemplate));
137 gst_fd_src_class_init (GstFdSrcClass * klass)
139 GObjectClass *gobject_class;
140 GstBaseSrcClass *gstbasesrc_class;
141 GstElementClass *gstelement_class;
142 GstPushSrcClass *gstpush_src_class;
144 gobject_class = G_OBJECT_CLASS (klass);
145 gstelement_class = GST_ELEMENT_CLASS (klass);
146 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
147 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
149 parent_class = g_type_class_peek_parent (klass);
151 gobject_class->set_property = gst_fd_src_set_property;
152 gobject_class->get_property = gst_fd_src_get_property;
153 gobject_class->dispose = gst_fd_src_dispose;
155 g_object_class_install_property (gobject_class, PROP_FD,
156 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
157 0, G_MAXINT, 0, G_PARAM_READWRITE));
159 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
160 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
161 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
162 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
163 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
164 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
165 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
167 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
171 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
175 fdsrc->seekable_fd = FALSE;
176 fdsrc->uri = g_strdup_printf ("fd://0");
177 fdsrc->curoffset = 0;
181 gst_fd_src_dispose (GObject * obj)
183 GstFdSrc *src = GST_FD_SRC (obj);
188 G_OBJECT_CLASS (parent_class)->dispose (obj);
192 gst_fd_src_update_fd (GstFdSrc * src)
194 struct stat stat_results;
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);
230 gint control_sock[2];
234 gst_fd_src_update_fd (src);
236 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
239 READ_SOCKET (src) = control_sock[0];
240 WRITE_SOCKET (src) = control_sock[1];
242 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
243 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
250 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
257 gst_fd_src_stop (GstBaseSrc * bsrc)
259 GstFdSrc *src = GST_FD_SRC (bsrc);
261 close (READ_SOCKET (src));
262 close (WRITE_SOCKET (src));
268 gst_fd_src_unlock (GstBaseSrc * bsrc)
270 GstFdSrc *src = GST_FD_SRC (bsrc);
272 GST_LOG_OBJECT (src, "sending unlock command");
273 SEND_COMMAND (src, CONTROL_STOP);
279 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
281 GstFdSrc *src = GST_FD_SRC (bsrc);
283 GST_LOG_OBJECT (src, "clearing unlock command queue");
285 /* read all stop commands */
290 GST_LOG_OBJECT (src, "reading command");
292 READ_COMMAND (src, command, res);
294 GST_LOG_OBJECT (src, "no more commands");
295 /* no more commands */
304 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
307 GstFdSrc *src = GST_FD_SRC (object);
311 src->new_fd = g_value_get_int (value);
313 /* If state is ready or below, update the current fd immediately
314 * so it is reflected in get_properties and uri */
315 GST_OBJECT_LOCK (object);
316 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
317 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
318 gst_fd_src_update_fd (src);
320 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
322 GST_OBJECT_UNLOCK (object);
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
334 GstFdSrc *src = GST_FD_SRC (object);
338 g_value_set_int (value, src->fd);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
359 src = GST_FD_SRC (psrc);
363 FD_SET (src->fd, &readfds);
364 FD_SET (READ_SOCKET (src), &readfds);
367 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
368 } while ((retval == -1 && errno == EINTR));
373 if (FD_ISSET (READ_SOCKET (src), &readfds))
377 blocksize = GST_BASE_SRC (src)->blocksize;
379 /* create the buffer */
380 buf = gst_buffer_new_and_alloc (blocksize);
383 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
384 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
385 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
393 GST_BUFFER_OFFSET (buf) = src->curoffset;
394 GST_BUFFER_SIZE (buf) = readbytes;
395 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
396 src->curoffset += readbytes;
398 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
400 /* we're done, return the buffer */
409 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
410 ("select on file descriptor: %s.", g_strerror (errno)));
411 GST_DEBUG_OBJECT (psrc, "Error during select");
412 return GST_FLOW_ERROR;
416 GST_DEBUG_OBJECT (psrc, "Select stopped");
417 return GST_FLOW_WRONG_STATE;
422 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
423 gst_buffer_unref (buf);
424 return GST_FLOW_UNEXPECTED;
428 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
429 ("read on file descriptor: %s.", g_strerror (errno)));
430 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
431 gst_buffer_unref (buf);
432 return GST_FLOW_ERROR;
437 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
439 GstFdSrc *src = GST_FD_SRC (bsrc);
441 return src->seekable_fd;
445 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
447 GstFdSrc *src = GST_FD_SRC (bsrc);
448 struct stat stat_results;
450 if (!src->seekable_fd) {
451 /* If it isn't seekable, we won't know the length (but fstat will still
452 * succeed, and wrongly say our length is zero. */
456 if (fstat (src->fd, &stat_results) < 0)
459 *size = stat_results.st_size;
471 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
475 GstFdSrc *src = GST_FD_SRC (bsrc);
477 offset = segment->start;
479 /* No need to seek to the current position */
480 if (offset == src->curoffset)
483 res = lseek (src->fd, offset, SEEK_SET);
484 if (G_UNLIKELY (res < 0 || res != offset))
487 segment->last_stop = segment->start;
488 segment->time = segment->start;
493 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
497 /*** GSTURIHANDLER INTERFACE *************************************************/
500 gst_fd_src_uri_get_type (void)
505 gst_fd_src_uri_get_protocols (void)
507 static gchar *protocols[] = { "fd", NULL };
512 gst_fd_src_uri_get_uri (GstURIHandler * handler)
514 GstFdSrc *src = GST_FD_SRC (handler);
520 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
523 GstFdSrc *src = GST_FD_SRC (handler);
526 protocol = gst_uri_get_protocol (uri);
527 if (strcmp (protocol, "fd") != 0) {
533 if (sscanf (uri, "fd://%d", &fd) != 1)
538 GST_OBJECT_LOCK (src);
539 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
540 gst_fd_src_update_fd (src);
542 GST_OBJECT_UNLOCK (src);
548 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
550 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
552 iface->get_type = gst_fd_src_uri_get_type;
553 iface->get_protocols = gst_fd_src_uri_get_protocols;
554 iface->get_uri = gst_fd_src_uri_get_uri;
555 iface->set_uri = gst_fd_src_uri_set_uri;