4656b9d2f22ef16bf23decc41d26371b6e0bc024
[paraslash.git] / osx_write.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file osx_write.c paraslash's output plugin for MacOs */
20
21 /*
22 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
23 * <skortze@sourceforge.net>
24 */
25
26 #include <CoreAudio/CoreAudio.h>
27 #include "para.h"
28 #include "fd.h"
29 #include "string.h"
30 #include "list.h"
31 #include "sched.h"
32 #include "write.h"
33 #include "osx_write.cmdline.h"
34 #include "error.h"
35
36
37 #include <CoreAudio/CoreAudio.h>
38 #include <AudioUnit/AudioUnit.h>
39 #include <AudioToolbox/DefaultAudioOutput.h>
40 struct osx_buffer {
41 float *buffer;
42 long size;
43 float *ptr; /* Where in the buffer are we? */
44 long remaining;
45 struct osx_buffer *next;
46 };
47 typedef struct osx_buffer osx_buffer; /* FIXME */
48
49 struct private_osx_write_data {
50 long size;
51 AudioUnit output;
52 char play;
53 osx_buffer *from; /* Current buffers */
54 osx_buffer *to;
55 unsigned samplerate;
56 unsigned channels;
57 };
58
59
60 /*
61 * Tried with 3 buffers, but then any little window move is sufficient to
62 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
63 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
64 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
65 * buffers don't work much further than the Bus error.
66 */
67 #define NUMBER_BUFFERS 2
68
69 static void destroy_buffers(struct private_osx_write_data *powd)
70 {
71 osx_buffer *ptr;
72 osx_buffer *ptr2;
73 ptr = powd->to->next;
74 powd->to->next = NULL;
75 while (ptr) {
76 ptr2 = ptr->next;
77 free(ptr->buffer);
78 free(ptr);
79 ptr = ptr2;
80 }
81 }
82
83 static void init_buffers(struct private_osx_write_data *powd)
84 {
85 int i;
86
87 osx_buffer ** ptrptr;
88 ptrptr = &powd->to;
89 for (i = 0; i < NUMBER_BUFFERS; i++) {
90 *ptrptr = malloc(sizeof(osx_buffer));
91 (*ptrptr)->size = 0;
92 (*ptrptr)->remaining = 0;
93 (*ptrptr)->buffer = NULL;
94 ptrptr = &(*ptrptr)->next;
95 }
96 *ptrptr = powd->from = powd->to;
97 }
98
99 static void fill_buffer(osx_buffer *b, short *source, long size)
100 {
101 float *dest;
102
103 if (b->remaining) /* Non empty buffer, must still be playing */
104 return;
105 if (b->size != size) {
106 b->buffer = para_realloc(b->buffer, size * sizeof(float));
107 b->size = size;
108 }
109 dest = b->buffer;
110 while (size--) {
111 char *tmp = (char *)source;
112 char c = *tmp;
113 *tmp = *(tmp + 1);
114 *(tmp + 1) = c;
115 /* *dest++ = ((*source++) + 32768) / 65536.0; */
116 *dest++ = (*source++) / 32768.0;
117 }
118 b->ptr = b->buffer;
119 b->remaining = b->size;
120 }
121
122 static OSStatus osx_callback(void * inClientData,
123 __a_unused AudioUnitRenderActionFlags *inActionFlags,
124 __a_unused const AudioTimeStamp *inTimeStamp,
125 __a_unused UInt32 inBusNumber,
126 __a_unused UInt32 inNumFrames,
127 AudioBufferList *outOutputData)
128 {
129 long m, n;
130 float *dest;
131 int i;
132 struct private_osx_write_data *powd = inClientData;
133
134 // PARA_INFO_LOG("%p\n", powd);
135 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
136 /* what we have to fill */
137 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
138 dest = outOutputData->mBuffers[i].mData;
139 while (m > 0) {
140 if ((n = powd->from->remaining) <= 0) {
141 PARA_INFO_LOG("%s", "buffer underrun\n");
142 /* no more bytes in the current read buffer! */
143 while ((n = powd->from->remaining) <= 0)
144 /* wait for the results */
145 usleep(2000);
146 }
147 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
148 /*
149 * we dump what we can. In fact, just the necessary
150 * should be sufficient
151 */
152 if (n > m)
153 n = m;
154 memcpy(dest, powd->from->ptr, n * sizeof(float));
155 dest += n;
156 /* remember all done work */
157 m -= n;
158 powd->from->ptr += n;
159 if ((powd->from->remaining -= n) <= 0)
160 powd->from = powd->from->next;
161 }
162 }
163 return 0;
164 }
165
166 static int osx_write_open(struct writer_node *wn)
167 {
168 struct private_osx_write_data *powd = para_calloc(
169 sizeof(struct private_osx_write_data));
170 ComponentDescription desc;
171 Component comp;
172 AURenderCallbackStruct inputCallback = {osx_callback, powd};
173 AudioStreamBasicDescription format;
174 int ret;
175 struct writer_node_group *wng = wn->wng;
176 struct osx_write_args_info *conf = wn->conf;
177
178 wn->private_data = powd;
179 /* where did that default audio output go? */
180 desc.componentType = kAudioUnitType_Output;
181 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
182 /* NOTE: and if default output isn't Apple? */
183 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
184 desc.componentFlags = 0;
185 desc.componentFlagsMask = 0;
186 ret = -E_DEFAULT_COMP;
187 comp = FindNextComponent(NULL, &desc);
188 if (!comp)
189 goto e0;
190 ret = -E_OPEN_COMP;
191 if (OpenAComponent(comp, &powd->output))
192 goto e0;
193 ret = -E_UNIT_INIT;
194 if (AudioUnitInitialize(powd->output))
195 goto e1;
196 powd->size = 0;
197 powd->play = 0;
198 /* Hmmm, let's choose PCM format */
199 /* We tell the Output Unit what format we're going to supply data to it.
200 * This is necessary if you're providing data through an input callback
201 * AND you want the DefaultOutputUnit to do any format conversions
202 * necessary from your format to the device's format.
203 */
204 if (!conf->samplerate_given && wng->samplerate)
205 powd->samplerate = *wng->samplerate;
206 else
207 powd->samplerate = conf->samplerate_arg;
208 format.mSampleRate = powd->samplerate;
209 /* The specific encoding type of audio stream*/
210 format.mFormatID = kAudioFormatLinearPCM;
211 /* flags specific to each format */
212 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
213 | kLinearPCMFormatFlagIsPacked
214 | kLinearPCMFormatFlagIsBigEndian;
215 if (!conf->channels_given && wng->channels)
216 powd->channels = *wng->channels;
217 else
218 powd->channels = conf->channels_arg;
219 format.mChannelsPerFrame = powd->channels;
220 format.mFramesPerPacket = 1;
221 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
222 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
223 /* one of the most constant constants of the whole computer history */
224 format.mBitsPerChannel = sizeof(float) * 8;
225 ret = -E_STREAM_FORMAT;
226 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
227 kAudioUnitScope_Input, 0, &format,
228 sizeof(AudioStreamBasicDescription)))
229 goto e2;
230 init_buffers(powd);
231 ret = -E_ADD_CALLBACK;
232 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
233 kAudioUnitScope_Input, 0, &inputCallback,
234 sizeof(inputCallback)) < 0)
235 goto e3;
236 return 1;
237 e3:
238 destroy_buffers(powd);
239 e2:
240 AudioUnitUninitialize(powd->output);
241 e1:
242 CloseComponent(powd->output);
243 e0:
244 return ret;
245 }
246
247 __malloc void *osx_write_parse_config(char *options)
248 {
249 struct osx_write_args_info *conf
250 = para_calloc(sizeof(struct osx_write_args_info));
251 PARA_INFO_LOG("options: %s\n", options);
252 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
253 if (ret)
254 goto err_out;
255 return conf;
256 err_out:
257 free(conf);
258 return NULL;
259
260 }
261
262 static void osx_write_close(struct writer_node *wn)
263 {
264 struct private_osx_write_data *powd = wn->private_data;
265
266 PARA_INFO_LOG("closing writer node %p\n", wn);
267 AudioOutputUnitStop(powd->output);
268 AudioUnitUninitialize(powd->output);
269 CloseComponent(powd->output);
270 destroy_buffers(powd);
271 free(powd);
272 }
273
274 static int need_new_buffer(struct writer_node *wn)
275 {
276 struct writer_node_group *wng = wn->wng;
277 struct private_osx_write_data *powd = wn->private_data;
278
279 if (*wng->loaded < sizeof(short))
280 return 0;
281 if (powd->to->remaining) /* Non empty buffer, must still be playing */
282 return 0;
283 return 1;
284 }
285
286 static int osx_write_post_select(__a_unused struct sched *s,
287 struct writer_node *wn)
288 {
289 struct private_osx_write_data *powd = wn->private_data;
290 struct writer_node_group *wng = wn->wng;
291 short *data = (short*)wng->buf;
292
293 if (!need_new_buffer(wn))
294 return 1;
295 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
296 powd->to = powd->to->next;
297 wn->written = *wng->loaded;
298 if (!powd->play) {
299 if (AudioOutputUnitStart(powd->output))
300 return -E_UNIT_START;
301 powd->play = 1;
302 }
303 return 1;
304 }
305
306 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
307 {
308 struct private_osx_write_data *powd = wn->private_data;
309 struct writer_node_group *wng = wn->wng;
310 size_t numbytes = powd->to->remaining * sizeof(short);
311 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
312 unsigned long divisor;
313
314 if (!numbytes && *wng->loaded >= sizeof(short))
315 goto min_delay; /* there's a buffer to fill */
316 if (!numbytes)
317 return 1;
318 divisor = powd->samplerate * powd->channels * 2 / numbytes;
319 if (divisor)
320 tv_divide(divisor, &tmp, &delay);
321 if (tv_diff(&s->timeout, &delay, NULL) > 0)
322 s->timeout = delay;
323 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
324 // (long unsigned) s->timeout.tv_usec);
325 return 1;
326 min_delay:
327 PARA_DEBUG_LOG("%s\n", "minimal delay");
328 s->timeout.tv_sec = 0;
329 s->timeout.tv_usec = 1;
330 return 1;
331 }
332
333 void osx_write_init(struct writer *w)
334 {
335 w->open = osx_write_open;
336 w->close = osx_write_close;
337 w->pre_select = osx_write_pre_select;
338 w->post_select = osx_write_post_select;
339 w->parse_config = osx_write_parse_config;
340 w->shutdown = NULL; /* nothing to do */
341 }