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>
5 * Copyright (C) 2012 Fluendo S.A. <support@fluendo.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43 * Boston, MA 02110-1301, USA.
45 * The development of this code was made possible due to the involvement of
46 * Pioneers of the Inevitable, the creators of the Songbird Music player
51 * SECTION:element-osxaudiosink
53 * This element renders raw audio samples using the CoreAudio api.
56 * <title>Example pipelines</title>
58 * gst-launch-1.0 filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! osxaudiosink
59 * ]| Play an Ogg/Vorbis file.
68 #include <gst/audio/audio.h>
69 #include <gst/audio/audio-channels.h>
70 #include <gst/audio/gstaudioiec61937.h>
72 #include "gstosxaudiosink.h"
73 #include "gstosxaudioelement.h"
75 GST_DEBUG_CATEGORY_STATIC (osx_audiosink_debug);
76 #define GST_CAT_DEFAULT osx_audiosink_debug
78 #include "gstosxcoreaudio.h"
80 /* Filter signals and args */
94 #define DEFAULT_VOLUME 1.0
96 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
99 GST_STATIC_CAPS (GST_OSX_AUDIO_SINK_CAPS)
102 static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static GstStateChangeReturn
108 gst_osx_audio_sink_change_state (GstElement * element,
109 GstStateChange transition);
111 static gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
113 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
115 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
118 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
120 static GstAudioRingBuffer
121 * gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink);
122 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
123 gpointer iface_data);
124 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
126 static OSStatus gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
127 AudioUnitRenderActionFlags * ioActionFlags,
128 const AudioTimeStamp * inTimeStamp,
129 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
132 gst_osx_audio_sink_do_init (GType type)
134 static const GInterfaceInfo osxelement_info = {
135 gst_osx_audio_sink_osxelement_init,
140 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
142 gst_core_audio_init_debug ();
143 GST_DEBUG ("Adding static interface");
144 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
148 #define gst_osx_audio_sink_parent_class parent_class
149 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSink, gst_osx_audio_sink,
150 GST_TYPE_AUDIO_BASE_SINK, gst_osx_audio_sink_do_init (g_define_type_id));
153 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
157 GstBaseSinkClass *gstbasesink_class;
158 GstAudioBaseSinkClass *gstaudiobasesink_class;
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
162 gstbasesink_class = (GstBaseSinkClass *) klass;
163 gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
165 parent_class = g_type_class_peek_parent (klass);
167 gobject_class->set_property = gst_osx_audio_sink_set_property;
168 gobject_class->get_property = gst_osx_audio_sink_get_property;
170 gstelement_class->change_state =
171 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_change_state);
174 g_object_class_install_property (gobject_class, ARG_DEVICE,
175 g_param_spec_int ("device", "Device ID", "Device ID of output device",
176 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
181 g_object_class_install_property (gobject_class, ARG_VOLUME,
182 g_param_spec_double ("volume", "Volume", "Volume of this stream",
183 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
185 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps);
187 gstaudiobasesink_class->create_ringbuffer =
188 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
189 gstaudiobasesink_class->payload =
190 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_sink_payload);
192 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
194 gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (OSX)",
196 "Output to a sound card in OS X",
197 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
201 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
203 GST_DEBUG ("Initialising object");
205 sink->device_id = kAudioDeviceUnknown;
206 sink->volume = DEFAULT_VOLUME;
210 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec)
213 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
218 sink->device_id = g_value_get_int (value);
222 sink->volume = g_value_get_double (value);
223 gst_osx_audio_sink_set_volume (sink);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 static GstStateChangeReturn
232 gst_osx_audio_sink_change_state (GstElement * element,
233 GstStateChange transition)
235 GstOsxAudioSink *osxsink = GST_OSX_AUDIO_SINK (element);
236 GstOsxAudioRingBuffer *ringbuffer;
237 GstStateChangeReturn ret;
239 switch (transition) {
244 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
245 if (ret == GST_STATE_CHANGE_FAILURE)
248 switch (transition) {
249 case GST_STATE_CHANGE_NULL_TO_READY:
250 /* Device has been selected, AudioUnit set up, so initialize volume */
251 gst_osx_audio_sink_set_volume (osxsink);
253 /* The device is open now, so fix our device_id if it changed */
255 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (osxsink)->ringbuffer);
256 if (ringbuffer->core_audio->device_id != osxsink->device_id) {
257 osxsink->device_id = ringbuffer->core_audio->device_id;
258 g_object_notify (G_OBJECT (osxsink), "device");
271 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
272 GValue * value, GParamSpec * pspec)
274 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
278 g_value_set_int (value, sink->device_id);
282 g_value_set_double (value, sink->volume);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
293 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
294 gboolean ret = FALSE;
296 switch (GST_QUERY_TYPE (query)) {
297 case GST_QUERY_ACCEPT_CAPS:
299 GstCaps *caps = NULL;
301 gst_query_parse_accept_caps (query, &caps);
302 ret = gst_osx_audio_sink_acceptcaps (sink, caps);
303 gst_query_set_accept_caps_result (query, ret);
308 ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
315 gst_osx_audio_sink_getcaps (GstBaseSink * sink, GstCaps * filter)
317 GstOsxAudioSink *osxsink;
318 GstAudioRingBuffer *buf;
319 GstOsxAudioRingBuffer *osxbuf;
320 GstCaps *caps, *filtered_caps;
322 osxsink = GST_OSX_AUDIO_SINK (sink);
324 GST_OBJECT_LOCK (osxsink);
325 buf = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
327 gst_object_ref (buf);
328 GST_OBJECT_UNLOCK (osxsink);
331 GST_DEBUG_OBJECT (sink, "no ring buffer, returning NULL caps");
332 return GST_BASE_SINK_CLASS (parent_class)->get_caps (sink, filter);
335 osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
337 /* protect against cached_caps going away */
338 GST_OBJECT_LOCK (buf);
340 if (osxbuf->core_audio->cached_caps_valid) {
341 GST_LOG_OBJECT (sink, "Returning cached caps");
342 caps = gst_caps_ref (osxbuf->core_audio->cached_caps);
343 } else if (buf->open) {
344 GstCaps *template_caps;
346 /* Get template caps */
348 gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (osxsink));
350 /* Device is open, let's probe its caps */
351 caps = gst_core_audio_probe_caps (osxbuf->core_audio, template_caps);
352 gst_caps_replace (&osxbuf->core_audio->cached_caps, caps);
354 gst_caps_unref (template_caps);
356 GST_DEBUG_OBJECT (sink, "ring buffer not open, returning NULL caps");
360 GST_OBJECT_UNLOCK (buf);
362 gst_object_unref (buf);
370 /* Take care of filtered caps */
372 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
373 gst_caps_unref (caps);
374 return filtered_caps;
378 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
382 gboolean ret = FALSE;
383 GstAudioRingBufferSpec spec = { 0 };
384 gchar *caps_string = NULL;
386 caps_string = gst_caps_to_string (caps);
387 GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
388 g_free (caps_string);
390 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
392 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
393 gst_caps_unref (pad_caps);
398 /* If we've not got fixed caps, creating a stream might fail,
399 * so let's just return from here with default acceptcaps
401 if (!gst_caps_is_fixed (caps))
404 /* parse helper expects this set, so avoid nasty warning
405 * will be set properly later on anyway */
406 spec.latency_time = GST_SECOND;
407 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
410 /* Make sure input is framed and can be payloaded */
412 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
414 gboolean framed = FALSE;
416 st = gst_caps_get_structure (caps, 0);
418 gst_structure_get_boolean (st, "framed", &framed);
419 if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
423 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
425 gboolean parsed = FALSE;
427 st = gst_caps_get_structure (caps, 0);
429 gst_structure_get_boolean (st, "parsed", &parsed);
430 if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
444 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
446 if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
447 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
449 GstMapInfo inmap, outmap;
455 out = gst_buffer_new_and_alloc (framesize);
457 gst_buffer_map (buf, &inmap, GST_MAP_READ);
458 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
460 /* FIXME: the endianness needs to be queried and then set */
461 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
462 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
464 gst_buffer_unmap (buf, &inmap);
465 gst_buffer_unmap (out, &outmap);
468 gst_buffer_unref (out);
472 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
476 return gst_buffer_ref (buf);
480 static GstAudioRingBuffer *
481 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
483 GstOsxAudioSink *osxsink;
484 GstOsxAudioRingBuffer *ringbuffer;
486 osxsink = GST_OSX_AUDIO_SINK (sink);
488 GST_DEBUG_OBJECT (sink, "Creating ringbuffer");
489 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
490 GST_DEBUG_OBJECT (sink, "osx sink %p element %p ioproc %p", osxsink,
491 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
492 (void *) gst_osx_audio_sink_io_proc);
494 ringbuffer->core_audio->element =
495 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
496 ringbuffer->core_audio->is_src = FALSE;
498 /* By default the coreaudio instance created by the ringbuffer
499 * has device_id==kAudioDeviceUnknown. The user might have
500 * selected a different one here
502 if (ringbuffer->core_audio->device_id != osxsink->device_id)
503 ringbuffer->core_audio->device_id = osxsink->device_id;
505 return GST_AUDIO_RING_BUFFER (ringbuffer);
508 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks
509 * of data, not of a fixed size. So, we keep track of where in
510 * the current ringbuffer segment we are, and only advance the segment
511 * once we've read the whole thing */
513 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
514 AudioUnitRenderActionFlags * ioActionFlags,
515 const AudioTimeStamp * inTimeStamp,
516 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
521 gint stream_idx = buf->core_audio->stream_idx;
522 gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
526 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
527 &readseg, &readptr, &len))
530 len -= buf->segoffset;
535 memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
536 readptr + buf->segoffset, len);
538 buf->segoffset += len;
542 if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
543 /* clear written samples */
544 gst_audio_ring_buffer_clear (GST_AUDIO_RING_BUFFER (buf), readseg);
546 /* we wrote one segment */
547 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
556 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
558 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
560 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
564 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
566 GstOsxAudioRingBuffer *osxbuf;
568 osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
572 gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);