]> git.sesse.net Git - vlc/blob - modules/audio_output/opensles_android.c
OpenSLES: remove leftover
[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 /*****************************************************************************
46  * aout_sys_t: audio output method descriptor
47  *****************************************************************************
48  * This structure is part of the audio output thread descriptor.
49  * It describes the direct sound specific properties of an audio device.
50  *****************************************************************************/
51 struct aout_sys_t
52 {
53     SLObjectItf                     engineObject;
54     SLEngineItf                     engineEngine;
55     SLObjectItf                     outputMixObject;
56     SLAndroidSimpleBufferQueueItf   playerBufferQueue;
57     SLObjectItf                     playerObject;
58     SLPlayItf                       playerPlay;
59     aout_buffer_t                 * p_buffer_array[BUFF_QUEUE];
60     int                             i_toclean_buffer;
61     int                             i_toappend_buffer;
62     SLInterfaceID                 * SL_IID_ENGINE;
63     SLInterfaceID                 * SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
64     SLInterfaceID                 * SL_IID_VOLUME;
65     SLInterfaceID                 * SL_IID_PLAY;
66     void                          * p_so_handle;
67 };
68
69 typedef SLresult (*slCreateEngine_t)(
70         SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
71         const SLInterfaceID*, const SLboolean* );
72
73 /*****************************************************************************
74  * Local prototypes.
75  *****************************************************************************/
76 static int  Open        ( vlc_object_t * );
77 static void Close       ( vlc_object_t * );
78 static void Play        ( audio_output_t *, block_t * );
79 static void PlayedCallback ( SLAndroidSimpleBufferQueueItf caller,  void *pContext);
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84
85 vlc_module_begin ()
86     set_description( N_("OpenSLES audio output") )
87     set_shortname( N_("OpenSLES") )
88     set_category( CAT_AUDIO )
89     set_subcategory( SUBCAT_AUDIO_AOUT )
90
91     set_capability( "audio output", 170 )
92     add_shortcut( "opensles", "android" )
93     set_callbacks( Open, Close )
94 vlc_module_end ()
95
96
97 #define CHECK_OPENSL_ERROR( res, msg )              \
98     if( unlikely( res != SL_RESULT_SUCCESS ) )      \
99     {                                               \
100         msg_Err( p_aout, msg" (%lu)", res );        \
101         goto error;                                 \
102     }
103
104 #define OPENSL_DLSYM( dest, handle, name )                   \
105     dest = (typeof(dest))dlsym( handle, name );              \
106     if( dest == NULL )                                       \
107     {                                                        \
108         msg_Err( p_aout, "Failed to load symbol %s", name ); \
109         goto error;                                          \
110     }
111
112 static void Clear( aout_sys_t *p_sys )
113 {
114     // Destroy buffer queue audio player object
115     // and invalidate all associated interfaces
116     if( p_sys->playerObject != NULL )
117         (*p_sys->playerObject)->Destroy( p_sys->playerObject );
118
119     // destroy output mix object, and invalidate all associated interfaces
120     if( p_sys->outputMixObject != NULL )
121         (*p_sys->outputMixObject)->Destroy( p_sys->outputMixObject );
122
123     // destroy engine object, and invalidate all associated interfaces
124     if( p_sys->engineObject != NULL )
125         (*p_sys->engineObject)->Destroy( p_sys->engineObject );
126
127     if( p_sys->p_so_handle != NULL )
128         dlclose( p_sys->p_so_handle );
129
130     free( p_sys );
131 }
132
133 /*****************************************************************************
134  * Open: open a dummy audio device
135  *****************************************************************************/
136 static int Open( vlc_object_t * p_this )
137 {
138     audio_output_t     *p_aout = (audio_output_t *)p_this;
139     SLresult            result;
140
141     /* Allocate structure */
142     p_aout->sys = malloc( sizeof( aout_sys_t ) );
143     if( unlikely( p_aout->sys == NULL ) )
144         return VLC_ENOMEM;
145
146     aout_sys_t * p_sys = p_aout->sys;
147
148     p_sys->playerObject     = NULL;
149     p_sys->engineObject     = NULL;
150     p_sys->outputMixObject  = NULL;
151     p_sys->i_toclean_buffer = 0;
152     p_sys->i_toappend_buffer= 0;
153
154     //Acquiring LibOpenSLES symbols :
155     p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
156     if( p_sys->p_so_handle == NULL )
157     {
158         msg_Err( p_aout, "Failed to load libOpenSLES" );
159         goto error;
160     }
161
162     slCreateEngine_t    slCreateEnginePtr = NULL;
163
164     OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
165     OPENSL_DLSYM( p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
166                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
167     OPENSL_DLSYM( p_sys->SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
168     OPENSL_DLSYM( p_sys->SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
169     OPENSL_DLSYM( p_sys->SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
170
171     // create engine
172     result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
173     CHECK_OPENSL_ERROR( result, "Failed to create engine" );
174
175     // realize the engine in synchronous mode
176     result = (*p_sys->engineObject)->Realize( p_sys->engineObject,
177                                              SL_BOOLEAN_FALSE );
178     CHECK_OPENSL_ERROR( result, "Failed to realize engine" );
179
180     // get the engine interface, needed to create other objects
181     result = (*p_sys->engineObject)->GetInterface( p_sys->engineObject,
182                                         *p_sys->SL_IID_ENGINE, &p_sys->engineEngine );
183     CHECK_OPENSL_ERROR( result, "Failed to get the engine interface" );
184
185     // create output mix, with environmental reverb specified as a non-required interface
186     const SLInterfaceID ids1[] = { *p_sys->SL_IID_VOLUME };
187     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
188     result = (*p_sys->engineEngine)->CreateOutputMix( p_sys->engineEngine,
189                                         &p_sys->outputMixObject, 1, ids1, req1 );
190     CHECK_OPENSL_ERROR( result, "Failed to create output mix" );
191
192     // realize the output mix in synchronous mode
193     result = (*p_sys->outputMixObject)->Realize( p_sys->outputMixObject,
194                                                  SL_BOOLEAN_FALSE );
195     CHECK_OPENSL_ERROR( result, "Failed to realize output mix" );
196
197
198     // configure audio source - this defines the number of samples you can enqueue.
199     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
200         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
201         BUFF_QUEUE
202     };
203
204     SLDataFormat_PCM format_pcm;
205     format_pcm.formatType       = SL_DATAFORMAT_PCM;
206     format_pcm.numChannels      = 2;
207     format_pcm.samplesPerSec    = ((SLuint32) p_aout->format.i_rate * 1000) ;
208     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
209     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
210     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
211     format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;
212
213     SLDataSource audioSrc = {&loc_bufq, &format_pcm};
214
215     // configure audio sink
216     SLDataLocator_OutputMix loc_outmix = {
217         SL_DATALOCATOR_OUTPUTMIX,
218         p_sys->outputMixObject
219     };
220     SLDataSink audioSnk = {&loc_outmix, NULL};
221
222     //create audio player
223     const SLInterfaceID ids2[] = { *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
224     const SLboolean     req2[] = { SL_BOOLEAN_TRUE };
225     result = (*p_sys->engineEngine)->CreateAudioPlayer( p_sys->engineEngine,
226                                     &p_sys->playerObject, &audioSrc,
227                                     &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
228                                     ids2, req2 );
229     CHECK_OPENSL_ERROR( result, "Failed to create audio player" );
230
231     // realize the player
232     result = (*p_sys->playerObject)->Realize( p_sys->playerObject,
233                                               SL_BOOLEAN_FALSE );
234     CHECK_OPENSL_ERROR( result, "Failed to realize player object." );
235
236     // get the play interface
237     result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
238                                                   *p_sys->SL_IID_PLAY, &p_sys->playerPlay );
239     CHECK_OPENSL_ERROR( result, "Failed to get player interface." );
240
241     // get the buffer queue interface
242     result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
243                                                   *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
244                                                   &p_sys->playerBufferQueue );
245     CHECK_OPENSL_ERROR( result, "Failed to get buff queue interface" );
246
247     result = (*p_sys->playerBufferQueue)->RegisterCallback( p_sys->playerBufferQueue,
248                                                             PlayedCallback,
249                                                             (void*)p_sys);
250     CHECK_OPENSL_ERROR( result, "Failed to register buff queue callback." );
251
252
253     // set the player's state to playing
254     result = (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay,
255                                                  SL_PLAYSTATE_PLAYING );
256     CHECK_OPENSL_ERROR( result, "Failed to switch to playing state" );
257
258     // we want 16bit signed data little endian.
259     p_aout->format.i_format              = VLC_CODEC_S16L;
260     p_aout->i_nb_samples                 = 2048;
261     p_aout->format.i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
262     p_aout->pf_play                      = Play;
263     p_aout->pf_pause                     = NULL;
264     p_aout->pf_flush                     = NULL;
265
266     aout_FormatPrepare( &p_aout->format );
267
268     return VLC_SUCCESS;
269 error:
270     Clear( p_sys );
271     return VLC_EGENERIC;
272 }
273
274 /*****************************************************************************
275  * Close: close our file
276  *****************************************************************************/
277 static void Close( vlc_object_t * p_this )
278 {
279     audio_output_t *p_aout = (audio_output_t *)p_this;
280     aout_sys_t      *p_sys = p_aout->sys;
281
282     msg_Dbg( p_aout, "Closing OpenSLES" );
283
284     (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
285     //Flush remaining buffers if any.
286     if( p_sys->playerBufferQueue != NULL )
287         (*p_sys->playerBufferQueue)->Clear( p_sys->playerBufferQueue );
288     Clear( p_sys );
289 }
290
291 /*****************************************************************************
292  * Play: play a sound
293  *****************************************************************************/
294 static void Play( audio_output_t * p_aout, block_t *p_buffer )
295 {
296     aout_sys_t * p_sys = p_aout->sys;
297     SLresult result;
298
299     for (;;)
300     {
301         result = (*p_sys->playerBufferQueue)->Enqueue(
302                             p_sys->playerBufferQueue, p_buffer->p_buffer,
303                             p_buffer->i_buffer );
304         if( result == SL_RESULT_SUCCESS )
305             break;
306         if ( result != SL_RESULT_BUFFER_INSUFFICIENT )
307         {
308             msg_Warn( p_aout, "Dropping invalid buffer" );
309             aout_BufferFree( p_buffer );
310             return ;
311         }
312
313         msg_Err( p_aout, "write error (%lu)", result );
314
315         // Wait a bit to retry. might miss calls to *cancel
316         // but this is supposed to be rare anyway
317         msleep(CLOCK_FREQ);
318     }
319     p_sys->p_buffer_array[p_sys->i_toappend_buffer] = p_buffer;
320     if( ++p_sys->i_toappend_buffer == BUFF_QUEUE )
321         p_sys->i_toappend_buffer = 0;
322 }
323
324 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
325 {
326     aout_sys_t *p_sys = (aout_sys_t*)pContext;
327
328     assert (caller == p_sys->playerBufferQueue);
329
330     aout_BufferFree( p_sys->p_buffer_array[p_sys->i_toclean_buffer] );
331     if( ++p_sys->i_toclean_buffer == BUFF_QUEUE )
332         p_sys->i_toclean_buffer = 0;
333 }