various: fix pad template leaks
[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_static_pad_template (element_class, &src_factory);
172   gst_element_class_set_details_simple (element_class, "Esound audio monitor",
173       "Source/Audio",
174       "Monitors audio from an esound server",
175       "Richard Boulton <richard-gst@tartarus.org>");
176 }
177
178 static void
179 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
180 {
181   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
182   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
183
184   parent_class = g_type_class_peek_parent (g_class);
185
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));
206
207   gobject_class->set_property = gst_esdmon_set_property;
208   gobject_class->get_property = gst_esdmon_get_property;
209
210   gstelement_class->change_state = gst_esdmon_change_state;
211 }
212
213 static void
214 gst_esdmon_init (GTypeInstance * instance, gpointer g_class)
215 {
216   GstEsdmon *esdmon = GST_ESDMON (instance);
217
218   esdmon->srcpad =
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);
224
225   esdmon->fd = -1;
226   /* FIXME: get default from somewhere better than just putting them inline. */
227   esdmon->depth = 16;
228   esdmon->channels = 2;
229   esdmon->frequency = 44100;
230   esdmon->host = NULL;
231   esdmon->bytes_per_read = 4096;
232   esdmon->curoffset = 0;
233   esdmon->basetime = 0;
234   esdmon->samples_since_basetime = 0;
235 }
236
237 static gboolean
238 gst_esdmon_sync_parms (GstEsdmon * esdmon)
239 {
240   g_return_val_if_fail (esdmon != NULL, FALSE);
241   g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
242
243   if (esdmon->fd == -1)
244     return TRUE;
245
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);
249 }
250
251 static GstData *
252 gst_esdmon_get (GstPad * pad)
253 {
254   GstEsdmon *esdmon;
255   GstBuffer *buf;
256   glong readbytes;
257   glong readsamples;
258
259   g_return_val_if_fail (pad != NULL, NULL);
260   esdmon = GST_ESDMON (gst_pad_get_parent (pad));
261
262   GST_DEBUG ("attempting to read something from esdmon");
263
264   buf = gst_buffer_new ();
265   g_return_val_if_fail (buf, NULL);
266
267   GST_BUFFER_DATA (buf) = (gpointer) g_malloc (esdmon->bytes_per_read);
268
269   readbytes = read (esdmon->fd, GST_BUFFER_DATA (buf), esdmon->bytes_per_read);
270
271   if (readbytes == 0) {
272     gst_element_set_eos (GST_ELEMENT (esdmon));
273     return NULL;
274   }
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,
283         NULL);
284
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);
289       return NULL;
290     }
291     gst_caps_free (caps);
292   }
293
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;
298
299   esdmon->curoffset += readbytes;
300   readsamples = readbytes / esdmon->channels;
301   if (esdmon->depth == 16)
302     readsamples /= 2;
303   esdmon->samples_since_basetime += readsamples;
304
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);
309 }
310
311 static void
312 gst_esdmon_set_property (GObject * object, guint prop_id, const GValue * value,
313     GParamSpec * pspec)
314 {
315   GstEsdmon *esdmon;
316
317   g_return_if_fail (GST_IS_ESDMON (object));
318   esdmon = GST_ESDMON (object);
319
320   switch (prop_id) {
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. */
324       break;
325     case ARG_DEPTH:
326       esdmon->depth = g_value_get_enum (value);
327       gst_esdmon_sync_parms (esdmon);
328       break;
329     case ARG_CHANNELS:
330       esdmon->channels = g_value_get_enum (value);
331       gst_esdmon_sync_parms (esdmon);
332       break;
333     case ARG_RATE:
334       /* Preserve the timestamps */
335       esdmon->basetime =
336           esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
337       esdmon->samples_since_basetime = 0;
338
339       /* Set the new frequency */
340       esdmon->frequency = g_value_get_int (value);
341       gst_esdmon_sync_parms (esdmon);
342       break;
343     case ARG_HOST:
344       if (esdmon->host != NULL)
345         g_free (esdmon->host);
346       if (g_value_get_string (value) == NULL)
347         esdmon->host = NULL;
348       else
349         esdmon->host = g_strdup (g_value_get_string (value));
350       break;
351     default:
352       break;
353   }
354 }
355
356 static void
357 gst_esdmon_get_property (GObject * object, guint prop_id, GValue * value,
358     GParamSpec * pspec)
359 {
360   GstEsdmon *esdmon;
361
362   g_return_if_fail (GST_IS_ESDMON (object));
363   esdmon = GST_ESDMON (object);
364
365   switch (prop_id) {
366     case ARG_BYTESPERREAD:
367       g_value_set_ulong (value, esdmon->bytes_per_read);
368       break;
369     case ARG_CUROFFSET:
370       g_value_set_ulong (value, esdmon->curoffset);
371       break;
372     case ARG_DEPTH:
373       g_value_set_enum (value, esdmon->depth);
374       break;
375     case ARG_CHANNELS:
376       g_value_set_enum (value, esdmon->channels);
377       break;
378     case ARG_RATE:
379       g_value_set_int (value, esdmon->frequency);
380       break;
381     case ARG_HOST:
382       g_value_set_string (value, esdmon->host);
383       break;
384     default:
385       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386       break;
387   }
388 }
389
390 static gboolean
391 gst_esdmon_open_audio (GstEsdmon * src)
392 {
393   /* Name used by esound for this connection. */
394   const char connname[] = "GStreamer";
395
396   /* Bitmap describing audio format. */
397   esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
398
399   g_return_val_if_fail (src->fd == -1, FALSE);
400
401   if (src->depth == 16)
402     esdformat |= ESD_BITS16;
403   else if (src->depth == 8)
404     esdformat |= ESD_BITS8;
405   else {
406     GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
407     return FALSE;
408   }
409
410   if (src->channels == 2)
411     esdformat |= ESD_STEREO;
412   else if (src->channels == 1)
413     esdformat |= ESD_MONO;
414   else {
415     GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
416     return FALSE;
417   }
418
419   GST_DEBUG ("esdmon: attempting to open connection to esound server");
420   src->fd = esd_monitor_stream (esdformat, src->frequency, src->host, connname);
421   if (src->fd < 0) {
422     GST_DEBUG ("esdmon: can't open connection to esound server");
423     return FALSE;
424   }
425
426   GST_OBJECT_FLAG_SET (src, GST_ESDMON_OPEN);
427
428   return TRUE;
429 }
430
431 static void
432 gst_esdmon_close_audio (GstEsdmon * src)
433 {
434   if (src->fd < 0)
435     return;
436
437   close (src->fd);
438   src->fd = -1;
439
440   GST_OBJECT_FLAG_UNSET (src, GST_ESDMON_OPEN);
441
442   GST_DEBUG ("esdmon: closed sound device");
443 }
444
445 static GstStateChangeReturn
446 gst_esdmon_change_state (GstElement * element, GstStateChange transition)
447 {
448   g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
449
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 */
455   } else {
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;
459     }
460   }
461
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;
465 }