2 * Copyright (C) <2005> Arwed v. Merkatz <v.merkatz@gmx.net>
4 * Roughly based on the gstreamer 0.8 esdsink plugin:
5 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
7 * esdsink.c: an EsounD audio sink
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-esdsink
27 * @see_also: #GstAlsaSink, #GstAutoAudioSink
29 * This element outputs sound to an already-running Enlightened Sound Daemon
30 * (ESound Daemon, esd). Note that a sound daemon will never be auto-spawned
31 * through this element (regardless of the system configuration), since this
32 * is actively prevented by the element. If you must use esd, you need to
33 * make sure it is started automatically with your session or otherwise.
35 * TODO: insert some comments about how sucky esd is and that all the cool
36 * kids use pulseaudio or whatever these days.
39 * <title>Example launch line</title>
41 * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! audioresample ! esdsink
42 * ]| play an Ogg/Vorbis audio file via esd
55 #include <gst/gst-i18n-plugin.h>
57 /* wtay: from my esd.h (debian unstable libesd0-dev 0.2.36-3) */
58 #ifndef ESD_MAX_WRITE_SIZE
59 #define ESD_MAX_WRITE_SIZE (21 * 4096)
62 GST_DEBUG_CATEGORY_EXTERN (esd_debug);
63 #define GST_CAT_DEFAULT esd_debug
71 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS ("audio/x-raw-int, "
75 "endianness = (int) BYTE_ORDER, "
76 "signed = (boolean) TRUE, "
79 "rate = (int) [ 1, MAX ], "
80 "channels = (int) [ 1, 2 ]; "
82 "signed = (boolean) { true, false }, "
85 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
88 static void gst_esdsink_finalize (GObject * object);
90 static GstCaps *gst_esdsink_getcaps (GstBaseSink * bsink);
92 static gboolean gst_esdsink_open (GstAudioSink * asink);
93 static gboolean gst_esdsink_close (GstAudioSink * asink);
94 static gboolean gst_esdsink_prepare (GstAudioSink * asink,
95 GstRingBufferSpec * spec);
96 static gboolean gst_esdsink_unprepare (GstAudioSink * asink);
97 static guint gst_esdsink_write (GstAudioSink * asink, gpointer data,
99 static guint gst_esdsink_delay (GstAudioSink * asink);
100 static void gst_esdsink_reset (GstAudioSink * asink);
102 static void gst_esdsink_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_esdsink_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 GST_BOILERPLATE (GstEsdSink, gst_esdsink, GstAudioSink, GST_TYPE_AUDIO_SINK);
110 gst_esdsink_base_init (gpointer g_class)
112 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
114 gst_element_class_add_static_pad_template (element_class, &sink_factory);
115 gst_element_class_set_details_simple (element_class, "Esound audio sink",
117 "Plays audio to an esound server",
118 "Arwed von Merkatz <v.merkatz@gmx.net>");
122 gst_esdsink_class_init (GstEsdSinkClass * klass)
124 GObjectClass *gobject_class;
125 GstBaseSinkClass *gstbasesink_class;
126 GstAudioSinkClass *gstaudiosink_class;
128 gobject_class = (GObjectClass *) klass;
129 gstbasesink_class = (GstBaseSinkClass *) klass;
130 gstaudiosink_class = (GstAudioSinkClass *) klass;
132 parent_class = g_type_class_peek_parent (klass);
134 gobject_class->finalize = gst_esdsink_finalize;
136 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_esdsink_getcaps);
138 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_esdsink_open);
139 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_esdsink_close);
140 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_esdsink_prepare);
141 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_esdsink_unprepare);
142 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_esdsink_write);
143 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_esdsink_delay);
144 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_esdsink_reset);
146 gobject_class->set_property = gst_esdsink_set_property;
147 gobject_class->get_property = gst_esdsink_get_property;
149 /* default value is filled in the _init method */
150 g_object_class_install_property (gobject_class, PROP_HOST,
151 g_param_spec_string ("host", "Host",
152 "The host running the esound daemon", NULL,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 gst_esdsink_init (GstEsdSink * esdsink, GstEsdSinkClass * klass)
160 esdsink->ctrl_fd = -1;
161 esdsink->host = g_strdup (g_getenv ("ESPEAKER"));
165 gst_esdsink_finalize (GObject * object)
167 GstEsdSink *esdsink = GST_ESDSINK (object);
169 gst_caps_replace (&esdsink->cur_caps, NULL);
170 g_free (esdsink->host);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
176 gst_esdsink_getcaps (GstBaseSink * bsink)
180 esdsink = GST_ESDSINK (bsink);
182 /* no fd, we're done with the template caps */
183 if (esdsink->ctrl_fd < 0 || esdsink->cur_caps == NULL) {
184 GST_LOG_OBJECT (esdsink, "getcaps called, returning template caps");
188 GST_LOG_OBJECT (esdsink, "returning %" GST_PTR_FORMAT, esdsink->cur_caps);
190 return gst_caps_ref (esdsink->cur_caps);
194 gst_esdsink_open (GstAudioSink * asink)
196 esd_server_info_t *server_info;
197 GstPadTemplate *pad_template;
202 esdsink = GST_ESDSINK (asink);
204 GST_DEBUG_OBJECT (esdsink, "open");
206 /* ensure libesd doesn't auto-spawn a sound daemon if none is running yet */
207 saved_env = g_strdup (g_getenv ("ESD_NO_SPAWN"));
208 g_setenv ("ESD_NO_SPAWN", "1", TRUE);
210 /* now try to connect to any existing/running sound daemons */
211 esdsink->ctrl_fd = esd_open_sound (esdsink->host);
213 /* and restore the previous state */
214 if (saved_env != NULL) {
215 g_setenv ("ESD_NO_SPAWN", saved_env, TRUE);
217 g_unsetenv ("ESD_NO_SPAWN");
221 if (esdsink->ctrl_fd < 0)
222 goto couldnt_connect;
224 /* get server info */
225 server_info = esd_get_server_info (esdsink->ctrl_fd);
229 GST_INFO_OBJECT (esdsink, "got server info rate: %i", server_info->rate);
231 pad_template = gst_static_pad_template_get (&sink_factory);
232 esdsink->cur_caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
233 gst_object_unref (pad_template);
235 for (i = 0; i < esdsink->cur_caps->structs->len; i++) {
238 s = gst_caps_get_structure (esdsink->cur_caps, i);
239 gst_structure_set (s, "rate", G_TYPE_INT, server_info->rate, NULL);
242 esd_free_server_info (server_info);
244 GST_INFO_OBJECT (esdsink, "server caps: %" GST_PTR_FORMAT, esdsink->cur_caps);
251 GST_ELEMENT_ERROR (esdsink, RESOURCE, OPEN_WRITE,
252 (_("Could not establish connection to sound server")),
253 ("can't open connection to esound server"));
258 GST_ELEMENT_ERROR (esdsink, RESOURCE, OPEN_WRITE,
259 (_("Failed to query sound server capabilities")),
260 ("couldn't get server info!"));
266 gst_esdsink_close (GstAudioSink * asink)
268 GstEsdSink *esdsink = GST_ESDSINK (asink);
270 GST_DEBUG_OBJECT (esdsink, "close");
272 gst_caps_replace (&esdsink->cur_caps, NULL);
273 esd_close (esdsink->ctrl_fd);
274 esdsink->ctrl_fd = -1;
280 gst_esdsink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
282 GstEsdSink *esdsink = GST_ESDSINK (asink);
283 esd_format_t esdformat;
285 /* Name used by esound for this connection. */
286 const char connname[] = "GStreamer";
288 GST_DEBUG_OBJECT (esdsink, "prepare");
290 /* Bitmap describing audio format. */
291 esdformat = ESD_STREAM | ESD_PLAY;
293 switch (spec->depth) {
295 esdformat |= ESD_BITS8;
298 esdformat |= ESD_BITS16;
301 goto unsupported_depth;
304 switch (spec->channels) {
306 esdformat |= ESD_MONO;
309 esdformat |= ESD_STEREO;
312 goto unsupported_channels;
315 GST_INFO_OBJECT (esdsink,
316 "attempting to open data connection to esound server");
319 esd_play_stream (esdformat, spec->rate, esdsink->host, connname);
321 if ((esdsink->fd < 0) || (esdsink->ctrl_fd < 0))
324 esdsink->rate = spec->rate;
326 spec->segsize = ESD_BUF_SIZE;
327 spec->segtotal = (ESD_MAX_WRITE_SIZE / spec->segsize);
329 /* FIXME: this is wrong for signed ints (and the
330 * audioringbuffers should do it for us anyway) */
331 spec->silence_sample[0] = 0;
332 spec->silence_sample[1] = 0;
333 spec->silence_sample[2] = 0;
334 spec->silence_sample[3] = 0;
336 GST_INFO_OBJECT (esdsink, "successfully opened connection to esound server");
343 GST_ELEMENT_ERROR (esdsink, STREAM, WRONG_TYPE, (NULL),
344 ("can't handle sample depth of %d, only 8 or 16 supported",
348 unsupported_channels:
350 GST_ELEMENT_ERROR (esdsink, STREAM, WRONG_TYPE, (NULL),
351 ("can't handle %d channels, only 1 or 2 supported", spec->channels));
356 GST_ELEMENT_ERROR (esdsink, RESOURCE, OPEN_WRITE,
357 (_("Could not establish connection to sound server")),
358 ("can't open connection to esound server"));
364 gst_esdsink_unprepare (GstAudioSink * asink)
366 GstEsdSink *esdsink = GST_ESDSINK (asink);
368 if ((esdsink->fd < 0) && (esdsink->ctrl_fd < 0))
374 GST_INFO_OBJECT (esdsink, "closed sound device");
381 gst_esdsink_write (GstAudioSink * asink, gpointer data, guint length)
383 GstEsdSink *esdsink = GST_ESDSINK (asink);
388 while (to_write > 0) {
391 done = write (esdsink->fd, data, to_write);
397 data = (char *) data + done;
404 GST_ELEMENT_ERROR (esdsink, RESOURCE, WRITE,
405 ("Failed to write data to the esound daemon"), GST_ERROR_SYSTEM);
411 gst_esdsink_delay (GstAudioSink * asink)
413 GstEsdSink *esdsink = GST_ESDSINK (asink);
416 latency = esd_get_latency (esdsink->ctrl_fd);
418 if (latency == (guint) - 1) {
419 GST_WARNING_OBJECT (asink, "couldn't get latency");
423 /* latency is measured in samples at a rate of 44100, this
424 * cannot overflow. */
425 latency = latency * G_GINT64_CONSTANT (44100) / esdsink->rate;
427 GST_DEBUG_OBJECT (asink, "got latency: %u", latency);
433 gst_esdsink_reset (GstAudioSink * asink)
435 GST_DEBUG_OBJECT (asink, "reset called");
439 gst_esdsink_set_property (GObject * object, guint prop_id, const GValue * value,
442 GstEsdSink *esdsink = GST_ESDSINK (object);
446 g_free (esdsink->host);
447 esdsink->host = g_value_dup_string (value);
455 gst_esdsink_get_property (GObject * object, guint prop_id, GValue * value,
458 GstEsdSink *esdsink = GST_ESDSINK (object);
462 g_value_set_string (value, esdsink->host);
465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);