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);
113 static gboolean gst_fdsrc_is_seekable (GstBaseSrc * bsrc);
114 static gboolean gst_fdsrc_get_size (GstBaseSrc * src, guint64 * size);
116 static GstFlowReturn gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf);
119 gst_fdsrc_base_init (gpointer g_class)
121 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
123 gst_element_class_add_pad_template (gstelement_class,
124 gst_static_pad_template_get (&srctemplate));
125 gst_element_class_set_details (gstelement_class, &gst_fdsrc_details);
129 gst_fdsrc_class_init (GstFdSrcClass * klass)
131 GObjectClass *gobject_class;
132 GstBaseSrcClass *gstbasesrc_class;
133 GstElementClass *gstelement_class;
134 GstPushSrcClass *gstpush_src_class;
136 gobject_class = G_OBJECT_CLASS (klass);
137 gstelement_class = GST_ELEMENT_CLASS (klass);
138 gstbasesrc_class = (GstBaseSrcClass *) klass;
139 gstpush_src_class = (GstPushSrcClass *) klass;
141 parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
143 gobject_class->set_property = gst_fdsrc_set_property;
144 gobject_class->get_property = gst_fdsrc_get_property;
145 gobject_class->dispose = gst_fdsrc_dispose;
147 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FD,
148 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
149 0, G_MAXINT, 0, G_PARAM_READWRITE));
151 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fdsrc_start);
152 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fdsrc_stop);
153 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fdsrc_unlock);
154 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fdsrc_is_seekable);
155 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fdsrc_get_size);
157 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fdsrc_create);
161 gst_fdsrc_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
165 fdsrc->seekable_fd = FALSE;
166 fdsrc->uri = g_strdup_printf ("fd://%d", fdsrc->fd);
167 fdsrc->curoffset = 0;
171 gst_fdsrc_dispose (GObject * obj)
173 GstFdSrc *src = GST_FDSRC (obj);
178 G_OBJECT_CLASS (parent_class)->dispose (obj);
182 gst_fdsrc_update_fd (GstFdSrc * src)
184 struct stat stat_results;
186 src->fd = src->new_fd;
188 src->uri = g_strdup_printf ("fd://%d", src->fd);
190 if (fstat (src->fd, &stat_results) < 0)
193 if (!S_ISREG (stat_results.st_mode))
196 /* Try a seek of 0 bytes offset to check for seekability */
197 if (lseek (src->fd, SEEK_CUR, 0) < 0)
200 src->seekable_fd = TRUE;
204 src->seekable_fd = FALSE;
208 gst_fdsrc_start (GstBaseSrc * bsrc)
210 GstFdSrc *src = GST_FDSRC (bsrc);
211 gint control_sock[2];
215 gst_fdsrc_update_fd (src);
217 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
220 READ_SOCKET (src) = control_sock[0];
221 WRITE_SOCKET (src) = control_sock[1];
223 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
224 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
231 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
238 gst_fdsrc_stop (GstBaseSrc * bsrc)
240 GstFdSrc *src = GST_FDSRC (bsrc);
242 close (READ_SOCKET (src));
243 close (WRITE_SOCKET (src));
249 gst_fdsrc_unlock (GstBaseSrc * bsrc)
251 GstFdSrc *src = GST_FDSRC (bsrc);
253 SEND_COMMAND (src, CONTROL_STOP);
259 gst_fdsrc_set_property (GObject * object, guint prop_id, const GValue * value,
262 GstFdSrc *src = GST_FDSRC (object);
266 src->new_fd = g_value_get_int (value);
268 /* If state is ready or below, update the current fd immediately
269 * so it is reflected in get_properties and uri */
270 GST_OBJECT_LOCK (object);
271 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
272 gst_fdsrc_update_fd (src);
274 GST_OBJECT_UNLOCK (object);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gst_fdsrc_get_property (GObject * object, guint prop_id, GValue * value,
286 GstFdSrc *src = GST_FDSRC (object);
290 g_value_set_int (value, src->fd);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 gst_fdsrc_create (GstPushSrc * psrc, GstBuffer ** outbuf)
311 src = GST_FDSRC (psrc);
315 FD_SET (src->fd, &readfds);
316 FD_SET (READ_SOCKET (src), &readfds);
319 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
320 } while ((retval == -1 && errno == EINTR));
325 if (FD_ISSET (READ_SOCKET (src), &readfds)) {
326 /* read all stop commands */
331 READ_COMMAND (src, command, res);
333 GST_LOG_OBJECT (src, "no more commands");
334 /* no more commands */
342 blocksize = GST_BASE_SRC (src)->blocksize;
344 /* create the buffer */
345 buf = gst_buffer_new_and_alloc (blocksize);
348 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
349 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
357 GST_BUFFER_OFFSET (buf) = src->curoffset;
358 GST_BUFFER_SIZE (buf) = readbytes;
359 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
360 src->curoffset += readbytes;
362 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
364 /* we're done, return the buffer */
373 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
374 ("select on file descriptor: %s.", g_strerror (errno)));
375 GST_DEBUG_OBJECT (psrc, "Error during select");
376 return GST_FLOW_ERROR;
380 GST_DEBUG_OBJECT (psrc, "Select stopped");
381 return GST_FLOW_WRONG_STATE;
386 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
387 gst_buffer_unref (buf);
388 return GST_FLOW_UNEXPECTED;
392 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
393 ("read on file descriptor: %s.", g_strerror (errno)));
394 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
395 gst_buffer_unref (buf);
396 return GST_FLOW_ERROR;
401 gst_fdsrc_is_seekable (GstBaseSrc * bsrc)
403 GstFdSrc *src = GST_FDSRC (bsrc);
405 return src->seekable_fd;
409 gst_fdsrc_get_size (GstBaseSrc * bsrc, guint64 * size)
411 GstFdSrc *src = GST_FDSRC (bsrc);
412 struct stat stat_results;
414 if (!src->seekable_fd) {
415 /* If it isn't seekable, we won't know the length (but fstat will still
416 * succeed, and wrongly say our length is zero. */
420 if (fstat (src->fd, &stat_results) < 0)
423 *size = stat_results.st_size;
435 /*** GSTURIHANDLER INTERFACE *************************************************/
438 gst_fdsrc_uri_get_type (void)
443 gst_fdsrc_uri_get_protocols (void)
445 static gchar *protocols[] = { "fd", NULL };
450 gst_fdsrc_uri_get_uri (GstURIHandler * handler)
452 GstFdSrc *src = GST_FDSRC (handler);
458 gst_fdsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
461 GstFdSrc *src = GST_FDSRC (handler);
464 protocol = gst_uri_get_protocol (uri);
465 if (strcmp (protocol, "fd") != 0) {
471 if (sscanf (uri, "fd://%d", &fd) != 1)
476 GST_OBJECT_LOCK (src);
477 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
478 gst_fdsrc_update_fd (src);
480 GST_OBJECT_UNLOCK (src);
486 gst_fdsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
488 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
490 iface->get_type = gst_fdsrc_uri_get_type;
491 iface->get_protocols = gst_fdsrc_uri_get_protocols;
492 iface->get_uri = gst_fdsrc_uri_get_uri;
493 iface->set_uri = gst_fdsrc_uri_set_uri;