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
24 * @see_also: #GstFileSrc
26 * Read data from a file in the local file system.
29 * <title>Example launch line</title>
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 */
80 #include "../../gst/gst-i18n-lib.h"
82 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
88 #define S_ISREG(mode) ((mode)&_S_IFREG)
91 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
94 #define S_ISSOCK(x) (0)
100 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
101 * use the 'file descriptor' opened in glib (and returned from this function)
102 * in this library, as they may have unrelated C runtimes. */
104 gst_open (const gchar * filename, int flags, int mode)
107 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
111 if (wfilename == NULL) {
116 retval = _wopen (wfilename, flags, mode);
123 #elif defined (__BIONIC__)
124 return open (filename, flags | O_LARGEFILE, mode);
126 return open (filename, flags, mode);
130 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
131 #define GST_CAT_DEFAULT gst_file_src_debug
133 /* FileSrc signals and args */
140 #define DEFAULT_BLOCKSIZE 4*1024
148 static void gst_file_src_finalize (GObject * object);
150 static void gst_file_src_set_property (GObject * object, guint prop_id,
151 const GValue * value, GParamSpec * pspec);
152 static void gst_file_src_get_property (GObject * object, guint prop_id,
153 GValue * value, GParamSpec * pspec);
155 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
156 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
158 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
159 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
160 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
161 guint length, GstBuffer * buf);
163 static void gst_file_src_uri_handler_init (gpointer g_iface,
164 gpointer iface_data);
167 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
168 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
169 #define gst_file_src_parent_class parent_class
170 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
173 gst_file_src_class_init (GstFileSrcClass * klass)
175 GObjectClass *gobject_class;
176 GstElementClass *gstelement_class;
177 GstBaseSrcClass *gstbasesrc_class;
179 gobject_class = G_OBJECT_CLASS (klass);
180 gstelement_class = GST_ELEMENT_CLASS (klass);
181 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
183 gobject_class->set_property = gst_file_src_set_property;
184 gobject_class->get_property = gst_file_src_get_property;
186 g_object_class_install_property (gobject_class, PROP_LOCATION,
187 g_param_spec_string ("location", "File Location",
188 "Location of the file to read", NULL,
189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
190 GST_PARAM_MUTABLE_READY));
192 gobject_class->finalize = gst_file_src_finalize;
194 gst_element_class_set_static_metadata (gstelement_class,
197 "Read from arbitrary point in a file",
198 "Erik Walthinsen <omega@cse.ogi.edu>");
199 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
201 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
202 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
203 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
204 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
205 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
207 if (sizeof (off_t) < 8) {
208 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
214 gst_file_src_init (GstFileSrc * src)
216 src->filename = NULL;
220 src->is_regular = FALSE;
222 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
226 gst_file_src_finalize (GObject * object)
230 src = GST_FILE_SRC (object);
232 g_free (src->filename);
235 G_OBJECT_CLASS (parent_class)->finalize (object);
239 gst_file_src_set_location (GstFileSrc * src, const gchar * location,
244 /* the element must be stopped in order to do this */
245 GST_OBJECT_LOCK (src);
246 state = GST_STATE (src);
247 if (state != GST_STATE_READY && state != GST_STATE_NULL)
249 GST_OBJECT_UNLOCK (src);
251 g_free (src->filename);
254 /* clear the filename if we get a NULL */
255 if (location == NULL) {
256 src->filename = NULL;
259 /* we store the filename as received by the application. On Windows this
261 src->filename = g_strdup (location);
262 src->uri = gst_filename_to_uri (location, NULL);
263 GST_INFO ("filename : %s", src->filename);
264 GST_INFO ("uri : %s", src->uri);
266 g_object_notify (G_OBJECT (src), "location");
267 /* FIXME 2.0: notify "uri" property once there is one */
274 g_warning ("Changing the `location' property on filesrc when a file is "
275 "open is not supported.");
277 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
278 "Changing the `location' property on filesrc when a file is "
279 "open is not supported.");
280 GST_OBJECT_UNLOCK (src);
286 gst_file_src_set_property (GObject * object, guint prop_id,
287 const GValue * value, GParamSpec * pspec)
291 g_return_if_fail (GST_IS_FILE_SRC (object));
293 src = GST_FILE_SRC (object);
297 gst_file_src_set_location (src, g_value_get_string (value), NULL);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
311 g_return_if_fail (GST_IS_FILE_SRC (object));
313 src = GST_FILE_SRC (object);
317 g_value_set_string (value, src->filename);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 * that is to say, you shouldn't read the code below, but the code that reads
328 * stuff is below. Well, you shouldn't not read the code below, feel free
329 * to read it of course. It's just that "read code below" is a pretty crappy
330 * documentation string because it sounds like we're expecting you to read
331 * the code to understand what it does, which, while true, is really not
332 * the sort of attitude we want to be advertising. No sir.
336 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
340 guint to_read, bytes_read;
345 src = GST_FILE_SRC_CAST (basesrc);
347 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
350 res = lseek (src->fd, offset, SEEK_SET);
351 if (G_UNLIKELY (res < 0 || res != offset))
354 src->read_position = offset;
357 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
358 goto buffer_write_fail;
363 while (to_read > 0) {
364 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
365 to_read, offset + bytes_read);
367 ret = read (src->fd, data + bytes_read, to_read);
368 if (G_UNLIKELY (ret < 0)) {
369 if (errno == EAGAIN || errno == EINTR)
374 /* files should eos if they read 0 and more was requested */
375 if (G_UNLIKELY (ret == 0)) {
376 /* .. but first we should return any remaining data */
385 src->read_position += ret;
388 gst_buffer_unmap (buf, &info);
389 if (bytes_read != length)
390 gst_buffer_resize (buf, 0, bytes_read);
392 GST_BUFFER_OFFSET (buf) = offset;
393 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
400 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
401 return GST_FLOW_ERROR;
405 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
406 gst_buffer_unmap (buf, &info);
407 gst_buffer_resize (buf, 0, 0);
408 return GST_FLOW_ERROR;
413 gst_buffer_unmap (buf, &info);
414 gst_buffer_resize (buf, 0, 0);
419 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
420 return GST_FLOW_ERROR;
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, 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, lseek failed: %s",
501 src->seekable = FALSE;
503 res = lseek (src->fd, 0, SEEK_SET);
506 /* We really don't like not being able to go back to 0 */
507 src->seekable = FALSE;
511 src->seekable = TRUE;
515 /* We can only really do seeking on regular files - for other file types, we
516 * don't know their length, so seeking isn't useful/meaningful */
517 src->seekable = src->seekable && src->is_regular;
519 gst_base_src_set_dynamic_size (basesrc, src->seekable);
526 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
527 (_("No file name specified for reading.")), (NULL));
534 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
535 ("No such file \"%s\"", src->filename));
538 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
539 (_("Could not open file \"%s\" for reading."), src->filename),
547 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
548 (_("Could not get info on \"%s\"."), src->filename), (NULL));
553 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
554 (_("\"%s\" is a directory."), src->filename), (NULL));
559 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
560 (_("File \"%s\" is a socket."), src->filename), (NULL));
565 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
566 ("Could not seek back to zero after seek test in file \"%s\"",
576 /* unmap and close the file */
578 gst_file_src_stop (GstBaseSrc * basesrc)
580 GstFileSrc *src = GST_FILE_SRC (basesrc);
585 /* zero out a lot of our state */
587 src->is_regular = FALSE;
592 /*** GSTURIHANDLER INTERFACE *************************************************/
595 gst_file_src_uri_get_type (GType type)
600 static const gchar *const *
601 gst_file_src_uri_get_protocols (GType type)
603 static const gchar *protocols[] = { "file", NULL };
609 gst_file_src_uri_get_uri (GstURIHandler * handler)
611 GstFileSrc *src = GST_FILE_SRC (handler);
613 /* FIXME: make thread-safe */
614 return g_strdup (src->uri);
618 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
621 gchar *location, *hostname = NULL;
622 gboolean ret = FALSE;
623 GstFileSrc *src = GST_FILE_SRC (handler);
625 if (strcmp (uri, "file://") == 0) {
626 /* Special case for "file://" as this is used by some applications
627 * to test with gst_element_make_from_uri if there's an element
628 * that supports the URI protocol. */
629 gst_file_src_set_location (src, NULL, NULL);
633 location = g_filename_from_uri (uri, &hostname, err);
635 if (!location || (err != NULL && *err != NULL)) {
636 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
637 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
641 if ((hostname) && (strcmp (hostname, "localhost"))) {
642 /* Only 'localhost' is permitted */
643 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
644 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
645 "File URI with invalid hostname '%s'", hostname);
649 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
650 * correctly on windows, it leaves them with an extra backslash
651 * at the start if they're of the mozilla-style file://///host/path/file
652 * form. Correct this.
654 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
655 memmove (location, location + 1, strlen (location + 1) + 1);
658 ret = gst_file_src_set_location (src, location, err);
670 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
672 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
674 iface->get_type = gst_file_src_uri_get_type;
675 iface->get_protocols = gst_file_src_uri_get_protocols;
676 iface->get_uri = gst_file_src_uri_get_uri;
677 iface->set_uri = gst_file_src_uri_set_uri;