80e1f301ee8fc24e488ecb964c54f69698d61eb4
[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 static void
98 _do_init (GType gst_fd_sink_type)
99 {
100   static const GInterfaceInfo urihandler_info = {
101     gst_fd_sink_uri_handler_init,
102     NULL,
103     NULL
104   };
105
106   g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
107       &urihandler_info);
108
109   GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
110 }
111
112 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
113     _do_init);
114
115 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
116     const GValue * value, GParamSpec * pspec);
117 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
118     GValue * value, GParamSpec * pspec);
119 static void gst_fd_sink_dispose (GObject * obj);
120
121 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
122 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
123     GstBuffer * buffer);
124 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
125 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
126 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
127 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
128 static gboolean gst_fd_sink_event (GstBaseSink * sink, GstEvent * event);
129
130 static gboolean gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset);
131
132 static void
133 gst_fd_sink_base_init (gpointer g_class)
134 {
135   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
136
137   gst_element_class_set_details_simple (gstelement_class,
138       "Filedescriptor Sink",
139       "Sink/File",
140       "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
141   gst_element_class_add_pad_template (gstelement_class,
142       gst_static_pad_template_get (&sinktemplate));
143 }
144
145 static void
146 gst_fd_sink_class_init (GstFdSinkClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstBaseSinkClass *gstbasesink_class;
150
151   gobject_class = G_OBJECT_CLASS (klass);
152   gstbasesink_class = GST_BASE_SINK_CLASS (klass);
153
154   gobject_class->set_property = gst_fd_sink_set_property;
155   gobject_class->get_property = gst_fd_sink_get_property;
156   gobject_class->dispose = gst_fd_sink_dispose;
157
158   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
159   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
160   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
161   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
162   gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
163   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_fd_sink_event);
164
165   g_object_class_install_property (gobject_class, ARG_FD,
166       g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
167           0, G_MAXINT, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 }
169
170 static void
171 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
172 {
173   GstPad *pad;
174
175   pad = GST_BASE_SINK_PAD (fdsink);
176   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
177
178   fdsink->fd = 1;
179   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
180   fdsink->bytes_written = 0;
181   fdsink->current_pos = 0;
182
183   gst_base_sink_set_sync (GST_BASE_SINK (fdsink), FALSE);
184 }
185
186 static void
187 gst_fd_sink_dispose (GObject * obj)
188 {
189   GstFdSink *fdsink = GST_FD_SINK (obj);
190
191   g_free (fdsink->uri);
192   fdsink->uri = NULL;
193
194   G_OBJECT_CLASS (parent_class)->dispose (obj);
195 }
196
197 static gboolean
198 gst_fd_sink_query (GstPad * pad, GstQuery * query)
199 {
200   GstFdSink *fdsink;
201   GstFormat format;
202
203   fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
204
205   switch (GST_QUERY_TYPE (query)) {
206     case GST_QUERY_POSITION:
207       gst_query_parse_position (query, &format, NULL);
208       switch (format) {
209         case GST_FORMAT_DEFAULT:
210         case GST_FORMAT_BYTES:
211           gst_query_set_position (query, GST_FORMAT_BYTES, fdsink->current_pos);
212           return TRUE;
213         default:
214           return FALSE;
215       }
216
217     case GST_QUERY_FORMATS:
218       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
219       return TRUE;
220
221     case GST_QUERY_URI:
222       gst_query_set_uri (query, fdsink->uri);
223       return TRUE;
224
225     case GST_QUERY_SEEKING:
226       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
227       if (format == GST_FORMAT_BYTES || format == GST_FORMAT_DEFAULT) {
228         gst_query_set_seeking (query, GST_FORMAT_BYTES, fdsink->seekable, 0,
229             -1);
230       } else {
231         gst_query_set_seeking (query, format, FALSE, 0, -1);
232       }
233       return TRUE;
234
235     default:
236       return gst_pad_query_default (pad, query);
237   }
238 }
239
240 static GstFlowReturn
241 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
242 {
243   GstFdSink *fdsink;
244   guint8 *data;
245   guint size;
246   gint written;
247
248 #ifndef HAVE_WIN32
249   gint retval;
250 #endif
251
252   fdsink = GST_FD_SINK (sink);
253
254   g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
255
256   data = GST_BUFFER_DATA (buffer);
257   size = GST_BUFFER_SIZE (buffer);
258
259 again:
260 #ifndef HAVE_WIN32
261   do {
262     GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
263         size);
264     retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
265   } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
266
267   if (retval == -1) {
268     if (errno == EBUSY)
269       goto stopped;
270     else
271       goto select_error;
272   }
273 #endif
274
275   GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
276       fdsink->fd);
277
278   written = write (fdsink->fd, data, size);
279
280   /* check for errors */
281   if (G_UNLIKELY (written < 0)) {
282     /* try to write again on non-fatal errors */
283     if (errno == EAGAIN || errno == EINTR)
284       goto again;
285
286     /* else go to our error handler */
287     goto write_error;
288   }
289
290   /* all is fine when we get here */
291   size -= written;
292   data += written;
293   fdsink->bytes_written += written;
294   fdsink->current_pos += written;
295
296   GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
297
298   /* short write, select and try to write the remainder */
299   if (G_UNLIKELY (size > 0))
300     goto again;
301
302   return GST_FLOW_OK;
303
304 #ifndef HAVE_WIN32
305 select_error:
306   {
307     GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
308         ("select on file descriptor: %s.", g_strerror (errno)));
309     GST_DEBUG_OBJECT (fdsink, "Error during select");
310     return GST_FLOW_ERROR;
311   }
312 stopped:
313   {
314     GST_DEBUG_OBJECT (fdsink, "Select stopped");
315     return GST_FLOW_WRONG_STATE;
316   }
317 #endif
318
319 write_error:
320   {
321     switch (errno) {
322       case ENOSPC:
323         GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
324         break;
325       default:{
326         GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
327             ("Error while writing to file descriptor %d: %s",
328                 fdsink->fd, g_strerror (errno)));
329       }
330     }
331     return GST_FLOW_ERROR;
332   }
333 }
334
335 static gboolean
336 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
337 {
338   struct stat stat_results;
339   off_t result;
340
341   /* see that it is a valid file descriptor */
342   if (fstat (fd, &stat_results) < 0)
343     goto invalid;
344
345   if (!S_ISREG (stat_results.st_mode))
346     goto not_seekable;
347
348   /* see if it is a seekable stream */
349   result = lseek (fd, 0, SEEK_CUR);
350   if (result == -1) {
351     switch (errno) {
352       case EINVAL:
353       case EBADF:
354         goto invalid;
355
356       case ESPIPE:
357         goto not_seekable;
358     }
359   } else
360     GST_DEBUG_OBJECT (fdsink, "File descriptor %d is seekable", fd);
361
362   return TRUE;
363
364 invalid:
365   {
366     GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE, (NULL),
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))
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)
452 {
453   if (new_fd < 0)
454     return FALSE;
455
456   if (!gst_fd_sink_check_fd (fdsink, new_fd))
457     goto invalid;
458
459   /* assign the fd */
460   GST_OBJECT_LOCK (fdsink);
461   if (fdsink->fdset) {
462     GstPollFD fd = GST_POLL_FD_INIT;
463
464     fd.fd = fdsink->fd;
465     gst_poll_remove_fd (fdsink->fdset, &fd);
466
467     fd.fd = new_fd;
468     gst_poll_add_fd (fdsink->fdset, &fd);
469     gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
470   }
471   fdsink->fd = new_fd;
472   g_free (fdsink->uri);
473   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
474
475   GST_OBJECT_UNLOCK (fdsink);
476
477   return TRUE;
478
479 invalid:
480   {
481     return FALSE;
482   }
483 }
484
485 static void
486 gst_fd_sink_set_property (GObject * object, guint prop_id,
487     const GValue * value, GParamSpec * pspec)
488 {
489   GstFdSink *fdsink;
490
491   fdsink = GST_FD_SINK (object);
492
493   switch (prop_id) {
494     case ARG_FD:{
495       int fd;
496
497       fd = g_value_get_int (value);
498       gst_fd_sink_update_fd (fdsink, fd);
499       break;
500     }
501     default:
502       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503       break;
504   }
505 }
506
507 static void
508 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
509     GParamSpec * pspec)
510 {
511   GstFdSink *fdsink;
512
513   fdsink = GST_FD_SINK (object);
514
515   switch (prop_id) {
516     case ARG_FD:
517       g_value_set_int (value, fdsink->fd);
518       break;
519     default:
520       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521       break;
522   }
523 }
524
525 static gboolean
526 gst_fd_sink_do_seek (GstFdSink * fdsink, guint64 new_offset)
527 {
528   off_t result;
529
530   result = lseek (fdsink->fd, new_offset, SEEK_SET);
531
532   if (result == -1)
533     goto seek_failed;
534
535   fdsink->current_pos = new_offset;
536
537   GST_DEBUG_OBJECT (fdsink, "File descriptor %d to seek to position "
538       "%" G_GUINT64_FORMAT, fdsink->fd, fdsink->current_pos);
539
540   return TRUE;
541
542   /* ERRORS */
543 seek_failed:
544   {
545     GST_DEBUG_OBJECT (fdsink, "File descriptor %d failed to seek to position "
546         "%" G_GUINT64_FORMAT, fdsink->fd, new_offset);
547     return FALSE;
548   }
549 }
550
551 static gboolean
552 gst_fd_sink_event (GstBaseSink * sink, GstEvent * event)
553 {
554   GstEventType type;
555   GstFdSink *fdsink;
556
557   fdsink = GST_FD_SINK (sink);
558
559   type = GST_EVENT_TYPE (event);
560
561   switch (type) {
562     case GST_EVENT_NEWSEGMENT:
563     {
564       gint64 start, stop, pos;
565       GstFormat format;
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 (fdsink->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_fd_sink_do_seek (fdsink, (guint64) start))
578             goto seek_failed;
579         }
580       } else {
581         GST_DEBUG_OBJECT (fdsink,
582             "Ignored NEWSEGMENT event of format %u (%s)", (guint) format,
583             gst_format_get_name (format));
584       }
585       break;
586     }
587     default:
588       break;
589   }
590
591   return TRUE;
592
593 seek_failed:
594   {
595     GST_ELEMENT_ERROR (fdsink, RESOURCE, SEEK, (NULL),
596         ("Error while seeking on file descriptor %d: %s",
597             fdsink->fd, g_strerror (errno)));
598     return FALSE;
599   }
600
601 }
602
603 /*** GSTURIHANDLER INTERFACE *************************************************/
604
605 static GstURIType
606 gst_fd_sink_uri_get_type (void)
607 {
608   return GST_URI_SINK;
609 }
610
611 static gchar **
612 gst_fd_sink_uri_get_protocols (void)
613 {
614   static gchar *protocols[] = { (char *) "fd", NULL };
615
616   return protocols;
617 }
618
619 static const gchar *
620 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
621 {
622   GstFdSink *sink = GST_FD_SINK (handler);
623
624   return sink->uri;
625 }
626
627 static gboolean
628 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
629 {
630   gchar *protocol;
631   GstFdSink *sink = GST_FD_SINK (handler);
632   gint fd;
633
634   protocol = gst_uri_get_protocol (uri);
635   if (strcmp (protocol, "fd") != 0) {
636     g_free (protocol);
637     return FALSE;
638   }
639   g_free (protocol);
640
641   if (sscanf (uri, "fd://%d", &fd) != 1)
642     return FALSE;
643
644   return gst_fd_sink_update_fd (sink, fd);
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 }