Merge branch 'master' into 0.11
[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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 ! decodebin2 ! 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 #ifdef G_OS_WIN32
46 #include <io.h>                 /* lseek, open, close, read */
47 /* On win32, stat* default to 32 bit; we need the 64-bit
48  * variants, so explicitly define it that way. */
49 #define stat __stat64
50 #define fstat _fstat64
51 #undef lseek
52 #define lseek _lseeki64
53 #undef off_t
54 #define off_t guint64
55 /* Prevent stat.h from defining the stat* functions as
56  * _stat*, since we're explicitly overriding that */
57 #undef _INC_STAT_INL
58 #endif
59 #include <sys/stat.h>
60 #include <fcntl.h>
61
62 #ifdef HAVE_UNISTD_H
63 #  include <unistd.h>
64 #endif
65
66 #include <errno.h>
67 #include <string.h>
68
69 #include "../../gst/gst-i18n-lib.h"
70
71 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS_ANY);
75
76 /* FIXME we should be using glib for this */
77 #ifndef S_ISREG
78 #define S_ISREG(mode) ((mode)&_S_IFREG)
79 #endif
80 #ifndef S_ISDIR
81 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
82 #endif
83 #ifndef S_ISSOCK
84 #define S_ISSOCK(x) (0)
85 #endif
86 #ifndef O_BINARY
87 #define O_BINARY (0)
88 #endif
89
90 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
91  * use the 'file descriptor' opened in glib (and returned from this function)
92  * in this library, as they may have unrelated C runtimes. */
93 static int
94 gst_open (const gchar * filename, int flags, int mode)
95 {
96 #ifdef G_OS_WIN32
97   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
98   int retval;
99   int save_errno;
100
101   if (wfilename == NULL) {
102     errno = EINVAL;
103     return -1;
104   }
105
106   retval = _wopen (wfilename, flags, mode);
107   save_errno = errno;
108
109   g_free (wfilename);
110
111   errno = save_errno;
112   return retval;
113 #else
114   return open (filename, flags, mode);
115 #endif
116 }
117
118 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
119 #define GST_CAT_DEFAULT gst_file_src_debug
120
121 /* FileSrc signals and args */
122 enum
123 {
124   /* FILL ME */
125   LAST_SIGNAL
126 };
127
128 #define DEFAULT_BLOCKSIZE       4*1024
129
130 enum
131 {
132   PROP_0,
133   PROP_LOCATION,
134   PROP_FD
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 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
152
153 static void gst_file_src_uri_handler_init (gpointer g_iface,
154     gpointer iface_data);
155
156 #define _do_init \
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);
161
162 static void
163 gst_file_src_class_init (GstFileSrcClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167   GstBaseSrcClass *gstbasesrc_class;
168
169   gobject_class = G_OBJECT_CLASS (klass);
170   gstelement_class = GST_ELEMENT_CLASS (klass);
171   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
172
173   gobject_class->set_property = gst_file_src_set_property;
174   gobject_class->get_property = gst_file_src_get_property;
175
176   g_object_class_install_property (gobject_class, PROP_FD,
177       g_param_spec_int ("fd", "File-descriptor",
178           "File-descriptor for the file being mmap()d", 0, G_MAXINT, 0,
179           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180   g_object_class_install_property (gobject_class, PROP_LOCATION,
181       g_param_spec_string ("location", "File Location",
182           "Location of the file to read", NULL,
183           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
184           GST_PARAM_MUTABLE_READY));
185
186   gobject_class->finalize = gst_file_src_finalize;
187
188   gst_element_class_set_details_simple (gstelement_class,
189       "File Source",
190       "Source/File",
191       "Read from arbitrary point in a file",
192       "Erik Walthinsen <omega@cse.ogi.edu>");
193   gst_element_class_add_pad_template (gstelement_class,
194       gst_static_pad_template_get (&srctemplate));
195
196   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
197   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
198   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
199   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
200   gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
201   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
202
203   if (sizeof (off_t) < 8) {
204     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
205         sizeof (off_t));
206   }
207 }
208
209 static void
210 gst_file_src_init (GstFileSrc * src)
211 {
212   src->filename = NULL;
213   src->fd = 0;
214   src->uri = NULL;
215
216   src->is_regular = FALSE;
217 }
218
219 static void
220 gst_file_src_finalize (GObject * object)
221 {
222   GstFileSrc *src;
223
224   src = GST_FILE_SRC (object);
225
226   g_free (src->filename);
227   g_free (src->uri);
228
229   G_OBJECT_CLASS (parent_class)->finalize (object);
230 }
231
232 static gboolean
233 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
234 {
235   GstState state;
236
237   /* the element must be stopped in order to do this */
238   GST_OBJECT_LOCK (src);
239   state = GST_STATE (src);
240   if (state != GST_STATE_READY && state != GST_STATE_NULL)
241     goto wrong_state;
242   GST_OBJECT_UNLOCK (src);
243
244   g_free (src->filename);
245   g_free (src->uri);
246
247   /* clear the filename if we get a NULL (is that possible?) */
248   if (location == NULL) {
249     src->filename = NULL;
250     src->uri = NULL;
251   } else {
252     /* we store the filename as received by the application. On Windows this
253      * should be UTF8 */
254     src->filename = g_strdup (location);
255     src->uri = gst_filename_to_uri (location, NULL);
256     GST_INFO ("filename : %s", src->filename);
257     GST_INFO ("uri      : %s", src->uri);
258   }
259   g_object_notify (G_OBJECT (src), "location");
260   gst_uri_handler_new_uri (GST_URI_HANDLER (src), src->uri);
261
262   return TRUE;
263
264   /* ERROR */
265 wrong_state:
266   {
267     g_warning ("Changing the `location' property on filesrc when a file is "
268         "open is not supported.");
269     GST_OBJECT_UNLOCK (src);
270     return FALSE;
271   }
272 }
273
274 static void
275 gst_file_src_set_property (GObject * object, guint prop_id,
276     const GValue * value, GParamSpec * pspec)
277 {
278   GstFileSrc *src;
279
280   g_return_if_fail (GST_IS_FILE_SRC (object));
281
282   src = GST_FILE_SRC (object);
283
284   switch (prop_id) {
285     case PROP_LOCATION:
286       gst_file_src_set_location (src, g_value_get_string (value));
287       break;
288     default:
289       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290       break;
291   }
292 }
293
294 static void
295 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
296     GParamSpec * pspec)
297 {
298   GstFileSrc *src;
299
300   g_return_if_fail (GST_IS_FILE_SRC (object));
301
302   src = GST_FILE_SRC (object);
303
304   switch (prop_id) {
305     case PROP_LOCATION:
306       g_value_set_string (value, src->filename);
307       break;
308     case PROP_FD:
309       g_value_set_int (value, src->fd);
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313       break;
314   }
315 }
316
317 /***
318  * read code below
319  * that is to say, you shouldn't read the code below, but the code that reads
320  * stuff is below.  Well, you shouldn't not read the code below, feel free
321  * to read it of course.  It's just that "read code below" is a pretty crappy
322  * documentation string because it sounds like we're expecting you to read
323  * the code to understand what it does, which, while true, is really not
324  * the sort of attitude we want to be advertising.  No sir.
325  *
326  */
327 static GstFlowReturn
328 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
329     GstBuffer * buf)
330 {
331   GstFileSrc *src;
332   int ret;
333   guint8 *data;
334
335   src = GST_FILE_SRC_CAST (basesrc);
336
337   if (G_UNLIKELY (src->read_position != offset)) {
338     off_t res;
339
340     res = lseek (src->fd, offset, SEEK_SET);
341     if (G_UNLIKELY (res < 0 || res != offset))
342       goto seek_failed;
343
344     src->read_position = offset;
345   }
346
347   data = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
348
349   GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
350       length, offset);
351
352   ret = read (src->fd, data, length);
353   if (G_UNLIKELY (ret < 0))
354     goto could_not_read;
355
356   /* seekable regular files should have given us what we expected */
357   if (G_UNLIKELY ((guint) ret < length && src->seekable))
358     goto unexpected_eos;
359
360   /* other files should eos if they read 0 and more was requested */
361   if (G_UNLIKELY (ret == 0))
362     goto eos;
363
364   length = ret;
365
366   gst_buffer_unmap (buf, data, length);
367
368   GST_BUFFER_OFFSET (buf) = offset;
369   GST_BUFFER_OFFSET_END (buf) = offset + length;
370
371   src->read_position += length;
372
373   return GST_FLOW_OK;
374
375   /* ERROR */
376 seek_failed:
377   {
378     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
379     return GST_FLOW_ERROR;
380   }
381 could_not_read:
382   {
383     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
384     gst_buffer_unmap (buf, data, 0);
385     return GST_FLOW_ERROR;
386   }
387 unexpected_eos:
388   {
389     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
390         ("unexpected end of file."));
391     gst_buffer_unmap (buf, data, 0);
392     return GST_FLOW_ERROR;
393   }
394 eos:
395   {
396     GST_DEBUG ("non-regular file hits EOS");
397     gst_buffer_unmap (buf, data, 0);
398     return GST_FLOW_EOS;
399   }
400 }
401
402 static gboolean
403 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
404 {
405   gboolean ret = FALSE;
406   GstFileSrc *src = GST_FILE_SRC (basesrc);
407
408   switch (GST_QUERY_TYPE (query)) {
409     case GST_QUERY_URI:
410       gst_query_set_uri (query, src->uri);
411       ret = TRUE;
412       break;
413     default:
414       ret = FALSE;
415       break;
416   }
417
418   if (!ret)
419     ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
420
421   return ret;
422 }
423
424 static gboolean
425 gst_file_src_is_seekable (GstBaseSrc * basesrc)
426 {
427   GstFileSrc *src = GST_FILE_SRC (basesrc);
428
429   return src->seekable;
430 }
431
432 static gboolean
433 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
434 {
435   struct stat stat_results;
436   GstFileSrc *src;
437
438   src = GST_FILE_SRC (basesrc);
439
440   if (!src->seekable) {
441     /* If it isn't seekable, we won't know the length (but fstat will still
442      * succeed, and wrongly say our length is zero. */
443     return FALSE;
444   }
445
446   if (fstat (src->fd, &stat_results) < 0)
447     goto could_not_stat;
448
449   *size = stat_results.st_size;
450
451   return TRUE;
452
453   /* ERROR */
454 could_not_stat:
455   {
456     return FALSE;
457   }
458 }
459
460 /* open the file and mmap it, necessary to go to READY state */
461 static gboolean
462 gst_file_src_start (GstBaseSrc * basesrc)
463 {
464   GstFileSrc *src = GST_FILE_SRC (basesrc);
465   struct stat stat_results;
466
467   if (src->filename == NULL || src->filename[0] == '\0')
468     goto no_filename;
469
470   GST_INFO_OBJECT (src, "opening file %s", src->filename);
471
472   /* open the file */
473   src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
474
475   if (src->fd < 0)
476     goto open_failed;
477
478   /* check if it is a regular file, otherwise bail out */
479   if (fstat (src->fd, &stat_results) < 0)
480     goto no_stat;
481
482   if (S_ISDIR (stat_results.st_mode))
483     goto was_directory;
484
485   if (S_ISSOCK (stat_results.st_mode))
486     goto was_socket;
487
488   src->read_position = 0;
489
490   /* record if it's a regular (hence seekable and lengthable) file */
491   if (S_ISREG (stat_results.st_mode))
492     src->is_regular = TRUE;
493
494   /* We need to check if the underlying file is seekable. */
495   {
496     off_t res = lseek (src->fd, 0, SEEK_END);
497
498     if (res < 0) {
499       GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
500           "failed: %s", g_strerror (errno));
501       src->seekable = FALSE;
502     } else {
503       src->seekable = TRUE;
504     }
505     lseek (src->fd, 0, SEEK_SET);
506   }
507
508   /* We can only really do seeking on regular files - for other file types, we
509    * don't know their length, so seeking isn't useful/meaningful */
510   src->seekable = src->seekable && src->is_regular;
511
512   gst_base_src_set_dynamic_size (basesrc, src->seekable);
513
514   return TRUE;
515
516   /* ERROR */
517 no_filename:
518   {
519     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
520         (_("No file name specified for reading.")), (NULL));
521     return FALSE;
522   }
523 open_failed:
524   {
525     switch (errno) {
526       case ENOENT:
527         GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
528             ("No such file \"%s\"", src->filename));
529         break;
530       default:
531         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
532             (_("Could not open file \"%s\" for reading."), src->filename),
533             GST_ERROR_SYSTEM);
534         break;
535     }
536     return FALSE;
537   }
538 no_stat:
539   {
540     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
541         (_("Could not get info on \"%s\"."), src->filename), (NULL));
542     close (src->fd);
543     return FALSE;
544   }
545 was_directory:
546   {
547     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
548         (_("\"%s\" is a directory."), src->filename), (NULL));
549     close (src->fd);
550     return FALSE;
551   }
552 was_socket:
553   {
554     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
555         (_("File \"%s\" is a socket."), src->filename), (NULL));
556     close (src->fd);
557     return FALSE;
558   }
559 }
560
561 /* unmap and close the file */
562 static gboolean
563 gst_file_src_stop (GstBaseSrc * basesrc)
564 {
565   GstFileSrc *src = GST_FILE_SRC (basesrc);
566
567   /* close the file */
568   close (src->fd);
569
570   /* zero out a lot of our state */
571   src->fd = 0;
572   src->is_regular = FALSE;
573
574   return TRUE;
575 }
576
577 /*** GSTURIHANDLER INTERFACE *************************************************/
578
579 static GstURIType
580 gst_file_src_uri_get_type (GType type)
581 {
582   return GST_URI_SRC;
583 }
584
585 static gchar **
586 gst_file_src_uri_get_protocols (GType type)
587 {
588   static gchar *protocols[] = { (char *) "file", NULL };
589
590   return protocols;
591 }
592
593 static const gchar *
594 gst_file_src_uri_get_uri (GstURIHandler * handler)
595 {
596   GstFileSrc *src = GST_FILE_SRC (handler);
597
598   return src->uri;
599 }
600
601 static gboolean
602 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
603 {
604   gchar *location, *hostname = NULL;
605   gboolean ret = FALSE;
606   GstFileSrc *src = GST_FILE_SRC (handler);
607   GError *error = NULL;
608
609   if (strcmp (uri, "file://") == 0) {
610     /* Special case for "file://" as this is used by some applications
611      *  to test with gst_element_make_from_uri if there's an element
612      *  that supports the URI protocol. */
613     gst_file_src_set_location (src, NULL);
614     return TRUE;
615   }
616
617   location = g_filename_from_uri (uri, &hostname, &error);
618
619   if (!location || error) {
620     if (error) {
621       GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
622           error->message);
623       g_error_free (error);
624     } else {
625       GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc", uri);
626     }
627     goto beach;
628   }
629
630   if ((hostname) && (strcmp (hostname, "localhost"))) {
631     /* Only 'localhost' is permitted */
632     GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
633     goto beach;
634   }
635 #ifdef G_OS_WIN32
636   /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
637    * correctly on windows, it leaves them with an extra backslash
638    * at the start if they're of the mozilla-style file://///host/path/file 
639    * form. Correct this.
640    */
641   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
642     g_memmove (location, location + 1, strlen (location + 1) + 1);
643 #endif
644
645   ret = gst_file_src_set_location (src, location);
646
647 beach:
648   if (location)
649     g_free (location);
650   if (hostname)
651     g_free (hostname);
652
653   return ret;
654 }
655
656 static void
657 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
658 {
659   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
660
661   iface->get_type = gst_file_src_uri_get_type;
662   iface->get_protocols = gst_file_src_uri_get_protocols;
663   iface->get_uri = gst_file_src_uri_get_uri;
664   iface->set_uri = gst_file_src_uri_set_uri;
665 }