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)
243 /* the element must be stopped in order to do this */
244 GST_OBJECT_LOCK (src);
245 state = GST_STATE (src);
246 if (state != GST_STATE_READY && state != GST_STATE_NULL)
248 GST_OBJECT_UNLOCK (src);
250 g_free (src->filename);
253 /* clear the filename if we get a NULL */
254 if (location == NULL) {
255 src->filename = NULL;
258 /* we store the filename as received by the application. On Windows this
260 src->filename = g_strdup (location);
261 src->uri = gst_filename_to_uri (location, NULL);
262 GST_INFO ("filename : %s", src->filename);
263 GST_INFO ("uri : %s", src->uri);
265 g_object_notify (G_OBJECT (src), "location");
266 /* FIXME 2.0: notify "uri" property once there is one */
273 g_warning ("Changing the `location' property on filesrc when a file is "
274 "open is not supported.");
275 GST_OBJECT_UNLOCK (src);
281 gst_file_src_set_property (GObject * object, guint prop_id,
282 const GValue * value, GParamSpec * pspec)
286 g_return_if_fail (GST_IS_FILE_SRC (object));
288 src = GST_FILE_SRC (object);
292 gst_file_src_set_location (src, g_value_get_string (value));
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
306 g_return_if_fail (GST_IS_FILE_SRC (object));
308 src = GST_FILE_SRC (object);
312 g_value_set_string (value, src->filename);
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 * that is to say, you shouldn't read the code below, but the code that reads
323 * stuff is below. Well, you shouldn't not read the code below, feel free
324 * to read it of course. It's just that "read code below" is a pretty crappy
325 * documentation string because it sounds like we're expecting you to read
326 * the code to understand what it does, which, while true, is really not
327 * the sort of attitude we want to be advertising. No sir.
331 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
335 guint to_read, bytes_read;
340 src = GST_FILE_SRC_CAST (basesrc);
342 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
345 res = lseek (src->fd, offset, SEEK_SET);
346 if (G_UNLIKELY (res < 0 || res != offset))
349 src->read_position = offset;
352 if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
353 goto buffer_write_fail;
358 while (to_read > 0) {
359 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
360 to_read, offset + bytes_read);
362 ret = read (src->fd, data + bytes_read, to_read);
363 if (G_UNLIKELY (ret < 0)) {
364 if (errno == EAGAIN || errno == EINTR)
369 /* files should eos if they read 0 and more was requested */
370 if (G_UNLIKELY (ret == 0)) {
371 /* .. but first we should return any remaining data */
380 src->read_position += ret;
383 gst_buffer_unmap (buf, &info);
384 if (bytes_read != length)
385 gst_buffer_resize (buf, 0, bytes_read);
387 GST_BUFFER_OFFSET (buf) = offset;
388 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
395 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
396 return GST_FLOW_ERROR;
400 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
401 gst_buffer_unmap (buf, &info);
402 gst_buffer_resize (buf, 0, 0);
403 return GST_FLOW_ERROR;
408 gst_buffer_unmap (buf, &info);
409 gst_buffer_resize (buf, 0, 0);
414 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
415 return GST_FLOW_ERROR;
420 gst_file_src_is_seekable (GstBaseSrc * basesrc)
422 GstFileSrc *src = GST_FILE_SRC (basesrc);
424 return src->seekable;
428 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
430 struct stat stat_results;
433 src = GST_FILE_SRC (basesrc);
435 if (!src->seekable) {
436 /* If it isn't seekable, we won't know the length (but fstat will still
437 * succeed, and wrongly say our length is zero. */
441 if (fstat (src->fd, &stat_results) < 0)
444 *size = stat_results.st_size;
455 /* open the file, necessary to go to READY state */
457 gst_file_src_start (GstBaseSrc * basesrc)
459 GstFileSrc *src = GST_FILE_SRC (basesrc);
460 struct stat stat_results;
462 if (src->filename == NULL || src->filename[0] == '\0')
465 GST_INFO_OBJECT (src, "opening file %s", src->filename);
468 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
473 /* check if it is a regular file, otherwise bail out */
474 if (fstat (src->fd, &stat_results) < 0)
477 if (S_ISDIR (stat_results.st_mode))
480 if (S_ISSOCK (stat_results.st_mode))
483 src->read_position = 0;
485 /* record if it's a regular (hence seekable and lengthable) file */
486 if (S_ISREG (stat_results.st_mode))
487 src->is_regular = TRUE;
489 /* We need to check if the underlying file is seekable. */
491 off_t res = lseek (src->fd, 0, SEEK_END);
494 GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
496 src->seekable = FALSE;
498 res = lseek (src->fd, 0, SEEK_SET);
501 /* We really don't like not being able to go back to 0 */
502 src->seekable = FALSE;
506 src->seekable = TRUE;
510 /* We can only really do seeking on regular files - for other file types, we
511 * don't know their length, so seeking isn't useful/meaningful */
512 src->seekable = src->seekable && src->is_regular;
514 gst_base_src_set_dynamic_size (basesrc, src->seekable);
521 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
522 (_("No file name specified for reading.")), (NULL));
529 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
530 ("No such file \"%s\"", src->filename));
533 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
534 (_("Could not open file \"%s\" for reading."), src->filename),
542 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
543 (_("Could not get info on \"%s\"."), src->filename), (NULL));
548 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
549 (_("\"%s\" is a directory."), src->filename), (NULL));
554 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
555 (_("File \"%s\" is a socket."), src->filename), (NULL));
560 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
561 ("Could not seek back to zero after seek test in file \"%s\"",
571 /* unmap and close the file */
573 gst_file_src_stop (GstBaseSrc * basesrc)
575 GstFileSrc *src = GST_FILE_SRC (basesrc);
580 /* zero out a lot of our state */
582 src->is_regular = FALSE;
587 /*** GSTURIHANDLER INTERFACE *************************************************/
590 gst_file_src_uri_get_type (GType type)
595 static const gchar *const *
596 gst_file_src_uri_get_protocols (GType type)
598 static const gchar *protocols[] = { "file", NULL };
604 gst_file_src_uri_get_uri (GstURIHandler * handler)
606 GstFileSrc *src = GST_FILE_SRC (handler);
608 /* FIXME: make thread-safe */
609 return g_strdup (src->uri);
613 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
616 gchar *location, *hostname = NULL;
617 gboolean ret = FALSE;
618 GstFileSrc *src = GST_FILE_SRC (handler);
620 if (strcmp (uri, "file://") == 0) {
621 /* Special case for "file://" as this is used by some applications
622 * to test with gst_element_make_from_uri if there's an element
623 * that supports the URI protocol. */
624 gst_file_src_set_location (src, NULL);
628 location = g_filename_from_uri (uri, &hostname, err);
630 if (!location || (err != NULL && *err != NULL)) {
631 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
632 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
636 if ((hostname) && (strcmp (hostname, "localhost"))) {
637 /* Only 'localhost' is permitted */
638 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
639 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
640 "File URI with invalid hostname '%s'", hostname);
644 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
645 * correctly on windows, it leaves them with an extra backslash
646 * at the start if they're of the mozilla-style file://///host/path/file
647 * form. Correct this.
649 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
650 memmove (location, location + 1, strlen (location + 1) + 1);
653 ret = gst_file_src_set_location (src, location);
665 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
667 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
669 iface->get_type = gst_file_src_uri_get_type;
670 iface->get_protocols = gst_file_src_uri_get_protocols;
671 iface->get_uri = gst_file_src_uri_get_uri;
672 iface->set_uri = gst_file_src_uri_set_uri;