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 static GstElementDetails gst_osx_audio_src_details =
72 GST_ELEMENT_DETAILS ("Audio Source (OSX)",
74 "Input from a sound card in OS X",
75 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
77 /* Filter signals and args */
90 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
93 GST_STATIC_CAPS ("audio/x-raw-float, "
94 "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
95 "signed = (boolean) { TRUE }, "
98 "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
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);
106 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src);
108 static GstRingBuffer *gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc *
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);
119 gst_osx_audio_src_do_init (GType type)
121 static const GInterfaceInfo osxelement_info = {
122 gst_osx_audio_src_osxelement_init,
127 GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
129 GST_DEBUG ("Adding static interface");
130 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
134 GST_BOILERPLATE_FULL (GstOsxAudioSrc, gst_osx_audio_src, GstBaseAudioSrc,
135 GST_TYPE_BASE_AUDIO_SRC, gst_osx_audio_src_do_init);
138 gst_osx_audio_src_base_init (gpointer g_class)
140 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
142 gst_element_class_add_pad_template (element_class,
143 gst_static_pad_template_get (&src_factory));
145 gst_element_class_set_details (element_class, &gst_osx_audio_src_details);
149 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
153 GstBaseSrcClass *gstbasesrc_class;
154 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
156 gobject_class = (GObjectClass *) klass;
157 gstelement_class = (GstElementClass *) klass;
158 gstbasesrc_class = (GstBaseSrcClass *) klass;
159 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
161 parent_class = g_type_class_peek_parent (klass);
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);
168 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
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));
174 gstbaseaudiosrc_class->create_ringbuffer =
175 GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
179 gst_osx_audio_src_init (GstOsxAudioSrc * src, GstOsxAudioSrcClass * gclass)
181 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
183 src->device_id = kAudioDeviceUnknown;
184 src->deviceChannels = -1;
188 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
189 const GValue * value, GParamSpec * pspec)
191 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
195 src->device_id = g_value_get_int (value);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
205 GValue * value, GParamSpec * pspec)
207 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
211 g_value_set_int (value, src->device_id);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gst_osx_audio_src_get_caps (GstBaseSrc * src)
222 GstElementClass *gstelement_class;
223 GstOsxAudioSrc *osxsrc;
224 GstPadTemplate *pad_template;
226 GstStructure *structure;
229 gstelement_class = GST_ELEMENT_GET_CLASS (src);
230 osxsrc = GST_OSX_AUDIO_SRC (src);
232 if (osxsrc->deviceChannels == -1) {
233 /* -1 means we don't know the number of channels yet. for now, return
239 max = osxsrc->deviceChannels;
241 max = 1; /* 0 channels means 1 channel? */
245 pad_template = gst_element_class_get_pad_template (gstelement_class, "src");
246 g_return_val_if_fail (pad_template != NULL, NULL);
248 caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
250 structure = gst_caps_get_structure (caps, 0);
251 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, min, max, NULL);
256 static GstRingBuffer *
257 gst_osx_audio_src_create_ringbuffer (GstBaseAudioSrc * src)
259 GstOsxAudioSrc *osxsrc;
260 GstOsxRingBuffer *ringbuffer;
262 osxsrc = GST_OSX_AUDIO_SRC (src);
264 gst_osx_audio_src_select_device (osxsrc);
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);
272 ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
273 ringbuffer->is_src = TRUE;
274 ringbuffer->device_id = osxsrc->device_id;
276 return GST_RING_BUFFER (ringbuffer);
280 gst_osx_audio_src_io_proc (GstOsxRingBuffer * buf,
281 AudioUnitRenderActionFlags * ioActionFlags,
282 const AudioTimeStamp * inTimeStamp,
283 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
292 status = AudioUnitRender (buf->audiounit, ioActionFlags, inTimeStamp,
293 inBusNumber, inNumberFrames, buf->recBufferList);
296 GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
300 remaining = buf->recBufferList->mBuffers[0].mDataByteSize;
303 if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
304 &writeseg, &writeptr, &len))
307 len -= buf->segoffset;
312 memcpy (writeptr + buf->segoffset,
313 (char *) buf->recBufferList->mBuffers[0].mData + offset, len);
315 buf->segoffset += len;
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);
330 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
332 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
334 iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;
338 gst_osx_audio_src_select_device (GstOsxAudioSrc * osxsrc)
343 if (osxsrc->device_id == kAudioDeviceUnknown) {
344 /* If no specific device has been selected by the user, then pick the
346 GST_DEBUG_OBJECT (osxsrc, "Selecting device for OSXAudioSrc");
347 propertySize = sizeof (osxsrc->device_id);
348 status = AudioHardwareGetProperty (kAudioHardwarePropertyDefaultInputDevice,
349 &propertySize, &osxsrc->device_id);
352 GST_WARNING_OBJECT (osxsrc,
353 "AudioHardwareGetProperty returned %d", (int) status);
355 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty returned 0");
358 if (osxsrc->device_id == kAudioDeviceUnknown) {
359 GST_WARNING_OBJECT (osxsrc,
360 "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
363 GST_DEBUG_OBJECT (osxsrc, "AudioHardwareGetProperty: device_id is %lu",
364 (long) osxsrc->device_id);