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