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