upload tizen1.0 source
[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_pad_template (element_class,
137       gst_static_pad_template_get (&src_factory));
138
139   gst_element_class_set_details_simple (element_class, "Audio Source (OSX)",
140       "Source/Audio",
141       "Input from a sound card in OS X",
142       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
143 }
144
145 static void
146 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstElementClass *gstelement_class;
150   GstBaseSrcClass *gstbasesrc_class;
151   GstBaseAudioSrcClass *gstbaseaudiosrc_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155   gstbasesrc_class = (GstBaseSrcClass *) klass;
156   gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
157
158   parent_class = g_type_class_peek_parent (klass);
159
160   gobject_class->set_property = gst_osx_audio_src_set_property;
161   gobject_class->get_property = gst_osx_audio_src_get_property;
162
163   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
164
165   g_object_class_install_property (gobject_class, ARG_DEVICE,
166       g_param_spec_int ("device", "Device ID", "Device ID of input device",
167           0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168
169   gstbaseaudiosrc_class->create_ringbuffer =
170       GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
171 }
172
173 static void
174 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
175 {
176   gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
177
178   src->device_id = kAudioDeviceUnknown;
179   src->deviceChannels = -1;
180 }
181
182 static void
183 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
184     const GValue * value, GParamSpec * pspec)
185 {
186   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
187
188   switch (prop_id) {
189     case ARG_DEVICE:
190       src->device_id = g_value_get_int (value);
191       break;
192     default:
193       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194       break;
195   }
196 }
197
198 static void
199 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
200     GValue * value, GParamSpec * pspec)
201 {
202   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
203
204   switch (prop_id) {
205     case ARG_DEVICE:
206       g_value_set_int (value, src->device_id);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213
214 static GstCaps *
215 gst_osx_audio_src_get_caps (GstBaseSrc * src)
216 {
217   GstElementClass *gstelement_class;
218   GstOsxAudioSrc *osxsrc;
219   GstPadTemplate *pad_template;
220   GstCaps *caps;
221   gint min, max;
222
223   gstelement_class = GST_ELEMENT_GET_CLASS (src);
224   osxsrc = GST_OSX_AUDIO_SRC (src);
225
226   if (osxsrc->deviceChannels == -1) {
227     /* -1 means we don't know the number of channels yet.  for now, return
228      * template caps.
229      */
230     return NULL;
231   }
232
233   max = osxsrc->deviceChannels;
234   if (max < 1)
235     max = 1;                    /* 0 channels means 1 channel? */
236
237   min = MIN (1, max);
238
239   pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
240   g_return_val_if_fail (pad_template != NULL, NULL);
241
242   caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
243
244   if (min == max) {
245     gst_caps_set_simple (caps, "channels", G_TYPE_INT, max, NULL);
246   } else {
247     gst_caps_set_simple (caps, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
248   }
249
250   return caps;
251 }
252
253 static GstRingBuffer *
254 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
255 {
256   GstOsxAudioSrc *osxsrc;
257   GstOsxRingBuffer *ringbuffer;
258
259   osxsrc = GST_OSX_AUDIO_SRC (src);
260
261   gst_osx_audio_src_select_device (osxsrc);
262
263   GST_DEBUG ("Creating ringbuffer");
264   ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
265   GST_DEBUG ("osx src 0x%p element 0x%p  ioproc 0x%p", osxsrc,
266       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
267       (void *) gst_osx_audio_src_io_proc);
268
269   ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
270   ringbuffer->is_src = TRUE;
271   ringbuffer->device_id = osxsrc->device_id;
272
273   return GST_RING_BUFFER (ringbuffer);
274 }
275
276 static OSStatus
277 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
278     AudioUnitRenderActionFlags * ioActionFlags,
279     const AudioTimeStamp * inTimeStamp,
280     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
281 {
282   OSStatus status;
283   guint8 *writeptr;
284   gint writeseg;
285   gint len;
286   gint remaining;
287   gint offset = 0;
288
289   status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
290       inBusNumber, inNumberFrames, buf->recBufferList);
291
292   if (status) {
293     GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
294     return status;
295   }
296
297   remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
298
299   while (remaining) {
300     if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
301             &writeseg, &writeptr, &len))
302       return 0;
303
304     len -= buf->segoffset;
305
306     if (len > remaining)
307       len = remaining;
308
309     memcpy (writeptr + buf->segoffset,
310         (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
311
312     buf->segoffset += len;
313     offset += len;
314     remaining -= len;
315
316     if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
317       /* we wrote one segment */
318       gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
319
320       buf->segoffset = 0;
321     }
322   }
323   return 0;
324 }
325
326 static void
327 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
328 {
329   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
330
331   iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
332 }
333
334 static void
335 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
336 {
337   OSStatus status;
338   UInt32 propertySize;
339
340   if (osxsrc->device_id == kAudioDeviceUnknown) {
341     /* If no specific device has been selected by the user, then pick the
342      * default device */
343     GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
344     propertySize = sizeof (osxsrc->device_id);
345     status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
346         &propertySize, &osxsrc->device_id);
347
348     if (status) {
349       GST_WARNING_OBJECT (osxsrc,
350           "AudioHardwareGetProperty returned %d", (int) status);
351     } else {
352       GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
353     }
354
355     if (osxsrc->device_id == kAudioDeviceUnknown) {
356       GST_WARNING_OBJECT (osxsrc,
357           "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
358     }
359
360     GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
361         (long) osxsrc->device_id);
362   }
363 }