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_static_pad_template (element_class, &src_factory);
172 gst_element_class_set_details_simple (element_class, "Esound audio monitor",
174 "Monitors audio from an esound server",
175 "Richard Boulton <richard-gst@tartarus.org>");
179 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
181 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
182 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
184 parent_class = g_type_class_peek_parent (g_class);
186 /* FIXME: add long property descriptions */
187 g_object_class_install_property (gobject_class, ARG_BYTESPERREAD,
188 g_param_spec_ulong ("bytes-per-read", "bytes per read", "bytes per read",
189 0, G_MAXULONG, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190 g_object_class_install_property (gobject_class, ARG_CUROFFSET,
191 g_param_spec_ulong ("curoffset", "curoffset", "curoffset",
192 0, G_MAXULONG, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, ARG_DEPTH,
194 g_param_spec_enum ("depth", "depth", "depth", GST_TYPE_ESDMON_DEPTHS,
195 16, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class, ARG_CHANNELS,
197 g_param_spec_enum ("channels", "channels", "channels",
198 GST_TYPE_ESDMON_CHANNELS, 2,
199 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class, ARG_RATE,
201 g_param_spec_int ("frequency", "frequency", "frequency",
202 G_MININT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, ARG_HOST,
204 g_param_spec_string ("host", "host", "host", NULL,
205 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 gobject_class->set_property = gst_esdmon_set_property;
208 gobject_class->get_property = gst_esdmon_get_property;
210 gstelement_class->change_state = gst_esdmon_change_state;
214 gst_esdmon_init (GTypeInstance * instance, gpointer g_class)
216 GstEsdmon *esdmon = GST_ESDMON (instance);
219 gst_pad_new_from_template (gst_element_class_get_pad_template
220 (GST_ELEMENT_GET_CLASS (esdmon), "src"), "src");
221 gst_pad_set_get_function (esdmon->srcpad, gst_esdmon_get);
222 gst_pad_use_explicit_caps (esdmon->srcpad);
223 gst_element_add_pad (GST_ELEMENT (esdmon), esdmon->srcpad);
226 /* FIXME: get default from somewhere better than just putting them inline. */
228 esdmon->channels = 2;
229 esdmon->frequency = 44100;
231 esdmon->bytes_per_read = 4096;
232 esdmon->curoffset = 0;
233 esdmon->basetime = 0;
234 esdmon->samples_since_basetime = 0;
238 gst_esdmon_sync_parms (GstEsdmon * esdmon)
240 g_return_val_if_fail (esdmon != NULL, FALSE);
241 g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
243 if (esdmon->fd == -1)
246 /* Need to set fd to use new parameters: only way to do this is to reopen. */
247 gst_esdmon_close_audio (esdmon);
248 return gst_esdmon_open_audio (esdmon);
252 gst_esdmon_get (GstPad * pad)
259 g_return_val_if_fail (pad != NULL, NULL);
260 esdmon = GST_ESDMON (gst_pad_get_parent (pad));
262 GST_DEBUG ("attempting to read something from esdmon");
264 buf = gst_buffer_new ();
265 g_return_val_if_fail (buf, NULL);
267 GST_BUFFER_DATA (buf) = (gpointer) g_malloc (esdmon->bytes_per_read);
269 readbytes = read (esdmon->fd, GST_BUFFER_DATA (buf), esdmon->bytes_per_read);
271 if (readbytes == 0) {
272 gst_element_set_eos (GST_ELEMENT (esdmon));
275 if (!GST_PAD_CAPS (pad)) {
276 GstCaps *caps = gst_caps_new_simple ("audio/x-raw-int",
277 "endianness", G_TYPE_INT, G_BYTE_ORDER,
278 "signed", G_TYPE_BOOLEAN, esdmon->depth == 8 ? FALSE : TRUE,
279 "width", G_TYPE_INT, esdmon->depth,
280 "depth", G_TYPE_INT, esdmon->depth,
281 "rate", G_TYPE_INT, esdmon->frequency,
282 "channels", G_TYPE_INT, esdmon->channels,
285 /* set caps on src pad */
286 if (gst_pad_set_explicit_caps (esdmon->srcpad, caps) <= 0) {
287 GST_ELEMENT_ERROR (esdmon, CORE, NEGOTIATION, (NULL), (NULL));
288 gst_caps_free (caps);
291 gst_caps_free (caps);
294 GST_BUFFER_SIZE (buf) = readbytes;
295 GST_BUFFER_OFFSET (buf) = esdmon->curoffset;
296 GST_BUFFER_TIMESTAMP (buf) = esdmon->basetime +
297 esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
299 esdmon->curoffset += readbytes;
300 readsamples = readbytes / esdmon->channels;
301 if (esdmon->depth == 16)
303 esdmon->samples_since_basetime += readsamples;
305 GST_DEBUG ("pushed buffer from esdmon of %ld bytes, timestamp %"
306 G_GINT64_FORMAT, readbytes, GST_BUFFER_TIMESTAMP (buf));
307 gst_object_unref (esdmon);
308 return GST_DATA (buf);
312 gst_esdmon_set_property (GObject * object, guint prop_id, const GValue * value,
317 g_return_if_fail (GST_IS_ESDMON (object));
318 esdmon = GST_ESDMON (object);
321 case ARG_BYTESPERREAD:
322 esdmon->bytes_per_read = g_value_get_ulong (value);
323 /* No need to sync params - will just happen on next read. */
326 esdmon->depth = g_value_get_enum (value);
327 gst_esdmon_sync_parms (esdmon);
330 esdmon->channels = g_value_get_enum (value);
331 gst_esdmon_sync_parms (esdmon);
334 /* Preserve the timestamps */
336 esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
337 esdmon->samples_since_basetime = 0;
339 /* Set the new frequency */
340 esdmon->frequency = g_value_get_int (value);
341 gst_esdmon_sync_parms (esdmon);
344 if (esdmon->host != NULL)
345 g_free (esdmon->host);
346 if (g_value_get_string (value) == NULL)
349 esdmon->host = g_strdup (g_value_get_string (value));
357 gst_esdmon_get_property (GObject * object, guint prop_id, GValue * value,
362 g_return_if_fail (GST_IS_ESDMON (object));
363 esdmon = GST_ESDMON (object);
366 case ARG_BYTESPERREAD:
367 g_value_set_ulong (value, esdmon->bytes_per_read);
370 g_value_set_ulong (value, esdmon->curoffset);
373 g_value_set_enum (value, esdmon->depth);
376 g_value_set_enum (value, esdmon->channels);
379 g_value_set_int (value, esdmon->frequency);
382 g_value_set_string (value, esdmon->host);
385 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
391 gst_esdmon_open_audio (GstEsdmon * src)
393 /* Name used by esound for this connection. */
394 const char connname[] = "GStreamer";
396 /* Bitmap describing audio format. */
397 esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
399 g_return_val_if_fail (src->fd == -1, FALSE);
401 if (src->depth == 16)
402 esdformat |= ESD_BITS16;
403 else if (src->depth == 8)
404 esdformat |= ESD_BITS8;
406 GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
410 if (src->channels == 2)
411 esdformat |= ESD_STEREO;
412 else if (src->channels == 1)
413 esdformat |= ESD_MONO;
415 GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
419 GST_DEBUG ("esdmon: attempting to open connection to esound server");
420 src->fd = esd_monitor_stream (esdformat, src->frequency, src->host, connname);
422 GST_DEBUG ("esdmon: can't open connection to esound server");
426 GST_OBJECT_FLAG_SET (src, GST_ESDMON_OPEN);
432 gst_esdmon_close_audio (GstEsdmon * src)
440 GST_OBJECT_FLAG_UNSET (src, GST_ESDMON_OPEN);
442 GST_DEBUG ("esdmon: closed sound device");
445 static GstStateChangeReturn
446 gst_esdmon_change_state (GstElement * element, GstStateChange transition)
448 g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
450 /* if going down into NULL state, close the fd if it's open */
451 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
452 if (GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN))
453 gst_esdmon_close_audio (GST_ESDMON (element));
454 /* otherwise (READY or higher) we need to open the fd */
456 if (!GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN)) {
457 if (!gst_esdmon_open_audio (GST_ESDMON (element)))
458 return GST_STATE_CHANGE_FAILURE;
462 if (GST_ELEMENT_CLASS (parent_class)->change_state)
463 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
464 return GST_STATE_CHANGE_SUCCESS;