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