Branch data Line data Source code
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 : 12 : 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 [ + - ]: 12 : if (!buffer_mode_type) {
88 : 12 : buffer_mode_type =
89 : 12 : g_enum_register_static ("GstFileSinkBufferMode", buffer_mode);
90 : : }
91 : 12 : 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 : 2 : 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 : 2 : 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 : 153 : _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 : 153 : g_type_add_interface_static (filesink_type, GST_TYPE_URI_HANDLER,
187 : : &urihandler_info);
188 [ + - ]: 153 : GST_DEBUG_CATEGORY_INIT (gst_file_sink_debug, "filesink", 0,
189 : : "filesink element");
190 : 153 : }
191 : :
192 [ + + ]: 238 : GST_BOILERPLATE_FULL (GstFileSink, gst_file_sink, GstBaseSink,
193 : 238 : GST_TYPE_BASE_SINK, _do_init);
194 : :
195 : : static void
196 : 12 : gst_file_sink_base_init (gpointer g_class)
197 : : {
198 : 12 : GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
199 : :
200 : 12 : 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 : 12 : gst_element_class_add_pad_template (gstelement_class,
205 : : gst_static_pad_template_get (&sinktemplate));
206 : 12 : }
207 : :
208 : : static void
209 : 12 : gst_file_sink_class_init (GstFileSinkClass * klass)
210 : : {
211 : 12 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
212 : 12 : GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
213 : :
214 : 12 : gobject_class->dispose = gst_file_sink_dispose;
215 : :
216 : 12 : gobject_class->set_property = gst_file_sink_set_property;
217 : 12 : gobject_class->get_property = gst_file_sink_get_property;
218 : :
219 : 12 : 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 : 12 : 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 : 12 : 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 : 12 : 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 : 12 : gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_file_sink_start);
248 : 12 : gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_file_sink_stop);
249 : 12 : gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_file_sink_render);
250 : 12 : 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 : 12 : }
257 : :
258 : : static void
259 : 7 : gst_file_sink_init (GstFileSink * filesink, GstFileSinkClass * g_class)
260 : : {
261 : : GstPad *pad;
262 : :
263 : 7 : pad = GST_BASE_SINK_PAD (filesink);
264 : :
265 : 7 : gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_file_sink_query));
266 : :
267 : 7 : filesink->filename = NULL;
268 : 7 : filesink->file = NULL;
269 : 7 : filesink->buffer_mode = DEFAULT_BUFFER_MODE;
270 : 7 : filesink->buffer_size = DEFAULT_BUFFER_SIZE;
271 : 7 : filesink->buffer = NULL;
272 : 7 : filesink->append = FALSE;
273 : :
274 : 7 : gst_base_sink_set_sync (GST_BASE_SINK (filesink), FALSE);
275 : 7 : }
276 : :
277 : : static void
278 : 7 : gst_file_sink_dispose (GObject * object)
279 : : {
280 : 7 : GstFileSink *sink = GST_FILE_SINK (object);
281 : :
282 : 7 : G_OBJECT_CLASS (parent_class)->dispose (object);
283 : :
284 : 7 : g_free (sink->uri);
285 : 7 : sink->uri = NULL;
286 : 7 : g_free (sink->filename);
287 : 7 : sink->filename = NULL;
288 : 7 : g_free (sink->buffer);
289 : 7 : sink->buffer = NULL;
290 : 7 : sink->buffer_size = 0;
291 : 7 : }
292 : :
293 : : static gboolean
294 : 8 : gst_file_sink_set_location (GstFileSink * sink, const gchar * location)
295 : : {
296 [ - + ]: 8 : if (sink->file)
297 : 0 : goto was_open;
298 : :
299 : 8 : g_free (sink->filename);
300 : 8 : g_free (sink->uri);
301 [ + + ]: 8 : if (location != NULL) {
302 : : /* we store the filename as we received it from the application. On Windows
303 : : * this should be in UTF8 */
304 : 7 : sink->filename = g_strdup (location);
305 : 7 : sink->uri = gst_filename_to_uri (location, NULL);
306 [ - + ]: 7 : GST_INFO ("filename : %s", sink->filename);
307 [ - + ]: 7 : GST_INFO ("uri : %s", sink->uri);
308 : : } else {
309 : 1 : sink->filename = NULL;
310 : 1 : sink->uri = NULL;
311 : : }
312 : :
313 : 8 : return TRUE;
314 : :
315 : : /* ERRORS */
316 : : was_open:
317 : : {
318 : 0 : g_warning ("Changing the `location' property on filesink when a file is "
319 : : "open is not supported.");
320 : 8 : return FALSE;
321 : : }
322 : : }
323 : :
324 : : static void
325 : 5 : gst_file_sink_set_property (GObject * object, guint prop_id,
326 : : const GValue * value, GParamSpec * pspec)
327 : : {
328 : 5 : GstFileSink *sink = GST_FILE_SINK (object);
329 : :
330 [ + - - - : 5 : switch (prop_id) {
- ]
331 : : case PROP_LOCATION:
332 : 5 : gst_file_sink_set_location (sink, g_value_get_string (value));
333 : 5 : break;
334 : : case PROP_BUFFER_MODE:
335 : 0 : sink->buffer_mode = g_value_get_enum (value);
336 : 0 : break;
337 : : case PROP_BUFFER_SIZE:
338 : 0 : sink->buffer_size = g_value_get_uint (value);
339 : 0 : break;
340 : : case PROP_APPEND:
341 : 0 : sink->append = g_value_get_boolean (value);
342 : 0 : break;
343 : : default:
344 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 : 0 : break;
346 : : }
347 : 5 : }
348 : :
349 : : static void
350 : 6 : gst_file_sink_get_property (GObject * object, guint prop_id, GValue * value,
351 : : GParamSpec * pspec)
352 : : {
353 : 6 : GstFileSink *sink = GST_FILE_SINK (object);
354 : :
355 [ + - - - : 6 : switch (prop_id) {
- ]
356 : : case PROP_LOCATION:
357 : 6 : g_value_set_string (value, sink->filename);
358 : 6 : break;
359 : : case PROP_BUFFER_MODE:
360 : 0 : g_value_set_enum (value, sink->buffer_mode);
361 : 0 : break;
362 : : case PROP_BUFFER_SIZE:
363 : 0 : g_value_set_uint (value, sink->buffer_size);
364 : 0 : break;
365 : : case PROP_APPEND:
366 : 0 : g_value_set_boolean (value, sink->append);
367 : 0 : break;
368 : : default:
369 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 : 0 : break;
371 : : }
372 : 6 : }
373 : :
374 : : static gboolean
375 : 29 : gst_file_sink_open_file (GstFileSink * sink)
376 : : {
377 : : gint mode;
378 : :
379 : : /* open the file */
380 [ + + ][ + - ]: 29 : if (sink->filename == NULL || sink->filename[0] == '\0')
381 : : goto no_filename;
382 : :
383 [ - + ]: 2 : if (sink->append)
384 : 0 : sink->file = gst_fopen (sink->filename, "ab");
385 : : else
386 : 2 : sink->file = gst_fopen (sink->filename, "wb");
387 [ + + ]: 2 : if (sink->file == NULL)
388 : 1 : goto open_failed;
389 : :
390 : : /* see if we are asked to perform a specific kind of buffering */
391 [ - + ]: 1 : if ((mode = sink->buffer_mode) != -1) {
392 : : guint buffer_size;
393 : :
394 : : /* free previous buffer if any */
395 : 0 : g_free (sink->buffer);
396 : :
397 [ # # ]: 0 : if (mode == _IONBF) {
398 : : /* no buffering */
399 : 0 : sink->buffer = NULL;
400 : 0 : buffer_size = 0;
401 : : } else {
402 : : /* allocate buffer */
403 : 0 : sink->buffer = g_malloc (sink->buffer_size);
404 : 0 : buffer_size = sink->buffer_size;
405 : : }
406 : : #ifdef HAVE_STDIO_EXT_H
407 [ # # ]: 0 : 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 [ # # ]: 0 : if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) {
414 [ # # ]: 0 : GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s",
415 : : g_strerror (errno));
416 : : }
417 : : }
418 : :
419 : 1 : sink->current_pos = 0;
420 : : /* try to seek in the file to figure out if it is seekable */
421 : 1 : sink->seekable = gst_file_sink_do_seek (sink, 0);
422 : :
423 [ - + ]: 1 : GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d",
424 : : sink->filename, sink->seekable);
425 : :
426 : 1 : return TRUE;
427 : :
428 : : /* ERRORS */
429 : : no_filename:
430 : : {
431 [ + - ][ - + ]: 27 : GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
[ - + ][ # # ]
432 : : (_("No file name specified for writing.")), (NULL));
433 : 27 : return FALSE;
434 : : }
435 : : open_failed:
436 : : {
437 [ + - ][ - + ]: 1 : GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
[ + - ][ - + ]
438 : : (_("Could not open file \"%s\" for writing."), sink->filename),
439 : : GST_ERROR_SYSTEM);
440 : 29 : return FALSE;
441 : : }
442 : : }
443 : :
444 : : static void
445 : 1 : gst_file_sink_close_file (GstFileSink * sink)
446 : : {
447 [ + - ]: 1 : if (sink->file) {
448 [ - + ]: 1 : if (fclose (sink->file) != 0)
449 : 0 : goto close_failed;
450 : :
451 [ - + ]: 1 : GST_DEBUG_OBJECT (sink, "closed file");
452 : 1 : sink->file = NULL;
453 : :
454 : 1 : g_free (sink->buffer);
455 : 1 : sink->buffer = NULL;
456 : : }
457 : 1 : return;
458 : :
459 : : /* ERRORS */
460 : : close_failed:
461 : : {
462 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
[ # # ][ # # ]
463 : : (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
464 : 1 : return;
465 : : }
466 : : }
467 : :
468 : : static gboolean
469 : 8 : gst_file_sink_query (GstPad * pad, GstQuery * query)
470 : : {
471 : : GstFileSink *self;
472 : : GstFormat format;
473 : :
474 : 8 : self = GST_FILE_SINK (GST_PAD_PARENT (pad));
475 : :
476 [ + - - - ]: 8 : switch (GST_QUERY_TYPE (query)) {
477 : : case GST_QUERY_POSITION:
478 : 8 : gst_query_parse_position (query, &format, NULL);
479 [ + - ]: 8 : switch (format) {
480 : : case GST_FORMAT_DEFAULT:
481 : : case GST_FORMAT_BYTES:
482 : 8 : gst_query_set_position (query, GST_FORMAT_BYTES, self->current_pos);
483 : 8 : return TRUE;
484 : : default:
485 : 0 : return FALSE;
486 : : }
487 : :
488 : : case GST_QUERY_FORMATS:
489 : 0 : gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
490 : 0 : return TRUE;
491 : :
492 : : case GST_QUERY_URI:
493 : 0 : gst_query_set_uri (query, self->uri);
494 : 0 : return TRUE;
495 : :
496 : : default:
497 : 8 : 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 : 2 : gst_file_sink_do_seek (GstFileSink * filesink, guint64 new_offset)
511 : : {
512 [ - + ]: 2 : GST_DEBUG_OBJECT (filesink, "Seeking to offset %" G_GUINT64_FORMAT
513 : : " using " __GST_STDIO_SEEK_FUNCTION, new_offset);
514 : :
515 [ - + ]: 2 : if (fflush (filesink->file))
516 : 0 : goto flush_failed;
517 : :
518 : : #ifdef HAVE_FSEEKO
519 [ - + ]: 2 : if (fseeko (filesink->file, (off_t) new_offset, SEEK_SET) != 0)
520 : 0 : 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 : 2 : gst_file_sink_get_current_offset (filesink, &filesink->current_pos);
533 : :
534 : 2 : return TRUE;
535 : :
536 : : /* ERRORS */
537 : : flush_failed:
538 : : {
539 [ # # ]: 0 : GST_DEBUG_OBJECT (filesink, "Flush failed: %s", g_strerror (errno));
540 : 0 : return FALSE;
541 : : }
542 : : seek_failed:
543 : : {
544 [ # # ]: 0 : GST_DEBUG_OBJECT (filesink, "Seeking failed: %s", g_strerror (errno));
545 : 2 : return FALSE;
546 : : }
547 : : }
548 : :
549 : : /* handle events (search) */
550 : : static gboolean
551 : 3 : gst_file_sink_event (GstBaseSink * sink, GstEvent * event)
552 : : {
553 : : GstEventType type;
554 : : GstFileSink *filesink;
555 : :
556 : 3 : filesink = GST_FILE_SINK (sink);
557 : :
558 : 3 : type = GST_EVENT_TYPE (event);
559 : :
560 [ + + - ]: 3 : switch (type) {
561 : : case GST_EVENT_NEWSEGMENT:
562 : : {
563 : : gint64 start, stop, pos;
564 : : GstFormat format;
565 : :
566 : 2 : gst_event_parse_new_segment (event, NULL, NULL, &format, &start,
567 : : &stop, &pos);
568 : :
569 [ + - ]: 2 : if (format == GST_FORMAT_BYTES) {
570 : : /* only try to seek and fail when we are going to a different
571 : : * position */
572 [ + + ]: 2 : 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 [ - + ]: 1 : if (!gst_file_sink_do_seek (filesink, (guint64) start))
578 : 0 : goto seek_failed;
579 : : } else {
580 [ - + ]: 1 : GST_DEBUG_OBJECT (filesink, "Ignored NEWSEGMENT, no seek needed");
581 : : }
582 : : } else {
583 [ # # ]: 0 : GST_DEBUG_OBJECT (filesink,
584 : : "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
585 : : gst_format_get_name (format));
586 : : }
587 : 2 : break;
588 : : }
589 : : case GST_EVENT_EOS:
590 [ - + ]: 1 : if (fflush (filesink->file))
591 : 0 : goto flush_failed;
592 : 1 : break;
593 : : default:
594 : 0 : break;
595 : : }
596 : :
597 : 3 : return TRUE;
598 : :
599 : : /* ERRORS */
600 : : seek_failed:
601 : : {
602 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (filesink, RESOURCE, SEEK,
[ # # ][ # # ]
603 : : (_("Error while seeking in file \"%s\"."), filesink->filename),
604 : : GST_ERROR_SYSTEM);
605 : 0 : return FALSE;
606 : : }
607 : : flush_failed:
608 : : {
609 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
[ # # ][ # # ]
610 : : (_("Error while writing to file \"%s\"."), filesink->filename),
611 : : GST_ERROR_SYSTEM);
612 : 3 : return FALSE;
613 : : }
614 : : }
615 : :
616 : : static gboolean
617 : 2 : gst_file_sink_get_current_offset (GstFileSink * filesink, guint64 * p_pos)
618 : : {
619 : 2 : off_t ret = -1;
620 : :
621 : : #ifdef HAVE_FTELLO
622 : 2 : 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 [ + - ]: 2 : if (ret != (off_t) - 1)
634 : 2 : *p_pos = (guint64) ret;
635 : :
636 : 2 : return (ret != (off_t) - 1);
637 : : }
638 : :
639 : : static GstFlowReturn
640 : 6 : gst_file_sink_render (GstBaseSink * sink, GstBuffer * buffer)
641 : : {
642 : : GstFileSink *filesink;
643 : : guint size;
644 : : guint8 *data;
645 : :
646 : 6 : filesink = GST_FILE_SINK (sink);
647 : :
648 : 6 : size = GST_BUFFER_SIZE (buffer);
649 : 6 : data = GST_BUFFER_DATA (buffer);
650 : :
651 [ - + ]: 6 : GST_DEBUG_OBJECT (filesink, "writing %u bytes at %" G_GUINT64_FORMAT,
652 : : size, filesink->current_pos);
653 : :
654 [ + + ][ + - ]: 6 : if (size > 0 && data != NULL) {
655 [ - + ]: 5 : if (fwrite (data, size, 1, filesink->file) != 1)
656 : 0 : goto handle_error;
657 : :
658 : 5 : filesink->current_pos += size;
659 : : }
660 : :
661 : 6 : return GST_FLOW_OK;
662 : :
663 : : handle_error:
664 : : {
665 [ # # ]: 0 : switch (errno) {
666 : : case ENOSPC:{
667 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (filesink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
[ # # ][ # # ]
668 : 0 : break;
669 : : }
670 : : default:{
671 [ # # ][ # # ]: 0 : GST_ELEMENT_ERROR (filesink, RESOURCE, WRITE,
[ # # ][ # # ]
672 : : (_("Error while writing to file \"%s\"."), filesink->filename),
673 : : ("%s", g_strerror (errno)));
674 : : }
675 : : }
676 : 6 : return GST_FLOW_ERROR;
677 : : }
678 : : }
679 : :
680 : : static gboolean
681 : 29 : gst_file_sink_start (GstBaseSink * basesink)
682 : : {
683 : 29 : return gst_file_sink_open_file (GST_FILE_SINK (basesink));
684 : : }
685 : :
686 : : static gboolean
687 : 1 : gst_file_sink_stop (GstBaseSink * basesink)
688 : : {
689 : 1 : gst_file_sink_close_file (GST_FILE_SINK (basesink));
690 : 1 : return TRUE;
691 : : }
692 : :
693 : : /*** GSTURIHANDLER INTERFACE *************************************************/
694 : :
695 : : static GstURIType
696 : 4 : gst_file_sink_uri_get_type (void)
697 : : {
698 : 4 : return GST_URI_SINK;
699 : : }
700 : :
701 : : static gchar **
702 : 4 : gst_file_sink_uri_get_protocols (void)
703 : : {
704 : : static gchar *protocols[] = { (char *) "file", NULL };
705 : :
706 : 4 : return protocols;
707 : : }
708 : :
709 : : static const gchar *
710 : 4 : gst_file_sink_uri_get_uri (GstURIHandler * handler)
711 : : {
712 : 4 : GstFileSink *sink = GST_FILE_SINK (handler);
713 : :
714 : 4 : return sink->uri;
715 : : }
716 : :
717 : : static gboolean
718 : 4 : gst_file_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
719 : : {
720 : : gchar *protocol, *location;
721 : : gboolean ret;
722 : 4 : GstFileSink *sink = GST_FILE_SINK (handler);
723 : :
724 : 4 : protocol = gst_uri_get_protocol (uri);
725 [ - + ]: 4 : if (strcmp (protocol, "file") != 0) {
726 : 0 : g_free (protocol);
727 : 0 : return FALSE;
728 : : }
729 : 4 : g_free (protocol);
730 : :
731 : : /* allow file://localhost/foo/bar by stripping localhost but fail
732 : : * for every other hostname */
733 [ + + ]: 4 : if (g_str_has_prefix (uri, "file://localhost/")) {
734 : : char *tmp;
735 : :
736 : : /* 16 == strlen ("file://localhost") */
737 : 1 : tmp = g_strconcat ("file://", uri + 16, NULL);
738 : : /* we use gst_uri_get_location() although we already have the
739 : : * "location" with uri + 16 because it provides unescaping */
740 : 1 : location = gst_uri_get_location (tmp);
741 : 1 : g_free (tmp);
742 [ - + ]: 3 : } else if (strcmp (uri, "file://") == 0) {
743 : : /* Special case for "file://" as this is used by some applications
744 : : * to test with gst_element_make_from_uri if there's an element
745 : : * that supports the URI protocol. */
746 : 0 : gst_file_sink_set_location (sink, NULL);
747 : 0 : return TRUE;
748 : : } else {
749 : 3 : location = gst_uri_get_location (uri);
750 : : }
751 : :
752 [ - + ]: 4 : if (!location)
753 : 0 : return FALSE;
754 [ + + ]: 4 : if (!g_path_is_absolute (location)) {
755 : 1 : g_free (location);
756 : 1 : return FALSE;
757 : : }
758 : :
759 : 3 : ret = gst_file_sink_set_location (sink, location);
760 : 3 : g_free (location);
761 : :
762 : 4 : return ret;
763 : : }
764 : :
765 : : static void
766 : 12 : gst_file_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
767 : : {
768 : 12 : GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
769 : :
770 : 12 : iface->get_type = gst_file_sink_uri_get_type;
771 : 12 : iface->get_protocols = gst_file_sink_uri_get_protocols;
772 : 12 : iface->get_uri = gst_file_sink_uri_get_uri;
773 : 12 : iface->set_uri = gst_file_sink_uri_set_uri;
774 : 12 : }
|