Don't use base_init where not absolutely necessary. For example it's not necessary...
[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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 /**
24  * SECTION:element-filesink
25  * @short_description: write stream to a file
26  * @see_also: #GstFileSrc
27  *
28  * Write incoming data to a file in the local file system.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #endif
34
35 #include "../../gst/gst-i18n-lib.h"
36
37 #include <gst/gst.h>
38 #include <stdio.h>              /* for fseeko() */
39 #ifdef HAVE_STDIO_EXT_H
40 #include <stdio_ext.h>          /* for __fbufsize, for debugging */
41 #endif
42 #include <errno.h>
43 #include "gstfilesink.h"
44 #include <string.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 #define GST_TYPE_BUFFER_MODE (buffer_mode_get_type ())
57 static GType
58 buffer_mode_get_type (void)
59 {
60   static GType buffer_mode_type = 0;
61   static const GEnumValue buffer_mode[] = {
62     {-1, "Default buffering", "default"},
63     {_IOFBF, "Fully buffered", "full"},
64     {_IOLBF, "Line buffered", "line"},
65     {_IONBF, "Unbuffered", "unbuffered"},
66     {0, NULL, NULL},
67   };
68
69   if (!buffer_mode_type) {
70     buffer_mode_type =
71         g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
72   }
73   return buffer_mode_type;
74 }
75
76 GST_DEBUG_CATEGORY_STATIC (gst_file_sink_debug);
77 #define GST_CAT_DEFAULT gst_file_sink_debug
78
79 #define DEFAULT_LOCATION        NULL
80 #define DEFAULT_BUFFER_MODE     -1
81 #define DEFAULT_BUFFER_SIZE     64 * 1024
82
83 enum
84 {
85   PROP_0,
86   PROP_LOCATION,
87   PROP_BUFFER_MODE,
88   PROP_BUFFER_SIZE,
89   PROP_LAST
90 };
91
92 static void gst_file_sink_dispose (GObject * object);
93
94 static void gst_file_sink_set_property (GObject * object, guint prop_id,
95     const GValue * value, GParamSpec * pspec);
96 static void gst_file_sink_get_property (GObject * object, guint prop_id,
97     GValue * value, GParamSpec * pspec);
98
99 static gboolean gst_file_sink_open_file (GstFileSink * sink);
100 static void gst_file_sink_close_file (GstFileSink * sink);
101
102 static gboolean gst_file_sink_start (GstBaseSink * sink);
103 static gboolean gst_file_sink_stop (GstBaseSink * sink);
104 static gboolean gst_file_sink_event (GstBaseSink * sink, GstEvent * event);
105 static GstFlowReturn gst_file_sink_render (GstBaseSink * sink,
106     GstBuffer * buffer);
107
108 static gboolean gst_file_sink_do_seek (GstFileSink * filesink,
109     guint64 new_offset);
110 static gboolean gst_file_sink_get_current_offset (GstFileSink * filesink,
111     guint64 * p_pos);
112
113 static gboolean gst_file_sink_query (GstPad * pad, GstQuery * query);
114
115 static void gst_file_sink_uri_handler_init (gpointer g_iface,
116     gpointer iface_data);
117
118
119 static void
120 _do_init (GType filesink_type)
121 {
122   static const GInterfaceInfo urihandler_info = {
123     gst_file_sink_uri_handler_init,
124     NULL,
125     NULL
126   };
127
128   g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
129       &urihandler_info);
130   GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
131       "filesink element");
132 }
133
134 GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
135     GST_TYPE_BASE_SINK, _do_init);
136
137 static void
138 gst_file_sink_base_init (gpointer g_class)
139 {
140 }
141
142 static void
143 gst_file_sink_class_init (GstFileSinkClass * klass)
144 {
145   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
146   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
147   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
148
149   gobject_class->dispose = gst_file_sink_dispose;
150
151   gobject_class->set_property = gst_file_sink_set_property;
152   gobject_class->get_property = gst_file_sink_get_property;
153
154   g_object_class_install_property (gobject_class, PROP_LOCATION,
155       g_param_spec_string ("location", "File Location",
156           "Location of the file to write", NULL, G_PARAM_READWRITE));
157
158   g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
159       g_param_spec_enum ("buffer-mode", "Buffering mode",
160           "The buffering mode to use", GST_TYPE_BUFFER_MODE,
161           DEFAULT_BUFFER_MODE, G_PARAM_READWRITE));
162
163   g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
164       g_param_spec_uint ("buffer-size", "Buffering size",
165           "Size of buffer in number of bytes for line or full buffer-mode", 0,
166           G_MAXUINT, DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE));
167
168   gst_element_class_set_details_simple (gstelement_class,
169       "File Sink",
170       "Sink/File", "Write stream to a file", "Thomas <thomas@apestaart.org>");
171   gst_element_class_add_pad_template (gstelement_class,
172       gst_static_pad_template_get (&sinktemplate));
173
174   gstbasesink_class->get_times = NULL;
175   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
176   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
177   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
178   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_file_sink_event);
179
180   if (sizeof (off_t) < 8) {
181     GST_LOG ("No large file support, sizeof (off_t) = %" G_GSIZE_FORMAT "!",
182         sizeof (off_t));
183   }
184 }
185
186 static void
187 gst_file_sink_init (GstFileSink * filesink, GstFileSinkClass * g_class)
188 {
189   GstPad *pad;
190
191   pad = GST_BASE_SINK_PAD (filesink);
192
193   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_file_sink_query));
194
195   filesink->filename = NULL;
196   filesink->file = NULL;
197   filesink->buffer_mode = DEFAULT_BUFFER_MODE;
198   filesink->buffer_size = DEFAULT_BUFFER_SIZE;
199   filesink->buffer = NULL;
200
201   gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
202 }
203
204 static void
205 gst_file_sink_dispose (GObject * object)
206 {
207   GstFileSink *sink = GST_FILE_SINK (object);
208
209   G_OBJECT_CLASS (parent_class)->dispose (object);
210
211   g_free (sink->uri);
212   sink->uri = NULL;
213   g_free (sink->filename);
214   sink->filename = NULL;
215   g_free (sink->buffer);
216   sink->buffer = NULL;
217   sink->buffer_size = 0;
218 }
219
220 static gboolean
221 gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
222 {
223   if (sink->file)
224     goto was_open;
225
226   g_free (sink->filename);
227   g_free (sink->uri);
228   if (location != NULL) {
229     sink->filename = g_strdup (location);
230     sink->uri = gst_uri_construct ("file", location);
231   } else {
232     sink->filename = NULL;
233     sink->uri = NULL;
234   }
235
236   return TRUE;
237
238   /* ERRORS */
239 was_open:
240   {
241     g_warning ("Changing the `location' property on filesink when "
242         "a file is open not supported.");
243     return FALSE;
244   }
245 }
246 static void
247 gst_file_sink_set_property (GObject * object, guint prop_id,
248     const GValue * value, GParamSpec * pspec)
249 {
250   GstFileSink *sink = GST_FILE_SINK (object);
251
252   switch (prop_id) {
253     case PROP_LOCATION:
254       gst_file_sink_set_location (sink, g_value_get_string (value));
255       break;
256     case PROP_BUFFER_MODE:
257       sink->buffer_mode = g_value_get_enum (value);
258       break;
259     case PROP_BUFFER_SIZE:
260       sink->buffer_size = g_value_get_uint (value);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264       break;
265   }
266 }
267
268 static void
269 gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
270     GParamSpec * pspec)
271 {
272   GstFileSink *sink = GST_FILE_SINK (object);
273
274   switch (prop_id) {
275     case PROP_LOCATION:
276       g_value_set_string (value, sink->filename);
277       break;
278     case PROP_BUFFER_MODE:
279       g_value_set_enum (value, sink->buffer_mode);
280       break;
281     case PROP_BUFFER_SIZE:
282       g_value_set_uint (value, sink->buffer_size);
283       break;
284     default:
285       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286       break;
287   }
288 }
289
290 static gboolean
291 gst_file_sink_open_file (GstFileSink * sink)
292 {
293   gint mode;
294
295   /* open the file */
296   if (sink->filename == NULL || sink->filename[0] == '\0')
297     goto no_filename;
298
299   sink->file = fopen (sink->filename, "wb");
300   if (sink->file == NULL)
301     goto open_failed;
302
303   /* see if we are asked to perform a specific kind of buffering */
304   if ((mode = sink->buffer_mode) != -1) {
305     size_t buffer_size;
306
307     /* free previous buffer if any */
308     g_free (sink->buffer);
309
310     if (mode == _IONBF) {
311       /* no buffering */
312       sink->buffer = NULL;
313       buffer_size = 0;
314     } else {
315       /* allocate buffer */
316       sink->buffer = g_malloc (sink->buffer_size);
317       buffer_size = sink->buffer_size;
318     }
319 #ifdef HAVE_STDIO_EXT_H
320     GST_DEBUG_OBJECT (sink, "change buffer size %d to %d, mode %d",
321         __fbufsize (sink->file), buffer_size, mode);
322 #else
323     GST_DEBUG_OBJECT (sink, "change  buffer size to %d, mode %d",
324         sink->buffer_size, mode);
325 #endif
326     if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
327       GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
328           g_strerror (errno));
329     }
330   }
331
332   sink->current_pos = 0;
333   /* try to seek in the file to figure out if it is seekable */
334   sink->seekable = gst_file_sink_do_seek (sink, 0);
335
336   GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
337       sink->filename, sink->seekable);
338
339   return TRUE;
340
341   /* ERRORS */
342 no_filename:
343   {
344     GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
345         (_("No file name specified for writing.")), (NULL));
346     return FALSE;
347   }
348 open_failed:
349   {
350     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
351         (_("Could not open file \"%s\" for writing."), sink->filename),
352         GST_ERROR_SYSTEM);
353     return FALSE;
354   }
355 }
356
357 static void
358 gst_file_sink_close_file (GstFileSink * sink)
359 {
360   if (sink->file) {
361     if (fclose (sink->file) != 0)
362       goto close_failed;
363
364     GST_DEBUG_OBJECT (sink, "closed file");
365     sink->file = NULL;
366
367     g_free (sink->buffer);
368     sink->buffer = NULL;
369   }
370   return;
371
372   /* ERRORS */
373 close_failed:
374   {
375     GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
376         (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
377     return;
378   }
379 }
380
381 static gboolean
382 gst_file_sink_query (GstPad * pad, GstQuery * query)
383 {
384   GstFileSink *self;
385   GstFormat format;
386
387   self = GST_FILE_SINK (GST_PAD_PARENT (pad));
388
389   switch (GST_QUERY_TYPE (query)) {
390     case GST_QUERY_POSITION:
391       gst_query_parse_position (query, &format, NULL);
392       switch (format) {
393         case GST_FORMAT_DEFAULT:
394         case GST_FORMAT_BYTES:
395           gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
396           return TRUE;
397         default:
398           return FALSE;
399       }
400
401     case GST_QUERY_FORMATS:
402       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
403       return TRUE;
404
405     default:
406       return gst_pad_query_default (pad, query);
407   }
408 }
409
410 #ifdef HAVE_FSEEKO
411 # define __GST_STDIO_SEEK_FUNCTION "fseeko"
412 #elif defined (G_OS_UNIX)
413 # define __GST_STDIO_SEEK_FUNCTION "lseek"
414 #else
415 # define __GST_STDIO_SEEK_FUNCTION "fseek"
416 #endif
417
418 static gboolean
419 gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
420 {
421   GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
422       " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
423
424   if (fflush (filesink->file))
425     goto flush_failed;
426
427 #ifdef HAVE_FSEEKO
428   if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
429     goto seek_failed;
430 #elif defined (G_OS_UNIX)
431   if (lseek (fileno (filesink->file), (off_t) new_offset,
432           SEEK_SET) == (off_t) - 1)
433     goto seek_failed;
434 #else
435   if (fseek (filesink->file, (long) new_offset, SEEK_SET) != 0)
436     goto seek_failed;
437 #endif
438
439   /* adjust position reporting after seek;
440    * presumably this should basically yield new_offset */
441   gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
442
443   return TRUE;
444
445   /* ERRORS */
446 flush_failed:
447   {
448     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
449     return FALSE;
450   }
451 seek_failed:
452   {
453     GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
454     return FALSE;
455   }
456 }
457
458 /* handle events (search) */
459 static gboolean
460 gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
461 {
462   GstEventType type;
463   GstFileSink *filesink;
464
465   filesink = GST_FILE_SINK (sink);
466
467   type = GST_EVENT_TYPE (event);
468
469   switch (type) {
470     case GST_EVENT_NEWSEGMENT:
471     {
472       gint64 start, stop, pos;
473       GstFormat format;
474
475       gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
476           &stop, &pos);
477
478       if (format == GST_FORMAT_BYTES) {
479         /* only try to seek and fail when we are going to a different
480          * position */
481         if (filesink->current_pos != start) {
482           /* FIXME, the seek should be performed on the pos field, start/stop are
483            * just boundaries for valid bytes offsets. We should also fill the file
484            * with zeroes if the new position extends the current EOF (sparse streams
485            * and segment accumulation). */
486           if (!gst_file_sink_do_seek (filesink, (guint64) start))
487             goto seek_failed;
488         } else {
489           GST_DEBUG_OBJECT (filesink, "Ignored NEWSEGMENT, no seek needed");
490         }
491       } else {
492         GST_DEBUG_OBJECT (filesink,
493             "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
494             gst_format_get_name (format));
495       }
496       break;
497     }
498     case GST_EVENT_EOS:
499       if (fflush (filesink->file))
500         goto flush_failed;
501       break;
502     default:
503       break;
504   }
505
506   return TRUE;
507
508   /* ERRORS */
509 seek_failed:
510   {
511     GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
512         (_("Error while seeking in file \"%s\"."), filesink->filename),
513         GST_ERROR_SYSTEM);
514     return FALSE;
515   }
516 flush_failed:
517   {
518     GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
519         (_("Error while writing to file \"%s\"."), filesink->filename),
520         GST_ERROR_SYSTEM);
521     return FALSE;
522   }
523 }
524
525 static gboolean
526 gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
527 {
528   off_t ret;
529
530 #ifdef HAVE_FTELLO
531   ret = ftello (filesink->file);
532 #elif defined (G_OS_UNIX)
533   if (fflush (filesink->file)) {
534     GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
535     /* ignore and continue */
536   }
537   ret = lseek (fileno (filesink->file), 0, SEEK_CUR);
538 #else
539   ret = (off_t) ftell (filesink->file);
540 #endif
541
542   if (ret != (off_t) - 1)
543     *p_pos = (guint64) ret;
544
545   return (ret != (off_t) - 1);
546 }
547
548 static GstFlowReturn
549 gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
550 {
551   GstFileSink *filesink;
552   guint size;
553
554   size = GST_BUFFER_SIZE (buffer);
555
556   filesink = GST_FILE_SINK (sink);
557
558   GST_DEBUG_OBJECT (filesink, "writing %u bytes at %" G_GUINT64_FORMAT,
559       size, filesink->current_pos);
560
561   if (size > 0 && GST_BUFFER_DATA (buffer) != NULL) {
562     if (fwrite (GST_BUFFER_DATA (buffer), size, 1, filesink->file) != 1)
563       goto handle_error;
564
565     filesink->current_pos += size;
566   }
567
568   return GST_FLOW_OK;
569
570 handle_error:
571   {
572     switch (errno) {
573       case ENOSPC:{
574         GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
575         break;
576       }
577       default:{
578         GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
579             (_("Error while writing to file \"%s\"."), filesink->filename),
580             ("%s", g_strerror (errno)));
581       }
582     }
583     return GST_FLOW_ERROR;
584   }
585 }
586
587 static gboolean
588 gst_file_sink_start (GstBaseSink * basesink)
589 {
590   return gst_file_sink_open_file (GST_FILE_SINK (basesink));
591 }
592
593 static gboolean
594 gst_file_sink_stop (GstBaseSink * basesink)
595 {
596   gst_file_sink_close_file (GST_FILE_SINK (basesink));
597   return TRUE;
598 }
599
600 /*** GSTURIHANDLER INTERFACE *************************************************/
601
602 static GstURIType
603 gst_file_sink_uri_get_type (void)
604 {
605   return GST_URI_SINK;
606 }
607 static gchar **
608 gst_file_sink_uri_get_protocols (void)
609 {
610   static gchar *protocols[] = { "file", NULL };
611
612   return protocols;
613 }
614 static const gchar *
615 gst_file_sink_uri_get_uri (GstURIHandler * handler)
616 {
617   GstFileSink *sink = GST_FILE_SINK (handler);
618
619   return sink->uri;
620 }
621
622 static gboolean
623 gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
624 {
625   gchar *protocol, *location;
626   gboolean ret;
627   GstFileSink *sink = GST_FILE_SINK (handler);
628
629   protocol = gst_uri_get_protocol (uri);
630   if (strcmp (protocol, "file") != 0) {
631     g_free (protocol);
632     return FALSE;
633   }
634   g_free (protocol);
635
636   /* allow file://localhost/foo/bar by stripping localhost but fail
637    * for every other hostname */
638   if (g_str_has_prefix (uri, "file://localhost/")) {
639     char *tmp;
640
641     /* 16 == strlen ("file://localhost") */
642     tmp = g_strconcat ("file://", uri + 16, NULL);
643     /* we use gst_uri_get_location() although we already have the
644      * "location" with uri + 16 because it provides unescaping */
645     location = gst_uri_get_location (tmp);
646     g_free (tmp);
647   } else if (strcmp (uri, "file://") == 0) {
648     /* Special case for "file://" as this is used by some applications
649      *  to test with gst_element_make_from_uri if there's an element
650      *  that supports the URI protocol. */
651     gst_file_sink_set_location (sink, NULL);
652     return TRUE;
653   } else {
654     location = gst_uri_get_location (uri);
655   }
656
657   if (!location)
658     return FALSE;
659   if (!g_path_is_absolute (location)) {
660     g_free (location);
661     return FALSE;
662   }
663
664   ret = gst_file_sink_set_location (sink, location);
665   g_free (location);
666
667   return ret;
668 }
669
670 static void
671 gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
672 {
673   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
674
675   iface->get_type = gst_file_sink_uri_get_type;
676   iface->get_protocols = gst_file_sink_uri_get_protocols;
677   iface->get_uri = gst_file_sink_uri_get_uri;
678   iface->set_uri = gst_file_sink_uri_set_uri;
679 }