2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file osx_write.c paraslash's output plugin for MacOs */
10 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
11 * <skortze@sourceforge.net>
15 #include <sys/types.h>
24 #include "buffer_tree.h"
26 #include "write_common.h"
27 #include "osx_write.cmdline.h"
30 #include <CoreServices/CoreServices.h>
31 #include <AudioUnit/AudioUnit.h>
32 #include <AudioToolbox/AudioToolbox.h>
34 /** describes one input buffer for the osx writer */
36 /** pointer to the beginning of the buffer */
38 /** the size of this buffer */
40 /** current position in the buffer */
42 /** number of floats not yet consuned */
44 /** pointer to the next audio buffer */
45 struct osx_buffer
*next
;
48 /** data specific to the osx writer */
49 struct private_osx_write_data
{
50 /** the main control structure for audio data manipulation */
52 /** non-zero if playback has started */
54 /** callback reads audio data from this buffer */
55 struct osx_buffer
*from
;
56 /** the post_select writes audio data here */
57 struct osx_buffer
*to
;
58 /** sample rate of the current audio stream */
60 /** Sample format of the current audio stream */
61 unsigned sample_format
;
62 /** number of channels of the current audio stream */
66 static void destroy_buffers(struct private_osx_write_data
*powd
)
68 struct osx_buffer
*ptr
;
69 struct osx_buffer
*ptr2
;
71 powd
->to
->next
= NULL
;
80 static void init_buffers(struct writer_node
*wn
)
82 struct private_osx_write_data
*powd
= wn
->private_data
;
83 struct osx_write_args_info
*conf
= wn
->conf
;
84 struct osx_buffer
**ptrptr
;
88 for (i
= 0; i
< conf
->numbuffers_arg
; i
++) {
89 *ptrptr
= para_malloc(sizeof(struct osx_buffer
));
91 (*ptrptr
)->remaining
= 0;
92 (*ptrptr
)->buffer
= NULL
;
93 ptrptr
= &(*ptrptr
)->next
;
95 *ptrptr
= powd
->from
= powd
->to
;
98 static void fill_buffer(struct private_osx_write_data
*powd
, char *data
, long bytes
)
100 struct osx_buffer
*b
= powd
->to
;
103 enum sample_format sf
= powd
->sample_format
;
105 samples
= (sf
== SF_S8
|| sf
== SF_U8
)? bytes
: bytes
/ 2;
106 assert(b
->remaining
== 0 || samples
> 0);
107 if (b
->size
!= samples
) {
108 b
->buffer
= para_realloc(b
->buffer
, samples
* sizeof(float));
112 switch (powd
->sample_format
) {
114 uint8_t *src
= (uint8_t *)data
;
116 *dest
++ = (*src
++) / 256.0;
121 int8_t *src
= (int8_t *)data
;
123 *dest
++ = ((*src
++) + 128) / 256.0;
128 short *src
= (short *)data
;
130 *dest
++ = (*src
++) / 32768.0;
134 b
->remaining
= b
->size
;
137 static OSStatus
osx_callback(void * inClientData
,
138 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
139 __a_unused
const AudioTimeStamp
*inTimeStamp
,
140 __a_unused UInt32 inBusNumber
,
141 __a_unused UInt32 inNumFrames
,
142 AudioBufferList
*outOutputData
)
147 struct private_osx_write_data
*powd
= inClientData
;
149 // PARA_INFO_LOG("%p\n", powd);
150 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
151 /* what we have to fill */
152 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
153 dest
= outOutputData
->mBuffers
[i
].mData
;
155 n
= powd
->from
->remaining
;
157 n
= powd
->from
->next
->remaining
;
159 PARA_INFO_LOG("buffer underrun\n");
162 powd
->from
= powd
->from
->next
;
164 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
166 * we dump what we can. In fact, just the necessary
167 * should be sufficient
171 memcpy(dest
, powd
->from
->ptr
, n
* sizeof(float));
173 /* remember all done work */
175 powd
->from
->ptr
+= n
;
176 if ((powd
->from
->remaining
-= n
) <= 0)
177 powd
->from
= powd
->from
->next
;
183 #ifdef WORDS_BIGENDIAN /* ppc */
184 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
186 #define ENDIAN_FLAGS 0
189 static int core_audio_init(struct writer_node
*wn
)
191 struct private_osx_write_data
*powd
= para_calloc(sizeof(*powd
));
192 ComponentDescription desc
;
194 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
195 AudioStreamBasicDescription format
;
197 struct btr_node
*btrn
= wn
->btrn
;
200 wn
->private_data
= powd
;
202 /* where did that default audio output go? */
203 desc
.componentType
= kAudioUnitType_Output
;
204 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
205 /* NOTE: and if default output isn't Apple? */
206 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
207 desc
.componentFlags
= 0;
208 desc
.componentFlagsMask
= 0;
209 ret
= -E_DEFAULT_COMP
;
210 comp
= FindNextComponent(NULL
, &desc
);
214 if (OpenAComponent(comp
, &powd
->audio_unit
))
217 if (AudioUnitInitialize(powd
->audio_unit
))
220 get_btr_sample_rate(btrn
, &val
);
221 powd
->sample_rate
= val
;
222 get_btr_channels(btrn
, &val
);
223 powd
->channels
= val
;
224 get_btr_sample_format(btrn
, &val
);
225 powd
->sample_format
= val
;
227 * Choose PCM format. We tell the Output Unit what format we're going
228 * to supply data to it. This is necessary if you're providing data
229 * through an input callback AND you want the DefaultOutputUnit to do
230 * any format conversions necessary from your format to the device's
233 format
.mFormatID
= kAudioFormatLinearPCM
;
234 format
.mFramesPerPacket
= 1;
235 format
.mSampleRate
= powd
->sample_rate
;
236 /* flags specific to each format */
237 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
238 | kLinearPCMFormatFlagIsPacked
240 switch (powd
->sample_format
) {
243 wn
->min_iqs
= powd
->channels
;
246 wn
->min_iqs
= powd
->channels
* 2;
248 format
.mBitsPerChannel
= 8 * sizeof(float);
249 format
.mBytesPerPacket
= powd
->channels
* sizeof(float);
250 format
.mBytesPerFrame
= format
.mBytesPerPacket
;
251 format
.mChannelsPerFrame
= powd
->channels
;
253 ret
= -E_STREAM_FORMAT
;
254 if (AudioUnitSetProperty(powd
->audio_unit
, kAudioUnitProperty_StreamFormat
,
255 kAudioUnitScope_Input
, 0, &format
,
256 sizeof(AudioStreamBasicDescription
)))
258 ret
= -E_ADD_CALLBACK
;
259 if (AudioUnitSetProperty(powd
->audio_unit
, kAudioUnitProperty_SetRenderCallback
,
260 kAudioUnitScope_Input
, 0, &inputCallback
,
261 sizeof(inputCallback
)) < 0)
265 destroy_buffers(powd
);
267 AudioUnitUninitialize(powd
->audio_unit
);
269 CloseComponent(powd
->audio_unit
);
272 wn
->private_data
= NULL
;
276 __malloc
static void *osx_write_parse_config_or_die(const char *options
)
278 struct osx_write_args_info
*conf
= para_calloc(sizeof(*conf
));
280 /* exits on errors */
281 osx_cmdline_parser_string(options
, conf
, "osx_write");
285 static void osx_free_config(void *conf
)
287 osx_cmdline_parser_free(conf
);
290 static void osx_write_close(struct writer_node
*wn
)
292 struct private_osx_write_data
*powd
= wn
->private_data
;
296 PARA_INFO_LOG("closing writer node %p\n", wn
);
297 AudioOutputUnitStop(powd
->audio_unit
);
298 AudioUnitUninitialize(powd
->audio_unit
);
299 CloseComponent(powd
->audio_unit
);
300 destroy_buffers(powd
);
304 static void osx_write_post_select(__a_unused
struct sched
*s
, struct task
*t
)
306 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
307 struct private_osx_write_data
*powd
= wn
->private_data
;
308 struct btr_node
*btrn
= wn
->btrn
;
313 while (!powd
|| powd
->to
->remaining
<= 0) {
314 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
318 ret
= core_audio_init(wn
);
321 powd
= wn
->private_data
;
323 btr_merge(btrn
, 8192);
324 bytes
= btr_next_buffer(btrn
, &data
);
325 //PARA_CRIT_LOG("have: %zu\n", bytes);
326 fill_buffer(powd
, data
, bytes
);
327 btr_consume(btrn
, bytes
);
330 if (AudioOutputUnitStart(powd
->audio_unit
))
334 powd
->to
= powd
->to
->next
;
336 if (ret
< 0 && (!powd
|| powd
->from
->remaining
<= 0)) {
337 btr_remove_node(btrn
);
342 static void osx_write_pre_select(struct sched
*s
, struct task
*t
)
344 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
345 struct private_osx_write_data
*powd
= wn
->private_data
;
346 struct timeval tmp
= {.tv_sec
= 1, .tv_usec
= 0}, delay
= tmp
;
347 unsigned long factor
;
349 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
353 if (ret
< 0 || !powd
)
354 return sched_min_delay(s
);
355 assert(powd
->sample_rate
> 0);
356 assert(wn
->min_iqs
> 0);
357 numbytes
= powd
->to
->remaining
* sizeof(short);
358 factor
= numbytes
/ powd
->sample_rate
/ wn
->min_iqs
;
359 tv_scale(factor
, &tmp
, &delay
);
360 sched_request_timeout(&delay
, s
);
363 /** the init function of the osx writer */
364 void osx_write_init(struct writer
*w
)
366 struct osx_write_args_info dummy
;
368 osx_cmdline_parser_init(&dummy
);
369 w
->close
= osx_write_close
;
370 w
->pre_select
= osx_write_pre_select
;
371 w
->post_select
= osx_write_post_select
;
372 w
->parse_config_or_die
= osx_write_parse_config_or_die
;
373 w
->free_config
= osx_free_config
;
374 w
->shutdown
= NULL
; /* nothing to do */
375 w
->help
= (struct ggo_help
) {
376 .short_help
= osx_write_args_info_help
,
377 .detailed_help
= osx_write_args_info_detailed_help
379 osx_cmdline_parser_free(&dummy
);