osx_write.c: Kill the typedef for struct osx_buffer
[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
48 struct private_osx_write_data {
49 long size;
50 AudioUnit output;
51 char play;
52 struct osx_buffer *from; /* Current buffers */
53 struct osx_buffer *to;
54 unsigned samplerate;
55 unsigned channels;
56 };
57
58
59 /*
60 * Tried with 3 buffers, but then any little window move is sufficient to
61 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
62 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
63 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
64 * buffers don't work much further than the Bus error.
65 */
66 #define NUMBER_BUFFERS 2
67
68 static void destroy_buffers(struct private_osx_write_data *powd)
69 {
70 struct osx_buffer *ptr;
71 struct osx_buffer *ptr2;
72 ptr = powd->to->next;
73 powd->to->next = NULL;
74 while (ptr) {
75 ptr2 = ptr->next;
76 free(ptr->buffer);
77 free(ptr);
78 ptr = ptr2;
79 }
80 }
81
82 static void init_buffers(struct private_osx_write_data *powd)
83 {
84 int i;
85
86 struct osx_buffer **ptrptr;
87 ptrptr = &powd->to;
88 for (i = 0; i < NUMBER_BUFFERS; i++) {
89 *ptrptr = malloc(sizeof(struct osx_buffer));
90 (*ptrptr)->size = 0;
91 (*ptrptr)->remaining = 0;
92 (*ptrptr)->buffer = NULL;
93 ptrptr = &(*ptrptr)->next;
94 }
95 *ptrptr = powd->from = powd->to;
96 }
97
98 static void fill_buffer(struct osx_buffer *b, short *source, long size)
99 {
100 float *dest;
101
102 if (b->remaining) /* Non empty buffer, must still be playing */
103 return;
104 if (b->size != size) {
105 b->buffer = para_realloc(b->buffer, size * sizeof(float));
106 b->size = size;
107 }
108 dest = b->buffer;
109 while (size--) {
110 char *tmp = (char *)source;
111 char c = *tmp;
112 *tmp = *(tmp + 1);
113 *(tmp + 1) = c;
114 /* *dest++ = ((*source++) + 32768) / 65536.0; */
115 *dest++ = (*source++) / 32768.0;
116 }
117 b->ptr = b->buffer;
118 b->remaining = b->size;
119 }
120
121 static OSStatus osx_callback(void * inClientData,
122 __a_unused AudioUnitRenderActionFlags *inActionFlags,
123 __a_unused const AudioTimeStamp *inTimeStamp,
124 __a_unused UInt32 inBusNumber,
125 __a_unused UInt32 inNumFrames,
126 AudioBufferList *outOutputData)
127 {
128 long m, n;
129 float *dest;
130 int i;
131 struct private_osx_write_data *powd = inClientData;
132
133 // PARA_INFO_LOG("%p\n", powd);
134 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
135 /* what we have to fill */
136 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
137 dest = outOutputData->mBuffers[i].mData;
138 while (m > 0) {
139 if ((n = powd->from->remaining) <= 0) {
140 PARA_INFO_LOG("%s", "buffer underrun\n");
141 /* no more bytes in the current read buffer! */
142 while ((n = powd->from->remaining) <= 0)
143 /* wait for the results */
144 usleep(2000);
145 }
146 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
147 /*
148 * we dump what we can. In fact, just the necessary
149 * should be sufficient
150 */
151 if (n > m)
152 n = m;
153 memcpy(dest, powd->from->ptr, n * sizeof(float));
154 dest += n;
155 /* remember all done work */
156 m -= n;
157 powd->from->ptr += n;
158 if ((powd->from->remaining -= n) <= 0)
159 powd->from = powd->from->next;
160 }
161 }
162 return 0;
163 }
164
165 static int osx_write_open(struct writer_node *wn)
166 {
167 struct private_osx_write_data *powd = para_calloc(
168 sizeof(struct private_osx_write_data));
169 ComponentDescription desc;
170 Component comp;
171 AURenderCallbackStruct inputCallback = {osx_callback, powd};
172 AudioStreamBasicDescription format;
173 int ret;
174 struct writer_node_group *wng = wn->wng;
175 struct osx_write_args_info *conf = wn->conf;
176
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);
187 if (!comp)
188 goto e0;
189 ret = -E_OPEN_COMP;
190 if (OpenAComponent(comp, &powd->output))
191 goto e0;
192 ret = -E_UNIT_INIT;
193 if (AudioUnitInitialize(powd->output))
194 goto e1;
195 powd->size = 0;
196 powd->play = 0;
197 /* Hmmm, let's choose PCM format */
198 /* We tell the Output Unit what format we're going to supply data to it.
199 * This is necessary if you're providing data through an input callback
200 * AND you want the DefaultOutputUnit to do any format conversions
201 * necessary from your format to the device's format.
202 */
203 if (!conf->samplerate_given && wng->samplerate)
204 powd->samplerate = *wng->samplerate;
205 else
206 powd->samplerate = conf->samplerate_arg;
207 format.mSampleRate = powd->samplerate;
208 /* The specific encoding type of audio stream*/
209 format.mFormatID = kAudioFormatLinearPCM;
210 /* flags specific to each format */
211 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
212 | kLinearPCMFormatFlagIsPacked
213 | kLinearPCMFormatFlagIsBigEndian;
214 if (!conf->channels_given && wng->channels)
215 powd->channels = *wng->channels;
216 else
217 powd->channels = conf->channels_arg;
218 format.mChannelsPerFrame = powd->channels;
219 format.mFramesPerPacket = 1;
220 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
221 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
222 /* one of the most constant constants of the whole computer history */
223 format.mBitsPerChannel = sizeof(float) * 8;
224 ret = -E_STREAM_FORMAT;
225 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
226 kAudioUnitScope_Input, 0, &format,
227 sizeof(AudioStreamBasicDescription)))
228 goto e2;
229 init_buffers(powd);
230 ret = -E_ADD_CALLBACK;
231 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
232 kAudioUnitScope_Input, 0, &inputCallback,
233 sizeof(inputCallback)) < 0)
234 goto e3;
235 return 1;
236 e3:
237 destroy_buffers(powd);
238 e2:
239 AudioUnitUninitialize(powd->output);
240 e1:
241 CloseComponent(powd->output);
242 e0:
243 return ret;
244 }
245
246 __malloc void *osx_write_parse_config(char *options)
247 {
248 struct osx_write_args_info *conf
249 = para_calloc(sizeof(struct osx_write_args_info));
250 PARA_INFO_LOG("options: %s\n", options);
251 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
252 if (ret)
253 goto err_out;
254 return conf;
255 err_out:
256 free(conf);
257 return NULL;
258
259 }
260
261 static void osx_write_close(struct writer_node *wn)
262 {
263 struct private_osx_write_data *powd = wn->private_data;
264
265 PARA_INFO_LOG("closing writer node %p\n", wn);
266 AudioOutputUnitStop(powd->output);
267 AudioUnitUninitialize(powd->output);
268 CloseComponent(powd->output);
269 destroy_buffers(powd);
270 free(powd);
271 }
272
273 static int need_new_buffer(struct writer_node *wn)
274 {
275 struct writer_node_group *wng = wn->wng;
276 struct private_osx_write_data *powd = wn->private_data;
277
278 if (*wng->loaded < sizeof(short))
279 return 0;
280 if (powd->to->remaining) /* Non empty buffer, must still be playing */
281 return 0;
282 return 1;
283 }
284
285 static int osx_write_post_select(__a_unused struct sched *s,
286 struct writer_node *wn)
287 {
288 struct private_osx_write_data *powd = wn->private_data;
289 struct writer_node_group *wng = wn->wng;
290 short *data = (short*)wng->buf;
291
292 if (!need_new_buffer(wn))
293 return 1;
294 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
295 powd->to = powd->to->next;
296 wn->written = *wng->loaded;
297 if (!powd->play) {
298 if (AudioOutputUnitStart(powd->output))
299 return -E_UNIT_START;
300 powd->play = 1;
301 }
302 return 1;
303 }
304
305 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
306 {
307 struct private_osx_write_data *powd = wn->private_data;
308 struct writer_node_group *wng = wn->wng;
309 size_t numbytes = powd->to->remaining * sizeof(short);
310 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
311 unsigned long divisor;
312
313 if (!numbytes && *wng->loaded >= sizeof(short))
314 goto min_delay; /* there's a buffer to fill */
315 if (!numbytes)
316 return 1;
317 divisor = powd->samplerate * powd->channels * 2 / numbytes;
318 if (divisor)
319 tv_divide(divisor, &tmp, &delay);
320 if (tv_diff(&s->timeout, &delay, NULL) > 0)
321 s->timeout = delay;
322 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
323 // (long unsigned) s->timeout.tv_usec);
324 return 1;
325 min_delay:
326 PARA_DEBUG_LOG("%s\n", "minimal delay");
327 s->timeout.tv_sec = 0;
328 s->timeout.tv_usec = 1;
329 return 1;
330 }
331
332 void osx_write_init(struct writer *w)
333 {
334 w->open = osx_write_open;
335 w->close = osx_write_close;
336 w->pre_select = osx_write_pre_select;
337 w->post_select = osx_write_post_select;
338 w->parse_config = osx_write_parse_config;
339 w->shutdown = NULL; /* nothing to do */
340 }