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