2 * Copyright (C) 2006-2010 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>
25 #include "buffer_tree.h"
27 #include "write_common.h"
28 #include "osx_write.cmdline.h"
31 #include <CoreServices/CoreServices.h>
32 #include <AudioUnit/AudioUnit.h>
33 #include <AudioToolbox/AudioToolbox.h>
35 /** describes one input buffer for the osx writer */
37 /** pointer to the beginning of the buffer */
39 /** the size of this buffer */
41 /** current position in the buffer */
43 /** number of floats not yet consuned */
45 /** pointer to the next audio buffer */
46 struct osx_buffer *next;
49 /** data specific to the osx writer */
50 struct private_osx_write_data {
51 /** the main control structure for audio data manipulation */
53 /** non-zero if playback has started */
55 /** callback reads audio data from this buffer */
56 struct osx_buffer *from;
57 /** the post_select writes audio data here */
58 struct osx_buffer *to;
59 /** sample rate of the current audio stream */
61 /** number of channels of the current audio stream */
65 static void destroy_buffers(struct private_osx_write_data *powd)
67 struct osx_buffer *ptr;
68 struct osx_buffer *ptr2;
70 powd->to->next = NULL;
79 static void init_buffers(struct writer_node *wn)
81 struct private_osx_write_data *powd = wn->private_data;
82 struct osx_write_args_info *conf = wn->conf;
83 struct osx_buffer **ptrptr;
87 for (i = 0; i < conf->numbuffers_arg; i++) {
88 *ptrptr = para_malloc(sizeof(struct osx_buffer));
90 (*ptrptr)->remaining = 0;
91 (*ptrptr)->buffer = NULL;
92 ptrptr = &(*ptrptr)->next;
94 *ptrptr = powd->from = powd->to;
97 static void fill_buffer(struct osx_buffer *b, short *source, long size)
101 assert(b->remaining == 0 || size > 0);
102 if (b->size != size) {
103 b->buffer = para_realloc(b->buffer, size * sizeof(float));
108 *dest++ = (*source++) / 32768.0;
110 b->remaining = b->size;
113 static OSStatus osx_callback(void * inClientData,
114 __a_unused AudioUnitRenderActionFlags *inActionFlags,
115 __a_unused const AudioTimeStamp *inTimeStamp,
116 __a_unused UInt32 inBusNumber,
117 __a_unused UInt32 inNumFrames,
118 AudioBufferList *outOutputData)
123 struct private_osx_write_data *powd = inClientData;
125 // PARA_INFO_LOG("%p\n", powd);
126 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
127 /* what we have to fill */
128 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
129 dest = outOutputData->mBuffers[i].mData;
131 n = powd->from->remaining;
133 n = powd->from->next->remaining;
135 PARA_INFO_LOG("buffer underrun\n");
138 powd->from = powd->from->next;
140 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
142 * we dump what we can. In fact, just the necessary
143 * should be sufficient
147 memcpy(dest, powd->from->ptr, n * sizeof(float));
149 /* remember all done work */
151 powd->from->ptr += n;
152 if ((powd->from->remaining -= n) <= 0)
153 powd->from = powd->from->next;
159 #ifdef WORDS_BIGENDIAN /* ppc */
160 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
162 #define ENDIAN_FLAGS 0
165 static int osx_write_open(struct writer_node *wn)
167 struct private_osx_write_data *powd = para_calloc(
168 sizeof(struct private_osx_write_data));
169 ComponentDescription desc;
171 AURenderCallbackStruct inputCallback = {osx_callback, powd};
172 AudioStreamBasicDescription format;
174 struct btr_node *btrn = wn->btrn;
177 wn->private_data = powd;
178 /* where did that default audio output go? */
179 desc.componentType = kAudioUnitType_Output;
180 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
181 /* NOTE: and if default output isn't Apple? */
182 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
183 desc.componentFlags = 0;
184 desc.componentFlagsMask = 0;
185 ret = -E_DEFAULT_COMP;
186 comp = FindNextComponent(NULL, &desc);
190 if (OpenAComponent(comp, &powd->audio_unit))
193 if (AudioUnitInitialize(powd->audio_unit))
196 get_btr_sample_rate(btrn, &val);
197 powd->sample_rate = val;
198 get_btr_channels(btrn, &val);
199 powd->channels = val;
201 * Choose PCM format. We tell the Output Unit what format we're going
202 * to supply data to it. This is necessary if you're providing data
203 * through an input callback AND you want the DefaultOutputUnit to do
204 * any format conversions necessary from your format to the device's
207 format.mSampleRate = powd->sample_rate;
208 /* The specific encoding type of audio stream */
209 format.mFormatID = kAudioFormatLinearPCM;
210 /* flags specific to each format */
211 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
212 | kLinearPCMFormatFlagIsPacked
214 format.mChannelsPerFrame = powd->channels;
215 format.mFramesPerPacket = 1;
216 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
217 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
218 /* one of the most constant constants of the whole computer history */
219 format.mBitsPerChannel = sizeof(float) * 8;
220 ret = -E_STREAM_FORMAT;
221 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
222 kAudioUnitScope_Input, 0, &format,
223 sizeof(AudioStreamBasicDescription)))
226 ret = -E_ADD_CALLBACK;
227 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
228 kAudioUnitScope_Input, 0, &inputCallback,
229 sizeof(inputCallback)) < 0)
231 wn->min_iqs = powd->channels * 2;
234 destroy_buffers(powd);
236 AudioUnitUninitialize(powd->audio_unit);
238 CloseComponent(powd->audio_unit);
243 __malloc static void *osx_write_parse_config(const char *options)
245 struct osx_write_args_info *conf
246 = para_calloc(sizeof(struct osx_write_args_info));
247 PARA_INFO_LOG("options: %s\n", options);
248 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
258 static void osx_free_config(void *conf)
260 osx_cmdline_parser_free(conf);
263 static void osx_write_close(struct writer_node *wn)
265 struct private_osx_write_data *powd = wn->private_data;
267 PARA_INFO_LOG("closing writer node %p\n", wn);
268 AudioOutputUnitStop(powd->audio_unit);
269 AudioUnitUninitialize(powd->audio_unit);
270 CloseComponent(powd->audio_unit);
271 destroy_buffers(powd);
275 static void osx_write_post_select(__a_unused struct sched *s, struct task *t)
277 struct writer_node *wn = container_of(t, struct writer_node, task);
278 struct private_osx_write_data *powd = wn->private_data;
279 struct btr_node *btrn = wn->btrn;
284 while (powd->to->remaining <= 0) {
285 ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
288 btr_merge(btrn, 8192);
289 bytes = btr_next_buffer(btrn, &data);
290 //PARA_CRIT_LOG("have: %zu\n", bytes);
291 fill_buffer(powd->to, (short *)data, bytes / sizeof(short));
292 btr_consume(btrn, bytes);
295 if (AudioOutputUnitStart(powd->audio_unit))
299 powd->to = powd->to->next;
302 btr_remove_node(btrn);
306 static void osx_write_pre_select(struct sched *s, struct task *t)
308 struct writer_node *wn = container_of(t, struct writer_node, task);
309 struct private_osx_write_data *powd = wn->private_data;
310 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
311 unsigned long divisor;
312 size_t numbytes = powd->to->remaining * sizeof(short);
313 int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
317 if (ret <= 0 || numbytes < wn->min_iqs)
319 divisor = powd->sample_rate * wn->min_iqs / numbytes;
321 tv_divide(divisor, &tmp, &delay);
322 sched_request_timeout(&delay, s);
325 /** the init function of the osx writer */
326 void osx_write_init(struct writer *w)
328 struct osx_write_args_info dummy;
330 osx_cmdline_parser_init(&dummy);
331 w->open = osx_write_open;
332 w->close = osx_write_close;
333 w->pre_select = osx_write_pre_select;
334 w->post_select = osx_write_post_select;
335 w->parse_config = osx_write_parse_config;
336 w->free_config = osx_free_config;
337 w->shutdown = NULL; /* nothing to do */
338 w->help = (struct ggo_help) {
339 .short_help = osx_write_args_info_help,
340 .detailed_help = osx_write_args_info_detailed_help
342 osx_cmdline_parser_free(&dummy);