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 ! 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);
153 static void gst_file_src_uri_handler_init (gpointer g_iface,
154 gpointer iface_data);
157 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
158 GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
159 #define gst_file_src_parent_class parent_class
160 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
163 gst_file_src_class_init (GstFileSrcClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseSrcClass *gstbasesrc_class;
169 gobject_class = G_OBJECT_CLASS (klass);
170 gstelement_class = GST_ELEMENT_CLASS (klass);
171 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
173 gobject_class->set_property = gst_file_src_set_property;
174 gobject_class->get_property = gst_file_src_get_property;
176 g_object_class_install_property (gobject_class, PROP_LOCATION,
177 g_param_spec_string ("location", "File Location",
178 "Location of the file to read", NULL,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
180 GST_PARAM_MUTABLE_READY));
182 gobject_class->finalize = gst_file_src_finalize;
184 gst_element_class_set_static_metadata (gstelement_class,
187 "Read from arbitrary point in a file",
188 "Erik Walthinsen <omega@cse.ogi.edu>");
189 gst_element_class_add_pad_template (gstelement_class,
190 gst_static_pad_template_get (&srctemplate));
192 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
193 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
194 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
195 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
196 gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
198 if (sizeof (off_t) < 8) {
199 GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
205 gst_file_src_init (GstFileSrc * src)
207 src->filename = NULL;
211 src->is_regular = FALSE;
213 gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
217 gst_file_src_finalize (GObject * object)
221 src = GST_FILE_SRC (object);
223 g_free (src->filename);
226 G_OBJECT_CLASS (parent_class)->finalize (object);
230 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
234 /* the element must be stopped in order to do this */
235 GST_OBJECT_LOCK (src);
236 state = GST_STATE (src);
237 if (state != GST_STATE_READY && state != GST_STATE_NULL)
239 GST_OBJECT_UNLOCK (src);
241 g_free (src->filename);
244 /* clear the filename if we get a NULL */
245 if (location == NULL) {
246 src->filename = NULL;
249 /* we store the filename as received by the application. On Windows this
251 src->filename = g_strdup (location);
252 src->uri = gst_filename_to_uri (location, NULL);
253 GST_INFO ("filename : %s", src->filename);
254 GST_INFO ("uri : %s", src->uri);
256 g_object_notify (G_OBJECT (src), "location");
257 /* FIXME 0.11: notify "uri" property once there is one */
264 g_warning ("Changing the `location' property on filesrc when a file is "
265 "open is not supported.");
266 GST_OBJECT_UNLOCK (src);
272 gst_file_src_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec)
277 g_return_if_fail (GST_IS_FILE_SRC (object));
279 src = GST_FILE_SRC (object);
283 gst_file_src_set_location (src, g_value_get_string (value));
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
297 g_return_if_fail (GST_IS_FILE_SRC (object));
299 src = GST_FILE_SRC (object);
303 g_value_set_string (value, src->filename);
306 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 * that is to say, you shouldn't read the code below, but the code that reads
314 * stuff is below. Well, you shouldn't not read the code below, feel free
315 * to read it of course. It's just that "read code below" is a pretty crappy
316 * documentation string because it sounds like we're expecting you to read
317 * the code to understand what it does, which, while true, is really not
318 * the sort of attitude we want to be advertising. No sir.
322 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
326 guint to_read, bytes_read;
331 src = GST_FILE_SRC_CAST (basesrc);
333 if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
336 res = lseek (src->fd, offset, SEEK_SET);
337 if (G_UNLIKELY (res < 0 || res != offset))
340 src->read_position = offset;
343 gst_buffer_map (buf, &info, GST_MAP_WRITE);
348 while (to_read > 0) {
349 GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
350 to_read, offset + bytes_read);
352 ret = read (src->fd, data + bytes_read, to_read);
353 if (G_UNLIKELY (ret < 0)) {
354 if (errno == EAGAIN || errno == EINTR)
359 /* files should eos if they read 0 and more was requested */
360 if (G_UNLIKELY (ret == 0)) {
361 /* .. but first we should return any remaining data */
370 src->read_position += ret;
373 gst_buffer_unmap (buf, &info);
374 if (bytes_read != length)
375 gst_buffer_resize (buf, 0, bytes_read);
377 GST_BUFFER_OFFSET (buf) = offset;
378 GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
385 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
386 return GST_FLOW_ERROR;
390 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
391 gst_buffer_unmap (buf, &info);
392 gst_buffer_resize (buf, 0, 0);
393 return GST_FLOW_ERROR;
398 gst_buffer_unmap (buf, &info);
399 gst_buffer_resize (buf, 0, 0);
405 gst_file_src_is_seekable (GstBaseSrc * basesrc)
407 GstFileSrc *src = GST_FILE_SRC (basesrc);
409 return src->seekable;
413 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
415 struct stat stat_results;
418 src = GST_FILE_SRC (basesrc);
420 if (!src->seekable) {
421 /* If it isn't seekable, we won't know the length (but fstat will still
422 * succeed, and wrongly say our length is zero. */
426 if (fstat (src->fd, &stat_results) < 0)
429 *size = stat_results.st_size;
440 /* open the file, necessary to go to READY state */
442 gst_file_src_start (GstBaseSrc * basesrc)
444 GstFileSrc *src = GST_FILE_SRC (basesrc);
445 struct stat stat_results;
447 if (src->filename == NULL || src->filename[0] == '\0')
450 GST_INFO_OBJECT (src, "opening file %s", src->filename);
453 src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
458 /* check if it is a regular file, otherwise bail out */
459 if (fstat (src->fd, &stat_results) < 0)
462 if (S_ISDIR (stat_results.st_mode))
465 if (S_ISSOCK (stat_results.st_mode))
468 src->read_position = 0;
470 /* record if it's a regular (hence seekable and lengthable) file */
471 if (S_ISREG (stat_results.st_mode))
472 src->is_regular = TRUE;
474 /* We need to check if the underlying file is seekable. */
476 off_t res = lseek (src->fd, 0, SEEK_END);
479 GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
481 src->seekable = FALSE;
483 src->seekable = TRUE;
485 lseek (src->fd, 0, SEEK_SET);
488 /* We can only really do seeking on regular files - for other file types, we
489 * don't know their length, so seeking isn't useful/meaningful */
490 src->seekable = src->seekable && src->is_regular;
492 gst_base_src_set_dynamic_size (basesrc, src->seekable);
499 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
500 (_("No file name specified for reading.")), (NULL));
507 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
508 ("No such file \"%s\"", src->filename));
511 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
512 (_("Could not open file \"%s\" for reading."), src->filename),
520 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
521 (_("Could not get info on \"%s\"."), src->filename), (NULL));
526 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
527 (_("\"%s\" is a directory."), src->filename), (NULL));
532 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
533 (_("File \"%s\" is a socket."), src->filename), (NULL));
542 /* unmap and close the file */
544 gst_file_src_stop (GstBaseSrc * basesrc)
546 GstFileSrc *src = GST_FILE_SRC (basesrc);
551 /* zero out a lot of our state */
553 src->is_regular = FALSE;
558 /*** GSTURIHANDLER INTERFACE *************************************************/
561 gst_file_src_uri_get_type (GType type)
566 static const gchar *const *
567 gst_file_src_uri_get_protocols (GType type)
569 static const gchar *protocols[] = { "file", NULL };
575 gst_file_src_uri_get_uri (GstURIHandler * handler)
577 GstFileSrc *src = GST_FILE_SRC (handler);
579 /* FIXME: make thread-safe */
580 return g_strdup (src->uri);
584 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
587 gchar *location, *hostname = NULL;
588 gboolean ret = FALSE;
589 GstFileSrc *src = GST_FILE_SRC (handler);
591 if (strcmp (uri, "file://") == 0) {
592 /* Special case for "file://" as this is used by some applications
593 * to test with gst_element_make_from_uri if there's an element
594 * that supports the URI protocol. */
595 gst_file_src_set_location (src, NULL);
599 location = g_filename_from_uri (uri, &hostname, err);
601 if (!location || (err != NULL && *err != NULL)) {
602 GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
603 (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
607 if ((hostname) && (strcmp (hostname, "localhost"))) {
608 /* Only 'localhost' is permitted */
609 GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
610 g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
611 "File URI with invalid hostname '%s'", hostname);
615 /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
616 * correctly on windows, it leaves them with an extra backslash
617 * at the start if they're of the mozilla-style file://///host/path/file
618 * form. Correct this.
620 if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
621 g_memmove (location, location + 1, strlen (location + 1) + 1);
624 ret = gst_file_src_set_location (src, location);
636 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
638 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
640 iface->get_type = gst_file_src_uri_get_type;
641 iface->get_protocols = gst_file_src_uri_get_protocols;
642 iface->get_uri = gst_file_src_uri_get_uri;
643 iface->set_uri = gst_file_src_uri_set_uri;