3 * Copyright (C) 2005,2006 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4 * Copyright (C) 2007,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.
44 * The development of this code was made possible due to the involvement of
45 * Pioneers of the Inevitable, the creators of the Songbird Music player
50 * SECTION:element-osxaudiosink
52 * This element renders raw audio samples using the CoreAudio api.
55 * <title>Example pipelines</title>
57 * gst-launch filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! osxaudiosink
58 * ]| Play an Ogg/Vorbis file.
61 * Last reviewed on 2006-03-01 (0.10.4)
69 #include <CoreAudio/CoreAudio.h>
70 #include <CoreAudio/AudioHardware.h>
71 #include "gstosxaudiosink.h"
72 #include "gstosxaudioelement.h"
74 GST_DEBUG_CATEGORY_STATIC (osx_audiosink_debug);
75 #define GST_CAT_DEFAULT osx_audiosink_debug
77 /* Filter signals and args */
91 #define DEFAULT_VOLUME 1.0
93 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_STATIC_CAPS ("audio/x-raw-float, "
97 "endianness = (int) {" G_STRINGIFY (G_BYTE_ORDER) " }, "
98 "signed = (boolean) { TRUE }, "
101 "rate = (int) [1, MAX], " "channels = (int) [1, MAX]")
104 static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static GstRingBuffer *gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink *
111 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
112 gpointer iface_data);
113 static void gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink);
114 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
116 static OSStatus gst_osx_audio_sink_io_proc (GstOsxRingBuffer * buf,
117 AudioUnitRenderActionFlags * ioActionFlags,
118 const AudioTimeStamp * inTimeStamp,
119 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
122 gst_osx_audio_sink_do_init (GType type)
124 static const GInterfaceInfo osxelement_info = {
125 gst_osx_audio_sink_osxelement_init,
130 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
132 GST_DEBUG ("Adding static interface");
133 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
137 GST_BOILERPLATE_FULL (GstOsxAudioSink, gst_osx_audio_sink, GstBaseAudioSink,
138 GST_TYPE_BASE_AUDIO_SINK, gst_osx_audio_sink_do_init);
141 gst_osx_audio_sink_base_init (gpointer g_class)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145 gst_element_class_add_static_pad_template (element_class, &sink_factory);
147 gst_element_class_set_details_simple (element_class, "Audio Sink (OSX)",
149 "Output to a sound card in OS X",
150 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
154 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
156 GObjectClass *gobject_class;
157 GstElementClass *gstelement_class;
158 GstBaseSinkClass *gstbasesink_class;
159 GstBaseAudioSinkClass *gstbaseaudiosink_class;
161 gobject_class = (GObjectClass *) klass;
162 gstelement_class = (GstElementClass *) klass;
163 gstbasesink_class = (GstBaseSinkClass *) klass;
164 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
166 parent_class = g_type_class_peek_parent (klass);
168 gobject_class->set_property = gst_osx_audio_sink_set_property;
169 gobject_class->get_property = gst_osx_audio_sink_get_property;
171 g_object_class_install_property (gobject_class, ARG_DEVICE,
172 g_param_spec_int ("device", "Device ID", "Device ID of output device",
173 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, ARG_VOLUME,
176 g_param_spec_double ("volume", "Volume", "Volume of this stream",
177 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 gstbaseaudiosink_class->create_ringbuffer =
180 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
184 gst_osx_audio_sink_init (GstOsxAudioSink * sink, GstOsxAudioSinkClass * gclass)
186 GST_DEBUG ("Initialising object");
188 sink->device_id = kAudioDeviceUnknown;
189 sink->volume = DEFAULT_VOLUME;
193 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
194 const GValue * value, GParamSpec * pspec)
196 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
200 sink->device_id = g_value_get_int (value);
203 sink->volume = g_value_get_double (value);
204 gst_osx_audio_sink_set_volume (sink);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
214 GValue * value, GParamSpec * pspec)
216 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
219 g_value_set_int (value, sink->device_id);
222 g_value_set_double (value, sink->volume);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 static GstRingBuffer *
231 gst_osx_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
233 GstOsxAudioSink *osxsink;
234 GstOsxRingBuffer *ringbuffer;
236 osxsink = GST_OSX_AUDIO_SINK (sink);
238 gst_osx_audio_sink_select_device (osxsink);
240 GST_DEBUG ("Creating ringbuffer");
241 ringbuffer = g_object_new (GST_TYPE_OSX_RING_BUFFER, NULL);
242 GST_DEBUG ("osx sink 0x%p element 0x%p ioproc 0x%p", osxsink,
243 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
244 (void *) gst_osx_audio_sink_io_proc);
246 gst_osx_audio_sink_set_volume (osxsink);
248 ringbuffer->element = GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
249 ringbuffer->device_id = osxsink->device_id;
251 return GST_RING_BUFFER (ringbuffer);
254 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks of data,
255 * not of a fixed size. So, we keep track of where in the current ringbuffer
256 * segment we are, and only advance the segment once we've read the whole
259 gst_osx_audio_sink_io_proc (GstOsxRingBuffer * buf,
260 AudioUnitRenderActionFlags * ioActionFlags,
261 const AudioTimeStamp * inTimeStamp,
262 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
267 gint remaining = bufferList->mBuffers[0].mDataByteSize;
271 if (!gst_ring_buffer_prepare_read (GST_RING_BUFFER (buf),
272 &readseg, &readptr, &len))
275 len -= buf->segoffset;
280 memcpy ((char *) bufferList->mBuffers[0].mData + offset,
281 readptr + buf->segoffset, len);
283 buf->segoffset += len;
287 if ((gint) buf->segoffset == GST_RING_BUFFER (buf)->spec.segsize) {
288 /* clear written samples */
289 gst_ring_buffer_clear (GST_RING_BUFFER (buf), readseg);
291 /* we wrote one segment */
292 gst_ring_buffer_advance (GST_RING_BUFFER (buf), 1);
301 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
303 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
305 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
309 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
311 if (!sink->audiounit)
314 AudioUnitSetParameter (sink->audiounit, kHALOutputParam_Volume,
315 kAudioUnitScope_Global, 0, (float) sink->volume, 0);
319 gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink)
324 if (osxsink->device_id == kAudioDeviceUnknown) {
325 /* If no specific device has been selected by the user, then pick the
327 GST_DEBUG_OBJECT (osxsink, "Selecting device for OSXAudioSink");
328 propertySize = sizeof (osxsink->device_id);
330 AudioHardwareGetProperty (kAudioHardwarePropertyDefaultOutputDevice,
331 &propertySize, &osxsink->device_id);
334 GST_WARNING_OBJECT (osxsink,
335 "AudioHardwareGetProperty returned %d", (int) status);
337 GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty returned 0");
340 if (osxsink->device_id == kAudioDeviceUnknown) {
341 GST_WARNING_OBJECT (osxsink,
342 "AudioHardwareGetProperty: device_id is kAudioDeviceUnknown");
345 GST_DEBUG_OBJECT (osxsink, "AudioHardwareGetProperty: device_id is %lu",
346 (long) osxsink->device_id);