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