Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / plugins / elements / gstfdsink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstfdsink.c:
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:element-fdsink
25  * @see_also: #GstFdSrc
26  *
27  * Write data to a unix file descriptor.
28  *
29  * This element will synchronize on the clock before writing the data on the
30  * socket. For file descriptors where this does not make sense (files, ...) the
31  * #GstBaseSink:sync property can be used to disable synchronisation.
32  *
33  * Last reviewed on 2006-04-28 (0.10.6)
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include "../../gst/gst-i18n-lib.h"
41
42 #include <sys/types.h>
43
44 #ifdef G_OS_WIN32
45 #include <io.h>                 /* lseek, open, close, read */
46 #undef lseek
47 #define lseek _lseeki64
48 #undef off_t
49 #define off_t guint64
50 #endif
51
52 #include <sys/stat.h>
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
55 #endif
56 #include <fcntl.h>
57 #include <stdio.h>
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61 #ifdef _MSC_VER
62 #undef stat
63 #define stat _stat
64 #define fstat _fstat
65 #define S_ISREG(m)      (((m)&S_IFREG)==S_IFREG)
66 #endif
67 #include <errno.h>
68 #include <string.h>
69
70 #include "gstfdsink.h"
71
72 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
73     GST_PAD_SINK,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS_ANY);
76
77 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
78 #define GST_CAT_DEFAULT gst_fd_sink__debug
79
80
81 /* FdSink signals and args */
82 enum
83 {
84   /* FILL ME */
85   LAST_SIGNAL
86 };
87
88 enum
89 {
90   ARG_0,
91   ARG_FD
92 };
93
94 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
95     gpointer iface_data);
96
97 #define _do_init \
98   G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_fd_sink_uri_handler_init); \
99   GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
100 #define gst_fd_sink_parent_class parent_class
101 G_DEFINE_TYPE_WITH_CODE (GstFdSink, gst_fd_sink, GST_TYPE_BASE_SINK, _do_init);
102
103 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
104     const GValue * value, GParamSpec * pspec);
105 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
106     GValue * value, GParamSpec * pspec);
107 static void gst_fd_sink_dispose (GObject * obj);
108
109 static gboolean gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query);
110 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
111     GstBuffer * buffer);
112 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
113 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
114 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
115 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
116 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
117
118 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
119
120 static void
121 gst_fd_sink_class_init (GstFdSinkClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstElementClass *gstelement_class;
125   GstBaseSinkClass *gstbasesink_class;
126
127   gobject_class = G_OBJECT_CLASS (klass);
128   gstelement_class = GST_ELEMENT_CLASS (klass);
129   gstbasesink_class = GST_BASE_SINK_CLASS (klass);
130
131   gobject_class->set_property = gst_fd_sink_set_property;
132   gobject_class->get_property = gst_fd_sink_get_property;
133   gobject_class->dispose = gst_fd_sink_dispose;
134
135   gst_element_class_set_details_simple (gstelement_class,
136       "Filedescriptor Sink",
137       "Sink/File",
138       "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
139   gst_element_class_add_pad_template (gstelement_class,
140       gst_static_pad_template_get (&sinktemplate));
141
142   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
143   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
144   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
145   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
146   gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
147   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
148   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_fd_sink_query);
149
150   g_object_class_install_property (gobject_class, ARG_FD,
151       g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
152           0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 }
154
155 static void
156 gst_fd_sink_init (GstFdSink * fdsink)
157 {
158   fdsink->fd = 1;
159   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
160   fdsink->bytes_written = 0;
161   fdsink->current_pos = 0;
162
163   gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
164 }
165
166 static void
167 gst_fd_sink_dispose (GObject * obj)
168 {
169   GstFdSink *fdsink = GST_FD_SINK (obj);
170
171   g_free (fdsink->uri);
172   fdsink->uri = NULL;
173
174   G_OBJECT_CLASS (parent_class)->dispose (obj);
175 }
176
177 static gboolean
178 gst_fd_sink_query (GstBaseSink * bsink, GstQuery * query)
179 {
180   gboolean res = FALSE;
181   GstFdSink *fdsink;
182
183   fdsink = GST_FD_SINK (bsink);
184
185   switch (GST_QUERY_TYPE (query)) {
186     case GST_QUERY_POSITION:
187     {
188       GstFormat format;
189
190       gst_query_parse_position (query, &format, NULL);
191
192       switch (format) {
193         case GST_FORMAT_DEFAULT:
194         case GST_FORMAT_BYTES:
195           gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
196           res = TRUE;
197           break;
198         default:
199           break;
200       }
201       break;
202     }
203     case GST_QUERY_FORMATS:
204       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
205       res = TRUE;
206       break;
207     case GST_QUERY_URI:
208       gst_query_set_uri (query, fdsink->uri);
209       res = TRUE;
210       break;
211     case GST_QUERY_SEEKING:
212       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
213       if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
214         gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
215             -1);
216       } else {
217         gst_query_set_seeking (query, format, FALSE, 0, -1);
218       }
219       res = TRUE;
220       break;
221     default:
222       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
223       break;
224
225   }
226   return res;
227 }
228
229 static GstFlowReturn
230 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
231 {
232   GstFdSink *fdsink;
233   GstMapInfo info;
234   guint8 *ptr;
235   gsize left;
236   gint written;
237
238 #ifndef HAVE_WIN32
239   gint retval;
240 #endif
241
242   fdsink = GST_FD_SINK (sink);
243
244   g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
245
246   gst_buffer_map (buffer, &info, GST_MAP_READ);
247
248   ptr = info.data;
249   left = info.size;
250
251 again:
252 #ifndef HAVE_WIN32
253   do {
254     GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT
255         " bytes to write", info.size);
256     retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
257   } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
258
259   if (retval == -1) {
260     if (errno == EBUSY)
261       goto stopped;
262     else
263       goto select_error;
264   }
265 #endif
266
267   GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to"
268       " file descriptor %d", info.size, fdsink->fd);
269
270   written = write (fdsink->fd, ptr, left);
271
272   /* check for errors */
273   if (G_UNLIKELY (written < 0)) {
274     /* try to write again on non-fatal errors */
275     if (errno == EAGAIN || errno == EINTR)
276       goto again;
277
278     /* else go to our error handler */
279     goto write_error;
280   }
281
282   /* all is fine when we get here */
283   left -= written;
284   ptr += written;
285   fdsink->bytes_written += written;
286   fdsink->current_pos += written;
287
288   GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
289       left);
290
291   /* short write, select and try to write the remainder */
292   if (G_UNLIKELY (left > 0))
293     goto again;
294
295   gst_buffer_unmap (buffer, &info);
296
297   return GST_FLOW_OK;
298
299 #ifndef HAVE_WIN32
300 select_error:
301   {
302     GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
303         ("select on file descriptor: %s.", g_strerror (errno)));
304     GST_DEBUG_OBJECT (fdsink, "Error during select");
305     gst_buffer_unmap (buffer, &info);
306     return GST_FLOW_ERROR;
307   }
308 stopped:
309   {
310     GST_DEBUG_OBJECT (fdsink, "Select stopped");
311     gst_buffer_unmap (buffer, &info);
312     return GST_FLOW_FLUSHING;
313   }
314 #endif
315
316 write_error:
317   {
318     switch (errno) {
319       case ENOSPC:
320         GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
321         break;
322       default:{
323         GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
324             ("Error while writing to file descriptor %d: %s",
325                 fdsink->fd, g_strerror (errno)));
326       }
327     }
328     gst_buffer_unmap (buffer, &info);
329     return GST_FLOW_ERROR;
330   }
331 }
332
333 static gboolean
334 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
335 {
336   struct stat stat_results;
337   off_t result;
338
339   /* see that it is a valid file descriptor */
340   if (fstat (fd, &stat_results) < 0)
341     goto invalid;
342
343   if (!S_ISREG (stat_results.st_mode))
344     goto not_seekable;
345
346   /* see if it is a seekable stream */
347   result = lseek (fd, 0, SEEK_CUR);
348   if (result == -1) {
349     switch (errno) {
350       case EINVAL:
351       case EBADF:
352         goto invalid;
353
354       case ESPIPE:
355         goto not_seekable;
356     }
357   } else
358     GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
359
360   return TRUE;
361
362 invalid:
363   {
364     GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
365         ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
366     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
367         "File descriptor %d is not valid: %s", fd, g_strerror (errno));
368     return FALSE;
369   }
370 not_seekable:
371   {
372     GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
373     return TRUE;
374   }
375 }
376
377 static gboolean
378 gst_fd_sink_start (GstBaseSink * basesink)
379 {
380   GstFdSink *fdsink;
381   GstPollFD fd = GST_POLL_FD_INIT;
382
383   fdsink = GST_FD_SINK (basesink);
384   if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
385     return FALSE;
386
387   if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
388     goto socket_pair;
389
390   fd.fd = fdsink->fd;
391   gst_poll_add_fd (fdsink->fdset, &fd);
392   gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
393
394   fdsink->bytes_written = 0;
395   fdsink->current_pos = 0;
396
397   fdsink->seekable = gst_fd_sink_do_seek (fdsink, 0);
398   GST_INFO_OBJECT (fdsink, "seeking supported: %d", fdsink->seekable);
399
400   return TRUE;
401
402   /* ERRORS */
403 socket_pair:
404   {
405     GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
406         GST_ERROR_SYSTEM);
407     return FALSE;
408   }
409 }
410
411 static gboolean
412 gst_fd_sink_stop (GstBaseSink * basesink)
413 {
414   GstFdSink *fdsink = GST_FD_SINK (basesink);
415
416   if (fdsink->fdset) {
417     gst_poll_free (fdsink->fdset);
418     fdsink->fdset = NULL;
419   }
420
421   return TRUE;
422 }
423
424 static gboolean
425 gst_fd_sink_unlock (GstBaseSink * basesink)
426 {
427   GstFdSink *fdsink = GST_FD_SINK (basesink);
428
429   GST_LOG_OBJECT (fdsink, "Flushing");
430   GST_OBJECT_LOCK (fdsink);
431   gst_poll_set_flushing (fdsink->fdset, TRUE);
432   GST_OBJECT_UNLOCK (fdsink);
433
434   return TRUE;
435 }
436
437 static gboolean
438 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
439 {
440   GstFdSink *fdsink = GST_FD_SINK (basesink);
441
442   GST_LOG_OBJECT (fdsink, "No longer flushing");
443   GST_OBJECT_LOCK (fdsink);
444   gst_poll_set_flushing (fdsink->fdset, FALSE);
445   GST_OBJECT_UNLOCK (fdsink);
446
447   return TRUE;
448 }
449
450 static gboolean
451 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
452 {
453   if (new_fd < 0) {
454     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
455         "File descriptor %d is not valid", new_fd);
456     return FALSE;
457   }
458
459   if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
460     goto invalid;
461
462   /* assign the fd */
463   GST_OBJECT_LOCK (fdsink);
464   if (fdsink->fdset) {
465     GstPollFD fd = GST_POLL_FD_INIT;
466
467     fd.fd = fdsink->fd;
468     gst_poll_remove_fd (fdsink->fdset, &fd);
469
470     fd.fd = new_fd;
471     gst_poll_add_fd (fdsink->fdset, &fd);
472     gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
473   }
474   fdsink->fd = new_fd;
475   g_free (fdsink->uri);
476   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
477
478   GST_OBJECT_UNLOCK (fdsink);
479
480   return TRUE;
481
482 invalid:
483   {
484     return FALSE;
485   }
486 }
487
488 static void
489 gst_fd_sink_set_property (GObject * object, guint prop_id,
490     const GValue * value, GParamSpec * pspec)
491 {
492   GstFdSink *fdsink;
493
494   fdsink = GST_FD_SINK (object);
495
496   switch (prop_id) {
497     case ARG_FD:{
498       int fd;
499
500       fd = g_value_get_int (value);
501       gst_fd_sink_update_fd (fdsink, fd, NULL);
502       break;
503     }
504     default:
505       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506       break;
507   }
508 }
509
510 static void
511 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
512     GParamSpec * pspec)
513 {
514   GstFdSink *fdsink;
515
516   fdsink = GST_FD_SINK (object);
517
518   switch (prop_id) {
519     case ARG_FD:
520       g_value_set_int (value, fdsink->fd);
521       break;
522     default:
523       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
524       break;
525   }
526 }
527
528 static gboolean
529 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
530 {
531   off_t result;
532
533   result = lseek (fdsink->fd, new_offset, SEEK_SET);
534
535   if (result == -1)
536     goto seek_failed;
537
538   fdsink->current_pos = new_offset;
539
540   GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
541       "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
542
543   return TRUE;
544
545   /* ERRORS */
546 seek_failed:
547   {
548     GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
549         "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
550     return FALSE;
551   }
552 }
553
554 static gboolean
555 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
556 {
557   GstEventType type;
558   GstFdSink *fdsink;
559
560   fdsink = GST_FD_SINK (sink);
561
562   type = GST_EVENT_TYPE (event);
563
564   switch (type) {
565     case GST_EVENT_SEGMENT:
566     {
567       const GstSegment *segment;
568
569       gst_event_parse_segment (event, &segment);
570
571       if (segment->format == GST_FORMAT_BYTES) {
572         /* only try to seek and fail when we are going to a different
573          * position */
574         if (fdsink->current_pos != segment->start) {
575           /* FIXME, the seek should be performed on the pos field, start/stop are
576            * just boundaries for valid bytes offsets. We should also fill the file
577            * with zeroes if the new position extends the current EOF (sparse streams
578            * and segment accumulation). */
579           if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
580             goto seek_failed;
581         }
582       } else {
583         GST_DEBUG_OBJECT (fdsink,
584             "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
585             gst_format_get_name (segment->format));
586       }
587       break;
588     }
589     default:
590       break;
591   }
592
593   return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
594
595 seek_failed:
596   {
597     GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
598         ("Error while seeking on file descriptor %d: %s",
599             fdsink->fd, g_strerror (errno)));
600     gst_event_unref (event);
601     return FALSE;
602   }
603
604 }
605
606 /*** GSTURIHANDLER INTERFACE *************************************************/
607
608 static GstURIType
609 gst_fd_sink_uri_get_type (GType type)
610 {
611   return GST_URI_SINK;
612 }
613
614 static const gchar *const *
615 gst_fd_sink_uri_get_protocols (GType type)
616 {
617   static const gchar *protocols[] = { "fd", NULL };
618
619   return protocols;
620 }
621
622 static gchar *
623 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
624 {
625   GstFdSink *sink = GST_FD_SINK (handler);
626
627   /* FIXME: make thread-safe */
628   return g_strdup (sink->uri);
629 }
630
631 static gboolean
632 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
633     GError ** error)
634 {
635   GstFdSink *sink = GST_FD_SINK (handler);
636   gint fd;
637
638   if (sscanf (uri, "fd://%d", &fd) != 1) {
639     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
640         "File descriptor URI could not be parsed");
641     return FALSE;
642   }
643
644   return gst_fd_sink_update_fd (sink, fd, error);
645 }
646
647 static void
648 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
649 {
650   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
651
652   iface->get_type = gst_fd_sink_uri_get_type;
653   iface->get_protocols = gst_fd_sink_uri_get_protocols;
654   iface->get_uri = gst_fd_sink_uri_get_uri;
655   iface->set_uri = gst_fd_sink_uri_set_uri;
656 }