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.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 GstElementStateReturn 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)
276 GST_INFO ("osxaudioelement: attempting to open sound device");
278 /* Allocate ring-buffer memory */
279 for (i = 0; i < NUM_BUFS; i++)
280 osxaudio->buffer[i] = (unsigned char *) malloc (osxaudio->buffer_len);
283 AudioDeviceAddIOProc (osxaudio->device_id, inputAudioDeviceIOProc,
286 /* Set the IO proc that CoreAudio will call when it needs data */
288 AudioDeviceAddIOProc (osxaudio->device_id, outputAudioDeviceIOProc,
292 GST_DEBUG ("AudioDeviceAddIOProc returned %d\n", (int) status);
296 pthread_mutex_lock (&osxaudio->buffer_mutex);
298 /* reset ring-buffer state */
299 osxaudio->buf_read = 0;
300 osxaudio->buf_write = 0;
301 osxaudio->buf_read_pos = 0;
302 osxaudio->buf_write_pos = 0;
304 osxaudio->full_buffers = 0;
305 osxaudio->buffered_bytes = 0;
308 for (i = 0; i < NUM_BUFS; i++)
309 bzero (osxaudio->buffer[i], osxaudio->buffer_len);
311 pthread_mutex_unlock (&osxaudio->buffer_mutex);
317 gst_osxaudioelement_close_audio (GstOsxAudioElement * osxaudio, gboolean input)
324 status = AudioDeviceStop (osxaudio->device_id, inputAudioDeviceIOProc);
326 status = AudioDeviceStop (osxaudio->device_id, outputAudioDeviceIOProc);
329 GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
333 AudioDeviceRemoveIOProc (osxaudio->device_id, inputAudioDeviceIOProc);
336 AudioDeviceRemoveIOProc (osxaudio->device_id, outputAudioDeviceIOProc);
339 GST_DEBUG ("AudioDeviceRemoveIOProc " "returned %d\n", (int) status);
341 for (i = 0; i < NUM_BUFS; i++)
342 free (osxaudio->buffer[i]);
347 gst_osxaudioelement_set_property (GObject * object,
348 guint prop_id, const GValue * value, GParamSpec * pspec)
350 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
355 AudioDeviceID *devids;
359 /* check index given is in bounds, if not use default device */
360 status = AudioHardwareGetPropertyInfo (kAudioHardwarePropertyDevices,
361 &propertySize, NULL);
362 nDevices = propertySize / sizeof (AudioDeviceID);
363 deviceid = g_value_get_int (value);
364 if (deviceid < nDevices) {
365 devids = malloc (propertySize);
367 AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
368 &propertySize, devids);
369 osxaudio->device_id = devids[deviceid];
372 GST_DEBUG ("device index %d out of range. Max index is currently %d\n",
382 gst_osxaudioelement_get_property (GObject * object,
383 guint prop_id, GValue * value, GParamSpec * pspec)
385 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
390 AudioDeviceID *devids;
395 /* figure out what index the current one is */
396 status = AudioHardwareGetPropertyInfo (kAudioHardwarePropertyDevices,
397 &propertySize, NULL);
398 nDevices = propertySize / sizeof (AudioDeviceID);
399 devids = malloc (propertySize);
401 AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
402 &propertySize, devids);
403 for (i = 0; i < nDevices; i++) {
404 if (osxaudio->device_id == devids[i])
407 g_value_set_int (value, i);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 static GstElementStateReturn
417 gst_osxaudioelement_change_state (GstElement * element)
419 GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (element);
420 const GList *padlist;
421 gboolean input = TRUE;
423 padlist = gst_element_get_pad_list (element);
424 if (padlist != NULL) {
425 GstPad *firstpad = padlist->data;
427 if (GST_PAD_IS_SINK (firstpad)) {
431 switch (GST_STATE_TRANSITION (element)) {
432 case GST_STATE_NULL_TO_READY:
433 if (!gst_osxaudioelement_open_audio (osxaudio, input)) {
434 return GST_STATE_FAILURE;
436 GST_INFO ("osxaudioelement: opened sound device");
438 case GST_STATE_READY_TO_NULL:
439 gst_osxaudioelement_close_audio (osxaudio, input);
440 GST_INFO ("osxaudioelement: closed sound device");
446 if (GST_ELEMENT_CLASS (parent_class)->change_state)
447 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
449 return GST_STATE_SUCCESS;