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