zaheer :
[platform/upstream/gst-plugins-good.git] / sys / osxaudio / gstosxaudioelement.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstosxaudioelement.c: 
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
27 #include <CoreAudio/CoreAudio.h>
28 #include <pthread.h>
29
30 #include <unistd.h>
31 #include <errno.h>
32 #include <string.h>
33
34 #include "gstosxaudioelement.h"
35
36 enum
37 {
38   ARG_0,
39   ARG_DEVICE
40 };
41
42 /* elementfactory information */
43 static GstElementDetails gst_osxaudioelement_details =
44 GST_ELEMENT_DETAILS ("Audio Mixer (OSX)",
45     "Generic/Audio",
46     "Mac OS X audio mixer element",
47     "Zaheer Abbas Merali <zaheerabbas at merali.org>");
48
49 static void gst_osxaudioelement_base_init (GstOsxAudioElementClass * klass);
50 static void gst_osxaudioelement_class_init (GstOsxAudioElementClass * klass);
51
52 static void gst_osxaudioelement_init (GstOsxAudioElement * osxaudio);
53 static void gst_osxaudioelement_dispose (GObject * object);
54
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 *
60     element);
61
62 static GstElementClass *parent_class = NULL;
63
64
65 GType
66 gst_osxaudioelement_get_type (void)
67 {
68   static GType osxaudioelement_type = 0;
69
70   if (!osxaudioelement_type) {
71     static const GTypeInfo osxaudioelement_info = {
72       sizeof (GstOsxAudioElementClass),
73       (GBaseInitFunc) gst_osxaudioelement_base_init,
74       NULL,
75       (GClassInitFunc) gst_osxaudioelement_class_init,
76       NULL,
77       NULL,
78       sizeof (GstOsxAudioElement),
79       0,
80       (GInstanceInitFunc) gst_osxaudioelement_init
81     };
82
83     osxaudioelement_type = g_type_register_static (GST_TYPE_ELEMENT,
84         "GstOsxAudioElement", &osxaudioelement_info, 0);
85   }
86
87   return osxaudioelement_type;
88 }
89
90 static void
91 gst_osxaudioelement_base_init (GstOsxAudioElementClass * klass)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94
95   klass->device_combinations = NULL;
96
97   gst_element_class_set_details (element_class, &gst_osxaudioelement_details);
98 }
99
100 static void
101 gst_osxaudioelement_class_init (GstOsxAudioElementClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105
106   gobject_class = (GObjectClass *) klass;
107   gstelement_class = (GstElementClass *) klass;
108
109   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
110
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,
114           G_PARAM_READWRITE));
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;
118
119   gstelement_class->change_state = gst_osxaudioelement_change_state;
120 }
121
122 static void
123 gst_osxaudioelement_init (GstOsxAudioElement * osxaudio)
124 {
125   OSStatus status;
126   UInt32 propertySize;
127
128   pthread_mutex_init (&osxaudio->buffer_mutex, NULL);
129   pthread_mutex_unlock (&osxaudio->buffer_mutex);
130   propertySize = sizeof (osxaudio->device_id);
131   status =
132       AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
133       &propertySize, &(osxaudio->device_id));
134
135   if (status) {
136     GST_DEBUG ("AudioHardwareGetProperty returned %d\n", (int) status);
137   }
138   if (osxaudio->device_id == kAudioDeviceUnknown) {
139     GST_DEBUG ("AudioHardwareGetProperty: device_id is kAudioDeviceUnknown\n");
140   }
141   /* get requested buffer length */
142   propertySize = sizeof (osxaudio->buffer_len);
143   status =
144       AudioDeviceGetProperty (osxaudio->device_id, 0, false,
145       kAudioDevicePropertyBufferSize, &propertySize, &osxaudio->buffer_len);
146   if (status) {
147     GST_DEBUG
148         ("AudioDeviceGetProperty returned %d when getting kAudioDevicePropertyBufferSize\n",
149         (int) status);
150   }
151   GST_DEBUG ("%5d osxaudio->buffer_len\n", (int) osxaudio->buffer_len);
152
153 }
154
155 static void
156 gst_osxaudioelement_dispose (GObject * object)
157 {
158   /* GstOsxAudioElement *osxaudio = (GstOsxAudioElement *) object; */
159
160   G_OBJECT_CLASS (parent_class)->dispose (object);
161 }
162
163 /* General purpose Ring-buffering routines */
164 int
165 write_buffer (GstOsxAudioElement * osxaudio, unsigned char *data, int len)
166 {
167   int len2 = 0;
168   int x;
169
170   while (len > 0) {
171     if (osxaudio->full_buffers == NUM_BUFS) {
172       GST_DEBUG ("Buffer overrun\n");
173       break;
174     }
175
176     x = osxaudio->buffer_len - osxaudio->buf_write_pos;
177     if (x > len)
178       x = len;
179     memcpy (osxaudio->buffer[osxaudio->buf_write] + osxaudio->buf_write_pos,
180         data + len2, x);
181
182     /* accessing common variables, locking mutex */
183     pthread_mutex_lock (&osxaudio->buffer_mutex);
184     len2 += x;
185     len -= x;
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;
193     }
194     pthread_mutex_unlock (&osxaudio->buffer_mutex);
195   }
196
197   return len2;
198 }
199
200 int
201 read_buffer (GstOsxAudioElement * osxaudio, unsigned char *data)
202 {
203   int len2 = 0;
204   int len = osxaudio->buffer_len;
205   int x;
206
207   while (len > 0) {
208     if (osxaudio->full_buffers == 0) {
209       GST_DEBUG ("Buffer underrun\n");
210       break;
211     }
212
213     x = osxaudio->buffer_len - osxaudio->buf_read_pos;
214     if (x > len)
215       x = len;
216     memcpy (data + len2,
217         osxaudio->buffer[osxaudio->buf_read] + osxaudio->buf_read_pos, x);
218     len2 += x;
219     len -= x;
220
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;
230     }
231     pthread_mutex_unlock (&osxaudio->buffer_mutex);
232   }
233
234
235   return len2;
236 }
237
238 /* The function that the CoreAudio thread calls when it has data */
239 OSStatus
240 inputAudioDeviceIOProc (AudioDeviceID inDevice, const AudioTimeStamp * inNow,
241     const AudioBufferList * inInputData, const AudioTimeStamp * inInputTime,
242     AudioBufferList * outOutputData, const AudioTimeStamp * inOutputTime,
243     void *inClientData)
244 {
245   GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (inClientData);
246
247   write_buffer (GST_OSXAUDIOELEMENT (osxaudio),
248       (char *) inInputData->mBuffers[0].mData, osxaudio->buffer_len);
249
250   return 0;
251 }
252
253 /* The function that the CoreAudio thread calls when it wants more data */
254
255 OSStatus
256 outputAudioDeviceIOProc (AudioDeviceID inDevice, const AudioTimeStamp * inNow,
257     const AudioBufferList * inInputData, const AudioTimeStamp * inInputTime,
258     AudioBufferList * outOutputData, const AudioTimeStamp * inOutputTime,
259     void *inClientData)
260 {
261   GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (inClientData);
262
263   outOutputData->mBuffers[0].mDataByteSize =
264       read_buffer (osxaudio, (char *) outOutputData->mBuffers[0].mData);
265
266   return 0;
267 }
268
269 static gboolean
270 gst_osxaudioelement_open_audio (GstOsxAudioElement * osxaudio, gboolean input)
271 {
272   int i;
273   OSErr status;
274
275   GST_INFO ("osxaudioelement: attempting to open sound device");
276
277   /* Allocate ring-buffer memory */
278   for (i = 0; i < NUM_BUFS; i++)
279     osxaudio->buffer[i] = (unsigned char *) malloc (osxaudio->buffer_len);
280   if (input) {
281     status =
282         AudioDeviceAddIOProc (osxaudio->device_id, inputAudioDeviceIOProc,
283         osxaudio);
284   } else {
285     /* Set the IO proc that CoreAudio will call when it needs data */
286     status =
287         AudioDeviceAddIOProc (osxaudio->device_id, outputAudioDeviceIOProc,
288         osxaudio);
289   }
290   if (status) {
291     GST_DEBUG ("AudioDeviceAddIOProc returned %d\n", (int) status);
292     return FALSE;
293   }
294
295   pthread_mutex_lock (&osxaudio->buffer_mutex);
296
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;
302
303   osxaudio->full_buffers = 0;
304   osxaudio->buffered_bytes = 0;
305
306   /* zero buffer */
307   for (i = 0; i < NUM_BUFS; i++)
308     bzero (osxaudio->buffer[i], osxaudio->buffer_len);
309
310   pthread_mutex_unlock (&osxaudio->buffer_mutex);
311
312   return TRUE;
313 }
314
315 static void
316 gst_osxaudioelement_close_audio (GstOsxAudioElement * osxaudio, gboolean input)
317 {
318   OSErr status;
319   int i;
320
321   /* stop callback */
322   if (input) {
323     status = AudioDeviceStop (osxaudio->device_id, inputAudioDeviceIOProc);
324   } else {
325     status = AudioDeviceStop (osxaudio->device_id, outputAudioDeviceIOProc);
326   }
327   if (status)
328     GST_DEBUG ("AudioDeviceStop returned %d\n", (int) status);
329
330   if (input) {
331     status =
332         AudioDeviceRemoveIOProc (osxaudio->device_id, inputAudioDeviceIOProc);
333   } else {
334     status =
335         AudioDeviceRemoveIOProc (osxaudio->device_id, outputAudioDeviceIOProc);
336   }
337   if (status)
338     GST_DEBUG ("AudioDeviceRemoveIOProc " "returned %d\n", (int) status);
339
340   for (i = 0; i < NUM_BUFS; i++)
341     free (osxaudio->buffer[i]);
342
343 }
344
345 static void
346 gst_osxaudioelement_set_property (GObject * object,
347     guint prop_id, const GValue * value, GParamSpec * pspec)
348 {
349   GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
350   OSStatus status;
351   int nDevices;
352   UInt32 propertySize;
353   int deviceid;
354   AudioDeviceID *devids;
355
356   switch (prop_id) {
357     case ARG_DEVICE:
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);
365         status =
366             AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
367             &propertySize, devids);
368         osxaudio->device_id = devids[deviceid];
369         free (devids);
370       } else {
371         GST_DEBUG ("device index %d out of range.  Max index is currently %d\n",
372             deviceid, nDevices);
373       }
374       break;
375     default:
376       break;
377   }
378 }
379
380 static void
381 gst_osxaudioelement_get_property (GObject * object,
382     guint prop_id, GValue * value, GParamSpec * pspec)
383 {
384   GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (object);
385   OSStatus status;
386   int nDevices;
387   UInt32 propertySize;
388   AudioDeviceID *devids;
389   int i;
390
391   switch (prop_id) {
392     case ARG_DEVICE:
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);
398       status =
399           AudioHardwareGetProperty (kAudioHardwarePropertyDevices,
400           &propertySize, devids);
401       for (i = 0; i < nDevices; i++) {
402         if (osxaudio->device_id == devids[i])
403           break;
404       }
405       g_value_set_int (value, i);
406       free (devids);
407       break;
408     default:
409       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410       break;
411   }
412 }
413
414 static GstElementStateReturn
415 gst_osxaudioelement_change_state (GstElement * element)
416 {
417   GstOsxAudioElement *osxaudio = GST_OSXAUDIOELEMENT (element);
418   const GList *padlist;
419   gboolean input = TRUE;
420
421   padlist = gst_element_get_pad_list (element);
422   if (padlist != NULL) {
423     GstPad *firstpad = padlist->data;
424
425     if (GST_PAD_IS_SINK (firstpad)) {
426       input = FALSE;
427     }
428   }
429   switch (GST_STATE_TRANSITION (element)) {
430     case GST_STATE_NULL_TO_READY:
431       if (!gst_osxaudioelement_open_audio (osxaudio, input)) {
432         return GST_STATE_FAILURE;
433       }
434       GST_INFO ("osxaudioelement: opened sound device");
435       break;
436     case GST_STATE_READY_TO_NULL:
437       gst_osxaudioelement_close_audio (osxaudio, input);
438       GST_INFO ("osxaudioelement: closed sound device");
439       break;
440     default:
441       break;
442   }
443
444   if (GST_ELEMENT_CLASS (parent_class)->change_state)
445     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
446
447   return GST_STATE_SUCCESS;
448 }