]> git.sesse.net Git - vlc/blob - modules/audio_output/opensles_android.c
opensles: avoid infinite loop in linked list
[vlc] / modules / audio_output / opensles_android.c
1 /*****************************************************************************
2  * opensles_android.c : audio output for android native code
3  *****************************************************************************
4  * Copyright © 2011-2012 VideoLAN
5  *
6  * Authors: Dominique Martinet <asmadeus@codewreck.org>
7  *          Hugo Beauzée-Luyssen <beauze.h@gmail.com>
8  *          Rafaël Carré <funman@videolanorg>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <assert.h>
37 #include <dlfcn.h>
38
39 // For native audio
40 #include <SLES/OpenSLES.h>
41 #include <SLES/OpenSLES_Android.h>
42
43 // Maximum number of buffers to enqueue.
44 #define BUFF_QUEUE  42
45
46 #define Destroy(a) (*a)->Destroy(a);
47 #define SetPlayState(a, b) (*a)->SetPlayState(a, b)
48 #define RegisterCallback(a, b, c) (*a)->RegisterCallback(a, b, c)
49 #define GetInterface(a, b, c) (*a)->GetInterface(a, b, c)
50 #define Realize(a, b) (*a)->Realize(a, b)
51 #define CreateOutputMix(a, b, c, d, e) (*a)->CreateOutputMix(a, b, c, d, e)
52 #define CreateAudioPlayer(a, b, c, d, e, f, g) \
53     (*a)->CreateAudioPlayer(a, b, c, d, e, f, g)
54 #define Enqueue(a, b, c) (*a)->Enqueue(a, b, c)
55 #define Clear(a) (*a)->Clear(a)
56 #define GetState(a, b) (*a)->GetState(a, b)
57
58 /*****************************************************************************
59  * aout_sys_t: audio output method descriptor
60  *****************************************************************************
61  * This structure is part of the audio output thread descriptor.
62  * It describes the direct sound specific properties of an audio device.
63  *****************************************************************************/
64 struct aout_sys_t
65 {
66     SLObjectItf                     engineObject;
67     SLObjectItf                     outputMixObject;
68     SLAndroidSimpleBufferQueueItf   playerBufferQueue;
69     SLObjectItf                     playerObject;
70
71     SLPlayItf                       playerPlay;
72
73     vlc_mutex_t                     lock;
74     mtime_t                         length;
75
76     /* audio buffered through opensles */
77     block_t                        *p_chain;
78     block_t                       **pp_last;
79
80     /* audio not yet buffered through opensles */
81     block_t                        *p_buffer_chain;
82     block_t                       **pp_buffer_last;
83
84     void                           *p_so_handle;
85     audio_sample_format_t           format;
86 };
87
88 /*****************************************************************************
89  * Local prototypes.
90  *****************************************************************************/
91 static int  Open        ( vlc_object_t * );
92
93 /*****************************************************************************
94  * Module descriptor
95  *****************************************************************************/
96
97 vlc_module_begin ()
98     set_description( N_("OpenSLES audio output") )
99     set_shortname( N_("OpenSLES") )
100     set_category( CAT_AUDIO )
101     set_subcategory( SUBCAT_AUDIO_AOUT )
102
103     set_capability( "audio output", 170 )
104     add_shortcut( "opensles", "android" )
105     set_callbacks( Open, NULL )
106 vlc_module_end ()
107
108
109 static void Clean( aout_sys_t *p_sys )
110 {
111     if( p_sys->playerObject )
112         Destroy( p_sys->playerObject );
113     if( p_sys->outputMixObject )
114         Destroy( p_sys->outputMixObject );
115     if( p_sys->engineObject )
116         Destroy( p_sys->engineObject );
117
118     if( p_sys->p_so_handle )
119         dlclose( p_sys->p_so_handle );
120
121     free( p_sys );
122 }
123
124 static void Flush(audio_output_t *p_aout, bool drain)
125 {
126     aout_sys_t *p_sys = p_aout->sys;
127
128     if (drain) {
129         mtime_t delay;
130         vlc_mutex_lock( &p_sys->lock );
131         delay = p_sys->length;
132         vlc_mutex_unlock( &p_sys->lock );
133         msleep(delay);
134     } else {
135         vlc_mutex_lock( &p_sys->lock );
136         SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
137         Clear( p_sys->playerBufferQueue );
138         SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
139
140         p_sys->length = 0;
141
142         /* release audio data not yet written to opensles */
143         block_ChainRelease( p_sys->p_buffer_chain );
144         p_sys->p_buffer_chain = NULL;
145         p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
146
147         /* release audio data written to opensles, but not yet
148          * played on hardware */
149         block_ChainRelease( p_sys->p_chain );
150         p_sys->p_chain = NULL;
151         p_sys->pp_last = &p_sys->p_chain;
152
153         vlc_mutex_unlock( &p_sys->lock );
154     }
155 }
156
157 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
158 {
159     (void)date;
160     aout_sys_t *p_sys = p_aout->sys;
161     SetPlayState( p_sys->playerPlay,
162         pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING );
163 }
164
165 static int TimeGet(audio_output_t* p_aout, mtime_t* restrict drift)
166 {
167     aout_sys_t *p_sys = p_aout->sys;
168
169     vlc_mutex_lock( &p_sys->lock );
170     mtime_t delay = p_sys->length;
171     vlc_mutex_unlock( &p_sys->lock );
172
173     SLAndroidSimpleBufferQueueState st;
174     SLresult res = GetState(p_sys->playerBufferQueue, &st);
175     if (unlikely(res != SL_RESULT_SUCCESS)) {
176         msg_Err(p_aout, "Could not query buffer queue state in TimeGet (%lu)", res);
177         return -1;
178     }
179
180     *drift = (delay && st.count) ? delay : 0;
181     return 0;
182 }
183
184 static int WriteBuffer(audio_output_t *p_aout)
185 {
186     aout_sys_t *p_sys = p_aout->sys;
187
188     block_t *b = p_sys->p_buffer_chain;
189     if (!b)
190         return false;
191
192     if (!b->i_length)
193         b->i_length = (mtime_t)(b->i_buffer / 2 / p_sys->format.i_channels) * CLOCK_FREQ / p_sys->format.i_rate;
194
195     /* If something bad happens, we must remove this buffer from the FIFO */
196     block_t **pp_last_saved = p_sys->pp_last;
197     block_t *p_last_saved = *pp_last_saved;
198     block_t *next_saved = b->p_next;
199     b->p_next = NULL;
200
201     /* Put this block in the list of audio already written to opensles */
202     block_ChainLastAppend( &p_sys->pp_last, b );
203
204     mtime_t len = b->i_length;
205     p_sys->length += len;
206     block_t *next = b->p_next;
207
208     vlc_mutex_unlock( &p_sys->lock );
209     SLresult r = Enqueue( p_sys->playerBufferQueue, b->p_buffer, b->i_buffer );
210     vlc_mutex_lock( &p_sys->lock );
211
212     if (r == SL_RESULT_SUCCESS) {
213         /* Remove that block from the list of audio not yet written */
214         p_sys->p_buffer_chain = next;
215         if (!p_sys->p_buffer_chain)
216             p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
217     } else {
218         /* Remove that block from the list of audio already written */
219         msg_Err( p_aout, "error %lu%s (%d bytes)", r, (r == SL_RESULT_BUFFER_INSUFFICIENT)
220                 ? " buffer insufficient"
221                 : "", b->i_buffer);
222
223         p_sys->pp_last = pp_last_saved;
224         *pp_last_saved = p_last_saved;
225
226         b->p_next = next_saved;
227
228         p_sys->length -= len;
229         next = NULL; /* We'll try again next time */
230     }
231
232     return next != NULL;
233 }
234
235 /*****************************************************************************
236  * Play: play a sound
237  *****************************************************************************/
238 static void Play( audio_output_t *p_aout, block_t *p_buffer )
239 {
240     aout_sys_t *p_sys = p_aout->sys;
241
242     SLAndroidSimpleBufferQueueState st;
243     SLresult res = GetState(p_sys->playerBufferQueue, &st);
244     if (unlikely(res != SL_RESULT_SUCCESS)) {
245         msg_Err(p_aout, "Could not query buffer queue state (%lu)", res);
246         st.count = 0;
247     }
248
249     p_buffer->p_next = NULL; /* Make sur our linked list doesn't use old references */
250     vlc_mutex_lock(&p_sys->lock);
251     block_ChainLastAppend( &p_sys->pp_buffer_last, p_buffer );
252     while (WriteBuffer(p_aout))
253         ;
254     vlc_mutex_unlock( &p_sys->lock );
255 }
256
257 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
258 {
259     (void)caller;
260     block_t *p_block;
261     audio_output_t *p_aout = pContext;
262     aout_sys_t *p_sys = p_aout->sys;
263
264     assert (caller == p_sys->playerBufferQueue);
265
266     vlc_mutex_lock( &p_sys->lock );
267
268     p_block = p_sys->p_chain;
269     assert( p_block );
270
271     p_sys->p_chain = p_sys->p_chain->p_next;
272     /* if we exhausted our fifo, we must reset the pointer to the last
273      * appended block */
274     if (!p_sys->p_chain)
275         p_sys->pp_last = &p_sys->p_chain;
276
277     p_sys->length -= p_block->i_length;
278
279     vlc_mutex_unlock( &p_sys->lock );
280
281     block_Release( p_block );
282 }
283 /*****************************************************************************
284  * Open
285  *****************************************************************************/
286 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
287 {
288     SLresult       result;
289     SLEngineItf    engineEngine;
290
291     /* Allocate structure */
292     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
293     if( unlikely( p_aout->sys == NULL ) )
294         return VLC_ENOMEM;
295
296     aout_sys_t *p_sys = p_aout->sys;
297
298     //Acquiring LibOpenSLES symbols :
299     p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
300     if( p_sys->p_so_handle == NULL )
301     {
302         msg_Err( p_aout, "Failed to load libOpenSLES" );
303         goto error;
304     }
305
306     typedef SLresult (*slCreateEngine_t)(
307             SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
308             const SLInterfaceID*, const SLboolean* );
309     slCreateEngine_t    slCreateEnginePtr = NULL;
310
311     SLInterfaceID *SL_IID_ENGINE;
312     SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
313     SLInterfaceID *SL_IID_VOLUME;
314     SLInterfaceID *SL_IID_PLAY;
315
316 #define OPENSL_DLSYM( dest, handle, name )                   \
317     dest = dlsym( handle, name );                            \
318     if( dest == NULL )                                       \
319     {                                                        \
320         msg_Err( p_aout, "Failed to load symbol %s", name ); \
321         goto error;                                          \
322     }
323
324     OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
325     OPENSL_DLSYM( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
326                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
327     OPENSL_DLSYM( SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
328     OPENSL_DLSYM( SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
329     OPENSL_DLSYM( SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
330
331
332 #define CHECK_OPENSL_ERROR( msg )                   \
333     if( unlikely( result != SL_RESULT_SUCCESS ) )   \
334     {                                               \
335         msg_Err( p_aout, msg" (%lu)", result );     \
336         goto error;                                 \
337     }
338
339     // create engine
340     result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
341     CHECK_OPENSL_ERROR( "Failed to create engine" );
342
343     // realize the engine in synchronous mode
344     result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
345     CHECK_OPENSL_ERROR( "Failed to realize engine" );
346
347     // get the engine interface, needed to create other objects
348     result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
349     CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
350
351     // create output mix, with environmental reverb specified as a non-required interface
352     const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
353     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
354     result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
355     CHECK_OPENSL_ERROR( "Failed to create output mix" );
356
357     // realize the output mix in synchronous mode
358     result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
359     CHECK_OPENSL_ERROR( "Failed to realize output mix" );
360
361
362     // configure audio source - this defines the number of samples you can enqueue.
363     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
364         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
365         BUFF_QUEUE
366     };
367
368     SLDataFormat_PCM format_pcm;
369     format_pcm.formatType       = SL_DATAFORMAT_PCM;
370     format_pcm.numChannels      = 2;
371     format_pcm.samplesPerSec    = ((SLuint32) fmt->i_rate * 1000) ;
372     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
373     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
374     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
375     format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;
376
377     SLDataSource audioSrc = {&loc_bufq, &format_pcm};
378
379     // configure audio sink
380     SLDataLocator_OutputMix loc_outmix = {
381         SL_DATALOCATOR_OUTPUTMIX,
382         p_sys->outputMixObject
383     };
384     SLDataSink audioSnk = {&loc_outmix, NULL};
385
386     //create audio player
387     const SLInterfaceID ids2[] = { *SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
388     static const SLboolean req2[] = { SL_BOOLEAN_TRUE };
389     result = CreateAudioPlayer( engineEngine, &p_sys->playerObject, &audioSrc,
390                                     &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
391                                     ids2, req2 );
392     CHECK_OPENSL_ERROR( "Failed to create audio player" );
393
394     result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
395     CHECK_OPENSL_ERROR( "Failed to realize player object." );
396
397     result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
398     CHECK_OPENSL_ERROR( "Failed to get player interface." );
399
400     result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
401                                                   &p_sys->playerBufferQueue );
402     CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
403
404     result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
405                                    (void*)p_aout);
406     CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
407
408
409     // set the player's state to playing
410     result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
411     CHECK_OPENSL_ERROR( "Failed to switch to playing state" );
412
413     vlc_mutex_init( &p_sys->lock );
414     p_sys->p_chain = NULL;
415     p_sys->pp_last = &p_sys->p_chain;
416     p_sys->p_buffer_chain = NULL;
417     p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
418
419     // we want 16bit signed data little endian.
420     fmt->i_format              = VLC_CODEC_S16L;
421     fmt->i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
422     p_aout->play               = Play;
423     p_aout->pause              = Pause;
424     p_aout->flush              = Flush;
425
426     aout_FormatPrepare( fmt );
427
428     p_sys->format = *fmt;
429
430     return VLC_SUCCESS;
431 error:
432     Clean( p_sys );
433     return VLC_EGENERIC;
434 }
435
436 /*****************************************************************************
437  * Close
438  *****************************************************************************/
439 static void Stop( audio_output_t *p_aout )
440 {
441     aout_sys_t     *p_sys = p_aout->sys;
442
443     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
444     //Flush remaining buffers if any.
445     Clear( p_sys->playerBufferQueue );
446     block_ChainRelease( p_sys->p_chain );
447     block_ChainRelease( p_sys->p_buffer_chain);
448     vlc_mutex_destroy( &p_sys->lock );
449     Clean( p_sys );
450 }
451
452 static int Open (vlc_object_t *obj)
453 {
454     audio_output_t *aout = (audio_output_t *)obj;
455
456     /* FIXME: set volume/mute here */
457     aout->start = Start;
458     aout->stop = Stop;
459     aout->time_get = TimeGet;
460     return VLC_SUCCESS;
461 }