ext/raw1394/: Implement GstPropertyProbe interface and add "device-name" property...
[platform/upstream/gst-plugins-good.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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "esdmon.h"
27 #include <esd.h>
28 #include <unistd.h>
29
30
31 /* elementfactory information */
32 static const GstElementDetails esdmon_details =
33 GST_ELEMENT_DETAILS ("Esound audio monitor",
34     "Source/Audio",
35     "Monitors audio from an esound server",
36     "Richard Boulton <richard-gst@tartarus.org>");
37
38 /* Signals and args */
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0,
48   ARG_DEPTH,
49   ARG_BYTESPERREAD,
50   ARG_CUROFFSET,
51   ARG_CHANNELS,
52   ARG_RATE,
53   ARG_HOST
54 };
55
56 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("audio/x-raw-int, "
60         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
61         "signed = (boolean) TRUE, "
62         "width = (int) 16, "
63         "depth = (int) 16, "
64         "rate = [ 8000, 96000 ], "
65         "channels = [ 1, 2 ]; "
66         "audio/x-raw-int, "
67         "signed = (boolean) FALSE, "
68         "width = (int) 8, "
69         "depth = (int) 8, " "rate = [ 8000, 96000 ], " "channels = [ 1, 2 ]")
70     );
71
72 static void gst_esdmon_base_init (gpointer g_class);
73 static void gst_esdmon_class_init (gpointer g_class, gpointer class_data);
74 static void gst_esdmon_init (GTypeInstance * instance, gpointer g_class);
75
76 static gboolean gst_esdmon_open_audio (GstEsdmon * src);
77 static void gst_esdmon_close_audio (GstEsdmon * src);
78 static GstStateChangeReturn gst_esdmon_change_state (GstElement * element,
79     GstStateChange transition);
80 static gboolean gst_esdmon_sync_parms (GstEsdmon * esdmon);
81
82 static GstData *gst_esdmon_get (GstPad * pad);
83
84 static void gst_esdmon_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86 static void gst_esdmon_get_property (GObject * object, guint prop_id,
87     GValue * value, GParamSpec * pspec);
88
89 #define GST_TYPE_ESDMON_DEPTHS (gst_esdmon_depths_get_type())
90 static GType
91 gst_esdmon_depths_get_type (void)
92 {
93   static GType esdmon_depths_type = 0;
94   static const GEnumValue esdmon_depths[] = {
95     {8, "8 Bits", "8"},
96     {16, "16 Bits", "16"},
97     {0, NULL, NULL},
98   };
99
100   if (!esdmon_depths_type) {
101     esdmon_depths_type =
102         g_enum_register_static ("GstEsdmonDepths", esdmon_depths);
103   }
104   return esdmon_depths_type;
105 }
106
107 #define GST_TYPE_ESDMON_CHANNELS (gst_esdmon_channels_get_type())
108 static GType
109 gst_esdmon_channels_get_type (void)
110 {
111   static GType esdmon_channels_type = 0;
112   static const GEnumValue esdmon_channels[] = {
113     {1, "Mono", "mono"},
114     {2, "Stereo", "stereo"},
115     {0, NULL, NULL},
116   };
117
118   if (!esdmon_channels_type) {
119     esdmon_channels_type =
120         g_enum_register_static ("GstEsdmonChannels", esdmon_channels);
121   }
122   return esdmon_channels_type;
123 }
124
125
126 static GstElementClass *parent_class = NULL;
127
128 /*static guint gst_esdmon_signals[LAST_SIGNAL] = { 0 }; */
129
130 GType
131 gst_esdmon_get_type (void)
132 {
133   static GType esdmon_type = 0;
134
135   if (!esdmon_type) {
136     static const GTypeInfo esdmon_info = {
137       sizeof (GstEsdmonClass),
138       gst_esdmon_base_init,
139       NULL,
140       gst_esdmon_class_init,
141       NULL,
142       NULL,
143       sizeof (GstEsdmon),
144       0,
145       gst_esdmon_init,
146     };
147
148     esdmon_type =
149         g_type_register_static (GST_TYPE_ELEMENT, "GstEsdmon", &esdmon_info, 0);
150   }
151   return esdmon_type;
152 }
153
154 static void
155 gst_esdmon_base_init (gpointer g_class)
156 {
157   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
158
159   gst_element_class_add_pad_template (element_class,
160       gst_static_pad_template_get (&src_factory));
161   gst_element_class_set_details (element_class, &esdmon_details);
162 }
163
164 static void
165 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
166 {
167   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
168   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
169
170   parent_class = g_type_class_peek_parent (g_class);
171
172   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 */
173   g_object_class_install_property (gobject_class, ARG_CUROFFSET, g_param_spec_ulong ("curoffset", "curoffset", "curoffset", 0, G_MAXULONG, 0, G_PARAM_READABLE));       /* CHECKME */
174   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! */
175   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! */
176   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 */
177   g_object_class_install_property (gobject_class, ARG_HOST, g_param_spec_string ("host", "host", "host", NULL, G_PARAM_READWRITE));     /* CHECKME */
178
179   gobject_class->set_property = gst_esdmon_set_property;
180   gobject_class->get_property = gst_esdmon_get_property;
181
182   gstelement_class->change_state = gst_esdmon_change_state;
183 }
184
185 static void
186 gst_esdmon_init (GTypeInstance * instance, gpointer g_class)
187 {
188   GstEsdmon *esdmon = GST_ESDMON (instance);
189
190   esdmon->srcpad =
191       gst_pad_new_from_template (gst_element_class_get_pad_template
192       (GST_ELEMENT_GET_CLASS (esdmon), "src"), "src");
193   gst_pad_set_get_function (esdmon->srcpad, gst_esdmon_get);
194   gst_pad_use_explicit_caps (esdmon->srcpad);
195   gst_element_add_pad (GST_ELEMENT (esdmon), esdmon->srcpad);
196
197   esdmon->fd = -1;
198   /* FIXME: get default from somewhere better than just putting them inline. */
199   esdmon->depth = 16;
200   esdmon->channels = 2;
201   esdmon->frequency = 44100;
202   esdmon->host = NULL;
203   esdmon->bytes_per_read = 4096;
204   esdmon->curoffset = 0;
205   esdmon->basetime = 0;
206   esdmon->samples_since_basetime = 0;
207 }
208
209 static gboolean
210 gst_esdmon_sync_parms (GstEsdmon * esdmon)
211 {
212   g_return_val_if_fail (esdmon != NULL, FALSE);
213   g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
214
215   if (esdmon->fd == -1)
216     return TRUE;
217
218   /* Need to set fd to use new parameters: only way to do this is to reopen. */
219   gst_esdmon_close_audio (esdmon);
220   return gst_esdmon_open_audio (esdmon);
221 }
222
223 static GstData *
224 gst_esdmon_get (GstPad * pad)
225 {
226   GstEsdmon *esdmon;
227   GstBuffer *buf;
228   glong readbytes;
229   glong readsamples;
230
231   g_return_val_if_fail (pad != NULL, NULL);
232   esdmon = GST_ESDMON (gst_pad_get_parent (pad));
233
234   GST_DEBUG ("attempting to read something from esdmon");
235
236   buf = gst_buffer_new ();
237   g_return_val_if_fail (buf, NULL);
238
239   GST_BUFFER_DATA (buf) = (gpointer) g_malloc (esdmon->bytes_per_read);
240
241   readbytes = read (esdmon->fd, GST_BUFFER_DATA (buf), esdmon->bytes_per_read);
242
243   if (readbytes == 0) {
244     gst_element_set_eos (GST_ELEMENT (esdmon));
245     return NULL;
246   }
247   if (!GST_PAD_CAPS (pad)) {
248     GstCaps *caps = gst_caps_new_simple ("audio/x-raw-int",
249         "endianness", G_TYPE_INT, G_BYTE_ORDER,
250         "signed", G_TYPE_BOOLEAN, esdmon->depth == 8 ? FALSE : TRUE,
251         "width", G_TYPE_INT, esdmon->depth,
252         "depth", G_TYPE_INT, esdmon->depth,
253         "rate", G_TYPE_INT, esdmon->frequency,
254         "channels", G_TYPE_INT, esdmon->channels,
255         NULL);
256
257     /* set caps on src pad */
258     if (gst_pad_set_explicit_caps (esdmon->srcpad, caps) <= 0) {
259       GST_ELEMENT_ERROR (esdmon, CORE, NEGOTIATION, (NULL), (NULL));
260       gst_caps_free (caps);
261       return NULL;
262     }
263     gst_caps_free (caps);
264   }
265
266   GST_BUFFER_SIZE (buf) = readbytes;
267   GST_BUFFER_OFFSET (buf) = esdmon->curoffset;
268   GST_BUFFER_TIMESTAMP (buf) = esdmon->basetime +
269       esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
270
271   esdmon->curoffset += readbytes;
272   readsamples = readbytes / esdmon->channels;
273   if (esdmon->depth == 16)
274     readsamples /= 2;
275   esdmon->samples_since_basetime += readsamples;
276
277   GST_DEBUG ("pushed buffer from esdmon of %ld bytes, timestamp %"
278       G_GINT64_FORMAT, readbytes, GST_BUFFER_TIMESTAMP (buf));
279   gst_object_unref (esdmon);
280   return GST_DATA (buf);
281 }
282
283 static void
284 gst_esdmon_set_property (GObject * object, guint prop_id, const GValue * value,
285     GParamSpec * pspec)
286 {
287   GstEsdmon *esdmon;
288
289   g_return_if_fail (GST_IS_ESDMON (object));
290   esdmon = GST_ESDMON (object);
291
292   switch (prop_id) {
293     case ARG_BYTESPERREAD:
294       esdmon->bytes_per_read = g_value_get_ulong (value);
295       /* No need to sync params - will just happen on next read. */
296       break;
297     case ARG_DEPTH:
298       esdmon->depth = g_value_get_enum (value);
299       gst_esdmon_sync_parms (esdmon);
300       break;
301     case ARG_CHANNELS:
302       esdmon->channels = g_value_get_enum (value);
303       gst_esdmon_sync_parms (esdmon);
304       break;
305     case ARG_RATE:
306       /* Preserve the timestamps */
307       esdmon->basetime =
308           esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
309       esdmon->samples_since_basetime = 0;
310
311       /* Set the new frequency */
312       esdmon->frequency = g_value_get_int (value);
313       gst_esdmon_sync_parms (esdmon);
314       break;
315     case ARG_HOST:
316       if (esdmon->host != NULL)
317         g_free (esdmon->host);
318       if (g_value_get_string (value) == NULL)
319         esdmon->host = NULL;
320       else
321         esdmon->host = g_strdup (g_value_get_string (value));
322       break;
323     default:
324       break;
325   }
326 }
327
328 static void
329 gst_esdmon_get_property (GObject * object, guint prop_id, GValue * value,
330     GParamSpec * pspec)
331 {
332   GstEsdmon *esdmon;
333
334   g_return_if_fail (GST_IS_ESDMON (object));
335   esdmon = GST_ESDMON (object);
336
337   switch (prop_id) {
338     case ARG_BYTESPERREAD:
339       g_value_set_ulong (value, esdmon->bytes_per_read);
340       break;
341     case ARG_CUROFFSET:
342       g_value_set_ulong (value, esdmon->curoffset);
343       break;
344     case ARG_DEPTH:
345       g_value_set_enum (value, esdmon->depth);
346       break;
347     case ARG_CHANNELS:
348       g_value_set_enum (value, esdmon->channels);
349       break;
350     case ARG_RATE:
351       g_value_set_int (value, esdmon->frequency);
352       break;
353     case ARG_HOST:
354       g_value_set_string (value, esdmon->host);
355       break;
356     default:
357       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358       break;
359   }
360 }
361
362 static gboolean
363 gst_esdmon_open_audio (GstEsdmon * src)
364 {
365   /* Name used by esound for this connection. */
366   const char connname[] = "GStreamer";
367
368   /* Bitmap describing audio format. */
369   esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
370
371   g_return_val_if_fail (src->fd == -1, FALSE);
372
373   if (src->depth == 16)
374     esdformat |= ESD_BITS16;
375   else if (src->depth == 8)
376     esdformat |= ESD_BITS8;
377   else {
378     GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
379     return FALSE;
380   }
381
382   if (src->channels == 2)
383     esdformat |= ESD_STEREO;
384   else if (src->channels == 1)
385     esdformat |= ESD_MONO;
386   else {
387     GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
388     return FALSE;
389   }
390
391   GST_DEBUG ("esdmon: attempting to open connection to esound server");
392   src->fd = esd_monitor_stream (esdformat, src->frequency, src->host, connname);
393   if (src->fd < 0) {
394     GST_DEBUG ("esdmon: can't open connection to esound server");
395     return FALSE;
396   }
397
398   GST_OBJECT_FLAG_SET (src, GST_ESDMON_OPEN);
399
400   return TRUE;
401 }
402
403 static void
404 gst_esdmon_close_audio (GstEsdmon * src)
405 {
406   if (src->fd < 0)
407     return;
408
409   close (src->fd);
410   src->fd = -1;
411
412   GST_OBJECT_FLAG_UNSET (src, GST_ESDMON_OPEN);
413
414   GST_DEBUG ("esdmon: closed sound device");
415 }
416
417 static GstStateChangeReturn
418 gst_esdmon_change_state (GstElement * element, GstStateChange transition)
419 {
420   g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
421
422   /* if going down into NULL state, close the fd if it's open */
423   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
424     if (GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN))
425       gst_esdmon_close_audio (GST_ESDMON (element));
426     /* otherwise (READY or higher) we need to open the fd */
427   } else {
428     if (!GST_OBJECT_FLAG_IS_SET (element, GST_ESDMON_OPEN)) {
429       if (!gst_esdmon_open_audio (GST_ESDMON (element)))
430         return GST_STATE_CHANGE_FAILURE;
431     }
432   }
433
434   if (GST_ELEMENT_CLASS (parent_class)->change_state)
435     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
436   return GST_STATE_CHANGE_SUCCESS;
437 }