3 * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4 * Copyright (C) 2008 Pioneers of the Inevitable <songbird@songbirdnest.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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
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.
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.
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.
46 * SECTION:element-osxaudiosrc
48 * This element captures raw audio samples using the CoreAudio api.
51 * <title>Example launch line</title>
53 * gst-launch osxaudiosrc ! wavenc ! filesink location=audio.wav
63 #include <CoreAudio/CoreAudio.h>
64 #include <CoreAudio/AudioHardware.h>
65 #include "gstosxaudiosrc.h"
66 #include "gstosxaudioelement.h"
68 GST_DEBUG_CATEGORY_STATIC (osx_audiosrc_debug);
69 #define GST_CAT_DEFAULT osx_audiosrc_debug
71 /* Filter signals and args */
84 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
87 GST_STATIC_CAPS ("audio/x-raw-float, "
88 "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
89 "signed = (boolean) { TRUE }, "
92 "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
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);
100 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src);
102 static GstRingBuffer *gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc *
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);
113 gst_osx_audio_src_do_init (GType type)
115 static const GInterfaceInfo osxelement_info = {
116 gst_osx_audio_src_osxelement_init,
121 GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
123 GST_DEBUG ("Adding static interface");
124 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
128 GST_BOILERPLATE_FULL (GstOsxAudioSrc, gst_osx_audio_src, GstBaseAudioSrc,
129 GST_TYPE_BASE_AUDIO_SRC, gst_osx_audio_src_do_init);
132 gst_osx_audio_src_base_init (gpointer g_class)
134 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
136 gst_element_class_add_static_pad_template (element_class, &src_factory);
138 gst_element_class_set_details_simple (element_class, "Audio Source (OSX)",
140 "Input from a sound card in OS X",
141 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
145 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
149 GstBaseSrcClass *gstbasesrc_class;
150 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
152 gobject_class = (GObjectClass *) klass;
153 gstelement_class = (GstElementClass *) klass;
154 gstbasesrc_class = (GstBaseSrcClass *) klass;
155 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
157 parent_class = g_type_class_peek_parent (klass);
159 gobject_class->set_property = gst_osx_audio_src_set_property;
160 gobject_class->get_property = gst_osx_audio_src_get_property;
162 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
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));
168 gstbaseaudiosrc_class->create_ringbuffer =
169 GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
173 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
175 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
177 src->device_id = kAudioDeviceUnknown;
178 src->deviceChannels = -1;
182 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
183 const GValue * value, GParamSpec * pspec)
185 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
189 src->device_id = g_value_get_int (value);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
199 GValue * value, GParamSpec * pspec)
201 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
205 g_value_set_int (value, src->device_id);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gst_osx_audio_src_get_caps (GstBaseSrc * src)
216 GstElementClass *gstelement_class;
217 GstOsxAudioSrc *osxsrc;
218 GstPadTemplate *pad_template;
222 gstelement_class = GST_ELEMENT_GET_CLASS (src);
223 osxsrc = GST_OSX_AUDIO_SRC (src);
225 if (osxsrc->deviceChannels == -1) {
226 /* -1 means we don't know the number of channels yet. for now, return
232 max = osxsrc->deviceChannels;
234 max = 1; /* 0 channels means 1 channel? */
238 pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
239 g_return_val_if_fail (pad_template != NULL, NULL);
241 caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
244 gst_caps_set_simple (caps, "channels", G_TYPE_INT, max, NULL);
246 gst_caps_set_simple (caps, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
252 static GstRingBuffer *
253 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
255 GstOsxAudioSrc *osxsrc;
256 GstOsxRingBuffer *ringbuffer;
258 osxsrc = GST_OSX_AUDIO_SRC (src);
260 gst_osx_audio_src_select_device (osxsrc);
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);
268 ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
269 ringbuffer->is_src = TRUE;
270 ringbuffer->device_id = osxsrc->device_id;
272 return GST_RING_BUFFER (ringbuffer);
276 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
277 AudioUnitRenderActionFlags * ioActionFlags,
278 const AudioTimeStamp * inTimeStamp,
279 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
288 status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
289 inBusNumber, inNumberFrames, buf->recBufferList);
292 GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
296 remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
299 if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
300 &writeseg, &writeptr, &len))
303 len -= buf->segoffset;
308 memcpy (writeptr + buf->segoffset,
309 (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
311 buf->segoffset += len;
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);
326 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
328 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
330 iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
334 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
339 if (osxsrc->device_id == kAudioDeviceUnknown) {
340 /* If no specific device has been selected by the user, then pick the
342 GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
343 propertySize = sizeof (osxsrc->device_id);
344 status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
345 &propertySize, &osxsrc->device_id);
348 GST_WARNING_OBJECT (osxsrc,
349 "AudioHardwareGetProperty returned %d", (int) status);
351 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
354 if (osxsrc->device_id == kAudioDeviceUnknown) {
355 GST_WARNING_OBJECT (osxsrc,
356 "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
359 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
360 (long) osxsrc->device_id);