Merge remote-tracking branch 'origin/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 };
135
136 static void gst_file_src_finalize (GObject * object);
137
138 static void gst_file_src_set_property (GObject * object, guint prop_id,
139     const GValue * value, GParamSpec * pspec);
140 static void gst_file_src_get_property (GObject * object, guint prop_id,
141     GValue * value, GParamSpec * pspec);
142
143 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
144 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
145
146 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
147 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
148 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
149     guint length, GstBuffer * buf);
150 static gboolean gst_file_src_query (GstBaseSrc * src, GstQuery * query);
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_details_simple (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   gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_file_src_query);
197
198   if (sizeof (off_t) < 8) {
199     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
200         sizeof (off_t));
201   }
202 }
203
204 static void
205 gst_file_src_init (GstFileSrc * src)
206 {
207   src->filename = NULL;
208   src->fd = 0;
209   src->uri = NULL;
210
211   src->is_regular = FALSE;
212 }
213
214 static void
215 gst_file_src_finalize (GObject * object)
216 {
217   GstFileSrc *src;
218
219   src = GST_FILE_SRC (object);
220
221   g_free (src->filename);
222   g_free (src->uri);
223
224   G_OBJECT_CLASS (parent_class)->finalize (object);
225 }
226
227 static gboolean
228 gst_file_src_set_location (GstFileSrc * src, const gchar * location)
229 {
230   GstState state;
231
232   /* the element must be stopped in order to do this */
233   GST_OBJECT_LOCK (src);
234   state = GST_STATE (src);
235   if (state != GST_STATE_READY && state != GST_STATE_NULL)
236     goto wrong_state;
237   GST_OBJECT_UNLOCK (src);
238
239   g_free (src->filename);
240   g_free (src->uri);
241
242   /* clear the filename if we get a NULL (is that possible?) */
243   if (location == NULL) {
244     src->filename = NULL;
245     src->uri = NULL;
246   } else {
247     /* we store the filename as received by the application. On Windows this
248      * should be UTF8 */
249     src->filename = g_strdup (location);
250     src->uri = gst_filename_to_uri (location, NULL);
251     GST_INFO ("filename : %s", src->filename);
252     GST_INFO ("uri      : %s", src->uri);
253   }
254   g_object_notify (G_OBJECT (src), "location");
255   /* FIXME 0.11: notify "uri" property once there is one */
256
257   return TRUE;
258
259   /* ERROR */
260 wrong_state:
261   {
262     g_warning ("Changing the `location' property on filesrc when a file is "
263         "open is not supported.");
264     GST_OBJECT_UNLOCK (src);
265     return FALSE;
266   }
267 }
268
269 static void
270 gst_file_src_set_property (GObject * object, guint prop_id,
271     const GValue * value, GParamSpec * pspec)
272 {
273   GstFileSrc *src;
274
275   g_return_if_fail (GST_IS_FILE_SRC (object));
276
277   src = GST_FILE_SRC (object);
278
279   switch (prop_id) {
280     case PROP_LOCATION:
281       gst_file_src_set_location (src, g_value_get_string (value));
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286   }
287 }
288
289 static void
290 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
291     GParamSpec * pspec)
292 {
293   GstFileSrc *src;
294
295   g_return_if_fail (GST_IS_FILE_SRC (object));
296
297   src = GST_FILE_SRC (object);
298
299   switch (prop_id) {
300     case PROP_LOCATION:
301       g_value_set_string (value, src->filename);
302       break;
303     default:
304       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305       break;
306   }
307 }
308
309 /***
310  * read code below
311  * that is to say, you shouldn't read the code below, but the code that reads
312  * stuff is below.  Well, you shouldn't not read the code below, feel free
313  * to read it of course.  It's just that "read code below" is a pretty crappy
314  * documentation string because it sounds like we're expecting you to read
315  * the code to understand what it does, which, while true, is really not
316  * the sort of attitude we want to be advertising.  No sir.
317  *
318  */
319 static GstFlowReturn
320 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
321     GstBuffer * buf)
322 {
323   GstFileSrc *src;
324   guint to_read, bytes_read;
325   int ret;
326   GstMapInfo info;
327   guint8 *data;
328
329   src = GST_FILE_SRC_CAST (basesrc);
330
331   if (G_UNLIKELY (src->read_position != offset)) {
332     off_t res;
333
334     res = lseek (src->fd, offset, SEEK_SET);
335     if (G_UNLIKELY (res < 0 || res != offset))
336       goto seek_failed;
337
338     src->read_position = offset;
339   }
340
341   gst_buffer_map (buf, &info, GST_MAP_WRITE);
342   data = info.data;
343
344   bytes_read = 0;
345   to_read = length;
346   while (to_read > 0) {
347     GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
348         to_read, offset + bytes_read);
349     errno = 0;
350     ret = read (src->fd, data + bytes_read, to_read);
351     if (G_UNLIKELY (ret < 0)) {
352       if (errno == EAGAIN || errno == EINTR)
353         continue;
354       goto could_not_read;
355     }
356
357     /* files should eos if they read 0 and more was requested */
358     if (G_UNLIKELY (ret == 0)) {
359       /* .. but first we should return any remaining data */
360       if (bytes_read > 0)
361         break;
362       goto eos;
363     }
364
365     to_read -= ret;
366     bytes_read += ret;
367
368     src->read_position += ret;
369   }
370
371   gst_buffer_unmap (buf, &info);
372   gst_buffer_resize (buf, 0, bytes_read);
373
374   GST_BUFFER_OFFSET (buf) = offset;
375   GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
376
377   return GST_FLOW_OK;
378
379   /* ERROR */
380 seek_failed:
381   {
382     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
383     return GST_FLOW_ERROR;
384   }
385 could_not_read:
386   {
387     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
388     gst_buffer_unmap (buf, &info);
389     gst_buffer_resize (buf, 0, 0);
390     return GST_FLOW_ERROR;
391   }
392 eos:
393   {
394     GST_DEBUG ("EOS");
395     gst_buffer_unmap (buf, &info);
396     gst_buffer_resize (buf, 0, 0);
397     return GST_FLOW_EOS;
398   }
399 }
400
401 static gboolean
402 gst_file_src_query (GstBaseSrc * basesrc, GstQuery * query)
403 {
404   gboolean ret = FALSE;
405   GstFileSrc *src = GST_FILE_SRC (basesrc);
406
407   switch (GST_QUERY_TYPE (query)) {
408     case GST_QUERY_URI:
409       gst_query_set_uri (query, src->uri);
410       ret = TRUE;
411       break;
412     default:
413       ret = FALSE;
414       break;
415   }
416
417   if (!ret)
418     ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
419
420   return ret;
421 }
422
423 static gboolean
424 gst_file_src_is_seekable (GstBaseSrc * basesrc)
425 {
426   GstFileSrc *src = GST_FILE_SRC (basesrc);
427
428   return src->seekable;
429 }
430
431 static gboolean
432 gst_file_src_get_size (GstBaseSrc * basesrc, guint64 * size)
433 {
434   struct stat stat_results;
435   GstFileSrc *src;
436
437   src = GST_FILE_SRC (basesrc);
438
439   if (!src->seekable) {
440     /* If it isn't seekable, we won't know the length (but fstat will still
441      * succeed, and wrongly say our length is zero. */
442     return FALSE;
443   }
444
445   if (fstat (src->fd, &stat_results) < 0)
446     goto could_not_stat;
447
448   *size = stat_results.st_size;
449
450   return TRUE;
451
452   /* ERROR */
453 could_not_stat:
454   {
455     return FALSE;
456   }
457 }
458
459 /* open the file and mmap it, necessary to go to READY state */
460 static gboolean
461 gst_file_src_start (GstBaseSrc * basesrc)
462 {
463   GstFileSrc *src = GST_FILE_SRC (basesrc);
464   struct stat stat_results;
465
466   if (src->filename == NULL || src->filename[0] == '\0')
467     goto no_filename;
468
469   GST_INFO_OBJECT (src, "opening file %s", src->filename);
470
471   /* open the file */
472   src->fd = gst_open (src->filename, O_RDONLY | O_BINARY, 0);
473
474   if (src->fd < 0)
475     goto open_failed;
476
477   /* check if it is a regular file, otherwise bail out */
478   if (fstat (src->fd, &stat_results) < 0)
479     goto no_stat;
480
481   if (S_ISDIR (stat_results.st_mode))
482     goto was_directory;
483
484   if (S_ISSOCK (stat_results.st_mode))
485     goto was_socket;
486
487   src->read_position = 0;
488
489   /* record if it's a regular (hence seekable and lengthable) file */
490   if (S_ISREG (stat_results.st_mode))
491     src->is_regular = TRUE;
492
493   /* We need to check if the underlying file is seekable. */
494   {
495     off_t res = lseek (src->fd, 0, SEEK_END);
496
497     if (res < 0) {
498       GST_LOG_OBJECT (src, "disabling seeking, not in mmap mode and lseek "
499           "failed: %s", g_strerror (errno));
500       src->seekable = FALSE;
501     } else {
502       src->seekable = TRUE;
503     }
504     lseek (src->fd, 0, SEEK_SET);
505   }
506
507   /* We can only really do seeking on regular files - for other file types, we
508    * don't know their length, so seeking isn't useful/meaningful */
509   src->seekable = src->seekable && src->is_regular;
510
511   gst_base_src_set_dynamic_size (basesrc, src->seekable);
512
513   return TRUE;
514
515   /* ERROR */
516 no_filename:
517   {
518     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
519         (_("No file name specified for reading.")), (NULL));
520     goto error_exit;
521   }
522 open_failed:
523   {
524     switch (errno) {
525       case ENOENT:
526         GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
527             ("No such file \"%s\"", src->filename));
528         break;
529       default:
530         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
531             (_("Could not open file \"%s\" for reading."), src->filename),
532             GST_ERROR_SYSTEM);
533         break;
534     }
535     goto error_exit;
536   }
537 no_stat:
538   {
539     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
540         (_("Could not get info on \"%s\"."), src->filename), (NULL));
541     goto error_close;
542   }
543 was_directory:
544   {
545     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
546         (_("\"%s\" is a directory."), src->filename), (NULL));
547     goto error_close;
548   }
549 was_socket:
550   {
551     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
552         (_("File \"%s\" is a socket."), src->filename), (NULL));
553     goto error_close;
554   }
555 error_close:
556   close (src->fd);
557 error_exit:
558   return FALSE;
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 const gchar *const *
586 gst_file_src_uri_get_protocols (GType type)
587 {
588   static const gchar *protocols[] = { "file", NULL };
589
590   return protocols;
591 }
592
593 static gchar *
594 gst_file_src_uri_get_uri (GstURIHandler * handler)
595 {
596   GstFileSrc *src = GST_FILE_SRC (handler);
597
598   /* FIXME: make thread-safe */
599   return g_strdup (src->uri);
600 }
601
602 static gboolean
603 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
604     GError ** err)
605 {
606   gchar *location, *hostname = NULL;
607   gboolean ret = FALSE;
608   GstFileSrc *src = GST_FILE_SRC (handler);
609
610   if (strcmp (uri, "file://") == 0) {
611     /* Special case for "file://" as this is used by some applications
612      *  to test with gst_element_make_from_uri if there's an element
613      *  that supports the URI protocol. */
614     gst_file_src_set_location (src, NULL);
615     return TRUE;
616   }
617
618   location = g_filename_from_uri (uri, &hostname, err);
619
620   if (!location || (err != NULL && *err != NULL)) {
621     GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
622         (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
623     goto beach;
624   }
625
626   if ((hostname) && (strcmp (hostname, "localhost"))) {
627     /* Only 'localhost' is permitted */
628     GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
629     g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
630         "File URI with invalid hostname '%s'", hostname);
631     goto beach;
632   }
633 #ifdef G_OS_WIN32
634   /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
635    * correctly on windows, it leaves them with an extra backslash
636    * at the start if they're of the mozilla-style file://///host/path/file 
637    * form. Correct this.
638    */
639   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
640     g_memmove (location, location + 1, strlen (location + 1) + 1);
641 #endif
642
643   ret = gst_file_src_set_location (src, location);
644
645 beach:
646   if (location)
647     g_free (location);
648   if (hostname)
649     g_free (hostname);
650
651   return ret;
652 }
653
654 static void
655 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
656 {
657   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
658
659   iface->get_type = gst_file_src_uri_get_type;
660   iface->get_protocols = gst_file_src_uri_get_protocols;
661   iface->get_uri = gst_file_src_uri_get_uri;
662   iface->set_uri = gst_file_src_uri_set_uri;
663 }