New codename for 0.4.
[paraslash.git] / osx_write.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file osx_write.c paraslash's output plugin for MacOs */
8
9 /*
10 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
11 * <skortze@sourceforge.net>
12 */
13
14 #include <sys/types.h>
15 #include <dirent.h>
16 #include <CoreAudio/CoreAudio.h>
17 #include "para.h"
18 #include "fd.h"
19 #include "string.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "write.h"
24 #include "osx_write.cmdline.h"
25 #include "error.h"
26
27
28 #include <CoreAudio/CoreAudio.h>
29 #include <AudioUnit/AudioUnit.h>
30 #include <AudioToolbox/DefaultAudioOutput.h>
31
32 /** describes one input buffer for the osx writer */
33 struct osx_buffer {
34 /** pointer to the beginning of the buffer */
35 float *buffer;
36 /** the size of this buffer */
37 long size;
38 /** current position in the buffer */
39 float *ptr;
40 /** number of floats not yet consuned */
41 long remaining;
42 /** pointer to the next audio buffer */
43 struct osx_buffer *next;
44 };
45
46 /** data specific to the osx writer */
47 struct private_osx_write_data {
48 /** the main control structure for audio data manipulation */
49 AudioUnit audio_unit;
50 /** non-zero if playback has started */
51 char play;
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 */
57 unsigned samplerate;
58 /** number of channels of the current audio stream */
59 unsigned channels;
60 };
61
62 static void destroy_buffers(struct private_osx_write_data *powd)
63 {
64 struct osx_buffer *ptr;
65 struct osx_buffer *ptr2;
66 ptr = powd->to->next;
67 powd->to->next = NULL;
68 while (ptr) {
69 ptr2 = ptr->next;
70 free(ptr->buffer);
71 free(ptr);
72 ptr = ptr2;
73 }
74 }
75
76 static void init_buffers(struct writer_node *wn)
77 {
78 struct private_osx_write_data *powd = wn->private_data;
79 struct osx_write_args_info *conf = wn->conf;
80 struct osx_buffer **ptrptr;
81 int i;
82
83 ptrptr = &powd->to;
84 for (i = 0; i < conf->numbuffers_arg; i++) {
85 *ptrptr = malloc(sizeof(struct osx_buffer));
86 (*ptrptr)->size = 0;
87 (*ptrptr)->remaining = 0;
88 (*ptrptr)->buffer = NULL;
89 ptrptr = &(*ptrptr)->next;
90 }
91 *ptrptr = powd->from = powd->to;
92 }
93
94 static void fill_buffer(struct osx_buffer *b, short *source, long size)
95 {
96 float *dest;
97
98 if (b->remaining) /* Non empty buffer, must still be playing */
99 return;
100 if (b->size != size) {
101 b->buffer = para_realloc(b->buffer, size * sizeof(float));
102 b->size = size;
103 }
104 dest = b->buffer;
105 while (size--)
106 *dest++ = (*source++) / 32768.0;
107 b->ptr = b->buffer;
108 b->remaining = b->size;
109 }
110
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)
117 {
118 long m, n;
119 float *dest;
120 int i;
121 struct private_osx_write_data *powd = inClientData;
122
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;
128 while (m > 0) {
129 if ((n = powd->from->remaining) <= 0) {
130 PARA_INFO_LOG("buffer underrun\n");
131 return 0;
132 }
133 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
134 /*
135 * we dump what we can. In fact, just the necessary
136 * should be sufficient
137 */
138 if (n > m)
139 n = m;
140 memcpy(dest, powd->from->ptr, n * sizeof(float));
141 dest += n;
142 /* remember all done work */
143 m -= n;
144 powd->from->ptr += n;
145 if ((powd->from->remaining -= n) <= 0)
146 powd->from = powd->from->next;
147 }
148 }
149 return 0;
150 }
151
152 #ifdef WORDS_BIGENDIAN /* ppc */
153 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
154 #else
155 #define ENDIAN_FLAGS 0
156 #endif
157
158 static int osx_write_open(struct writer_node *wn)
159 {
160 struct private_osx_write_data *powd = para_calloc(
161 sizeof(struct private_osx_write_data));
162 ComponentDescription desc;
163 Component comp;
164 AURenderCallbackStruct inputCallback = {osx_callback, powd};
165 AudioStreamBasicDescription format;
166 int ret;
167 struct writer_node_group *wng = wn->wng;
168 struct osx_write_args_info *conf = wn->conf;
169
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);
180 if (!comp)
181 goto e0;
182 ret = -E_OPEN_COMP;
183 if (OpenAComponent(comp, &powd->audio_unit))
184 goto e0;
185 ret = -E_UNIT_INIT;
186 if (AudioUnitInitialize(powd->audio_unit))
187 goto e1;
188 powd->play = 0;
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.
194 */
195 if (!conf->samplerate_given && wng->samplerate)
196 powd->samplerate = *wng->samplerate;
197 else
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
205 | ENDIAN_FLAGS;
206 if (!conf->channels_given && wng->channels)
207 powd->channels = *wng->channels;
208 else
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)))
220 goto e2;
221 init_buffers(wn);
222 ret = -E_ADD_CALLBACK;
223 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
224 kAudioUnitScope_Input, 0, &inputCallback,
225 sizeof(inputCallback)) < 0)
226 goto e3;
227 return 1;
228 e3:
229 destroy_buffers(powd);
230 e2:
231 AudioUnitUninitialize(powd->audio_unit);
232 e1:
233 CloseComponent(powd->audio_unit);
234 e0:
235 return ret;
236 }
237
238 __malloc static void *osx_write_parse_config(const char *options)
239 {
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");
244 if (ret)
245 goto err_out;
246 return conf;
247 err_out:
248 free(conf);
249 return NULL;
250
251 }
252
253 static void osx_write_close(struct writer_node *wn)
254 {
255 struct private_osx_write_data *powd = wn->private_data;
256
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);
262 free(powd);
263 }
264
265 static int need_new_buffer(struct writer_node *wn)
266 {
267 struct writer_node_group *wng = wn->wng;
268 struct private_osx_write_data *powd = wn->private_data;
269
270 if (*wng->loaded < sizeof(short))
271 return 0;
272 if (powd->to->remaining) /* Non empty buffer, must still be playing */
273 return 0;
274 return 1;
275 }
276
277 static int osx_write_post_select(__a_unused struct sched *s,
278 struct writer_node *wn)
279 {
280 struct private_osx_write_data *powd = wn->private_data;
281 struct writer_node_group *wng = wn->wng;
282 short *data = (short*)*wng->bufp;
283
284 if (!need_new_buffer(wn))
285 return 1;
286 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
287 powd->to = powd->to->next;
288 wn->written = *wng->loaded;
289 if (!powd->play) {
290 if (AudioOutputUnitStart(powd->audio_unit))
291 return -E_UNIT_START;
292 powd->play = 1;
293 }
294 return 1;
295 }
296
297 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
298 {
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;
304
305 if (!numbytes && *wng->loaded >= sizeof(short))
306 goto min_delay; /* there's a buffer to fill */
307 if (!numbytes)
308 return 1;
309 divisor = powd->samplerate * powd->channels * 2 / numbytes;
310 if (divisor)
311 tv_divide(divisor, &tmp, &delay);
312 if (tv_diff(&s->timeout, &delay, NULL) > 0)
313 s->timeout = delay;
314 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
315 // (long unsigned) s->timeout.tv_usec);
316 return 1;
317 min_delay:
318 PARA_DEBUG_LOG("%s\n", "minimal delay");
319 s->timeout.tv_sec = 0;
320 s->timeout.tv_usec = 1;
321 return 1;
322 }
323
324 /** the init function of the osx writer */
325 void osx_write_init(struct writer *w)
326 {
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 */
333 }