Update and add documentation for platform specific plugins (sys).
[platform/upstream/gst-plugins-good.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 static GstElementDetails gst_osx_audio_src_details =
72 GST_ELEMENT_DETAILS ("Audio Source (OSX)",
73     "Source/Audio",
74     "Input from a sound card in OS X",
75     "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
76
77 /* Filter signals and args */
78 enum
79 {
80   /* FILL ME */
81   LAST_SIGNAL
82 };
83
84 enum
85 {
86   ARG_0,
87   ARG_DEVICE
88 };
89
90 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
91     GST_PAD_SRC,
92     GST_PAD_ALWAYS,
93     GST_STATIC_CAPS ("audio/x-raw-float, "
94         "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
95         "signed = (boolean) { TRUE }, "
96         "width = (int) 32, "
97         "depth = (int) 32, "
98         "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
99     );
100
101 static void gst_osx_audio_src_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_osx_audio_src_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105
106 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src);
107
108 static GstRingBuffer *gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc *
109     src);
110 static void gst_osx_audio_src_osxelement_init (gpointer g_iface,
111     gpointer iface_data);
112 static OSStatus gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
113     AudioUnitRenderActionFlags * ioActionFlags,
114     const AudioTimeStamp * inTimeStamp, UInt32 inBusNumber,
115     UInt32 inNumberFrames, AudioBufferList * bufferList);
116 static void gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc);
117
118 static void
119 gst_osx_audio_src_do_init (GType type)
120 {
121   static const GInterfaceInfo osxelement_info = {
122     gst_osx_audio_src_osxelement_init,
123     NULL,
124     NULL
125   };
126
127   GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
128       "OSX Audio Src");
129   GST_DEBUG ("Adding static interface");
130   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
131       &osxelement_info);
132 }
133
134 GST_BOILERPLATE_FULL (GstOsxAudioSrc, gst_osx_audio_src, GstBaseAudioSrc,
135     GST_TYPE_BASE_AUDIO_SRC, gst_osx_audio_src_do_init);
136
137 static void
138 gst_osx_audio_src_base_init (gpointer g_class)
139 {
140   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
141
142   gst_element_class_add_pad_template (element_class,
143       gst_static_pad_template_get (&src_factory));
144
145   gst_element_class_set_details (element_class, &gst_osx_audio_src_details);
146 }
147
148 static void
149 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
150 {
151   GObjectClass *gobject_class;
152   GstElementClass *gstelement_class;
153   GstBaseSrcClass *gstbasesrc_class;
154   GstBaseAudioSrcClass *gstbaseaudiosrc_class;
155
156   gobject_class = (GObjectClass *) klass;
157   gstelement_class = (GstElementClass *) klass;
158   gstbasesrc_class = (GstBaseSrcClass *) klass;
159   gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
160
161   parent_class = g_type_class_peek_parent (klass);
162
163   gobject_class->set_property =
164       GST_DEBUG_FUNCPTR (gst_osx_audio_src_set_property);
165   gobject_class->get_property =
166       GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_property);
167
168   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
169
170   g_object_class_install_property (gobject_class, ARG_DEVICE,
171       g_param_spec_int ("device", "Device ID", "Device ID of input device",
172           0, G_MAXINT, 0, G_PARAM_READWRITE));
173
174   gstbaseaudiosrc_class->create_ringbuffer =
175       GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
176 }
177
178 static void
179 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
180 {
181   gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
182
183   src->device_id = kAudioDeviceUnknown;
184   src->deviceChannels = -1;
185 }
186
187 static void
188 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
189     const GValue * value, GParamSpec * pspec)
190 {
191   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
192
193   switch (prop_id) {
194     case ARG_DEVICE:
195       src->device_id = g_value_get_int (value);
196       break;
197     default:
198       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199       break;
200   }
201 }
202
203 static void
204 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
205     GValue * value, GParamSpec * pspec)
206 {
207   GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
208
209   switch (prop_id) {
210     case ARG_DEVICE:
211       g_value_set_int (value, src->device_id);
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216   }
217 }
218
219 static GstCaps *
220 gst_osx_audio_src_get_caps (GstBaseSrc * src)
221 {
222   GstElementClass *gstelement_class;
223   GstOsxAudioSrc *osxsrc;
224   GstPadTemplate *pad_template;
225   GstCaps *caps;
226   GstStructure *structure;
227   gint min, max;
228
229   gstelement_class = GST_ELEMENT_GET_CLASS (src);
230   osxsrc = GST_OSX_AUDIO_SRC (src);
231
232   if (osxsrc->deviceChannels == -1) {
233     /* -1 means we don't know the number of channels yet.  for now, return
234      * template caps.
235      */
236     return NULL;
237   }
238
239   max = osxsrc->deviceChannels;
240   if (max < 1)
241     max = 1;                    /* 0 channels means 1 channel? */
242
243   min = MIN (1, max);
244
245   pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
246   g_return_val_if_fail (pad_template != NULL, NULL);
247
248   caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
249
250   structure = gst_caps_get_structure (caps, 0);
251   gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
252
253   return caps;
254 }
255
256 static GstRingBuffer *
257 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
258 {
259   GstOsxAudioSrc *osxsrc;
260   GstOsxRingBuffer *ringbuffer;
261
262   osxsrc = GST_OSX_AUDIO_SRC (src);
263
264   gst_osx_audio_src_select_device (osxsrc);
265
266   GST_DEBUG ("Creating ringbuffer");
267   ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
268   GST_DEBUG ("osx src 0x%p element 0x%p  ioproc 0x%p", osxsrc,
269       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
270       (void *) gst_osx_audio_src_io_proc);
271
272   ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
273   ringbuffer->is_src = TRUE;
274   ringbuffer->device_id = osxsrc->device_id;
275
276   return GST_RING_BUFFER (ringbuffer);
277 }
278
279 static OSStatus
280 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
281     AudioUnitRenderActionFlags * ioActionFlags,
282     const AudioTimeStamp * inTimeStamp,
283     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
284 {
285   OSStatus status;
286   guint8 *writeptr;
287   gint writeseg;
288   gint len;
289   gint remaining;
290   gint offset = 0;
291
292   status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
293       inBusNumber, inNumberFrames, buf->recBufferList);
294
295   if (status) {
296     GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
297     return status;
298   }
299
300   remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
301
302   while (remaining) {
303     if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
304             &writeseg, &writeptr, &len))
305       return 0;
306
307     len -= buf->segoffset;
308
309     if (len > remaining)
310       len = remaining;
311
312     memcpy (writeptr + buf->segoffset,
313         (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
314
315     buf->segoffset += len;
316     offset += len;
317     remaining -= len;
318
319     if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
320       /* we wrote one segment */
321       gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
322
323       buf->segoffset = 0;
324     }
325   }
326   return 0;
327 }
328
329 static void
330 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
331 {
332   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
333
334   iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
335 }
336
337 static void
338 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
339 {
340   OSStatus status;
341   UInt32 propertySize;
342
343   if (osxsrc->device_id == kAudioDeviceUnknown) {
344     /* If no specific device has been selected by the user, then pick the
345      * default device */
346     GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
347     propertySize = sizeof (osxsrc->device_id);
348     status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
349         &propertySize, &osxsrc->device_id);
350
351     if (status) {
352       GST_WARNING_OBJECT (osxsrc,
353           "AudioHardwareGetProperty returned %d", (int) status);
354     } else {
355       GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
356     }
357
358     if (osxsrc->device_id == kAudioDeviceUnknown) {
359       GST_WARNING_OBJECT (osxsrc,
360           "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
361     }
362
363     GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
364         (long) osxsrc->device_id);
365   }
366 }