Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / plugins / elements / gstfilesink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2006 Wim Taymans <wim@fluendo.com>
5  *
6  * gstfilesink.c:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-filesink
25  * @see_also: #GstFileSrc
26  *
27  * Write incoming data to a file in the local file system.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
31  * |[
32  * gst-launch v4l2src num-buffers=1 ! jpegenc ! filesink location=capture1.jpeg
33  * ]| Capture one frame from a v4l2 camera and save as jpeg image.
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #  include "config.h"
39 #endif
40
41 #include "../../gst/gst-i18n-lib.h"
42
43 #include <gst/gst.h>
44 #include <stdio.h>              /* for fseeko() */
45 #ifdef HAVE_STDIO_EXT_H
46 #include <stdio_ext.h>          /* for __fbufsize, for debugging */
47 #endif
48 #include <errno.h>
49 #include "gstfilesink.h"
50 #include <string.h>
51 #include <sys/types.h>
52
53 #ifdef G_OS_WIN32
54 #include <io.h>                 /* lseek, open, close, read */
55 #undef lseek
56 #define lseek _lseeki64
57 #undef off_t
58 #define off_t guint64
59 #ifdef _MSC_VER                 /* Check if we are using MSVC, fileno is deprecated in favour */
60 #define fileno _fileno          /* of _fileno */
61 #endif
62 #endif
63
64 #include <sys/stat.h>
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #endif
68
69 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
70     GST_PAD_SINK,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS_ANY);
73
74 #define GST_TYPE_BUFFER_MODE (buffer_mode_get_type ())
75 static GType
76 buffer_mode_get_type (void)
77 {
78   static GType buffer_mode_type = 0;
79   static const GEnumValue buffer_mode[] = {
80     {-1, "Default buffering", "default"},
81     {_IOFBF, "Fully buffered", "full"},
82     {_IOLBF, "Line buffered", "line"},
83     {_IONBF, "Unbuffered", "unbuffered"},
84     {0, NULL, NULL},
85   };
86
87   if (!buffer_mode_type) {
88     buffer_mode_type =
89         g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
90   }
91   return buffer_mode_type;
92 }
93
94 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
95 #define GST_CAT_DEFAULT gst_file_sink_debug
96
97 #define DEFAULT_LOCATION        NULL
98 #define DEFAULT_BUFFER_MODE     -1
99 #define DEFAULT_BUFFER_SIZE     64 * 1024
100 #define DEFAULT_APPEND          FALSE
101
102 enum
103 {
104   PROP_0,
105   PROP_LOCATION,
106   PROP_BUFFER_MODE,
107   PROP_BUFFER_SIZE,
108   PROP_APPEND,
109   PROP_LAST
110 };
111
112 /* Copy of glib's g_fopen due to win32 libc/cross-DLL brokenness: we can't
113  * use the 'file pointer' opened in glib (and returned from this function)
114  * in this library, as they may have unrelated C runtimes. */
115 static FILE *
116 gst_fopen (const gchar * filename, const gchar * mode)
117 {
118 #ifdef G_OS_WIN32
119   wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
120   wchar_t *wmode;
121   FILE *retval;
122   int save_errno;
123
124   if (wfilename == NULL) {
125     errno = EINVAL;
126     return NULL;
127   }
128
129   wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
130
131   if (wmode == NULL) {
132     g_free (wfilename);
133     errno = EINVAL;
134     return NULL;
135   }
136
137   retval = _wfopen (wfilename, wmode);
138   save_errno = errno;
139
140   g_free (wfilename);
141   g_free (wmode);
142
143   errno = save_errno;
144   return retval;
145 #else
146   return fopen (filename, mode);
147 #endif
148 }
149
150 static void gst_file_sink_dispose (GObject * object);
151
152 static void gst_file_sink_set_property (GObject * object, guint prop_id,
153     const GValue * value, GParamSpec * pspec);
154 static void gst_file_sink_get_property (GObject * object, guint prop_id,
155     GValue * value, GParamSpec * pspec);
156
157 static gboolean gst_file_sink_open_file (GstFileSink * sink);
158 static void gst_file_sink_close_file (GstFileSink * sink);
159
160 static gboolean gst_file_sink_start (GstBaseSink * sink);
161 static gboolean gst_file_sink_stop (GstBaseSink * sink);
162 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
163 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
164     GstBuffer * buffer);
165
166 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
167     guint64 new_offset);
168 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
169     guint64 * p_pos);
170
171 static gboolean gst_file_sink_query (GstBaseSink * bsink, GstQuery * query);
172
173 static void gst_file_sink_uri_handler_init (gpointer g_iface,
174     gpointer iface_data);
175
176 #define _do_init \
177   G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_file_sink_uri_handler_init); \
178   GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0, "filesink element");
179 #define gst_file_sink_parent_class parent_class
180 G_DEFINE_TYPE_WITH_CODE (GstFileSink, gst_file_sink, GST_TYPE_BASE_SINK,
181     _do_init);
182
183 static void
184 gst_file_sink_class_init (GstFileSinkClass * klass)
185 {
186   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
188   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
189
190   gobject_class->dispose = gst_file_sink_dispose;
191
192   gobject_class->set_property = gst_file_sink_set_property;
193   gobject_class->get_property = gst_file_sink_get_property;
194
195   g_object_class_install_property (gobject_class, PROP_LOCATION,
196       g_param_spec_string ("location", "File Location",
197           "Location of the file to write", NULL,
198           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199
200   g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
201       g_param_spec_enum ("buffer-mode", "Buffering mode",
202           "The buffering mode to use", GST_TYPE_BUFFER_MODE,
203           DEFAULT_BUFFER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
206       g_param_spec_uint ("buffer-size", "Buffering size",
207           "Size of buffer in number of bytes for line or full buffer-mode", 0,
208           G_MAXUINT, DEFAULT_BUFFER_SIZE,
209           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210
211   /**
212    * GstFileSink:append
213    * 
214    * Append to an already existing file.
215    *
216    * Since: 0.10.25
217    */
218   g_object_class_install_property (gobject_class, PROP_APPEND,
219       g_param_spec_boolean ("append", "Append",
220           "Append to an already existing file", DEFAULT_APPEND,
221           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222
223   gst_element_class_set_details_simple (gstelement_class,
224       "File Sink",
225       "Sink/File", "Write stream to a file",
226       "Thomas Vander Stichele <thomas at apestaart dot org>");
227   gst_element_class_add_pad_template (gstelement_class,
228       gst_static_pad_template_get (&sinktemplate));
229
230   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
231   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
232   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
233   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
234   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
235
236   if (sizeof (off_t) < 8) {
237     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
238         sizeof (off_t));
239   }
240 }
241
242 static void
243 gst_file_sink_init (GstFileSink * filesink)
244 {
245   filesink->filename = NULL;
246   filesink->file = NULL;
247   filesink->buffer_mode = DEFAULT_BUFFER_MODE;
248   filesink->buffer_size = DEFAULT_BUFFER_SIZE;
249   filesink->buffer = NULL;
250   filesink->append = FALSE;
251
252   gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
253 }
254
255 static void
256 gst_file_sink_dispose (GObject * object)
257 {
258   GstFileSink *sink = GST_FILE_SINK (object);
259
260   G_OBJECT_CLASS (parent_class)->dispose (object);
261
262   g_free (sink->uri);
263   sink->uri = NULL;
264   g_free (sink->filename);
265   sink->filename = NULL;
266   g_free (sink->buffer);
267   sink->buffer = NULL;
268   sink->buffer_size = 0;
269 }
270
271 static gboolean
272 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
273 {
274   if (sink->file)
275     goto was_open;
276
277   g_free (sink->filename);
278   g_free (sink->uri);
279   if (location != NULL) {
280     /* we store the filename as we received it from the application. On Windows
281      * this should be in UTF8 */
282     sink->filename = g_strdup (location);
283     sink->uri = gst_filename_to_uri (location, NULL);
284     GST_INFO ("filename : %s", sink->filename);
285     GST_INFO ("uri      : %s", sink->uri);
286   } else {
287     sink->filename = NULL;
288     sink->uri = NULL;
289   }
290
291   return TRUE;
292
293   /* ERRORS */
294 was_open:
295   {
296     g_warning ("Changing the `location' property on filesink when a file is "
297         "open is not supported.");
298     return FALSE;
299   }
300 }
301
302 static void
303 gst_file_sink_set_property (GObject * object, guint prop_id,
304     const GValue * value, GParamSpec * pspec)
305 {
306   GstFileSink *sink = GST_FILE_SINK (object);
307
308   switch (prop_id) {
309     case PROP_LOCATION:
310       gst_file_sink_set_location (sink, g_value_get_string (value));
311       break;
312     case PROP_BUFFER_MODE:
313       sink->buffer_mode = g_value_get_enum (value);
314       break;
315     case PROP_BUFFER_SIZE:
316       sink->buffer_size = g_value_get_uint (value);
317       break;
318     case PROP_APPEND:
319       sink->append = g_value_get_boolean (value);
320       break;
321     default:
322       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323       break;
324   }
325 }
326
327 static void
328 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
329     GParamSpec * pspec)
330 {
331   GstFileSink *sink = GST_FILE_SINK (object);
332
333   switch (prop_id) {
334     case PROP_LOCATION:
335       g_value_set_string (value, sink->filename);
336       break;
337     case PROP_BUFFER_MODE:
338       g_value_set_enum (value, sink->buffer_mode);
339       break;
340     case PROP_BUFFER_SIZE:
341       g_value_set_uint (value, sink->buffer_size);
342       break;
343     case PROP_APPEND:
344       g_value_set_boolean (value, sink->append);
345       break;
346     default:
347       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
348       break;
349   }
350 }
351
352 static gboolean
353 gst_file_sink_open_file (GstFileSink * sink)
354 {
355   gint mode;
356
357   /* open the file */
358   if (sink->filename == NULL || sink->filename[0] == '\0')
359     goto no_filename;
360
361   if (sink->append)
362     sink->file = gst_fopen (sink->filename, "ab");
363   else
364     sink->file = gst_fopen (sink->filename, "wb");
365   if (sink->file == NULL)
366     goto open_failed;
367
368   /* see if we are asked to perform a specific kind of buffering */
369   if ((mode = sink->buffer_mode) != -1) {
370     guint buffer_size;
371
372     /* free previous buffer if any */
373     g_free (sink->buffer);
374
375     if (mode == _IONBF) {
376       /* no buffering */
377       sink->buffer = NULL;
378       buffer_size = 0;
379     } else {
380       /* allocate buffer */
381       sink->buffer = g_malloc (sink->buffer_size);
382       buffer_size = sink->buffer_size;
383     }
384 #ifdef HAVE_STDIO_EXT_H
385     GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
386         (guint) __fbufsize (sink->file), buffer_size, mode);
387 #else
388     GST_DEBUG_OBJECT (sink, "change  buffer size to %u, mode %d",
389         sink->buffer_size, mode);
390 #endif
391     if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
392       GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
393           g_strerror (errno));
394     }
395   }
396
397   sink->current_pos = 0;
398   /* try to seek in the file to figure out if it is seekable */
399   sink->seekable = gst_file_sink_do_seek (sink, 0);
400
401   GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
402       sink->filename, sink->seekable);
403
404   return TRUE;
405
406   /* ERRORS */
407 no_filename:
408   {
409     GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
410         (_("No file name specified for writing.")), (NULL));
411     return FALSE;
412   }
413 open_failed:
414   {
415     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
416         (_("Could not open file \"%s\" for writing."), sink->filename),
417         GST_ERROR_SYSTEM);
418     return FALSE;
419   }
420 }
421
422 static void
423 gst_file_sink_close_file (GstFileSink * sink)
424 {
425   if (sink->file) {
426     if (fclose (sink->file) != 0)
427       goto close_failed;
428
429     GST_DEBUG_OBJECT (sink, "closed file");
430     sink->file = NULL;
431
432     g_free (sink->buffer);
433     sink->buffer = NULL;
434   }
435   return;
436
437   /* ERRORS */
438 close_failed:
439   {
440     GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
441         (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
442     return;
443   }
444 }
445
446 static gboolean
447 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
448 {
449   gboolean res;
450   GstFileSink *self;
451   GstFormat format;
452
453   self = GST_FILE_SINK (bsink);
454
455   switch (GST_QUERY_TYPE (query)) {
456     case GST_QUERY_POSITION:
457       gst_query_parse_position (query, &format, NULL);
458
459       switch (format) {
460         case GST_FORMAT_DEFAULT:
461         case GST_FORMAT_BYTES:
462           gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
463           res = TRUE;
464           break;
465         default:
466           res = FALSE;
467           break;
468       }
469       break;
470
471     case GST_QUERY_FORMATS:
472       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
473       res = TRUE;
474       break;
475
476     case GST_QUERY_URI:
477       gst_query_set_uri (query, self->uri);
478       res = TRUE;
479       break;
480
481     default:
482       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
483       break;
484   }
485   return res;
486 }
487
488 #ifdef HAVE_FSEEKO
489 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
490 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
491 # define __GST_STDIO_SEEK_FUNCTION "lseek"
492 #else
493 # define __GST_STDIO_SEEK_FUNCTION "fseek"
494 #endif
495
496 static gboolean
497 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
498 {
499   GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
500       " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
501
502   if (fflush (filesink->file))
503     goto flush_failed;
504
505 #ifdef HAVE_FSEEKO
506   if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
507     goto seek_failed;
508 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
509   if (lseek (fileno (filesink->file), (off_t) new_offset,
510           SEEK_SET) == (off_t) - 1)
511     goto seek_failed;
512 #else
513   if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
514     goto seek_failed;
515 #endif
516
517   /* adjust position reporting after seek;
518    * presumably this should basically yield new_offset */
519   gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
520
521   return TRUE;
522
523   /* ERRORS */
524 flush_failed:
525   {
526     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
527     return FALSE;
528   }
529 seek_failed:
530   {
531     GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
532     return FALSE;
533   }
534 }
535
536 /* handle events (search) */
537 static gboolean
538 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
539 {
540   GstEventType type;
541   GstFileSink *filesink;
542
543   filesink = GST_FILE_SINK (sink);
544
545   type = GST_EVENT_TYPE (event);
546
547   switch (type) {
548     case GST_EVENT_SEGMENT:
549     {
550       const GstSegment *segment;
551
552       gst_event_parse_segment (event, &segment);
553
554       if (segment->format == GST_FORMAT_BYTES) {
555         /* only try to seek and fail when we are going to a different
556          * position */
557         if (filesink->current_pos != segment->start) {
558           /* FIXME, the seek should be performed on the pos field, start/stop are
559            * just boundaries for valid bytes offsets. We should also fill the file
560            * with zeroes if the new position extends the current EOF (sparse streams
561            * and segment accumulation). */
562           if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
563             goto seek_failed;
564         } else {
565           GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
566         }
567       } else {
568         GST_DEBUG_OBJECT (filesink,
569             "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
570             gst_format_get_name (segment->format));
571       }
572       break;
573     }
574     case GST_EVENT_EOS:
575       if (fflush (filesink->file))
576         goto flush_failed;
577       break;
578     default:
579       break;
580   }
581
582   return TRUE;
583
584   /* ERRORS */
585 seek_failed:
586   {
587     GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
588         (_("Error while seeking in file \"%s\"."), filesink->filename),
589         GST_ERROR_SYSTEM);
590     return FALSE;
591   }
592 flush_failed:
593   {
594     GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
595         (_("Error while writing to file \"%s\"."), filesink->filename),
596         GST_ERROR_SYSTEM);
597     return FALSE;
598   }
599 }
600
601 static gboolean
602 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
603 {
604   off_t ret = -1;
605
606 #ifdef HAVE_FTELLO
607   ret = ftello (filesink->file);
608 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
609   if (fflush (filesink->file)) {
610     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
611     /* ignore and continue */
612   }
613   ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
614 #else
615   ret = (off_t) ftell (filesink->file);
616 #endif
617
618   if (ret != (off_t) - 1)
619     *p_pos = (guint64) ret;
620
621   return (ret != (off_t) - 1);
622 }
623
624 static GstFlowReturn
625 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
626 {
627   GstFileSink *filesink;
628   gsize size;
629   guint8 *data;
630
631   filesink = GST_FILE_SINK (sink);
632
633   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
634
635   GST_DEBUG_OBJECT (filesink,
636       "writing %" G_GSIZE_FORMAT " bytes at %" G_GUINT64_FORMAT,
637       size, filesink->current_pos);
638
639   if (size > 0 && data != NULL) {
640     if (fwrite (data, size, 1, filesink->file) != 1)
641       goto handle_error;
642
643     filesink->current_pos += size;
644   }
645   gst_buffer_unmap (buffer, data, size);
646
647   return GST_FLOW_OK;
648
649 handle_error:
650   {
651     switch (errno) {
652       case ENOSPC:{
653         GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
654         break;
655       }
656       default:{
657         GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
658             (_("Error while writing to file \"%s\"."), filesink->filename),
659             ("%s", g_strerror (errno)));
660       }
661     }
662     gst_buffer_unmap (buffer, data, size);
663     return GST_FLOW_ERROR;
664   }
665 }
666
667 static gboolean
668 gst_file_sink_start (GstBaseSink * basesink)
669 {
670   return gst_file_sink_open_file (GST_FILE_SINK (basesink));
671 }
672
673 static gboolean
674 gst_file_sink_stop (GstBaseSink * basesink)
675 {
676   gst_file_sink_close_file (GST_FILE_SINK (basesink));
677   return TRUE;
678 }
679
680 /*** GSTURIHANDLER INTERFACE *************************************************/
681
682 static GstURIType
683 gst_file_sink_uri_get_type (GType type)
684 {
685   return GST_URI_SINK;
686 }
687
688 static gchar **
689 gst_file_sink_uri_get_protocols (GType type)
690 {
691   static gchar *protocols[] = { (char *) "file", NULL };
692
693   return protocols;
694 }
695
696 static const gchar *
697 gst_file_sink_uri_get_uri (GstURIHandler * handler)
698 {
699   GstFileSink *sink = GST_FILE_SINK (handler);
700
701   return sink->uri;
702 }
703
704 static gboolean
705 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
706 {
707   gchar *protocol, *location;
708   gboolean ret;
709   GstFileSink *sink = GST_FILE_SINK (handler);
710
711   protocol = gst_uri_get_protocol (uri);
712   if (strcmp (protocol, "file") != 0) {
713     g_free (protocol);
714     return FALSE;
715   }
716   g_free (protocol);
717
718   /* allow file://localhost/foo/bar by stripping localhost but fail
719    * for every other hostname */
720   if (g_str_has_prefix (uri, "file://localhost/")) {
721     char *tmp;
722
723     /* 16 == strlen ("file://localhost") */
724     tmp = g_strconcat ("file://", uri + 16, NULL);
725     /* we use gst_uri_get_location() although we already have the
726      * "location" with uri + 16 because it provides unescaping */
727     location = gst_uri_get_location (tmp);
728     g_free (tmp);
729   } else if (strcmp (uri, "file://") == 0) {
730     /* Special case for "file://" as this is used by some applications
731      *  to test with gst_element_make_from_uri if there's an element
732      *  that supports the URI protocol. */
733     gst_file_sink_set_location (sink, NULL);
734     return TRUE;
735   } else {
736     location = gst_uri_get_location (uri);
737   }
738
739   if (!location)
740     return FALSE;
741   if (!g_path_is_absolute (location)) {
742     g_free (location);
743     return FALSE;
744   }
745
746   ret = gst_file_sink_set_location (sink, location);
747   g_free (location);
748
749   return ret;
750 }
751
752 static void
753 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
754 {
755   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
756
757   iface->get_type = gst_file_sink_uri_get_type;
758   iface->get_protocols = gst_file_sink_uri_get_protocols;
759   iface->get_uri = gst_file_sink_uri_get_uri;
760   iface->set_uri = gst_file_sink_uri_set_uri;
761 }