Port gtk-doc comments to their equivalent markdown syntax
[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  * @title: filesrc
25  * @see_also: #GstFileSrc
26  *
27  * Read data from a file in the local file system.
28  *
29  * ## Example launch line
30  * |[
31  * gst-launch-1.0 filesrc location=song.ogg ! decodebin ! audioconvert ! audioresample ! autoaudiosink
32  * ]| Play song.ogg audio file which must be in the current working directory.
33  *
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 #ifdef __BIONIC__               /* Android */
69 #undef lseek
70 #define lseek lseek64
71 #undef fstat
72 #define fstat fstat64
73 #undef off_t
74 #define off_t guint64
75 #endif
76
77 #include <errno.h>
78 #include <string.h>
79
80 #include "../../gst/gst-i18n-lib.h"
81
82 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
83     GST_PAD_SRC,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS_ANY);
86
87 #ifndef S_ISREG
88 #define S_ISREG(mode) ((mode)&_S_IFREG)
89 #endif
90 #ifndef S_ISDIR
91 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
92 #endif
93 #ifndef S_ISSOCK
94 #define S_ISSOCK(x) (0)
95 #endif
96 #ifndef O_BINARY
97 #define O_BINARY (0)
98 #endif
99
100 /* Copy of glib's g_open due to win32 libc/cross-DLL brokenness: we can't
101  * use the 'file descriptor' opened in glib (and returned from this function)
102  * in this library, as they may have unrelated C runtimes. */
103 static int
104 gst_open (const gchar * filename, int flags, int mode)
105 {
106 #ifdef G_OS_WIN32
107   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
108   int retval;
109   int save_errno;
110
111   if (wfilename == NULL) {
112     errno = EINVAL;
113     return -1;
114   }
115
116   retval = _wopen (wfilename, flags, mode);
117   save_errno = errno;
118
119   g_free (wfilename);
120
121   errno = save_errno;
122   return retval;
123 #elif defined (__BIONIC__)
124   return open (filename, flags | O_LARGEFILE, mode);
125 #else
126   return open (filename, flags, mode);
127 #endif
128 }
129
130 GST_DEBUG_CATEGORY_STATIC (gst_file_src_debug);
131 #define GST_CAT_DEFAULT gst_file_src_debug
132
133 /* FileSrc signals and args */
134 enum
135 {
136   /* FILL ME */
137   LAST_SIGNAL
138 };
139
140 #define DEFAULT_BLOCKSIZE       4*1024
141
142 enum
143 {
144   PROP_0,
145   PROP_LOCATION
146 };
147
148 static void gst_file_src_finalize (GObject * object);
149
150 static void gst_file_src_set_property (GObject * object, guint prop_id,
151     const GValue * value, GParamSpec * pspec);
152 static void gst_file_src_get_property (GObject * object, guint prop_id,
153     GValue * value, GParamSpec * pspec);
154
155 static gboolean gst_file_src_start (GstBaseSrc * basesrc);
156 static gboolean gst_file_src_stop (GstBaseSrc * basesrc);
157
158 static gboolean gst_file_src_is_seekable (GstBaseSrc * src);
159 static gboolean gst_file_src_get_size (GstBaseSrc * src, guint64 * size);
160 static GstFlowReturn gst_file_src_fill (GstBaseSrc * src, guint64 offset,
161     guint length, GstBuffer * buf);
162
163 static void gst_file_src_uri_handler_init (gpointer g_iface,
164     gpointer iface_data);
165
166 #define _do_init \
167   G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_src_uri_handler_init); \
168   GST_DEBUG_CATEGORY_INIT (gst_file_src_debug, "filesrc", 0, "filesrc element");
169 #define gst_file_src_parent_class parent_class
170 G_DEFINE_TYPE_WITH_CODE (GstFileSrc, gst_file_src, GST_TYPE_BASE_SRC, _do_init);
171
172 static void
173 gst_file_src_class_init (GstFileSrcClass * klass)
174 {
175   GObjectClass *gobject_class;
176   GstElementClass *gstelement_class;
177   GstBaseSrcClass *gstbasesrc_class;
178
179   gobject_class = G_OBJECT_CLASS (klass);
180   gstelement_class = GST_ELEMENT_CLASS (klass);
181   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
182
183   gobject_class->set_property = gst_file_src_set_property;
184   gobject_class->get_property = gst_file_src_get_property;
185
186   g_object_class_install_property (gobject_class, PROP_LOCATION,
187       g_param_spec_string ("location", "File Location",
188           "Location of the file to read", NULL,
189           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
190           GST_PARAM_MUTABLE_READY));
191
192   gobject_class->finalize = gst_file_src_finalize;
193
194   gst_element_class_set_static_metadata (gstelement_class,
195       "File Source",
196       "Source/File",
197       "Read from arbitrary point in a file",
198       "Erik Walthinsen <omega@cse.ogi.edu>");
199   gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
200
201   gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_file_src_start);
202   gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_file_src_stop);
203   gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_file_src_is_seekable);
204   gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_file_src_get_size);
205   gstbasesrc_class->fill = GST_DEBUG_FUNCPTR (gst_file_src_fill);
206
207   if (sizeof (off_t) < 8) {
208     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
209         sizeof (off_t));
210   }
211 }
212
213 static void
214 gst_file_src_init (GstFileSrc * src)
215 {
216   src->filename = NULL;
217   src->fd = 0;
218   src->uri = NULL;
219
220   src->is_regular = FALSE;
221
222   gst_base_src_set_blocksize (GST_BASE_SRC (src), DEFAULT_BLOCKSIZE);
223 }
224
225 static void
226 gst_file_src_finalize (GObject * object)
227 {
228   GstFileSrc *src;
229
230   src = GST_FILE_SRC (object);
231
232   g_free (src->filename);
233   g_free (src->uri);
234
235   G_OBJECT_CLASS (parent_class)->finalize (object);
236 }
237
238 static gboolean
239 gst_file_src_set_location (GstFileSrc * src, const gchar * location,
240     GError ** err)
241 {
242   GstState state;
243
244   /* the element must be stopped in order to do this */
245   GST_OBJECT_LOCK (src);
246   state = GST_STATE (src);
247   if (state != GST_STATE_READY && state != GST_STATE_NULL)
248     goto wrong_state;
249   GST_OBJECT_UNLOCK (src);
250
251   g_free (src->filename);
252   g_free (src->uri);
253
254   /* clear the filename if we get a NULL */
255   if (location == NULL) {
256     src->filename = NULL;
257     src->uri = NULL;
258   } else {
259     /* we store the filename as received by the application. On Windows this
260      * should be UTF8 */
261     src->filename = g_strdup (location);
262     src->uri = gst_filename_to_uri (location, NULL);
263     GST_INFO ("filename : %s", src->filename);
264     GST_INFO ("uri      : %s", src->uri);
265   }
266   g_object_notify (G_OBJECT (src), "location");
267   /* FIXME 2.0: notify "uri" property once there is one */
268
269   return TRUE;
270
271   /* ERROR */
272 wrong_state:
273   {
274     g_warning ("Changing the `location' property on filesrc when a file is "
275         "open is not supported.");
276     if (err)
277       g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
278           "Changing the `location' property on filesrc when a file is "
279           "open is not supported.");
280     GST_OBJECT_UNLOCK (src);
281     return FALSE;
282   }
283 }
284
285 static void
286 gst_file_src_set_property (GObject * object, guint prop_id,
287     const GValue * value, GParamSpec * pspec)
288 {
289   GstFileSrc *src;
290
291   g_return_if_fail (GST_IS_FILE_SRC (object));
292
293   src = GST_FILE_SRC (object);
294
295   switch (prop_id) {
296     case PROP_LOCATION:
297       gst_file_src_set_location (src, g_value_get_string (value), NULL);
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302   }
303 }
304
305 static void
306 gst_file_src_get_property (GObject * object, guint prop_id, GValue * value,
307     GParamSpec * pspec)
308 {
309   GstFileSrc *src;
310
311   g_return_if_fail (GST_IS_FILE_SRC (object));
312
313   src = GST_FILE_SRC (object);
314
315   switch (prop_id) {
316     case PROP_LOCATION:
317       g_value_set_string (value, src->filename);
318       break;
319     default:
320       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321       break;
322   }
323 }
324
325 /***
326  * read code below
327  * that is to say, you shouldn't read the code below, but the code that reads
328  * stuff is below.  Well, you shouldn't not read the code below, feel free
329  * to read it of course.  It's just that "read code below" is a pretty crappy
330  * documentation string because it sounds like we're expecting you to read
331  * the code to understand what it does, which, while true, is really not
332  * the sort of attitude we want to be advertising.  No sir.
333  *
334  */
335 static GstFlowReturn
336 gst_file_src_fill (GstBaseSrc * basesrc, guint64 offset, guint length,
337     GstBuffer * buf)
338 {
339   GstFileSrc *src;
340   guint to_read, bytes_read;
341   int ret;
342   GstMapInfo info;
343   guint8 *data;
344
345   src = GST_FILE_SRC_CAST (basesrc);
346
347   if (G_UNLIKELY (offset != -1 && src->read_position != offset)) {
348     off_t res;
349
350     res = lseek (src->fd, offset, SEEK_SET);
351     if (G_UNLIKELY (res < 0 || res != offset))
352       goto seek_failed;
353
354     src->read_position = offset;
355   }
356
357   if (!gst_buffer_map (buf, &info, GST_MAP_WRITE))
358     goto buffer_write_fail;
359   data = info.data;
360
361   bytes_read = 0;
362   to_read = length;
363   while (to_read > 0) {
364     GST_LOG_OBJECT (src, "Reading %d bytes at offset 0x%" G_GINT64_MODIFIER "x",
365         to_read, offset + bytes_read);
366     errno = 0;
367     ret = read (src->fd, data + bytes_read, to_read);
368     if (G_UNLIKELY (ret < 0)) {
369       if (errno == EAGAIN || errno == EINTR)
370         continue;
371       goto could_not_read;
372     }
373
374     /* files should eos if they read 0 and more was requested */
375     if (G_UNLIKELY (ret == 0)) {
376       /* .. but first we should return any remaining data */
377       if (bytes_read > 0)
378         break;
379       goto eos;
380     }
381
382     to_read -= ret;
383     bytes_read += ret;
384
385     src->read_position += ret;
386   }
387
388   gst_buffer_unmap (buf, &info);
389   if (bytes_read != length)
390     gst_buffer_resize (buf, 0, bytes_read);
391
392   GST_BUFFER_OFFSET (buf) = offset;
393   GST_BUFFER_OFFSET_END (buf) = offset + bytes_read;
394
395   return GST_FLOW_OK;
396
397   /* ERROR */
398 seek_failed:
399   {
400     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
401     return GST_FLOW_ERROR;
402   }
403 could_not_read:
404   {
405     GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
406     gst_buffer_unmap (buf, &info);
407     gst_buffer_resize (buf, 0, 0);
408     return GST_FLOW_ERROR;
409   }
410 eos:
411   {
412     GST_DEBUG ("EOS");
413     gst_buffer_unmap (buf, &info);
414     gst_buffer_resize (buf, 0, 0);
415     return GST_FLOW_EOS;
416   }
417 buffer_write_fail:
418   {
419     GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL), ("Can't write to buffer"));
420     return GST_FLOW_ERROR;
421   }
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, 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, lseek failed: %s",
500           g_strerror (errno));
501       src->seekable = FALSE;
502     } else {
503       res = lseek (src->fd, 0, SEEK_SET);
504
505       if (res < 0) {
506         /* We really don't like not being able to go back to 0 */
507         src->seekable = FALSE;
508         goto lseek_wonky;
509       }
510
511       src->seekable = TRUE;
512     }
513   }
514
515   /* We can only really do seeking on regular files - for other file types, we
516    * don't know their length, so seeking isn't useful/meaningful */
517   src->seekable = src->seekable && src->is_regular;
518
519   gst_base_src_set_dynamic_size (basesrc, src->seekable);
520
521   return TRUE;
522
523   /* ERROR */
524 no_filename:
525   {
526     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND,
527         (_("No file name specified for reading.")), (NULL));
528     goto error_exit;
529   }
530 open_failed:
531   {
532     switch (errno) {
533       case ENOENT:
534         GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
535             ("No such file \"%s\"", src->filename));
536         break;
537       default:
538         GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
539             (_("Could not open file \"%s\" for reading."), src->filename),
540             GST_ERROR_SYSTEM);
541         break;
542     }
543     goto error_exit;
544   }
545 no_stat:
546   {
547     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
548         (_("Could not get info on \"%s\"."), src->filename), (NULL));
549     goto error_close;
550   }
551 was_directory:
552   {
553     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
554         (_("\"%s\" is a directory."), src->filename), (NULL));
555     goto error_close;
556   }
557 was_socket:
558   {
559     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
560         (_("File \"%s\" is a socket."), src->filename), (NULL));
561     goto error_close;
562   }
563 lseek_wonky:
564   {
565     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
566         ("Could not seek back to zero after seek test in file \"%s\"",
567             src->filename));
568     goto error_close;
569   }
570 error_close:
571   close (src->fd);
572 error_exit:
573   return FALSE;
574 }
575
576 /* unmap and close the file */
577 static gboolean
578 gst_file_src_stop (GstBaseSrc * basesrc)
579 {
580   GstFileSrc *src = GST_FILE_SRC (basesrc);
581
582   /* close the file */
583   close (src->fd);
584
585   /* zero out a lot of our state */
586   src->fd = 0;
587   src->is_regular = FALSE;
588
589   return TRUE;
590 }
591
592 /*** GSTURIHANDLER INTERFACE *************************************************/
593
594 static GstURIType
595 gst_file_src_uri_get_type (GType type)
596 {
597   return GST_URI_SRC;
598 }
599
600 static const gchar *const *
601 gst_file_src_uri_get_protocols (GType type)
602 {
603   static const gchar *protocols[] = { "file", NULL };
604
605   return protocols;
606 }
607
608 static gchar *
609 gst_file_src_uri_get_uri (GstURIHandler * handler)
610 {
611   GstFileSrc *src = GST_FILE_SRC (handler);
612
613   /* FIXME: make thread-safe */
614   return g_strdup (src->uri);
615 }
616
617 static gboolean
618 gst_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
619     GError ** err)
620 {
621   gchar *location, *hostname = NULL;
622   gboolean ret = FALSE;
623   GstFileSrc *src = GST_FILE_SRC (handler);
624
625   if (strcmp (uri, "file://") == 0) {
626     /* Special case for "file://" as this is used by some applications
627      *  to test with gst_element_make_from_uri if there's an element
628      *  that supports the URI protocol. */
629     gst_file_src_set_location (src, NULL, NULL);
630     return TRUE;
631   }
632
633   location = g_filename_from_uri (uri, &hostname, err);
634
635   if (!location || (err != NULL && *err != NULL)) {
636     GST_WARNING_OBJECT (src, "Invalid URI '%s' for filesrc: %s", uri,
637         (err != NULL && *err != NULL) ? (*err)->message : "unknown error");
638     goto beach;
639   }
640
641   if ((hostname) && (strcmp (hostname, "localhost"))) {
642     /* Only 'localhost' is permitted */
643     GST_WARNING_OBJECT (src, "Invalid hostname '%s' for filesrc", hostname);
644     g_set_error (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
645         "File URI with invalid hostname '%s'", hostname);
646     goto beach;
647   }
648 #ifdef G_OS_WIN32
649   /* Unfortunately, g_filename_from_uri() doesn't handle some UNC paths
650    * correctly on windows, it leaves them with an extra backslash
651    * at the start if they're of the mozilla-style file://///host/path/file
652    * form. Correct this.
653    */
654   if (location[0] == '\\' && location[1] == '\\' && location[2] == '\\')
655     memmove (location, location + 1, strlen (location + 1) + 1);
656 #endif
657
658   ret = gst_file_src_set_location (src, location, err);
659
660 beach:
661   if (location)
662     g_free (location);
663   if (hostname)
664     g_free (hostname);
665
666   return ret;
667 }
668
669 static void
670 gst_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
671 {
672   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
673
674   iface->get_type = gst_file_src_uri_get_type;
675   iface->get_protocols = gst_file_src_uri_get_protocols;
676   iface->get_uri = gst_file_src_uri_get_uri;
677   iface->set_uri = gst_file_src_uri_set_uri;
678 }