]> git.sesse.net Git - vlc/blob - modules/audio_output/audiounit_ios.c
New AudioUnit output module for iOS based on the Mac counterpart code
[vlc] / modules / audio_output / audiounit_ios.c
1 /*****************************************************************************
2  * audiounit_ios.c: AudioUnit output plugin for iOS
3  *****************************************************************************
4  * Copyright (C) 2012 - 2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #pragma mark includes
25
26 #ifdef HAVE_CONFIG_H
27 # import "config.h"
28 #endif
29
30 #import <vlc_common.h>
31 #import <vlc_plugin.h>
32 #import <vlc_aout.h>
33
34 #import <AudioUnit/AudioUnit.h>
35 #import <CoreAudio/CoreAudioTypes.h>
36 #import <AudioToolbox/AudioToolbox.h>
37 #import <mach/mach_time.h>
38
39 #import "TPCircularBuffer.h"
40
41 #pragma mark -
42 #pragma mark private declarations
43
44 #define STREAM_FORMAT_MSG(pre, sfm) \
45     pre "[%f][%4.4s][%u][%u][%u][%u][%u][%u]", \
46     sfm.mSampleRate, (char *)&sfm.mFormatID, \
47     (unsigned int)sfm.mFormatFlags, (unsigned int)sfm.mBytesPerPacket, \
48     (unsigned int)sfm.mFramesPerPacket, (unsigned int)sfm.mBytesPerFrame, \
49     (unsigned int)sfm.mChannelsPerFrame, (unsigned int)sfm.mBitsPerChannel
50
51 #define kBufferLength 2048 * 8 * 8 * 4
52
53 /*****************************************************************************
54  * aout_sys_t: private audio output method descriptor
55  *****************************************************************************
56  * This structure is part of the audio output thread descriptor.
57  * It describes the CoreAudio specific properties of an output thread.
58  *****************************************************************************/
59 struct aout_sys_t
60 {
61     uint8_t                     chans_to_reorder;   /* do we need channel reordering */
62     uint8_t                     chan_table[AOUT_CHAN_MAX];
63
64     UInt32                      i_numberOfChannels;
65     TPCircularBuffer            circular_buffer;    /* circular buffer to swap the audio data */
66
67     /* AUHAL specific */
68     AudioComponent              au_component;       /* The AudioComponent we use */
69     AudioUnit                   au_unit;            /* The AudioUnit we use */
70
71     int                         i_rate;             /* media sample rate */
72     mtime_t                     i_played_length;    /* how much did we play already */
73     mtime_t                     i_last_sample_time; /* last sample time played by the AudioUnit */
74     mtime_t                     i_first_time_stamp;
75     bool                        b_got_first_sample;
76
77     vlc_mutex_t                 lock;
78 };
79
80 #pragma mark -
81 #pragma mark local prototypes & module descriptor
82
83 static int      Open                    (vlc_object_t *);
84 static void     Close                   (vlc_object_t *);
85 static int      Start                   (audio_output_t *, audio_sample_format_t *);
86 static int      StartAnalog             (audio_output_t *, audio_sample_format_t *);
87 static void     Stop                    (audio_output_t *);
88
89 static void     Play                    (audio_output_t *, block_t *);
90 static void     Pause                   (audio_output_t *, bool, mtime_t);
91 static void     Flush                   (audio_output_t *, bool);
92 static int      TimeGet                 (audio_output_t *, mtime_t *);
93 static OSStatus RenderCallback    (vlc_object_t *, AudioUnitRenderActionFlags *, const AudioTimeStamp *,
94                                          UInt32 , UInt32, AudioBufferList *);
95 vlc_module_begin ()
96     set_shortname("audiounit_ios")
97     set_description(N_("AudioUnit output for iOS"))
98     set_capability("audio output", 101)
99     set_category(CAT_AUDIO)
100     set_subcategory(SUBCAT_AUDIO_AOUT)
101     set_callbacks(Open, Close)
102 vlc_module_end ()
103
104 #pragma mark -
105 #pragma mark initialization
106
107 static int Open(vlc_object_t *obj)
108 {
109     audio_output_t *aout = (audio_output_t *)obj;
110     aout_sys_t *sys = malloc(sizeof (*sys));
111
112     if (unlikely(sys == NULL))
113         return VLC_ENOMEM;
114
115     vlc_mutex_init(&sys->lock);
116
117     aout->sys = sys;
118     aout->start = Start;
119     aout->stop = Stop;
120
121     return VLC_SUCCESS;
122 }
123
124 static void Close(vlc_object_t *obj)
125 {
126     audio_output_t *aout = (audio_output_t *)obj;
127     aout_sys_t *sys = aout->sys;
128
129     vlc_mutex_destroy(&sys->lock);
130
131     free(sys);
132 }
133
134 static int Start(audio_output_t *p_aout, audio_sample_format_t *restrict fmt)
135 {
136     struct aout_sys_t *p_sys = NULL;
137
138     p_sys = p_aout->sys;
139     p_sys->au_component = NULL;
140     p_sys->au_unit = NULL;
141
142     aout_FormatPrint(p_aout, "VLC is looking for:", fmt);
143
144     if (StartAnalog(p_aout, fmt)) {
145         msg_Dbg(p_aout, "analog AudioUnit output successfully opened");
146         p_aout->play = Play;
147         p_aout->flush = Flush;
148         p_aout->time_get = TimeGet;
149         p_aout->pause = Pause;
150         return VLC_SUCCESS;
151     }
152
153     /* If we reach this, this aout has failed */
154     msg_Err(p_aout, "opening AudioUnit output failed");
155     return VLC_EGENERIC;
156 }
157
158 /*
159  * StartAnalog: open and setup a HAL AudioUnit to do PCM audio output
160  */
161 static int StartAnalog(audio_output_t *p_aout, audio_sample_format_t *fmt)
162 {
163     struct aout_sys_t           *p_sys = p_aout->sys;
164     UInt32                      i_param_size = 0;
165     AudioComponentDescription   desc;
166     AURenderCallbackStruct      callback;
167     p_aout->sys->chans_to_reorder = 0;
168     OSStatus status;
169
170     /* Lets go find our Component */
171     desc.componentType = kAudioUnitType_Output;
172     desc.componentSubType = kAudioUnitSubType_RemoteIO;
173     desc.componentManufacturer = kAudioUnitManufacturer_Apple;
174     desc.componentFlags = 0;
175     desc.componentFlagsMask = 0;
176
177     p_sys->au_component = AudioComponentFindNext(NULL, &desc);
178     if (p_sys->au_component == NULL) {
179         msg_Warn(p_aout, "we cannot find our audio component");
180         return false;
181     }
182
183     status = AudioComponentInstanceNew(p_sys->au_component, &p_sys->au_unit);
184     if (status != noErr) {
185         msg_Warn(p_aout, "we cannot open our audio component (%li)", status);
186         return false;
187     }
188
189     UInt32 flag = 1;
190     status = AudioUnitSetProperty(p_sys->au_unit,
191                                   kAudioOutputUnitProperty_EnableIO,
192                                   kAudioUnitScope_Output,
193                                   0,
194                                   &flag,
195                                   sizeof(flag));
196     if (status != noErr)
197         msg_Warn(p_aout, "failed to set IO mode (%li)", status);
198
199     /* Get the current format */
200     AudioStreamBasicDescription streamDescription;
201     streamDescription.mSampleRate = fmt->i_rate;
202     fmt->i_format = VLC_CODEC_FL32;
203     streamDescription.mFormatID = kAudioFormatLinearPCM;
204     streamDescription.mFormatFlags = kAudioFormatFlagsNativeFloatPacked; // FL32
205     streamDescription.mChannelsPerFrame = 2;
206     streamDescription.mFramesPerPacket = 1;
207     streamDescription.mBitsPerChannel = 32;
208     streamDescription.mBytesPerFrame = streamDescription.mBitsPerChannel * streamDescription.mChannelsPerFrame / 8;
209     streamDescription.mBytesPerPacket = streamDescription.mBytesPerFrame * streamDescription.mFramesPerPacket;
210     i_param_size = sizeof(streamDescription);
211     p_sys->i_rate = fmt->i_rate;
212
213     /* Set the desired format */
214     i_param_size = sizeof(AudioStreamBasicDescription);
215     status = AudioUnitSetProperty(p_sys->au_unit,
216                                   kAudioUnitProperty_StreamFormat,
217                                   kAudioUnitScope_Input,
218                                   0,
219                                   &streamDescription,
220                                   i_param_size);
221     if (status != noErr) {
222         msg_Err(p_aout, "failed to set stream format (%li)", status);
223         return false;
224     }
225     msg_Dbg(p_aout, STREAM_FORMAT_MSG("we set the AU format: " , streamDescription));
226
227     /* Retrieve actual format */
228     status = AudioUnitGetProperty(p_sys->au_unit,
229                                   kAudioUnitProperty_StreamFormat,
230                                   kAudioUnitScope_Input,
231                                   0,
232                                   &streamDescription,
233                                   &i_param_size);
234     if (status != noErr)
235         msg_Warn(p_aout, "failed to verify stream format (%li)", status);
236     msg_Dbg(p_aout, STREAM_FORMAT_MSG("the actual set AU format is " , streamDescription));
237
238     /* Do the last VLC aout setups */
239     aout_FormatPrepare(fmt);
240
241     /* set the IOproc callback */
242     callback.inputProc = (AURenderCallback) RenderCallback;
243     callback.inputProcRefCon = p_aout;
244
245     status = AudioUnitSetProperty(p_sys->au_unit,
246                             kAudioUnitProperty_SetRenderCallback,
247                             kAudioUnitScope_Input,
248                             0, &callback, sizeof(callback));
249     if (status != noErr) {
250         msg_Err(p_aout, "render callback setup failed (%li)", status);
251         return false;
252     }
253
254     /* AU initiliaze */
255     status = AudioUnitInitialize(p_sys->au_unit);
256     if (status != noErr) {
257         msg_Err(p_aout, "failed to init AudioUnit (%li)", status);
258         return false;
259     }
260
261     /* setup circular buffer */
262     TPCircularBufferInit(&p_sys->circular_buffer, kBufferLength);
263
264     p_sys->b_got_first_sample = false;
265     p_sys->i_played_length = 0;
266     p_sys->i_last_sample_time = 0;
267     p_sys->i_first_time_stamp = 0;
268
269     return true;
270 }
271
272 static void Stop(audio_output_t *p_aout)
273 {
274     struct aout_sys_t   *p_sys = p_aout->sys;
275     OSStatus status;
276
277     if (p_sys->au_unit) {
278         status = AudioOutputUnitStop(p_sys->au_unit);
279         if (status != noErr)
280             msg_Warn(p_aout, "failed to stop AudioUnit (%li)", status);
281
282         status = AudioComponentInstanceDispose(p_sys->au_unit);
283         if (status != noErr)
284             msg_Warn(p_aout, "failed to dispose Audio Component instance (%li)", status);
285     }
286
287     p_sys->i_played_length = 0;
288     p_sys->i_last_sample_time = 0;
289     p_sys->i_first_time_stamp = 0;
290
291     /* clean-up circular buffer */
292     TPCircularBufferCleanup(&p_sys->circular_buffer);
293 }
294
295 #pragma mark -
296 #pragma mark actual playback
297
298 static void Play (audio_output_t * p_aout, block_t * p_block)
299 {
300     struct aout_sys_t *p_sys = p_aout->sys;
301
302     if (p_block->i_nb_samples > 0) {
303         if (!p_sys->b_got_first_sample) {
304             /* Start the AU */
305             OSStatus status = AudioOutputUnitStart(p_sys->au_unit);
306             msg_Dbg(p_aout, "audio output unit started: %li", status);
307             p_sys->b_got_first_sample = true;
308         }
309
310         /* Do the channel reordering */
311         if (p_sys->chans_to_reorder) {
312            aout_ChannelReorder(p_block->p_buffer,
313                                p_block->i_buffer,
314                                p_sys->chans_to_reorder,
315                                p_sys->chan_table,
316                                VLC_CODEC_FL32);
317         }
318
319         /* keep track of the played data */
320         p_aout->sys->i_played_length += p_block->i_length;
321
322         /* move data to buffer */
323         TPCircularBufferProduceBytes(&p_sys->circular_buffer, p_block->p_buffer, p_block->i_buffer);
324     }
325
326     block_Release(p_block);
327 }
328
329 static void Pause (audio_output_t *p_aout, bool pause, mtime_t date)
330 {
331     struct aout_sys_t * p_sys = p_aout->sys;
332     VLC_UNUSED(date);
333
334     if (pause)
335         AudioOutputUnitStop(p_sys->au_unit);
336     else
337         AudioOutputUnitStart(p_sys->au_unit);
338 }
339
340 static void Flush(audio_output_t *p_aout, bool wait)
341 {
342     struct aout_sys_t * p_sys = p_aout->sys;
343     VLC_UNUSED(wait);
344
345     p_sys->b_got_first_sample = false;
346
347     /* flush circular buffer */
348     AudioOutputUnitStop(p_aout->sys->au_unit);
349     TPCircularBufferClear(&p_aout->sys->circular_buffer);
350
351     p_sys->i_played_length = 0;
352     p_sys->i_last_sample_time = 0;
353     p_sys->i_first_time_stamp = 0;
354 }
355
356 static int TimeGet(audio_output_t *p_aout, mtime_t *delay)
357 {
358     struct aout_sys_t * p_sys = p_aout->sys;
359
360     vlc_mutex_lock(&p_sys->lock);
361     mtime_t i_pos = (p_sys->i_last_sample_time - p_sys->i_first_time_stamp) * CLOCK_FREQ / p_sys->i_rate;
362     vlc_mutex_unlock(&p_sys->lock);
363
364     if (i_pos > 0) {
365         *delay = p_aout->sys->i_played_length - i_pos;
366         return 0;
367     }
368     else
369         return -1;
370 }
371
372 /*****************************************************************************
373  * RenderCallback: This function is called everytime the AudioUnit wants
374  * us to provide some more audio data.
375  * Don't print anything during normal playback, calling blocking function from
376  * this callback is not allowed.
377  *****************************************************************************/
378 static OSStatus RenderCallback(vlc_object_t *p_obj,
379                                AudioUnitRenderActionFlags *ioActionFlags,
380                                const AudioTimeStamp *inTimeStamp,
381                                UInt32 inBusNumber,
382                                UInt32 inNumberFrames,
383                                AudioBufferList *ioData) {
384     VLC_UNUSED(ioActionFlags);
385     VLC_UNUSED(inTimeStamp);
386     VLC_UNUSED(inBusNumber);
387
388     audio_output_t * p_aout = (audio_output_t *)p_obj;
389     struct aout_sys_t * p_sys = p_aout->sys;
390
391     int bytesToCopy = ioData->mBuffers[0].mDataByteSize;
392     Float32 *targetBuffer = (Float32*)ioData->mBuffers[0].mData;
393
394     /* Pull audio from buffer */
395     int32_t availableBytes = 0;
396     Float32 *buffer = TPCircularBufferTail(&p_sys->circular_buffer, &availableBytes);
397
398     /* check if we have enough data */
399     if (!availableBytes) {
400         /* return an empty buffer so silence is played until we have data */
401         for (UInt32 j = 0; j < inNumberFrames; j++)
402             targetBuffer[j] = 0.;
403     } else {
404         memcpy(targetBuffer, buffer, __MIN(bytesToCopy, availableBytes));
405         TPCircularBufferConsume(&p_sys->circular_buffer, __MIN(bytesToCopy, availableBytes));
406         VLC_UNUSED(inNumberFrames);
407         vlc_mutex_lock(&p_sys->lock);
408         p_sys->i_last_sample_time = inTimeStamp->mSampleTime;
409         vlc_mutex_unlock(&p_sys->lock);
410         if (p_sys->i_first_time_stamp == 0)
411             p_sys->i_first_time_stamp = inTimeStamp->mSampleTime;
412     }
413
414     return noErr;
415 }
416