filesrc: remove FIXME
[platform/upstream/gstreamer.git] / plugins / elements / gstfilesrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstfilesrc.c:
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:element-filesrc
24  * @see_also: #GstFileSrc
25  *
26  * Read data from a file in the local file system.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
31  * gst-launch filesrc location=song.ogg ! decodebin ! autoaudiosink
32  * ]| Play a song.ogg from local dir.
33  * </refsect2>
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <gst/gst.h>
41 #include "gstfilesrc.h"
42
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #ifdef G_OS_WIN32
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. */
50 #undef stat
51 #define stat __stat64
52 #undef fstat
53 #define fstat _fstat64
54 #undef lseek
55 #define lseek _lseeki64
56 #undef off_t
57 #define off_t guint64
58 /* Prevent stat.h from defining the stat* functions as
59  * _stat*, since we're explicitly overriding that */
60 #undef _INC_STAT_INL
61 #endif
62 #include <fcntl.h>
63
64 #ifdef HAVE_UNISTD_H
65 #  include <unistd.h>
66 #endif
67
68 #include <errno.h>
69 #include <string.h>
70
71 #include "../../gst/gst-i18n-lib.h"
72
73 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS_ANY);
77
78 #ifndef S_ISREG
79 #define S_ISREG(mode) ((mode)&_S_IFREG)
80 #endif
81 #ifndef S_ISDIR
82 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
83 #endif
84 #ifndef S_ISSOCK
85 #define S_ISSOCK(x) (0)
86 #endif
87 #ifndef O_BINARY
88 #define O_BINARY (0)
89 #endif
90
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. */
94 static int
95 gst_open (const gchar * filename, int flags, int mode)
96 {
97 #ifdef G_OS_WIN32
98   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
99   int retval;
100   int save_errno;
101
102   if (wfilename == NULL) {
103     errno = EINVAL;
104     return -1;
105   }
106
107   retval = _wopen (wfilename, flags, mode);
108   save_errno = errno;
109
110   g_free (wfilename);
111
112   errno = save_errno;
113   return retval;
114 #else
115   return open (filename, flags, mode);
116 #endif
117 }
118
119 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
120 #define GST_CAT_DEFAULT gst_file_src_debug
121
122 /* FileSrc signals and args */
123 enum
124 {
125   /* FILL ME */
126   LAST_SIGNAL
127 };
128
129 #define DEFAULT_BLOCKSIZE       4*1024
130
131 enum
132 {
133   PROP_0,
134   PROP_LOCATION
135 };
136
137 static void gst_file_src_finalize (GObject * object);
138
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);
143
144 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
145 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
146
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);
151
152 static void gst_file_src_uri_handler_init (gpointer g_iface,
153     gpointer iface_data);
154
155 #define _do_init \
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);
160
161 static void
162 gst_file_src_class_init (GstFileSrcClass * klass)
163 {
164   GObjectClass *gobject_class;
165   GstElementClass *gstelement_class;
166   GstBaseSrcClass *gstbasesrc_class;
167
168   gobject_class = G_OBJECT_CLASS (klass);
169   gstelement_class = GST_ELEMENT_CLASS (klass);
170   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
171
172   gobject_class->set_property = gst_file_src_set_property;
173   gobject_class->get_property = gst_file_src_get_property;
174
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));
180
181   gobject_class->finalize = gst_file_src_finalize;
182
183   gst_element_class_set_static_metadata (gstelement_class,
184       "File Source",
185       "Source/File",
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));
190
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);
196
197   if (sizeof (off_t) < 8) {
198     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
199         sizeof (off_t));
200   }
201 }
202
203 static void
204 gst_file_src_init (GstFileSrc * src)
205 {
206   src->filename = NULL;
207   src->fd = 0;
208   src->uri = NULL;
209
210   src->is_regular = FALSE;
211
212   gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
213 }
214
215 static void
216 gst_file_src_finalize (GObject * object)
217 {
218   GstFileSrc *src;
219
220   src = GST_FILE_SRC (object);
221
222   g_free (src->filename);
223   g_free (src->uri);
224
225   G_OBJECT_CLASS (parent_class)->finalize (object);
226 }
227
228 static gboolean
229 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
230 {
231   GstState state;
232
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)
237     goto wrong_state;
238   GST_OBJECT_UNLOCK (src);
239
240   g_free (src->filename);
241   g_free (src->uri);
242
243   /* clear the filename if we get a NULL */
244   if (location == NULL) {
245     src->filename = NULL;
246     src->uri = NULL;
247   } else {
248     /* we store the filename as received by the application. On Windows this
249      * should be UTF8 */
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);
254   }
255   g_object_notify (G_OBJECT (src), "location");
256   /* FIXME 0.11: notify "uri" property once there is one */
257
258   return TRUE;
259
260   /* ERROR */
261 wrong_state:
262   {
263     g_warning ("Changing the `location' property on filesrc when a file is "
264         "open is not supported.");
265     GST_OBJECT_UNLOCK (src);
266     return FALSE;
267   }
268 }
269
270 static void
271 gst_file_src_set_property (GObject * object, guint prop_id,
272     const GValue * value, GParamSpec * pspec)
273 {
274   GstFileSrc *src;
275
276   g_return_if_fail (GST_IS_FILE_SRC (object));
277
278   src = GST_FILE_SRC (object);
279
280   switch (prop_id) {
281     case PROP_LOCATION:
282       gst_file_src_set_location (src, g_value_get_string (value));
283       break;
284     default:
285       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286       break;
287   }
288 }
289
290 static void
291 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
292     GParamSpec * pspec)
293 {
294   GstFileSrc *src;
295
296   g_return_if_fail (GST_IS_FILE_SRC (object));
297
298   src = GST_FILE_SRC (object);
299
300   switch (prop_id) {
301     case PROP_LOCATION:
302       g_value_set_string (value, src->filename);
303       break;
304     default:
305       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306       break;
307   }
308 }
309
310 /***
311  * read code below
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.
318  *
319  */
320 static GstFlowReturn
321 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
322     GstBuffer * buf)
323 {
324   GstFileSrc *src;
325   guint to_read, bytes_read;
326   int ret;
327   GstMapInfo info;
328   guint8 *data;
329
330   src = GST_FILE_SRC_CAST (basesrc);
331
332   if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
333     off_t res;
334
335     res = lseek (src->fd, offset, SEEK_SET);
336     if (G_UNLIKELY (res < 0 || res != offset))
337       goto seek_failed;
338
339     src->read_position = offset;
340   }
341
342   gst_buffer_map (buf, &info, GST_MAP_WRITE);
343   data = info.data;
344
345   bytes_read = 0;
346   to_read = length;
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);
350     errno = 0;
351     ret = read (src->fd, data + bytes_read, to_read);
352     if (G_UNLIKELY (ret < 0)) {
353       if (errno == EAGAIN || errno == EINTR)
354         continue;
355       goto could_not_read;
356     }
357
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 */
361       if (bytes_read > 0)
362         break;
363       goto eos;
364     }
365
366     to_read -= ret;
367     bytes_read += ret;
368
369     src->read_position += ret;
370   }
371
372   gst_buffer_unmap (buf, &info);
373   if (bytes_read != length)
374     gst_buffer_resize (buf, 0, bytes_read);
375
376   GST_BUFFER_OFFSET (buf) = offset;
377   GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
378
379   return GST_FLOW_OK;
380
381   /* ERROR */
382 seek_failed:
383   {
384     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
385     return GST_FLOW_ERROR;
386   }
387 could_not_read:
388   {
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;
393   }
394 eos:
395   {
396     GST_DEBUG ("EOS");
397     gst_buffer_unmap (buf, &info);
398     gst_buffer_resize (buf, 0, 0);
399     return GST_FLOW_EOS;
400   }
401 }
402
403 static gboolean
404 gst_file_src_is_seekable (GstBaseSrc * basesrc)
405 {
406   GstFileSrc *src = GST_FILE_SRC (basesrc);
407
408   return src->seekable;
409 }
410
411 static gboolean
412 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
413 {
414   struct stat stat_results;
415   GstFileSrc *src;
416
417   src = GST_FILE_SRC (basesrc);
418
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. */
422     return FALSE;
423   }
424
425   if (fstat (src->fd, &stat_results) < 0)
426     goto could_not_stat;
427
428   *size = stat_results.st_size;
429
430   return TRUE;
431
432   /* ERROR */
433 could_not_stat:
434   {
435     return FALSE;
436   }
437 }
438
439 /* open the file, necessary to go to READY state */
440 static gboolean
441 gst_file_src_start (GstBaseSrc * basesrc)
442 {
443   GstFileSrc *src = GST_FILE_SRC (basesrc);
444   struct stat stat_results;
445
446   if (src->filename == NULL || src->filename[0] == '\0')
447     goto no_filename;
448
449   GST_INFO_OBJECT (src, "opening file %s", src->filename);
450
451   /* open the file */
452   src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
453
454   if (src->fd < 0)
455     goto open_failed;
456
457   /* check if it is a regular file, otherwise bail out */
458   if (fstat (src->fd, &stat_results) < 0)
459     goto no_stat;
460
461   if (S_ISDIR (stat_results.st_mode))
462     goto was_directory;
463
464   if (S_ISSOCK (stat_results.st_mode))
465     goto was_socket;
466
467   src->read_position = 0;
468
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;
472
473   /* We need to check if the underlying file is seekable. */
474   {
475     off_t res = lseek (src->fd, 0, SEEK_END);
476
477     if (res < 0) {
478       GST_LOG_OBJECT (src, "disabling seeking, lseek failed: %s",
479           g_strerror (errno));
480       src->seekable = FALSE;
481     } else {
482       res = lseek (src->fd, 0, SEEK_SET);
483
484       if (res < 0) {
485         /* We really don't like not being able to go back to 0 */
486         src->seekable = FALSE;
487         goto lseek_wonky;
488       }
489
490       src->seekable = TRUE;
491     }
492   }
493
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;
497
498   gst_base_src_set_dynamic_size (basesrc, src->seekable);
499
500   return TRUE;
501
502   /* ERROR */
503 no_filename:
504   {
505     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
506         (_("No file name specified for reading.")), (NULL));
507     goto error_exit;
508   }
509 open_failed:
510   {
511     switch (errno) {
512       case ENOENT:
513         GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
514             ("No such file \"%s\"", src->filename));
515         break;
516       default:
517         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
518             (_("Could not open file \"%s\" for reading."), src->filename),
519             GST_ERROR_SYSTEM);
520         break;
521     }
522     goto error_exit;
523   }
524 no_stat:
525   {
526     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
527         (_("Could not get info on \"%s\"."), src->filename), (NULL));
528     goto error_close;
529   }
530 was_directory:
531   {
532     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
533         (_("\"%s\" is a directory."), src->filename), (NULL));
534     goto error_close;
535   }
536 was_socket:
537   {
538     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
539         (_("File \"%s\" is a socket."), src->filename), (NULL));
540     goto error_close;
541   }
542 lseek_wonky:
543   {
544     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
545         ("Could not seek back to zero after seek test in file \"%s\"",
546             src->filename));
547     goto error_close;
548   }
549 error_close:
550   close (src->fd);
551 error_exit:
552   return FALSE;
553 }
554
555 /* unmap and close the file */
556 static gboolean
557 gst_file_src_stop (GstBaseSrc * basesrc)
558 {
559   GstFileSrc *src = GST_FILE_SRC (basesrc);
560
561   /* close the file */
562   close (src->fd);
563
564   /* zero out a lot of our state */
565   src->fd = 0;
566   src->is_regular = FALSE;
567
568   return TRUE;
569 }
570
571 /*** GSTURIHANDLER INTERFACE *************************************************/
572
573 static GstURIType
574 gst_file_src_uri_get_type (GType type)
575 {
576   return GST_URI_SRC;
577 }
578
579 static const gchar *const *
580 gst_file_src_uri_get_protocols (GType type)
581 {
582   static const gchar *protocols[] = { "file", NULL };
583
584   return protocols;
585 }
586
587 static gchar *
588 gst_file_src_uri_get_uri (GstURIHandler * handler)
589 {
590   GstFileSrc *src = GST_FILE_SRC (handler);
591
592   /* FIXME: make thread-safe */
593   return g_strdup (src->uri);
594 }
595
596 static gboolean
597 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
598     GError ** err)
599 {
600   gchar *location, *hostname = NULL;
601   gboolean ret = FALSE;
602   GstFileSrc *src = GST_FILE_SRC (handler);
603
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);
609     return TRUE;
610   }
611
612   location = g_filename_from_uri (uri, &hostname, err);
613
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");
617     goto beach;
618   }
619
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);
625     goto beach;
626   }
627 #ifdef G_OS_WIN32
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.
632    */
633   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
634     memmove (location, location + 1, strlen (location + 1) + 1);
635 #endif
636
637   ret = gst_file_src_set_location (src, location);
638
639 beach:
640   if (location)
641     g_free (location);
642   if (hostname)
643     g_free (hostname);
644
645   return ret;
646 }
647
648 static void
649 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
650 {
651   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
652
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;
657 }