configure.ac: print optional executables in configuration output
[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 static void destroy_buffers(struct private_osx_write_data *powd)
59 {
60         struct osx_buffer *ptr;
61         struct osx_buffer *ptr2;
62         ptr = powd->to->next;
63         powd->to->next = NULL;
64         while (ptr) {
65                 ptr2 = ptr->next;
66                 free(ptr->buffer);
67                 free(ptr);
68                 ptr = ptr2;
69         }
70 }
71
72 static void init_buffers(struct writer_node *wn)
73 {
74         struct private_osx_write_data *powd = wn->private_data;
75         struct osx_write_args_info *conf = wn->conf;
76         struct osx_buffer **ptrptr;
77         int i;
78
79         ptrptr = &powd->to;
80         for (i = 0; i < conf->numbuffers_arg; i++) {
81                 *ptrptr = malloc(sizeof(struct osx_buffer));
82                 (*ptrptr)->size = 0;
83                 (*ptrptr)->remaining = 0;
84                 (*ptrptr)->buffer = NULL;
85                 ptrptr = &(*ptrptr)->next;
86         }
87         *ptrptr = powd->from = powd->to;
88 }
89
90 static void fill_buffer(struct osx_buffer *b, short *source, long size)
91 {
92         float *dest;
93
94         if (b->remaining) /* Non empty buffer, must still be playing */
95                 return;
96         if (b->size != size) {
97                 b->buffer = para_realloc(b->buffer, size * sizeof(float));
98                 b->size = size;
99         }
100         dest = b->buffer;
101         while (size--) {
102                 char *tmp = (char *)source;
103                 char c = *tmp;
104                 *tmp = *(tmp + 1);
105                 *(tmp + 1) = c;
106                 /* *dest++ = ((*source++) + 32768) / 65536.0; */
107                 *dest++ = (*source++) / 32768.0;
108         }
109         b->ptr = b->buffer;
110         b->remaining = b->size;
111 }
112
113 static OSStatus osx_callback(void * inClientData,
114         __a_unused AudioUnitRenderActionFlags *inActionFlags,
115         __a_unused const AudioTimeStamp *inTimeStamp,
116         __a_unused  UInt32 inBusNumber,
117         __a_unused UInt32 inNumFrames,
118         AudioBufferList *outOutputData)
119 {
120         long m, n;
121         float *dest;
122         int i;
123         struct private_osx_write_data *powd = inClientData;
124
125 //      PARA_INFO_LOG("%p\n", powd);
126         for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
127                 /* what we have to fill */
128                 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
129                 dest = outOutputData->mBuffers[i].mData;
130                 while (m > 0) {
131                         if ((n = powd->from->remaining) <= 0) {
132                                 PARA_INFO_LOG("%s", "buffer underrun\n");
133                                 /* no more bytes in the current read buffer! */
134                                 while ((n = powd->from->remaining) <= 0)
135                                         /* wait for the results */
136                                         usleep(2000);
137                         }
138 //                      PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
139                         /*
140                          * we dump what we can. In fact, just the necessary
141                          * should be sufficient
142                          */
143                         if (n > m)
144                                 n = m;
145                         memcpy(dest, powd->from->ptr, n * sizeof(float));
146                         dest += n;
147                         /* remember all done work */
148                         m -= n;
149                         powd->from->ptr += n;
150                         if ((powd->from->remaining -= n) <= 0)
151                                 powd->from = powd->from->next;
152                 }
153         }
154         return 0;
155 }
156
157 static int osx_write_open(struct writer_node *wn)
158 {
159         struct private_osx_write_data *powd = para_calloc(
160                 sizeof(struct private_osx_write_data));
161         ComponentDescription desc;
162         Component comp;
163         AURenderCallbackStruct inputCallback = {osx_callback, powd};
164         AudioStreamBasicDescription format;
165         int ret;
166         struct writer_node_group *wng = wn->wng;
167         struct osx_write_args_info *conf = wn->conf;
168
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);
179         if (!comp)
180                 goto e0;
181         ret = -E_OPEN_COMP;
182         if (OpenAComponent(comp, &powd->output))
183                 goto e0;
184         ret = -E_UNIT_INIT;
185         if (AudioUnitInitialize(powd->output))
186                 goto e1;
187         powd->size = 0;
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                 | kLinearPCMFormatFlagIsBigEndian;
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->output, 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->output, 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->output);
232 e1:
233         CloseComponent(powd->output);
234 e0:
235         return ret;
236 }
237
238 __malloc void *osx_write_parse_config(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->output);
259         AudioUnitUninitialize(powd->output);
260         CloseComponent(powd->output);
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->buf;
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->output))
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 void osx_write_init(struct writer *w)
325 {
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 */
332 }