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
81 static const GstElementDetails gst_fd_src_details =
82 GST_ELEMENT_DETAILS ("Disk Source",
84 "Synchronous read from a file",
85 "Erik Walthinsen <omega@cse.ogi.edu>");
93 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
96 _do_init (GType fd_src_type)
98 static const GInterfaceInfo urihandler_info = {
99 gst_fd_src_uri_handler_init,
104 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
107 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
110 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstElement, GST_TYPE_PUSH_SRC,
113 static void gst_fd_src_set_property (GObject * object, guint prop_id,
114 const GValue * value, GParamSpec * pspec);
115 static void gst_fd_src_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec);
117 static void gst_fd_src_dispose (GObject * obj);
119 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
120 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
121 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
122 static gboolean gst_fd_src_unlock_stop (GstBaseSrc * bsrc);
123 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
124 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
125 static gboolean gst_fd_src_do_seek (GstBaseSrc * src, GstSegment * segment);
127 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
130 gst_fd_src_base_init (gpointer g_class)
132 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
134 gst_element_class_add_pad_template (gstelement_class,
135 gst_static_pad_template_get (&srctemplate));
136 gst_element_class_set_details (gstelement_class, &gst_fd_src_details);
140 gst_fd_src_class_init (GstFdSrcClass * klass)
142 GObjectClass *gobject_class;
143 GstBaseSrcClass *gstbasesrc_class;
144 GstElementClass *gstelement_class;
145 GstPushSrcClass *gstpush_src_class;
147 gobject_class = G_OBJECT_CLASS (klass);
148 gstelement_class = GST_ELEMENT_CLASS (klass);
149 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
150 gstpush_src_class = GST_PUSH_SRC_CLASS (klass);
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->set_property = gst_fd_src_set_property;
155 gobject_class->get_property = gst_fd_src_get_property;
156 gobject_class->dispose = gst_fd_src_dispose;
158 g_object_class_install_property (gobject_class, PROP_FD,
159 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
160 0, G_MAXINT, 0, G_PARAM_READWRITE));
162 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
163 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
164 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
165 gstbasesrc_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_src_unlock_stop);
166 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
167 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
168 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_fd_src_do_seek);
170 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
174 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
178 fdsrc->seekable_fd = FALSE;
179 fdsrc->uri = g_strdup_printf ("fd://0");
180 fdsrc->curoffset = 0;
184 gst_fd_src_dispose (GObject * obj)
186 GstFdSrc *src = GST_FD_SRC (obj);
191 G_OBJECT_CLASS (parent_class)->dispose (obj);
195 gst_fd_src_update_fd (GstFdSrc * src)
197 struct stat stat_results;
199 if (src->fd != src->new_fd) {
200 GST_INFO_OBJECT (src, "Updating to fd %d", src->new_fd);
202 src->fd = src->new_fd;
205 src->uri = g_strdup_printf ("fd://%d", src->fd);
207 if (fstat (src->fd, &stat_results) < 0)
210 if (!S_ISREG (stat_results.st_mode))
213 /* Try a seek of 0 bytes offset to check for seekability */
214 if (lseek (src->fd, 0, SEEK_CUR) < 0)
217 GST_INFO_OBJECT (src, "marking fd %d as seekable", src->fd);
218 src->seekable_fd = TRUE;
224 GST_INFO_OBJECT (src, "marking fd %d as NOT seekable", src->fd);
225 src->seekable_fd = FALSE;
230 gst_fd_src_start (GstBaseSrc * bsrc)
232 GstFdSrc *src = GST_FD_SRC (bsrc);
233 gint control_sock[2];
237 gst_fd_src_update_fd (src);
239 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
242 READ_SOCKET (src) = control_sock[0];
243 WRITE_SOCKET (src) = control_sock[1];
245 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
246 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
253 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
260 gst_fd_src_stop (GstBaseSrc * bsrc)
262 GstFdSrc *src = GST_FD_SRC (bsrc);
264 close (READ_SOCKET (src));
265 close (WRITE_SOCKET (src));
271 gst_fd_src_unlock (GstBaseSrc * bsrc)
273 GstFdSrc *src = GST_FD_SRC (bsrc);
275 GST_LOG_OBJECT (src, "sending unlock command");
276 SEND_COMMAND (src, CONTROL_STOP);
282 gst_fd_src_unlock_stop (GstBaseSrc * bsrc)
284 GstFdSrc *src = GST_FD_SRC (bsrc);
286 GST_LOG_OBJECT (src, "clearing unlock command queue");
288 /* read all stop commands */
293 GST_LOG_OBJECT (src, "reading command");
295 READ_COMMAND (src, command, res);
297 GST_LOG_OBJECT (src, "no more commands");
298 /* no more commands */
307 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
310 GstFdSrc *src = GST_FD_SRC (object);
314 src->new_fd = g_value_get_int (value);
316 /* If state is ready or below, update the current fd immediately
317 * so it is reflected in get_properties and uri */
318 GST_OBJECT_LOCK (object);
319 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
320 GST_DEBUG_OBJECT (src, "state ready or lower, updating to use new fd");
321 gst_fd_src_update_fd (src);
323 GST_DEBUG_OBJECT (src, "state above ready, not updating to new fd yet");
325 GST_OBJECT_UNLOCK (object);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
337 GstFdSrc *src = GST_FD_SRC (object);
341 g_value_set_int (value, src->fd);
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
362 src = GST_FD_SRC (psrc);
366 FD_SET (src->fd, &readfds);
367 FD_SET (READ_SOCKET (src), &readfds);
370 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
371 } while ((retval == -1 && errno == EINTR));
376 if (FD_ISSET (READ_SOCKET (src), &readfds))
380 blocksize = GST_BASE_SRC (src)->blocksize;
382 /* create the buffer */
383 buf = gst_buffer_new_and_alloc (blocksize);
386 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
387 GST_LOG_OBJECT (src, "read %" G_GSSIZE_FORMAT, readbytes);
388 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
396 GST_BUFFER_OFFSET (buf) = src->curoffset;
397 GST_BUFFER_SIZE (buf) = readbytes;
398 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
399 src->curoffset += readbytes;
401 GST_LOG_OBJECT (psrc, "Read buffer of size %" G_GSSIZE_FORMAT, readbytes);
403 /* we're done, return the buffer */
412 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
413 ("select on file descriptor: %s.", g_strerror (errno)));
414 GST_DEBUG_OBJECT (psrc, "Error during select");
415 return GST_FLOW_ERROR;
419 GST_DEBUG_OBJECT (psrc, "Select stopped");
420 return GST_FLOW_WRONG_STATE;
425 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
426 gst_buffer_unref (buf);
427 return GST_FLOW_UNEXPECTED;
431 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
432 ("read on file descriptor: %s.", g_strerror (errno)));
433 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
434 gst_buffer_unref (buf);
435 return GST_FLOW_ERROR;
440 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
442 GstFdSrc *src = GST_FD_SRC (bsrc);
444 return src->seekable_fd;
448 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
450 GstFdSrc *src = GST_FD_SRC (bsrc);
451 struct stat stat_results;
453 if (!src->seekable_fd) {
454 /* If it isn't seekable, we won't know the length (but fstat will still
455 * succeed, and wrongly say our length is zero. */
459 if (fstat (src->fd, &stat_results) < 0)
462 *size = stat_results.st_size;
474 gst_fd_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
478 GstFdSrc *src = GST_FD_SRC (bsrc);
480 offset = segment->start;
482 /* No need to seek to the current position */
483 if (offset == src->curoffset)
486 res = lseek (src->fd, offset, SEEK_SET);
487 if (G_UNLIKELY (res < 0 || res != offset))
490 segment->last_stop = segment->start;
491 segment->time = segment->start;
496 GST_DEBUG_OBJECT (src, "lseek returned %" G_GINT64_FORMAT, offset);
500 /*** GSTURIHANDLER INTERFACE *************************************************/
503 gst_fd_src_uri_get_type (void)
508 gst_fd_src_uri_get_protocols (void)
510 static gchar *protocols[] = { "fd", NULL };
515 gst_fd_src_uri_get_uri (GstURIHandler * handler)
517 GstFdSrc *src = GST_FD_SRC (handler);
523 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
526 GstFdSrc *src = GST_FD_SRC (handler);
529 protocol = gst_uri_get_protocol (uri);
530 if (strcmp (protocol, "fd") != 0) {
536 if (sscanf (uri, "fd://%d", &fd) != 1)
541 GST_OBJECT_LOCK (src);
542 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
543 gst_fd_src_update_fd (src);
545 GST_OBJECT_UNLOCK (src);
551 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
553 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
555 iface->get_type = gst_fd_src_uri_get_type;
556 iface->get_protocols = gst_fd_src_uri_get_protocols;
557 iface->get_uri = gst_fd_src_uri_get_uri;
558 iface->set_uri = gst_fd_src_uri_set_uri;