c40428895ece1e8376cd6e58b83a2369eba53fa4
[paraslash.git] / osx_write.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file osx_write.c paraslash's output plugin for MacOs */
8
9 /*
10 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
11 * <skortze@sourceforge.net>
12 */
13
14 #include <sys/types.h>
15 #include <dirent.h>
16
17 #include "para.h"
18 #include "fd.h"
19 #include "string.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "write.h"
24 #include "osx_write.cmdline.h"
25 #include "error.h"
26
27 #include <CoreServices/CoreServices.h>
28 #include <AudioUnit/AudioUnit.h>
29 #include <AudioToolbox/AudioToolbox.h>
30
31 /** describes one input buffer for the osx writer */
32 struct osx_buffer {
33 /** pointer to the beginning of the buffer */
34 float *buffer;
35 /** the size of this buffer */
36 long size;
37 /** current position in the buffer */
38 float *ptr;
39 /** number of floats not yet consuned */
40 long remaining;
41 /** pointer to the next audio buffer */
42 struct osx_buffer *next;
43 };
44
45 /** data specific to the osx writer */
46 struct private_osx_write_data {
47 /** the main control structure for audio data manipulation */
48 AudioUnit audio_unit;
49 /** non-zero if playback has started */
50 char play;
51 /** callback reads audio data from this buffer */
52 struct osx_buffer *from;
53 /** the post_select writes audio data here */
54 struct osx_buffer *to;
55 /** sample rate of the current audio stream */
56 unsigned samplerate;
57 /** number of channels of the current audio stream */
58 unsigned channels;
59 };
60
61 static void destroy_buffers(struct private_osx_write_data *powd)
62 {
63 struct osx_buffer *ptr;
64 struct osx_buffer *ptr2;
65 ptr = powd->to->next;
66 powd->to->next = NULL;
67 while (ptr) {
68 ptr2 = ptr->next;
69 free(ptr->buffer);
70 free(ptr);
71 ptr = ptr2;
72 }
73 }
74
75 static void init_buffers(struct writer_node *wn)
76 {
77 struct private_osx_write_data *powd = wn->private_data;
78 struct osx_write_args_info *conf = wn->conf;
79 struct osx_buffer **ptrptr;
80 int i;
81
82 ptrptr = &powd->to;
83 for (i = 0; i < conf->numbuffers_arg; i++) {
84 *ptrptr = para_malloc(sizeof(struct osx_buffer));
85 (*ptrptr)->size = 0;
86 (*ptrptr)->remaining = 0;
87 (*ptrptr)->buffer = NULL;
88 ptrptr = &(*ptrptr)->next;
89 }
90 *ptrptr = powd->from = powd->to;
91 }
92
93 static void fill_buffer(struct osx_buffer *b, short *source, long size)
94 {
95 float *dest;
96
97 if (b->remaining) /* Non empty buffer, must still be playing */
98 return;
99 if (b->size != size) {
100 b->buffer = para_realloc(b->buffer, size * sizeof(float));
101 b->size = size;
102 }
103 dest = b->buffer;
104 while (size--)
105 *dest++ = (*source++) / 32768.0;
106 b->ptr = b->buffer;
107 b->remaining = b->size;
108 }
109
110 static OSStatus osx_callback(void * inClientData,
111 __a_unused AudioUnitRenderActionFlags *inActionFlags,
112 __a_unused const AudioTimeStamp *inTimeStamp,
113 __a_unused UInt32 inBusNumber,
114 __a_unused UInt32 inNumFrames,
115 AudioBufferList *outOutputData)
116 {
117 long m, n;
118 float *dest;
119 int i;
120 struct private_osx_write_data *powd = inClientData;
121
122 // PARA_INFO_LOG("%p\n", powd);
123 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
124 /* what we have to fill */
125 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
126 dest = outOutputData->mBuffers[i].mData;
127 while (m > 0) {
128 if ((n = powd->from->remaining) <= 0) {
129 PARA_INFO_LOG("buffer underrun\n");
130 return 0;
131 }
132 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
133 /*
134 * we dump what we can. In fact, just the necessary
135 * should be sufficient
136 */
137 if (n > m)
138 n = m;
139 memcpy(dest, powd->from->ptr, n * sizeof(float));
140 dest += n;
141 /* remember all done work */
142 m -= n;
143 powd->from->ptr += n;
144 if ((powd->from->remaining -= n) <= 0)
145 powd->from = powd->from->next;
146 }
147 }
148 return 0;
149 }
150
151 #ifdef WORDS_BIGENDIAN /* ppc */
152 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
153 #else
154 #define ENDIAN_FLAGS 0
155 #endif
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->audio_unit))
183 goto e0;
184 ret = -E_UNIT_INIT;
185 if (AudioUnitInitialize(powd->audio_unit))
186 goto e1;
187 powd->play = 0;
188 /* Hmmm, let's choose PCM format */
189 /* We tell the Output Unit what format we're going to supply data to it.
190 * This is necessary if you're providing data through an input callback
191 * AND you want the DefaultOutputUnit to do any format conversions
192 * necessary from your format to the device's format.
193 */
194 if (!conf->samplerate_given && wng->samplerate)
195 powd->samplerate = *wng->samplerate;
196 else
197 powd->samplerate = conf->samplerate_arg;
198 format.mSampleRate = powd->samplerate;
199 /* The specific encoding type of audio stream*/
200 format.mFormatID = kAudioFormatLinearPCM;
201 /* flags specific to each format */
202 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
203 | kLinearPCMFormatFlagIsPacked
204 | ENDIAN_FLAGS;
205 if (!conf->channels_given && wng->channels)
206 powd->channels = *wng->channels;
207 else
208 powd->channels = conf->channels_arg;
209 format.mChannelsPerFrame = powd->channels;
210 format.mFramesPerPacket = 1;
211 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
212 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
213 /* one of the most constant constants of the whole computer history */
214 format.mBitsPerChannel = sizeof(float) * 8;
215 ret = -E_STREAM_FORMAT;
216 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
217 kAudioUnitScope_Input, 0, &format,
218 sizeof(AudioStreamBasicDescription)))
219 goto e2;
220 init_buffers(wn);
221 ret = -E_ADD_CALLBACK;
222 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
223 kAudioUnitScope_Input, 0, &inputCallback,
224 sizeof(inputCallback)) < 0)
225 goto e3;
226 return 1;
227 e3:
228 destroy_buffers(powd);
229 e2:
230 AudioUnitUninitialize(powd->audio_unit);
231 e1:
232 CloseComponent(powd->audio_unit);
233 e0:
234 return ret;
235 }
236
237 __malloc static void *osx_write_parse_config(const char *options)
238 {
239 struct osx_write_args_info *conf
240 = para_calloc(sizeof(struct osx_write_args_info));
241 PARA_INFO_LOG("options: %s\n", options);
242 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
243 if (ret)
244 goto err_out;
245 return conf;
246 err_out:
247 free(conf);
248 return NULL;
249
250 }
251
252 static void osx_write_close(struct writer_node *wn)
253 {
254 struct private_osx_write_data *powd = wn->private_data;
255
256 PARA_INFO_LOG("closing writer node %p\n", wn);
257 AudioOutputUnitStop(powd->audio_unit);
258 AudioUnitUninitialize(powd->audio_unit);
259 CloseComponent(powd->audio_unit);
260 destroy_buffers(powd);
261 free(powd);
262 }
263
264 static int need_new_buffer(struct writer_node *wn)
265 {
266 struct writer_node_group *wng = wn->wng;
267 struct private_osx_write_data *powd = wn->private_data;
268
269 if (*wng->loaded < sizeof(short))
270 return 0;
271 if (powd->to->remaining) /* Non empty buffer, must still be playing */
272 return 0;
273 return 1;
274 }
275
276 static int osx_write_post_select(__a_unused struct sched *s,
277 struct writer_node *wn)
278 {
279 struct private_osx_write_data *powd = wn->private_data;
280 struct writer_node_group *wng = wn->wng;
281 short *data = (short*)*wng->bufp;
282
283 if (!need_new_buffer(wn))
284 return 1;
285 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
286 powd->to = powd->to->next;
287 wn->written = *wng->loaded;
288 if (!powd->play) {
289 if (AudioOutputUnitStart(powd->audio_unit))
290 return -E_UNIT_START;
291 powd->play = 1;
292 }
293 return 1;
294 }
295
296 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
297 {
298 struct private_osx_write_data *powd = wn->private_data;
299 struct writer_node_group *wng = wn->wng;
300 size_t numbytes = powd->to->remaining * sizeof(short);
301 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
302 unsigned long divisor;
303
304 if (!numbytes && *wng->loaded >= sizeof(short))
305 goto min_delay; /* there's a buffer to fill */
306 if (!numbytes)
307 return 1;
308 divisor = powd->samplerate * powd->channels * 2 / numbytes;
309 if (divisor)
310 tv_divide(divisor, &tmp, &delay);
311 if (tv_diff(&s->timeout, &delay, NULL) > 0)
312 s->timeout = delay;
313 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
314 // (long unsigned) s->timeout.tv_usec);
315 return 1;
316 min_delay:
317 PARA_DEBUG_LOG("%s\n", "minimal delay");
318 s->timeout.tv_sec = 0;
319 s->timeout.tv_usec = 1;
320 return 1;
321 }
322
323 /** the init function of the osx writer */
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 }