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