2af4ad92d834996ee4e807003c9b48f5997a8a72
[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  * @short_description: write to a unix file descriptor
26  * @see_also: #GstFdSrc
27  *
28  * Write data to a unix file descriptor.
29  *
30  * This element will synchronize on the clock before writing the data on the
31  * socket. For file descriptors where this does not make sense (files, ...) the
32  * #GstBaseSink:sync property can be used to disable synchronisation.
33  *
34  * Last reviewed on 2006-04-28 (0.10.6)
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #  include "config.h"
39 #endif
40
41 #include "../../gst/gst-i18n-lib.h"
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/socket.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51 #ifdef _MSC_VER
52 #include <io.h>
53 #endif
54 #include <errno.h>
55 #include <string.h>
56
57 #include "gstfdsink.h"
58
59 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
60     GST_PAD_SINK,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS_ANY);
63
64 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
65 #define GST_CAT_DEFAULT gst_fd_sink__debug
66
67
68 /* FdSink signals and args */
69 enum
70 {
71   /* FILL ME */
72   LAST_SIGNAL
73 };
74
75 enum
76 {
77   ARG_0,
78   ARG_FD
79 };
80
81 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
82     gpointer iface_data);
83
84 static void
85 _do_init (GType gst_fd_sink_type)
86 {
87   static const GInterfaceInfo urihandler_info = {
88     gst_fd_sink_uri_handler_init,
89     NULL,
90     NULL
91   };
92
93   g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
94       &urihandler_info);
95
96   GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
97 }
98
99 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
100     _do_init);
101
102 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106 static void gst_fd_sink_dispose (GObject * obj);
107
108 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
109 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
110     GstBuffer * buffer);
111 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
112 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
113 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
114 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
115
116 static void
117 gst_fd_sink_base_init (gpointer g_class)
118 {
119   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
120
121   gst_element_class_set_details_simple (gstelement_class,
122       "Filedescriptor Sink",
123       "Sink/File",
124       "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
125   gst_element_class_add_pad_template (gstelement_class,
126       gst_static_pad_template_get (&sinktemplate));
127 }
128
129 static void
130 gst_fd_sink_class_init (GstFdSinkClass * klass)
131 {
132   GObjectClass *gobject_class;
133   GstBaseSinkClass *gstbasesink_class;
134
135   gobject_class = G_OBJECT_CLASS (klass);
136   gstbasesink_class = GST_BASE_SINK_CLASS (klass);
137
138   gobject_class->set_property = gst_fd_sink_set_property;
139   gobject_class->get_property = gst_fd_sink_get_property;
140   gobject_class->dispose = gst_fd_sink_dispose;
141
142   gstbasesink_class->get_times = NULL;
143   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
144   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
145   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
146   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
147   gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock_stop);
148   gstbasesink_class->event = NULL;
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));
153 }
154
155 static void
156 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
157 {
158   GstPad *pad;
159
160   pad = GST_BASE_SINK_PAD (fdsink);
161   gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
162
163   fdsink->fd = 1;
164   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
165   fdsink->bytes_written = 0;
166
167   GST_BASE_SINK (fdsink)->sync = TRUE;
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,
196               fdsink->bytes_written);
197           return TRUE;
198         default:
199           return FALSE;
200       }
201
202     case GST_QUERY_FORMATS:
203       gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
204       return TRUE;
205
206     default:
207       return gst_pad_query_default (pad, query);
208   }
209 }
210
211 static GstFlowReturn
212 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
213 {
214   GstFdSink *fdsink;
215   guint8 *data;
216   guint size;
217   gint written;
218
219 #ifndef HAVE_WIN32
220   gint retval;
221 #endif
222
223   fdsink = GST_FD_SINK (sink);
224
225   g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
226
227   data = GST_BUFFER_DATA (buffer);
228   size = GST_BUFFER_SIZE (buffer);
229
230 again:
231 #ifndef HAVE_WIN32
232   do {
233     GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
234         size);
235     retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
236   } while (retval == -1 && errno == EINTR);
237
238   if (retval == -1) {
239     if (errno == EBUSY)
240       goto stopped;
241     else
242       goto select_error;
243   }
244 #endif
245
246   GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
247       fdsink->fd);
248
249   written = write (fdsink->fd, data, size);
250
251   /* check for errors */
252   if (G_UNLIKELY (written < 0)) {
253     /* try to write again on non-fatal errors */
254     if (errno == EAGAIN || errno == EINTR)
255       goto again;
256
257     /* else go to our error handler */
258     goto write_error;
259   }
260
261   /* all is fine when we get here */
262   size -= written;
263   data += written;
264   fdsink->bytes_written += written;
265
266   GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
267
268   /* short write, select and try to write the remainder */
269   if (G_UNLIKELY (size > 0))
270     goto again;
271
272   return GST_FLOW_OK;
273
274 #ifndef HAVE_WIN32
275 select_error:
276   {
277     GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
278         ("select on file descriptor: %s.", g_strerror (errno)));
279     GST_DEBUG_OBJECT (fdsink, "Error during select");
280     return GST_FLOW_ERROR;
281   }
282 stopped:
283   {
284     GST_DEBUG_OBJECT (fdsink, "Select stopped");
285     return GST_FLOW_WRONG_STATE;
286   }
287 #endif
288
289 write_error:
290   {
291     switch (errno) {
292       case ENOSPC:
293         GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
294         break;
295       default:{
296         GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
297             (_("Error while writing to file descriptor \"%d\"."), fdsink->fd),
298             ("%s", g_strerror (errno)));
299       }
300     }
301     return GST_FLOW_ERROR;
302   }
303 }
304
305 static gboolean
306 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
307 {
308   struct stat stat_results;
309   off_t result;
310
311   /* see that it is a valid file descriptor */
312   if (fstat (fd, &stat_results) < 0)
313     goto invalid;
314
315   if (!S_ISREG (stat_results.st_mode))
316     goto not_seekable;
317
318   /* see if it is a seekable stream */
319   result = lseek (fd, 0, SEEK_CUR);
320   if (result == -1) {
321     switch (errno) {
322       case EINVAL:
323       case EBADF:
324         goto invalid;
325
326       case ESPIPE:
327         goto not_seekable;
328     }
329   } else
330     GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is seekable", fd);
331
332   return TRUE;
333
334 invalid:
335   {
336     GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
337         (_("File descriptor \"%d\" is not valid."), fd),
338         ("%s", g_strerror (errno)));
339     return FALSE;
340   }
341 not_seekable:
342   {
343     GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is a pipe", fd);
344     return TRUE;
345   }
346 }
347
348 static gboolean
349 gst_fd_sink_start (GstBaseSink * basesink)
350 {
351   GstFdSink *fdsink;
352   GstPollFD fd = { 0, };
353
354   fdsink = GST_FD_SINK (basesink);
355   if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
356     return FALSE;
357
358   if ((fdsink->fdset = gst_poll_new (GST_POLL_MODE_AUTO, TRUE)) == NULL)
359     goto socket_pair;
360
361   fd.fd = fdsink->fd;
362   gst_poll_add_fd (fdsink->fdset, &fd);
363   gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
364
365   return TRUE;
366
367   /* ERRORS */
368 socket_pair:
369   {
370     GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
371         GST_ERROR_SYSTEM);
372     return FALSE;
373   }
374 }
375
376 static gboolean
377 gst_fd_sink_stop (GstBaseSink * basesink)
378 {
379   GstFdSink *fdsink = GST_FD_SINK (basesink);
380
381   if (fdsink->fdset) {
382     gst_poll_free (fdsink->fdset);
383     fdsink->fdset = NULL;
384   }
385
386   return TRUE;
387 }
388
389 static gboolean
390 gst_fd_sink_unlock (GstBaseSink * basesink)
391 {
392   GstFdSink *fdsink = GST_FD_SINK (basesink);
393
394   GST_LOG_OBJECT (fdsink, "Flushing");
395   GST_OBJECT_LOCK (fdsink);
396   gst_poll_set_flushing (fdsink->fdset, TRUE);
397   GST_OBJECT_UNLOCK (fdsink);
398
399   return TRUE;
400 }
401
402 static gboolean
403 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
404 {
405   GstFdSink *fdsink = GST_FD_SINK (basesink);
406
407   GST_LOG_OBJECT (fdsink, "No longer flushing");
408   GST_OBJECT_LOCK (fdsink);
409   gst_poll_set_flushing (fdsink->fdset, FALSE);
410   GST_OBJECT_UNLOCK (fdsink);
411
412   return TRUE;
413 }
414
415 static gboolean
416 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
417 {
418   if (new_fd < 0)
419     return FALSE;
420
421   if (!gst_fd_sink_check_fd (fdsink, new_fd))
422     goto invalid;
423
424   /* assign the fd */
425   GST_OBJECT_LOCK (fdsink);
426   if (fdsink->fdset) {
427     GstPollFD fd = { 0 };
428
429     fd.fd = fdsink->fd;
430     gst_poll_remove_fd (fdsink->fdset, &fd);
431
432     /* Reset the GstPollFD */
433     memset (&fd, 0, sizeof (GstPollFD));
434
435     fd.fd = new_fd;
436     gst_poll_add_fd (fdsink->fdset, &fd);
437     gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
438   }
439   fdsink->fd = new_fd;
440   g_free (fdsink->uri);
441   fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
442
443   GST_OBJECT_UNLOCK (fdsink);
444
445   return TRUE;
446
447 invalid:
448   {
449     return FALSE;
450   }
451 }
452
453 static void
454 gst_fd_sink_set_property (GObject * object, guint prop_id,
455     const GValue * value, GParamSpec * pspec)
456 {
457   GstFdSink *fdsink;
458
459   g_return_if_fail (GST_IS_FD_SINK (object));
460
461   fdsink = GST_FD_SINK (object);
462
463   switch (prop_id) {
464     case ARG_FD:{
465       int fd;
466
467       fd = g_value_get_int (value);
468       gst_fd_sink_update_fd (fdsink, fd);
469       break;
470     }
471     default:
472       break;
473   }
474 }
475
476 static void
477 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
478     GParamSpec * pspec)
479 {
480   GstFdSink *fdsink;
481
482   g_return_if_fail (GST_IS_FD_SINK (object));
483
484   fdsink = GST_FD_SINK (object);
485
486   switch (prop_id) {
487     case ARG_FD:
488       g_value_set_int (value, fdsink->fd);
489       break;
490     default:
491       break;
492   }
493 }
494
495 /*** GSTURIHANDLER INTERFACE *************************************************/
496
497 static GstURIType
498 gst_fd_sink_uri_get_type (void)
499 {
500   return GST_URI_SINK;
501 }
502 static gchar **
503 gst_fd_sink_uri_get_protocols (void)
504 {
505   static gchar *protocols[] = { "fd", NULL };
506
507   return protocols;
508 }
509 static const gchar *
510 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
511 {
512   GstFdSink *sink = GST_FD_SINK (handler);
513
514   return sink->uri;
515 }
516
517 static gboolean
518 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
519 {
520   gchar *protocol;
521   GstFdSink *sink = GST_FD_SINK (handler);
522   gint fd;
523
524   protocol = gst_uri_get_protocol (uri);
525   if (strcmp (protocol, "fd") != 0) {
526     g_free (protocol);
527     return FALSE;
528   }
529   g_free (protocol);
530
531   if (sscanf (uri, "fd://%d", &fd) != 1)
532     return FALSE;
533
534   return gst_fd_sink_update_fd (sink, fd);
535 }
536
537 static void
538 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
539 {
540   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
541
542   iface->get_type = gst_fd_sink_uri_get_type;
543   iface->get_protocols = gst_fd_sink_uri_get_protocols;
544   iface->get_uri = gst_fd_sink_uri_get_uri;
545   iface->set_uri = gst_fd_sink_uri_set_uri;
546 }