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,
335 src = GST_FILE_SRC_CAST (basesrc);
337 if (G_UNLIKELY (src->read_position != offset)) {
340 res = lseek (src->fd, offset, SEEK_SET);
341 if (G_UNLIKELY (res < 0 || res != offset))
344 src->read_position = offset;
347 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
349 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
352 ret = read (src->fd, data, length);
353 if (G_UNLIKELY (ret < 0))
356 /* seekable regular files should have given us what we expected */
357 if (G_UNLIKELY ((guint) ret < length && src->seekable))
360 /* other files should eos if they read 0 and more was requested */
361 if (G_UNLIKELY (ret == 0))
366 gst_buffer_unmap (buf, data, length);
368 GST_BUFFER_OFFSET (buf) = offset;
369 GST_BUFFER_OFFSET_END (buf) = offset + length;
371 src->read_position += length;
378 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
379 return GST_FLOW_ERROR;
383 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
384 gst_buffer_unmap (buf, data, 0);
385 return GST_FLOW_ERROR;
389 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
390 ("unexpected end of file."));
391 gst_buffer_unmap (buf, data, 0);
392 return GST_FLOW_ERROR;
396 GST_DEBUG ("non-regular file hits EOS");
397 gst_buffer_unmap (buf, data, 0);
403 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
405 gboolean ret = FALSE;
406 GstFileSrc *src = GST_FILE_SRC (basesrc);
408 switch (GST_QUERY_TYPE (query)) {
410 gst_query_set_uri (query, src->uri);
419 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
425 gst_file_src_is_seekable (GstBaseSrc * basesrc)
427 GstFileSrc *src = GST_FILE_SRC (basesrc);
429 return src->seekable;
433 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
435 struct stat stat_results;
438 src = GST_FILE_SRC (basesrc);
440 if (!src->seekable) {
441 /* If it isn't seekable, we won't know the length (but fstat will still
442 * succeed, and wrongly say our length is zero. */
446 if (fstat (src->fd, &stat_results) < 0)
449 *size = stat_results.st_size;
460 /* open the file and mmap it, necessary to go to READY state */
462 gst_file_src_start (GstBaseSrc * basesrc)
464 GstFileSrc *src = GST_FILE_SRC (basesrc);
465 struct stat stat_results;
467 if (src->filename == NULL || src->filename[0] == '\0')
470 GST_INFO_OBJECT (src, "opening file %s", src->filename);
473 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
478 /* check if it is a regular file, otherwise bail out */
479 if (fstat (src->fd, &stat_results) < 0)
482 if (S_ISDIR (stat_results.st_mode))
485 if (S_ISSOCK (stat_results.st_mode))
488 src->read_position = 0;
490 /* record if it's a regular (hence seekable and lengthable) file */
491 if (S_ISREG (stat_results.st_mode))
492 src->is_regular = TRUE;
494 /* We need to check if the underlying file is seekable. */
496 off_t res = lseek (src->fd, 0, SEEK_END);
499 GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
500 "failed: %s", g_strerror (errno));
501 src->seekable = FALSE;
503 src->seekable = TRUE;
505 lseek (src->fd, 0, SEEK_SET);
508 /* We can only really do seeking on regular files - for other file types, we
509 * don't know their length, so seeking isn't useful/meaningful */
510 src->seekable = src->seekable && src->is_regular;
512 gst_base_src_set_dynamic_size (basesrc, src->seekable);
519 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
520 (_("No file name specified for reading.")), (NULL));
527 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
528 ("No such file \"%s\"", src->filename));
531 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
532 (_("Could not open file \"%s\" for reading."), src->filename),
540 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
541 (_("Could not get info on \"%s\"."), src->filename), (NULL));
547 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
548 (_("\"%s\" is a directory."), src->filename), (NULL));
554 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
555 (_("File \"%s\" is a socket."), src->filename), (NULL));
561 /* unmap and close the file */
563 gst_file_src_stop (GstBaseSrc * basesrc)
565 GstFileSrc *src = GST_FILE_SRC (basesrc);
570 /* zero out a lot of our state */
572 src->is_regular = FALSE;
577 /*** GSTURIHANDLER INTERFACE *************************************************/
580 gst_file_src_uri_get_type (GType type)
585 static const gchar *const *
586 gst_file_src_uri_get_protocols (GType type)
588 static const gchar *protocols[] = { "file", NULL };
594 gst_file_src_uri_get_uri (GstURIHandler * handler)
596 GstFileSrc *src = GST_FILE_SRC (handler);
598 /* FIXME: make thread-safe */
599 return g_strdup (src->uri);
603 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
606 gchar *location, *hostname = NULL;
607 gboolean ret = FALSE;
608 GstFileSrc *src = GST_FILE_SRC (handler);
610 if (strcmp (uri, "file://") == 0) {
611 /* Special case for "file://" as this is used by some applications
612 * to test with gst_element_make_from_uri if there's an element
613 * that supports the URI protocol. */
614 gst_file_src_set_location (src, NULL);
618 location = g_filename_from_uri (uri, &hostname, err);
620 if (!location || (err != NULL && *err != NULL)) {
621 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
622 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
626 if ((hostname) && (strcmp (hostname, "localhost"))) {
627 /* Only 'localhost' is permitted */
628 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
629 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
630 "File URI with invalid hostname '%s'", hostname);
634 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
635 * correctly on windows, it leaves them with an extra backslash
636 * at the start if they're of the mozilla-style file://///host/path/file
637 * form. Correct this.
639 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
640 g_memmove (location, location + 1, strlen (location + 1) + 1);
643 ret = gst_file_src_set_location (src, location);
655 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
657 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
659 iface->get_type = gst_file_src_uri_get_type;
660 iface->get_protocols = gst_file_src_uri_get_protocols;
661 iface->get_uri = gst_file_src_uri_get_uri;
662 iface->set_uri = gst_file_src_uri_set_uri;