2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
24 * SECTION:element-fdsink
25 * @short_description: write to a unix file descriptor
26 * @see_also: #GstFdSrc
28 * Write data to a unix file descriptor.
30 * This element will sycnhronize on the clock before writing the data on the
31 * socket. For file descriptors where this does not make sense (files, ...) the
32 * ::sync property can be used to disable synchronisation.
34 * Last reviewed on 2006-04-28 (0.10.6)
41 #include "../../gst/gst-i18n-lib.h"
43 #include <sys/types.h>
45 #include <sys/socket.h>
57 #include "gstfdsink.h"
59 /* We add a control socket as in fdsrc to make it shutdown quickly when it's blocking on the fd.
60 * Select is used to determine when the fd is ready for use. When the element state is changed,
61 * it happens from another thread while fdsink is select'ing on the fd. The state-change thread
62 * sends a control message, so fdsink wakes up and changes state immediately otherwise
63 * it would stay blocked until it receives some data. */
65 /* the select call is also performed on the control sockets, that way
66 * we can send special commands to unblock the select call */
67 #define CONTROL_STOP 'S' /* stop the select call */
68 #define CONTROL_SOCKETS(sink) sink->control_sock
69 #define WRITE_SOCKET(sink) sink->control_sock[1]
70 #define READ_SOCKET(sink) sink->control_sock[0]
72 #define SEND_COMMAND(sink, command) \
74 unsigned char c; c = command; \
75 write (WRITE_SOCKET(sink), &c, 1); \
78 #define READ_COMMAND(sink, command, res) \
80 res = read(READ_SOCKET(sink), &command, 1); \
83 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
89 #define GST_CAT_DEFAULT gst_fd_sink__debug
91 static const GstElementDetails gst_fd_sink__details =
92 GST_ELEMENT_DETAILS ("Filedescriptor Sink",
94 "Write data to a file descriptor",
95 "Erik Walthinsen <omega@cse.ogi.edu>");
98 /* FdSink signals and args */
111 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
112 gpointer iface_data);
115 _do_init (GType gst_fd_sink_type)
117 static const GInterfaceInfo urihandler_info = {
118 gst_fd_sink_uri_handler_init,
123 g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
126 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
129 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
132 static void gst_fd_sink_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec);
134 static void gst_fd_sink_get_property (GObject * object, guint prop_id,
135 GValue * value, GParamSpec * pspec);
136 static void gst_fd_sink_dispose (GObject * obj);
138 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
139 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
141 static gboolean gst_fd_sink_start (GstBaseSink * basesink);
142 static gboolean gst_fd_sink_stop (GstBaseSink * basesink);
143 static gboolean gst_fd_sink_unlock (GstBaseSink * basesink);
146 gst_fd_sink_base_init (gpointer g_class)
148 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
150 gst_element_class_add_pad_template (gstelement_class,
151 gst_static_pad_template_get (&sinktemplate));
152 gst_element_class_set_details (gstelement_class, &gst_fd_sink__details);
156 gst_fd_sink_class_init (GstFdSinkClass * klass)
158 GObjectClass *gobject_class;
159 GstBaseSinkClass *gstbasesink_class;
161 gobject_class = G_OBJECT_CLASS (klass);
162 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
164 gobject_class->set_property = gst_fd_sink_set_property;
165 gobject_class->get_property = gst_fd_sink_get_property;
166 gobject_class->dispose = gst_fd_sink_dispose;
168 gstbasesink_class->get_times = NULL;
169 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_fd_sink_render);
170 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_fd_sink_start);
171 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_fd_sink_stop);
172 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_fd_sink_unlock);
173 gstbasesink_class->event = NULL;
175 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FD,
176 g_param_spec_int ("fd", "fd", "An open file descriptor to write to",
177 0, G_MAXINT, 1, G_PARAM_READWRITE));
181 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
185 pad = GST_BASE_SINK_PAD (fdsink);
186 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
189 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
190 fdsink->bytes_written = 0;
192 GST_BASE_SINK (fdsink)->sync = TRUE;
196 gst_fd_sink_dispose (GObject * obj)
198 GstFdSink *fdsink = GST_FD_SINK (obj);
200 g_free (fdsink->uri);
203 G_OBJECT_CLASS (parent_class)->dispose (obj);
207 gst_fd_sink_query (GstPad * pad, GstQuery * query)
212 fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
214 switch (GST_QUERY_TYPE (query)) {
215 case GST_QUERY_POSITION:
216 gst_query_parse_position (query, &format, NULL);
218 case GST_FORMAT_DEFAULT:
219 case GST_FORMAT_BYTES:
220 gst_query_set_position (query, GST_FORMAT_BYTES,
221 fdsink->bytes_written);
227 case GST_QUERY_FORMATS:
228 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
232 return gst_pad_query_default (pad, query);
237 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
250 fdsink = GST_FD_SINK (sink);
252 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
254 data = GST_BUFFER_DATA (buffer);
255 size = GST_BUFFER_SIZE (buffer);
261 FD_SET (READ_SOCKET (fdsink), &readfds);
264 FD_SET (fdsink->fd, &writefds);
267 GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
269 retval = select (FD_SETSIZE, &readfds, &writefds, NULL, NULL);
270 } while ((retval == -1 && errno == EINTR));
275 if (FD_ISSET (READ_SOCKET (fdsink), &readfds)) {
276 /* read all stop commands */
281 READ_COMMAND (fdsink, command, res);
283 GST_LOG_OBJECT (fdsink, "no more commands");
284 /* no more commands */
292 GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
295 written = write (fdsink->fd, data, size);
297 /* check for errors */
298 if (G_UNLIKELY (written < 0)) {
299 /* try to write again on non-fatal errors */
300 if (errno == EAGAIN || errno == EINTR)
303 /* else go to our error handler */
307 /* all is fine when we get here */
310 fdsink->bytes_written += written;
312 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
314 /* short write, select and try to write the remainder */
315 if (G_UNLIKELY (size > 0))
323 GST_ELEMENT_ERROR (fdsink, RESOURCE, READ, (NULL),
324 ("select on file descriptor: %s.", g_strerror (errno)));
325 GST_DEBUG_OBJECT (fdsink, "Error during select");
326 return GST_FLOW_ERROR;
330 GST_DEBUG_OBJECT (fdsink, "Select stopped");
331 return GST_FLOW_WRONG_STATE;
339 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
342 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
343 (_("Error while writing to file descriptor \"%d\"."), fdsink->fd),
344 ("%s", g_strerror (errno)));
347 return GST_FLOW_ERROR;
352 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
354 struct stat stat_results;
357 /* see that it is a valid file descriptor */
358 if (fstat (fd, &stat_results) < 0)
361 if (!S_ISREG (stat_results.st_mode))
364 /* see if it is a seekable stream */
365 result = lseek (fd, 0, SEEK_CUR);
376 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is seekable", fd);
382 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
383 (_("File descriptor \"%d\" is not valid."), fd),
384 ("%s", g_strerror (errno)));
389 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is a pipe", fd);
395 gst_fd_sink_start (GstBaseSink * basesink)
398 gint control_sock[2];
400 fdsink = GST_FD_SINK (basesink);
401 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
404 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
407 READ_SOCKET (fdsink) = control_sock[0];
408 WRITE_SOCKET (fdsink) = control_sock[1];
410 fcntl (READ_SOCKET (fdsink), F_SETFL, O_NONBLOCK);
411 fcntl (WRITE_SOCKET (fdsink), F_SETFL, O_NONBLOCK);
418 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
425 gst_fd_sink_stop (GstBaseSink * basesink)
427 GstFdSink *fdsink = GST_FD_SINK (basesink);
429 close (READ_SOCKET (fdsink));
430 close (WRITE_SOCKET (fdsink));
436 gst_fd_sink_unlock (GstBaseSink * basesink)
438 GstFdSink *fdsink = GST_FD_SINK (basesink);
440 SEND_COMMAND (fdsink, CONTROL_STOP);
446 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
451 if (!gst_fd_sink_check_fd (fdsink, new_fd))
455 GST_OBJECT_LOCK (fdsink);
457 g_free (fdsink->uri);
458 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
459 GST_OBJECT_UNLOCK (fdsink);
470 gst_fd_sink_set_property (GObject * object, guint prop_id,
471 const GValue * value, GParamSpec * pspec)
475 g_return_if_fail (GST_IS_FD_SINK (object));
477 fdsink = GST_FD_SINK (object);
483 fd = g_value_get_int (value);
484 gst_fd_sink_update_fd (fdsink, fd);
493 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
498 g_return_if_fail (GST_IS_FD_SINK (object));
500 fdsink = GST_FD_SINK (object);
504 g_value_set_int (value, fdsink->fd);
511 /*** GSTURIHANDLER INTERFACE *************************************************/
514 gst_fd_sink_uri_get_type (void)
519 gst_fd_sink_uri_get_protocols (void)
521 static gchar *protocols[] = { "fd", NULL };
526 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
528 GstFdSink *sink = GST_FD_SINK (handler);
534 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
537 GstFdSink *sink = GST_FD_SINK (handler);
540 protocol = gst_uri_get_protocol (uri);
541 if (strcmp (protocol, "fd") != 0) {
547 if (sscanf (uri, "fd://%d", &fd) != 1)
550 return gst_fd_sink_update_fd (sink, fd);
554 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
556 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
558 iface->get_type = gst_fd_sink_uri_get_type;
559 iface->get_protocols = gst_fd_sink_uri_get_protocols;
560 iface->get_uri = gst_fd_sink_uri_get_uri;
561 iface->set_uri = gst_fd_sink_uri_set_uri;