2 * Copyright (C) 2006-2009 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 "osx_write.cmdline.h"
28 #include <CoreServices/CoreServices.h>
29 #include <AudioUnit/AudioUnit.h>
30 #include <AudioToolbox/AudioToolbox.h>
32 /** describes one input buffer for the osx writer */
34 /** pointer to the beginning of the buffer */
36 /** the size of this buffer */
38 /** current position in the buffer */
40 /** number of floats not yet consuned */
42 /** pointer to the next audio buffer */
43 struct osx_buffer *next;
46 /** data specific to the osx writer */
47 struct private_osx_write_data {
48 /** the main control structure for audio data manipulation */
50 /** non-zero if playback has started */
52 /** callback reads audio data from this buffer */
53 struct osx_buffer *from;
54 /** the post_select writes audio data here */
55 struct osx_buffer *to;
56 /** sample rate of the current audio stream */
58 /** number of channels of the current audio stream */
62 static void destroy_buffers(struct private_osx_write_data *powd)
64 struct osx_buffer *ptr;
65 struct osx_buffer *ptr2;
67 powd->to->next = NULL;
76 static void init_buffers(struct writer_node *wn)
78 struct private_osx_write_data *powd = wn->private_data;
79 struct osx_write_args_info *conf = wn->conf;
80 struct osx_buffer **ptrptr;
84 for (i = 0; i < conf->numbuffers_arg; i++) {
85 *ptrptr = para_malloc(sizeof(struct osx_buffer));
87 (*ptrptr)->remaining = 0;
88 (*ptrptr)->buffer = NULL;
89 ptrptr = &(*ptrptr)->next;
91 *ptrptr = powd->from = powd->to;
94 static void fill_buffer(struct osx_buffer *b, short *source, long size)
98 if (b->remaining) /* Non empty buffer, must still be playing */
100 if (b->size != size) {
101 b->buffer = para_realloc(b->buffer, size * sizeof(float));
106 *dest++ = (*source++) / 32768.0;
108 b->remaining = b->size;
111 static OSStatus osx_callback(void * inClientData,
112 __a_unused AudioUnitRenderActionFlags *inActionFlags,
113 __a_unused const AudioTimeStamp *inTimeStamp,
114 __a_unused UInt32 inBusNumber,
115 __a_unused UInt32 inNumFrames,
116 AudioBufferList *outOutputData)
121 struct private_osx_write_data *powd = inClientData;
123 // PARA_INFO_LOG("%p\n", powd);
124 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
125 /* what we have to fill */
126 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
127 dest = outOutputData->mBuffers[i].mData;
129 if ((n = powd->from->remaining) <= 0) {
130 PARA_INFO_LOG("buffer underrun\n");
133 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
135 * we dump what we can. In fact, just the necessary
136 * should be sufficient
140 memcpy(dest, powd->from->ptr, n * sizeof(float));
142 /* remember all done work */
144 powd->from->ptr += n;
145 if ((powd->from->remaining -= n) <= 0)
146 powd->from = powd->from->next;
152 #ifdef WORDS_BIGENDIAN /* ppc */
153 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
155 #define ENDIAN_FLAGS 0
158 static int osx_write_open(struct writer_node *wn)
160 struct private_osx_write_data *powd = para_calloc(
161 sizeof(struct private_osx_write_data));
162 ComponentDescription desc;
164 AURenderCallbackStruct inputCallback = {osx_callback, powd};
165 AudioStreamBasicDescription format;
167 struct writer_node_group *wng = wn->wng;
168 struct osx_write_args_info *conf = wn->conf;
170 wn->private_data = powd;
171 /* where did that default audio output go? */
172 desc.componentType = kAudioUnitType_Output;
173 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
174 /* NOTE: and if default output isn't Apple? */
175 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
176 desc.componentFlags = 0;
177 desc.componentFlagsMask = 0;
178 ret = -E_DEFAULT_COMP;
179 comp = FindNextComponent(NULL, &desc);
183 if (OpenAComponent(comp, &powd->audio_unit))
186 if (AudioUnitInitialize(powd->audio_unit))
189 /* Hmmm, let's choose PCM format */
190 /* We tell the Output Unit what format we're going to supply data to it.
191 * This is necessary if you're providing data through an input callback
192 * AND you want the DefaultOutputUnit to do any format conversions
193 * necessary from your format to the device's format.
195 if (!conf->samplerate_given && wng->samplerate)
196 powd->samplerate = *wng->samplerate;
198 powd->samplerate = conf->samplerate_arg;
199 format.mSampleRate = powd->samplerate;
200 /* The specific encoding type of audio stream*/
201 format.mFormatID = kAudioFormatLinearPCM;
202 /* flags specific to each format */
203 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
204 | kLinearPCMFormatFlagIsPacked
206 if (!conf->channels_given && wng->channels)
207 powd->channels = *wng->channels;
209 powd->channels = conf->channels_arg;
210 format.mChannelsPerFrame = powd->channels;
211 format.mFramesPerPacket = 1;
212 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
213 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
214 /* one of the most constant constants of the whole computer history */
215 format.mBitsPerChannel = sizeof(float) * 8;
216 ret = -E_STREAM_FORMAT;
217 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
218 kAudioUnitScope_Input, 0, &format,
219 sizeof(AudioStreamBasicDescription)))
222 ret = -E_ADD_CALLBACK;
223 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
224 kAudioUnitScope_Input, 0, &inputCallback,
225 sizeof(inputCallback)) < 0)
229 destroy_buffers(powd);
231 AudioUnitUninitialize(powd->audio_unit);
233 CloseComponent(powd->audio_unit);
238 __malloc static void *osx_write_parse_config(const char *options)
240 struct osx_write_args_info *conf
241 = para_calloc(sizeof(struct osx_write_args_info));
242 PARA_INFO_LOG("options: %s\n", options);
243 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
253 static void osx_write_close(struct writer_node *wn)
255 struct private_osx_write_data *powd = wn->private_data;
257 PARA_INFO_LOG("closing writer node %p\n", wn);
258 AudioOutputUnitStop(powd->audio_unit);
259 AudioUnitUninitialize(powd->audio_unit);
260 CloseComponent(powd->audio_unit);
261 destroy_buffers(powd);
265 static int need_new_buffer(struct writer_node *wn)
267 struct writer_node_group *wng = wn->wng;
268 struct private_osx_write_data *powd = wn->private_data;
270 if (*wng->loaded < sizeof(short))
272 if (powd->to->remaining) /* Non empty buffer, must still be playing */
277 static int osx_write_post_select(__a_unused struct sched *s,
278 struct writer_node *wn)
280 struct private_osx_write_data *powd = wn->private_data;
281 struct writer_node_group *wng = wn->wng;
282 short *data = (short*)*wng->bufp;
284 if (!need_new_buffer(wn))
286 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
287 powd->to = powd->to->next;
288 wn->written = *wng->loaded;
290 if (AudioOutputUnitStart(powd->audio_unit))
291 return -E_UNIT_START;
297 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
299 struct private_osx_write_data *powd = wn->private_data;
300 struct writer_node_group *wng = wn->wng;
301 size_t numbytes = powd->to->remaining * sizeof(short);
302 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
303 unsigned long divisor;
305 if (!numbytes && *wng->loaded >= sizeof(short))
306 goto min_delay; /* there's a buffer to fill */
309 divisor = powd->samplerate * powd->channels * 2 / numbytes;
311 tv_divide(divisor, &tmp, &delay);
312 if (tv_diff(&s->timeout, &delay, NULL) > 0)
314 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
315 // (long unsigned) s->timeout.tv_usec);
318 PARA_DEBUG_LOG("%s\n", "minimal delay");
319 s->timeout.tv_sec = 0;
320 s->timeout.tv_usec = 1;
324 /** the init function of the osx writer */
325 void osx_write_init(struct writer *w)
327 w->open = osx_write_open;
328 w->close = osx_write_close;
329 w->pre_select = osx_write_pre_select;
330 w->post_select = osx_write_post_select;
331 w->parse_config = osx_write_parse_config;
332 w->shutdown = NULL; /* nothing to do */