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