]> git.sesse.net Git - vlc/blob - modules/audio_output/opensles_android.c
sndio: allocate sys structure
[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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU 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     block_t                        *p_chain;
75     block_t                       **pp_last;
76     mtime_t                         length;
77
78     void                           *p_so_handle;
79 };
80
81 /*****************************************************************************
82  * Local prototypes.
83  *****************************************************************************/
84 static int  Open        ( vlc_object_t * );
85 static void Close       ( vlc_object_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90
91 vlc_module_begin ()
92     set_description( N_("OpenSLES audio output") )
93     set_shortname( N_("OpenSLES") )
94     set_category( CAT_AUDIO )
95     set_subcategory( SUBCAT_AUDIO_AOUT )
96
97     set_capability( "audio output", 170 )
98     add_shortcut( "opensles", "android" )
99     set_callbacks( Open, Close )
100 vlc_module_end ()
101
102
103 static void Clean( aout_sys_t *p_sys )
104 {
105     if( p_sys->playerObject )
106         Destroy( p_sys->playerObject );
107     if( p_sys->outputMixObject )
108         Destroy( p_sys->outputMixObject );
109     if( p_sys->engineObject )
110         Destroy( p_sys->engineObject );
111
112     if( p_sys->p_so_handle )
113         dlclose( p_sys->p_so_handle );
114
115     free( p_sys );
116 }
117
118 static void Flush(audio_output_t *p_aout, bool wait)
119 {
120     (void)wait; /* FIXME */
121     aout_sys_t *p_sys = p_aout->sys;
122
123     vlc_mutex_lock( &p_sys->lock );
124     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
125     Clear( p_sys->playerBufferQueue );
126     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
127     block_ChainRelease( p_sys->p_chain );
128     p_sys->p_chain = NULL;
129     p_sys->pp_last = &p_sys->p_chain;
130     vlc_mutex_unlock( &p_sys->lock );
131 }
132
133 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
134 {
135     (void)date;
136     aout_sys_t *p_sys = p_aout->sys;
137     SetPlayState( p_sys->playerPlay,
138         pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING );
139 }
140
141 /*****************************************************************************
142  * Play: play a sound
143  *****************************************************************************/
144 static void Play( audio_output_t *p_aout, block_t *p_buffer )
145 {
146     aout_sys_t *p_sys = p_aout->sys;
147     int tries = 5;
148     block_t **pp_last, *p_block;
149
150     SLAndroidSimpleBufferQueueState st;
151     SLresult res = GetState(p_sys->playerBufferQueue, &st);
152     if (unlikely(res != SL_RESULT_SUCCESS)) {
153         msg_Err(p_aout, "Could not query buffer queue state (%lu)", res);
154         st.count = 0;
155     }
156
157     if (!p_buffer->i_length) {
158         p_buffer->i_length = (mtime_t)(p_buffer->i_buffer / 2 / p_aout->format.i_channels) * CLOCK_FREQ / p_aout->format.i_rate;
159     }
160
161     vlc_mutex_lock( &p_sys->lock );
162
163     mtime_t delay = p_sys->length;
164     p_sys->length += p_buffer->i_length;
165
166     /* If something bad happens, we must remove this buffer from the FIFO */
167     pp_last = p_sys->pp_last;
168     p_block = *pp_last;
169
170     block_ChainLastAppend( &p_sys->pp_last, p_buffer );
171     vlc_mutex_unlock( &p_sys->lock );
172
173     if (delay && st.count) {
174         aout_TimeReport(p_aout, p_buffer->i_pts - delay);
175     }
176
177     for (;;)
178     {
179         SLresult result = Enqueue( p_sys->playerBufferQueue, p_buffer->p_buffer,
180                             p_buffer->i_buffer );
181
182         switch (result)
183         {
184         case SL_RESULT_BUFFER_INSUFFICIENT:
185             msg_Err( p_aout, "buffer insufficient");
186
187             if (tries--)
188             {
189                 // Wait a bit to retry.
190                 // FIXME: buffer in aout_sys_t and retry at next Play() call
191                 msleep(CLOCK_FREQ);
192                 continue;
193             }
194
195         default:
196             msg_Warn( p_aout, "Error %lu, dropping buffer", result );
197             vlc_mutex_lock( &p_sys->lock );
198             p_sys->pp_last = pp_last;
199             *pp_last = p_block;
200             p_sys->length -= p_buffer->i_length;
201             vlc_mutex_unlock( &p_sys->lock );
202             block_Release( p_buffer );
203         case SL_RESULT_SUCCESS:
204             return;
205         }
206     }
207 }
208
209 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
210 {
211     (void)caller;
212     block_t *p_block;
213     audio_output_t *p_aout = pContext;
214     aout_sys_t *p_sys = p_aout->sys;
215
216     assert (caller == p_sys->playerBufferQueue);
217
218     vlc_mutex_lock( &p_sys->lock );
219
220     p_block = p_sys->p_chain;
221     assert( p_block );
222
223     p_sys->p_chain = p_sys->p_chain->p_next;
224     /* if we exhausted our fifo, we must reset the pointer to the last
225      * appended block */
226     if (!p_sys->p_chain)
227         p_sys->pp_last = &p_sys->p_chain;
228
229     p_sys->length -= p_block->i_length;
230
231     vlc_mutex_unlock( &p_sys->lock );
232
233     block_Release( p_block );
234 }
235 /*****************************************************************************
236  * Open
237  *****************************************************************************/
238 static int Open( vlc_object_t *p_this )
239 {
240     audio_output_t *p_aout = (audio_output_t *)p_this;
241     SLresult       result;
242     SLEngineItf    engineEngine;
243
244     /* Allocate structure */
245     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
246     if( unlikely( p_aout->sys == NULL ) )
247         return VLC_ENOMEM;
248
249     aout_sys_t *p_sys = p_aout->sys;
250
251     //Acquiring LibOpenSLES symbols :
252     p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
253     if( p_sys->p_so_handle == NULL )
254     {
255         msg_Err( p_aout, "Failed to load libOpenSLES" );
256         goto error;
257     }
258
259     typedef SLresult (*slCreateEngine_t)(
260             SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
261             const SLInterfaceID*, const SLboolean* );
262     slCreateEngine_t    slCreateEnginePtr = NULL;
263
264     SLInterfaceID *SL_IID_ENGINE;
265     SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
266     SLInterfaceID *SL_IID_VOLUME;
267     SLInterfaceID *SL_IID_PLAY;
268
269 #define OPENSL_DLSYM( dest, handle, name )                   \
270     dest = dlsym( handle, name );                            \
271     if( dest == NULL )                                       \
272     {                                                        \
273         msg_Err( p_aout, "Failed to load symbol %s", name ); \
274         goto error;                                          \
275     }
276
277     OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
278     OPENSL_DLSYM( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
279                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
280     OPENSL_DLSYM( SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
281     OPENSL_DLSYM( SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
282     OPENSL_DLSYM( SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
283
284
285 #define CHECK_OPENSL_ERROR( msg )                   \
286     if( unlikely( result != SL_RESULT_SUCCESS ) )   \
287     {                                               \
288         msg_Err( p_aout, msg" (%lu)", result );     \
289         goto error;                                 \
290     }
291
292     // create engine
293     result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
294     CHECK_OPENSL_ERROR( "Failed to create engine" );
295
296     // realize the engine in synchronous mode
297     result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
298     CHECK_OPENSL_ERROR( "Failed to realize engine" );
299
300     // get the engine interface, needed to create other objects
301     result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
302     CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
303
304     // create output mix, with environmental reverb specified as a non-required interface
305     const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
306     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
307     result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
308     CHECK_OPENSL_ERROR( "Failed to create output mix" );
309
310     // realize the output mix in synchronous mode
311     result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
312     CHECK_OPENSL_ERROR( "Failed to realize output mix" );
313
314
315     // configure audio source - this defines the number of samples you can enqueue.
316     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
317         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
318         BUFF_QUEUE
319     };
320
321     SLDataFormat_PCM format_pcm;
322     format_pcm.formatType       = SL_DATAFORMAT_PCM;
323     format_pcm.numChannels      = 2;
324     format_pcm.samplesPerSec    = ((SLuint32) p_aout->format.i_rate * 1000) ;
325     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
326     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
327     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
328     format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;
329
330     SLDataSource audioSrc = {&loc_bufq, &format_pcm};
331
332     // configure audio sink
333     SLDataLocator_OutputMix loc_outmix = {
334         SL_DATALOCATOR_OUTPUTMIX,
335         p_sys->outputMixObject
336     };
337     SLDataSink audioSnk = {&loc_outmix, NULL};
338
339     //create audio player
340     const SLInterfaceID ids2[] = { *SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
341     static const SLboolean req2[] = { SL_BOOLEAN_TRUE };
342     result = CreateAudioPlayer( engineEngine, &p_sys->playerObject, &audioSrc,
343                                     &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
344                                     ids2, req2 );
345     CHECK_OPENSL_ERROR( "Failed to create audio player" );
346
347     result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
348     CHECK_OPENSL_ERROR( "Failed to realize player object." );
349
350     result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
351     CHECK_OPENSL_ERROR( "Failed to get player interface." );
352
353     result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
354                                                   &p_sys->playerBufferQueue );
355     CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
356
357     result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
358                                    (void*)p_aout);
359     CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
360
361
362     // set the player's state to playing
363     result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
364     CHECK_OPENSL_ERROR( "Failed to switch to playing state" );
365
366     vlc_mutex_init( &p_sys->lock );
367     p_sys->p_chain = NULL;
368     p_sys->pp_last = &p_sys->p_chain;
369
370     // we want 16bit signed data little endian.
371     p_aout->format.i_format              = VLC_CODEC_S16L;
372     p_aout->format.i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
373     p_aout->pf_play                      = Play;
374     p_aout->pf_pause                     = Pause;
375     p_aout->pf_flush                     = Flush;
376
377     aout_FormatPrepare( &p_aout->format );
378
379     return VLC_SUCCESS;
380 error:
381     Clean( p_sys );
382     return VLC_EGENERIC;
383 }
384
385 /*****************************************************************************
386  * Close
387  *****************************************************************************/
388 static void Close( vlc_object_t *p_this )
389 {
390     audio_output_t *p_aout = (audio_output_t*)p_this;
391     aout_sys_t     *p_sys = p_aout->sys;
392
393     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
394     //Flush remaining buffers if any.
395     Clear( p_sys->playerBufferQueue );
396     block_ChainRelease( p_sys->p_chain );
397     vlc_mutex_destroy( &p_sys->lock );
398     Clean( p_sys );
399 }