osxaudiosink: Prefer filter caps order while getting caps
[platform/upstream/gst-plugins-good.git] / sys / osxaudio / gstosxaudiosink.c
1 /*
2  * GStreamer
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>
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  *
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
28  * mentioned above:
29  *
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.
34  *
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.
39  *
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.
44  *
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
47  *
48  */
49
50 /**
51  * SECTION:element-osxaudiosink
52  *
53  * This element renders raw audio samples using the CoreAudio api.
54  *
55  * <refsect2>
56  * <title>Example pipelines</title>
57  * |[
58  * gst-launch-1.0 filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! osxaudiosink
59  * ]| Play an Ogg/Vorbis file.
60  * </refsect2>
61  */
62
63 #ifdef HAVE_CONFIG_H
64 #  include <config.h>
65 #endif
66
67 #include <gst/gst.h>
68 #include <gst/audio/audio.h>
69 #include <gst/audio/audio-channels.h>
70 #include <gst/audio/gstaudioiec61937.h>
71
72 #include "gstosxaudiosink.h"
73 #include "gstosxaudioelement.h"
74
75 GST_DEBUG_CATEGORY_STATIC (osx_audiosink_debug);
76 #define GST_CAT_DEFAULT osx_audiosink_debug
77
78 #include "gstosxcoreaudio.h"
79
80 /* Filter signals and args */
81 enum
82 {
83   /* FILL ME */
84   LAST_SIGNAL
85 };
86
87 enum
88 {
89   ARG_0,
90   ARG_DEVICE,
91   ARG_VOLUME
92 };
93
94 #define DEFAULT_VOLUME 1.0
95
96 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
97 # define FORMATS "{ S32LE, S24LE, S16LE, U8 }"
98 #else
99 # define FORMATS "{ S32BE, S24BE, S16BE, U8 }"
100 #endif
101
102 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
103     GST_PAD_SINK,
104     GST_PAD_ALWAYS,
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")
112     );
113
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);
118
119 static GstStateChangeReturn
120 gst_osx_audio_sink_change_state (GstElement * element,
121     GstStateChange transition);
122
123 static gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
124
125 static gboolean gst_osx_audio_sink_stop (GstBaseSink * base);
126 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
127     GstCaps * filter);
128 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
129     GstCaps * caps);
130
131 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
132     GstBuffer * buf);
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);
139
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);
144
145 static void
146 gst_osx_audio_sink_do_init (GType type)
147 {
148   static const GInterfaceInfo osxelement_info = {
149     gst_osx_audio_sink_osxelement_init,
150     NULL,
151     NULL
152   };
153
154   GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
155       "OSX Audio Sink");
156   gst_core_audio_init_debug ();
157   GST_DEBUG ("Adding static interface");
158   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
159       &osxelement_info);
160 }
161
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));
165
166 static void
167 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
168 {
169   GObjectClass *gobject_class;
170   GstElementClass *gstelement_class;
171   GstBaseSinkClass *gstbasesink_class;
172   GstAudioBaseSinkClass *gstaudiobasesink_class;
173
174   gobject_class = (GObjectClass *) klass;
175   gstelement_class = (GstElementClass *) klass;
176   gstbasesink_class = (GstBaseSinkClass *) klass;
177   gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
178
179   parent_class = g_type_class_peek_parent (klass);
180
181   gobject_class->set_property = gst_osx_audio_sink_set_property;
182   gobject_class->get_property = gst_osx_audio_sink_get_property;
183
184   gstelement_class->change_state =
185       GST_DEBUG_FUNCPTR (gst_osx_audio_sink_change_state);
186
187 #ifndef HAVE_IOS
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));
191 #endif
192
193   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
194
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));
198
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);
201
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);
206
207   gst_element_class_add_pad_template (gstelement_class,
208       gst_static_pad_template_get (&sink_factory));
209
210   gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (OSX)",
211       "Sink/Audio",
212       "Output to a sound card in OS X",
213       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
214 }
215
216 static void
217 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
218 {
219   gint i;
220
221   GST_DEBUG ("Initialising object");
222
223   sink->device_id = kAudioDeviceUnknown;
224   sink->cached_caps = NULL;
225
226   sink->volume = DEFAULT_VOLUME;
227
228   sink->channels = 0;
229   for (i = 0; i < GST_OSX_AUDIO_MAX_CHANNEL; i++) {
230     sink->channel_positions[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
231   }
232 }
233
234 static void
235 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
236     const GValue * value, GParamSpec * pspec)
237 {
238   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
239
240   switch (prop_id) {
241 #ifndef HAVE_IOS
242     case ARG_DEVICE:
243       sink->device_id = g_value_get_int (value);
244       break;
245 #endif
246     case ARG_VOLUME:
247       sink->volume = g_value_get_double (value);
248       gst_osx_audio_sink_set_volume (sink);
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static GstStateChangeReturn
257 gst_osx_audio_sink_change_state (GstElement * element,
258     GstStateChange transition)
259 {
260   GstOsxAudioSink *osxsink = GST_OSX_AUDIO_SINK (element);
261   GstOsxAudioRingBuffer *ringbuffer;
262   GstStateChangeReturn ret;
263
264   switch (transition) {
265     default:
266       break;
267   }
268
269   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
270   if (ret == GST_STATE_CHANGE_FAILURE)
271     goto out;
272
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);
277       break;
278
279     case GST_STATE_CHANGE_READY_TO_PAUSED:
280       /* The device is open now, so fix our device_id if it changed */
281       ringbuffer =
282           GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (osxsink)->ringbuffer);
283       osxsink->device_id = ringbuffer->core_audio->device_id;
284       break;
285
286     default:
287       break;
288   }
289
290 out:
291   return ret;
292 }
293
294 static void
295 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
296     GValue * value, GParamSpec * pspec)
297 {
298   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
299   switch (prop_id) {
300 #ifndef HAVE_IOS
301     case ARG_DEVICE:
302       g_value_set_int (value, sink->device_id);
303       break;
304 #endif
305     case ARG_VOLUME:
306       g_value_set_double (value, sink->volume);
307       break;
308     default:
309       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310       break;
311   }
312 }
313
314 static gboolean
315 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
316 {
317   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
318   gboolean ret = FALSE;
319
320   switch (GST_QUERY_TYPE (query)) {
321     case GST_QUERY_ACCEPT_CAPS:
322     {
323       GstCaps *caps = NULL;
324
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);
328       ret = TRUE;
329       break;
330     }
331     default:
332       ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
333       break;
334   }
335   return ret;
336 }
337
338 static gboolean
339 gst_osx_audio_sink_stop (GstBaseSink * base)
340 {
341   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
342
343   if (sink->cached_caps) {
344     gst_caps_unref (sink->cached_caps);
345     sink->cached_caps = NULL;
346   }
347
348   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SINK_CLASS, stop, (base), TRUE);
349 }
350
351 static GstCaps *
352 gst_osx_audio_sink_getcaps (GstBaseSink * sink, GstCaps * filter)
353 {
354   GstElementClass *gstelement_class;
355   GstOsxAudioSink *osxsink;
356   GstAudioRingBuffer *buf;
357   GstCaps *ret = NULL;
358
359   gstelement_class = GST_ELEMENT_GET_CLASS (sink);
360   osxsink = GST_OSX_AUDIO_SINK (sink);
361
362   GST_OBJECT_LOCK (osxsink);
363   buf = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
364   if (buf)
365     gst_object_ref (buf);
366   GST_OBJECT_UNLOCK (osxsink);
367
368   if (buf) {
369     GST_OBJECT_LOCK (buf);
370
371     if (buf->acquired) {
372       /* Caps are fixed, use what we have */
373       ret = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
374       if (!ret) {
375         GST_OBJECT_UNLOCK (buf);
376         g_return_val_if_reached (NULL);
377       }
378
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);
382     }
383
384     if (!ret && osxsink->cached_caps)
385       ret = gst_caps_ref (osxsink->cached_caps);
386
387     GST_OBJECT_UNLOCK (buf);
388
389     gst_object_unref (buf);
390   }
391
392   if (ret && filter) {
393     GstCaps *tmp;
394     tmp = gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
395     gst_caps_unref (ret);
396     ret = tmp;
397   }
398
399   return ret;
400 }
401
402 static gboolean
403 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
404 {
405   GstCaps *pad_caps;
406   GstStructure *st;
407   gboolean ret = FALSE;
408   GstAudioRingBufferSpec spec = { 0 };
409   gchar *caps_string = NULL;
410
411   caps_string = gst_caps_to_string (caps);
412   GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
413   g_free (caps_string);
414
415   pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
416   if (pad_caps) {
417     gboolean cret = gst_caps_can_intersect (pad_caps, caps);
418     gst_caps_unref (pad_caps);
419     if (!cret)
420       goto done;
421   }
422
423   /* If we've not got fixed caps, creating a stream might fail,
424    * so let's just return from here with default acceptcaps
425    * behaviour */
426   if (!gst_caps_is_fixed (caps))
427     goto done;
428
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))
433     goto done;
434
435   /* Make sure input is framed and can be payloaded */
436   switch (spec.type) {
437     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
438     {
439       gboolean framed = FALSE;
440
441       st = gst_caps_get_structure (caps, 0);
442
443       gst_structure_get_boolean (st, "framed", &framed);
444       if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
445         goto done;
446       break;
447     }
448     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
449     {
450       gboolean parsed = FALSE;
451
452       st = gst_caps_get_structure (caps, 0);
453
454       gst_structure_get_boolean (st, "parsed", &parsed);
455       if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
456         goto done;
457       break;
458     }
459     default:
460       break;
461   }
462   ret = TRUE;
463
464 done:
465   return ret;
466 }
467
468 static GstBuffer *
469 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
470 {
471   if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
472     gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
473     GstBuffer *out;
474     GstMapInfo inmap, outmap;
475     gboolean res;
476
477     if (framesize <= 0)
478       return NULL;
479
480     out = gst_buffer_new_and_alloc (framesize);
481
482     gst_buffer_map (buf, &inmap, GST_MAP_READ);
483     gst_buffer_map (out, &outmap, GST_MAP_WRITE);
484
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);
488
489     gst_buffer_unmap (buf, &inmap);
490     gst_buffer_unmap (out, &outmap);
491
492     if (!res) {
493       gst_buffer_unref (out);
494       return NULL;
495     }
496
497     gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
498     return out;
499
500   } else {
501     return gst_buffer_ref (buf);
502   }
503 }
504
505 static GstAudioRingBuffer *
506 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
507 {
508   GstOsxAudioSink *osxsink;
509   GstOsxAudioRingBuffer *ringbuffer;
510
511   osxsink = GST_OSX_AUDIO_SINK (sink);
512
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);
518
519   ringbuffer->core_audio->element =
520       GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
521   ringbuffer->core_audio->is_src = FALSE;
522
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");
526   }
527
528   return GST_AUDIO_RING_BUFFER (ringbuffer);
529 }
530
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 */
535 static OSStatus
536 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
537     AudioUnitRenderActionFlags * ioActionFlags,
538     const AudioTimeStamp * inTimeStamp,
539     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
540 {
541   guint8 *readptr;
542   gint readseg;
543   gint len;
544   gint stream_idx = buf->core_audio->stream_idx;
545   gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
546   gint offset = 0;
547
548   while (remaining) {
549     if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
550             &readseg, &readptr, &len))
551       return 0;
552
553     len -= buf->segoffset;
554
555     if (len > remaining)
556       len = remaining;
557
558     memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
559         readptr + buf->segoffset, len);
560
561     buf->segoffset += len;
562     offset += len;
563     remaining -= len;
564
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);
568
569       /* we wrote one segment */
570       gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
571
572       buf->segoffset = 0;
573     }
574   }
575   return 0;
576 }
577
578 static void
579 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
580 {
581   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
582
583   iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
584 }
585
586 static void
587 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
588 {
589   GstOsxAudioRingBuffer *osxbuf;
590
591   osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
592   if (!osxbuf)
593     return;
594
595   gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
596 }
597
598 static void
599 gst_osx_audio_sink_probe_caps (GstOsxAudioSink * osxsink)
600 {
601   gint i, channels;
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;
609
610   /* First collect info about the HW capabilites and preferences */
611   spdif_allowed =
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,
614       TRUE);
615
616   GST_DEBUG_OBJECT (osxsink, "Selected device ID: %u SPDIF allowed: %d",
617       (unsigned) osxsink->device_id, spdif_allowed);
618
619   if (layout) {
620     channels = MIN (layout->mNumberChannelDescriptions,
621         GST_OSX_AUDIO_MAX_CHANNEL);
622   } else {
623     GST_WARNING_OBJECT (osxsink, "This driver does not support "
624         "kAudioDevicePropertyPreferredChannelLayout.");
625     channels = 2;
626   }
627
628   if (!gst_core_audio_parse_channel_layout (layout, channels, &channel_mask,
629           pos)) {
630     GST_WARNING_OBJECT (osxsink, "Failed to parse channel layout");
631   }
632
633   g_free (layout);
634
635   if (!gst_audio_channel_positions_to_mask (pos, channels, TRUE, &channel_mask)) {
636     GST_WARNING_OBJECT (osxsink, "Probably unsupported channel order");
637   }
638
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);
643
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;
648
649     in_s = gst_caps_get_structure (in_caps, i);
650
651     if (gst_structure_has_name (in_s, "audio/x-ac3") ||
652         gst_structure_has_name (in_s, "audio/x-dts")) {
653       if (spdif_allowed) {
654         gst_caps_append_structure (caps, gst_structure_copy (in_s));
655       }
656     } else {
657       out_s = gst_structure_copy (in_s);
658
659       gst_structure_remove_fields (out_s, "channels", NULL);
660       gst_structure_set (out_s, "channels", G_TYPE_INT, channels, NULL);
661
662       if (channel_mask) {
663         gst_structure_remove_fields (out_s, "channel-mask", NULL);
664         gst_structure_set (out_s, "channel-mask", GST_TYPE_BITMASK,
665             channel_mask, NULL);
666       }
667
668       gst_caps_append_structure (caps, out_s);
669     }
670   }
671
672   if (osxsink->cached_caps) {
673     gst_caps_unref (osxsink->cached_caps);
674   }
675
676   osxsink->cached_caps = caps;
677   osxsink->channels = channels;
678 }