2 * Copyright (C) 2006-2008 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>
14 #include <sys/types.h>
16 #include <CoreAudio/CoreAudio.h>
23 #include "osx_write.cmdline.h"
27 #include <CoreAudio/CoreAudio.h>
28 #include <AudioUnit/AudioUnit.h>
29 #include <AudioToolbox/DefaultAudioOutput.h>
31 /** describes one input buffer for the osx writer */
33 /** pointer to the beginning of the buffer */
35 /** the size of this buffer */
37 /** current position in the buffer */
39 /** number of floats not yet consuned */
41 /** pointer to the next audio buffer */
42 struct osx_buffer *next;
45 /** data specific to the osx writer */
46 struct private_osx_write_data {
47 /** the main control structure for audio data manipulation */
49 /** non-zero if playback has started */
51 /** callback reads audio data from this buffer */
52 struct osx_buffer *from;
53 /** the post_select writes audio data here */
54 struct osx_buffer *to;
55 /** sample rate of the current audio stream */
57 /** number of channels of the current audio stream */
61 static void destroy_buffers(struct private_osx_write_data *powd)
63 struct osx_buffer *ptr;
64 struct osx_buffer *ptr2;
66 powd->to->next = NULL;
75 static void init_buffers(struct writer_node *wn)
77 struct private_osx_write_data *powd = wn->private_data;
78 struct osx_write_args_info *conf = wn->conf;
79 struct osx_buffer **ptrptr;
83 for (i = 0; i < conf->numbuffers_arg; i++) {
84 *ptrptr = malloc(sizeof(struct osx_buffer));
86 (*ptrptr)->remaining = 0;
87 (*ptrptr)->buffer = NULL;
88 ptrptr = &(*ptrptr)->next;
90 *ptrptr = powd->from = powd->to;
93 static void fill_buffer(struct osx_buffer *b, short *source, long size)
97 if (b->remaining) /* Non empty buffer, must still be playing */
99 if (b->size != size) {
100 b->buffer = para_realloc(b->buffer, size * sizeof(float));
105 *dest++ = (*source++) / 32768.0;
107 b->remaining = b->size;
110 static OSStatus osx_callback(void * inClientData,
111 __a_unused AudioUnitRenderActionFlags *inActionFlags,
112 __a_unused const AudioTimeStamp *inTimeStamp,
113 __a_unused UInt32 inBusNumber,
114 __a_unused UInt32 inNumFrames,
115 AudioBufferList *outOutputData)
120 struct private_osx_write_data *powd = inClientData;
122 // PARA_INFO_LOG("%p\n", powd);
123 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
124 /* what we have to fill */
125 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
126 dest = outOutputData->mBuffers[i].mData;
128 if ((n = powd->from->remaining) <= 0) {
129 PARA_INFO_LOG("buffer underrun\n");
132 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
134 * we dump what we can. In fact, just the necessary
135 * should be sufficient
139 memcpy(dest, powd->from->ptr, n * sizeof(float));
141 /* remember all done work */
143 powd->from->ptr += n;
144 if ((powd->from->remaining -= n) <= 0)
145 powd->from = powd->from->next;
151 #ifdef WORDS_BIGENDIAN /* ppc */
152 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
154 #define ENDIAN_FLAGS 0
157 static int osx_write_open(struct writer_node *wn)
159 struct private_osx_write_data *powd = para_calloc(
160 sizeof(struct private_osx_write_data));
161 ComponentDescription desc;
163 AURenderCallbackStruct inputCallback = {osx_callback, powd};
164 AudioStreamBasicDescription format;
166 struct writer_node_group *wng = wn->wng;
167 struct osx_write_args_info *conf = wn->conf;
169 wn->private_data = powd;
170 /* where did that default audio output go? */
171 desc.componentType = kAudioUnitType_Output;
172 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
173 /* NOTE: and if default output isn't Apple? */
174 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
175 desc.componentFlags = 0;
176 desc.componentFlagsMask = 0;
177 ret = -E_DEFAULT_COMP;
178 comp = FindNextComponent(NULL, &desc);
182 if (OpenAComponent(comp, &powd->audio_unit))
185 if (AudioUnitInitialize(powd->audio_unit))
188 /* Hmmm, let's choose PCM format */
189 /* We tell the Output Unit what format we're going to supply data to it.
190 * This is necessary if you're providing data through an input callback
191 * AND you want the DefaultOutputUnit to do any format conversions
192 * necessary from your format to the device's format.
194 if (!conf->samplerate_given && wng->samplerate)
195 powd->samplerate = *wng->samplerate;
197 powd->samplerate = conf->samplerate_arg;
198 format.mSampleRate = powd->samplerate;
199 /* The specific encoding type of audio stream*/
200 format.mFormatID = kAudioFormatLinearPCM;
201 /* flags specific to each format */
202 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
203 | kLinearPCMFormatFlagIsPacked
205 if (!conf->channels_given && wng->channels)
206 powd->channels = *wng->channels;
208 powd->channels = conf->channels_arg;
209 format.mChannelsPerFrame = powd->channels;
210 format.mFramesPerPacket = 1;
211 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
212 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
213 /* one of the most constant constants of the whole computer history */
214 format.mBitsPerChannel = sizeof(float) * 8;
215 ret = -E_STREAM_FORMAT;
216 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
217 kAudioUnitScope_Input, 0, &format,
218 sizeof(AudioStreamBasicDescription)))
221 ret = -E_ADD_CALLBACK;
222 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
223 kAudioUnitScope_Input, 0, &inputCallback,
224 sizeof(inputCallback)) < 0)
228 destroy_buffers(powd);
230 AudioUnitUninitialize(powd->audio_unit);
232 CloseComponent(powd->audio_unit);
237 __malloc static void *osx_write_parse_config(const char *options)
239 struct osx_write_args_info *conf
240 = para_calloc(sizeof(struct osx_write_args_info));
241 PARA_INFO_LOG("options: %s\n", options);
242 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
252 static void osx_write_close(struct writer_node *wn)
254 struct private_osx_write_data *powd = wn->private_data;
256 PARA_INFO_LOG("closing writer node %p\n", wn);
257 AudioOutputUnitStop(powd->audio_unit);
258 AudioUnitUninitialize(powd->audio_unit);
259 CloseComponent(powd->audio_unit);
260 destroy_buffers(powd);
264 static int need_new_buffer(struct writer_node *wn)
266 struct writer_node_group *wng = wn->wng;
267 struct private_osx_write_data *powd = wn->private_data;
269 if (*wng->loaded < sizeof(short))
271 if (powd->to->remaining) /* Non empty buffer, must still be playing */
276 static int osx_write_post_select(__a_unused struct sched *s,
277 struct writer_node *wn)
279 struct private_osx_write_data *powd = wn->private_data;
280 struct writer_node_group *wng = wn->wng;
281 short *data = (short*)wng->buf;
283 if (!need_new_buffer(wn))
285 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
286 powd->to = powd->to->next;
287 wn->written = *wng->loaded;
289 if (AudioOutputUnitStart(powd->audio_unit))
290 return -E_UNIT_START;
296 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
298 struct private_osx_write_data *powd = wn->private_data;
299 struct writer_node_group *wng = wn->wng;
300 size_t numbytes = powd->to->remaining * sizeof(short);
301 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
302 unsigned long divisor;
304 if (!numbytes && *wng->loaded >= sizeof(short))
305 goto min_delay; /* there's a buffer to fill */
308 divisor = powd->samplerate * powd->channels * 2 / numbytes;
310 tv_divide(divisor, &tmp, &delay);
311 if (tv_diff(&s->timeout, &delay, NULL) > 0)
313 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
314 // (long unsigned) s->timeout.tv_usec);
317 PARA_DEBUG_LOG("%s\n", "minimal delay");
318 s->timeout.tv_sec = 0;
319 s->timeout.tv_usec = 1;
323 /** the init function of the osx writer */
324 void osx_write_init(struct writer *w)
326 w->open = osx_write_open;
327 w->close = osx_write_close;
328 w->pre_select = osx_write_pre_select;
329 w->post_select = osx_write_post_select;
330 w->parse_config = osx_write_parse_config;
331 w->shutdown = NULL; /* nothing to do */