Debian Codegraph @

sys/osxaudio/gstosxaudiosink.c

raw
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 <[email protected]>
5 * Copyright (C) 2012 Fluendo S.A. <[email protected]>
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 ARG_UNIQUE_ID,
93 ARG_CONFIGURE_SESSION,
94 };
95
96 #define DEFAULT_VOLUME 1.0
97 #define DEFAULT_CONFIGURE_SESSION TRUE
98
99 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_PAD_SINK,
101 GST_PAD_ALWAYS,
102 GST_STATIC_CAPS (GST_OSX_AUDIO_SINK_CAPS)
103 );
104
105 static void gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
109 static void gst_osx_audio_sink_finalize (GObject * object);
110
111 static GstStateChangeReturn
112 gst_osx_audio_sink_change_state (GstElement * element,
113 GstStateChange transition);
114
115 static gboolean gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query);
116
117 static GstCaps *gst_osx_audio_sink_getcaps (GstBaseSink * base,
118 GstCaps * filter);
119 static gboolean gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink,
120 GstCaps * caps);
121
122 static GstBuffer *gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink,
123 GstBuffer * buf);
124 static GstAudioRingBuffer
125 * gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink);
126 static void gst_osx_audio_sink_osxelement_init (gpointer g_iface,
127 gpointer iface_data);
128 static void gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink);
129
130 static OSStatus gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
131 AudioUnitRenderActionFlags * ioActionFlags,
132 const AudioTimeStamp * inTimeStamp,
133 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList);
134
135 static void
136 gst_osx_audio_sink_do_init (GType type)
137 {
138 static const GInterfaceInfo osxelement_info = {
139 gst_osx_audio_sink_osxelement_init,
140 NULL,
141 NULL
142 };
143
144 GST_DEBUG_CATEGORY_INIT (osx_audiosink_debug, "osxaudiosink", 0,
145 "OSX Audio Sink");
146 gst_core_audio_init_debug ();
147 GST_DEBUG ("Adding static interface");
148 g_type_add_interface_static (type, GST_OSX_AUDIO_ELEMENT_TYPE,
149 &osxelement_info);
150 }
151
152 #define gst_osx_audio_sink_parent_class parent_class
153 G_DEFINE_TYPE_WITH_CODE (GstOsxAudioSink, gst_osx_audio_sink,
154 GST_TYPE_AUDIO_BASE_SINK, gst_osx_audio_sink_do_init (g_define_type_id));
155 GST_ELEMENT_REGISTER_DEFINE (osxaudiosink, "osxaudiosink", GST_RANK_PRIMARY,
156 GST_TYPE_OSX_AUDIO_SINK);
157
158 static void
159 gst_osx_audio_sink_class_init (GstOsxAudioSinkClass * klass)
160 {
161 GObjectClass *gobject_class;
162 GstElementClass *gstelement_class;
163 GstBaseSinkClass *gstbasesink_class;
164 GstAudioBaseSinkClass *gstaudiobasesink_class;
165
166 gobject_class = (GObjectClass *) klass;
167 gstelement_class = (GstElementClass *) klass;
168 gstbasesink_class = (GstBaseSinkClass *) klass;
169 gstaudiobasesink_class = (GstAudioBaseSinkClass *) klass;
170
171 parent_class = g_type_class_peek_parent (klass);
172
173 gobject_class->set_property = gst_osx_audio_sink_set_property;
174 gobject_class->get_property = gst_osx_audio_sink_get_property;
175 gobject_class->finalize = gst_osx_audio_sink_finalize;
176
177 gstelement_class->change_state =
178 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_change_state);
179
180 #if TARGET_OS_OSX
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
185 /**
186 * osxaudiosink:unique-id
187 *
188 * Unique persistent ID for the output device
189 *
190 * Since: 1.26
191 */
192 g_object_class_install_property (gobject_class, ARG_UNIQUE_ID,
193 g_param_spec_string ("unique-id", "Unique ID",
194 "Unique persistent ID for the input device",
195 NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
196 #else
197 /**
198 * GstOsxAudioSink:configure-session:
199 *
200 * Whether the app-wide AVAudioSession should be automatically set up for audio playback.
201 * This will set the category to AVAudioSessionCategoryPlayback and activate the session
202 * when the element goes to READY. No other settings will be changed.
203 *
204 * The category change will only occur if current category is the default one (SoloAmbient)
205 * to avoid setting the category 'lower' if an osxaudiosrc element is also running in the
206 * same process.
207 *
208 * If your application needs to configure anything more than the category, set this to FALSE
209 * for all osxaudiosink/src instances and handle the AVAudioSession setup yourself.
210 *
211 * Since: 1.26
212 */
213 g_object_class_install_property (gobject_class, ARG_CONFIGURE_SESSION,
214 g_param_spec_boolean ("configure-session",
215 "Enable automatic AVAudioSession setup",
216 "Whether the app-wide AVAudioSession should be automatically configured for audio playback",
217 DEFAULT_CONFIGURE_SESSION,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 #endif
220
221 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_query);
222
223 g_object_class_install_property (gobject_class, ARG_VOLUME,
224 g_param_spec_double ("volume", "Volume", "Volume of this stream",
225 0, 1.0, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226
227 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_osx_audio_sink_getcaps);
228
229 gstaudiobasesink_class->create_ringbuffer =
230 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_create_ringbuffer);
231 gstaudiobasesink_class->payload =
232 GST_DEBUG_FUNCPTR (gst_osx_audio_sink_sink_payload);
233
234 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
235
236 gst_element_class_set_static_metadata (gstelement_class, "Audio Sink (macOS)",
237 "Sink/Audio",
238 "Output to a sound card on macOS",
239 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
240 }
241
242 static void
243 gst_osx_audio_sink_init (GstOsxAudioSink * sink)
244 {
245 GST_DEBUG ("Initialising object");
246
247 sink->device_id = kAudioDeviceUnknown;
248 sink->volume = DEFAULT_VOLUME;
249
250 #if !TARGET_OS_OSX
251 sink->configure_session = DEFAULT_CONFIGURE_SESSION;
252 #endif
253 }
254
255 static void
256 gst_osx_audio_sink_finalize (GObject * object)
257 {
258 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
259 g_clear_pointer (&sink->unique_id, g_free);
260 G_OBJECT_CLASS (parent_class)->finalize (object);
261 }
262
263 static void
264 gst_osx_audio_sink_set_property (GObject * object, guint prop_id,
265 const GValue * value, GParamSpec * pspec)
266 {
267 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
268
269 switch (prop_id) {
270 #if TARGET_OS_OSX
271 case ARG_DEVICE:
272 sink->device_id = g_value_get_int (value);
273 break;
274 case ARG_UNIQUE_ID:
275 sink->unique_id = g_value_dup_string (value);
276 break;
277 #else
278 case ARG_CONFIGURE_SESSION:
279 sink->configure_session = g_value_get_boolean (value);
280 break;
281 #endif
282 case ARG_VOLUME:
283 sink->volume = g_value_get_double (value);
284 gst_osx_audio_sink_set_volume (sink);
285 break;
286 default:
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 break;
289 }
290 }
291
292 static GstStateChangeReturn
293 gst_osx_audio_sink_change_state (GstElement * element,
294 GstStateChange transition)
295 {
296 GstOsxAudioSink *osxsink = GST_OSX_AUDIO_SINK (element);
297 GstOsxAudioRingBuffer *ringbuffer;
298 GstStateChangeReturn ret;
299
300 switch (transition) {
301 case GST_STATE_CHANGE_READY_TO_NULL:
302 GST_OBJECT_LOCK (osxsink);
303 osxsink->device_id = kAudioDeviceUnknown;
304 g_clear_pointer (&osxsink->unique_id, g_free);
305 GST_OBJECT_UNLOCK (osxsink);
306 break;
307 default:
308 break;
309 }
310
311 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
312 if (ret == GST_STATE_CHANGE_FAILURE)
313 goto out;
314
315 switch (transition) {
316 case GST_STATE_CHANGE_NULL_TO_READY:
317 /* Device has been selected, AudioUnit set up, so initialize volume */
318 gst_osx_audio_sink_set_volume (osxsink);
319
320 /* The device is open now, so fix our device_id if it changed */
321 ringbuffer =
322 GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (osxsink)->ringbuffer);
323 if (ringbuffer->core_audio->device_id != osxsink->device_id) {
324 GST_OBJECT_LOCK (osxsink);
325 osxsink->device_id = ringbuffer->core_audio->device_id;
326 g_free (osxsink->unique_id);
327 osxsink->unique_id = g_strdup (ringbuffer->core_audio->unique_id);
328 GST_OBJECT_UNLOCK (osxsink);
329 g_object_notify (G_OBJECT (osxsink), "device");
330 }
331 break;
332
333 default:
334 break;
335 }
336
337 out:
338 return ret;
339 }
340
341 static void
342 gst_osx_audio_sink_get_property (GObject * object, guint prop_id,
343 GValue * value, GParamSpec * pspec)
344 {
345 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (object);
346 switch (prop_id) {
347 #if TARGET_OS_OSX
348 case ARG_DEVICE:
349 g_value_set_int (value, sink->device_id);
350 break;
351 case ARG_UNIQUE_ID:
352 g_value_set_string (value, sink->unique_id);
353 break;
354 #else
355 case ARG_CONFIGURE_SESSION:
356 g_value_set_boolean (value, sink->configure_session);
357 break;
358 #endif
359 case ARG_VOLUME:
360 g_value_set_double (value, sink->volume);
361 break;
362 default:
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 break;
365 }
366 }
367
368 static gboolean
369 gst_osx_audio_sink_query (GstBaseSink * base, GstQuery * query)
370 {
371 GstOsxAudioSink *sink = GST_OSX_AUDIO_SINK (base);
372 gboolean ret = FALSE;
373
374 switch (GST_QUERY_TYPE (query)) {
375 case GST_QUERY_ACCEPT_CAPS:
376 {
377 GstCaps *caps = NULL;
378
379 gst_query_parse_accept_caps (query, &caps);
380 ret = gst_osx_audio_sink_acceptcaps (sink, caps);
381 gst_query_set_accept_caps_result (query, ret);
382 ret = TRUE;
383 break;
384 }
385 default:
386 ret = GST_BASE_SINK_CLASS (parent_class)->query (base, query);
387 break;
388 }
389 return ret;
390 }
391
392 static GstCaps *
393 gst_osx_audio_sink_getcaps (GstBaseSink * sink, GstCaps * filter)
394 {
395 GstOsxAudioSink *osxsink;
396 GstAudioRingBuffer *buf;
397 GstOsxAudioRingBuffer *osxbuf;
398 GstCaps *caps, *filtered_caps;
399
400 osxsink = GST_OSX_AUDIO_SINK (sink);
401
402 GST_OBJECT_LOCK (osxsink);
403 buf = GST_AUDIO_BASE_SINK (sink)->ringbuffer;
404 if (buf)
405 gst_object_ref (buf);
406 GST_OBJECT_UNLOCK (osxsink);
407
408 if (!buf) {
409 GST_DEBUG_OBJECT (sink, "no ring buffer, returning NULL caps");
410 return GST_BASE_SINK_CLASS (parent_class)->get_caps (sink, filter);
411 }
412
413 osxbuf = GST_OSX_AUDIO_RING_BUFFER (buf);
414
415 /* protect against cached_caps going away */
416 GST_OBJECT_LOCK (buf);
417
418 if (osxbuf->core_audio->cached_caps_valid) {
419 GST_LOG_OBJECT (sink, "Returning cached caps");
420 caps = gst_caps_ref (osxbuf->core_audio->cached_caps);
421 } else if (buf->open) {
422 GstCaps *template_caps;
423
424 /* Get template caps */
425 template_caps =
426 gst_pad_get_pad_template_caps (GST_AUDIO_BASE_SINK_PAD (osxsink));
427
428 /* Device is open, let's probe its caps */
429 caps = gst_core_audio_probe_caps (osxbuf->core_audio, template_caps);
430 gst_caps_replace (&osxbuf->core_audio->cached_caps, caps);
431
432 gst_caps_unref (template_caps);
433 } else {
434 GST_DEBUG_OBJECT (sink, "ring buffer not open, returning NULL caps");
435 caps = NULL;
436 }
437
438 GST_OBJECT_UNLOCK (buf);
439
440 gst_object_unref (buf);
441
442 if (!caps)
443 return NULL;
444
445 if (!filter)
446 return caps;
447
448 /* Take care of filtered caps */
449 filtered_caps =
450 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
451 gst_caps_unref (caps);
452 return filtered_caps;
453 }
454
455 static gboolean
456 gst_osx_audio_sink_acceptcaps (GstOsxAudioSink * sink, GstCaps * caps)
457 {
458 GstCaps *pad_caps;
459 GstStructure *st;
460 gboolean ret = FALSE;
461 GstAudioRingBufferSpec spec = { 0 };
462 gchar *caps_string = NULL;
463
464 caps_string = gst_caps_to_string (caps);
465 GST_DEBUG_OBJECT (sink, "acceptcaps called with %s", caps_string);
466 g_free (caps_string);
467
468 pad_caps = gst_pad_query_caps (GST_BASE_SINK_PAD (sink), caps);
469 if (pad_caps) {
470 gboolean cret = gst_caps_can_intersect (pad_caps, caps);
471 gst_caps_unref (pad_caps);
472 if (!cret)
473 goto done;
474 }
475
476 /* If we've not got fixed caps, creating a stream might fail,
477 * so let's just return from here with default acceptcaps
478 * behaviour */
479 if (!gst_caps_is_fixed (caps))
480 goto done;
481
482 /* parse helper expects this set, so avoid nasty warning
483 * will be set properly later on anyway */
484 spec.latency_time = GST_SECOND;
485 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
486 goto done;
487
488 /* Make sure input is framed and can be payloaded */
489 switch (spec.type) {
490 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_AC3:
491 {
492 gboolean framed = FALSE;
493
494 st = gst_caps_get_structure (caps, 0);
495
496 gst_structure_get_boolean (st, "framed", &framed);
497 if (!framed || gst_audio_iec61937_frame_size (&spec) <= 0)
498 goto done;
499 break;
500 }
501 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_DTS:
502 {
503 gboolean parsed = FALSE;
504
505 st = gst_caps_get_structure (caps, 0);
506
507 gst_structure_get_boolean (st, "parsed", &parsed);
508 if (!parsed || gst_audio_iec61937_frame_size (&spec) <= 0)
509 goto done;
510 break;
511 }
512 default:
513 break;
514 }
515 ret = TRUE;
516
517 done:
518 return ret;
519 }
520
521 static GstBuffer *
522 gst_osx_audio_sink_sink_payload (GstAudioBaseSink * sink, GstBuffer * buf)
523 {
524 if (RINGBUFFER_IS_SPDIF (sink->ringbuffer->spec.type)) {
525 gint framesize = gst_audio_iec61937_frame_size (&sink->ringbuffer->spec);
526 GstBuffer *out;
527 GstMapInfo inmap, outmap;
528 gboolean res;
529
530 if (framesize <= 0)
531 return NULL;
532
533 out = gst_buffer_new_and_alloc (framesize);
534
535 gst_buffer_map (buf, &inmap, GST_MAP_READ);
536 gst_buffer_map (out, &outmap, GST_MAP_WRITE);
537
538 /* FIXME: the endianness needs to be queried and then set */
539 res = gst_audio_iec61937_payload (inmap.data, inmap.size,
540 outmap.data, outmap.size, &sink->ringbuffer->spec, G_BIG_ENDIAN);
541
542 gst_buffer_unmap (buf, &inmap);
543 gst_buffer_unmap (out, &outmap);
544
545 if (!res) {
546 gst_buffer_unref (out);
547 return NULL;
548 }
549
550 gst_buffer_copy_into (out, buf, GST_BUFFER_COPY_METADATA, 0, -1);
551 return out;
552
553 } else {
554 return gst_buffer_ref (buf);
555 }
556 }
557
558 static GstAudioRingBuffer *
559 gst_osx_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
560 {
561 GstOsxAudioSink *osxsink;
562 GstOsxAudioRingBuffer *ringbuffer;
563
564 osxsink = GST_OSX_AUDIO_SINK (sink);
565
566 GST_DEBUG_OBJECT (sink, "Creating ringbuffer");
567 ringbuffer = g_object_new (GST_TYPE_OSX_AUDIO_RING_BUFFER, NULL);
568 GST_DEBUG_OBJECT (sink, "osx sink %p element %p ioproc %p", osxsink,
569 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink),
570 (void *) gst_osx_audio_sink_io_proc);
571
572 ringbuffer->core_audio = g_object_new (GST_TYPE_CORE_AUDIO, "is-src", FALSE,
573 "device", osxsink->device_id, "unique-id", osxsink->unique_id,
574 #if !TARGET_OS_OSX
575 "configure-session", osxsink->configure_session,
576 #endif
577 NULL);
578 ringbuffer->core_audio->osxbuf = GST_OBJECT (ringbuffer);
579 ringbuffer->core_audio->element =
580 GST_OSX_AUDIO_ELEMENT_GET_INTERFACE (osxsink);
581
582 return GST_AUDIO_RING_BUFFER (ringbuffer);
583 }
584
585 /* HALOutput AudioUnit will request fairly arbitrarily-sized chunks
586 * of data, not of a fixed size. So, we keep track of where in
587 * the current ringbuffer segment we are, and only advance the segment
588 * once we've read the whole thing */
589 static OSStatus
590 gst_osx_audio_sink_io_proc (GstOsxAudioRingBuffer * buf,
591 AudioUnitRenderActionFlags * ioActionFlags,
592 const AudioTimeStamp * inTimeStamp,
593 UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList * bufferList)
594 {
595 guint8 *readptr;
596 gint readseg;
597 gint len;
598 gint stream_idx = buf->core_audio->stream_idx;
599 gint remaining = bufferList->mBuffers[stream_idx].mDataByteSize;
600 gint offset = 0;
601 GstAudioRingBuffer *rbuf = GST_AUDIO_RING_BUFFER (buf);
602 const GstAudioFormatInfo *finfo = rbuf->spec.info.finfo;
603
604 while (remaining) {
605 if (g_atomic_int_get (&buf->core_audio->io_proc_dropping)) {
606 gst_audio_format_info_fill_silence (finfo, (char *)
607 bufferList->mBuffers[stream_idx].mData + offset, remaining);
608 GST_TRACE_OBJECT (buf, "Wrote silence, %d samples", remaining);
609 return 0;
610 }
611
612 if (!gst_audio_ring_buffer_prepare_read (rbuf, &readseg, &readptr, &len))
613 return 0;
614
615 len -= buf->segoffset;
616
617 if (len > remaining)
618 len = remaining;
619
620 memcpy ((char *) bufferList->mBuffers[stream_idx].mData + offset,
621 readptr + buf->segoffset, len);
622
623 buf->segoffset += len;
624 offset += len;
625 remaining -= len;
626
627 if ((gint) buf->segoffset == rbuf->spec.segsize) {
628 /* clear written samples */
629 gst_audio_ring_buffer_clear (rbuf, readseg);
630
631 /* we wrote one segment */
632 CORE_AUDIO_TIMING_LOCK (buf->core_audio);
633 gst_audio_ring_buffer_advance (rbuf, 1);
634 /* FIXME: Update the timestamp and reported frames in smaller increments
635 * when the segment size is larger than the total inNumberFrames */
636 gst_core_audio_update_timing (buf->core_audio, inTimeStamp,
637 inNumberFrames);
638 CORE_AUDIO_TIMING_UNLOCK (buf->core_audio);
639
640 buf->segoffset = 0;
641 }
642 }
643 return 0;
644 }
645
646 static void
647 gst_osx_audio_sink_osxelement_init (gpointer g_iface, gpointer iface_data)
648 {
649 GstOsxAudioElementInterface *iface = (GstOsxAudioElementInterface *) g_iface;
650
651 iface->io_proc = (AURenderCallback) gst_osx_audio_sink_io_proc;
652 }
653
654 static void
655 gst_osx_audio_sink_set_volume (GstOsxAudioSink * sink)
656 {
657 GstOsxAudioRingBuffer *osxbuf;
658
659 osxbuf = GST_OSX_AUDIO_RING_BUFFER (GST_AUDIO_BASE_SINK (sink)->ringbuffer);
660 if (!osxbuf)
661 return;
662
663 gst_core_audio_set_volume (osxbuf->core_audio, sink->volume);
664 }