2 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
5 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
26 /* elementfactory information */
27 static GstElementDetails esdsink_details = {
30 "Plays audio to an esound server",
32 "Richard Boulton <richard-gst@tartarus.org>",
36 /* Signals and args */
51 GST_PADTEMPLATE_FACTORY (sink_factory,
52 "sink", /* the name of the pads */
53 GST_PAD_SINK, /* type of the pad */
54 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
56 "esdsink_sink8", /* the name of the caps */
57 "audio/raw", /* the mime type of the caps */
58 /* Properties follow: */
59 "format", GST_PROPS_STRING ("int"),
60 "law", GST_PROPS_INT (0),
61 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
62 "width", GST_PROPS_INT (8),
63 "depth", GST_PROPS_INT (8),
64 "rate", GST_PROPS_INT_RANGE (8000, 96000),
65 "channels", GST_PROPS_LIST (GST_PROPS_INT (1), GST_PROPS_INT (2))
68 "esdsink_sink16", /* the name of the caps */
69 "audio/raw", /* the mime type of the caps */
70 /* Properties follow: */
71 "format", GST_PROPS_STRING ("int"),
72 "law", GST_PROPS_INT (0),
73 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
74 "signed", GST_PROPS_BOOLEAN (TRUE),
75 "width", GST_PROPS_INT (16),
76 "depth", GST_PROPS_INT (16),
77 "rate", GST_PROPS_INT_RANGE (8000, 96000),
78 "channels", GST_PROPS_LIST (GST_PROPS_INT (1), GST_PROPS_INT (2))
82 static void gst_esdsink_class_init (GstEsdsinkClass *klass);
83 static void gst_esdsink_init (GstEsdsink *esdsink);
85 static gboolean gst_esdsink_open_audio (GstEsdsink *sink);
86 static void gst_esdsink_close_audio (GstEsdsink *sink);
87 static GstElementStateReturn gst_esdsink_change_state (GstElement *element);
88 static gboolean gst_esdsink_sync_parms (GstEsdsink *esdsink);
89 static GstPadConnectReturn gst_esdsink_sinkconnect (GstPad *pad, GstCaps *caps);
91 static void gst_esdsink_chain (GstPad *pad, GstBuffer *buf);
93 static void gst_esdsink_set_property (GObject *object, guint prop_id,
94 const GValue *value, GParamSpec *pspec);
95 static void gst_esdsink_get_property (GObject *object, guint prop_id,
96 GValue *value, GParamSpec *pspec);
98 #define GST_TYPE_ESDSINK_DEPTHS (gst_esdsink_depths_get_type())
100 gst_esdsink_depths_get_type (void)
102 static GType esdsink_depths_type = 0;
103 static GEnumValue esdsink_depths[] = {
105 {16, "16", "16 Bits"},
108 if (!esdsink_depths_type) {
109 esdsink_depths_type = g_enum_register_static("GstEsdsinkDepths", esdsink_depths);
111 return esdsink_depths_type;
114 #define GST_TYPE_ESDSINK_CHANNELS (gst_esdsink_channels_get_type())
116 gst_esdsink_channels_get_type (void)
118 static GType esdsink_channels_type = 0;
119 static GEnumValue esdsink_channels[] = {
124 if (!esdsink_channels_type) {
125 esdsink_channels_type = g_enum_register_static("GstEsdsinkChannels", esdsink_channels);
127 return esdsink_channels_type;
131 static GstElementClass *parent_class = NULL;
132 /*static guint gst_esdsink_signals[LAST_SIGNAL] = { 0 }; */
135 gst_esdsink_get_type (void)
137 static GType esdsink_type = 0;
140 static const GTypeInfo esdsink_info = {
141 sizeof(GstEsdsinkClass), NULL,
143 (GClassInitFunc)gst_esdsink_class_init,
148 (GInstanceInitFunc)gst_esdsink_init,
150 esdsink_type = g_type_register_static(GST_TYPE_ELEMENT, "GstEsdsink", &esdsink_info, 0);
156 gst_esdsink_class_init (GstEsdsinkClass *klass)
158 GObjectClass *gobject_class;
159 GstElementClass *gstelement_class;
161 gobject_class = (GObjectClass*)klass;
162 gstelement_class = (GstElementClass*)klass;
164 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
166 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
167 g_param_spec_boolean("mute","mute","mute",
168 TRUE,G_PARAM_READWRITE)); /* CHECKME */
169 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEPTH,
170 g_param_spec_enum("depth","depth","depth",
171 GST_TYPE_ESDSINK_DEPTHS,16,G_PARAM_READWRITE)); /* CHECKME! */
172 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNELS,
173 g_param_spec_enum("channels","channels","channels",
174 GST_TYPE_ESDSINK_CHANNELS,2,G_PARAM_READWRITE)); /* CHECKME! */
175 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_RATE,
176 g_param_spec_int("frequency","frequency","frequency",
177 G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
178 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HOST,
179 g_param_spec_string("host","host","host",
180 NULL, G_PARAM_READWRITE)); /* CHECKME */
182 gobject_class->set_property = gst_esdsink_set_property;
183 gobject_class->get_property = gst_esdsink_get_property;
185 gstelement_class->change_state = gst_esdsink_change_state;
189 gst_esdsink_init(GstEsdsink *esdsink)
191 esdsink->sinkpad = gst_pad_new_from_template (
192 GST_PADTEMPLATE_GET (sink_factory), "sink");
193 gst_element_add_pad(GST_ELEMENT(esdsink), esdsink->sinkpad);
194 gst_pad_set_chain_function(esdsink->sinkpad, GST_DEBUG_FUNCPTR(gst_esdsink_chain));
195 gst_pad_set_connect_function(esdsink->sinkpad, gst_esdsink_sinkconnect);
197 esdsink->mute = FALSE;
199 /* FIXME: get default from somewhere better than just putting them inline. */
200 esdsink->format = 16;
202 esdsink->channels = 2;
203 esdsink->frequency = 44100;
204 esdsink->host = NULL;
208 gst_esdsink_sync_parms (GstEsdsink *esdsink)
210 g_return_val_if_fail (esdsink != NULL, FALSE);
211 g_return_val_if_fail (GST_IS_ESDSINK (esdsink), FALSE);
213 if (esdsink->fd == -1) return TRUE;
215 /* Need to set fd to use new parameters: only way to do this is to reopen. */
216 gst_esdsink_close_audio (esdsink);
217 return gst_esdsink_open_audio (esdsink);
220 static GstPadConnectReturn
221 gst_esdsink_sinkconnect (GstPad *pad, GstCaps *caps)
225 esdsink = GST_ESDSINK (gst_pad_get_parent (pad));
227 if (!GST_CAPS_IS_FIXED (caps))
228 return GST_PAD_CONNECT_DELAYED;
230 esdsink->depth = gst_caps_get_int (caps, "depth");
231 esdsink->channels = gst_caps_get_int (caps, "channels");
232 esdsink->frequency = gst_caps_get_int (caps, "rate");
234 if (gst_esdsink_sync_parms (esdsink))
235 return GST_PAD_CONNECT_OK;
237 return GST_PAD_CONNECT_REFUSED;
241 gst_esdsink_chain (GstPad *pad, GstBuffer *buf)
245 g_return_if_fail(pad != NULL);
246 g_return_if_fail(GST_IS_PAD(pad));
247 g_return_if_fail(buf != NULL);
249 esdsink = GST_ESDSINK (gst_pad_get_parent (pad));
251 if (GST_BUFFER_DATA (buf) != NULL) {
252 if (!esdsink->mute && esdsink->fd >= 0) {
253 GST_DEBUG (0, "esdsink: fd=%d data=%p size=%d",
254 esdsink->fd, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
255 write (esdsink->fd, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
258 gst_buffer_unref (buf);
262 gst_esdsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
266 /* it's not null if we got it, but it might not be ours */
267 g_return_if_fail(GST_IS_ESDSINK(object));
268 esdsink = GST_ESDSINK(object);
272 esdsink->mute = g_value_get_boolean (value);
275 esdsink->depth = g_value_get_enum (value);
276 gst_esdsink_sync_parms (esdsink);
279 esdsink->channels = g_value_get_enum (value);
280 gst_esdsink_sync_parms (esdsink);
283 esdsink->frequency = g_value_get_int (value);
284 gst_esdsink_sync_parms (esdsink);
287 if (esdsink->host != NULL) g_free(esdsink->host);
288 if (g_value_get_string (value) == NULL)
289 esdsink->host = NULL;
291 esdsink->host = g_strdup (g_value_get_string (value));
299 gst_esdsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
303 /* it's not null if we got it, but it might not be ours */
304 g_return_if_fail(GST_IS_ESDSINK(object));
305 esdsink = GST_ESDSINK(object);
309 g_value_set_boolean (value, esdsink->mute);
312 g_value_set_enum (value, esdsink->depth);
315 g_value_set_enum (value, esdsink->channels);
318 g_value_set_int (value, esdsink->frequency);
321 g_value_set_string (value, esdsink->host);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 plugin_init (GModule *module, GstPlugin *plugin)
332 GstElementFactory *factory;
334 factory = gst_elementfactory_new("esdsink", GST_TYPE_ESDSINK,
336 g_return_val_if_fail(factory != NULL, FALSE);
338 gst_elementfactory_add_padtemplate(factory, GST_PADTEMPLATE_GET (sink_factory));
340 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
345 GstPluginDesc plugin_desc = {
353 gst_esdsink_open_audio (GstEsdsink *sink)
355 /* Name used by esound for this connection. */
356 const char * connname = "GStreamer";
358 /* Bitmap describing audio format. */
359 esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
361 g_return_val_if_fail (sink->fd == -1, FALSE);
363 if (sink->depth == 16) esdformat |= ESD_BITS16;
364 else if (sink->depth == 8) esdformat |= ESD_BITS8;
366 GST_DEBUG (0, "esdsink: invalid bit depth (%d)", sink->depth);
370 if (sink->channels == 2) esdformat |= ESD_STEREO;
371 else if (sink->channels == 1) esdformat |= ESD_MONO;
373 GST_DEBUG (0, "esdsink: invalid number of channels (%d)", sink->channels);
377 GST_DEBUG (0, "esdsink: attempting to open connection to esound server");
378 sink->fd = esd_play_stream_fallback(esdformat, sink->frequency, sink->host, connname);
379 if ( sink->fd < 0 ) {
380 GST_DEBUG (0, "esdsink: can't open connection to esound server");
384 GST_FLAG_SET (sink, GST_ESDSINK_OPEN);
390 gst_esdsink_close_audio (GstEsdsink *sink)
392 if (sink->fd < 0) return;
397 GST_FLAG_UNSET (sink, GST_ESDSINK_OPEN);
399 GST_DEBUG (0, "esdsink: closed sound device");
402 static GstElementStateReturn
403 gst_esdsink_change_state (GstElement *element)
405 g_return_val_if_fail (GST_IS_ESDSINK (element), FALSE);
407 /* if going down into NULL state, close the fd if it's open */
408 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
409 if (GST_FLAG_IS_SET (element, GST_ESDSINK_OPEN))
410 gst_esdsink_close_audio (GST_ESDSINK (element));
411 /* otherwise (READY or higher) we need to open the fd */
413 if (!GST_FLAG_IS_SET (element, GST_ESDSINK_OPEN)) {
414 if (!gst_esdsink_open_audio (GST_ESDSINK (element)))
415 return GST_STATE_FAILURE;
419 if (GST_ELEMENT_CLASS (parent_class)->change_state)
420 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
421 return GST_STATE_SUCCESS;