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.
27 #include "gst/gst_private.h"
29 #include <sys/types.h>
31 #include <sys/socket.h>
42 /* the select call is also performed on the control sockets, that way
43 * we can send special commands to unblock the select call */
44 #define CONTROL_STOP 'S' /* stop the select call */
45 #define CONTROL_SOCKETS(src) src->control_sock
46 #define WRITE_SOCKET(src) src->control_sock[1]
47 #define READ_SOCKET(src) src->control_sock[0]
49 #define SEND_COMMAND(src, command) \
51 unsigned char c; c = command; \
52 write (WRITE_SOCKET(src), &c, 1); \
55 #define READ_COMMAND(src, command, res) \
57 res = read(READ_SOCKET(src), &command, 1); \
60 #define DEFAULT_BLOCKSIZE 4096
62 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
67 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
68 #define GST_CAT_DEFAULT gst_fdsrc_debug
70 static GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS ("Disk Source",
72 "Synchronous read from a file",
73 "Erik Walthinsen <omega@cse.ogi.edu>");
82 #define _do_init(bla) \
83 GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
85 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_PUSH_SRC,
88 static void gst_fdsrc_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_fdsrc_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
93 static gboolean gst_fdsrc_start (GstBaseSrc * bsrc);
94 static gboolean gst_fdsrc_stop (GstBaseSrc * bsrc);
95 static gboolean gst_fdsrc_unlock (GstBaseSrc * bsrc);
97 static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
100 gst_fdsrc_base_init (gpointer g_class)
102 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
104 gst_element_class_add_pad_template (gstelement_class,
105 gst_static_pad_template_get (&srctemplate));
106 gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
110 gst_fdsrc_class_init (GstFdSrcClass * klass)
112 GObjectClass *gobject_class;
113 GstBaseSrcClass *gstbasesrc_class;
114 GstElementClass *gstelement_class;
115 GstPushSrcClass *gstpush_src_class;
117 gobject_class = G_OBJECT_CLASS (klass);
118 gstelement_class = GST_ELEMENT_CLASS (klass);
119 gstbasesrc_class = (GstBaseSrcClass *) klass;
120 gstpush_src_class = (GstPushSrcClass *) klass;
122 parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
124 gobject_class->set_property = gst_fdsrc_set_property;
125 gobject_class->get_property = gst_fdsrc_get_property;
127 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FD,
128 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
129 0, G_MAXINT, 0, G_PARAM_READWRITE));
131 gstbasesrc_class->start = gst_fdsrc_start;
132 gstbasesrc_class->stop = gst_fdsrc_stop;
133 gstbasesrc_class->unlock = gst_fdsrc_unlock;
135 gstpush_src_class->create = gst_fdsrc_create;
139 gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
141 /* TODO set live only if it's actually a live source (check
142 * for seekable fd) */
143 gst_base_src_set_live (GST_BASE_SRC (fdsrc), TRUE);
146 fdsrc->curoffset = 0;
150 gst_fdsrc_start (GstBaseSrc * bsrc)
152 GstFdSrc *src = GST_FDSRC (bsrc);
153 gint control_sock[2];
157 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
160 READ_SOCKET (src) = control_sock[0];
161 WRITE_SOCKET (src) = control_sock[1];
163 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
164 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
171 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
178 gst_fdsrc_stop (GstBaseSrc * bsrc)
180 GstFdSrc *src = GST_FDSRC (bsrc);
182 close (READ_SOCKET (src));
183 close (WRITE_SOCKET (src));
189 gst_fdsrc_unlock (GstBaseSrc * bsrc)
191 GstFdSrc *src = GST_FDSRC (bsrc);
193 SEND_COMMAND (src, CONTROL_STOP);
199 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
202 GstFdSrc *src = GST_FDSRC (object);
206 src->fd = g_value_get_int (value);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
218 GstFdSrc *src = GST_FDSRC (object);
222 g_value_set_int (value, src->fd);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
243 src = GST_FDSRC (psrc);
247 FD_SET (src->fd, &readfds);
248 FD_SET (READ_SOCKET (src), &readfds);
251 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
252 } while ((retval == -1 && errno == EINTR));
257 if (FD_ISSET (READ_SOCKET (src), &readfds)) {
258 /* read all stop commands */
263 READ_COMMAND (src, command, res);
265 GST_LOG_OBJECT (src, "no more commands");
266 /* no more commands */
274 blocksize = GST_BASE_SRC (src)->blocksize;
276 /* create the buffer */
277 buf = gst_buffer_new_and_alloc (blocksize);
280 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
281 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
289 GST_BUFFER_OFFSET (buf) = src->curoffset;
290 GST_BUFFER_SIZE (buf) = readbytes;
291 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
292 src->curoffset += readbytes;
294 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
296 /* we're done, return the buffer */
305 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
306 ("select on file descriptor: %s.", g_strerror (errno)));
307 GST_DEBUG_OBJECT (psrc, "Error during select");
308 return GST_FLOW_ERROR;
312 GST_DEBUG_OBJECT (psrc, "Select stopped");
313 return GST_FLOW_WRONG_STATE;
318 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
319 gst_buffer_unref (buf);
320 return GST_FLOW_UNEXPECTED;
324 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
325 ("read on file descriptor: %s.", g_strerror (errno)));
326 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
327 gst_buffer_unref (buf);
328 return GST_FLOW_ERROR;