2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wim.taymans@chello.be>
5 * gstosxaudioelement.c:
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.
27 #include <CoreAudio/CoreAudio.h>
34 #include "gstosxaudioelement.h"
42 /* elementfactory information */
43 static GstElementDetails gst_osxaudioelement_details =
44 GST_ELEMENT_DETAILS ("Audio Mixer (OSX)",
46 "Mac OS X audio mixer element",
47 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
49 static void gst_osxaudioelement_base_init (GstOsxAudioElementClass * klass);
50 static void gst_osxaudioelement_class_init (GstOsxAudioElementClass * klass);
52 static void gst_osxaudioelement_init (GstOsxAudioElement * osxaudio);
53 static void gst_osxaudioelement_dispose (GObject * object);
55 static void gst_osxaudioelement_set_property (GObject * object,
56 guint prop_id, const GValue * value, GParamSpec * pspec);
57 static void gst_osxaudioelement_get_property (GObject * object,
58 guint prop_id, GValue * value, GParamSpec * pspec);
59 static GstStateChangeReturn gst_osxaudioelement_change_state (GstElement *
62 static GstElementClass *parent_class = NULL;
66 gst_osxaudioelement_get_type (void)
68 static GType osxaudioelement_type = 0;
70 if (!osxaudioelement_type) {
71 static const GTypeInfo osxaudioelement_info = {
72 sizeof (GstOsxAudioElementClass),
73 (GBaseInitFunc) gst_osxaudioelement_base_init,
75 (GClassInitFunc) gst_osxaudioelement_class_init,
78 sizeof (GstOsxAudioElement),
80 (GInstanceInitFunc) gst_osxaudioelement_init
83 osxaudioelement_type = g_type_register_static (GST_TYPE_ELEMENT,
84 "GstOsxAudioElement", &osxaudioelement_info, 0);
87 return osxaudioelement_type;
91 gst_osxaudioelement_base_init (GstOsxAudioElementClass * klass)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
95 klass->device_combinations = NULL;
97 gst_element_class_set_details (element_class, &gst_osxaudioelement_details);
101 gst_osxaudioelement_class_init (GstOsxAudioElementClass * klass)
103 GObjectClass *gobject_class;
104 GstElementClass *gstelement_class;
106 gobject_class = (GObjectClass *) klass;
107 gstelement_class = (GstElementClass *) klass;
109 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
111 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
112 g_param_spec_int ("device", "Device index",
113 "Mac OS X CoreAudio Device Index (0 usually)", 0, G_MAXINT, 0,
115 gobject_class->set_property = gst_osxaudioelement_set_property;
116 gobject_class->get_property = gst_osxaudioelement_get_property;
117 gobject_class->dispose = gst_osxaudioelement_dispose;
119 gstelement_class->change_state = gst_osxaudioelement_change_state;
123 gst_osxaudioelement_init (GstOsxAudioElement * osxaudio)
128 pthread_mutex_init (&osxaudio->buffer_mutex, NULL);
129 pthread_mutex_unlock (&osxaudio->buffer_mutex);
130 propertySize = sizeof (osxaudio->device_id);
132 AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
133 &propertySize, &(osxaudio->device_id));
136 GST_DEBUG ("AudioHardwareGetProperty returned %d\n", (int) status);
138 if (osxaudio->device_id == kAudioDeviceUnknown) {
139 GST_DEBUG ("AudioHardwareGetProperty: device_id is kAudioDeviceUnknown\n");
141 /* get requested buffer length */
142 propertySize = sizeof (osxaudio->buffer_len);
144 AudioDeviceGetProperty (osxaudio->device_id, 0, false,
145 kAudioDevicePropertyBufferSize, &propertySize, &osxaudio->buffer_len);
148 ("AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n",
151 GST_DEBUG ("%5d osxaudio->buffer_len\n", (int) osxaudio->buffer_len);
156 gst_osxaudioelement_dispose (GObject * object)
158 /* GstOsxAudioElement *osxaudio = (GstOsxAudioElement *) object; */
160 G_OBJECT_CLASS (parent_class)->dispose (object);
163 /* General purpose Ring-buffering routines */
165 write_buffer (GstOsxAudioElement * osxaudio, unsigned char *data, int len)
171 if (osxaudio->full_buffers == NUM_BUFS) {
172 GST_DEBUG ("Buffer overrun\n");
176 x = osxaudio->buffer_len - osxaudio->buf_write_pos;
179 memcpy (osxaudio->buffer[osxaudio->buf_write] + osxaudio->buf_write_pos,
182 /* accessing common variables, locking mutex */
183 pthread_mutex_lock (&osxaudio->buffer_mutex);
186 osxaudio->buffered_bytes += x;
187 osxaudio->buf_write_pos += x;
188 if (osxaudio->buf_write_pos >= osxaudio->buffer_len) {
189 /* block is full, find next! */
190 osxaudio->buf_write = (osxaudio->buf_write + 1) % NUM_BUFS;
191 ++osxaudio->full_buffers;
192 osxaudio->buf_write_pos = 0;
194 pthread_mutex_unlock (&osxaudio->buffer_mutex);
201 read_buffer (GstOsxAudioElement * osxaudio, unsigned char *data)
204 int len = osxaudio->buffer_len;
208 if (osxaudio->full_buffers == 0) {
209 GST_DEBUG ("Buffer underrun\n");
213 x = osxaudio->buffer_len - osxaudio->buf_read_pos;
217 osxaudio->buffer[osxaudio->buf_read] + osxaudio->buf_read_pos, x);
221 /* accessing common variables, locking mutex */
222 pthread_mutex_lock (&osxaudio->buffer_mutex);
223 osxaudio->buffered_bytes -= x;
224 osxaudio->buf_read_pos += x;
225 if (osxaudio->buf_read_pos >= osxaudio->buffer_len) {
226 /* block is empty, find next! */
227 osxaudio->buf_read = (osxaudio->buf_read + 1) % NUM_BUFS;
228 --osxaudio->full_buffers;
229 osxaudio->buf_read_pos = 0;
231 pthread_mutex_unlock (&osxaudio->buffer_mutex);
238 /* The function that the CoreAudio thread calls when it has data */
240 inputAudioDeviceIOProc (AudioDeviceID inDevice, const AudioTimeStamp * inNow,
241 const AudioBufferList * inInputData, const AudioTimeStamp * inInputTime,
242 AudioBufferList * outOutputData, const AudioTimeStamp * inOutputTime,
245 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (inClientData);
247 write_buffer (GST_OSXAUDIOELEMENT (osxaudio),
248 (char *) inInputData->mBuffers[0].mData, osxaudio->buffer_len);
253 /* The function that the CoreAudio thread calls when it wants more data */
256 outputAudioDeviceIOProc (AudioDeviceID inDevice, const AudioTimeStamp * inNow,
257 const AudioBufferList * inInputData, const AudioTimeStamp * inInputTime,
258 AudioBufferList * outOutputData, const AudioTimeStamp * inOutputTime,
261 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (inClientData);
263 outOutputData->mBuffers[0].mDataByteSize =
264 read_buffer (osxaudio, (char *) outOutputData->mBuffers[0].mData);
270 gst_osxaudioelement_open_audio (GstOsxAudioElement * osxaudio, gboolean input)
275 GST_INFO ("osxaudioelement: attempting to open sound device");
277 /* Allocate ring-buffer memory */
278 for (i = 0; i < NUM_BUFS; i++)
279 osxaudio->buffer[i] = (unsigned char *) malloc (osxaudio->buffer_len);
282 AudioDeviceAddIOProc (osxaudio->device_id, inputAudioDeviceIOProc,
285 /* Set the IO proc that CoreAudio will call when it needs data */
287 AudioDeviceAddIOProc (osxaudio->device_id, outputAudioDeviceIOProc,
291 GST_DEBUG ("AudioDeviceAddIOProc returned %d\n", (int) status);
295 pthread_mutex_lock (&osxaudio->buffer_mutex);
297 /* reset ring-buffer state */
298 osxaudio->buf_read = 0;
299 osxaudio->buf_write = 0;
300 osxaudio->buf_read_pos = 0;
301 osxaudio->buf_write_pos = 0;
303 osxaudio->full_buffers = 0;
304 osxaudio->buffered_bytes = 0;
307 for (i = 0; i < NUM_BUFS; i++)
308 bzero (osxaudio->buffer[i], osxaudio->buffer_len);
310 pthread_mutex_unlock (&osxaudio->buffer_mutex);
316 gst_osxaudioelement_close_audio (GstOsxAudioElement * osxaudio, gboolean input)
323 status = AudioDeviceStop (osxaudio->device_id, inputAudioDeviceIOProc);
325 status = AudioDeviceStop (osxaudio->device_id, outputAudioDeviceIOProc);
328 GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
332 AudioDeviceRemoveIOProc (osxaudio->device_id, inputAudioDeviceIOProc);
335 AudioDeviceRemoveIOProc (osxaudio->device_id, outputAudioDeviceIOProc);
338 GST_DEBUG ("AudioDeviceRemoveIOProc " "returned %d\n", (int) status);
340 for (i = 0; i < NUM_BUFS; i++)
341 free (osxaudio->buffer[i]);
346 gst_osxaudioelement_set_property (GObject * object,
347 guint prop_id, const GValue * value, GParamSpec * pspec)
349 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
354 AudioDeviceID *devids;
358 /* check index given is in bounds, if not use default device */
359 status = AudioHardwareGetPropertyInfo (kAudioHardwarePropertyDevices,
360 &propertySize, NULL);
361 nDevices = propertySize / sizeof (AudioDeviceID);
362 deviceid = g_value_get_int (value);
363 if (deviceid < nDevices) {
364 devids = malloc (propertySize);
366 AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
367 &propertySize, devids);
368 osxaudio->device_id = devids[deviceid];
371 GST_DEBUG ("device index %d out of range. Max index is currently %d\n",
381 gst_osxaudioelement_get_property (GObject * object,
382 guint prop_id, GValue * value, GParamSpec * pspec)
384 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
388 AudioDeviceID *devids;
393 /* figure out what index the current one is */
394 status = AudioHardwareGetPropertyInfo (kAudioHardwarePropertyDevices,
395 &propertySize, NULL);
396 nDevices = propertySize / sizeof (AudioDeviceID);
397 devids = malloc (propertySize);
399 AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
400 &propertySize, devids);
401 for (i = 0; i < nDevices; i++) {
402 if (osxaudio->device_id == devids[i])
405 g_value_set_int (value, i);
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
414 static GstStateChangeReturn
415 gst_osxaudioelement_change_state (GstElement * element,
416 GstStateChange transition)
418 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (element);
419 const GList *padlist;
420 gboolean input = TRUE;
422 padlist = gst_element_get_pad_list (element);
423 if (padlist != NULL) {
424 GstPad *firstpad = padlist->data;
426 if (GST_PAD_IS_SINK (firstpad)) {
430 switch (transition) {
431 case GST_STATE_CHANGE_NULL_TO_READY:
432 if (!gst_osxaudioelement_open_audio (osxaudio, input)) {
433 return GST_STATE_CHANGE_FAILURE;
435 GST_INFO ("osxaudioelement: opened sound device");
437 case GST_STATE_CHANGE_READY_TO_NULL:
438 gst_osxaudioelement_close_audio (osxaudio, input);
439 GST_INFO ("osxaudioelement: closed sound device");
445 if (GST_ELEMENT_CLASS (parent_class)->change_state)
446 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
448 return GST_STATE_CHANGE_SUCCESS;