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>
45 /* the select call is also performed on the control sockets, that way
46 * we can send special commands to unblock the select call */
47 #define CONTROL_STOP 'S' /* stop the select call */
48 #define CONTROL_SOCKETS(src) src->control_sock
49 #define WRITE_SOCKET(src) src->control_sock[1]
50 #define READ_SOCKET(src) src->control_sock[0]
52 #define SEND_COMMAND(src, command) \
54 unsigned char c; c = command; \
55 write (WRITE_SOCKET(src), &c, 1); \
58 #define READ_COMMAND(src, command, res) \
60 res = read(READ_SOCKET(src), &command, 1); \
63 #define DEFAULT_BLOCKSIZE 4096
65 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
70 GST_DEBUG_CATEGORY_STATIC (gst_fdsrc_debug);
71 #define GST_CAT_DEFAULT gst_fdsrc_debug
73 static GstElementDetails gst_fdsrc_details = GST_ELEMENT_DETAILS ("Disk Source",
75 "Synchronous read from a file",
76 "Erik Walthinsen <omega@cse.ogi.edu>");
84 static void gst_fdsrc_uri_handler_init (gpointer g_iface, gpointer iface_data);
87 _do_init (GType fdsrc_type)
89 static const GInterfaceInfo urihandler_info = {
90 gst_fdsrc_uri_handler_init,
95 g_type_add_interface_static (fdsrc_type, GST_TYPE_URI_HANDLER,
98 GST_DEBUG_CATEGORY_INIT (gst_fdsrc_debug, "fdsrc", 0, "fdsrc element");
101 GST_BOILERPLATE_FULL (GstFdSrc, gst_fdsrc, GstElement, GST_TYPE_PUSH_SRC,
104 static void gst_fdsrc_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_fdsrc_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
108 static void gst_fdsrc_dispose (GObject * obj);
110 static gboolean gst_fdsrc_start (GstBaseSrc * bsrc);
111 static gboolean gst_fdsrc_stop (GstBaseSrc * bsrc);
112 static gboolean gst_fdsrc_unlock (GstBaseSrc * bsrc);
114 static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
117 gst_fdsrc_base_init (gpointer g_class)
119 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
121 gst_element_class_add_pad_template (gstelement_class,
122 gst_static_pad_template_get (&srctemplate));
123 gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
127 gst_fdsrc_class_init (GstFdSrcClass * klass)
129 GObjectClass *gobject_class;
130 GstBaseSrcClass *gstbasesrc_class;
131 GstElementClass *gstelement_class;
132 GstPushSrcClass *gstpush_src_class;
134 gobject_class = G_OBJECT_CLASS (klass);
135 gstelement_class = GST_ELEMENT_CLASS (klass);
136 gstbasesrc_class = (GstBaseSrcClass *) klass;
137 gstpush_src_class = (GstPushSrcClass *) klass;
139 parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
141 gobject_class->set_property = gst_fdsrc_set_property;
142 gobject_class->get_property = gst_fdsrc_get_property;
143 gobject_class->dispose = gst_fdsrc_dispose;
145 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FD,
146 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
147 0, G_MAXINT, 0, G_PARAM_READWRITE));
149 gstbasesrc_class->start = gst_fdsrc_start;
150 gstbasesrc_class->stop = gst_fdsrc_stop;
151 gstbasesrc_class->unlock = gst_fdsrc_unlock;
153 gstpush_src_class->create = gst_fdsrc_create;
157 gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
159 /* TODO set live only if it's actually a live source (check
160 * for seekable fd) */
161 gst_base_src_set_live (GST_BASE_SRC (fdsrc), TRUE);
164 fdsrc->uri = g_strdup_printf ("fd://%d", fdsrc->fd);
165 fdsrc->curoffset = 0;
169 gst_fdsrc_dispose (GObject * obj)
171 GstFdSrc *src = GST_FDSRC (obj);
176 G_OBJECT_CLASS (parent_class)->dispose (obj);
180 gst_fdsrc_start (GstBaseSrc * bsrc)
182 GstFdSrc *src = GST_FDSRC (bsrc);
183 gint control_sock[2];
187 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
190 READ_SOCKET (src) = control_sock[0];
191 WRITE_SOCKET (src) = control_sock[1];
193 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
194 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
201 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
208 gst_fdsrc_stop (GstBaseSrc * bsrc)
210 GstFdSrc *src = GST_FDSRC (bsrc);
212 close (READ_SOCKET (src));
213 close (WRITE_SOCKET (src));
219 gst_fdsrc_unlock (GstBaseSrc * bsrc)
221 GstFdSrc *src = GST_FDSRC (bsrc);
223 SEND_COMMAND (src, CONTROL_STOP);
229 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
232 GstFdSrc *src = GST_FDSRC (object);
236 src->fd = g_value_get_int (value);
238 src->uri = g_strdup_printf ("fd://%d", src->fd);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
250 GstFdSrc *src = GST_FDSRC (object);
254 g_value_set_int (value, src->fd);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
275 src = GST_FDSRC (psrc);
279 FD_SET (src->fd, &readfds);
280 FD_SET (READ_SOCKET (src), &readfds);
283 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
284 } while ((retval == -1 && errno == EINTR));
289 if (FD_ISSET (READ_SOCKET (src), &readfds)) {
290 /* read all stop commands */
295 READ_COMMAND (src, command, res);
297 GST_LOG_OBJECT (src, "no more commands");
298 /* no more commands */
306 blocksize = GST_BASE_SRC (src)->blocksize;
308 /* create the buffer */
309 buf = gst_buffer_new_and_alloc (blocksize);
312 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
313 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
321 GST_BUFFER_OFFSET (buf) = src->curoffset;
322 GST_BUFFER_SIZE (buf) = readbytes;
323 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
324 src->curoffset += readbytes;
326 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
328 /* we're done, return the buffer */
337 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
338 ("select on file descriptor: %s.", g_strerror (errno)));
339 GST_DEBUG_OBJECT (psrc, "Error during select");
340 return GST_FLOW_ERROR;
344 GST_DEBUG_OBJECT (psrc, "Select stopped");
345 return GST_FLOW_WRONG_STATE;
350 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
351 gst_buffer_unref (buf);
352 return GST_FLOW_UNEXPECTED;
356 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
357 ("read on file descriptor: %s.", g_strerror (errno)));
358 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
359 gst_buffer_unref (buf);
360 return GST_FLOW_ERROR;
364 /*** GSTURIHANDLER INTERFACE *************************************************/
367 gst_fdsrc_uri_get_type (void)
372 gst_fdsrc_uri_get_protocols (void)
374 static gchar *protocols[] = { "fd", NULL };
379 gst_fdsrc_uri_get_uri (GstURIHandler * handler)
381 GstFdSrc *src = GST_FDSRC (handler);
387 gst_fdsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
390 GstFdSrc *src = GST_FDSRC (handler);
393 protocol = gst_uri_get_protocol (uri);
394 if (strcmp (protocol, "fd") != 0) {
399 sscanf (uri, "fd://%d", &fd);
402 src->uri = g_strdup (uri);
408 gst_fdsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
410 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
412 iface->get_type = gst_fdsrc_uri_get_type;
413 iface->get_protocols = gst_fdsrc_uri_get_protocols;
414 iface->get_uri = gst_fdsrc_uri_get_uri;
415 iface->set_uri = gst_fdsrc_uri_set_uri;