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 * @title: osxaudiosink
53 *
54 * This element renders raw audio samples using the CoreAudio api.
55 *
56 * ## Example pipelines
57 * |[
58 * gst-launch-1.0 filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! osxaudiosink
59 * ]| Play an Ogg/Vorbis file.
60 *
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 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_PAD_SINK,
98 GST_PAD_ALWAYS,
99 GST_STATIC_CAPS (GST_OSX_AUDIO_SINK_CAPS)
100 );
101
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);
106
107 static GstStateChangeReturn
108 gst_osx_audio_sink_change_state (GstElement * element,
109 GstStateChange transition);
110
111 static gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
112
113 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
114 GstCaps * filter);
115 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
116 GstCaps * caps);
117
118 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
119 GstBuffer * buf);
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);
125
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);
130
131 static void
gst_osx_audio_sink_do_init(GType type)132 gst_osx_audio_sink_do_init (GType type)
133 {
134 static const GInterfaceInfo osxelement_info = {
135 gst_osx_audio_sink_osxelement_init,
136 NULL,
137 NULL
138 };
139
140 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
141 "OSX Audio Sink");
142 gst_core_audio_init_debug ();
143 GST_DEBUG ("Adding static interface");
144 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
145 &osxelement_info);
146 }
147
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));
151
152 static void
gst_osx_audio_sink_class_init(GstOsxAudioSinkClass * klass)153 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
154 {
155 GObjectClass *gobject_class;
156 GstElementClass *gstelement_class;
157 GstBaseSinkClass *gstbasesink_class;
158 GstAudioBaseSinkClass *gstaudiobasesink_class;
159
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
162 gstbasesink_class = (GstBaseSinkClass *) klass;
163 gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
164
165 parent_class = g_type_class_peek_parent (klass);
166
167 gobject_class->set_property = gst_osx_audio_sink_set_property;
168 gobject_class->get_property = gst_osx_audio_sink_get_property;
169
170 gstelement_class->change_state =
171 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_change_state);
172
173 #ifndef HAVE_IOS
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));
177 #endif
178
179 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
180
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));
184
185 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps);
186
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);
191
192 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
193
194 gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (macOS)",
195 "Sink/Audio",
196 "Output to a sound card on macOS",
197 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
198 }
199
200 static void
gst_osx_audio_sink_init(GstOsxAudioSink * sink)201 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
202 {
203 GST_DEBUG ("Initialising object");
204
205 sink->device_id = kAudioDeviceUnknown;
206 sink->volume = DEFAULT_VOLUME;
207 }
208
209 static void
gst_osx_audio_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)210 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
211 const GValue * value, GParamSpec * pspec)
212 {
213 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
214
215 switch (prop_id) {
216 #ifndef HAVE_IOS
217 case ARG_DEVICE:
218 sink->device_id = g_value_get_int (value);
219 break;
220 #endif
221 case ARG_VOLUME:
222 sink->volume = g_value_get_double (value);
223 gst_osx_audio_sink_set_volume (sink);
224 break;
225 default:
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 break;
228 }
229 }
230
231 static GstStateChangeReturn
gst_osx_audio_sink_change_state(GstElement * element,GstStateChange transition)232 gst_osx_audio_sink_change_state (GstElement * element,
233 GstStateChange transition)
234 {
235 GstOsxAudioSink *osxsink = GST_OSX_AUDIO_SINK (element);
236 GstOsxAudioRingBuffer *ringbuffer;
237 GstStateChangeReturn ret;
238
239 switch (transition) {
240 default:
241 break;
242 }
243
244 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
245 if (ret == GST_STATE_CHANGE_FAILURE)
246 goto out;
247
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);
252
253 /* The device is open now, so fix our device_id if it changed */
254 ringbuffer =
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");
259 }
260 break;
261
262 default:
263 break;
264 }
265
266 out:
267 return ret;
268 }
269
270 static void
gst_osx_audio_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)271 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
272 GValue * value, GParamSpec * pspec)
273 {
274 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
275 switch (prop_id) {
276 #ifndef HAVE_IOS
277 case ARG_DEVICE:
278 g_value_set_int (value, sink->device_id);
279 break;
280 #endif
281 case ARG_VOLUME:
282 g_value_set_double (value, sink->volume);
283 break;
284 default:
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 break;
287 }
288 }
289
290 static gboolean
gst_osx_audio_sink_query(GstBaseSink * base,GstQuery * query)291 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
292 {
293 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
294 gboolean ret = FALSE;
295
296 switch (GST_QUERY_TYPE (query)) {
297 case GST_QUERY_ACCEPT_CAPS:
298 {
299 GstCaps *caps = NULL;
300
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);
304 ret = TRUE;
305 break;
306 }
307 default:
308 ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
309 break;
310 }
311 return ret;
312 }
313
314 static GstCaps *
gst_osx_audio_sink_getcaps(GstBaseSink * sink,GstCaps * filter)315 gst_osx_audio_sink_getcaps (GstBaseSink * sink, GstCaps * filter)
316 {
317 GstOsxAudioSink *osxsink;
318 GstAudioRingBuffer *buf;
319 GstOsxAudioRingBuffer *osxbuf;
320 GstCaps *caps, *filtered_caps;
321
322 osxsink = GST_OSX_AUDIO_SINK (sink);
323
324 GST_OBJECT_LOCK (osxsink);
325 buf = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
326 if (buf)
327 gst_object_ref (buf);
328 GST_OBJECT_UNLOCK (osxsink);
329
330 if (!buf) {
331 GST_DEBUG_OBJECT (sink, "no ring buffer, returning NULL caps");
332 return GST_BASE_SINK_CLASS (parent_class)->get_caps (sink, filter);
333 }
334
335 osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
336
337 /* protect against cached_caps going away */
338 GST_OBJECT_LOCK (buf);
339
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;
345
346 /* Get template caps */
347 template_caps =
348 gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (osxsink));
349
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);
353
354 gst_caps_unref (template_caps);
355 } else {
356 GST_DEBUG_OBJECT (sink, "ring buffer not open, returning NULL caps");
357 caps = NULL;
358 }
359
360 GST_OBJECT_UNLOCK (buf);
361
362 gst_object_unref (buf);
363
364 if (!caps)
365 return NULL;
366
367 if (!filter)
368 return caps;
369
370 /* Take care of filtered caps */
371 filtered_caps =
372 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
373 gst_caps_unref (caps);
374 return filtered_caps;
375 }
376
377 static gboolean
gst_osx_audio_sink_acceptcaps(GstOsxAudioSink * sink,GstCaps * caps)378 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
379 {
380 GstCaps *pad_caps;
381 GstStructure *st;
382 gboolean ret = FALSE;
383 GstAudioRingBufferSpec spec = { 0 };
384 gchar *caps_string = NULL;
385
386 caps_string = gst_caps_to_string (caps);
387 GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
388 g_free (caps_string);
389
390 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
391 if (pad_caps) {
392 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
393 gst_caps_unref (pad_caps);
394 if (!cret)
395 goto done;
396 }
397
398 /* If we've not got fixed caps, creating a stream might fail,
399 * so let's just return from here with default acceptcaps
400 * behaviour */
401 if (!gst_caps_is_fixed (caps))
402 goto done;
403
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))
408 goto done;
409
410 /* Make sure input is framed and can be payloaded */
411 switch (spec.type) {
412 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
413 {
414 gboolean framed = FALSE;
415
416 st = gst_caps_get_structure (caps, 0);
417
418 gst_structure_get_boolean (st, "framed", &framed);
419 if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
420 goto done;
421 break;
422 }
423 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
424 {
425 gboolean parsed = FALSE;
426
427 st = gst_caps_get_structure (caps, 0);
428
429 gst_structure_get_boolean (st, "parsed", &parsed);
430 if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
431 goto done;
432 break;
433 }
434 default:
435 break;
436 }
437 ret = TRUE;
438
439 done:
440 return ret;
441 }
442
443 static GstBuffer *
gst_osx_audio_sink_sink_payload(GstAudioBaseSink * sink,GstBuffer * buf)444 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
445 {
446 if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
447 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
448 GstBuffer *out;
449 GstMapInfo inmap, outmap;
450 gboolean res;
451
452 if (framesize <= 0)
453 return NULL;
454
455 out = gst_buffer_new_and_alloc (framesize);
456
457 gst_buffer_map (buf, &inmap, GST_MAP_READ);
458 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
459
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);
463
464 gst_buffer_unmap (buf, &inmap);
465 gst_buffer_unmap (out, &outmap);
466
467 if (!res) {
468 gst_buffer_unref (out);
469 return NULL;
470 }
471
472 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
473 return out;
474
475 } else {
476 return gst_buffer_ref (buf);
477 }
478 }
479
480 static GstAudioRingBuffer *
gst_osx_audio_sink_create_ringbuffer(GstAudioBaseSink * sink)481 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
482 {
483 GstOsxAudioSink *osxsink;
484 GstOsxAudioRingBuffer *ringbuffer;
485
486 osxsink = GST_OSX_AUDIO_SINK (sink);
487
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);
493
494 ringbuffer->core_audio->element =
495 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
496 ringbuffer->core_audio->is_src = FALSE;
497
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
501 */
502 if (ringbuffer->core_audio->device_id != osxsink->device_id)
503 ringbuffer->core_audio->device_id = osxsink->device_id;
504
505 return GST_AUDIO_RING_BUFFER (ringbuffer);
506 }
507
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 */
512 static OSStatus
gst_osx_audio_sink_io_proc(GstOsxAudioRingBuffer * buf,AudioUnitRenderActionFlags * ioActionFlags,const AudioTimeStamp * inTimeStamp,UInt32 inBusNumber,UInt32 inNumberFrames,AudioBufferList * bufferList)513 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
514 AudioUnitRenderActionFlags * ioActionFlags,
515 const AudioTimeStamp * inTimeStamp,
516 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
517 {
518 guint8 *readptr;
519 gint readseg;
520 gint len;
521 gint stream_idx = buf->core_audio->stream_idx;
522 gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
523 gint offset = 0;
524
525 while (remaining) {
526 if (!gst_audio_ring_buffer_prepare_read (GST_AUDIO_RING_BUFFER (buf),
527 &readseg, &readptr, &len))
528 return 0;
529
530 len -= buf->segoffset;
531
532 if (len > remaining)
533 len = remaining;
534
535 memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
536 readptr + buf->segoffset, len);
537
538 buf->segoffset += len;
539 offset += len;
540 remaining -= len;
541
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);
545
546 /* we wrote one segment */
547 gst_audio_ring_buffer_advance (GST_AUDIO_RING_BUFFER (buf), 1);
548
549 buf->segoffset = 0;
550 }
551 }
552 return 0;
553 }
554
555 static void
gst_osx_audio_sink_osxelement_init(gpointer g_iface,gpointer iface_data)556 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
557 {
558 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
559
560 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
561 }
562
563 static void
gst_osx_audio_sink_set_volume(GstOsxAudioSink * sink)564 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
565 {
566 GstOsxAudioRingBuffer *osxbuf;
567
568 osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
569 if (!osxbuf)
570 return;
571
572 gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
573 }
574