2 * Copyright (C) <2001,2002> 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.
23 * SECTION:element-esdmon
24 * @see_also: #GstAlsaSrc, #GstAutoAudioSrc
26 * This element records sound from an already-running Enlightened Sound Daemon
27 * (ESound Daemon, esd). Note that a sound daemon will never be auto-spawned
28 * through this element (regardless of the system configuration), since this
29 * is actively prevented by the element. If you must use esd, you need to
30 * make sure it is started automatically with your session or otherwise.
32 * TODO: insert some comments about how sucky esd is and that all the cool
33 * kids use pulseaudio or whatever these days.
36 * <title>Example launch line</title>
38 * gst-launch esdmon ! audioconvert ! waveenc ! filesink location=record.wav
39 * ]| Record from audioinput
50 /* Signals and args */
68 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("audio/x-raw-int, "
72 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
73 "signed = (boolean) TRUE, "
76 "rate = [ 8000, 96000 ], "
77 "channels = [ 1, 2 ]; "
79 "signed = (boolean) FALSE, "
81 "depth = (int) 8, " "rate = [ 8000, 96000 ], " "channels = [ 1, 2 ]")
84 static void gst_esdmon_base_init (gpointer g_class);
85 static void gst_esdmon_class_init (gpointer g_class, gpointer class_data);
86 static void gst_esdmon_init (GTypeInstance * instance, gpointer g_class);
88 static gboolean gst_esdmon_open_audio (GstEsdmon * src);
89 static void gst_esdmon_close_audio (GstEsdmon * src);
90 static GstStateChangeReturn gst_esdmon_change_state (GstElement * element,
91 GstStateChange transition);
92 static gboolean gst_esdmon_sync_parms (GstEsdmon * esdmon);
94 static GstData *gst_esdmon_get (GstPad * pad);
96 static void gst_esdmon_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
98 static void gst_esdmon_get_property (GObject * object, guint prop_id,
99 GValue * value, GParamSpec * pspec);
101 #define GST_TYPE_ESDMON_DEPTHS (gst_esdmon_depths_get_type())
103 gst_esdmon_depths_get_type (void)
105 static GType esdmon_depths_type = 0;
106 static const GEnumValue esdmon_depths[] = {
108 {16, "16 Bits", "16"},
112 if (!esdmon_depths_type) {
114 g_enum_register_static ("GstEsdmonDepths", esdmon_depths);
116 return esdmon_depths_type;
119 #define GST_TYPE_ESDMON_CHANNELS (gst_esdmon_channels_get_type())
121 gst_esdmon_channels_get_type (void)
123 static GType esdmon_channels_type = 0;
124 static const GEnumValue esdmon_channels[] = {
126 {2, "Stereo", "stereo"},
130 if (!esdmon_channels_type) {
131 esdmon_channels_type =
132 g_enum_register_static ("GstEsdmonChannels", esdmon_channels);
134 return esdmon_channels_type;
138 static GstElementClass *parent_class = NULL;
140 /*static guint gst_esdmon_signals[LAST_SIGNAL] = { 0 }; */
143 gst_esdmon_get_type (void)
145 static GType esdmon_type = 0;
148 static const GTypeInfo esdmon_info = {
149 sizeof (GstEsdmonClass),
150 gst_esdmon_base_init,
152 gst_esdmon_class_init,
161 g_type_register_static (GST_TYPE_ELEMENT, "GstEsdmon", &esdmon_info, 0);
167 gst_esdmon_base_init (gpointer g_class)
169 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
171 gst_element_class_add_pad_template (element_class,
172 gst_static_pad_template_get (&src_factory));
173 gst_element_class_set_details_simple (element_class, "Esound audio monitor",
175 "Monitors audio from an esound server",
176 "Richard Boulton <richard-gst@tartarus.org>");
180 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
182 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
183 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
185 parent_class = g_type_class_peek_parent (g_class);
187 g_object_class_install_property (gobject_class, ARG_BYTESPERREAD, g_param_spec_ulong ("bytes-per-read", "bytes per read", "bytes per read", 0, G_MAXULONG, 0, G_PARAM_READWRITE)); /* CHECKME */
188 g_object_class_install_property (gobject_class, ARG_CUROFFSET, g_param_spec_ulong ("curoffset", "curoffset", "curoffset", 0, G_MAXULONG, 0, G_PARAM_READABLE)); /* CHECKME */
189 g_object_class_install_property (gobject_class, ARG_DEPTH, g_param_spec_enum ("depth", "depth", "depth", GST_TYPE_ESDMON_DEPTHS, 16, G_PARAM_READWRITE)); /* CHECKME! */
190 g_object_class_install_property (gobject_class, ARG_CHANNELS, g_param_spec_enum ("channels", "channels", "channels", GST_TYPE_ESDMON_CHANNELS, 2, G_PARAM_READWRITE)); /* CHECKME! */
191 g_object_class_install_property (gobject_class, ARG_RATE, g_param_spec_int ("frequency", "frequency", "frequency", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE)); /* CHECKME */
192 g_object_class_install_property (gobject_class, ARG_HOST, g_param_spec_string ("host", "host", "host", NULL, G_PARAM_READWRITE)); /* CHECKME */
194 gobject_class->set_property = gst_esdmon_set_property;
195 gobject_class->get_property = gst_esdmon_get_property;
197 gstelement_class->change_state = gst_esdmon_change_state;
201 gst_esdmon_init (GTypeInstance * instance, gpointer g_class)
203 GstEsdmon *esdmon = GST_ESDMON (instance);
206 gst_pad_new_from_template (gst_element_class_get_pad_template
207 (GST_ELEMENT_GET_CLASS (esdmon), "src"), "src");
208 gst_pad_set_get_function (esdmon->srcpad, gst_esdmon_get);
209 gst_pad_use_explicit_caps (esdmon->srcpad);
210 gst_element_add_pad (GST_ELEMENT (esdmon), esdmon->srcpad);
213 /* FIXME: get default from somewhere better than just putting them inline. */
215 esdmon->channels = 2;
216 esdmon->frequency = 44100;
218 esdmon->bytes_per_read = 4096;
219 esdmon->curoffset = 0;
220 esdmon->basetime = 0;
221 esdmon->samples_since_basetime = 0;
225 gst_esdmon_sync_parms (GstEsdmon * esdmon)
227 g_return_val_if_fail (esdmon != NULL, FALSE);
228 g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
230 if (esdmon->fd == -1)
233 /* Need to set fd to use new parameters: only way to do this is to reopen. */
234 gst_esdmon_close_audio (esdmon);
235 return gst_esdmon_open_audio (esdmon);
239 gst_esdmon_get (GstPad * pad)
246 g_return_val_if_fail (pad != NULL, NULL);
247 esdmon = GST_ESDMON (gst_pad_get_parent (pad));
249 GST_DEBUG ("attempting to read something from esdmon");
251 buf = gst_buffer_new ();
252 g_return_val_if_fail (buf, NULL);
254 GST_BUFFER_DATA (buf) = (gpointer) g_malloc (esdmon->bytes_per_read);
256 readbytes = read (esdmon->fd, GST_BUFFER_DATA (buf), esdmon->bytes_per_read);
258 if (readbytes == 0) {
259 gst_element_set_eos (GST_ELEMENT (esdmon));
262 if (!GST_PAD_CAPS (pad)) {
263 GstCaps *caps = gst_caps_new_simple ("audio/x-raw-int",
264 "endianness", G_TYPE_INT, G_BYTE_ORDER,
265 "signed", G_TYPE_BOOLEAN, esdmon->depth == 8 ? FALSE : TRUE,
266 "width", G_TYPE_INT, esdmon->depth,
267 "depth", G_TYPE_INT, esdmon->depth,
268 "rate", G_TYPE_INT, esdmon->frequency,
269 "channels", G_TYPE_INT, esdmon->channels,
272 /* set caps on src pad */
273 if (gst_pad_set_explicit_caps (esdmon->srcpad, caps) <= 0) {
274 GST_ELEMENT_ERROR (esdmon, CORE, NEGOTIATION, (NULL), (NULL));
275 gst_caps_free (caps);
278 gst_caps_free (caps);
281 GST_BUFFER_SIZE (buf) = readbytes;
282 GST_BUFFER_OFFSET (buf) = esdmon->curoffset;
283 GST_BUFFER_TIMESTAMP (buf) = esdmon->basetime +
284 esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
286 esdmon->curoffset += readbytes;
287 readsamples = readbytes / esdmon->channels;
288 if (esdmon->depth == 16)
290 esdmon->samples_since_basetime += readsamples;
292 GST_DEBUG ("pushed buffer from esdmon of %ld bytes, timestamp %"
293 G_GINT64_FORMAT, readbytes, GST_BUFFER_TIMESTAMP (buf));
294 gst_object_unref (esdmon);
295 return GST_DATA (buf);
299 gst_esdmon_set_property (GObject * object, guint prop_id, const GValue * value,
304 g_return_if_fail (GST_IS_ESDMON (object));
305 esdmon = GST_ESDMON (object);
308 case ARG_BYTESPERREAD:
309 esdmon->bytes_per_read = g_value_get_ulong (value);
310 /* No need to sync params - will just happen on next read. */
313 esdmon->depth = g_value_get_enum (value);
314 gst_esdmon_sync_parms (esdmon);
317 esdmon->channels = g_value_get_enum (value);
318 gst_esdmon_sync_parms (esdmon);
321 /* Preserve the timestamps */
323 esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
324 esdmon->samples_since_basetime = 0;
326 /* Set the new frequency */
327 esdmon->frequency = g_value_get_int (value);
328 gst_esdmon_sync_parms (esdmon);
331 if (esdmon->host != NULL)
332 g_free (esdmon->host);
333 if (g_value_get_string (value) == NULL)
336 esdmon->host = g_strdup (g_value_get_string (value));
344 gst_esdmon_get_property (GObject * object, guint prop_id, GValue * value,
349 g_return_if_fail (GST_IS_ESDMON (object));
350 esdmon = GST_ESDMON (object);
353 case ARG_BYTESPERREAD:
354 g_value_set_ulong (value, esdmon->bytes_per_read);
357 g_value_set_ulong (value, esdmon->curoffset);
360 g_value_set_enum (value, esdmon->depth);
363 g_value_set_enum (value, esdmon->channels);
366 g_value_set_int (value, esdmon->frequency);
369 g_value_set_string (value, esdmon->host);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 gst_esdmon_open_audio (GstEsdmon * src)
380 /* Name used by esound for this connection. */
381 const char connname[] = "GStreamer";
383 /* Bitmap describing audio format. */
384 esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
386 g_return_val_if_fail (src->fd == -1, FALSE);
388 if (src->depth == 16)
389 esdformat |= ESD_BITS16;
390 else if (src->depth == 8)
391 esdformat |= ESD_BITS8;
393 GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
397 if (src->channels == 2)
398 esdformat |= ESD_STEREO;
399 else if (src->channels == 1)
400 esdformat |= ESD_MONO;
402 GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
406 GST_DEBUG ("esdmon: attempting to open connection to esound server");
407 src->fd = esd_monitor_stream (esdformat, src->frequency, src->host, connname);
409 GST_DEBUG ("esdmon: can't open connection to esound server");
413 GST_OBJECT_FLAG_SET (src, GST_ESDMON_OPEN);
419 gst_esdmon_close_audio (GstEsdmon * src)
427 GST_OBJECT_FLAG_UNSET (src, GST_ESDMON_OPEN);
429 GST_DEBUG ("esdmon: closed sound device");
432 static GstStateChangeReturn
433 gst_esdmon_change_state (GstElement * element, GstStateChange transition)
435 g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
437 /* if going down into NULL state, close the fd if it's open */
438 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
439 if (GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN))
440 gst_esdmon_close_audio (GST_ESDMON (element));
441 /* otherwise (READY or higher) we need to open the fd */
443 if (!GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN)) {
444 if (!gst_esdmon_open_audio (GST_ESDMON (element)))
445 return GST_STATE_CHANGE_FAILURE;
449 if (GST_ELEMENT_CLASS (parent_class)->change_state)
450 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
451 return GST_STATE_CHANGE_SUCCESS;