tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / sys / osxaudio / gstosxaudiosrc.c
1 /*
2  * GStreamer
3  * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4  * Copyright (C) 2008 Pioneers of the Inevitable <songbird@songbirdnest.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Alternatively, the contents of this file may be used under the
25  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26  * which case the following provisions apply instead of the ones
27  * mentioned above:
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
42  * Boston, MA 02111-1307, USA.
43  */
44
45 /**
46  * SECTION:element-osxaudiosrc
47  *
48  * This element captures raw audio samples using the CoreAudio api.
49  *
50  * <refsect2>
51  * <title>Example launch line</title>
52  * |[
53  * gst-launch osxaudiosrc ! wavenc ! filesink location=audio.wav
54  * ]|
55  * </refsect2>
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #  include <config.h>
60 #endif
61
62 #include <gst/gst.h>
63 #include <CoreAudio/CoreAudio.h>
64 #include <CoreAudio/AudioHardware.h>
65 #include "gstosxaudiosrc.h"
66 #include "gstosxaudioelement.h"
67
68 GST_DEBUG_CATEGORY_STATIC (osx_audiosrc_debug);
69 #define GST_CAT_DEFAULT osx_audiosrc_debug
70
71 /* Filter signals and args */
72 enum
73 {
74   /* FILL ME */
75   LAST_SIGNAL
76 };
77
78 enum
79 {
80   ARG_0,
81   ARG_DEVICE
82 };
83
84 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
85     GST_PAD_SRC,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS ("audio/x-raw-float, "
88         "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
89         "signed = (boolean) { TRUE }, "
90         "width = (int) 32, "
91         "depth = (int) 32, "
92         "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
93     );
94
95 static void gst_osx_audio_src_set_property (GObject * object, guint prop_id,
96     const GValue * value, GParamSpec * pspec);
97 static void gst_osx_audio_src_get_property (GObject * object, guint prop_id,
98     GValue * value, GParamSpec * pspec);
99
100 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src);
101
102 static GstRingBuffer *gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc *
103     src);
104 static void gst_osx_audio_src_osxelement_init (gpointer g_iface,
105     gpointer iface_data);
106 static OSStatus gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
107     AudioUnitRenderActionFlags * ioActionFlags,
108     const AudioTimeStamp * inTimeStamp, UInt32 inBusNumber,
109     UInt32 inNumberFrames, AudioBufferList * bufferList);
110 static void gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc);
111
112 static void
113 gst_osx_audio_src_do_init (GType type)
114 {
115   static const GInterfaceInfo osxelement_info = {
116     gst_osx_audio_src_osxelement_init,
117     NULL,
118     NULL
119   };
120
121   GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
122       "OSX Audio Src");
123   GST_DEBUG ("Adding static interface");
124   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
125       &osxelement_info);
126 }
127
128 GST_BOILERPLATE_FULL (GstOsxAudioSrc, gst_osx_audio_src, GstBaseAudioSrc,
129     GST_TYPE_BASE_AUDIO_SRC, gst_osx_audio_src_do_init);
130
131 static void
132 gst_osx_audio_src_base_init (gpointer g_class)
133 {
134   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
135
136   gst_element_class_add_static_pad_template (element_class, &src_factory);
137
138   gst_element_class_set_details_simple (element_class, "Audio Source (OSX)",
139       "Source/Audio",
140       "Input from a sound card in OS X",
141       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
142 }
143
144 static void
145 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
146 {
147   GObjectClass *gobject_class;
148   GstElementClass *gstelement_class;
149   GstBaseSrcClass *gstbasesrc_class;
150   GstBaseAudioSrcClass *gstbaseaudiosrc_class;
151
152   gobject_class = (GObjectClass *) klass;
153   gstelement_class = (GstElementClass *) klass;
154   gstbasesrc_class = (GstBaseSrcClass *) klass;
155   gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
156
157   parent_class = g_type_class_peek_parent (klass);
158
159   gobject_class->set_property = gst_osx_audio_src_set_property;
160   gobject_class->get_property = gst_osx_audio_src_get_property;
161
162   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
163
164   g_object_class_install_property (gobject_class, ARG_DEVICE,
165       g_param_spec_int ("device", "Device ID", "Device ID of input device",
166           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167
168   gstbaseaudiosrc_class->create_ringbuffer =
169       GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
170 }
171
172 static void
173 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
174 {
175   gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
176
177   src->device_id = kAudioDeviceUnknown;
178   src->deviceChannels = -1;
179 }
180
181 static void
182 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
183     const GValue * value, GParamSpec * pspec)
184 {
185   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
186
187   switch (prop_id) {
188     case ARG_DEVICE:
189       src->device_id = g_value_get_int (value);
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194   }
195 }
196
197 static void
198 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
199     GValue * value, GParamSpec * pspec)
200 {
201   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
202
203   switch (prop_id) {
204     case ARG_DEVICE:
205       g_value_set_int (value, src->device_id);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213 static GstCaps *
214 gst_osx_audio_src_get_caps (GstBaseSrc * src)
215 {
216   GstElementClass *gstelement_class;
217   GstOsxAudioSrc *osxsrc;
218   GstPadTemplate *pad_template;
219   GstCaps *caps;
220   gint min, max;
221
222   gstelement_class = GST_ELEMENT_GET_CLASS (src);
223   osxsrc = GST_OSX_AUDIO_SRC (src);
224
225   if (osxsrc->deviceChannels == -1) {
226     /* -1 means we don't know the number of channels yet.  for now, return
227      * template caps.
228      */
229     return NULL;
230   }
231
232   max = osxsrc->deviceChannels;
233   if (max < 1)
234     max = 1;                    /* 0 channels means 1 channel? */
235
236   min = MIN (1, max);
237
238   pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
239   g_return_val_if_fail (pad_template != NULL, NULL);
240
241   caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
242
243   if (min == max) {
244     gst_caps_set_simple (caps, "channels", G_TYPE_INT, max, NULL);
245   } else {
246     gst_caps_set_simple (caps, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
247   }
248
249   return caps;
250 }
251
252 static GstRingBuffer *
253 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
254 {
255   GstOsxAudioSrc *osxsrc;
256   GstOsxRingBuffer *ringbuffer;
257
258   osxsrc = GST_OSX_AUDIO_SRC (src);
259
260   gst_osx_audio_src_select_device (osxsrc);
261
262   GST_DEBUG ("Creating ringbuffer");
263   ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
264   GST_DEBUG ("osx src 0x%p element 0x%p  ioproc 0x%p", osxsrc,
265       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
266       (void *) gst_osx_audio_src_io_proc);
267
268   ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
269   ringbuffer->is_src = TRUE;
270   ringbuffer->device_id = osxsrc->device_id;
271
272   return GST_RING_BUFFER (ringbuffer);
273 }
274
275 static OSStatus
276 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
277     AudioUnitRenderActionFlags * ioActionFlags,
278     const AudioTimeStamp * inTimeStamp,
279     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
280 {
281   OSStatus status;
282   guint8 *writeptr;
283   gint writeseg;
284   gint len;
285   gint remaining;
286   gint offset = 0;
287
288   status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
289       inBusNumber, inNumberFrames, buf->recBufferList);
290
291   if (status) {
292     GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
293     return status;
294   }
295
296   remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
297
298   while (remaining) {
299     if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
300             &writeseg, &writeptr, &len))
301       return 0;
302
303     len -= buf->segoffset;
304
305     if (len > remaining)
306       len = remaining;
307
308     memcpy (writeptr + buf->segoffset,
309         (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
310
311     buf->segoffset += len;
312     offset += len;
313     remaining -= len;
314
315     if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
316       /* we wrote one segment */
317       gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
318
319       buf->segoffset = 0;
320     }
321   }
322   return 0;
323 }
324
325 static void
326 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
327 {
328   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
329
330   iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
331 }
332
333 static void
334 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
335 {
336   OSStatus status;
337   UInt32 propertySize;
338
339   if (osxsrc->device_id == kAudioDeviceUnknown) {
340     /* If no specific device has been selected by the user, then pick the
341      * default device */
342     GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
343     propertySize = sizeof (osxsrc->device_id);
344     status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
345         &propertySize, &osxsrc->device_id);
346
347     if (status) {
348       GST_WARNING_OBJECT (osxsrc,
349           "AudioHardwareGetProperty returned %d", (int) status);
350     } else {
351       GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
352     }
353
354     if (osxsrc->device_id == kAudioDeviceUnknown) {
355       GST_WARNING_OBJECT (osxsrc,
356           "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
357     }
358
359     GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
360         (long) osxsrc->device_id);
361   }
362 }