2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * SECTION:element-filesrc
24 * @see_also: #GstFileSrc
26 * Read data from a file in the local file system.
29 * <title>Example launch line</title>
31 * gst-launch filesrc location=song.ogg ! decodebin2 ! autoaudiosink
32 * ]| Play a song.ogg from local dir.
41 #include "gstfilesrc.h"
44 #include <sys/types.h>
46 #include <io.h> /* lseek, open, close, read */
47 /* On win32, stat* default to 32 bit; we need the 64-bit
48 * variants, so explicitly define it that way. */
50 #define fstat _fstat64
52 #define lseek _lseeki64
55 /* Prevent stat.h from defining the stat* functions as
56 * _stat*, since we're explicitly overriding that */
69 #include "../../gst/gst-i18n-lib.h"
71 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76 /* FIXME we should be using glib for this */
78 #define S_ISREG(mode) ((mode)&_S_IFREG)
81 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
84 #define S_ISSOCK(x) (0)
90 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
91 * use the 'file descriptor' opened in glib (and returned from this function)
92 * in this library, as they may have unrelated C runtimes. */
94 gst_open (const gchar * filename, int flags, int mode)
97 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
101 if (wfilename == NULL) {
106 retval = _wopen (wfilename, flags, mode);
114 return open (filename, flags, mode);
118 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
119 #define GST_CAT_DEFAULT gst_file_src_debug
121 /* FileSrc signals and args */
128 #define DEFAULT_BLOCKSIZE 4*1024
137 static void gst_file_src_finalize (GObject * object);
139 static void gst_file_src_set_property (GObject * object, guint prop_id,
140 const GValue * value, GParamSpec * pspec);
141 static void gst_file_src_get_property (GObject * object, guint prop_id,
142 GValue * value, GParamSpec * pspec);
144 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
145 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
147 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
148 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
149 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
150 guint length, GstBuffer * buf);
151 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
153 static void gst_file_src_uri_handler_init (gpointer g_iface,
154 gpointer iface_data);
157 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
158 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
159 #define gst_file_src_parent_class parent_class
160 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
163 gst_file_src_class_init (GstFileSrcClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseSrcClass *gstbasesrc_class;
169 gobject_class = G_OBJECT_CLASS (klass);
170 gstelement_class = GST_ELEMENT_CLASS (klass);
171 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
173 gobject_class->set_property = gst_file_src_set_property;
174 gobject_class->get_property = gst_file_src_get_property;
176 g_object_class_install_property (gobject_class, PROP_FD,
177 g_param_spec_int ("fd", "File-descriptor",
178 "File-descriptor for the file being mmap()d", 0, G_MAXINT, 0,
179 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_LOCATION,
181 g_param_spec_string ("location", "File Location",
182 "Location of the file to read", NULL,
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
184 GST_PARAM_MUTABLE_READY));
186 gobject_class->finalize = gst_file_src_finalize;
188 gst_element_class_set_details_simple (gstelement_class,
191 "Read from arbitrary point in a file",
192 "Erik Walthinsen <omega@cse.ogi.edu>");
193 gst_element_class_add_pad_template (gstelement_class,
194 gst_static_pad_template_get (&srctemplate));
196 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
197 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
198 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
199 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
200 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
201 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
203 if (sizeof (off_t) < 8) {
204 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
210 gst_file_src_init (GstFileSrc * src)
212 src->filename = NULL;
216 src->is_regular = FALSE;
220 gst_file_src_finalize (GObject * object)
224 src = GST_FILE_SRC (object);
226 g_free (src->filename);
229 G_OBJECT_CLASS (parent_class)->finalize (object);
233 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
237 /* the element must be stopped in order to do this */
238 GST_OBJECT_LOCK (src);
239 state = GST_STATE (src);
240 if (state != GST_STATE_READY && state != GST_STATE_NULL)
242 GST_OBJECT_UNLOCK (src);
244 g_free (src->filename);
247 /* clear the filename if we get a NULL (is that possible?) */
248 if (location == NULL) {
249 src->filename = NULL;
252 /* we store the filename as received by the application. On Windows this
254 src->filename = g_strdup (location);
255 src->uri = gst_filename_to_uri (location, NULL);
256 GST_INFO ("filename : %s", src->filename);
257 GST_INFO ("uri : %s", src->uri);
259 g_object_notify (G_OBJECT (src), "location");
260 /* FIXME 0.11: notify "uri" property once there is one */
267 g_warning ("Changing the `location' property on filesrc when a file is "
268 "open is not supported.");
269 GST_OBJECT_UNLOCK (src);
275 gst_file_src_set_property (GObject * object, guint prop_id,
276 const GValue * value, GParamSpec * pspec)
280 g_return_if_fail (GST_IS_FILE_SRC (object));
282 src = GST_FILE_SRC (object);
286 gst_file_src_set_location (src, g_value_get_string (value));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
300 g_return_if_fail (GST_IS_FILE_SRC (object));
302 src = GST_FILE_SRC (object);
306 g_value_set_string (value, src->filename);
309 g_value_set_int (value, src->fd);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 * that is to say, you shouldn't read the code below, but the code that reads
320 * stuff is below. Well, you shouldn't not read the code below, feel free
321 * to read it of course. It's just that "read code below" is a pretty crappy
322 * documentation string because it sounds like we're expecting you to read
323 * the code to understand what it does, which, while true, is really not
324 * the sort of attitude we want to be advertising. No sir.
328 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
332 guint to_read, bytes_read;
336 src = GST_FILE_SRC_CAST (basesrc);
338 if (G_UNLIKELY (src->read_position != offset)) {
341 res = lseek (src->fd, offset, SEEK_SET);
342 if (G_UNLIKELY (res < 0 || res != offset))
345 src->read_position = offset;
348 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
352 while (to_read > 0) {
353 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
354 to_read, offset + bytes_read);
356 ret = read (src->fd, data + bytes_read, to_read);
357 if (G_UNLIKELY (ret < 0)) {
358 if (errno == EAGAIN || errno == EINTR)
363 /* files should eos if they read 0 and more was requested */
364 if (G_UNLIKELY (ret == 0))
370 src->read_position += ret;
373 gst_buffer_unmap (buf, data, bytes_read);
375 GST_BUFFER_OFFSET (buf) = offset;
376 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
383 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
384 return GST_FLOW_ERROR;
388 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
389 gst_buffer_unmap (buf, data, 0);
390 return GST_FLOW_ERROR;
395 gst_buffer_unmap (buf, data, 0);
401 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
403 gboolean ret = FALSE;
404 GstFileSrc *src = GST_FILE_SRC (basesrc);
406 switch (GST_QUERY_TYPE (query)) {
408 gst_query_set_uri (query, src->uri);
417 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
423 gst_file_src_is_seekable (GstBaseSrc * basesrc)
425 GstFileSrc *src = GST_FILE_SRC (basesrc);
427 return src->seekable;
431 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
433 struct stat stat_results;
436 src = GST_FILE_SRC (basesrc);
438 if (!src->seekable) {
439 /* If it isn't seekable, we won't know the length (but fstat will still
440 * succeed, and wrongly say our length is zero. */
444 if (fstat (src->fd, &stat_results) < 0)
447 *size = stat_results.st_size;
458 /* open the file and mmap it, necessary to go to READY state */
460 gst_file_src_start (GstBaseSrc * basesrc)
462 GstFileSrc *src = GST_FILE_SRC (basesrc);
463 struct stat stat_results;
465 if (src->filename == NULL || src->filename[0] == '\0')
468 GST_INFO_OBJECT (src, "opening file %s", src->filename);
471 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
476 /* check if it is a regular file, otherwise bail out */
477 if (fstat (src->fd, &stat_results) < 0)
480 if (S_ISDIR (stat_results.st_mode))
483 if (S_ISSOCK (stat_results.st_mode))
486 src->read_position = 0;
488 /* record if it's a regular (hence seekable and lengthable) file */
489 if (S_ISREG (stat_results.st_mode))
490 src->is_regular = TRUE;
492 /* We need to check if the underlying file is seekable. */
494 off_t res = lseek (src->fd, 0, SEEK_END);
497 GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
498 "failed: %s", g_strerror (errno));
499 src->seekable = FALSE;
501 src->seekable = TRUE;
503 lseek (src->fd, 0, SEEK_SET);
506 /* We can only really do seeking on regular files - for other file types, we
507 * don't know their length, so seeking isn't useful/meaningful */
508 src->seekable = src->seekable && src->is_regular;
510 gst_base_src_set_dynamic_size (basesrc, src->seekable);
517 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
518 (_("No file name specified for reading.")), (NULL));
525 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
526 ("No such file \"%s\"", src->filename));
529 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
530 (_("Could not open file \"%s\" for reading."), src->filename),
538 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
539 (_("Could not get info on \"%s\"."), src->filename), (NULL));
544 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
545 (_("\"%s\" is a directory."), src->filename), (NULL));
550 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
551 (_("File \"%s\" is a socket."), src->filename), (NULL));
560 /* unmap and close the file */
562 gst_file_src_stop (GstBaseSrc * basesrc)
564 GstFileSrc *src = GST_FILE_SRC (basesrc);
569 /* zero out a lot of our state */
571 src->is_regular = FALSE;
576 /*** GSTURIHANDLER INTERFACE *************************************************/
579 gst_file_src_uri_get_type (GType type)
584 static const gchar *const *
585 gst_file_src_uri_get_protocols (GType type)
587 static const gchar *protocols[] = { "file", NULL };
593 gst_file_src_uri_get_uri (GstURIHandler * handler)
595 GstFileSrc *src = GST_FILE_SRC (handler);
597 /* FIXME: make thread-safe */
598 return g_strdup (src->uri);
602 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
605 gchar *location, *hostname = NULL;
606 gboolean ret = FALSE;
607 GstFileSrc *src = GST_FILE_SRC (handler);
609 if (strcmp (uri, "file://") == 0) {
610 /* Special case for "file://" as this is used by some applications
611 * to test with gst_element_make_from_uri if there's an element
612 * that supports the URI protocol. */
613 gst_file_src_set_location (src, NULL);
617 location = g_filename_from_uri (uri, &hostname, err);
619 if (!location || (err != NULL && *err != NULL)) {
620 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
621 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
625 if ((hostname) && (strcmp (hostname, "localhost"))) {
626 /* Only 'localhost' is permitted */
627 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
628 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
629 "File URI with invalid hostname '%s'", hostname);
633 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
634 * correctly on windows, it leaves them with an extra backslash
635 * at the start if they're of the mozilla-style file://///host/path/file
636 * form. Correct this.
638 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
639 g_memmove (location, location + 1, strlen (location + 1) + 1);
642 ret = gst_file_src_set_location (src, location);
654 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
656 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
658 iface->get_type = gst_file_src_uri_get_type;
659 iface->get_protocols = gst_file_src_uri_get_protocols;
660 iface->get_uri = gst_file_src_uri_get_uri;
661 iface->set_uri = gst_file_src_uri_set_uri;