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>
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 */
71 #include "../../gst/gst-i18n-lib.h"
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
78 /* FIXME we should be using glib for this */
80 #define S_ISREG(mode) ((mode)&_S_IFREG)
83 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
86 #define S_ISSOCK(x) (0)
92 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
93 * use the 'file descriptor' opened in glib (and returned from this function)
94 * in this library, as they may have unrelated C runtimes. */
96 gst_open (const gchar * filename, int flags, int mode)
99 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
103 if (wfilename == NULL) {
108 retval = _wopen (wfilename, flags, mode);
116 return open (filename, flags, mode);
120 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
121 #define GST_CAT_DEFAULT gst_file_src_debug
123 /* FileSrc signals and args */
130 #define DEFAULT_BLOCKSIZE 4*1024
138 static void gst_file_src_finalize (GObject * object);
140 static void gst_file_src_set_property (GObject * object, guint prop_id,
141 const GValue * value, GParamSpec * pspec);
142 static void gst_file_src_get_property (GObject * object, guint prop_id,
143 GValue * value, GParamSpec * pspec);
145 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
146 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
148 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
149 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
150 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
151 guint length, GstBuffer * buf);
152 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
154 static void gst_file_src_uri_handler_init (gpointer g_iface,
155 gpointer iface_data);
158 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
159 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
160 #define gst_file_src_parent_class parent_class
161 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
164 gst_file_src_class_init (GstFileSrcClass * klass)
166 GObjectClass *gobject_class;
167 GstElementClass *gstelement_class;
168 GstBaseSrcClass *gstbasesrc_class;
170 gobject_class = G_OBJECT_CLASS (klass);
171 gstelement_class = GST_ELEMENT_CLASS (klass);
172 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
174 gobject_class->set_property = gst_file_src_set_property;
175 gobject_class->get_property = gst_file_src_get_property;
177 g_object_class_install_property (gobject_class, PROP_LOCATION,
178 g_param_spec_string ("location", "File Location",
179 "Location of the file to read", NULL,
180 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
181 GST_PARAM_MUTABLE_READY));
183 gobject_class->finalize = gst_file_src_finalize;
185 gst_element_class_set_static_metadata (gstelement_class,
188 "Read from arbitrary point in a file",
189 "Erik Walthinsen <omega@cse.ogi.edu>");
190 gst_element_class_add_pad_template (gstelement_class,
191 gst_static_pad_template_get (&srctemplate));
193 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
194 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
195 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
196 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
197 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
198 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
200 if (sizeof (off_t) < 8) {
201 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
207 gst_file_src_init (GstFileSrc * src)
209 src->filename = NULL;
213 src->is_regular = FALSE;
215 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
219 gst_file_src_finalize (GObject * object)
223 src = GST_FILE_SRC (object);
225 g_free (src->filename);
228 G_OBJECT_CLASS (parent_class)->finalize (object);
232 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
236 /* the element must be stopped in order to do this */
237 GST_OBJECT_LOCK (src);
238 state = GST_STATE (src);
239 if (state != GST_STATE_READY && state != GST_STATE_NULL)
241 GST_OBJECT_UNLOCK (src);
243 g_free (src->filename);
246 /* clear the filename if we get a NULL (is that possible?) */
247 if (location == NULL) {
248 src->filename = NULL;
251 /* we store the filename as received by the application. On Windows this
253 src->filename = g_strdup (location);
254 src->uri = gst_filename_to_uri (location, NULL);
255 GST_INFO ("filename : %s", src->filename);
256 GST_INFO ("uri : %s", src->uri);
258 g_object_notify (G_OBJECT (src), "location");
259 /* FIXME 0.11: notify "uri" property once there is one */
266 g_warning ("Changing the `location' property on filesrc when a file is "
267 "open is not supported.");
268 GST_OBJECT_UNLOCK (src);
274 gst_file_src_set_property (GObject * object, guint prop_id,
275 const GValue * value, GParamSpec * pspec)
279 g_return_if_fail (GST_IS_FILE_SRC (object));
281 src = GST_FILE_SRC (object);
285 gst_file_src_set_location (src, g_value_get_string (value));
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
299 g_return_if_fail (GST_IS_FILE_SRC (object));
301 src = GST_FILE_SRC (object);
305 g_value_set_string (value, src->filename);
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 * that is to say, you shouldn't read the code below, but the code that reads
316 * stuff is below. Well, you shouldn't not read the code below, feel free
317 * to read it of course. It's just that "read code below" is a pretty crappy
318 * documentation string because it sounds like we're expecting you to read
319 * the code to understand what it does, which, while true, is really not
320 * the sort of attitude we want to be advertising. No sir.
324 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
328 guint to_read, bytes_read;
333 src = GST_FILE_SRC_CAST (basesrc);
335 if (G_UNLIKELY (src->read_position != offset)) {
338 res = lseek (src->fd, offset, SEEK_SET);
339 if (G_UNLIKELY (res < 0 || res != offset))
342 src->read_position = offset;
345 gst_buffer_map (buf, &info, GST_MAP_WRITE);
350 while (to_read > 0) {
351 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
352 to_read, offset + bytes_read);
354 ret = read (src->fd, data + bytes_read, to_read);
355 if (G_UNLIKELY (ret < 0)) {
356 if (errno == EAGAIN || errno == EINTR)
361 /* files should eos if they read 0 and more was requested */
362 if (G_UNLIKELY (ret == 0)) {
363 /* .. but first we should return any remaining data */
372 src->read_position += ret;
375 gst_buffer_unmap (buf, &info);
376 if (bytes_read != length)
377 gst_buffer_resize (buf, 0, bytes_read);
379 GST_BUFFER_OFFSET (buf) = offset;
380 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
387 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
388 return GST_FLOW_ERROR;
392 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
393 gst_buffer_unmap (buf, &info);
394 gst_buffer_resize (buf, 0, 0);
395 return GST_FLOW_ERROR;
400 gst_buffer_unmap (buf, &info);
401 gst_buffer_resize (buf, 0, 0);
407 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
409 gboolean ret = FALSE;
410 GstFileSrc *src = GST_FILE_SRC (basesrc);
412 switch (GST_QUERY_TYPE (query)) {
414 gst_query_set_uri (query, src->uri);
423 ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
429 gst_file_src_is_seekable (GstBaseSrc * basesrc)
431 GstFileSrc *src = GST_FILE_SRC (basesrc);
433 return src->seekable;
437 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
439 struct stat stat_results;
442 src = GST_FILE_SRC (basesrc);
444 if (!src->seekable) {
445 /* If it isn't seekable, we won't know the length (but fstat will still
446 * succeed, and wrongly say our length is zero. */
450 if (fstat (src->fd, &stat_results) < 0)
453 *size = stat_results.st_size;
464 /* open the file, necessary to go to READY state */
466 gst_file_src_start (GstBaseSrc * basesrc)
468 GstFileSrc *src = GST_FILE_SRC (basesrc);
469 struct stat stat_results;
471 if (src->filename == NULL || src->filename[0] == '\0')
474 GST_INFO_OBJECT (src, "opening file %s", src->filename);
477 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
482 /* check if it is a regular file, otherwise bail out */
483 if (fstat (src->fd, &stat_results) < 0)
486 if (S_ISDIR (stat_results.st_mode))
489 if (S_ISSOCK (stat_results.st_mode))
492 src->read_position = 0;
494 /* record if it's a regular (hence seekable and lengthable) file */
495 if (S_ISREG (stat_results.st_mode))
496 src->is_regular = TRUE;
498 /* We need to check if the underlying file is seekable. */
500 off_t res = lseek (src->fd, 0, SEEK_END);
503 GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
505 src->seekable = FALSE;
507 src->seekable = TRUE;
509 lseek (src->fd, 0, SEEK_SET);
512 /* We can only really do seeking on regular files - for other file types, we
513 * don't know their length, so seeking isn't useful/meaningful */
514 src->seekable = src->seekable && src->is_regular;
516 gst_base_src_set_dynamic_size (basesrc, src->seekable);
523 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
524 (_("No file name specified for reading.")), (NULL));
531 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
532 ("No such file \"%s\"", src->filename));
535 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
536 (_("Could not open file \"%s\" for reading."), src->filename),
544 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
545 (_("Could not get info on \"%s\"."), src->filename), (NULL));
550 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
551 (_("\"%s\" is a directory."), src->filename), (NULL));
556 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
557 (_("File \"%s\" is a socket."), src->filename), (NULL));
566 /* unmap and close the file */
568 gst_file_src_stop (GstBaseSrc * basesrc)
570 GstFileSrc *src = GST_FILE_SRC (basesrc);
575 /* zero out a lot of our state */
577 src->is_regular = FALSE;
582 /*** GSTURIHANDLER INTERFACE *************************************************/
585 gst_file_src_uri_get_type (GType type)
590 static const gchar *const *
591 gst_file_src_uri_get_protocols (GType type)
593 static const gchar *protocols[] = { "file", NULL };
599 gst_file_src_uri_get_uri (GstURIHandler * handler)
601 GstFileSrc *src = GST_FILE_SRC (handler);
603 /* FIXME: make thread-safe */
604 return g_strdup (src->uri);
608 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
611 gchar *location, *hostname = NULL;
612 gboolean ret = FALSE;
613 GstFileSrc *src = GST_FILE_SRC (handler);
615 if (strcmp (uri, "file://") == 0) {
616 /* Special case for "file://" as this is used by some applications
617 * to test with gst_element_make_from_uri if there's an element
618 * that supports the URI protocol. */
619 gst_file_src_set_location (src, NULL);
623 location = g_filename_from_uri (uri, &hostname, err);
625 if (!location || (err != NULL && *err != NULL)) {
626 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
627 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
631 if ((hostname) && (strcmp (hostname, "localhost"))) {
632 /* Only 'localhost' is permitted */
633 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
634 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
635 "File URI with invalid hostname '%s'", hostname);
639 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
640 * correctly on windows, it leaves them with an extra backslash
641 * at the start if they're of the mozilla-style file://///host/path/file
642 * form. Correct this.
644 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
645 g_memmove (location, location + 1, strlen (location + 1) + 1);
648 ret = gst_file_src_set_location (src, location);
660 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
662 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
664 iface->get_type = gst_file_src_uri_get_type;
665 iface->get_protocols = gst_file_src_uri_get_protocols;
666 iface->get_uri = gst_file_src_uri_get_uri;
667 iface->set_uri = gst_file_src_uri_set_uri;