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 * @see_also: #GstFdSrc
27 * Write data to a unix file descriptor.
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.
33 * Last reviewed on 2006-04-28 (0.10.6)
40 #include "../../gst/gst-i18n-lib.h"
42 #include <sys/types.h>
44 #include <sys/socket.h>
56 #include "gstfdsink.h"
58 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_DEBUG_CATEGORY_STATIC (gst_fd_sink__debug);
64 #define GST_CAT_DEFAULT gst_fd_sink__debug
67 /* FdSink signals and args */
80 static void gst_fd_sink_uri_handler_init (gpointer g_iface,
84 _do_init (GType gst_fd_sink_type)
86 static const GInterfaceInfo urihandler_info = {
87 gst_fd_sink_uri_handler_init,
92 g_type_add_interface_static (gst_fd_sink_type, GST_TYPE_URI_HANDLER,
95 GST_DEBUG_CATEGORY_INIT (gst_fd_sink__debug, "fdsink", 0, "fdsink element");
98 GST_BOILERPLATE_FULL (GstFdSink, gst_fd_sink, GstBaseSink, GST_TYPE_BASE_SINK,
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);
107 static gboolean gst_fd_sink_query (GstPad * pad, GstQuery * query);
108 static GstFlowReturn gst_fd_sink_render (GstBaseSink * sink,
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);
116 gst_fd_sink_base_init (gpointer g_class)
118 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_set_details_simple (gstelement_class,
121 "Filedescriptor Sink",
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));
129 gst_fd_sink_class_init (GstFdSinkClass * klass)
131 GObjectClass *gobject_class;
132 GstBaseSinkClass *gstbasesink_class;
134 gobject_class = G_OBJECT_CLASS (klass);
135 gstbasesink_class = GST_BASE_SINK_CLASS (klass);
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;
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;
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));
155 gst_fd_sink_init (GstFdSink * fdsink, GstFdSinkClass * klass)
159 pad = GST_BASE_SINK_PAD (fdsink);
160 gst_pad_set_query_function (pad, GST_DEBUG_FUNCPTR (gst_fd_sink_query));
163 fdsink->uri = g_strdup_printf ("fd://%d", fdsink->fd);
164 fdsink->bytes_written = 0;
166 GST_BASE_SINK (fdsink)->sync = TRUE;
170 gst_fd_sink_dispose (GObject * obj)
172 GstFdSink *fdsink = GST_FD_SINK (obj);
174 g_free (fdsink->uri);
177 G_OBJECT_CLASS (parent_class)->dispose (obj);
181 gst_fd_sink_query (GstPad * pad, GstQuery * query)
186 fdsink = GST_FD_SINK (GST_PAD_PARENT (pad));
188 switch (GST_QUERY_TYPE (query)) {
189 case GST_QUERY_POSITION:
190 gst_query_parse_position (query, &format, NULL);
192 case GST_FORMAT_DEFAULT:
193 case GST_FORMAT_BYTES:
194 gst_query_set_position (query, GST_FORMAT_BYTES,
195 fdsink->bytes_written);
201 case GST_QUERY_FORMATS:
202 gst_query_set_formats (query, 2, GST_FORMAT_DEFAULT, GST_FORMAT_BYTES);
206 gst_query_set_uri (query, fdsink->uri);
210 return gst_pad_query_default (pad, query);
215 gst_fd_sink_render (GstBaseSink * sink, GstBuffer * buffer)
226 fdsink = GST_FD_SINK (sink);
228 g_return_val_if_fail (fdsink->fd >= 0, GST_FLOW_ERROR);
230 data = GST_BUFFER_DATA (buffer);
231 size = GST_BUFFER_SIZE (buffer);
236 GST_DEBUG_OBJECT (fdsink, "going into select, have %d bytes to write",
238 retval = gst_poll_wait (fdsink->fdset, GST_CLOCK_TIME_NONE);
239 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
249 GST_DEBUG_OBJECT (fdsink, "writing %d bytes to file descriptor %d", size,
252 written = write (fdsink->fd, data, size);
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)
260 /* else go to our error handler */
264 /* all is fine when we get here */
267 fdsink->bytes_written += written;
269 GST_DEBUG_OBJECT (fdsink, "wrote %d bytes, %d left", written, size);
271 /* short write, select and try to write the remainder */
272 if (G_UNLIKELY (size > 0))
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;
287 GST_DEBUG_OBJECT (fdsink, "Select stopped");
288 return GST_FLOW_WRONG_STATE;
296 GST_ELEMENT_ERROR (fdsink, RESOURCE, NO_SPACE_LEFT, (NULL), (NULL));
299 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
300 (_("Error while writing to file descriptor \"%d\"."), fdsink->fd),
301 ("%s", g_strerror (errno)));
304 return GST_FLOW_ERROR;
309 gst_fd_sink_check_fd (GstFdSink * fdsink, int fd)
311 struct stat stat_results;
314 /* see that it is a valid file descriptor */
315 if (fstat (fd, &stat_results) < 0)
318 if (!S_ISREG (stat_results.st_mode))
321 /* see if it is a seekable stream */
322 result = lseek (fd, 0, SEEK_CUR);
333 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is seekable", fd);
339 GST_ELEMENT_ERROR (fdsink, RESOURCE, WRITE,
340 (_("File descriptor \"%d\" is not valid."), fd),
341 ("%s", g_strerror (errno)));
346 GST_DEBUG_OBJECT (fdsink, "File descriptor \"%d\" is a pipe", fd);
352 gst_fd_sink_start (GstBaseSink * basesink)
355 GstPollFD fd = GST_POLL_FD_INIT;
357 fdsink = GST_FD_SINK (basesink);
358 if (!gst_fd_sink_check_fd (fdsink, fdsink->fd))
361 if ((fdsink->fdset = gst_poll_new (TRUE)) == NULL)
365 gst_poll_add_fd (fdsink->fdset, &fd);
366 gst_poll_fd_ctl_write (fdsink->fdset, &fd, TRUE);
373 GST_ELEMENT_ERROR (fdsink, RESOURCE, OPEN_READ_WRITE, (NULL),
380 gst_fd_sink_stop (GstBaseSink * basesink)
382 GstFdSink *fdsink = GST_FD_SINK (basesink);
385 gst_poll_free (fdsink->fdset);
386 fdsink->fdset = NULL;
393 gst_fd_sink_unlock (GstBaseSink * basesink)
395 GstFdSink *fdsink = GST_FD_SINK (basesink);
397 GST_LOG_OBJECT (fdsink, "Flushing");
398 GST_OBJECT_LOCK (fdsink);
399 gst_poll_set_flushing (fdsink->fdset, TRUE);
400 GST_OBJECT_UNLOCK (fdsink);
406 gst_fd_sink_unlock_stop (GstBaseSink * basesink)
408 GstFdSink *fdsink = GST_FD_SINK (basesink);
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);
419 gst_fd_sink_update_fd (GstFdSink * fdsink, int new_fd)
424 if (!gst_fd_sink_check_fd (fdsink, new_fd))
428 GST_OBJECT_LOCK (fdsink);
430 GstPollFD fd = GST_POLL_FD_INIT;
433 gst_poll_remove_fd (fdsink->fdset, &fd);
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)
504 gst_fd_sink_uri_get_protocols (void)
506 static gchar *protocols[] = { "fd", NULL };
512 gst_fd_sink_uri_get_uri (GstURIHandler * handler)
514 GstFdSink *sink = GST_FD_SINK (handler);
520 gst_fd_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri)
523 GstFdSink *sink = GST_FD_SINK (handler);
526 protocol = gst_uri_get_protocol (uri);
527 if (strcmp (protocol, "fd") != 0) {
533 if (sscanf (uri, "fd://%d", &fd) != 1)
536 return gst_fd_sink_update_fd (sink, fd);
540 gst_fd_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
542 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
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;