Fix FSF address
[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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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   g_object_class_install_property (gobject_class, PROP_APPEND,
217       g_param_spec_boolean ("append", "Append",
218           "Append to an already existing file", DEFAULT_APPEND,
219           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220
221   gst_element_class_set_static_metadata (gstelement_class,
222       "File Sink",
223       "Sink/File", "Write stream to a file",
224       "Thomas Vander Stichele <thomas at apestaart dot org>");
225   gst_element_class_add_pad_template (gstelement_class,
226       gst_static_pad_template_get (&sinktemplate));
227
228   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
229   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
230   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_file_sink_query);
231   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
232   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
233
234   if (sizeof (off_t) < 8) {
235     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
236         sizeof (off_t));
237   }
238 }
239
240 static void
241 gst_file_sink_init (GstFileSink * filesink)
242 {
243   filesink->filename = NULL;
244   filesink->file = NULL;
245   filesink->buffer_mode = DEFAULT_BUFFER_MODE;
246   filesink->buffer_size = DEFAULT_BUFFER_SIZE;
247   filesink->buffer = NULL;
248   filesink->append = FALSE;
249
250   gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
251 }
252
253 static void
254 gst_file_sink_dispose (GObject * object)
255 {
256   GstFileSink *sink = GST_FILE_SINK (object);
257
258   G_OBJECT_CLASS (parent_class)->dispose (object);
259
260   g_free (sink->uri);
261   sink->uri = NULL;
262   g_free (sink->filename);
263   sink->filename = NULL;
264   g_free (sink->buffer);
265   sink->buffer = NULL;
266   sink->buffer_size = 0;
267 }
268
269 static gboolean
270 gst_file_sink_set_location (GstFileSink * sink, const gchar * location,
271     GError ** error)
272 {
273   if (sink->file)
274     goto was_open;
275
276   g_free (sink->filename);
277   g_free (sink->uri);
278   if (location != NULL) {
279     /* we store the filename as we received it from the application. On Windows
280      * this should be in UTF8 */
281     sink->filename = g_strdup (location);
282     sink->uri = gst_filename_to_uri (location, NULL);
283     GST_INFO ("filename : %s", sink->filename);
284     GST_INFO ("uri      : %s", sink->uri);
285   } else {
286     sink->filename = NULL;
287     sink->uri = NULL;
288   }
289
290   return TRUE;
291
292   /* ERRORS */
293 was_open:
294   {
295     g_warning ("Changing the `location' property on filesink when a file is "
296         "open is not supported.");
297     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
298         "Changing the 'location' property on filesink when a file is "
299         "open is not supported");
300     return FALSE;
301   }
302 }
303
304 static void
305 gst_file_sink_set_property (GObject * object, guint prop_id,
306     const GValue * value, GParamSpec * pspec)
307 {
308   GstFileSink *sink = GST_FILE_SINK (object);
309
310   switch (prop_id) {
311     case PROP_LOCATION:
312       gst_file_sink_set_location (sink, g_value_get_string (value), NULL);
313       break;
314     case PROP_BUFFER_MODE:
315       sink->buffer_mode = g_value_get_enum (value);
316       break;
317     case PROP_BUFFER_SIZE:
318       sink->buffer_size = g_value_get_uint (value);
319       break;
320     case PROP_APPEND:
321       sink->append = g_value_get_boolean (value);
322       break;
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }
328
329 static void
330 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
331     GParamSpec * pspec)
332 {
333   GstFileSink *sink = GST_FILE_SINK (object);
334
335   switch (prop_id) {
336     case PROP_LOCATION:
337       g_value_set_string (value, sink->filename);
338       break;
339     case PROP_BUFFER_MODE:
340       g_value_set_enum (value, sink->buffer_mode);
341       break;
342     case PROP_BUFFER_SIZE:
343       g_value_set_uint (value, sink->buffer_size);
344       break;
345     case PROP_APPEND:
346       g_value_set_boolean (value, sink->append);
347       break;
348     default:
349       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
350       break;
351   }
352 }
353
354 static gboolean
355 gst_file_sink_open_file (GstFileSink * sink)
356 {
357   gint mode;
358
359   /* open the file */
360   if (sink->filename == NULL || sink->filename[0] == '\0')
361     goto no_filename;
362
363   if (sink->append)
364     sink->file = gst_fopen (sink->filename, "ab");
365   else
366     sink->file = gst_fopen (sink->filename, "wb");
367   if (sink->file == NULL)
368     goto open_failed;
369
370   /* see if we are asked to perform a specific kind of buffering */
371   if ((mode = sink->buffer_mode) != -1) {
372     guint buffer_size;
373
374     /* free previous buffer if any */
375     g_free (sink->buffer);
376
377     if (mode == _IONBF) {
378       /* no buffering */
379       sink->buffer = NULL;
380       buffer_size = 0;
381     } else {
382       /* allocate buffer */
383       sink->buffer = g_malloc (sink->buffer_size);
384       buffer_size = sink->buffer_size;
385     }
386     /* Cygwin does not have __fbufsize */
387 #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__)
388     GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d",
389         (guint) __fbufsize (sink->file), buffer_size, mode);
390 #else
391     GST_DEBUG_OBJECT (sink, "change  buffer size to %u, mode %d",
392         sink->buffer_size, mode);
393 #endif
394     if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
395       GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
396           g_strerror (errno));
397     }
398   }
399
400   sink->current_pos = 0;
401   /* try to seek in the file to figure out if it is seekable */
402   sink->seekable = gst_file_sink_do_seek (sink, 0);
403
404   GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
405       sink->filename, sink->seekable);
406
407   return TRUE;
408
409   /* ERRORS */
410 no_filename:
411   {
412     GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
413         (_("No file name specified for writing.")), (NULL));
414     return FALSE;
415   }
416 open_failed:
417   {
418     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
419         (_("Could not open file \"%s\" for writing."), sink->filename),
420         GST_ERROR_SYSTEM);
421     return FALSE;
422   }
423 }
424
425 static void
426 gst_file_sink_close_file (GstFileSink * sink)
427 {
428   if (sink->file) {
429     if (fclose (sink->file) != 0)
430       goto close_failed;
431
432     GST_DEBUG_OBJECT (sink, "closed file");
433     sink->file = NULL;
434
435     g_free (sink->buffer);
436     sink->buffer = NULL;
437   }
438   return;
439
440   /* ERRORS */
441 close_failed:
442   {
443     GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
444         (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
445     return;
446   }
447 }
448
449 static gboolean
450 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
451 {
452   gboolean res;
453   GstFileSink *self;
454   GstFormat format;
455
456   self = GST_FILE_SINK (bsink);
457
458   switch (GST_QUERY_TYPE (query)) {
459     case GST_QUERY_POSITION:
460       gst_query_parse_position (query, &format, NULL);
461
462       switch (format) {
463         case GST_FORMAT_DEFAULT:
464         case GST_FORMAT_BYTES:
465           gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
466           res = TRUE;
467           break;
468         default:
469           res = FALSE;
470           break;
471       }
472       break;
473
474     case GST_QUERY_FORMATS:
475       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
476       res = TRUE;
477       break;
478
479     case GST_QUERY_URI:
480       gst_query_set_uri (query, self->uri);
481       res = TRUE;
482       break;
483
484     case GST_QUERY_SEEKING:
485       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
486       if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
487         gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
488       } else {
489         gst_query_set_seeking (query, format, FALSE, 0, -1);
490       }
491       res = TRUE;
492       break;
493
494     default:
495       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
496       break;
497   }
498   return res;
499 }
500
501 #ifdef HAVE_FSEEKO
502 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
503 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
504 # define __GST_STDIO_SEEK_FUNCTION "lseek"
505 #else
506 # define __GST_STDIO_SEEK_FUNCTION "fseek"
507 #endif
508
509 static gboolean
510 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
511 {
512   GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
513       " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
514
515   if (fflush (filesink->file))
516     goto flush_failed;
517
518 #ifdef HAVE_FSEEKO
519   if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
520     goto seek_failed;
521 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
522   if (lseek (fileno (filesink->file), (off_t) new_offset,
523           SEEK_SET) == (off_t) - 1)
524     goto seek_failed;
525 #else
526   if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
527     goto seek_failed;
528 #endif
529
530   /* adjust position reporting after seek;
531    * presumably this should basically yield new_offset */
532   gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
533
534   return TRUE;
535
536   /* ERRORS */
537 flush_failed:
538   {
539     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
540     return FALSE;
541   }
542 seek_failed:
543   {
544     GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
545     return FALSE;
546   }
547 }
548
549 /* handle events (search) */
550 static gboolean
551 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
552 {
553   GstEventType type;
554   GstFileSink *filesink;
555
556   filesink = GST_FILE_SINK (sink);
557
558   type = GST_EVENT_TYPE (event);
559
560   switch (type) {
561     case GST_EVENT_SEGMENT:
562     {
563       const GstSegment *segment;
564
565       gst_event_parse_segment (event, &segment);
566
567       if (segment->format == GST_FORMAT_BYTES) {
568         /* only try to seek and fail when we are going to a different
569          * position */
570         if (filesink->current_pos != segment->start) {
571           /* FIXME, the seek should be performed on the pos field, start/stop are
572            * just boundaries for valid bytes offsets. We should also fill the file
573            * with zeroes if the new position extends the current EOF (sparse streams
574            * and segment accumulation). */
575           if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
576             goto seek_failed;
577         } else {
578           GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
579         }
580       } else {
581         GST_DEBUG_OBJECT (filesink,
582             "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
583             gst_format_get_name (segment->format));
584       }
585       break;
586     }
587     case GST_EVENT_EOS:
588       if (fflush (filesink->file))
589         goto flush_failed;
590       break;
591     default:
592       break;
593   }
594
595   return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
596
597   /* ERRORS */
598 seek_failed:
599   {
600     GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
601         (_("Error while seeking in file \"%s\"."), filesink->filename),
602         GST_ERROR_SYSTEM);
603     gst_event_unref (event);
604     return FALSE;
605   }
606 flush_failed:
607   {
608     GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
609         (_("Error while writing to file \"%s\"."), filesink->filename),
610         GST_ERROR_SYSTEM);
611     gst_event_unref (event);
612     return FALSE;
613   }
614 }
615
616 static gboolean
617 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
618 {
619   off_t ret = -1;
620
621 #ifdef HAVE_FTELLO
622   ret = ftello (filesink->file);
623 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
624   if (fflush (filesink->file)) {
625     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
626     /* ignore and continue */
627   }
628   ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
629 #else
630   ret = (off_t) ftell (filesink->file);
631 #endif
632
633   if (ret != (off_t) - 1)
634     *p_pos = (guint64) ret;
635
636   return (ret != (off_t) - 1);
637 }
638
639 static GstFlowReturn
640 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
641 {
642   GstFileSink *filesink;
643   GstMapInfo info;
644
645   filesink = GST_FILE_SINK (sink);
646
647   gst_buffer_map (buffer, &info, GST_MAP_READ);
648
649   GST_DEBUG_OBJECT (filesink,
650       "writing %" G_GSIZE_FORMAT " bytes at %" G_GUINT64_FORMAT,
651       info.size, filesink->current_pos);
652
653   if (info.size > 0 && info.data != NULL) {
654     if (fwrite (info.data, info.size, 1, filesink->file) != 1)
655       goto handle_error;
656
657     filesink->current_pos += info.size;
658   }
659   gst_buffer_unmap (buffer, &info);
660
661   return GST_FLOW_OK;
662
663 handle_error:
664   {
665     switch (errno) {
666       case ENOSPC:{
667         GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
668         break;
669       }
670       default:{
671         GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
672             (_("Error while writing to file \"%s\"."), filesink->filename),
673             ("%s", g_strerror (errno)));
674       }
675     }
676     gst_buffer_unmap (buffer, &info);
677     return GST_FLOW_ERROR;
678   }
679 }
680
681 static gboolean
682 gst_file_sink_start (GstBaseSink * basesink)
683 {
684   return gst_file_sink_open_file (GST_FILE_SINK (basesink));
685 }
686
687 static gboolean
688 gst_file_sink_stop (GstBaseSink * basesink)
689 {
690   gst_file_sink_close_file (GST_FILE_SINK (basesink));
691   return TRUE;
692 }
693
694 /*** GSTURIHANDLER INTERFACE *************************************************/
695
696 static GstURIType
697 gst_file_sink_uri_get_type (GType type)
698 {
699   return GST_URI_SINK;
700 }
701
702 static const gchar *const *
703 gst_file_sink_uri_get_protocols (GType type)
704 {
705   static const gchar *protocols[] = { "file", NULL };
706
707   return protocols;
708 }
709
710 static gchar *
711 gst_file_sink_uri_get_uri (GstURIHandler * handler)
712 {
713   GstFileSink *sink = GST_FILE_SINK (handler);
714
715   /* FIXME: make thread-safe */
716   return g_strdup (sink->uri);
717 }
718
719 static gboolean
720 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
721     GError ** error)
722 {
723   gchar *location;
724   gboolean ret;
725   GstFileSink *sink = GST_FILE_SINK (handler);
726
727   /* allow file://localhost/foo/bar by stripping localhost but fail
728    * for every other hostname */
729   if (g_str_has_prefix (uri, "file://localhost/")) {
730     char *tmp;
731
732     /* 16 == strlen ("file://localhost") */
733     tmp = g_strconcat ("file://", uri + 16, NULL);
734     /* we use gst_uri_get_location() although we already have the
735      * "location" with uri + 16 because it provides unescaping */
736     location = gst_uri_get_location (tmp);
737     g_free (tmp);
738   } else if (strcmp (uri, "file://") == 0) {
739     /* Special case for "file://" as this is used by some applications
740      *  to test with gst_element_make_from_uri if there's an element
741      *  that supports the URI protocol. */
742     gst_file_sink_set_location (sink, NULL, NULL);
743     return TRUE;
744   } else {
745     location = gst_uri_get_location (uri);
746   }
747
748   if (!location) {
749     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
750         "File URI without location");
751     return FALSE;
752   }
753
754   if (!g_path_is_absolute (location)) {
755     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
756         "File URI location must be an absolute path");
757     g_free (location);
758     return FALSE;
759   }
760
761   ret = gst_file_sink_set_location (sink, location, error);
762   g_free (location);
763
764   return ret;
765 }
766
767 static void
768 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
769 {
770   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
771
772   iface->get_type = gst_file_sink_uri_get_type;
773   iface->get_protocols = gst_file_sink_uri_get_protocols;
774   iface->get_uri = gst_file_sink_uri_get_uri;
775   iface->set_uri = gst_file_sink_uri_set_uri;
776 }