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
136 static void gst_file_src_finalize (GObject * object);
138 static void gst_file_src_set_property (GObject * object, guint prop_id,
139 const GValue * value, GParamSpec * pspec);
140 static void gst_file_src_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec);
143 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
144 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
146 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
147 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
148 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
149 guint length, GstBuffer * buf);
150 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
152 static void gst_file_src_uri_handler_init (gpointer g_iface,
153 gpointer iface_data);
156 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
157 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
158 #define gst_file_src_parent_class parent_class
159 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
162 gst_file_src_class_init (GstFileSrcClass * klass)
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
166 GstBaseSrcClass *gstbasesrc_class;
168 gobject_class = G_OBJECT_CLASS (klass);
169 gstelement_class = GST_ELEMENT_CLASS (klass);
170 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
172 gobject_class->set_property = gst_file_src_set_property;
173 gobject_class->get_property = gst_file_src_get_property;
175 g_object_class_install_property (gobject_class, PROP_LOCATION,
176 g_param_spec_string ("location", "File Location",
177 "Location of the file to read", NULL,
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
179 GST_PARAM_MUTABLE_READY));
181 gobject_class->finalize = gst_file_src_finalize;
183 gst_element_class_set_details_simple (gstelement_class,
186 "Read from arbitrary point in a file",
187 "Erik Walthinsen <omega@cse.ogi.edu>");
188 gst_element_class_add_pad_template (gstelement_class,
189 gst_static_pad_template_get (&srctemplate));
191 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
192 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
193 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
194 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
195 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
196 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
198 if (sizeof (off_t) < 8) {
199 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
205 gst_file_src_init (GstFileSrc * src)
207 src->filename = NULL;
211 src->is_regular = FALSE;
215 gst_file_src_finalize (GObject * object)
219 src = GST_FILE_SRC (object);
221 g_free (src->filename);
224 G_OBJECT_CLASS (parent_class)->finalize (object);
228 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
232 /* the element must be stopped in order to do this */
233 GST_OBJECT_LOCK (src);
234 state = GST_STATE (src);
235 if (state != GST_STATE_READY && state != GST_STATE_NULL)
237 GST_OBJECT_UNLOCK (src);
239 g_free (src->filename);
242 /* clear the filename if we get a NULL (is that possible?) */
243 if (location == NULL) {
244 src->filename = NULL;
247 /* we store the filename as received by the application. On Windows this
249 src->filename = g_strdup (location);
250 src->uri = gst_filename_to_uri (location, NULL);
251 GST_INFO ("filename : %s", src->filename);
252 GST_INFO ("uri : %s", src->uri);
254 g_object_notify (G_OBJECT (src), "location");
255 /* FIXME 0.11: notify "uri" property once there is one */
262 g_warning ("Changing the `location' property on filesrc when a file is "
263 "open is not supported.");
264 GST_OBJECT_UNLOCK (src);
270 gst_file_src_set_property (GObject * object, guint prop_id,
271 const GValue * value, GParamSpec * pspec)
275 g_return_if_fail (GST_IS_FILE_SRC (object));
277 src = GST_FILE_SRC (object);
281 gst_file_src_set_location (src, g_value_get_string (value));
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
295 g_return_if_fail (GST_IS_FILE_SRC (object));
297 src = GST_FILE_SRC (object);
301 g_value_set_string (value, src->filename);
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311 * that is to say, you shouldn't read the code below, but the code that reads
312 * stuff is below. Well, you shouldn't not read the code below, feel free
313 * to read it of course. It's just that "read code below" is a pretty crappy
314 * documentation string because it sounds like we're expecting you to read
315 * the code to understand what it does, which, while true, is really not
316 * the sort of attitude we want to be advertising. No sir.
320 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
324 guint to_read, bytes_read;
328 src = GST_FILE_SRC_CAST (basesrc);
330 if (G_UNLIKELY (src->read_position != offset)) {
333 res = lseek (src->fd, offset, SEEK_SET);
334 if (G_UNLIKELY (res < 0 || res != offset))
337 src->read_position = offset;
340 data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
344 while (to_read > 0) {
345 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
346 to_read, offset + bytes_read);
348 ret = read (src->fd, data + bytes_read, to_read);
349 if (G_UNLIKELY (ret < 0)) {
350 if (errno == EAGAIN || errno == EINTR)
355 /* files should eos if they read 0 and more was requested */
356 if (G_UNLIKELY (ret == 0)) {
357 /* .. but first we should return any remaining data */
366 src->read_position += ret;
369 gst_buffer_unmap (buf, data, bytes_read);
371 GST_BUFFER_OFFSET (buf) = offset;
372 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
379 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
380 return GST_FLOW_ERROR;
384 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
385 gst_buffer_unmap (buf, data, 0);
386 return GST_FLOW_ERROR;
391 gst_buffer_unmap (buf, data, 0);
397 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
399 gboolean ret = FALSE;
400 GstFileSrc *src = GST_FILE_SRC (basesrc);
402 switch (GST_QUERY_TYPE (query)) {
404 gst_query_set_uri (query, src->uri);
413 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
419 gst_file_src_is_seekable (GstBaseSrc * basesrc)
421 GstFileSrc *src = GST_FILE_SRC (basesrc);
423 return src->seekable;
427 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
429 struct stat stat_results;
432 src = GST_FILE_SRC (basesrc);
434 if (!src->seekable) {
435 /* If it isn't seekable, we won't know the length (but fstat will still
436 * succeed, and wrongly say our length is zero. */
440 if (fstat (src->fd, &stat_results) < 0)
443 *size = stat_results.st_size;
454 /* open the file and mmap it, necessary to go to READY state */
456 gst_file_src_start (GstBaseSrc * basesrc)
458 GstFileSrc *src = GST_FILE_SRC (basesrc);
459 struct stat stat_results;
461 if (src->filename == NULL || src->filename[0] == '\0')
464 GST_INFO_OBJECT (src, "opening file %s", src->filename);
467 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
472 /* check if it is a regular file, otherwise bail out */
473 if (fstat (src->fd, &stat_results) < 0)
476 if (S_ISDIR (stat_results.st_mode))
479 if (S_ISSOCK (stat_results.st_mode))
482 src->read_position = 0;
484 /* record if it's a regular (hence seekable and lengthable) file */
485 if (S_ISREG (stat_results.st_mode))
486 src->is_regular = TRUE;
488 /* We need to check if the underlying file is seekable. */
490 off_t res = lseek (src->fd, 0, SEEK_END);
493 GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
494 "failed: %s", g_strerror (errno));
495 src->seekable = FALSE;
497 src->seekable = TRUE;
499 lseek (src->fd, 0, SEEK_SET);
502 /* We can only really do seeking on regular files - for other file types, we
503 * don't know their length, so seeking isn't useful/meaningful */
504 src->seekable = src->seekable && src->is_regular;
506 gst_base_src_set_dynamic_size (basesrc, src->seekable);
513 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
514 (_("No file name specified for reading.")), (NULL));
521 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
522 ("No such file \"%s\"", src->filename));
525 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
526 (_("Could not open file \"%s\" for reading."), src->filename),
534 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
535 (_("Could not get info on \"%s\"."), src->filename), (NULL));
540 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
541 (_("\"%s\" is a directory."), src->filename), (NULL));
546 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
547 (_("File \"%s\" is a socket."), src->filename), (NULL));
556 /* unmap and close the file */
558 gst_file_src_stop (GstBaseSrc * basesrc)
560 GstFileSrc *src = GST_FILE_SRC (basesrc);
565 /* zero out a lot of our state */
567 src->is_regular = FALSE;
572 /*** GSTURIHANDLER INTERFACE *************************************************/
575 gst_file_src_uri_get_type (GType type)
580 static const gchar *const *
581 gst_file_src_uri_get_protocols (GType type)
583 static const gchar *protocols[] = { "file", NULL };
589 gst_file_src_uri_get_uri (GstURIHandler * handler)
591 GstFileSrc *src = GST_FILE_SRC (handler);
593 /* FIXME: make thread-safe */
594 return g_strdup (src->uri);
598 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
601 gchar *location, *hostname = NULL;
602 gboolean ret = FALSE;
603 GstFileSrc *src = GST_FILE_SRC (handler);
605 if (strcmp (uri, "file://") == 0) {
606 /* Special case for "file://" as this is used by some applications
607 * to test with gst_element_make_from_uri if there's an element
608 * that supports the URI protocol. */
609 gst_file_src_set_location (src, NULL);
613 location = g_filename_from_uri (uri, &hostname, err);
615 if (!location || (err != NULL && *err != NULL)) {
616 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
617 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
621 if ((hostname) && (strcmp (hostname, "localhost"))) {
622 /* Only 'localhost' is permitted */
623 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
624 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
625 "File URI with invalid hostname '%s'", hostname);
629 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
630 * correctly on windows, it leaves them with an extra backslash
631 * at the start if they're of the mozilla-style file://///host/path/file
632 * form. Correct this.
634 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
635 g_memmove (location, location + 1, strlen (location + 1) + 1);
638 ret = gst_file_src_set_location (src, location);
650 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
652 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
654 iface->get_type = gst_file_src_uri_get_type;
655 iface->get_protocols = gst_file_src_uri_get_protocols;
656 iface->get_uri = gst_file_src_uri_get_uri;
657 iface->set_uri = gst_file_src_uri_set_uri;