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_pad_template (element_class,
137 gst_static_pad_template_get (&src_factory));
139 gst_element_class_set_details_simple (element_class, "Audio Source (OSX)",
141 "Input from a sound card in OS X",
142 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
146 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
148 GObjectClass *gobject_class;
149 GstElementClass *gstelement_class;
150 GstBaseSrcClass *gstbasesrc_class;
151 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
153 gobject_class = (GObjectClass *) klass;
154 gstelement_class = (GstElementClass *) klass;
155 gstbasesrc_class = (GstBaseSrcClass *) klass;
156 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
158 parent_class = g_type_class_peek_parent (klass);
160 gobject_class->set_property = gst_osx_audio_src_set_property;
161 gobject_class->get_property = gst_osx_audio_src_get_property;
163 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
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));
169 gstbaseaudiosrc_class->create_ringbuffer =
170 GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
174 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
176 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
178 src->device_id = kAudioDeviceUnknown;
179 src->deviceChannels = -1;
183 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
184 const GValue * value, GParamSpec * pspec)
186 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
190 src->device_id = g_value_get_int (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
200 GValue * value, GParamSpec * pspec)
202 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
206 g_value_set_int (value, src->device_id);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_osx_audio_src_get_caps (GstBaseSrc * src)
217 GstElementClass *gstelement_class;
218 GstOsxAudioSrc *osxsrc;
219 GstPadTemplate *pad_template;
223 gstelement_class = GST_ELEMENT_GET_CLASS (src);
224 osxsrc = GST_OSX_AUDIO_SRC (src);
226 if (osxsrc->deviceChannels == -1) {
227 /* -1 means we don't know the number of channels yet. for now, return
233 max = osxsrc->deviceChannels;
235 max = 1; /* 0 channels means 1 channel? */
239 pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
240 g_return_val_if_fail (pad_template != NULL, NULL);
242 caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
245 gst_caps_set_simple (caps, "channels", G_TYPE_INT, max, NULL);
247 gst_caps_set_simple (caps, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
253 static GstRingBuffer *
254 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
256 GstOsxAudioSrc *osxsrc;
257 GstOsxRingBuffer *ringbuffer;
259 osxsrc = GST_OSX_AUDIO_SRC (src);
261 gst_osx_audio_src_select_device (osxsrc);
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);
269 ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
270 ringbuffer->is_src = TRUE;
271 ringbuffer->device_id = osxsrc->device_id;
273 return GST_RING_BUFFER (ringbuffer);
277 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
278 AudioUnitRenderActionFlags * ioActionFlags,
279 const AudioTimeStamp * inTimeStamp,
280 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
289 status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
290 inBusNumber, inNumberFrames, buf->recBufferList);
293 GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
297 remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
300 if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
301 &writeseg, &writeptr, &len))
304 len -= buf->segoffset;
309 memcpy (writeptr + buf->segoffset,
310 (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
312 buf->segoffset += len;
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);
327 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
329 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
331 iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
335 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
340 if (osxsrc->device_id == kAudioDeviceUnknown) {
341 /* If no specific device has been selected by the user, then pick the
343 GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
344 propertySize = sizeof (osxsrc->device_id);
345 status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
346 &propertySize, &osxsrc->device_id);
349 GST_WARNING_OBJECT (osxsrc,
350 "AudioHardwareGetProperty returned %d", (int) status);
352 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
355 if (osxsrc->device_id == kAudioDeviceUnknown) {
356 GST_WARNING_OBJECT (osxsrc,
357 "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
360 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
361 (long) osxsrc->device_id);