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_fd_src_debug);
71 #define GST_CAT_DEFAULT gst_fd_src_debug
73 static GstElementDetails gst_fd_src_details =
74 GST_ELEMENT_DETAILS ("Disk Source",
76 "Synchronous read from a file",
77 "Erik Walthinsen <omega@cse.ogi.edu>");
85 static void gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
88 _do_init (GType fd_src_type)
90 static const GInterfaceInfo urihandler_info = {
91 gst_fd_src_uri_handler_init,
96 g_type_add_interface_static (fd_src_type, GST_TYPE_URI_HANDLER,
99 GST_DEBUG_CATEGORY_INIT (gst_fd_src_debug, "fdsrc", 0, "fdsrc element");
102 GST_BOILERPLATE_FULL (GstFdSrc, gst_fd_src, GstElement, GST_TYPE_PUSH_SRC,
105 static void gst_fd_src_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_fd_src_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
109 static void gst_fd_src_dispose (GObject * obj);
111 static gboolean gst_fd_src_start (GstBaseSrc * bsrc);
112 static gboolean gst_fd_src_stop (GstBaseSrc * bsrc);
113 static gboolean gst_fd_src_unlock (GstBaseSrc * bsrc);
114 static gboolean gst_fd_src_is_seekable (GstBaseSrc * bsrc);
115 static gboolean gst_fd_src_get_size (GstBaseSrc * src, guint64 * size);
117 static GstFlowReturn gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
120 gst_fd_src_base_init (gpointer g_class)
122 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
124 gst_element_class_add_pad_template (gstelement_class,
125 gst_static_pad_template_get (&srctemplate));
126 gst_element_class_set_details (gstelement_class, &gst_fd_src_details);
130 gst_fd_src_class_init (GstFdSrcClass * klass)
132 GObjectClass *gobject_class;
133 GstBaseSrcClass *gstbasesrc_class;
134 GstElementClass *gstelement_class;
135 GstPushSrcClass *gstpush_src_class;
137 gobject_class = G_OBJECT_CLASS (klass);
138 gstelement_class = GST_ELEMENT_CLASS (klass);
139 gstbasesrc_class = (GstBaseSrcClass *) klass;
140 gstpush_src_class = (GstPushSrcClass *) klass;
142 parent_class = g_type_class_ref (GST_TYPE_PUSH_SRC);
144 gobject_class->set_property = gst_fd_src_set_property;
145 gobject_class->get_property = gst_fd_src_get_property;
146 gobject_class->dispose = gst_fd_src_dispose;
148 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FD,
149 g_param_spec_int ("fd", "fd", "An open file descriptor to read from",
150 0, G_MAXINT, 0, G_PARAM_READWRITE));
152 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_fd_src_start);
153 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_fd_src_stop);
154 gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_src_unlock);
155 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_fd_src_is_seekable);
156 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_fd_src_get_size);
158 gstpush_src_class->create = GST_DEBUG_FUNCPTR (gst_fd_src_create);
162 gst_fd_src_init (GstFdSrc * fdsrc, GstFdSrcClass * klass)
166 fdsrc->seekable_fd = FALSE;
167 fdsrc->uri = g_strdup_printf ("fd://%d", fdsrc->fd);
168 fdsrc->curoffset = 0;
172 gst_fd_src_dispose (GObject * obj)
174 GstFdSrc *src = GST_FD_SRC (obj);
179 G_OBJECT_CLASS (parent_class)->dispose (obj);
183 gst_fd_src_update_fd (GstFdSrc * src)
185 struct stat stat_results;
187 src->fd = src->new_fd;
189 src->uri = g_strdup_printf ("fd://%d", src->fd);
191 if (fstat (src->fd, &stat_results) < 0)
194 if (!S_ISREG (stat_results.st_mode))
197 /* Try a seek of 0 bytes offset to check for seekability */
198 if (lseek (src->fd, SEEK_CUR, 0) < 0)
201 src->seekable_fd = TRUE;
205 src->seekable_fd = FALSE;
209 gst_fd_src_start (GstBaseSrc * bsrc)
211 GstFdSrc *src = GST_FD_SRC (bsrc);
212 gint control_sock[2];
216 gst_fd_src_update_fd (src);
218 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
221 READ_SOCKET (src) = control_sock[0];
222 WRITE_SOCKET (src) = control_sock[1];
224 fcntl (READ_SOCKET (src), F_SETFL, O_NONBLOCK);
225 fcntl (WRITE_SOCKET (src), F_SETFL, O_NONBLOCK);
232 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
239 gst_fd_src_stop (GstBaseSrc * bsrc)
241 GstFdSrc *src = GST_FD_SRC (bsrc);
243 close (READ_SOCKET (src));
244 close (WRITE_SOCKET (src));
250 gst_fd_src_unlock (GstBaseSrc * bsrc)
252 GstFdSrc *src = GST_FD_SRC (bsrc);
254 SEND_COMMAND (src, CONTROL_STOP);
260 gst_fd_src_set_property (GObject * object, guint prop_id, const GValue * value,
263 GstFdSrc *src = GST_FD_SRC (object);
267 src->new_fd = g_value_get_int (value);
269 /* If state is ready or below, update the current fd immediately
270 * so it is reflected in get_properties and uri */
271 GST_OBJECT_LOCK (object);
272 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
273 gst_fd_src_update_fd (src);
275 GST_OBJECT_UNLOCK (object);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_fd_src_get_property (GObject * object, guint prop_id, GValue * value,
287 GstFdSrc *src = GST_FD_SRC (object);
291 g_value_set_int (value, src->fd);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gst_fd_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
312 src = GST_FD_SRC (psrc);
316 FD_SET (src->fd, &readfds);
317 FD_SET (READ_SOCKET (src), &readfds);
320 retval = select (FD_SETSIZE, &readfds, NULL, NULL, NULL);
321 } while ((retval == -1 && errno == EINTR));
326 if (FD_ISSET (READ_SOCKET (src), &readfds)) {
327 /* read all stop commands */
332 READ_COMMAND (src, command, res);
334 GST_LOG_OBJECT (src, "no more commands");
335 /* no more commands */
343 blocksize = GST_BASE_SRC (src)->blocksize;
345 /* create the buffer */
346 buf = gst_buffer_new_and_alloc (blocksize);
349 readbytes = read (src->fd, GST_BUFFER_DATA (buf), blocksize);
350 } while (readbytes == -1 && errno == EINTR); /* retry if interrupted */
358 GST_BUFFER_OFFSET (buf) = src->curoffset;
359 GST_BUFFER_SIZE (buf) = readbytes;
360 GST_BUFFER_TIMESTAMP (buf) = GST_CLOCK_TIME_NONE;
361 src->curoffset += readbytes;
363 GST_DEBUG_OBJECT (psrc, "Read buffer of size %u.", readbytes);
365 /* we're done, return the buffer */
374 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
375 ("select on file descriptor: %s.", g_strerror (errno)));
376 GST_DEBUG_OBJECT (psrc, "Error during select");
377 return GST_FLOW_ERROR;
381 GST_DEBUG_OBJECT (psrc, "Select stopped");
382 return GST_FLOW_WRONG_STATE;
387 GST_DEBUG_OBJECT (psrc, "Read 0 bytes. EOS.");
388 gst_buffer_unref (buf);
389 return GST_FLOW_UNEXPECTED;
393 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
394 ("read on file descriptor: %s.", g_strerror (errno)));
395 GST_DEBUG_OBJECT (psrc, "Error reading from fd");
396 gst_buffer_unref (buf);
397 return GST_FLOW_ERROR;
402 gst_fd_src_is_seekable (GstBaseSrc * bsrc)
404 GstFdSrc *src = GST_FD_SRC (bsrc);
406 return src->seekable_fd;
410 gst_fd_src_get_size (GstBaseSrc * bsrc, guint64 * size)
412 GstFdSrc *src = GST_FD_SRC (bsrc);
413 struct stat stat_results;
415 if (!src->seekable_fd) {
416 /* If it isn't seekable, we won't know the length (but fstat will still
417 * succeed, and wrongly say our length is zero. */
421 if (fstat (src->fd, &stat_results) < 0)
424 *size = stat_results.st_size;
436 /*** GSTURIHANDLER INTERFACE *************************************************/
439 gst_fd_src_uri_get_type (void)
444 gst_fd_src_uri_get_protocols (void)
446 static gchar *protocols[] = { "fd", NULL };
451 gst_fd_src_uri_get_uri (GstURIHandler * handler)
453 GstFdSrc *src = GST_FD_SRC (handler);
459 gst_fd_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
462 GstFdSrc *src = GST_FD_SRC (handler);
465 protocol = gst_uri_get_protocol (uri);
466 if (strcmp (protocol, "fd") != 0) {
472 if (sscanf (uri, "fd://%d", &fd) != 1)
477 GST_OBJECT_LOCK (src);
478 if (GST_STATE (GST_ELEMENT (src)) <= GST_STATE_READY) {
479 gst_fd_src_update_fd (src);
481 GST_OBJECT_UNLOCK (src);
487 gst_fd_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
489 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
491 iface->get_type = gst_fd_src_uri_get_type;
492 iface->get_protocols = gst_fd_src_uri_get_protocols;
493 iface->get_uri = gst_fd_src_uri_get_uri;
494 iface->set_uri = gst_fd_src_uri_set_uri;