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