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