Merge branch '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     default:
212       res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
213       break;
214
215   }
216   return res;
217 }
218
219 static GstFlowReturn
220 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
221 {
222   GstFdSink *fdsink;
223   guint8 *data, *ptr;
224   gsize size, left;
225   gint written;
226
227 #ifndef HAVE_WIN32
228   gint retval;
229 #endif
230
231   fdsink = GST_FD_SINK (sink);
232
233   g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
234
235   data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
236
237   ptr = data;
238   left = size;
239
240 again:
241 #ifndef HAVE_WIN32
242   do {
243     GST_DEBUG_OBJECT (fdsink, "going into select, have %" G_GSIZE_FORMAT
244         " bytes to write", size);
245     retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
246   } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
247
248   if (retval == -1) {
249     if (errno == EBUSY)
250       goto stopped;
251     else
252       goto select_error;
253   }
254 #endif
255
256   GST_DEBUG_OBJECT (fdsink, "writing %" G_GSIZE_FORMAT " bytes to"
257       " file descriptor %d", size, fdsink->fd);
258
259   written = write (fdsink->fd, ptr, left);
260
261   /* check for errors */
262   if (G_UNLIKELY (written < 0)) {
263     /* try to write again on non-fatal errors */
264     if (errno == EAGAIN || errno == EINTR)
265       goto again;
266
267     /* else go to our error handler */
268     goto write_error;
269   }
270
271   /* all is fine when we get here */
272   left -= written;
273   ptr += written;
274   fdsink->bytes_written += written;
275   fdsink->current_pos += written;
276
277   GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %" G_GSIZE_FORMAT " left", written,
278       left);
279
280   /* short write, select and try to write the remainder */
281   if (G_UNLIKELY (left > 0))
282     goto again;
283
284   gst_buffer_unmap (buffer, data, size);
285
286   return GST_FLOW_OK;
287
288 #ifndef HAVE_WIN32
289 select_error:
290   {
291     GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
292         ("select on file descriptor: %s.", g_strerror (errno)));
293     GST_DEBUG_OBJECT (fdsink, "Error during select");
294     gst_buffer_unmap (buffer, data, size);
295     return GST_FLOW_ERROR;
296   }
297 stopped:
298   {
299     GST_DEBUG_OBJECT (fdsink, "Select stopped");
300     gst_buffer_unmap (buffer, data, size);
301     return GST_FLOW_WRONG_STATE;
302   }
303 #endif
304
305 write_error:
306   {
307     switch (errno) {
308       case ENOSPC:
309         GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
310         break;
311       default:{
312         GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
313             ("Error while writing to file descriptor %d: %s",
314                 fdsink->fd, g_strerror (errno)));
315       }
316     }
317     gst_buffer_unmap (buffer, data, size);
318     return GST_FLOW_ERROR;
319   }
320 }
321
322 static gboolean
323 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd, GError ** error)
324 {
325   struct stat stat_results;
326   off_t result;
327
328   /* see that it is a valid file descriptor */
329   if (fstat (fd, &stat_results) < 0)
330     goto invalid;
331
332   if (!S_ISREG (stat_results.st_mode))
333     goto not_seekable;
334
335   /* see if it is a seekable stream */
336   result = lseek (fd, 0, SEEK_CUR);
337   if (result == -1) {
338     switch (errno) {
339       case EINVAL:
340       case EBADF:
341         goto invalid;
342
343       case ESPIPE:
344         goto not_seekable;
345     }
346   } else
347     GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
348
349   return TRUE;
350
351 invalid:
352   {
353     GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
354         ("File descriptor %d is not valid: %s", fd, g_strerror (errno)));
355     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
356         "File descriptor %d is not valid: %s", fd, g_strerror (errno));
357     return FALSE;
358   }
359 not_seekable:
360   {
361     GST_DEBUG_OBJECT (fdsink, "File descriptor %d is a pipe", fd);
362     return TRUE;
363   }
364 }
365
366 static gboolean
367 gst_fd_sink_start (GstBaseSink * basesink)
368 {
369   GstFdSink *fdsink;
370   GstPollFD fd = GST_POLL_FD_INIT;
371
372   fdsink = GST_FD_SINK (basesink);
373   if (!gst_fd_sink_check_fd (fdsink, fdsink->fd, NULL))
374     return FALSE;
375
376   if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
377     goto socket_pair;
378
379   fd.fd = fdsink->fd;
380   gst_poll_add_fd (fdsink->fdset, &fd);
381   gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
382
383   fdsink->bytes_written = 0;
384   fdsink->current_pos = 0;
385
386   return TRUE;
387
388   /* ERRORS */
389 socket_pair:
390   {
391     GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
392         GST_ERROR_SYSTEM);
393     return FALSE;
394   }
395 }
396
397 static gboolean
398 gst_fd_sink_stop (GstBaseSink * basesink)
399 {
400   GstFdSink *fdsink = GST_FD_SINK (basesink);
401
402   if (fdsink->fdset) {
403     gst_poll_free (fdsink->fdset);
404     fdsink->fdset = NULL;
405   }
406
407   return TRUE;
408 }
409
410 static gboolean
411 gst_fd_sink_unlock (GstBaseSink * basesink)
412 {
413   GstFdSink *fdsink = GST_FD_SINK (basesink);
414
415   GST_LOG_OBJECT (fdsink, "Flushing");
416   GST_OBJECT_LOCK (fdsink);
417   gst_poll_set_flushing (fdsink->fdset, TRUE);
418   GST_OBJECT_UNLOCK (fdsink);
419
420   return TRUE;
421 }
422
423 static gboolean
424 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
425 {
426   GstFdSink *fdsink = GST_FD_SINK (basesink);
427
428   GST_LOG_OBJECT (fdsink, "No longer flushing");
429   GST_OBJECT_LOCK (fdsink);
430   gst_poll_set_flushing (fdsink->fdset, FALSE);
431   GST_OBJECT_UNLOCK (fdsink);
432
433   return TRUE;
434 }
435
436 static gboolean
437 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd, GError ** error)
438 {
439   if (new_fd < 0) {
440     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
441         "File descriptor %d is not valid", new_fd);
442     return FALSE;
443   }
444
445   if (!gst_fd_sink_check_fd (fdsink, new_fd, error))
446     goto invalid;
447
448   /* assign the fd */
449   GST_OBJECT_LOCK (fdsink);
450   if (fdsink->fdset) {
451     GstPollFD fd = GST_POLL_FD_INIT;
452
453     fd.fd = fdsink->fd;
454     gst_poll_remove_fd (fdsink->fdset, &fd);
455
456     fd.fd = new_fd;
457     gst_poll_add_fd (fdsink->fdset, &fd);
458     gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
459   }
460   fdsink->fd = new_fd;
461   g_free (fdsink->uri);
462   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
463
464   GST_OBJECT_UNLOCK (fdsink);
465
466   return TRUE;
467
468 invalid:
469   {
470     return FALSE;
471   }
472 }
473
474 static void
475 gst_fd_sink_set_property (GObject * object, guint prop_id,
476     const GValue * value, GParamSpec * pspec)
477 {
478   GstFdSink *fdsink;
479
480   fdsink = GST_FD_SINK (object);
481
482   switch (prop_id) {
483     case ARG_FD:{
484       int fd;
485
486       fd = g_value_get_int (value);
487       gst_fd_sink_update_fd (fdsink, fd, NULL);
488       break;
489     }
490     default:
491       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
492       break;
493   }
494 }
495
496 static void
497 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
498     GParamSpec * pspec)
499 {
500   GstFdSink *fdsink;
501
502   fdsink = GST_FD_SINK (object);
503
504   switch (prop_id) {
505     case ARG_FD:
506       g_value_set_int (value, fdsink->fd);
507       break;
508     default:
509       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510       break;
511   }
512 }
513
514 static gboolean
515 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
516 {
517   off_t result;
518
519   result = lseek (fdsink->fd, new_offset, SEEK_SET);
520
521   if (result == -1)
522     goto seek_failed;
523
524   fdsink->current_pos = new_offset;
525
526   GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
527       "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
528
529   return TRUE;
530
531   /* ERRORS */
532 seek_failed:
533   {
534     GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
535         "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
536     return FALSE;
537   }
538 }
539
540 static gboolean
541 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
542 {
543   GstEventType type;
544   GstFdSink *fdsink;
545
546   fdsink = GST_FD_SINK (sink);
547
548   type = GST_EVENT_TYPE (event);
549
550   switch (type) {
551     case GST_EVENT_SEGMENT:
552     {
553       const GstSegment *segment;
554
555       gst_event_parse_segment (event, &segment);
556
557       if (segment->format == GST_FORMAT_BYTES) {
558         /* only try to seek and fail when we are going to a different
559          * position */
560         if (fdsink->current_pos != segment->start) {
561           /* FIXME, the seek should be performed on the pos field, start/stop are
562            * just boundaries for valid bytes offsets. We should also fill the file
563            * with zeroes if the new position extends the current EOF (sparse streams
564            * and segment accumulation). */
565           if (!gst_fd_sink_do_seek (fdsink, (guint64) segment->start))
566             goto seek_failed;
567         }
568       } else {
569         GST_DEBUG_OBJECT (fdsink,
570             "Ignored SEGMENT event of format %u (%s)", (guint) segment->format,
571             gst_format_get_name (segment->format));
572       }
573       break;
574     }
575     default:
576       break;
577   }
578
579   return TRUE;
580
581 seek_failed:
582   {
583     GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
584         ("Error while seeking on file descriptor %d: %s",
585             fdsink->fd, g_strerror (errno)));
586     return FALSE;
587   }
588
589 }
590
591 /*** GSTURIHANDLER INTERFACE *************************************************/
592
593 static GstURIType
594 gst_fd_sink_uri_get_type (GType type)
595 {
596   return GST_URI_SINK;
597 }
598
599 static const gchar *const *
600 gst_fd_sink_uri_get_protocols (GType type)
601 {
602   static const gchar *protocols[] = { "fd", NULL };
603
604   return protocols;
605 }
606
607 static gchar *
608 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
609 {
610   GstFdSink *sink = GST_FD_SINK (handler);
611
612   /* FIXME: make thread-safe */
613   return g_strdup (sink->uri);
614 }
615
616 static gboolean
617 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
618     GError ** error)
619 {
620   GstFdSink *sink = GST_FD_SINK (handler);
621   gint fd;
622
623   if (sscanf (uri, "fd://%d", &fd) != 1) {
624     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
625         "File descriptor URI could not be parsed");
626     return FALSE;
627   }
628
629   return gst_fd_sink_update_fd (sink, fd, error);
630 }
631
632 static void
633 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
634 {
635   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
636
637   iface->get_type = gst_fd_sink_uri_get_type;
638   iface->get_protocols = gst_fd_sink_uri_get_protocols;
639   iface->get_uri = gst_fd_sink_uri_get_uri;
640   iface->set_uri = gst_fd_sink_uri_set_uri;
641 }