203afa0176e9214e42c0beac6f0c30f59bc4a70b
[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 #include <sys/stat.h>
44 #include <sys/socket.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #ifdef _MSC_VER
51 #include <io.h>
52 #endif
53 #include <errno.h>
54 #include <string.h>
55
56 #include "gstfdsink.h"
57
58 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS_ANY);
62
63 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
64 #define GST_CAT_DEFAULT gst_fd_sink__debug
65
66
67 /* FdSink signals and args */
68 enum
69 {
70   /* FILL ME */
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   ARG_0,
77   ARG_FD
78 };
79
80 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
81     gpointer iface_data);
82
83 static void
84 _do_init (GType gst_fd_sink_type)
85 {
86   static const GInterfaceInfo urihandler_info = {
87     gst_fd_sink_uri_handler_init,
88     NULL,
89     NULL
90   };
91
92   g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
93       &urihandler_info);
94
95   GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
96 }
97
98 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
99     _do_init);
100
101 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105 static void gst_fd_sink_dispose (GObject * obj);
106
107 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
108 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
109     GstBuffer * buffer);
110 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
111 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
112 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
113 static gboolean gst_fd_sink_unlock_stop (GstBaseSink * basesink);
114
115 static void
116 gst_fd_sink_base_init (gpointer g_class)
117 {
118   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
119
120   gst_element_class_set_details_simple (gstelement_class,
121       "Filedescriptor Sink",
122       "Sink/File",
123       "Write data to a file descriptor", "Erik Walthinsen <omega@cse.ogi.edu>");
124   gst_element_class_add_pad_template (gstelement_class,
125       gst_static_pad_template_get (&sinktemplate));
126 }
127
128 static void
129 gst_fd_sink_class_init (GstFdSinkClass * klass)
130 {
131   GObjectClass *gobject_class;
132   GstBaseSinkClass *gstbasesink_class;
133
134   gobject_class = G_OBJECT_CLASS (klass);
135   gstbasesink_class = GST_BASE_SINK_CLASS (klass);
136
137   gobject_class->set_property = gst_fd_sink_set_property;
138   gobject_class->get_property = gst_fd_sink_get_property;
139   gobject_class->dispose = gst_fd_sink_dispose;
140
141   gstbasesink_class->get_times = NULL;
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 = NULL;
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, GstFdSinkClass * klass)
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
166   GST_BASE_SINK (fdsink)->sync = TRUE;
167 }
168
169 static void
170 gst_fd_sink_dispose (GObject * obj)
171 {
172   GstFdSink *fdsink = GST_FD_SINK (obj);
173
174   g_free (fdsink->uri);
175   fdsink->uri = NULL;
176
177   G_OBJECT_CLASS (parent_class)->dispose (obj);
178 }
179
180 static gboolean
181 gst_fd_sink_query (GstPad * pad, GstQuery * query)
182 {
183   GstFdSink *fdsink;
184   GstFormat format;
185
186   fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
187
188   switch (GST_QUERY_TYPE (query)) {
189     case GST_QUERY_POSITION:
190       gst_query_parse_position (query, &format, NULL);
191       switch (format) {
192         case GST_FORMAT_DEFAULT:
193         case GST_FORMAT_BYTES:
194           gst_query_set_position (query, GST_FORMAT_BYTES,
195               fdsink->bytes_written);
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;
219   guint size;
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_DATA (buffer);
231   size = GST_BUFFER_SIZE (buffer);
232
233 again:
234 #ifndef HAVE_WIN32
235   do {
236     GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
237         size);
238     retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
239   } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
240
241   if (retval == -1) {
242     if (errno == EBUSY)
243       goto stopped;
244     else
245       goto select_error;
246   }
247 #endif
248
249   GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
250       fdsink->fd);
251
252   written = write (fdsink->fd, data, size);
253
254   /* check for errors */
255   if (G_UNLIKELY (written < 0)) {
256     /* try to write again on non-fatal errors */
257     if (errno == EAGAIN || errno == EINTR)
258       goto again;
259
260     /* else go to our error handler */
261     goto write_error;
262   }
263
264   /* all is fine when we get here */
265   size -= written;
266   data += written;
267   fdsink->bytes_written += written;
268
269   GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
270
271   /* short write, select and try to write the remainder */
272   if (G_UNLIKELY (size > 0))
273     goto again;
274
275   return GST_FLOW_OK;
276
277 #ifndef HAVE_WIN32
278 select_error:
279   {
280     GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
281         ("select on file descriptor: %s.", g_strerror (errno)));
282     GST_DEBUG_OBJECT (fdsink, "Error during select");
283     return GST_FLOW_ERROR;
284   }
285 stopped:
286   {
287     GST_DEBUG_OBJECT (fdsink, "Select stopped");
288     return GST_FLOW_WRONG_STATE;
289   }
290 #endif
291
292 write_error:
293   {
294     switch (errno) {
295       case ENOSPC:
296         GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
297         break;
298       default:{
299         GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
300             (_("Error while writing to file descriptor \"%d\"."), fdsink->fd),
301             ("%s", g_strerror (errno)));
302       }
303     }
304     return GST_FLOW_ERROR;
305   }
306 }
307
308 static gboolean
309 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
310 {
311   struct stat stat_results;
312   off_t result;
313
314   /* see that it is a valid file descriptor */
315   if (fstat (fd, &stat_results) < 0)
316     goto invalid;
317
318   if (!S_ISREG (stat_results.st_mode))
319     goto not_seekable;
320
321   /* see if it is a seekable stream */
322   result = lseek (fd, 0, SEEK_CUR);
323   if (result == -1) {
324     switch (errno) {
325       case EINVAL:
326       case EBADF:
327         goto invalid;
328
329       case ESPIPE:
330         goto not_seekable;
331     }
332   } else
333     GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is seekable", fd);
334
335   return TRUE;
336
337 invalid:
338   {
339     GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
340         (_("File descriptor \"%d\" is not valid."), fd),
341         ("%s", g_strerror (errno)));
342     return FALSE;
343   }
344 not_seekable:
345   {
346     GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is a pipe", fd);
347     return TRUE;
348   }
349 }
350
351 static gboolean
352 gst_fd_sink_start (GstBaseSink * basesink)
353 {
354   GstFdSink *fdsink;
355   GstPollFD fd = GST_POLL_FD_INIT;
356
357   fdsink = GST_FD_SINK (basesink);
358   if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
359     return FALSE;
360
361   if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
362     goto socket_pair;
363
364   fd.fd = fdsink->fd;
365   gst_poll_add_fd (fdsink->fdset, &fd);
366   gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
367
368   return TRUE;
369
370   /* ERRORS */
371 socket_pair:
372   {
373     GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
374         GST_ERROR_SYSTEM);
375     return FALSE;
376   }
377 }
378
379 static gboolean
380 gst_fd_sink_stop (GstBaseSink * basesink)
381 {
382   GstFdSink *fdsink = GST_FD_SINK (basesink);
383
384   if (fdsink->fdset) {
385     gst_poll_free (fdsink->fdset);
386     fdsink->fdset = NULL;
387   }
388
389   return TRUE;
390 }
391
392 static gboolean
393 gst_fd_sink_unlock (GstBaseSink * basesink)
394 {
395   GstFdSink *fdsink = GST_FD_SINK (basesink);
396
397   GST_LOG_OBJECT (fdsink, "Flushing");
398   GST_OBJECT_LOCK (fdsink);
399   gst_poll_set_flushing (fdsink->fdset, TRUE);
400   GST_OBJECT_UNLOCK (fdsink);
401
402   return TRUE;
403 }
404
405 static gboolean
406 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
407 {
408   GstFdSink *fdsink = GST_FD_SINK (basesink);
409
410   GST_LOG_OBJECT (fdsink, "No longer flushing");
411   GST_OBJECT_LOCK (fdsink);
412   gst_poll_set_flushing (fdsink->fdset, FALSE);
413   GST_OBJECT_UNLOCK (fdsink);
414
415   return TRUE;
416 }
417
418 static gboolean
419 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
420 {
421   if (new_fd < 0)
422     return FALSE;
423
424   if (!gst_fd_sink_check_fd (fdsink, new_fd))
425     goto invalid;
426
427   /* assign the fd */
428   GST_OBJECT_LOCK (fdsink);
429   if (fdsink->fdset) {
430     GstPollFD fd = GST_POLL_FD_INIT;
431
432     fd.fd = fdsink->fd;
433     gst_poll_remove_fd (fdsink->fdset, &fd);
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
503 static gchar **
504 gst_fd_sink_uri_get_protocols (void)
505 {
506   static gchar *protocols[] = { "fd", NULL };
507
508   return protocols;
509 }
510
511 static const gchar *
512 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
513 {
514   GstFdSink *sink = GST_FD_SINK (handler);
515
516   return sink->uri;
517 }
518
519 static gboolean
520 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
521 {
522   gchar *protocol;
523   GstFdSink *sink = GST_FD_SINK (handler);
524   gint fd;
525
526   protocol = gst_uri_get_protocol (uri);
527   if (strcmp (protocol, "fd") != 0) {
528     g_free (protocol);
529     return FALSE;
530   }
531   g_free (protocol);
532
533   if (sscanf (uri, "fd://%d", &fd) != 1)
534     return FALSE;
535
536   return gst_fd_sink_update_fd (sink, fd);
537 }
538
539 static void
540 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
541 {
542   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
543
544   iface->get_type = gst_fd_sink_uri_get_type;
545   iface->get_protocols = gst_fd_sink_uri_get_protocols;
546   iface->get_uri = gst_fd_sink_uri_get_uri;
547   iface->set_uri = gst_fd_sink_uri_set_uri;
548 }