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