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