1 /*****************************************************************************
2 * opensles_android.c : audio output for android native code
3 *****************************************************************************
4 * Copyright © 2011 VideoLAN
6 * Authors: Dominique Martinet <asmadeus@codewreck.org>
7 * Hugo Beauzée-Luyssen <beauze.h@gmail.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
39 #include <SLES/OpenSLES.h>
40 #include <SLES/OpenSLES_Android.h>
42 // Maximum number of buffers to enqueue.
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)
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 *****************************************************************************/
64 SLObjectItf engineObject;
65 SLObjectItf outputMixObject;
66 SLAndroidSimpleBufferQueueItf playerBufferQueue;
67 SLObjectItf playerObject;
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open ( vlc_object_t * );
82 static void Close ( vlc_object_t * );
84 /*****************************************************************************
86 *****************************************************************************/
89 set_description( N_("OpenSLES audio output") )
90 set_shortname( N_("OpenSLES") )
91 set_category( CAT_AUDIO )
92 set_subcategory( SUBCAT_AUDIO_AOUT )
94 set_capability( "audio output", 170 )
95 add_shortcut( "opensles", "android" )
96 set_callbacks( Open, Close )
100 static void Clean( aout_sys_t *p_sys )
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 );
109 if( p_sys->p_so_handle )
110 dlclose( p_sys->p_so_handle );
115 /*****************************************************************************
117 *****************************************************************************/
118 static void Play( audio_output_t *p_aout, block_t *p_buffer )
120 aout_sys_t *p_sys = p_aout->sys;
122 block_t **pp_last, *p_block;;
124 vlc_mutex_lock( &p_sys->lock );
126 /* If something bad happens, we must remove this buffer from the FIFO */
127 pp_last = p_sys->pp_last;
130 block_ChainLastAppend( &p_sys->pp_last, p_buffer );
131 vlc_mutex_unlock( &p_sys->lock );
135 SLresult result = Enqueue( p_sys->playerBufferQueue, p_buffer->p_buffer,
136 p_buffer->i_buffer );
140 case SL_RESULT_BUFFER_INSUFFICIENT:
141 msg_Err( p_aout, "buffer insufficient");
145 // Wait a bit to retry.
151 msg_Warn( p_aout, "Error %lu, dropping buffer", result );
152 vlc_mutex_lock( &p_sys->lock );
153 p_sys->pp_last = pp_last;
155 vlc_mutex_unlock( &p_sys->lock );
156 block_Release( p_buffer );
157 case SL_RESULT_SUCCESS:
163 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
166 aout_sys_t *p_sys = pContext;
168 assert (caller == p_sys->playerBufferQueue);
170 vlc_mutex_lock( &p_sys->lock );
172 p_block = p_sys->p_chain;
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
179 p_sys->pp_last = &p_sys->p_chain;
181 vlc_mutex_unlock( &p_sys->lock );
183 block_Release( p_block );
185 /*****************************************************************************
187 *****************************************************************************/
188 static int Open( vlc_object_t *p_this )
190 audio_output_t *p_aout = (audio_output_t *)p_this;
192 SLEngineItf engineEngine;
194 /* Allocate structure */
195 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
196 if( unlikely( p_aout->sys == NULL ) )
199 aout_sys_t *p_sys = p_aout->sys;
201 //Acquiring LibOpenSLES symbols :
202 p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
203 if( p_sys->p_so_handle == NULL )
205 msg_Err( p_aout, "Failed to load libOpenSLES" );
209 typedef SLresult (*slCreateEngine_t)(
210 SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
211 const SLInterfaceID*, const SLboolean* );
212 slCreateEngine_t slCreateEnginePtr = NULL;
214 SLInterfaceID *SL_IID_ENGINE;
215 SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
216 SLInterfaceID *SL_IID_VOLUME;
217 SLInterfaceID *SL_IID_PLAY;
219 #define OPENSL_DLSYM( dest, handle, name ) \
220 dest = dlsym( handle, name ); \
223 msg_Err( p_aout, "Failed to load symbol %s", name ); \
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" );
235 #define CHECK_OPENSL_ERROR( msg ) \
236 if( unlikely( result != SL_RESULT_SUCCESS ) ) \
238 msg_Err( p_aout, msg" (%lu)", result ); \
243 result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
244 CHECK_OPENSL_ERROR( "Failed to create engine" );
246 // realize the engine in synchronous mode
247 result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
248 CHECK_OPENSL_ERROR( "Failed to realize engine" );
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" );
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" );
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" );
265 // configure audio source - this defines the number of samples you can enqueue.
266 SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
267 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
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;
280 SLDataSource audioSrc = {&loc_bufq, &format_pcm};
282 // configure audio sink
283 SLDataLocator_OutputMix loc_outmix = {
284 SL_DATALOCATOR_OUTPUTMIX,
285 p_sys->outputMixObject
287 SLDataSink audioSnk = {&loc_outmix, NULL};
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 ),
295 CHECK_OPENSL_ERROR( "Failed to create audio player" );
297 result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
298 CHECK_OPENSL_ERROR( "Failed to realize player object." );
300 result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
301 CHECK_OPENSL_ERROR( "Failed to get player interface." );
303 result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
304 &p_sys->playerBufferQueue );
305 CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
307 result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
309 CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
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" );
316 vlc_mutex_init( &p_sys->lock );
317 p_sys->p_chain = NULL;
318 p_sys->pp_last = &p_sys->p_chain;
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;
327 aout_FormatPrepare( &p_aout->format );
335 /*****************************************************************************
337 *****************************************************************************/
338 static void Close( vlc_object_t *p_this )
340 audio_output_t *p_aout = (audio_output_t*)p_this;
341 aout_sys_t *p_sys = p_aout->sys;
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 );