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