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