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