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