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