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