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 filesrc location=song.ogg ! decodebin ! 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",
79 #define S_ISREG(mode) ((mode)&_S_IFREG)
82 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
85 #define S_ISSOCK(x) (0)
91 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
92 * use the 'file descriptor' opened in glib (and returned from this function)
93 * in this library, as they may have unrelated C runtimes. */
95 gst_open (const gchar * filename, int flags, int mode)
98 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
102 if (wfilename == NULL) {
107 retval = _wopen (wfilename, flags, mode);
115 return open (filename, flags, mode);
119 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
120 #define GST_CAT_DEFAULT gst_file_src_debug
122 /* FileSrc signals and args */
129 #define DEFAULT_BLOCKSIZE 4*1024
137 static void gst_file_src_finalize (GObject * object);
139 static void gst_file_src_set_property (GObject * object, guint prop_id,
140 const GValue * value, GParamSpec * pspec);
141 static void gst_file_src_get_property (GObject * object, guint prop_id,
142 GValue * value, GParamSpec * pspec);
144 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
145 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
147 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
148 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
149 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
150 guint length, GstBuffer * buf);
152 static void gst_file_src_uri_handler_init (gpointer g_iface,
153 gpointer iface_data);
156 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
157 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
158 #define gst_file_src_parent_class parent_class
159 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
162 gst_file_src_class_init (GstFileSrcClass * klass)
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
166 GstBaseSrcClass *gstbasesrc_class;
168 gobject_class = G_OBJECT_CLASS (klass);
169 gstelement_class = GST_ELEMENT_CLASS (klass);
170 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
172 gobject_class->set_property = gst_file_src_set_property;
173 gobject_class->get_property = gst_file_src_get_property;
175 g_object_class_install_property (gobject_class, PROP_LOCATION,
176 g_param_spec_string ("location", "File Location",
177 "Location of the file to read", NULL,
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
179 GST_PARAM_MUTABLE_READY));
181 gobject_class->finalize = gst_file_src_finalize;
183 gst_element_class_set_static_metadata (gstelement_class,
186 "Read from arbitrary point in a file",
187 "Erik Walthinsen <omega@cse.ogi.edu>");
188 gst_element_class_add_pad_template (gstelement_class,
189 gst_static_pad_template_get (&srctemplate));
191 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
192 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
193 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
194 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
195 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
197 if (sizeof (off_t) < 8) {
198 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
204 gst_file_src_init (GstFileSrc * src)
206 src->filename = NULL;
210 src->is_regular = FALSE;
212 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
216 gst_file_src_finalize (GObject * object)
220 src = GST_FILE_SRC (object);
222 g_free (src->filename);
225 G_OBJECT_CLASS (parent_class)->finalize (object);
229 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
233 /* the element must be stopped in order to do this */
234 GST_OBJECT_LOCK (src);
235 state = GST_STATE (src);
236 if (state != GST_STATE_READY && state != GST_STATE_NULL)
238 GST_OBJECT_UNLOCK (src);
240 g_free (src->filename);
243 /* clear the filename if we get a NULL */
244 if (location == NULL) {
245 src->filename = NULL;
248 /* we store the filename as received by the application. On Windows this
250 src->filename = g_strdup (location);
251 src->uri = gst_filename_to_uri (location, NULL);
252 GST_INFO ("filename : %s", src->filename);
253 GST_INFO ("uri : %s", src->uri);
255 g_object_notify (G_OBJECT (src), "location");
256 /* FIXME 2.0: notify "uri" property once there is one */
263 g_warning ("Changing the `location' property on filesrc when a file is "
264 "open is not supported.");
265 GST_OBJECT_UNLOCK (src);
271 gst_file_src_set_property (GObject * object, guint prop_id,
272 const GValue * value, GParamSpec * pspec)
276 g_return_if_fail (GST_IS_FILE_SRC (object));
278 src = GST_FILE_SRC (object);
282 gst_file_src_set_location (src, g_value_get_string (value));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
296 g_return_if_fail (GST_IS_FILE_SRC (object));
298 src = GST_FILE_SRC (object);
302 g_value_set_string (value, src->filename);
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312 * that is to say, you shouldn't read the code below, but the code that reads
313 * stuff is below. Well, you shouldn't not read the code below, feel free
314 * to read it of course. It's just that "read code below" is a pretty crappy
315 * documentation string because it sounds like we're expecting you to read
316 * the code to understand what it does, which, while true, is really not
317 * the sort of attitude we want to be advertising. No sir.
321 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
325 guint to_read, bytes_read;
330 src = GST_FILE_SRC_CAST (basesrc);
332 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
335 res = lseek (src->fd, offset, SEEK_SET);
336 if (G_UNLIKELY (res < 0 || res != offset))
339 src->read_position = offset;
342 gst_buffer_map (buf, &info, GST_MAP_WRITE);
347 while (to_read > 0) {
348 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
349 to_read, offset + bytes_read);
351 ret = read (src->fd, data + bytes_read, to_read);
352 if (G_UNLIKELY (ret < 0)) {
353 if (errno == EAGAIN || errno == EINTR)
358 /* files should eos if they read 0 and more was requested */
359 if (G_UNLIKELY (ret == 0)) {
360 /* .. but first we should return any remaining data */
369 src->read_position += ret;
372 gst_buffer_unmap (buf, &info);
373 if (bytes_read != length)
374 gst_buffer_resize (buf, 0, bytes_read);
376 GST_BUFFER_OFFSET (buf) = offset;
377 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
384 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
385 return GST_FLOW_ERROR;
389 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
390 gst_buffer_unmap (buf, &info);
391 gst_buffer_resize (buf, 0, 0);
392 return GST_FLOW_ERROR;
397 gst_buffer_unmap (buf, &info);
398 gst_buffer_resize (buf, 0, 0);
404 gst_file_src_is_seekable (GstBaseSrc * basesrc)
406 GstFileSrc *src = GST_FILE_SRC (basesrc);
408 return src->seekable;
412 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
414 struct stat stat_results;
417 src = GST_FILE_SRC (basesrc);
419 if (!src->seekable) {
420 /* If it isn't seekable, we won't know the length (but fstat will still
421 * succeed, and wrongly say our length is zero. */
425 if (fstat (src->fd, &stat_results) < 0)
428 *size = stat_results.st_size;
439 /* open the file, necessary to go to READY state */
441 gst_file_src_start (GstBaseSrc * basesrc)
443 GstFileSrc *src = GST_FILE_SRC (basesrc);
444 struct stat stat_results;
446 if (src->filename == NULL || src->filename[0] == '\0')
449 GST_INFO_OBJECT (src, "opening file %s", src->filename);
452 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
457 /* check if it is a regular file, otherwise bail out */
458 if (fstat (src->fd, &stat_results) < 0)
461 if (S_ISDIR (stat_results.st_mode))
464 if (S_ISSOCK (stat_results.st_mode))
467 src->read_position = 0;
469 /* record if it's a regular (hence seekable and lengthable) file */
470 if (S_ISREG (stat_results.st_mode))
471 src->is_regular = TRUE;
473 /* We need to check if the underlying file is seekable. */
475 off_t res = lseek (src->fd, 0, SEEK_END);
478 GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
480 src->seekable = FALSE;
482 res = lseek (src->fd, 0, SEEK_SET);
485 /* We really don't like not being able to go back to 0 */
486 src->seekable = FALSE;
490 src->seekable = TRUE;
494 /* We can only really do seeking on regular files - for other file types, we
495 * don't know their length, so seeking isn't useful/meaningful */
496 src->seekable = src->seekable && src->is_regular;
498 gst_base_src_set_dynamic_size (basesrc, src->seekable);
505 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
506 (_("No file name specified for reading.")), (NULL));
513 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
514 ("No such file \"%s\"", src->filename));
517 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
518 (_("Could not open file \"%s\" for reading."), src->filename),
526 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
527 (_("Could not get info on \"%s\"."), src->filename), (NULL));
532 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
533 (_("\"%s\" is a directory."), src->filename), (NULL));
538 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
539 (_("File \"%s\" is a socket."), src->filename), (NULL));
544 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
545 ("Could not seek back to zero after seek test in file \"%s\"",
555 /* unmap and close the file */
557 gst_file_src_stop (GstBaseSrc * basesrc)
559 GstFileSrc *src = GST_FILE_SRC (basesrc);
564 /* zero out a lot of our state */
566 src->is_regular = FALSE;
571 /*** GSTURIHANDLER INTERFACE *************************************************/
574 gst_file_src_uri_get_type (GType type)
579 static const gchar *const *
580 gst_file_src_uri_get_protocols (GType type)
582 static const gchar *protocols[] = { "file", NULL };
588 gst_file_src_uri_get_uri (GstURIHandler * handler)
590 GstFileSrc *src = GST_FILE_SRC (handler);
592 /* FIXME: make thread-safe */
593 return g_strdup (src->uri);
597 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
600 gchar *location, *hostname = NULL;
601 gboolean ret = FALSE;
602 GstFileSrc *src = GST_FILE_SRC (handler);
604 if (strcmp (uri, "file://") == 0) {
605 /* Special case for "file://" as this is used by some applications
606 * to test with gst_element_make_from_uri if there's an element
607 * that supports the URI protocol. */
608 gst_file_src_set_location (src, NULL);
612 location = g_filename_from_uri (uri, &hostname, err);
614 if (!location || (err != NULL && *err != NULL)) {
615 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
616 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
620 if ((hostname) && (strcmp (hostname, "localhost"))) {
621 /* Only 'localhost' is permitted */
622 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
623 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
624 "File URI with invalid hostname '%s'", hostname);
628 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
629 * correctly on windows, it leaves them with an extra backslash
630 * at the start if they're of the mozilla-style file://///host/path/file
631 * form. Correct this.
633 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
634 memmove (location, location + 1, strlen (location + 1) + 1);
637 ret = gst_file_src_set_location (src, location);
649 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
651 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
653 iface->get_type = gst_file_src_uri_get_type;
654 iface->get_protocols = gst_file_src_uri_get_protocols;
655 iface->get_uri = gst_file_src_uri_get_uri;
656 iface->set_uri = gst_file_src_uri_set_uri;