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 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
97 # define FORMATS "{ S32LE, S24LE, S16LE, U8 }"
99 # define FORMATS "{ S32BE, S24BE, S16BE, U8 }"
102 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
105 GST_STATIC_CAPS ("audio/x-raw, "
106 "format = (string) " FORMATS ", "
107 "layout = (string) interleaved, "
108 "rate = (int) [1, MAX], "
109 "channels = (int) [1, 9];"
110 "audio/x-ac3, framed = (boolean) true;"
111 "audio/x-dts, framed = (boolean) true")
114 static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
115 const GValue * value, GParamSpec * pspec);
116 static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
117 GValue * value, GParamSpec * pspec);
119 static GstStateChangeReturn
120 gst_osx_audio_sink_change_state (GstElement * element,
121 GstStateChange transition);
123 static gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
125 static gboolean gst_osx_audio_sink_stop (GstBaseSink * base);
126 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
128 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
131 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
133 static GstAudioRingBuffer
134 * gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink);
135 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
136 gpointer iface_data);
137 static void gst_osx_audio_sink_probe_caps (GstOsxAudioSink * sink);
138 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
140 static OSStatus gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
141 AudioUnitRenderActionFlags * ioActionFlags,
142 const AudioTimeStamp * inTimeStamp,
143 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
146 gst_osx_audio_sink_do_init (GType type)
148 static const GInterfaceInfo osxelement_info = {
149 gst_osx_audio_sink_osxelement_init,
154 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
156 gst_core_audio_init_debug ();
157 GST_DEBUG ("Adding static interface");
158 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
162 #define gst_osx_audio_sink_parent_class parent_class
163 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSink, gst_osx_audio_sink,
164 GST_TYPE_AUDIO_BASE_SINK, gst_osx_audio_sink_do_init (g_define_type_id));
167 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
169 GObjectClass *gobject_class;
170 GstElementClass *gstelement_class;
171 GstBaseSinkClass *gstbasesink_class;
172 GstAudioBaseSinkClass *gstaudiobasesink_class;
174 gobject_class = (GObjectClass *) klass;
175 gstelement_class = (GstElementClass *) klass;
176 gstbasesink_class = (GstBaseSinkClass *) klass;
177 gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
179 parent_class = g_type_class_peek_parent (klass);
181 gobject_class->set_property = gst_osx_audio_sink_set_property;
182 gobject_class->get_property = gst_osx_audio_sink_get_property;
184 gstelement_class->change_state =
185 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_change_state);
188 g_object_class_install_property (gobject_class, ARG_DEVICE,
189 g_param_spec_int ("device", "Device ID", "Device ID of output device",
190 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
195 g_object_class_install_property (gobject_class, ARG_VOLUME,
196 g_param_spec_double ("volume", "Volume", "Volume of this stream",
197 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps);
200 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_stop);
202 gstaudiobasesink_class->create_ringbuffer =
203 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
204 gstaudiobasesink_class->payload =
205 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_sink_payload);
207 gst_element_class_add_pad_template (gstelement_class,
208 gst_static_pad_template_get (&sink_factory));
210 gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (OSX)",
212 "Output to a sound card in OS X",
213 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
217 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
221 GST_DEBUG ("Initialising object");
223 sink->device_id = kAudioDeviceUnknown;
224 sink->cached_caps = NULL;
226 sink->volume = DEFAULT_VOLUME;
229 for (i = 0; i < GST_OSX_AUDIO_MAX_CHANNEL; i++) {
230 sink->channel_positions[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
235 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec)
238 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
243 sink->device_id = g_value_get_int (value);
247 sink->volume = g_value_get_double (value);
248 gst_osx_audio_sink_set_volume (sink);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 static GstStateChangeReturn
257 gst_osx_audio_sink_change_state (GstElement * element,
258 GstStateChange transition)
260 GstOsxAudioSink *osxsink = GST_OSX_AUDIO_SINK (element);
261 GstOsxAudioRingBuffer *ringbuffer;
262 GstStateChangeReturn ret;
264 switch (transition) {
269 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
270 if (ret == GST_STATE_CHANGE_FAILURE)
273 switch (transition) {
274 case GST_STATE_CHANGE_NULL_TO_READY:
275 /* Device has been selected, AudioUnit set up, so initialize volume */
276 gst_osx_audio_sink_set_volume (osxsink);
279 case GST_STATE_CHANGE_READY_TO_PAUSED:
280 /* The device is open now, so fix our device_id if it changed */
282 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (osxsink)->ringbuffer);
283 osxsink->device_id = ringbuffer->core_audio->device_id;
295 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
296 GValue * value, GParamSpec * pspec)
298 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
302 g_value_set_int (value, sink->device_id);
306 g_value_set_double (value, sink->volume);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
317 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
318 gboolean ret = FALSE;
320 switch (GST_QUERY_TYPE (query)) {
321 case GST_QUERY_ACCEPT_CAPS:
323 GstCaps *caps = NULL;
325 gst_query_parse_accept_caps (query, &caps);
326 ret = gst_osx_audio_sink_acceptcaps (sink, caps);
327 gst_query_set_accept_caps_result (query, ret);
332 ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
339 gst_osx_audio_sink_stop (GstBaseSink * base)
341 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
343 if (sink->cached_caps) {
344 gst_caps_unref (sink->cached_caps);
345 sink->cached_caps = NULL;
348 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SINK_CLASS, stop, (base), TRUE);
352 gst_osx_audio_sink_getcaps (GstBaseSink * sink, GstCaps * filter)
354 GstElementClass *gstelement_class;
355 GstOsxAudioSink *osxsink;
356 GstAudioRingBuffer *buf;
359 gstelement_class = GST_ELEMENT_GET_CLASS (sink);
360 osxsink = GST_OSX_AUDIO_SINK (sink);
362 GST_OBJECT_LOCK (osxsink);
363 buf = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
365 gst_object_ref (buf);
366 GST_OBJECT_UNLOCK (osxsink);
369 GST_OBJECT_LOCK (buf);
372 /* Caps are fixed, use what we have */
373 ret = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
375 GST_OBJECT_UNLOCK (buf);
376 g_return_val_if_reached (NULL);
379 } else if (buf->open && !osxsink->cached_caps) {
380 /* Device is open, let's probe its caps */
381 gst_osx_audio_sink_probe_caps (osxsink);
384 if (!ret && osxsink->cached_caps)
385 ret = gst_caps_ref (osxsink->cached_caps);
387 GST_OBJECT_UNLOCK (buf);
389 gst_object_unref (buf);
394 tmp = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
395 gst_caps_unref (ret);
403 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
407 gboolean ret = FALSE;
408 GstAudioRingBufferSpec spec = { 0 };
409 gchar *caps_string = NULL;
411 caps_string = gst_caps_to_string (caps);
412 GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
413 g_free (caps_string);
415 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
417 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
418 gst_caps_unref (pad_caps);
423 /* If we've not got fixed caps, creating a stream might fail,
424 * so let's just return from here with default acceptcaps
426 if (!gst_caps_is_fixed (caps))
429 /* parse helper expects this set, so avoid nasty warning
430 * will be set properly later on anyway */
431 spec.latency_time = GST_SECOND;
432 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
435 /* Make sure input is framed and can be payloaded */
437 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
439 gboolean framed = FALSE;
441 st = gst_caps_get_structure (caps, 0);
443 gst_structure_get_boolean (st, "framed", &framed);
444 if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
448 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
450 gboolean parsed = FALSE;
452 st = gst_caps_get_structure (caps, 0);
454 gst_structure_get_boolean (st, "parsed", &parsed);
455 if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
469 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
471 if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
472 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
474 GstMapInfo inmap, outmap;
480 out = gst_buffer_new_and_alloc (framesize);
482 gst_buffer_map (buf, &inmap, GST_MAP_READ);
483 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
485 /* FIXME: the endianness needs to be queried and then set */
486 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
487 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
489 gst_buffer_unmap (buf, &inmap);
490 gst_buffer_unmap (out, &outmap);
493 gst_buffer_unref (out);
497 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
501 return gst_buffer_ref (buf);
505 static GstAudioRingBuffer *
506 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
508 GstOsxAudioSink *osxsink;
509 GstOsxAudioRingBuffer *ringbuffer;
511 osxsink = GST_OSX_AUDIO_SINK (sink);
513 GST_DEBUG_OBJECT (sink, "Creating ringbuffer");
514 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
515 GST_DEBUG_OBJECT (sink, "osx sink %p element %p ioproc %p", osxsink,
516 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
517 (void *) gst_osx_audio_sink_io_proc);
519 ringbuffer->core_audio->element =
520 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
521 ringbuffer->core_audio->is_src = FALSE;
523 if (ringbuffer->core_audio->device_id != osxsink->device_id) {
524 ringbuffer->core_audio->device_id = osxsink->device_id;
525 g_object_notify (G_OBJECT (osxsink), "device");
528 return GST_AUDIO_RING_BUFFER (ringbuffer);
531 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks
532 * of data, not of a fixed size. So, we keep track of where in
533 * the current ringbuffer segment we are, and only advance the segment
534 * once we've read the whole thing */
536 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
537 AudioUnitRenderActionFlags * ioActionFlags,
538 const AudioTimeStamp * inTimeStamp,
539 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
544 gint stream_idx = buf->core_audio->stream_idx;
545 gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
549 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
550 &readseg, &readptr, &len))
553 len -= buf->segoffset;
558 memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
559 readptr + buf->segoffset, len);
561 buf->segoffset += len;
565 if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
566 /* clear written samples */
567 gst_audio_ring_buffer_clear (GST_AUDIO_RING_BUFFER (buf), readseg);
569 /* we wrote one segment */
570 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
579 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
581 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
583 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
587 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
589 GstOsxAudioRingBuffer *osxbuf;
591 osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
595 gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
599 gst_osx_audio_sink_probe_caps (GstOsxAudioSink * osxsink)
602 gboolean spdif_allowed;
603 AudioChannelLayout *layout;
604 GstElementClass *element_class;
605 GstPadTemplate *pad_template;
606 GstCaps *caps, *in_caps;
607 guint64 channel_mask = 0;
608 GstAudioChannelPosition *pos = osxsink->channel_positions;
610 /* First collect info about the HW capabilites and preferences */
612 gst_core_audio_audio_device_is_spdif_avail (osxsink->device_id);
613 layout = gst_core_audio_audio_device_get_channel_layout (osxsink->device_id,
616 GST_DEBUG_OBJECT (osxsink, "Selected device ID: %u SPDIF allowed: %d",
617 (unsigned) osxsink->device_id, spdif_allowed);
620 channels = MIN (layout->mNumberChannelDescriptions,
621 GST_OSX_AUDIO_MAX_CHANNEL);
623 GST_WARNING_OBJECT (osxsink, "This driver does not support "
624 "kAudioDevicePropertyPreferredChannelLayout.");
628 if (!gst_core_audio_parse_channel_layout (layout, channels, &channel_mask,
630 GST_WARNING_OBJECT (osxsink, "Failed to parse channel layout");
635 if (!gst_audio_channel_positions_to_mask (pos, channels, TRUE, &channel_mask)) {
636 GST_WARNING_OBJECT (osxsink, "Probably unsupported channel order");
639 /* Recover the template caps */
640 element_class = GST_ELEMENT_GET_CLASS (osxsink);
641 pad_template = gst_element_class_get_pad_template (element_class, "sink");
642 in_caps = gst_pad_template_get_caps (pad_template);
644 /* Create the allowed subset */
645 caps = gst_caps_new_empty ();
646 for (i = 0; i < gst_caps_get_size (in_caps); i++) {
647 GstStructure *in_s, *out_s;
649 in_s = gst_caps_get_structure (in_caps, i);
651 if (gst_structure_has_name (in_s, "audio/x-ac3") ||
652 gst_structure_has_name (in_s, "audio/x-dts")) {
654 gst_caps_append_structure (caps, gst_structure_copy (in_s));
657 out_s = gst_structure_copy (in_s);
659 gst_structure_remove_fields (out_s, "channels", NULL);
660 gst_structure_set (out_s, "channels", G_TYPE_INT, channels, NULL);
663 gst_structure_remove_fields (out_s, "channel-mask", NULL);
664 gst_structure_set (out_s, "channel-mask", GST_TYPE_BITMASK,
668 gst_caps_append_structure (caps, out_s);
672 if (osxsink->cached_caps) {
673 gst_caps_unref (osxsink->cached_caps);
676 osxsink->cached_caps = caps;
677 osxsink->channels = channels;