doc/filesink: Add BufferMode enumeration
[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 ("filename : %s", sink->filename);
296     GST_INFO ("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       goto close_failed;
443
444     GST_DEBUG_OBJECT (sink, "closed file");
445     sink->file = NULL;
446
447     g_free (sink->buffer);
448     sink->buffer = NULL;
449   }
450   return;
451
452   /* ERRORS */
453 close_failed:
454   {
455     GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
456         (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
457     return;
458   }
459 }
460
461 static gboolean
462 gst_file_sink_query (GstBaseSink * bsink, GstQuery * query)
463 {
464   gboolean res;
465   GstFileSink *self;
466   GstFormat format;
467
468   self = GST_FILE_SINK (bsink);
469
470   switch (GST_QUERY_TYPE (query)) {
471     case GST_QUERY_POSITION:
472       gst_query_parse_position (query, &format, NULL);
473
474       switch (format) {
475         case GST_FORMAT_DEFAULT:
476         case GST_FORMAT_BYTES:
477           gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
478           res = TRUE;
479           break;
480         default:
481           res = FALSE;
482           break;
483       }
484       break;
485
486     case GST_QUERY_FORMATS:
487       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
488       res = TRUE;
489       break;
490
491     case GST_QUERY_URI:
492       gst_query_set_uri (query, self->uri);
493       res = TRUE;
494       break;
495
496     case GST_QUERY_SEEKING:
497       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
498       if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
499         gst_query_set_seeking (query, GST_FORMAT_BYTES, self->seekable, 0, -1);
500       } else {
501         gst_query_set_seeking (query, format, FALSE, 0, -1);
502       }
503       res = TRUE;
504       break;
505
506     default:
507       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
508       break;
509   }
510   return res;
511 }
512
513 #ifdef HAVE_FSEEKO
514 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
515 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
516 # define __GST_STDIO_SEEK_FUNCTION "lseek"
517 #else
518 # define __GST_STDIO_SEEK_FUNCTION "fseek"
519 #endif
520
521 static gboolean
522 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
523 {
524   GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
525       " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
526
527   if (fflush (filesink->file))
528     goto flush_failed;
529
530 #ifdef HAVE_FSEEKO
531   if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
532     goto seek_failed;
533 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
534   if (lseek (fileno (filesink->file), (off_t) new_offset,
535           SEEK_SET) == (off_t) - 1)
536     goto seek_failed;
537 #else
538   if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
539     goto seek_failed;
540 #endif
541
542   /* adjust position reporting after seek;
543    * presumably this should basically yield new_offset */
544   gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
545
546   return TRUE;
547
548   /* ERRORS */
549 flush_failed:
550   {
551     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
552     return FALSE;
553   }
554 seek_failed:
555   {
556     GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
557     return FALSE;
558   }
559 }
560
561 /* handle events (search) */
562 static gboolean
563 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
564 {
565   GstEventType type;
566   GstFileSink *filesink;
567
568   filesink = GST_FILE_SINK (sink);
569
570   type = GST_EVENT_TYPE (event);
571
572   switch (type) {
573     case GST_EVENT_SEGMENT:
574     {
575       const GstSegment *segment;
576
577       gst_event_parse_segment (event, &segment);
578
579       if (segment->format == GST_FORMAT_BYTES) {
580         /* only try to seek and fail when we are going to a different
581          * position */
582         if (filesink->current_pos != segment->start) {
583           /* FIXME, the seek should be performed on the pos field, start/stop are
584            * just boundaries for valid bytes offsets. We should also fill the file
585            * with zeroes if the new position extends the current EOF (sparse streams
586            * and segment accumulation). */
587           if (!gst_file_sink_do_seek (filesink, (guint64) segment->start))
588             goto seek_failed;
589         } else {
590           GST_DEBUG_OBJECT (filesink, "Ignored SEGMENT, no seek needed");
591         }
592       } else {
593         GST_DEBUG_OBJECT (filesink,
594             "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
595             gst_format_get_name (segment->format));
596       }
597       break;
598     }
599     case GST_EVENT_FLUSH_STOP:
600       if (filesink->current_pos != 0 && filesink->seekable) {
601         gst_file_sink_do_seek (filesink, 0);
602         if (ftruncate (fileno (filesink->file), 0))
603           goto flush_failed;
604       }
605       break;
606     case GST_EVENT_EOS:
607       if (fflush (filesink->file))
608         goto flush_failed;
609       break;
610     default:
611       break;
612   }
613
614   return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
615
616   /* ERRORS */
617 seek_failed:
618   {
619     GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
620         (_("Error while seeking in file \"%s\"."), filesink->filename),
621         GST_ERROR_SYSTEM);
622     gst_event_unref (event);
623     return FALSE;
624   }
625 flush_failed:
626   {
627     GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
628         (_("Error while writing to file \"%s\"."), filesink->filename),
629         GST_ERROR_SYSTEM);
630     gst_event_unref (event);
631     return FALSE;
632   }
633 }
634
635 static gboolean
636 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
637 {
638   off_t ret = -1;
639
640 #ifdef HAVE_FTELLO
641   ret = ftello (filesink->file);
642 #elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
643   if (fflush (filesink->file)) {
644     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
645     /* ignore and continue */
646   }
647   ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
648 #else
649   ret = (off_t) ftell (filesink->file);
650 #endif
651
652   if (ret != (off_t) - 1)
653     *p_pos = (guint64) ret;
654
655   return (ret != (off_t) - 1);
656 }
657
658 static GstFlowReturn
659 gst_file_sink_render_buffers (GstFileSink * sink, GstBuffer ** buffers,
660     guint num_buffers, guint8 * mem_nums, guint total_mems)
661 {
662   GST_DEBUG_OBJECT (sink,
663       "writing %u buffers (%u memories) at position %" G_GUINT64_FORMAT,
664       num_buffers, total_mems, sink->current_pos);
665
666   return gst_writev_buffers (GST_OBJECT_CAST (sink), fileno (sink->file), NULL,
667       buffers, num_buffers, mem_nums, total_mems, NULL, &sink->current_pos);
668 }
669
670 static GstFlowReturn
671 gst_file_sink_render_list (GstBaseSink * bsink, GstBufferList * buffer_list)
672 {
673   GstFlowReturn flow;
674   GstBuffer **buffers;
675   GstFileSink *sink;
676   guint8 *mem_nums;
677   guint total_mems;
678   guint i, num_buffers;
679   gboolean sync_after = FALSE;
680
681   sink = GST_FILE_SINK_CAST (bsink);
682
683   num_buffers = gst_buffer_list_length (buffer_list);
684   if (num_buffers == 0)
685     goto no_data;
686
687   /* extract buffers from list and count memories */
688   buffers = g_newa (GstBuffer *, num_buffers);
689   mem_nums = g_newa (guint8, num_buffers);
690   for (i = 0, total_mems = 0; i < num_buffers; ++i) {
691     buffers[i] = gst_buffer_list_get (buffer_list, i);
692     mem_nums[i] = gst_buffer_n_memory (buffers[i]);
693     total_mems += mem_nums[i];
694     if (GST_BUFFER_FLAG_IS_SET (buffers[i], GST_BUFFER_FLAG_SYNC_AFTER))
695       sync_after = TRUE;
696   }
697
698   flow =
699       gst_file_sink_render_buffers (sink, buffers, num_buffers, mem_nums,
700       total_mems);
701
702   if (flow == GST_FLOW_OK && sync_after) {
703     if (fflush (sink->file) || fsync (fileno (sink->file))) {
704       GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
705           (_("Error while writing to file \"%s\"."), sink->filename),
706           ("%s", g_strerror (errno)));
707       flow = GST_FLOW_ERROR;
708     }
709   }
710
711   return flow;
712
713 no_data:
714   {
715     GST_LOG_OBJECT (sink, "empty buffer list");
716     return GST_FLOW_OK;
717   }
718 }
719
720 static GstFlowReturn
721 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
722 {
723   GstFileSink *filesink;
724   GstFlowReturn flow;
725   guint8 n_mem;
726
727   filesink = GST_FILE_SINK_CAST (sink);
728
729   n_mem = gst_buffer_n_memory (buffer);
730
731   if (n_mem > 0)
732     flow = gst_file_sink_render_buffers (filesink, &buffer, 1, &n_mem, n_mem);
733   else
734     flow = GST_FLOW_OK;
735
736   if (flow == GST_FLOW_OK &&
737       GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_SYNC_AFTER)) {
738     if (fflush (filesink->file) || fsync (fileno (filesink->file))) {
739       GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
740           (_("Error while writing to file \"%s\"."), filesink->filename),
741           ("%s", g_strerror (errno)));
742       flow = GST_FLOW_ERROR;
743     }
744   }
745
746   return flow;
747 }
748
749 static gboolean
750 gst_file_sink_start (GstBaseSink * basesink)
751 {
752   return gst_file_sink_open_file (GST_FILE_SINK (basesink));
753 }
754
755 static gboolean
756 gst_file_sink_stop (GstBaseSink * basesink)
757 {
758   gst_file_sink_close_file (GST_FILE_SINK (basesink));
759   return TRUE;
760 }
761
762 /*** GSTURIHANDLER INTERFACE *************************************************/
763
764 static GstURIType
765 gst_file_sink_uri_get_type (GType type)
766 {
767   return GST_URI_SINK;
768 }
769
770 static const gchar *const *
771 gst_file_sink_uri_get_protocols (GType type)
772 {
773   static const gchar *protocols[] = { "file", NULL };
774
775   return protocols;
776 }
777
778 static gchar *
779 gst_file_sink_uri_get_uri (GstURIHandler * handler)
780 {
781   GstFileSink *sink = GST_FILE_SINK (handler);
782
783   /* FIXME: make thread-safe */
784   return g_strdup (sink->uri);
785 }
786
787 static gboolean
788 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
789     GError ** error)
790 {
791   gchar *location;
792   gboolean ret;
793   GstFileSink *sink = GST_FILE_SINK (handler);
794
795   /* allow file://localhost/foo/bar by stripping localhost but fail
796    * for every other hostname */
797   if (g_str_has_prefix (uri, "file://localhost/")) {
798     char *tmp;
799
800     /* 16 == strlen ("file://localhost") */
801     tmp = g_strconcat ("file://", uri + 16, NULL);
802     /* we use gst_uri_get_location() although we already have the
803      * "location" with uri + 16 because it provides unescaping */
804     location = gst_uri_get_location (tmp);
805     g_free (tmp);
806   } else if (strcmp (uri, "file://") == 0) {
807     /* Special case for "file://" as this is used by some applications
808      *  to test with gst_element_make_from_uri if there's an element
809      *  that supports the URI protocol. */
810     gst_file_sink_set_location (sink, NULL, NULL);
811     return TRUE;
812   } else {
813     location = gst_uri_get_location (uri);
814   }
815
816   if (!location) {
817     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
818         "File URI without location");
819     return FALSE;
820   }
821
822   if (!g_path_is_absolute (location)) {
823     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
824         "File URI location must be an absolute path");
825     g_free (location);
826     return FALSE;
827   }
828
829   ret = gst_file_sink_set_location (sink, location, error);
830   g_free (location);
831
832   return ret;
833 }
834
835 static void
836 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
837 {
838   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
839
840   iface->get_type = gst_file_sink_uri_get_type;
841   iface->get_protocols = gst_file_sink_uri_get_protocols;
842   iface->get_uri = gst_file_sink_uri_get_uri;
843   iface->set_uri = gst_file_sink_uri_set_uri;
844 }