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