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., 51 Franklin St, Fifth Floor,
42 * Boston, MA 02110-1301, 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-1.0 osxaudiosrc ! wavenc ! filesink location=audio.wav
63 #include "gstosxaudiosrc.h"
64 #include "gstosxaudioelement.h"
66 GST_DEBUG_CATEGORY_STATIC (osx_audiosrc_debug);
67 #define GST_CAT_DEFAULT osx_audiosrc_debug
69 /* Filter signals and args */
82 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
85 GST_STATIC_CAPS (GST_OSX_AUDIO_SRC_CAPS)
88 static void gst_osx_audio_src_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_osx_audio_src_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
93 static GstStateChangeReturn
94 gst_osx_audio_src_change_state (GstElement * element,
95 GstStateChange transition);
97 static GstCaps *gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter);
99 static GstAudioRingBuffer *gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc
101 static void gst_osx_audio_src_osxelement_init (gpointer g_iface,
102 gpointer iface_data);
103 static OSStatus gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
104 AudioUnitRenderActionFlags * ioActionFlags,
105 const AudioTimeStamp * inTimeStamp, UInt32 inBusNumber,
106 UInt32 inNumberFrames, AudioBufferList * bufferList);
109 gst_osx_audio_src_do_init (GType type)
111 static const GInterfaceInfo osxelement_info = {
112 gst_osx_audio_src_osxelement_init,
117 GST_DEBUG_CATEGORY_INIT (osx_audiosrc_debug, "osxaudiosrc", 0,
119 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
123 #define gst_osx_audio_src_parent_class parent_class
124 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSrc, gst_osx_audio_src,
125 GST_TYPE_AUDIO_BASE_SRC, gst_osx_audio_src_do_init (g_define_type_id));
128 gst_osx_audio_src_class_init (GstOsxAudioSrcClass * klass)
130 GObjectClass *gobject_class;
131 GstElementClass *gstelement_class;
132 GstBaseSrcClass *gstbasesrc_class;
133 GstAudioBaseSrcClass *gstaudiobasesrc_class;
135 gobject_class = (GObjectClass *) klass;
136 gstelement_class = (GstElementClass *) klass;
137 gstbasesrc_class = (GstBaseSrcClass *) klass;
138 gstaudiobasesrc_class = (GstAudioBaseSrcClass *) klass;
140 gobject_class->set_property = gst_osx_audio_src_set_property;
141 gobject_class->get_property = gst_osx_audio_src_get_property;
143 gstelement_class->change_state =
144 GST_DEBUG_FUNCPTR (gst_osx_audio_src_change_state);
146 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_src_get_caps);
148 g_object_class_install_property (gobject_class, ARG_DEVICE,
149 g_param_spec_int ("device", "Device ID", "Device ID of input device",
150 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 gstaudiobasesrc_class->create_ringbuffer =
153 GST_DEBUG_FUNCPTR (gst_osx_audio_src_create_ringbuffer);
155 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
157 gst_element_class_set_static_metadata (gstelement_class, "Audio Source (OSX)",
159 "Input from a sound card in OS X",
160 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
164 gst_osx_audio_src_init (GstOsxAudioSrc * src)
166 gst_base_src_set_live (GST_BASE_SRC (src), TRUE);
168 src->device_id = kAudioDeviceUnknown;
172 gst_osx_audio_src_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec)
175 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
179 src->device_id = g_value_get_int (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gst_osx_audio_src_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec)
191 GstOsxAudioSrc *src = GST_OSX_AUDIO_SRC (object);
195 g_value_set_int (value, src->device_id);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 static GstStateChangeReturn
204 gst_osx_audio_src_change_state (GstElement * element, GstStateChange transition)
206 GstOsxAudioSrc *osxsrc = GST_OSX_AUDIO_SRC (element);
207 GstOsxAudioRingBuffer *ringbuffer;
208 GstStateChangeReturn ret;
210 switch (transition) {
215 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
216 if (ret == GST_STATE_CHANGE_FAILURE)
219 switch (transition) {
220 case GST_STATE_CHANGE_NULL_TO_READY:
221 /* The device is open now, so fix our device_id if it changed */
223 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SRC (osxsrc)->ringbuffer);
224 if (ringbuffer->core_audio->device_id != osxsrc->device_id) {
225 osxsrc->device_id = ringbuffer->core_audio->device_id;
226 g_object_notify (G_OBJECT (osxsrc), "device");
239 gst_osx_audio_src_get_caps (GstBaseSrc * src, GstCaps * filter)
241 GstOsxAudioSrc *osxsrc;
242 GstAudioRingBuffer *buf;
243 GstOsxAudioRingBuffer *osxbuf;
244 GstCaps *caps, *filtered_caps;
246 osxsrc = GST_OSX_AUDIO_SRC (src);
248 GST_OBJECT_LOCK (osxsrc);
249 buf = GST_AUDIO_BASE_SRC (src)->ringbuffer;
251 gst_object_ref (buf);
252 GST_OBJECT_UNLOCK (osxsrc);
255 GST_DEBUG_OBJECT (src, "no ring buffer, using template caps");
256 return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
259 osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
261 /* protect against cached_caps going away */
262 GST_OBJECT_LOCK (buf);
264 if (osxbuf->core_audio->cached_caps_valid) {
265 GST_LOG_OBJECT (src, "Returning cached caps");
266 caps = gst_caps_ref (osxbuf->core_audio->cached_caps);
267 } else if (buf->open) {
268 GstCaps *template_caps;
270 /* Get template caps */
272 gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SRC_PAD (osxsrc));
274 /* Device is open, let's probe its caps */
275 caps = gst_core_audio_probe_caps (osxbuf->core_audio, template_caps);
276 gst_caps_replace (&osxbuf->core_audio->cached_caps, caps);
278 gst_caps_unref (template_caps);
280 GST_DEBUG_OBJECT (src, "ring buffer not open, using template caps");
281 caps = GST_BASE_SRC_CLASS (parent_class)->get_caps (src, NULL);
284 GST_OBJECT_UNLOCK (buf);
286 gst_object_unref (buf);
294 /* Take care of filtered caps */
296 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
297 gst_caps_unref (caps);
298 return filtered_caps;
301 static GstAudioRingBuffer *
302 gst_osx_audio_src_create_ringbuffer (GstAudioBaseSrc * src)
304 GstOsxAudioSrc *osxsrc;
305 GstOsxAudioRingBuffer *ringbuffer;
307 osxsrc = GST_OSX_AUDIO_SRC (src);
309 GST_DEBUG_OBJECT (osxsrc, "Creating ringbuffer");
310 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
311 GST_DEBUG_OBJECT (osxsrc, "osx src 0x%p element 0x%p ioproc 0x%p", osxsrc,
312 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc),
313 (void *) gst_osx_audio_src_io_proc);
315 ringbuffer->core_audio->element =
316 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsrc);
317 ringbuffer->core_audio->is_src = TRUE;
319 /* By default the coreaudio instance created by the ringbuffer
320 * has device_id==kAudioDeviceUnknown. The user might have
321 * selected a different one here
323 if (ringbuffer->core_audio->device_id != osxsrc->device_id)
324 ringbuffer->core_audio->device_id = osxsrc->device_id;
326 return GST_AUDIO_RING_BUFFER (ringbuffer);
330 gst_osx_audio_src_io_proc (GstOsxAudioRingBuffer * buf,
331 AudioUnitRenderActionFlags * ioActionFlags,
332 const AudioTimeStamp * inTimeStamp,
333 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
343 /* Previous invoke of AudioUnitRender changed mDataByteSize into
344 * number of bytes actually read. Reset the members. */
345 for (n = 0; n < buf->core_audio->recBufferList->mNumberBuffers; ++n) {
346 buf->core_audio->recBufferList->mBuffers[n].mDataByteSize =
347 buf->core_audio->recBufferSize;
350 status = AudioUnitRender (buf->core_audio->audiounit, ioActionFlags,
351 inTimeStamp, inBusNumber, inNumberFrames, buf->core_audio->recBufferList);
354 GST_WARNING_OBJECT (buf, "AudioUnitRender returned %d", (int) status);
358 /* TODO: To support non-interleaved audio, go over all mBuffers,
359 * not just the first one. */
361 remaining = buf->core_audio->recBufferList->mBuffers[0].mDataByteSize;
364 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
365 &writeseg, &writeptr, &len))
368 len -= buf->segoffset;
373 memcpy (writeptr + buf->segoffset,
374 (char *) buf->core_audio->recBufferList->mBuffers[0].mData + offset,
377 buf->segoffset += len;
381 if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
382 /* we wrote one segment */
383 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
392 gst_osx_audio_src_osxelement_init (gpointer g_iface, gpointer iface_data)
394 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
396 iface->io_proc = (AURenderCallback) gst_osx_audio_src_io_proc;