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 gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
121 static gboolean gst_osx_audio_sink_stop (GstBaseSink * base);
122 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
124 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
127 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
129 static GstAudioRingBuffer
130 * gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink);
131 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
132 gpointer iface_data);
133 static gboolean gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink);
134 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
136 static OSStatus gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
137 AudioUnitRenderActionFlags * ioActionFlags,
138 const AudioTimeStamp * inTimeStamp,
139 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
142 gst_osx_audio_sink_do_init (GType type)
144 static const GInterfaceInfo osxelement_info = {
145 gst_osx_audio_sink_osxelement_init,
150 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
152 gst_core_audio_init_debug ();
153 GST_DEBUG ("Adding static interface");
154 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
158 #define gst_osx_audio_sink_parent_class parent_class
159 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSink, gst_osx_audio_sink,
160 GST_TYPE_AUDIO_BASE_SINK, gst_osx_audio_sink_do_init (g_define_type_id));
163 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseSinkClass *gstbasesink_class;
168 GstAudioBaseSinkClass *gstaudiobasesink_class;
170 gobject_class = (GObjectClass *) klass;
171 gstelement_class = (GstElementClass *) klass;
172 gstbasesink_class = (GstBaseSinkClass *) klass;
173 gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
175 parent_class = g_type_class_peek_parent (klass);
177 gobject_class->set_property = gst_osx_audio_sink_set_property;
178 gobject_class->get_property = gst_osx_audio_sink_get_property;
181 g_object_class_install_property (gobject_class, ARG_DEVICE,
182 g_param_spec_int ("device", "Device ID", "Device ID of output device",
183 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
188 g_object_class_install_property (gobject_class, ARG_VOLUME,
189 g_param_spec_double ("volume", "Volume", "Volume of this stream",
190 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps);
193 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_stop);
195 gstaudiobasesink_class->create_ringbuffer =
196 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
197 gstaudiobasesink_class->payload =
198 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_sink_payload);
200 gst_element_class_add_pad_template (gstelement_class,
201 gst_static_pad_template_get (&sink_factory));
203 gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (OSX)",
205 "Output to a sound card in OS X",
206 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
210 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
214 GST_DEBUG ("Initialising object");
216 sink->device_id = kAudioDeviceUnknown;
217 sink->cached_caps = NULL;
219 sink->volume = DEFAULT_VOLUME;
222 for (i = 0; i < GST_OSX_AUDIO_MAX_CHANNEL; i++) {
223 sink->channel_positions[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
228 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
229 const GValue * value, GParamSpec * pspec)
231 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
236 sink->device_id = g_value_get_int (value);
240 sink->volume = g_value_get_double (value);
241 gst_osx_audio_sink_set_volume (sink);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec)
253 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
257 g_value_set_int (value, sink->device_id);
261 g_value_set_double (value, sink->volume);
264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
272 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
273 gboolean ret = FALSE;
275 switch (GST_QUERY_TYPE (query)) {
276 case GST_QUERY_ACCEPT_CAPS:
278 GstCaps *caps = NULL;
280 gst_query_parse_accept_caps (query, &caps);
281 ret = gst_osx_audio_sink_acceptcaps (sink, caps);
282 gst_query_set_accept_caps_result (query, ret);
287 ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
294 gst_osx_audio_sink_stop (GstBaseSink * base)
296 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
298 if (sink->cached_caps) {
299 gst_caps_unref (sink->cached_caps);
300 sink->cached_caps = NULL;
303 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SINK_CLASS, stop, (base), TRUE);
307 gst_osx_audio_sink_getcaps (GstBaseSink * base, GstCaps * filter)
309 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
310 gchar *caps_string = NULL;
312 if (sink->cached_caps) {
313 caps_string = gst_caps_to_string (sink->cached_caps);
314 GST_DEBUG_OBJECT (sink, "using cached caps: %s", caps_string);
315 g_free (caps_string);
317 return gst_caps_intersect_full (sink->cached_caps, filter,
318 GST_CAPS_INTERSECT_FIRST);
319 return gst_caps_ref (sink->cached_caps);
322 GST_DEBUG_OBJECT (sink, "using template caps");
327 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
331 gboolean ret = FALSE;
332 GstAudioRingBufferSpec spec = { 0 };
333 gchar *caps_string = NULL;
335 caps_string = gst_caps_to_string (caps);
336 GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
337 g_free (caps_string);
339 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
341 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
342 gst_caps_unref (pad_caps);
347 /* If we've not got fixed caps, creating a stream might fail,
348 * so let's just return from here with default acceptcaps
350 if (!gst_caps_is_fixed (caps))
353 /* parse helper expects this set, so avoid nasty warning
354 * will be set properly later on anyway */
355 spec.latency_time = GST_SECOND;
356 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
359 /* Make sure input is framed and can be payloaded */
361 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
363 gboolean framed = FALSE;
365 st = gst_caps_get_structure (caps, 0);
367 gst_structure_get_boolean (st, "framed", &framed);
368 if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
372 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
374 gboolean parsed = FALSE;
376 st = gst_caps_get_structure (caps, 0);
378 gst_structure_get_boolean (st, "parsed", &parsed);
379 if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
393 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
395 if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
396 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
398 GstMapInfo inmap, outmap;
404 out = gst_buffer_new_and_alloc (framesize);
406 gst_buffer_map (buf, &inmap, GST_MAP_READ);
407 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
409 /* FIXME: the endianness needs to be queried and then set */
410 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
411 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
413 gst_buffer_unmap (buf, &inmap);
414 gst_buffer_unmap (out, &outmap);
417 gst_buffer_unref (out);
421 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
425 return gst_buffer_ref (buf);
429 static GstAudioRingBuffer *
430 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
432 GstOsxAudioSink *osxsink;
433 GstOsxAudioRingBuffer *ringbuffer;
435 osxsink = GST_OSX_AUDIO_SINK (sink);
437 if (!gst_osx_audio_sink_select_device (osxsink)) {
438 GST_ERROR_OBJECT (sink, "Could not select device");
442 GST_DEBUG_OBJECT (sink, "Creating ringbuffer");
443 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
444 GST_DEBUG_OBJECT (sink, "osx sink %p element %p ioproc %p", osxsink,
445 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
446 (void *) gst_osx_audio_sink_io_proc);
448 gst_osx_audio_sink_set_volume (osxsink);
450 ringbuffer->core_audio->element =
451 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
452 ringbuffer->core_audio->device_id = osxsink->device_id;
453 ringbuffer->core_audio->is_src = FALSE;
455 return GST_AUDIO_RING_BUFFER (ringbuffer);
458 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks
459 * of data, not of a fixed size. So, we keep track of where in
460 * the current ringbuffer segment we are, and only advance the segment
461 * once we've read the whole thing */
463 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
464 AudioUnitRenderActionFlags * ioActionFlags,
465 const AudioTimeStamp * inTimeStamp,
466 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
471 gint stream_idx = buf->core_audio->stream_idx;
472 gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
476 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
477 &readseg, &readptr, &len))
480 len -= buf->segoffset;
485 memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
486 readptr + buf->segoffset, len);
488 buf->segoffset += len;
492 if ((gint) buf->segoffset == GST_AUDIO_RING_BUFFER (buf)->spec.segsize) {
493 /* clear written samples */
494 gst_audio_ring_buffer_clear (GST_AUDIO_RING_BUFFER (buf), readseg);
496 /* we wrote one segment */
497 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
506 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
508 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
510 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
514 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
516 GstOsxAudioRingBuffer *osxbuf;
518 osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
522 gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
526 gst_osx_audio_sink_allowed_caps (GstOsxAudioSink * osxsink)
529 gboolean spdif_allowed;
530 AudioChannelLayout *layout;
531 GstElementClass *element_class;
532 GstPadTemplate *pad_template;
533 GstCaps *caps, *in_caps;
534 guint64 channel_mask = 0;
535 GstAudioChannelPosition *pos = osxsink->channel_positions;
537 /* First collect info about the HW capabilites and preferences */
539 gst_core_audio_audio_device_is_spdif_avail (osxsink->device_id);
540 layout = gst_core_audio_audio_device_get_channel_layout (osxsink->device_id);
542 GST_DEBUG_OBJECT (osxsink, "Selected device ID: %u SPDIF allowed: %d",
543 (unsigned) osxsink->device_id, spdif_allowed);
546 channels = MIN (layout->mNumberChannelDescriptions,
547 GST_OSX_AUDIO_MAX_CHANNEL);
549 GST_WARNING_OBJECT (osxsink, "This driver does not support "
550 "kAudioDevicePropertyPreferredChannelLayout.");
556 pos[0] = GST_AUDIO_CHANNEL_POSITION_NONE;
559 pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
562 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
563 pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
564 channel_mask |= GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT);
565 channel_mask |= GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT);
568 channels = MIN (layout->mNumberChannelDescriptions,
569 GST_OSX_AUDIO_MAX_CHANNEL);
570 for (i = 0; i < channels; i++) {
571 switch (layout->mChannelDescriptions[i].mChannelLabel) {
572 case kAudioChannelLabel_Left:
573 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
575 case kAudioChannelLabel_Right:
576 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
578 case kAudioChannelLabel_Center:
579 pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
581 case kAudioChannelLabel_LFEScreen:
582 pos[i] = GST_AUDIO_CHANNEL_POSITION_LFE1;
584 case kAudioChannelLabel_LeftSurround:
585 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
587 case kAudioChannelLabel_RightSurround:
588 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
590 case kAudioChannelLabel_RearSurroundLeft:
591 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
593 case kAudioChannelLabel_RearSurroundRight:
594 pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
596 case kAudioChannelLabel_CenterSurround:
597 pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
600 GST_WARNING_OBJECT (osxsink, "unrecognized channel: %d",
601 (int) layout->mChannelDescriptions[i].mChannelLabel);
610 /* Recover the template caps */
611 element_class = GST_ELEMENT_GET_CLASS (osxsink);
612 pad_template = gst_element_class_get_pad_template (element_class, "sink");
613 in_caps = gst_pad_template_get_caps (pad_template);
615 /* Create the allowed subset */
616 caps = gst_caps_new_empty ();
617 for (i = 0; i < gst_caps_get_size (in_caps); i++) {
618 GstStructure *in_s, *out_s;
620 in_s = gst_caps_get_structure (in_caps, i);
622 if (gst_structure_has_name (in_s, "audio/x-ac3") ||
623 gst_structure_has_name (in_s, "audio/x-dts")) {
625 gst_caps_append_structure (caps, gst_structure_copy (in_s));
628 gst_audio_channel_positions_to_mask (pos, channels, false, &channel_mask);
629 out_s = gst_structure_copy (in_s);
630 gst_structure_remove_fields (out_s, "channels", "channel-mask", NULL);
631 gst_structure_set (out_s, "channels", G_TYPE_INT, channels,
632 "channel-mask", GST_TYPE_BITMASK, channel_mask, NULL);
633 gst_caps_append_structure (caps, out_s);
636 if (osxsink->cached_caps) {
637 gst_caps_unref (osxsink->cached_caps);
640 osxsink->cached_caps = caps;
641 osxsink->channels = channels;
647 gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink)
649 gboolean res = FALSE;
651 if (!gst_core_audio_select_device (&osxsink->device_id))
653 res = gst_osx_audio_sink_allowed_caps (osxsink);