ext, gst: canonicalise property names where this wasn't the case
[platform/upstream/gstreamer.git] / ext / esd / esdmon.c
1 /* GStreamer
2  * Copyright (C) <2001,2002> Richard Boulton <richard-gst@tartarus.org>
3  *
4  * Based on example.c:
5  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:element-esdmon
24  * @see_also: #GstAlsaSrc, #GstAutoAudioSrc
25  *
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.
31  *
32  * TODO: insert some comments about how sucky esd is and that all the cool
33  * kids use pulseaudio or whatever these days.
34  * 
35  * <refsect2>
36  * <title>Example launch line</title>
37  * |[
38  * gst-launch esdmon ! audioconvert ! waveenc ! filesink location=record.wav
39  * ]| Record from audioinput
40  * </refsect2>
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include "esdmon.h"
47 #include <esd.h>
48 #include <unistd.h>
49
50 /* Signals and args */
51 enum
52 {
53   /* FILL ME */
54   LAST_SIGNAL
55 };
56
57 enum
58 {
59   ARG_0,
60   ARG_DEPTH,
61   ARG_BYTESPERREAD,
62   ARG_CUROFFSET,
63   ARG_CHANNELS,
64   ARG_RATE,
65   ARG_HOST
66 };
67
68 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
69     GST_PAD_SRC,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("audio/x-raw-int, "
72         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
73         "signed = (boolean) TRUE, "
74         "width = (int) 16, "
75         "depth = (int) 16, "
76         "rate = [ 8000, 96000 ], "
77         "channels = [ 1, 2 ]; "
78         "audio/x-raw-int, "
79         "signed = (boolean) FALSE, "
80         "width = (int) 8, "
81         "depth = (int) 8, " "rate = [ 8000, 96000 ], " "channels = [ 1, 2 ]")
82     );
83
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);
87
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);
93
94 static GstData *gst_esdmon_get (GstPad * pad);
95
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);
100
101 #define GST_TYPE_ESDMON_DEPTHS (gst_esdmon_depths_get_type())
102 static GType
103 gst_esdmon_depths_get_type (void)
104 {
105   static GType esdmon_depths_type = 0;
106   static const GEnumValue esdmon_depths[] = {
107     {8, "8 Bits", "8"},
108     {16, "16 Bits", "16"},
109     {0, NULL, NULL},
110   };
111
112   if (!esdmon_depths_type) {
113     esdmon_depths_type =
114         g_enum_register_static ("GstEsdmonDepths", esdmon_depths);
115   }
116   return esdmon_depths_type;
117 }
118
119 #define GST_TYPE_ESDMON_CHANNELS (gst_esdmon_channels_get_type())
120 static GType
121 gst_esdmon_channels_get_type (void)
122 {
123   static GType esdmon_channels_type = 0;
124   static const GEnumValue esdmon_channels[] = {
125     {1, "Mono", "mono"},
126     {2, "Stereo", "stereo"},
127     {0, NULL, NULL},
128   };
129
130   if (!esdmon_channels_type) {
131     esdmon_channels_type =
132         g_enum_register_static ("GstEsdmonChannels", esdmon_channels);
133   }
134   return esdmon_channels_type;
135 }
136
137
138 static GstElementClass *parent_class = NULL;
139
140 /*static guint gst_esdmon_signals[LAST_SIGNAL] = { 0 }; */
141
142 GType
143 gst_esdmon_get_type (void)
144 {
145   static GType esdmon_type = 0;
146
147   if (!esdmon_type) {
148     static const GTypeInfo esdmon_info = {
149       sizeof (GstEsdmonClass),
150       gst_esdmon_base_init,
151       NULL,
152       gst_esdmon_class_init,
153       NULL,
154       NULL,
155       sizeof (GstEsdmon),
156       0,
157       gst_esdmon_init,
158     };
159
160     esdmon_type =
161         g_type_register_static (GST_TYPE_ELEMENT, "GstEsdmon", &esdmon_info, 0);
162   }
163   return esdmon_type;
164 }
165
166 static void
167 gst_esdmon_base_init (gpointer g_class)
168 {
169   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
170
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",
174       "Source/Audio",
175       "Monitors audio from an esound server",
176       "Richard Boulton <richard-gst@tartarus.org>");
177 }
178
179 static void
180 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
181 {
182   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
183   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
184
185   parent_class = g_type_class_peek_parent (g_class);
186
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 */
193
194   gobject_class->set_property = gst_esdmon_set_property;
195   gobject_class->get_property = gst_esdmon_get_property;
196
197   gstelement_class->change_state = gst_esdmon_change_state;
198 }
199
200 static void
201 gst_esdmon_init (GTypeInstance * instance, gpointer g_class)
202 {
203   GstEsdmon *esdmon = GST_ESDMON (instance);
204
205   esdmon->srcpad =
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);
211
212   esdmon->fd = -1;
213   /* FIXME: get default from somewhere better than just putting them inline. */
214   esdmon->depth = 16;
215   esdmon->channels = 2;
216   esdmon->frequency = 44100;
217   esdmon->host = NULL;
218   esdmon->bytes_per_read = 4096;
219   esdmon->curoffset = 0;
220   esdmon->basetime = 0;
221   esdmon->samples_since_basetime = 0;
222 }
223
224 static gboolean
225 gst_esdmon_sync_parms (GstEsdmon * esdmon)
226 {
227   g_return_val_if_fail (esdmon != NULL, FALSE);
228   g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
229
230   if (esdmon->fd == -1)
231     return TRUE;
232
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);
236 }
237
238 static GstData *
239 gst_esdmon_get (GstPad * pad)
240 {
241   GstEsdmon *esdmon;
242   GstBuffer *buf;
243   glong readbytes;
244   glong readsamples;
245
246   g_return_val_if_fail (pad != NULL, NULL);
247   esdmon = GST_ESDMON (gst_pad_get_parent (pad));
248
249   GST_DEBUG ("attempting to read something from esdmon");
250
251   buf = gst_buffer_new ();
252   g_return_val_if_fail (buf, NULL);
253
254   GST_BUFFER_DATA (buf) = (gpointer) g_malloc (esdmon->bytes_per_read);
255
256   readbytes = read (esdmon->fd, GST_BUFFER_DATA (buf), esdmon->bytes_per_read);
257
258   if (readbytes == 0) {
259     gst_element_set_eos (GST_ELEMENT (esdmon));
260     return NULL;
261   }
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,
270         NULL);
271
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);
276       return NULL;
277     }
278     gst_caps_free (caps);
279   }
280
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;
285
286   esdmon->curoffset += readbytes;
287   readsamples = readbytes / esdmon->channels;
288   if (esdmon->depth == 16)
289     readsamples /= 2;
290   esdmon->samples_since_basetime += readsamples;
291
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);
296 }
297
298 static void
299 gst_esdmon_set_property (GObject * object, guint prop_id, const GValue * value,
300     GParamSpec * pspec)
301 {
302   GstEsdmon *esdmon;
303
304   g_return_if_fail (GST_IS_ESDMON (object));
305   esdmon = GST_ESDMON (object);
306
307   switch (prop_id) {
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. */
311       break;
312     case ARG_DEPTH:
313       esdmon->depth = g_value_get_enum (value);
314       gst_esdmon_sync_parms (esdmon);
315       break;
316     case ARG_CHANNELS:
317       esdmon->channels = g_value_get_enum (value);
318       gst_esdmon_sync_parms (esdmon);
319       break;
320     case ARG_RATE:
321       /* Preserve the timestamps */
322       esdmon->basetime =
323           esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
324       esdmon->samples_since_basetime = 0;
325
326       /* Set the new frequency */
327       esdmon->frequency = g_value_get_int (value);
328       gst_esdmon_sync_parms (esdmon);
329       break;
330     case ARG_HOST:
331       if (esdmon->host != NULL)
332         g_free (esdmon->host);
333       if (g_value_get_string (value) == NULL)
334         esdmon->host = NULL;
335       else
336         esdmon->host = g_strdup (g_value_get_string (value));
337       break;
338     default:
339       break;
340   }
341 }
342
343 static void
344 gst_esdmon_get_property (GObject * object, guint prop_id, GValue * value,
345     GParamSpec * pspec)
346 {
347   GstEsdmon *esdmon;
348
349   g_return_if_fail (GST_IS_ESDMON (object));
350   esdmon = GST_ESDMON (object);
351
352   switch (prop_id) {
353     case ARG_BYTESPERREAD:
354       g_value_set_ulong (value, esdmon->bytes_per_read);
355       break;
356     case ARG_CUROFFSET:
357       g_value_set_ulong (value, esdmon->curoffset);
358       break;
359     case ARG_DEPTH:
360       g_value_set_enum (value, esdmon->depth);
361       break;
362     case ARG_CHANNELS:
363       g_value_set_enum (value, esdmon->channels);
364       break;
365     case ARG_RATE:
366       g_value_set_int (value, esdmon->frequency);
367       break;
368     case ARG_HOST:
369       g_value_set_string (value, esdmon->host);
370       break;
371     default:
372       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373       break;
374   }
375 }
376
377 static gboolean
378 gst_esdmon_open_audio (GstEsdmon * src)
379 {
380   /* Name used by esound for this connection. */
381   const char connname[] = "GStreamer";
382
383   /* Bitmap describing audio format. */
384   esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
385
386   g_return_val_if_fail (src->fd == -1, FALSE);
387
388   if (src->depth == 16)
389     esdformat |= ESD_BITS16;
390   else if (src->depth == 8)
391     esdformat |= ESD_BITS8;
392   else {
393     GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
394     return FALSE;
395   }
396
397   if (src->channels == 2)
398     esdformat |= ESD_STEREO;
399   else if (src->channels == 1)
400     esdformat |= ESD_MONO;
401   else {
402     GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
403     return FALSE;
404   }
405
406   GST_DEBUG ("esdmon: attempting to open connection to esound server");
407   src->fd = esd_monitor_stream (esdformat, src->frequency, src->host, connname);
408   if (src->fd < 0) {
409     GST_DEBUG ("esdmon: can't open connection to esound server");
410     return FALSE;
411   }
412
413   GST_OBJECT_FLAG_SET (src, GST_ESDMON_OPEN);
414
415   return TRUE;
416 }
417
418 static void
419 gst_esdmon_close_audio (GstEsdmon * src)
420 {
421   if (src->fd < 0)
422     return;
423
424   close (src->fd);
425   src->fd = -1;
426
427   GST_OBJECT_FLAG_UNSET (src, GST_ESDMON_OPEN);
428
429   GST_DEBUG ("esdmon: closed sound device");
430 }
431
432 static GstStateChangeReturn
433 gst_esdmon_change_state (GstElement * element, GstStateChange transition)
434 {
435   g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
436
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 */
442   } else {
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;
446     }
447   }
448
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;
452 }