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