2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
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.
27 #include "gstmultifdsink.h"
32 GST_DEBUG_CATEGORY_STATIC (gst_multifdsink_debug);
33 #define GST_CAT_DEFAULT gst_multifdsink_debug
35 GstElementDetails gst_multifdsink_details =
36 GST_ELEMENT_DETAILS ("Filedescriptor Sink",
38 "Write data to one or more file descriptors",
39 "Erik Walthinsen <omega@cse.ogi.edu>");
42 /* MultiFdSink signals and args */
51 static guint gst_multifdsink_signals[LAST_SIGNAL] = { 0 };
60 #define _do_init(bla) \
61 GST_DEBUG_CATEGORY_INIT (gst_multifdsink_debug, "multifdsink", 0, "multifdsink element");
63 GST_BOILERPLATE_FULL (GstMultiFdSink, gst_multifdsink, GstElement,
64 GST_TYPE_ELEMENT, _do_init);
66 static void gst_multifdsink_add (GstElement * sink, gint fd);
67 static void gst_multifdsink_remove (GstElement * sink, gint fd);
68 static void gst_multifdsink_clear (GstElement * sink);
70 static void gst_multifdsink_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_multifdsink_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
75 static void gst_multifdsink_chain (GstPad * pad, GstData * _data);
79 gst_multifdsink_base_init (gpointer g_class)
81 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
83 gst_element_class_set_details (gstelement_class, &gst_multifdsink_details);
86 gst_multifdsink_class_init (GstMultiFdSinkClass * klass)
88 GObjectClass *gobject_class;
90 gobject_class = G_OBJECT_CLASS (klass);
92 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FDS,
93 g_param_spec_int ("fds", "fds", "A GArray of filedescriptors",
94 0, G_MAXINT, 1, G_PARAM_READWRITE));
96 gst_multifdsink_signals[ADD_SIGNAL] =
97 g_signal_new ("add", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
98 G_STRUCT_OFFSET (GstMultiFdSinkClass, add),
99 NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0);
100 gst_multifdsink_signals[REMOVE_SIGNAL] =
101 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
102 G_STRUCT_OFFSET (GstMultiFdSinkClass, remove),
103 NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0);
104 gst_multifdsink_signals[CLEAR_SIGNAL] =
105 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
106 G_STRUCT_OFFSET (GstMultiFdSinkClass, clear),
107 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
109 klass->add = gst_multifdsink_add;
110 klass->remove = gst_multifdsink_remove;
111 klass->clear = gst_multifdsink_clear;
113 gobject_class->set_property = gst_multifdsink_set_property;
114 gobject_class->get_property = gst_multifdsink_get_property;
118 gst_multifdsink_init (GstMultiFdSink * multifdsink)
120 multifdsink->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
121 gst_element_add_pad (GST_ELEMENT (multifdsink), multifdsink->sinkpad);
122 gst_pad_set_chain_function (multifdsink->sinkpad, gst_multifdsink_chain);
124 FD_ZERO (&multifdsink->writefds);
128 gst_multifdsink_add (GstElement * element, gint fd)
130 GstMultiFdSink *sink = GST_MULTIFDSINK (element);
132 FD_SET (fd, &sink->writefds);
136 gst_multifdsink_remove (GstElement * element, gint fd)
138 GstMultiFdSink *sink = GST_MULTIFDSINK (element);
140 FD_CLR (fd, &sink->writefds);
144 gst_multifdsink_clear (GstElement * element)
146 GstMultiFdSink *sink = GST_MULTIFDSINK (element);
148 FD_ZERO (&sink->writefds);
152 gst_multifdsink_chain (GstPad * pad, GstData * _data)
155 GstMultiFdSink *sink;
156 struct timeval timeout;
157 struct timeval *timeoutp;
161 sink = GST_MULTIFDSINK (gst_pad_get_parent (pad));
162 buf = GST_BUFFER (_data);
164 /* if the incoming buffer has a duration, we can use that as the timeout
165 * value; otherwise, we block */
166 GST_LOG_OBJECT (sink, "incoming buffer duration: %" GST_TIME_FORMAT,
167 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
169 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buf))) {
170 GST_TIME_TO_TIMEVAL (GST_BUFFER_DURATION (buf), timeout);
172 GST_LOG_OBJECT (sink, "select will be with timeout %" GST_TIME_FORMAT,
173 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
174 GST_LOG_OBJECT (sink, "select will be with timeout %d.%d",
175 timeout.tv_sec, timeout.tv_usec);
182 result = select (FD_SETSIZE, (fd_set *) 0, &sink->writefds, (fd_set *) 0,
185 /* Check the writes */
186 for (fd = 0; fd < FD_SETSIZE; fd++) {
187 if (FD_ISSET (fd, &sink->writefds)) {
188 gint writesize = GST_BUFFER_SIZE (buf);
191 GST_DEBUG ("writing %d bytes to file descriptor %d", writesize, fd);
192 written = write (fd, GST_BUFFER_DATA (buf), writesize);
193 if (written < writesize) {
194 GST_DEBUG ("wrote only %d bytes, removing filedescriptor %d ", written,
196 gst_multifdsink_remove (GST_ELEMENT (sink), fd);
201 gst_buffer_unref (buf);
205 gst_multifdsink_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
208 GstMultiFdSink *multifdsink;
210 multifdsink = GST_MULTIFDSINK (object);
221 gst_multifdsink_get_property (GObject * object, guint prop_id, GValue * value,
224 GstMultiFdSink *multifdsink;
226 multifdsink = GST_MULTIFDSINK (object);