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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:element-filesrc
25 * @see_also: #GstFileSrc
27 * Read data from a file in the local file system.
29 * ## Example launch line
31 * gst-launch-1.0 filesrc location=song.ogg ! decodebin ! audioconvert ! audioresample ! autoaudiosink
32 * ]| Play song.ogg audio file which must be in the current working directory.
41 #include "gstfilesrc.h"
44 #include <sys/types.h>
47 #include <io.h> /* lseek, open, close, read */
48 /* On win32, stat* default to 32 bit; we need the 64-bit
49 * variants, so explicitly define it that way. */
53 #define fstat _fstat64
55 #define lseek _lseeki64
58 /* Prevent stat.h from defining the stat* functions as
59 * _stat*, since we're explicitly overriding that */
68 #ifdef __BIONIC__ /* Android */
69 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
78 #include "../../gst/gst-i18n-lib.h"
80 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
86 #define S_ISREG(mode) ((mode)&_S_IFREG)
89 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
92 #define S_ISSOCK(x) (0)
98 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
99 * use the 'file descriptor' opened in glib (and returned from this function)
100 * in this library, as they may have unrelated C runtimes. */
102 gst_open (const gchar * filename, int flags, int mode)
105 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
109 if (wfilename == NULL) {
114 retval = _wopen (wfilename, flags, mode);
121 #elif defined (__BIONIC__)
122 return open (filename, flags | O_LARGEFILE, mode);
124 return open (filename, flags, mode);
128 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
129 #define GST_CAT_DEFAULT gst_file_src_debug
131 /* FileSrc signals and args */
138 #define DEFAULT_BLOCKSIZE 4*1024
146 static void gst_file_src_finalize (GObject * object);
148 static void gst_file_src_set_property (GObject * object, guint prop_id,
149 const GValue * value, GParamSpec * pspec);
150 static void gst_file_src_get_property (GObject * object, guint prop_id,
151 GValue * value, GParamSpec * pspec);
153 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
154 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
156 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
157 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
158 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
159 guint length, GstBuffer * buf);
161 static void gst_file_src_uri_handler_init (gpointer g_iface,
162 gpointer iface_data);
165 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
166 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
167 #define gst_file_src_parent_class parent_class
168 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
171 gst_file_src_class_init (GstFileSrcClass * klass)
173 GObjectClass *gobject_class;
174 GstElementClass *gstelement_class;
175 GstBaseSrcClass *gstbasesrc_class;
177 gobject_class = G_OBJECT_CLASS (klass);
178 gstelement_class = GST_ELEMENT_CLASS (klass);
179 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
181 gobject_class->set_property = gst_file_src_set_property;
182 gobject_class->get_property = gst_file_src_get_property;
184 g_object_class_install_property (gobject_class, PROP_LOCATION,
185 g_param_spec_string ("location", "File Location",
186 "Location of the file to read", NULL,
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
188 GST_PARAM_MUTABLE_READY));
190 gobject_class->finalize = gst_file_src_finalize;
192 gst_element_class_set_static_metadata (gstelement_class,
195 "Read from arbitrary point in a file",
196 "Erik Walthinsen <omega@cse.ogi.edu>");
197 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
199 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
200 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
201 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
202 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
203 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
205 if (sizeof (off_t) < 8) {
206 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
212 gst_file_src_init (GstFileSrc * src)
214 src->filename = NULL;
218 src->is_regular = FALSE;
220 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
224 gst_file_src_finalize (GObject * object)
228 src = GST_FILE_SRC (object);
230 g_free (src->filename);
233 G_OBJECT_CLASS (parent_class)->finalize (object);
237 gst_file_src_set_location (GstFileSrc * src, const gchar * location,
242 /* the element must be stopped in order to do this */
243 GST_OBJECT_LOCK (src);
244 state = GST_STATE (src);
245 if (state != GST_STATE_READY && state != GST_STATE_NULL)
247 GST_OBJECT_UNLOCK (src);
249 g_free (src->filename);
252 /* clear the filename if we get a NULL */
253 if (location == NULL) {
254 src->filename = NULL;
257 /* we store the filename as received by the application. On Windows this
259 src->filename = g_strdup (location);
260 src->uri = gst_filename_to_uri (location, NULL);
261 GST_INFO ("filename : %s", src->filename);
262 GST_INFO ("uri : %s", src->uri);
264 g_object_notify (G_OBJECT (src), "location");
265 /* FIXME 2.0: notify "uri" property once there is one */
272 g_warning ("Changing the `location' property on filesrc when a file is "
273 "open is not supported.");
275 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
276 "Changing the `location' property on filesrc when a file is "
277 "open is not supported.");
278 GST_OBJECT_UNLOCK (src);
284 gst_file_src_set_property (GObject * object, guint prop_id,
285 const GValue * value, GParamSpec * pspec)
289 g_return_if_fail (GST_IS_FILE_SRC (object));
291 src = GST_FILE_SRC (object);
295 gst_file_src_set_location (src, g_value_get_string (value), NULL);
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
309 g_return_if_fail (GST_IS_FILE_SRC (object));
311 src = GST_FILE_SRC (object);
315 g_value_set_string (value, src->filename);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325 * that is to say, you shouldn't read the code below, but the code that reads
326 * stuff is below. Well, you shouldn't not read the code below, feel free
327 * to read it of course. It's just that "read code below" is a pretty crappy
328 * documentation string because it sounds like we're expecting you to read
329 * the code to understand what it does, which, while true, is really not
330 * the sort of attitude we want to be advertising. No sir.
334 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
338 guint to_read, bytes_read;
343 src = GST_FILE_SRC_CAST (basesrc);
345 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
348 res = lseek (src->fd, offset, SEEK_SET);
349 if (G_UNLIKELY (res < 0 || res != offset))
352 src->read_position = offset;
355 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
356 goto buffer_write_fail;
361 while (to_read > 0) {
362 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
363 to_read, offset + bytes_read);
365 ret = read (src->fd, data + bytes_read, to_read);
366 if (G_UNLIKELY (ret < 0)) {
367 if (errno == EAGAIN || errno == EINTR)
372 /* files should eos if they read 0 and more was requested */
373 if (G_UNLIKELY (ret == 0)) {
374 /* .. but first we should return any remaining data */
383 src->read_position += ret;
386 gst_buffer_unmap (buf, &info);
387 if (bytes_read != length)
388 gst_buffer_resize (buf, 0, bytes_read);
390 GST_BUFFER_OFFSET (buf) = offset;
391 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
398 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
399 return GST_FLOW_ERROR;
403 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
404 gst_buffer_unmap (buf, &info);
405 gst_buffer_resize (buf, 0, 0);
406 return GST_FLOW_ERROR;
411 gst_buffer_unmap (buf, &info);
412 gst_buffer_resize (buf, 0, 0);
417 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
418 return GST_FLOW_ERROR;
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, 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, lseek failed: %s",
499 src->seekable = FALSE;
501 res = lseek (src->fd, 0, SEEK_SET);
504 /* We really don't like not being able to go back to 0 */
505 src->seekable = FALSE;
509 src->seekable = TRUE;
513 /* We can only really do seeking on regular files - for other file types, we
514 * don't know their length, so seeking isn't useful/meaningful */
515 src->seekable = src->seekable && src->is_regular;
517 gst_base_src_set_dynamic_size (basesrc, src->seekable);
524 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
525 (_("No file name specified for reading.")), (NULL));
532 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
533 ("No such file \"%s\"", src->filename));
536 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
537 (_("Could not open file \"%s\" for reading."), src->filename),
545 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
546 (_("Could not get info on \"%s\"."), src->filename), (NULL));
551 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
552 (_("\"%s\" is a directory."), src->filename), (NULL));
557 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
558 (_("File \"%s\" is a socket."), src->filename), (NULL));
563 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
564 ("Could not seek back to zero after seek test in file \"%s\"",
574 /* unmap and close the file */
576 gst_file_src_stop (GstBaseSrc * basesrc)
578 GstFileSrc *src = GST_FILE_SRC (basesrc);
583 /* zero out a lot of our state */
585 src->is_regular = FALSE;
590 /*** GSTURIHANDLER INTERFACE *************************************************/
593 gst_file_src_uri_get_type (GType type)
598 static const gchar *const *
599 gst_file_src_uri_get_protocols (GType type)
601 static const gchar *protocols[] = { "file", NULL };
607 gst_file_src_uri_get_uri (GstURIHandler * handler)
609 GstFileSrc *src = GST_FILE_SRC (handler);
611 /* FIXME: make thread-safe */
612 return g_strdup (src->uri);
616 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
619 gchar *location, *hostname = NULL;
620 gboolean ret = FALSE;
621 GstFileSrc *src = GST_FILE_SRC (handler);
623 if (strcmp (uri, "file://") == 0) {
624 /* Special case for "file://" as this is used by some applications
625 * to test with gst_element_make_from_uri if there's an element
626 * that supports the URI protocol. */
627 gst_file_src_set_location (src, NULL, NULL);
631 location = g_filename_from_uri (uri, &hostname, err);
633 if (!location || (err != NULL && *err != NULL)) {
634 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
635 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
639 if ((hostname) && (strcmp (hostname, "localhost"))) {
640 /* Only 'localhost' is permitted */
641 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
642 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
643 "File URI with invalid hostname '%s'", hostname);
647 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
648 * correctly on windows, it leaves them with an extra backslash
649 * at the start if they're of the mozilla-style file://///host/path/file
650 * form. Correct this.
652 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
653 memmove (location, location + 1, strlen (location + 1) + 1);
656 ret = gst_file_src_set_location (src, location, err);
668 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
670 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
672 iface->get_type = gst_file_src_uri_get_type;
673 iface->get_protocols = gst_file_src_uri_get_protocols;
674 iface->get_uri = gst_file_src_uri_get_uri;
675 iface->set_uri = gst_file_src_uri_set_uri;