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 <glib/gstdio.h>
42 #include "gstfilesrc.h"
43 #include "gstcoreelementselements.h"
46 #include <sys/types.h>
50 #include <io.h> /* lseek, open, close, read */
51 /* On win32, stat* default to 32 bit; we need the 64-bit
52 * variants, so explicitly define it that way. */
54 #define lseek _lseeki64
57 /* Prevent stat.h from defining the stat* functions as
58 * _stat*, since we're explicitly overriding that */
68 #define struct_stat struct stat
70 #ifdef __BIONIC__ /* Android */
71 #if defined(__ANDROID_API__) && __ANDROID_API__ >= 21
75 #define struct_stat struct stat64
82 #include "../../gst/gst-i18n-lib.h"
84 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90 #define S_ISREG(mode) ((mode)&_S_IFREG)
93 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
96 #define S_ISSOCK(x) (0)
102 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
103 #define GST_CAT_DEFAULT gst_file_src_debug
105 /* FileSrc signals and args */
112 #define DEFAULT_BLOCKSIZE 4*1024
120 static void gst_file_src_finalize (GObject * object);
122 static void gst_file_src_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_file_src_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
128 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
130 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
131 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
132 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
133 guint length, GstBuffer * buf);
135 static void gst_file_src_uri_handler_init (gpointer g_iface,
136 gpointer iface_data);
139 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
140 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
141 #define gst_file_src_parent_class parent_class
142 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
143 GST_ELEMENT_REGISTER_DEFINE (filesrc, "filesrc", GST_RANK_PRIMARY,
147 gst_file_src_class_init (GstFileSrcClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
151 GstBaseSrcClass *gstbasesrc_class;
153 gobject_class = G_OBJECT_CLASS (klass);
154 gstelement_class = GST_ELEMENT_CLASS (klass);
155 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
157 gobject_class->set_property = gst_file_src_set_property;
158 gobject_class->get_property = gst_file_src_get_property;
160 g_object_class_install_property (gobject_class, PROP_LOCATION,
161 g_param_spec_string ("location", "File Location",
162 "Location of the file to read", NULL,
163 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
164 GST_PARAM_MUTABLE_READY));
166 gobject_class->finalize = gst_file_src_finalize;
168 gst_element_class_set_static_metadata (gstelement_class,
171 "Read from arbitrary point in a file",
172 "Erik Walthinsen <omega@cse.ogi.edu>");
173 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
175 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
176 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
177 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
178 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
179 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
181 if (sizeof (off_t) < 8) {
182 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
188 gst_file_src_init (GstFileSrc * src)
190 src->filename = NULL;
194 src->is_regular = FALSE;
196 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
200 gst_file_src_finalize (GObject * object)
204 src = GST_FILE_SRC (object);
206 g_free (src->filename);
209 G_OBJECT_CLASS (parent_class)->finalize (object);
213 gst_file_src_set_location (GstFileSrc * src, const gchar * location,
218 /* the element must be stopped in order to do this */
219 GST_OBJECT_LOCK (src);
220 state = GST_STATE (src);
221 if (state != GST_STATE_READY && state != GST_STATE_NULL)
223 GST_OBJECT_UNLOCK (src);
225 g_free (src->filename);
228 /* clear the filename if we get a NULL */
229 if (location == NULL) {
230 src->filename = NULL;
233 /* we store the filename as received by the application. On Windows this
235 src->filename = g_strdup (location);
236 src->uri = gst_filename_to_uri (location, NULL);
237 GST_INFO ("filename : %s", src->filename);
238 GST_INFO ("uri : %s", src->uri);
240 g_object_notify (G_OBJECT (src), "location");
241 /* FIXME 2.0: notify "uri" property once there is one */
248 g_warning ("Changing the `location' property on filesrc when a file is "
249 "open is not supported.");
251 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
252 "Changing the `location' property on filesrc when a file is "
253 "open is not supported.");
254 GST_OBJECT_UNLOCK (src);
260 gst_file_src_set_property (GObject * object, guint prop_id,
261 const GValue * value, GParamSpec * pspec)
265 g_return_if_fail (GST_IS_FILE_SRC (object));
267 src = GST_FILE_SRC (object);
271 gst_file_src_set_location (src, g_value_get_string (value), NULL);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
285 g_return_if_fail (GST_IS_FILE_SRC (object));
287 src = GST_FILE_SRC (object);
291 g_value_set_string (value, src->filename);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 * that is to say, you shouldn't read the code below, but the code that reads
302 * stuff is below. Well, you shouldn't not read the code below, feel free
303 * to read it of course. It's just that "read code below" is a pretty crappy
304 * documentation string because it sounds like we're expecting you to read
305 * the code to understand what it does, which, while true, is really not
306 * the sort of attitude we want to be advertising. No sir.
310 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
314 guint to_read, bytes_read;
319 src = GST_FILE_SRC_CAST (basesrc);
321 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
324 res = lseek (src->fd, offset, SEEK_SET);
325 if (G_UNLIKELY (res < 0 || res != offset))
328 src->read_position = offset;
331 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
332 goto buffer_write_fail;
337 while (to_read > 0) {
338 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
339 to_read, offset + bytes_read);
341 ret = read (src->fd, data + bytes_read, to_read);
342 if (G_UNLIKELY (ret < 0)) {
343 if (errno == EAGAIN || errno == EINTR)
348 /* files should eos if they read 0 and more was requested */
349 if (G_UNLIKELY (ret == 0)) {
350 /* .. but first we should return any remaining data */
359 src->read_position += ret;
362 gst_buffer_unmap (buf, &info);
363 if (bytes_read != length)
364 gst_buffer_resize (buf, 0, bytes_read);
366 GST_BUFFER_OFFSET (buf) = offset;
367 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
374 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
375 return GST_FLOW_ERROR;
379 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
380 gst_buffer_unmap (buf, &info);
381 gst_buffer_resize (buf, 0, 0);
382 return GST_FLOW_ERROR;
387 gst_buffer_unmap (buf, &info);
388 gst_buffer_resize (buf, 0, 0);
393 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
394 return GST_FLOW_ERROR;
399 gst_file_src_is_seekable (GstBaseSrc * basesrc)
401 GstFileSrc *src = GST_FILE_SRC (basesrc);
403 return src->seekable;
407 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
411 src = GST_FILE_SRC (basesrc);
413 if (!src->seekable) {
414 /* If it isn't seekable, we won't know the length (but fstat will still
415 * succeed, and wrongly say our length is zero. */
420 HANDLE h = (HANDLE) _get_osfhandle (src->fd);
421 LARGE_INTEGER file_size;
423 if (h == INVALID_HANDLE_VALUE)
426 if (!GetFileSizeEx (h, &file_size)) {
430 *size = file_size.QuadPart;
434 struct_stat stat_results;
436 if (fstat (src->fd, &stat_results) < 0)
439 *size = stat_results.st_size;
452 /* open the file, necessary to go to READY state */
454 gst_file_src_start (GstBaseSrc * basesrc)
456 GstFileSrc *src = GST_FILE_SRC (basesrc);
457 int flags = O_RDONLY | O_BINARY;
458 #if defined (__BIONIC__)
459 flags |= O_LARGEFILE;
462 if (src->filename == NULL || src->filename[0] == '\0')
465 GST_INFO_OBJECT (src, "opening file %s", src->filename);
468 src->fd = g_open (src->filename, flags, 0);
475 HANDLE h = (HANDLE) _get_osfhandle (src->fd);
476 FILE_STANDARD_INFO file_info;
478 if (h == INVALID_HANDLE_VALUE)
481 if (!GetFileInformationByHandleEx (h, FileStandardInfo, &file_info,
482 sizeof (FILE_STANDARD_INFO)))
485 if (file_info.Directory)
488 /* everything's a regular file on Windows */
489 src->is_regular = TRUE;
493 struct_stat stat_results;
495 /* check if it is a regular file, otherwise bail out */
496 if (fstat (src->fd, &stat_results) < 0)
499 if (S_ISDIR (stat_results.st_mode))
502 if (S_ISSOCK (stat_results.st_mode))
505 /* record if it's a regular (hence seekable and lengthable) file */
506 if (S_ISREG (stat_results.st_mode))
507 src->is_regular = TRUE;
511 src->read_position = 0;
513 /* We need to check if the underlying file is seekable. */
515 off_t res = lseek (src->fd, 0, SEEK_END);
518 GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
520 src->seekable = FALSE;
522 res = lseek (src->fd, 0, SEEK_SET);
525 /* We really don't like not being able to go back to 0 */
526 src->seekable = FALSE;
530 src->seekable = TRUE;
534 /* We can only really do seeking on regular files - for other file types, we
535 * don't know their length, so seeking isn't useful/meaningful */
536 src->seekable = src->seekable && src->is_regular;
538 gst_base_src_set_dynamic_size (basesrc, src->seekable);
545 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
546 (_("No file name specified for reading.")), (NULL));
553 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
554 ("No such file \"%s\"", src->filename));
557 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
558 (_("Could not open file \"%s\" for reading."), src->filename),
566 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
567 (_("Could not get info on \"%s\"."), src->filename), (NULL));
572 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
573 (_("\"%s\" is a directory."), src->filename), (NULL));
579 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
580 (_("File \"%s\" is a socket."), src->filename), (NULL));
586 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
587 ("Could not seek back to zero after seek test in file \"%s\"",
597 /* unmap and close the file */
599 gst_file_src_stop (GstBaseSrc * basesrc)
601 GstFileSrc *src = GST_FILE_SRC (basesrc);
604 g_close (src->fd, NULL);
606 /* zero out a lot of our state */
608 src->is_regular = FALSE;
613 /*** GSTURIHANDLER INTERFACE *************************************************/
616 gst_file_src_uri_get_type (GType type)
621 static const gchar *const *
622 gst_file_src_uri_get_protocols (GType type)
624 static const gchar *protocols[] = { "file", NULL };
630 gst_file_src_uri_get_uri (GstURIHandler * handler)
632 GstFileSrc *src = GST_FILE_SRC (handler);
634 /* FIXME: make thread-safe */
635 return g_strdup (src->uri);
639 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
642 gchar *location, *hostname = NULL;
643 gboolean ret = FALSE;
644 GstFileSrc *src = GST_FILE_SRC (handler);
646 if (strcmp (uri, "file://") == 0) {
647 /* Special case for "file://" as this is used by some applications
648 * to test with gst_element_make_from_uri if there's an element
649 * that supports the URI protocol. */
650 gst_file_src_set_location (src, NULL, NULL);
654 location = g_filename_from_uri (uri, &hostname, err);
656 if (!location || (err != NULL && *err != NULL)) {
657 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
658 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
662 if ((hostname) && (strcmp (hostname, "localhost"))) {
663 /* Only 'localhost' is permitted */
664 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
665 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
666 "File URI with invalid hostname '%s'", hostname);
670 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
671 * correctly on windows, it leaves them with an extra backslash
672 * at the start if they're of the mozilla-style file://///host/path/file
673 * form. Correct this.
675 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
676 memmove (location, location + 1, strlen (location + 1) + 1);
679 ret = gst_file_src_set_location (src, location, err);
691 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
693 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
695 iface->get_type = gst_file_src_uri_get_type;
696 iface->get_protocols = gst_file_src_uri_get_protocols;
697 iface->get_uri = gst_file_src_uri_get_uri;
698 iface->set_uri = gst_file_src_uri_set_uri;