]> git.sesse.net Git - vlc/blob - modules/audio_output/opensles_android.c
Partly revert b192dc3c38ac39d1dc0f5f2a55fd4f3abbcbbec0
[vlc] / modules / audio_output / opensles_android.c
1 /*****************************************************************************
2  * opensles_android.c : audio output for android native code
3  *****************************************************************************
4  * Copyright © 2011 VideoLAN
5  *
6  * Authors: Dominique Martinet <asmadeus@codewreck.org>
7  *          Hugo Beauzée-Luyssen <beauze.h@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35 #include <assert.h>
36 #include <dlfcn.h>
37
38 // For native audio
39 #include <SLES/OpenSLES.h>
40 #include <SLES/OpenSLES_Android.h>
41
42 // Maximum number of buffers to enqueue.
43 #define BUFF_QUEUE  42
44
45 #define Destroy(a) (*a)->Destroy(a);
46 #define SetPlayState(a, b) (*a)->SetPlayState(a, b)
47 #define RegisterCallback(a, b, c) (*a)->RegisterCallback(a, b, c)
48 #define GetInterface(a, b, c) (*a)->GetInterface(a, b, c)
49 #define Realize(a, b) (*a)->Realize(a, b)
50 #define CreateOutputMix(a, b, c, d, e) (*a)->CreateOutputMix(a, b, c, d, e)
51 #define CreateAudioPlayer(a, b, c, d, e, f, g) \
52     (*a)->CreateAudioPlayer(a, b, c, d, e, f, g)
53 #define Enqueue(a, b, c) (*a)->Enqueue(a, b, c)
54 #define Clear(a) (*a)->Clear(a)
55
56 /*****************************************************************************
57  * aout_sys_t: audio output method descriptor
58  *****************************************************************************
59  * This structure is part of the audio output thread descriptor.
60  * It describes the direct sound specific properties of an audio device.
61  *****************************************************************************/
62 struct aout_sys_t
63 {
64     SLObjectItf                     engineObject;
65     SLObjectItf                     outputMixObject;
66     SLAndroidSimpleBufferQueueItf   playerBufferQueue;
67     SLObjectItf                     playerObject;
68
69     SLPlayItf                       playerPlay;
70
71     vlc_mutex_t                     lock;
72     block_t                        *p_chain;
73     block_t                       **pp_last;
74
75     void                           *p_so_handle;
76 };
77
78 /*****************************************************************************
79  * Local prototypes.
80  *****************************************************************************/
81 static int  Open        ( vlc_object_t * );
82 static void Close       ( vlc_object_t * );
83
84 /*****************************************************************************
85  * Module descriptor
86  *****************************************************************************/
87
88 vlc_module_begin ()
89     set_description( N_("OpenSLES audio output") )
90     set_shortname( N_("OpenSLES") )
91     set_category( CAT_AUDIO )
92     set_subcategory( SUBCAT_AUDIO_AOUT )
93
94     set_capability( "audio output", 170 )
95     add_shortcut( "opensles", "android" )
96     set_callbacks( Open, Close )
97 vlc_module_end ()
98
99
100 static void Clean( aout_sys_t *p_sys )
101 {
102     if( p_sys->playerObject )
103         Destroy( p_sys->playerObject );
104     if( p_sys->outputMixObject )
105         Destroy( p_sys->outputMixObject );
106     if( p_sys->engineObject )
107         Destroy( p_sys->engineObject );
108
109     if( p_sys->p_so_handle )
110         dlclose( p_sys->p_so_handle );
111
112     free( p_sys );
113 }
114
115 /*****************************************************************************
116  * Play: play a sound
117  *****************************************************************************/
118 static void Play( audio_output_t *p_aout, block_t *p_buffer )
119 {
120     aout_sys_t *p_sys = p_aout->sys;
121     int tries = 5;
122     block_t **pp_last, *p_block;;
123
124     vlc_mutex_lock( &p_sys->lock );
125
126     /* If something bad happens, we must remove this buffer from the FIFO */
127     pp_last = p_sys->pp_last;
128     p_block = *pp_last;
129
130     block_ChainLastAppend( &p_sys->pp_last, p_buffer );
131     vlc_mutex_unlock( &p_sys->lock );
132
133     for (;;)
134     {
135         SLresult result = Enqueue( p_sys->playerBufferQueue, p_buffer->p_buffer,
136                             p_buffer->i_buffer );
137
138         switch (result)
139         {
140         case SL_RESULT_BUFFER_INSUFFICIENT:
141             msg_Err( p_aout, "buffer insufficient");
142
143             if (tries--)
144             {
145                 // Wait a bit to retry.
146                 msleep(CLOCK_FREQ);
147                 continue;
148             }
149
150         default:
151             msg_Warn( p_aout, "Error %lu, dropping buffer", result );
152             vlc_mutex_lock( &p_sys->lock );
153             p_sys->pp_last = pp_last;
154             *pp_last = p_block;
155             vlc_mutex_unlock( &p_sys->lock );
156             block_Release( p_buffer );
157         case SL_RESULT_SUCCESS:
158             return;
159         }
160     }
161 }
162
163 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
164 {
165     block_t *p_block;
166     aout_sys_t *p_sys = pContext;
167
168     assert (caller == p_sys->playerBufferQueue);
169
170     vlc_mutex_lock( &p_sys->lock );
171
172     p_block = p_sys->p_chain;
173     assert( p_block );
174
175     p_sys->p_chain = p_sys->p_chain->p_next;
176     /* if we exhausted our fifo, we must reset the pointer to the last
177      * appended block */
178     if (!p_sys->p_chain)
179         p_sys->pp_last = &p_sys->p_chain;
180
181     vlc_mutex_unlock( &p_sys->lock );
182
183     block_Release( p_block );
184 }
185 /*****************************************************************************
186  * Open
187  *****************************************************************************/
188 static int Open( vlc_object_t *p_this )
189 {
190     audio_output_t *p_aout = (audio_output_t *)p_this;
191     SLresult       result;
192     SLEngineItf    engineEngine;
193
194     /* Allocate structure */
195     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
196     if( unlikely( p_aout->sys == NULL ) )
197         return VLC_ENOMEM;
198
199     aout_sys_t *p_sys = p_aout->sys;
200
201     //Acquiring LibOpenSLES symbols :
202     p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
203     if( p_sys->p_so_handle == NULL )
204     {
205         msg_Err( p_aout, "Failed to load libOpenSLES" );
206         goto error;
207     }
208
209     typedef SLresult (*slCreateEngine_t)(
210             SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
211             const SLInterfaceID*, const SLboolean* );
212     slCreateEngine_t    slCreateEnginePtr = NULL;
213
214     SLInterfaceID *SL_IID_ENGINE;
215     SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
216     SLInterfaceID *SL_IID_VOLUME;
217     SLInterfaceID *SL_IID_PLAY;
218
219 #define OPENSL_DLSYM( dest, handle, name )                   \
220     dest = dlsym( handle, name );                            \
221     if( dest == NULL )                                       \
222     {                                                        \
223         msg_Err( p_aout, "Failed to load symbol %s", name ); \
224         goto error;                                          \
225     }
226
227     OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
228     OPENSL_DLSYM( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
229                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
230     OPENSL_DLSYM( SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
231     OPENSL_DLSYM( SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
232     OPENSL_DLSYM( SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
233
234
235 #define CHECK_OPENSL_ERROR( msg )                   \
236     if( unlikely( result != SL_RESULT_SUCCESS ) )   \
237     {                                               \
238         msg_Err( p_aout, msg" (%lu)", result );     \
239         goto error;                                 \
240     }
241
242     // create engine
243     result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
244     CHECK_OPENSL_ERROR( "Failed to create engine" );
245
246     // realize the engine in synchronous mode
247     result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
248     CHECK_OPENSL_ERROR( "Failed to realize engine" );
249
250     // get the engine interface, needed to create other objects
251     result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
252     CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
253
254     // create output mix, with environmental reverb specified as a non-required interface
255     const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
256     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
257     result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
258     CHECK_OPENSL_ERROR( "Failed to create output mix" );
259
260     // realize the output mix in synchronous mode
261     result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
262     CHECK_OPENSL_ERROR( "Failed to realize output mix" );
263
264
265     // configure audio source - this defines the number of samples you can enqueue.
266     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
267         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
268         BUFF_QUEUE
269     };
270
271     SLDataFormat_PCM format_pcm;
272     format_pcm.formatType       = SL_DATAFORMAT_PCM;
273     format_pcm.numChannels      = 2;
274     format_pcm.samplesPerSec    = ((SLuint32) p_aout->format.i_rate * 1000) ;
275     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
276     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
277     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
278     format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;
279
280     SLDataSource audioSrc = {&loc_bufq, &format_pcm};
281
282     // configure audio sink
283     SLDataLocator_OutputMix loc_outmix = {
284         SL_DATALOCATOR_OUTPUTMIX,
285         p_sys->outputMixObject
286     };
287     SLDataSink audioSnk = {&loc_outmix, NULL};
288
289     //create audio player
290     const SLInterfaceID ids2[] = { *SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
291     static const SLboolean req2[] = { SL_BOOLEAN_TRUE };
292     result = CreateAudioPlayer( engineEngine, &p_sys->playerObject, &audioSrc,
293                                     &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
294                                     ids2, req2 );
295     CHECK_OPENSL_ERROR( "Failed to create audio player" );
296
297     result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
298     CHECK_OPENSL_ERROR( "Failed to realize player object." );
299
300     result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
301     CHECK_OPENSL_ERROR( "Failed to get player interface." );
302
303     result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
304                                                   &p_sys->playerBufferQueue );
305     CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
306
307     result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
308                                    (void*)p_sys);
309     CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
310
311
312     // set the player's state to playing
313     result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
314     CHECK_OPENSL_ERROR( "Failed to switch to playing state" );
315
316     vlc_mutex_init( &p_sys->lock );
317     p_sys->p_chain = NULL;
318     p_sys->pp_last = &p_sys->p_chain;
319
320     // we want 16bit signed data little endian.
321     p_aout->format.i_format              = VLC_CODEC_S16L;
322     p_aout->format.i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
323     p_aout->pf_play                      = Play;
324     p_aout->pf_pause                     = NULL;
325     p_aout->pf_flush                     = NULL;
326
327     aout_FormatPrepare( &p_aout->format );
328
329     return VLC_SUCCESS;
330 error:
331     Clean( p_sys );
332     return VLC_EGENERIC;
333 }
334
335 /*****************************************************************************
336  * Close
337  *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
339 {
340     audio_output_t *p_aout = (audio_output_t*)p_this;
341     aout_sys_t     *p_sys = p_aout->sys;
342
343     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
344     //Flush remaining buffers if any.
345     Clear( p_sys->playerBufferQueue );
346     block_ChainRelease( p_sys->p_chain );
347     vlc_mutex_destroy( &p_sys->lock );
348     Clean( p_sys );
349 }