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 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.
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 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
65 #define GST_CAT_DEFAULT gst_fd_sink__debug
68 /* FdSink signals and args */
81 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
85 _do_init (GType gst_fd_sink_type)
87 static const GInterfaceInfo urihandler_info = {
88 gst_fd_sink_uri_handler_init,
93 g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
96 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
99 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
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);
108 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
109 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
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);
117 gst_fd_sink_base_init (gpointer g_class)
119 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
121 gst_element_class_set_details_simple (gstelement_class,
122 "Filedescriptor Sink",
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));
130 gst_fd_sink_class_init (GstFdSinkClass * klass)
132 GObjectClass *gobject_class;
133 GstBaseSinkClass *gstbasesink_class;
135 gobject_class = G_OBJECT_CLASS (klass);
136 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
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;
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;
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));
156 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
160 pad = GST_BASE_SINK_PAD (fdsink);
161 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
164 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
165 fdsink->bytes_written = 0;
167 GST_BASE_SINK (fdsink)->sync = TRUE;
171 gst_fd_sink_dispose (GObject * obj)
173 GstFdSink *fdsink = GST_FD_SINK (obj);
175 g_free (fdsink->uri);
178 G_OBJECT_CLASS (parent_class)->dispose (obj);
182 gst_fd_sink_query (GstPad * pad, GstQuery * query)
187 fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
189 switch (GST_QUERY_TYPE (query)) {
190 case GST_QUERY_POSITION:
191 gst_query_parse_position (query, &format, NULL);
193 case GST_FORMAT_DEFAULT:
194 case GST_FORMAT_BYTES:
195 gst_query_set_position (query, GST_FORMAT_BYTES,
196 fdsink->bytes_written);
202 case GST_QUERY_FORMATS:
203 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
207 return gst_pad_query_default (pad, query);
212 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
223 fdsink = GST_FD_SINK (sink);
225 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
227 data = GST_BUFFER_DATA (buffer);
228 size = GST_BUFFER_SIZE (buffer);
233 GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
235 retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
236 } while (retval == -1 && errno == EINTR);
246 GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
249 written = write (fdsink->fd, data, size);
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)
257 /* else go to our error handler */
261 /* all is fine when we get here */
264 fdsink->bytes_written += written;
266 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
268 /* short write, select and try to write the remainder */
269 if (G_UNLIKELY (size > 0))
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;
284 GST_DEBUG_OBJECT (fdsink, "Select stopped");
285 return GST_FLOW_WRONG_STATE;
293 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
296 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
297 (_("Error while writing to file descriptor \"%d\"."), fdsink->fd),
298 ("%s", g_strerror (errno)));
301 return GST_FLOW_ERROR;
306 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
308 struct stat stat_results;
311 /* see that it is a valid file descriptor */
312 if (fstat (fd, &stat_results) < 0)
315 if (!S_ISREG (stat_results.st_mode))
318 /* see if it is a seekable stream */
319 result = lseek (fd, 0, SEEK_CUR);
330 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is seekable", fd);
336 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
337 (_("File descriptor \"%d\" is not valid."), fd),
338 ("%s", g_strerror (errno)));
343 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is a pipe", fd);
349 gst_fd_sink_start (GstBaseSink * basesink)
352 GstPollFD fd = GST_POLL_FD_INIT;
354 fdsink = GST_FD_SINK (basesink);
355 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
358 if ((fdsink->fdset = gst_poll_new (GST_POLL_MODE_AUTO, TRUE)) == NULL)
362 gst_poll_add_fd (fdsink->fdset, &fd);
363 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
370 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
377 gst_fd_sink_stop (GstBaseSink * basesink)
379 GstFdSink *fdsink = GST_FD_SINK (basesink);
382 gst_poll_free (fdsink->fdset);
383 fdsink->fdset = NULL;
390 gst_fd_sink_unlock (GstBaseSink * basesink)
392 GstFdSink *fdsink = GST_FD_SINK (basesink);
394 GST_LOG_OBJECT (fdsink, "Flushing");
395 GST_OBJECT_LOCK (fdsink);
396 gst_poll_set_flushing (fdsink->fdset, TRUE);
397 GST_OBJECT_UNLOCK (fdsink);
403 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
405 GstFdSink *fdsink = GST_FD_SINK (basesink);
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);
416 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
421 if (!gst_fd_sink_check_fd (fdsink, new_fd))
425 GST_OBJECT_LOCK (fdsink);
427 GstPollFD fd = { 0 };
430 gst_poll_remove_fd (fdsink->fdset, &fd);
432 /* Reset the GstPollFD */
433 memset (&fd, 0, sizeof (GstPollFD));
436 gst_poll_add_fd (fdsink->fdset, &fd);
437 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
440 g_free (fdsink->uri);
441 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
443 GST_OBJECT_UNLOCK (fdsink);
454 gst_fd_sink_set_property (GObject * object, guint prop_id,
455 const GValue * value, GParamSpec * pspec)
459 g_return_if_fail (GST_IS_FD_SINK (object));
461 fdsink = GST_FD_SINK (object);
467 fd = g_value_get_int (value);
468 gst_fd_sink_update_fd (fdsink, fd);
477 gst_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
482 g_return_if_fail (GST_IS_FD_SINK (object));
484 fdsink = GST_FD_SINK (object);
488 g_value_set_int (value, fdsink->fd);
495 /*** GSTURIHANDLER INTERFACE *************************************************/
498 gst_fd_sink_uri_get_type (void)
503 gst_fd_sink_uri_get_protocols (void)
505 static gchar *protocols[] = { "fd", NULL };
510 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
512 GstFdSink *sink = GST_FD_SINK (handler);
518 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
521 GstFdSink *sink = GST_FD_SINK (handler);
524 protocol = gst_uri_get_protocol (uri);
525 if (strcmp (protocol, "fd") != 0) {
531 if (sscanf (uri, "fd://%d", &fd) != 1)
534 return gst_fd_sink_update_fd (sink, fd);
538 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
540 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
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;