2 * Copyright (C) <2001,2002> Richard Boulton <richard-gst@tartarus.org>
5 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
31 /* elementfactory information */
32 static GstElementDetails esdmon_details = {
33 "Esound audio monitor",
35 "Monitors audio from an esound server",
36 "Richard Boulton <richard-gst@tartarus.org>",
39 /* Signals and args */
55 static GstStaticPadTemplate src_factory =
56 GST_STATIC_PAD_TEMPLATE (
62 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
63 "signed = (boolean) TRUE, "
66 "rate = [ 8000, 96000 ], "
67 "channels = [ 1, 2 ]; "
69 "signed = (boolean) FALSE, "
72 "rate = [ 8000, 96000 ], "
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);
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);
86 static GstData * gst_esdmon_get (GstPad *pad);
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);
93 #define GST_TYPE_ESDMON_DEPTHS (gst_esdmon_depths_get_type())
95 gst_esdmon_depths_get_type (void)
97 static GType esdmon_depths_type = 0;
98 static GEnumValue esdmon_depths[] = {
100 {16, "16", "16 Bits"},
103 if (!esdmon_depths_type) {
104 esdmon_depths_type = g_enum_register_static("GstEsdmonDepths", esdmon_depths);
106 return esdmon_depths_type;
109 #define GST_TYPE_ESDMON_CHANNELS (gst_esdmon_channels_get_type())
111 gst_esdmon_channels_get_type (void)
113 static GType esdmon_channels_type = 0;
114 static GEnumValue esdmon_channels[] = {
119 if (!esdmon_channels_type) {
120 esdmon_channels_type = g_enum_register_static("GstEsdmonChannels", esdmon_channels);
122 return esdmon_channels_type;
126 static GstElementClass *parent_class = NULL;
127 /*static guint gst_esdmon_signals[LAST_SIGNAL] = { 0 }; */
130 gst_esdmon_get_type (void)
132 static GType esdmon_type = 0;
135 static const GTypeInfo esdmon_info = {
136 sizeof(GstEsdmonClass),
137 gst_esdmon_base_init,
139 gst_esdmon_class_init,
146 esdmon_type = g_type_register_static(GST_TYPE_ELEMENT, "GstEsdmon", &esdmon_info, 0);
152 gst_esdmon_base_init (gpointer g_class)
154 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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);
162 gst_esdmon_class_init (gpointer g_class, gpointer class_data)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
165 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
167 parent_class = g_type_class_peek_parent (g_class);
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 */
188 gobject_class->set_property = gst_esdmon_set_property;
189 gobject_class->get_property = gst_esdmon_get_property;
191 gstelement_class->change_state = gst_esdmon_change_state;
195 gst_esdmon_init(GTypeInstance *instance, gpointer g_class)
197 GstEsdmon *esdmon = GST_ESDMON (instance);
199 esdmon->srcpad = gst_pad_new_from_template (
200 gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (esdmon), "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);
207 /* FIXME: get default from somewhere better than just putting them inline. */
209 esdmon->channels = 2;
210 esdmon->frequency = 44100;
212 esdmon->bytes_per_read = 4096;
213 esdmon->curoffset = 0;
214 esdmon->basetime = 0;
215 esdmon->samples_since_basetime = 0;
219 gst_esdmon_sync_parms (GstEsdmon *esdmon)
221 g_return_val_if_fail (esdmon != NULL, FALSE);
222 g_return_val_if_fail (GST_IS_ESDMON (esdmon), FALSE);
224 if (esdmon->fd == -1) return TRUE;
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);
232 gst_esdmon_get (GstPad *pad)
239 g_return_val_if_fail (pad != NULL, NULL);
240 esdmon = GST_ESDMON(gst_pad_get_parent (pad));
242 GST_DEBUG ("attempting to read something from esdmon");
244 buf = gst_buffer_new ();
245 g_return_val_if_fail (buf, NULL);
247 GST_BUFFER_DATA (buf) = (gpointer)g_malloc (esdmon->bytes_per_read);
249 readbytes = read (esdmon->fd,
250 GST_BUFFER_DATA (buf),
251 esdmon->bytes_per_read);
253 if (readbytes == 0) {
254 gst_element_set_eos (GST_ELEMENT (esdmon));
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
270 GST_ELEMENT_ERROR (esdmon, CORE, NEGOTIATION, (NULL), (NULL));
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;
280 esdmon->curoffset += readbytes;
281 readsamples = readbytes / esdmon->channels;
282 if (esdmon->depth == 16) readsamples /= 2;
283 esdmon->samples_since_basetime += readsamples;
285 GST_DEBUG ("pushed buffer from esdmon of %ld bytes, timestamp %" G_GINT64_FORMAT, readbytes, GST_BUFFER_TIMESTAMP (buf));
286 return GST_DATA (buf);
290 gst_esdmon_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
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);
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. */
304 esdmon->depth = g_value_get_enum (value);
305 gst_esdmon_sync_parms (esdmon);
308 esdmon->channels = g_value_get_enum (value);
309 gst_esdmon_sync_parms (esdmon);
312 /* Preserve the timestamps */
313 esdmon->basetime = esdmon->samples_since_basetime * GST_SECOND / esdmon->frequency;
314 esdmon->samples_since_basetime = 0;
316 /* Set the new frequency */
317 esdmon->frequency = g_value_get_int (value);
318 gst_esdmon_sync_parms (esdmon);
321 if (esdmon->host != NULL) g_free(esdmon->host);
322 if (g_value_get_string (value) == NULL)
325 esdmon->host = g_strdup (g_value_get_string (value));
333 gst_esdmon_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
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);
342 case ARG_BYTESPERREAD:
343 g_value_set_ulong (value, esdmon->bytes_per_read);
346 g_value_set_ulong (value, esdmon->curoffset);
349 g_value_set_enum (value, esdmon->depth);
352 g_value_set_enum (value, esdmon->channels);
355 g_value_set_int (value, esdmon->frequency);
358 g_value_set_string (value, esdmon->host);
361 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367 gst_esdmon_factory_init (GstPlugin *plugin)
369 if (!gst_element_register (plugin, "esdmon", GST_RANK_NONE, GST_TYPE_ESDMON))
376 gst_esdmon_open_audio (GstEsdmon *src)
378 /* Name used by esound for this connection. */
379 const char * connname = "GStreamer";
381 /* Bitmap describing audio format. */
382 esd_format_t esdformat = ESD_STREAM | ESD_PLAY;
384 g_return_val_if_fail (src->fd == -1, FALSE);
386 if (src->depth == 16) esdformat |= ESD_BITS16;
387 else if (src->depth == 8) esdformat |= ESD_BITS8;
389 GST_DEBUG ("esdmon: invalid bit depth (%d)", src->depth);
393 if (src->channels == 2) esdformat |= ESD_STEREO;
394 else if (src->channels == 1) esdformat |= ESD_MONO;
396 GST_DEBUG ("esdmon: invalid number of channels (%d)", src->channels);
400 GST_DEBUG ("esdmon: attempting to open connection to esound server");
401 src->fd = esd_monitor_stream(esdformat, src->frequency, src->host, connname);
403 GST_DEBUG ("esdmon: can't open connection to esound server");
407 GST_FLAG_SET (src, GST_ESDMON_OPEN);
413 gst_esdmon_close_audio (GstEsdmon *src)
415 if (src->fd < 0) return;
420 GST_FLAG_UNSET (src, GST_ESDMON_OPEN);
422 GST_DEBUG ("esdmon: closed sound device");
425 static GstElementStateReturn
426 gst_esdmon_change_state (GstElement *element)
428 g_return_val_if_fail (GST_IS_ESDMON (element), FALSE);
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 */
436 if (!GST_FLAG_IS_SET (element, GST_ESDMON_OPEN)) {
437 if (!gst_esdmon_open_audio (GST_ESDMON (element)))
438 return GST_STATE_FAILURE;
442 if (GST_ELEMENT_CLASS (parent_class)->change_state)
443 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
444 return GST_STATE_SUCCESS;