multipartdemux: guard against having no MIME type
[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 gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
120
121 static gboolean gst_osx_audio_sink_stop (GstBaseSink * base);
122 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
123     GstCaps * filter);
124 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
125     GstCaps * caps);
126
127 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
128     GstBuffer * buf);
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);
135
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);
140
141 static void
142 gst_osx_audio_sink_do_init (GType type)
143 {
144   static const GInterfaceInfo osxelement_info = {
145     gst_osx_audio_sink_osxelement_init,
146     NULL,
147     NULL
148   };
149
150   GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
151       "OSX Audio Sink");
152   gst_core_audio_init_debug ();
153   GST_DEBUG ("Adding static interface");
154   g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
155       &osxelement_info);
156 }
157
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));
161
162 static void
163 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167   GstBaseSinkClass *gstbasesink_class;
168   GstAudioBaseSinkClass *gstaudiobasesink_class;
169
170   gobject_class = (GObjectClass *) klass;
171   gstelement_class = (GstElementClass *) klass;
172   gstbasesink_class = (GstBaseSinkClass *) klass;
173   gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
174
175   parent_class = g_type_class_peek_parent (klass);
176
177   gobject_class->set_property = gst_osx_audio_sink_set_property;
178   gobject_class->get_property = gst_osx_audio_sink_get_property;
179
180 #ifndef HAVE_IOS
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));
184 #endif
185
186   gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
187
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));
191
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);
194
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);
199
200   gst_element_class_add_pad_template (gstelement_class,
201       gst_static_pad_template_get (&sink_factory));
202
203   gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (OSX)",
204       "Sink/Audio",
205       "Output to a sound card in OS X",
206       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
207 }
208
209 static void
210 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
211 {
212   gint i;
213
214   GST_DEBUG ("Initialising object");
215
216   sink->device_id = kAudioDeviceUnknown;
217   sink->cached_caps = NULL;
218
219   sink->volume = DEFAULT_VOLUME;
220
221   sink->channels = 0;
222   for (i = 0; i < GST_OSX_AUDIO_MAX_CHANNEL; i++) {
223     sink->channel_positions[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
224   }
225 }
226
227 static void
228 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
229     const GValue * value, GParamSpec * pspec)
230 {
231   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
232
233   switch (prop_id) {
234 #ifndef HAVE_IOS
235     case ARG_DEVICE:
236       sink->device_id = g_value_get_int (value);
237       break;
238 #endif
239     case ARG_VOLUME:
240       sink->volume = g_value_get_double (value);
241       gst_osx_audio_sink_set_volume (sink);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248
249 static void
250 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
251     GValue * value, GParamSpec * pspec)
252 {
253   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
254   switch (prop_id) {
255 #ifndef HAVE_IOS
256     case ARG_DEVICE:
257       g_value_set_int (value, sink->device_id);
258       break;
259 #endif
260     case ARG_VOLUME:
261       g_value_set_double (value, sink->volume);
262       break;
263     default:
264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265       break;
266   }
267 }
268
269 static gboolean
270 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
271 {
272   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
273   gboolean ret = FALSE;
274
275   switch (GST_QUERY_TYPE (query)) {
276     case GST_QUERY_ACCEPT_CAPS:
277     {
278       GstCaps *caps = NULL;
279
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);
283       ret = TRUE;
284       break;
285     }
286     default:
287       ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
288       break;
289   }
290   return ret;
291 }
292
293 static gboolean
294 gst_osx_audio_sink_stop (GstBaseSink * base)
295 {
296   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
297
298   if (sink->cached_caps) {
299     gst_caps_unref (sink->cached_caps);
300     sink->cached_caps = NULL;
301   }
302
303   return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SINK_CLASS, stop, (base), TRUE);
304 }
305
306 static GstCaps *
307 gst_osx_audio_sink_getcaps (GstBaseSink * base, GstCaps * filter)
308 {
309   GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
310   gchar *caps_string = NULL;
311
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);
316     if (filter)
317       return gst_caps_intersect_full (sink->cached_caps, filter,
318           GST_CAPS_INTERSECT_FIRST);
319     return gst_caps_ref (sink->cached_caps);
320   }
321
322   GST_DEBUG_OBJECT (sink, "using template caps");
323   return NULL;
324 }
325
326 static gboolean
327 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
328 {
329   GstCaps *pad_caps;
330   GstStructure *st;
331   gboolean ret = FALSE;
332   GstAudioRingBufferSpec spec = { 0 };
333   gchar *caps_string = NULL;
334
335   caps_string = gst_caps_to_string (caps);
336   GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
337   g_free (caps_string);
338
339   pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
340   if (pad_caps) {
341     gboolean cret = gst_caps_can_intersect (pad_caps, caps);
342     gst_caps_unref (pad_caps);
343     if (!cret)
344       goto done;
345   }
346
347   /* If we've not got fixed caps, creating a stream might fail,
348    * so let's just return from here with default acceptcaps
349    * behaviour */
350   if (!gst_caps_is_fixed (caps))
351     goto done;
352
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))
357     goto done;
358
359   /* Make sure input is framed and can be payloaded */
360   switch (spec.type) {
361     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
362     {
363       gboolean framed = FALSE;
364
365       st = gst_caps_get_structure (caps, 0);
366
367       gst_structure_get_boolean (st, "framed", &framed);
368       if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
369         goto done;
370       break;
371     }
372     case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
373     {
374       gboolean parsed = FALSE;
375
376       st = gst_caps_get_structure (caps, 0);
377
378       gst_structure_get_boolean (st, "parsed", &parsed);
379       if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
380         goto done;
381       break;
382     }
383     default:
384       break;
385   }
386   ret = TRUE;
387
388 done:
389   return ret;
390 }
391
392 static GstBuffer *
393 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
394 {
395   if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
396     gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
397     GstBuffer *out;
398     GstMapInfo inmap, outmap;
399     gboolean res;
400
401     if (framesize <= 0)
402       return NULL;
403
404     out = gst_buffer_new_and_alloc (framesize);
405
406     gst_buffer_map (buf, &inmap, GST_MAP_READ);
407     gst_buffer_map (out, &outmap, GST_MAP_WRITE);
408
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);
412
413     gst_buffer_unmap (buf, &inmap);
414     gst_buffer_unmap (out, &outmap);
415
416     if (!res) {
417       gst_buffer_unref (out);
418       return NULL;
419     }
420
421     gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
422     return out;
423
424   } else {
425     return gst_buffer_ref (buf);
426   }
427 }
428
429 static GstAudioRingBuffer *
430 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
431 {
432   GstOsxAudioSink *osxsink;
433   GstOsxAudioRingBuffer *ringbuffer;
434
435   osxsink = GST_OSX_AUDIO_SINK (sink);
436
437   if (!gst_osx_audio_sink_select_device (osxsink)) {
438     GST_ERROR_OBJECT (sink, "Could not select device");
439     return NULL;
440   }
441
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);
447
448   gst_osx_audio_sink_set_volume (osxsink);
449
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;
454
455   return GST_AUDIO_RING_BUFFER (ringbuffer);
456 }
457
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 */
462 static OSStatus
463 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
464     AudioUnitRenderActionFlags * ioActionFlags,
465     const AudioTimeStamp * inTimeStamp,
466     UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
467 {
468   guint8 *readptr;
469   gint readseg;
470   gint len;
471   gint stream_idx = buf->core_audio->stream_idx;
472   gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
473   gint offset = 0;
474
475   while (remaining) {
476     if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
477             &readseg, &readptr, &len))
478       return 0;
479
480     len -= buf->segoffset;
481
482     if (len > remaining)
483       len = remaining;
484
485     memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
486         readptr + buf->segoffset, len);
487
488     buf->segoffset += len;
489     offset += len;
490     remaining -= len;
491
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);
495
496       /* we wrote one segment */
497       gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
498
499       buf->segoffset = 0;
500     }
501   }
502   return 0;
503 }
504
505 static void
506 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
507 {
508   GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
509
510   iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
511 }
512
513 static void
514 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
515 {
516   GstOsxAudioRingBuffer *osxbuf;
517
518   osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
519   if (!osxbuf)
520     return;
521
522   gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
523 }
524
525 static gboolean
526 gst_osx_audio_sink_allowed_caps (GstOsxAudioSink * osxsink)
527 {
528   gint i, channels;
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;
536
537   /* First collect info about the HW capabilites and preferences */
538   spdif_allowed =
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);
541
542   GST_DEBUG_OBJECT (osxsink, "Selected device ID: %u SPDIF allowed: %d",
543       (unsigned) osxsink->device_id, spdif_allowed);
544
545   if (layout) {
546     channels = MIN (layout->mNumberChannelDescriptions,
547         GST_OSX_AUDIO_MAX_CHANNEL);
548   } else {
549     GST_WARNING_OBJECT (osxsink, "This driver does not support "
550         "kAudioDevicePropertyPreferredChannelLayout.");
551     channels = 2;
552   }
553
554   switch (channels) {
555     case 0:
556       pos[0] = GST_AUDIO_CHANNEL_POSITION_NONE;
557       break;
558     case 1:
559       pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
560       break;
561     case 2:
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);
566       break;
567     default:
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;
574             break;
575           case kAudioChannelLabel_Right:
576             pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
577             break;
578           case kAudioChannelLabel_Center:
579             pos[i] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
580             break;
581           case kAudioChannelLabel_LFEScreen:
582             pos[i] = GST_AUDIO_CHANNEL_POSITION_LFE1;
583             break;
584           case kAudioChannelLabel_LeftSurround:
585             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
586             break;
587           case kAudioChannelLabel_RightSurround:
588             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
589             break;
590           case kAudioChannelLabel_RearSurroundLeft:
591             pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
592             break;
593           case kAudioChannelLabel_RearSurroundRight:
594             pos[i] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
595             break;
596           case kAudioChannelLabel_CenterSurround:
597             pos[i] = GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
598             break;
599           default:
600             GST_WARNING_OBJECT (osxsink, "unrecognized channel: %d",
601                 (int) layout->mChannelDescriptions[i].mChannelLabel);
602             channel_mask = 0;
603             channels = 2;
604             break;
605         }
606       }
607   }
608   g_free (layout);
609
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);
614
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;
619
620     in_s = gst_caps_get_structure (in_caps, i);
621
622     if (gst_structure_has_name (in_s, "audio/x-ac3") ||
623         gst_structure_has_name (in_s, "audio/x-dts")) {
624       if (spdif_allowed) {
625         gst_caps_append_structure (caps, gst_structure_copy (in_s));
626       }
627     }
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);
634   }
635
636   if (osxsink->cached_caps) {
637     gst_caps_unref (osxsink->cached_caps);
638   }
639
640   osxsink->cached_caps = caps;
641   osxsink->channels = channels;
642
643   return TRUE;
644 }
645
646 static gboolean
647 gst_osx_audio_sink_select_device (GstOsxAudioSink * osxsink)
648 {
649   gboolean res = FALSE;
650
651   if (!gst_core_audio_select_device (&osxsink->device_id))
652     return FALSE;
653   res = gst_osx_audio_sink_allowed_caps (osxsink);
654
655   return res;
656 }