2 * Copyright (C) 2006-2010 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>
25 #include "buffer_tree.h"
27 #include "write_common.h"
28 #include "osx_write.cmdline.h"
31 #include <CoreServices/CoreServices.h>
32 #include <AudioUnit/AudioUnit.h>
33 #include <AudioToolbox/AudioToolbox.h>
35 /** describes one input buffer for the osx writer */
37 /** pointer to the beginning of the buffer */
39 /** the size of this buffer */
41 /** current position in the buffer */
43 /** number of floats not yet consuned */
45 /** pointer to the next audio buffer */
46 struct osx_buffer
*next
;
49 /** data specific to the osx writer */
50 struct private_osx_write_data
{
51 /** the main control structure for audio data manipulation */
53 /** non-zero if playback has started */
55 /** callback reads audio data from this buffer */
56 struct osx_buffer
*from
;
57 /** the post_select writes audio data here */
58 struct osx_buffer
*to
;
59 /** sample rate of the current audio stream */
61 /** Sample format of the current audio stream */
62 unsigned sample_format
;
63 /** number of channels of the current audio stream */
67 static void destroy_buffers(struct private_osx_write_data
*powd
)
69 struct osx_buffer
*ptr
;
70 struct osx_buffer
*ptr2
;
72 powd
->to
->next
= NULL
;
81 static void init_buffers(struct writer_node
*wn
)
83 struct private_osx_write_data
*powd
= wn
->private_data
;
84 struct osx_write_args_info
*conf
= wn
->conf
;
85 struct osx_buffer
**ptrptr
;
89 for (i
= 0; i
< conf
->numbuffers_arg
; i
++) {
90 *ptrptr
= para_malloc(sizeof(struct osx_buffer
));
92 (*ptrptr
)->remaining
= 0;
93 (*ptrptr
)->buffer
= NULL
;
94 ptrptr
= &(*ptrptr
)->next
;
96 *ptrptr
= powd
->from
= powd
->to
;
99 static void fill_buffer(struct private_osx_write_data
*powd
, char *data
, long bytes
)
101 struct osx_buffer
*b
= powd
->to
;
104 enum sample_format sf
= powd
->sample_format
;
106 samples
= (sf
== SF_S8
|| sf
== SF_U8
)? bytes
: bytes
/ 2;
107 assert(b
->remaining
== 0 || samples
> 0);
108 if (b
->size
!= samples
) {
109 b
->buffer
= para_realloc(b
->buffer
, samples
* sizeof(float));
113 switch (powd
->sample_format
) {
115 uint8_t *src
= (uint8_t *)data
;
117 *dest
++ = (*src
++) / 256.0;
122 int8_t *src
= (int8_t *)data
;
124 *dest
++ = ((*src
++) + 128) / 256.0;
129 short *src
= (short *)data
;
131 *dest
++ = (*src
++) / 32768.0;
135 b
->remaining
= b
->size
;
138 static OSStatus
osx_callback(void * inClientData
,
139 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
140 __a_unused
const AudioTimeStamp
*inTimeStamp
,
141 __a_unused UInt32 inBusNumber
,
142 __a_unused UInt32 inNumFrames
,
143 AudioBufferList
*outOutputData
)
148 struct private_osx_write_data
*powd
= inClientData
;
150 // PARA_INFO_LOG("%p\n", powd);
151 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
152 /* what we have to fill */
153 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
154 dest
= outOutputData
->mBuffers
[i
].mData
;
156 n
= powd
->from
->remaining
;
158 n
= powd
->from
->next
->remaining
;
160 PARA_INFO_LOG("buffer underrun\n");
163 powd
->from
= powd
->from
->next
;
165 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
167 * we dump what we can. In fact, just the necessary
168 * should be sufficient
172 memcpy(dest
, powd
->from
->ptr
, n
* sizeof(float));
174 /* remember all done work */
176 powd
->from
->ptr
+= n
;
177 if ((powd
->from
->remaining
-= n
) <= 0)
178 powd
->from
= powd
->from
->next
;
184 #ifdef WORDS_BIGENDIAN /* ppc */
185 #define ENDIAN_FLAGS kLinearPCMFormatFlagIsBigEndian
187 #define ENDIAN_FLAGS 0
190 static void osx_write_open(struct writer_node
*wn
)
192 struct private_osx_write_data
*powd
= para_calloc(sizeof(*powd
));
194 wn
->private_data
= powd
;
198 static int core_audio_init(struct writer_node
*wn
)
200 struct private_osx_write_data
*powd
= wn
->private_data
;
201 ComponentDescription desc
;
203 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
204 AudioStreamBasicDescription format
;
206 struct btr_node
*btrn
= wn
->btrn
;
209 /* where did that default audio output go? */
210 desc
.componentType
= kAudioUnitType_Output
;
211 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
212 /* NOTE: and if default output isn't Apple? */
213 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
214 desc
.componentFlags
= 0;
215 desc
.componentFlagsMask
= 0;
216 ret
= -E_DEFAULT_COMP
;
217 comp
= FindNextComponent(NULL
, &desc
);
221 if (OpenAComponent(comp
, &powd
->audio_unit
))
224 if (AudioUnitInitialize(powd
->audio_unit
))
227 get_btr_sample_rate(btrn
, &val
);
228 powd
->sample_rate
= val
;
229 get_btr_channels(btrn
, &val
);
230 powd
->channels
= val
;
231 get_btr_sample_format(btrn
, &val
);
232 powd
->sample_format
= val
;
234 * Choose PCM format. We tell the Output Unit what format we're going
235 * to supply data to it. This is necessary if you're providing data
236 * through an input callback AND you want the DefaultOutputUnit to do
237 * any format conversions necessary from your format to the device's
240 format
.mFormatID
= kAudioFormatLinearPCM
;
241 format
.mFramesPerPacket
= 1;
242 format
.mSampleRate
= powd
->sample_rate
;
243 /* flags specific to each format */
244 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
245 | kLinearPCMFormatFlagIsPacked
247 switch (powd
->sample_format
) {
250 wn
->min_iqs
= powd
->channels
;
253 wn
->min_iqs
= powd
->channels
* 2;
255 format
.mBitsPerChannel
= 8 * sizeof(float);
256 format
.mBytesPerPacket
= powd
->channels
* sizeof(float);
257 format
.mBytesPerFrame
= format
.mBytesPerPacket
;
258 format
.mChannelsPerFrame
= powd
->channels
;
260 ret
= -E_STREAM_FORMAT
;
261 if (AudioUnitSetProperty(powd
->audio_unit
, kAudioUnitProperty_StreamFormat
,
262 kAudioUnitScope_Input
, 0, &format
,
263 sizeof(AudioStreamBasicDescription
)))
265 ret
= -E_ADD_CALLBACK
;
266 if (AudioUnitSetProperty(powd
->audio_unit
, kAudioUnitProperty_SetRenderCallback
,
267 kAudioUnitScope_Input
, 0, &inputCallback
,
268 sizeof(inputCallback
)) < 0)
272 destroy_buffers(powd
);
274 AudioUnitUninitialize(powd
->audio_unit
);
276 CloseComponent(powd
->audio_unit
);
281 __malloc
static void *osx_write_parse_config_or_die(const char *options
)
283 struct osx_write_args_info
*conf
= para_calloc(sizeof(*conf
));
285 /* exits on errors */
286 osx_cmdline_parser_string(options
, conf
, "osx_write");
290 static void osx_free_config(void *conf
)
292 osx_cmdline_parser_free(conf
);
295 static void osx_write_close(struct writer_node
*wn
)
297 struct private_osx_write_data
*powd
= wn
->private_data
;
299 PARA_INFO_LOG("closing writer node %p\n", wn
);
300 AudioOutputUnitStop(powd
->audio_unit
);
301 AudioUnitUninitialize(powd
->audio_unit
);
302 CloseComponent(powd
->audio_unit
);
303 destroy_buffers(powd
);
307 static void osx_write_post_select(__a_unused
struct sched
*s
, struct task
*t
)
309 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
310 struct private_osx_write_data
*powd
= wn
->private_data
;
311 struct btr_node
*btrn
= wn
->btrn
;
316 while (powd
->to
->remaining
<= 0) {
317 ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
320 if (powd
->sample_rate
== 0) {
321 ret
= core_audio_init(wn
);
325 btr_merge(btrn
, 8192);
326 bytes
= btr_next_buffer(btrn
, &data
);
327 //PARA_CRIT_LOG("have: %zu\n", bytes);
328 fill_buffer(powd
, data
, bytes
);
329 btr_consume(btrn
, bytes
);
332 if (AudioOutputUnitStart(powd
->audio_unit
))
336 powd
->to
= powd
->to
->next
;
338 if (ret
< 0 && powd
->from
->remaining
<= 0) {
339 btr_remove_node(btrn
);
344 static void osx_write_pre_select(struct sched
*s
, struct task
*t
)
346 struct writer_node
*wn
= container_of(t
, struct writer_node
, task
);
347 struct private_osx_write_data
*powd
= wn
->private_data
;
348 struct timeval tmp
= {.tv_sec
= 1, .tv_usec
= 0}, delay
= tmp
;
349 unsigned long divisor
;
350 size_t numbytes
= powd
->to
->remaining
* sizeof(short);
351 int ret
= btr_node_status(wn
->btrn
, wn
->min_iqs
, BTR_NT_LEAF
);
355 if (ret
<= 0 || numbytes
< wn
->min_iqs
)
357 divisor
= powd
->sample_rate
* wn
->min_iqs
/ numbytes
;
359 tv_divide(divisor
, &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
->open
= osx_write_open
;
370 w
->close
= osx_write_close
;
371 w
->pre_select
= osx_write_pre_select
;
372 w
->post_select
= osx_write_post_select
;
373 w
->parse_config_or_die
= osx_write_parse_config_or_die
;
374 w
->free_config
= osx_free_config
;
375 w
->shutdown
= NULL
; /* nothing to do */
376 w
->help
= (struct ggo_help
) {
377 .short_help
= osx_write_args_info_help
,
378 .detailed_help
= osx_write_args_info_detailed_help
380 osx_cmdline_parser_free(&dummy
);