2 * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osx_write.c paraslash's output plugin for MacOs */
10 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
11 * <skortze@sourceforge.net>
15 #include <sys/types.h>
23 #include "buffer_tree.h"
25 #include "write_common.h"
26 #include "osx_write.cmdline.h"
30 #include <CoreServices/CoreServices.h>
31 #include <AudioUnit/AudioUnit.h>
32 #include <AudioToolbox/AudioToolbox.h>
34 /** Data specific to the osx writer. */
35 struct private_osx_write_data {
36 /** The main CoreAudio handle. */
38 /** True if we wrote some audio data. */
40 /** Sample rate of the current audio stream. */
42 /** Sample format of the current audio stream */
43 unsigned sample_format;
44 /** Number of channels of the current audio stream. */
46 /** Serializes access to buffer tree nodes. */
48 /** The btr node of the callback. */
49 struct btr_node *callback_btrn;
52 /* This function writes the address and the number of bytes to one end of the socket.
53 * The post_select() function then fills the buffer and notifies the callback also
56 static OSStatus osx_callback(void *cb_arg, __a_unused AudioUnitRenderActionFlags *af,
57 __a_unused const AudioTimeStamp *ts, __a_unused UInt32 bus_number,
58 __a_unused UInt32 num_frames, AudioBufferList *abl)
61 struct writer_node *wn = cb_arg;
62 struct private_osx_write_data *powd = wn->private_data;
63 size_t samples_have, samples_want = 0;
65 mutex_lock(powd->mutex);
67 * We fill with zeros if no data was yet written and we do not have
68 * enough to fill all buffers.
71 size_t want = 0, have =
72 btr_get_input_queue_size(powd->callback_btrn);
73 for (i = 0; i < abl->mNumberBuffers; i++)
74 want += abl->mBuffers[i].mDataByteSize;
76 PARA_DEBUG_LOG("deferring playback (have = %zu < %zu = want)\n",
78 for (i = 0; i < abl->mNumberBuffers; i++)
79 memset(abl->mBuffers[i].mData, 0,
80 abl->mBuffers[i].mDataByteSize);
86 for (i = 0; i < abl->mNumberBuffers; i++) {
87 /* what we have to fill */
88 void *dest = abl->mBuffers[i].mData;
89 size_t sz = abl->mBuffers[i].mDataByteSize, samples, bytes;
91 samples_want = sz / wn->min_iqs;
92 while (samples_want > 0) {
94 btr_merge(powd->callback_btrn, wn->min_iqs);
95 samples_have = btr_next_buffer(powd->callback_btrn, &buf) / wn->min_iqs;
96 //PARA_INFO_LOG("i: %d want %zu samples to addr %p, have: %zu\n", i, samples_want,
97 // dest, samples_have);
98 samples = PARA_MIN(samples_have, samples_want);
101 bytes = samples * wn->min_iqs;
102 memcpy(dest, buf, bytes);
103 btr_consume(powd->callback_btrn, bytes);
104 samples_want -= samples;
107 if (samples_want == 0)
109 if (btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF) >= 0)
110 PARA_INFO_LOG("zero-padding (%zu samples)\n",
112 memset(dest, 0, samples_want * wn->min_iqs);
116 mutex_unlock(powd->mutex);
120 static int core_audio_init(struct writer_node *wn)
122 struct private_osx_write_data *powd = para_calloc(sizeof(*powd));
126 AURenderCallbackStruct input_callback;
127 ComponentDescription desc = {
128 .componentType = kAudioUnitType_Output,
129 .componentSubType = kAudioUnitSubType_DefaultOutput,
130 .componentManufacturer = kAudioUnitManufacturer_Apple,
132 AudioStreamBasicDescription format = {
133 .mFormatID = kAudioFormatLinearPCM,
134 .mFramesPerPacket = 1,
136 struct btr_node *btrn = wn->btrn;
137 struct btr_node_description bnd;
139 PARA_INFO_LOG("wn: %p\n", wn);
140 ret = -E_DEFAULT_COMP;
141 comp = FindNextComponent(NULL, &desc);
145 if (OpenAComponent(comp, &powd->audio_unit))
148 if (AudioUnitInitialize(powd->audio_unit))
150 get_btr_sample_rate(btrn, &val);
151 powd->sample_rate = val;
152 get_btr_channels(btrn, &val);
153 powd->channels = val;
154 get_btr_sample_format(btrn, &val);
155 powd->sample_format = val;
157 * Choose PCM format. We tell the Output Unit what format we're going
158 * to supply data to it. This is necessary if you're providing data
159 * through an input callback AND you want the DefaultOutputUnit to do
160 * any format conversions necessary from your format to the device's
164 format.mSampleRate = powd->sample_rate;
165 format.mChannelsPerFrame = powd->channels;
167 switch (powd->sample_format) {
170 wn->min_iqs = powd->channels;
171 format.mBitsPerChannel = 8;
172 format.mBytesPerPacket = powd->channels;
173 format.mFormatFlags |= kLinearPCMFormatFlagIsPacked;
176 wn->min_iqs = powd->channels * 2;
177 format.mBytesPerPacket = powd->channels * 2;
178 format.mBitsPerChannel = 16;
179 format.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
181 format.mBytesPerFrame = format.mBytesPerPacket;
183 if (powd->sample_format == SF_S16_BE || powd->sample_format == SF_U16_BE)
184 format.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
186 input_callback = (AURenderCallbackStruct){osx_callback, wn};
187 ret = -E_STREAM_FORMAT;
188 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
189 kAudioUnitScope_Input, 0, &format, sizeof(format)))
191 ret = -E_ADD_CALLBACK;
192 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
193 kAudioUnitScope_Input, 0, &input_callback,
194 sizeof(input_callback)) < 0)
201 /* set up callback btr node */
202 bnd.name = "cb_node";
207 powd->callback_btrn = btr_new_node(&bnd);
208 wn->private_data = powd;
211 AudioUnitUninitialize(powd->audio_unit);
213 CloseComponent(powd->audio_unit);
216 wn->private_data = NULL;
220 __malloc static void *osx_write_parse_config_or_die(int argc, char **argv)
222 struct osx_write_args_info *conf = para_calloc(sizeof(*conf));
224 /* exits on errors */
225 osx_write_cmdline_parser(argc, argv, conf);
229 static void osx_free_config(void *conf)
231 osx_write_cmdline_parser_free(conf);
234 static void osx_write_close(struct writer_node *wn)
236 struct private_osx_write_data *powd = wn->private_data;
240 PARA_INFO_LOG("closing writer node %p\n", wn);
241 mutex_destroy(powd->mutex);
243 wn->private_data = NULL;
246 /* must be called with the mutex held */
247 static inline bool need_drain_delay(struct private_osx_write_data *powd)
251 return btr_get_input_queue_size(powd->callback_btrn) != 0;
254 static void osx_write_pre_select(struct sched *s, struct task *t)
256 struct writer_node *wn = container_of(t, struct writer_node, task);
257 struct private_osx_write_data *powd = wn->private_data;
259 bool drain_delay_nec = false;
262 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
268 mutex_lock(powd->mutex);
269 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
271 drain_delay_nec = need_drain_delay(powd);
272 mutex_unlock(powd->mutex);
275 return sched_request_timeout_ms(50, s);
277 return sched_min_delay(s);
278 sched_request_timeout_ms(50, s);
281 static void osx_write_post_select(__a_unused struct sched *s, struct task *t)
283 struct writer_node *wn = container_of(t, struct writer_node, task);
284 struct private_osx_write_data *powd = wn->private_data;
285 struct btr_node *btrn = wn->btrn;
288 ret = task_get_notification(t);
292 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
297 ret = core_audio_init(wn);
300 powd = wn->private_data;
301 AudioOutputUnitStart(powd->audio_unit);
303 mutex_lock(powd->mutex);
305 ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
306 if (ret < 0 && need_drain_delay(powd))
308 mutex_unlock(powd->mutex);
313 if (powd && powd->callback_btrn) {
314 AudioOutputUnitStop(powd->audio_unit);
315 AudioUnitUninitialize(powd->audio_unit);
316 CloseComponent(powd->audio_unit);
317 btr_remove_node(&powd->callback_btrn);
319 btr_remove_node(&wn->btrn);
320 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
325 * The init function of the osx writer.
327 * \param w Filled in by the function.
329 void osx_write_init(struct writer *w)
331 struct osx_write_args_info dummy;
333 osx_write_cmdline_parser_init(&dummy);
334 w->close = osx_write_close;
335 w->pre_select = osx_write_pre_select;
336 w->post_select = osx_write_post_select;
337 w->parse_config_or_die = osx_write_parse_config_or_die;
338 w->free_config = osx_free_config;
339 w->help = (struct ggo_help) {
340 .short_help = osx_write_args_info_help,
341 .detailed_help = osx_write_args_info_detailed_help
343 osx_write_cmdline_parser_free(&dummy);