1 /*****************************************************************************
2 * opensles_android.c : audio output for android native code
3 *****************************************************************************
4 * Copyright © 2011-2012 VideoLAN
6 * Authors: Dominique Martinet <asmadeus@codewreck.org>
7 * Hugo Beauzée-Luyssen <beauze.h@gmail.com>
8 * Rafaël Carré <funman@videolanorg>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
40 #include <SLES/OpenSLES.h>
41 #include <SLES/OpenSLES_Android.h>
43 // Maximum number of buffers to enqueue.
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)
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 *****************************************************************************/
66 SLObjectItf engineObject;
67 SLObjectItf outputMixObject;
68 SLAndroidSimpleBufferQueueItf playerBufferQueue;
69 SLObjectItf playerObject;
79 audio_sample_format_t format;
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open ( vlc_object_t * );
87 /*****************************************************************************
89 *****************************************************************************/
92 set_description( N_("OpenSLES audio output") )
93 set_shortname( N_("OpenSLES") )
94 set_category( CAT_AUDIO )
95 set_subcategory( SUBCAT_AUDIO_AOUT )
97 set_capability( "audio output", 170 )
98 add_shortcut( "opensles", "android" )
99 set_callbacks( Open, NULL )
103 static void Clean( aout_sys_t *p_sys )
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 );
112 if( p_sys->p_so_handle )
113 dlclose( p_sys->p_so_handle );
118 static void Flush(audio_output_t *p_aout, bool wait)
120 (void)wait; /* FIXME */
121 aout_sys_t *p_sys = p_aout->sys;
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 );
133 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
136 aout_sys_t *p_sys = p_aout->sys;
137 SetPlayState( p_sys->playerPlay,
138 pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING );
141 static int TimeGet(audio_output_t* p_aout, mtime_t* restrict drift)
143 aout_sys_t *p_sys = p_aout->sys;
145 vlc_mutex_lock( &p_sys->lock );
146 mtime_t delay = p_sys->length;
147 vlc_mutex_unlock( &p_sys->lock );
149 SLAndroidSimpleBufferQueueState st;
150 SLresult res = GetState(p_sys->playerBufferQueue, &st);
151 if (unlikely(res != SL_RESULT_SUCCESS)) {
152 msg_Err(p_aout, "Could not query buffer queue state in TimeGet (%lu)", res);
156 *drift = (delay && st.count) ? delay : 0;
160 /*****************************************************************************
162 *****************************************************************************/
163 static void Play( audio_output_t *p_aout, block_t *p_buffer )
165 aout_sys_t *p_sys = p_aout->sys;
167 block_t **pp_last, *p_block;
169 SLAndroidSimpleBufferQueueState st;
170 SLresult res = GetState(p_sys->playerBufferQueue, &st);
171 if (unlikely(res != SL_RESULT_SUCCESS)) {
172 msg_Err(p_aout, "Could not query buffer queue state (%lu)", res);
176 if (!p_buffer->i_length) {
177 p_buffer->i_length = (mtime_t)(p_buffer->i_buffer / 2 / p_sys->format.i_channels) * CLOCK_FREQ / p_sys->format.i_rate;
180 vlc_mutex_lock( &p_sys->lock );
182 p_sys->length += p_buffer->i_length;
184 /* If something bad happens, we must remove this buffer from the FIFO */
185 pp_last = p_sys->pp_last;
188 block_ChainLastAppend( &p_sys->pp_last, p_buffer );
189 vlc_mutex_unlock( &p_sys->lock );
193 SLresult result = Enqueue( p_sys->playerBufferQueue, p_buffer->p_buffer,
194 p_buffer->i_buffer );
198 case SL_RESULT_BUFFER_INSUFFICIENT:
199 msg_Err( p_aout, "buffer insufficient");
203 // Wait a bit to retry.
204 // FIXME: buffer in aout_sys_t and retry at next Play() call
210 msg_Warn( p_aout, "Error %lu, dropping buffer", result );
211 vlc_mutex_lock( &p_sys->lock );
212 p_sys->pp_last = pp_last;
214 p_sys->length -= p_buffer->i_length;
215 vlc_mutex_unlock( &p_sys->lock );
216 block_Release( p_buffer );
217 case SL_RESULT_SUCCESS:
223 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
227 audio_output_t *p_aout = pContext;
228 aout_sys_t *p_sys = p_aout->sys;
230 assert (caller == p_sys->playerBufferQueue);
232 vlc_mutex_lock( &p_sys->lock );
234 p_block = p_sys->p_chain;
237 p_sys->p_chain = p_sys->p_chain->p_next;
238 /* if we exhausted our fifo, we must reset the pointer to the last
241 p_sys->pp_last = &p_sys->p_chain;
243 p_sys->length -= p_block->i_length;
245 vlc_mutex_unlock( &p_sys->lock );
247 block_Release( p_block );
249 /*****************************************************************************
251 *****************************************************************************/
252 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
255 SLEngineItf engineEngine;
257 /* Allocate structure */
258 p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
259 if( unlikely( p_aout->sys == NULL ) )
262 aout_sys_t *p_sys = p_aout->sys;
264 //Acquiring LibOpenSLES symbols :
265 p_sys->p_so_handle = dlopen( "libOpenSLES.so", RTLD_NOW );
266 if( p_sys->p_so_handle == NULL )
268 msg_Err( p_aout, "Failed to load libOpenSLES" );
272 typedef SLresult (*slCreateEngine_t)(
273 SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
274 const SLInterfaceID*, const SLboolean* );
275 slCreateEngine_t slCreateEnginePtr = NULL;
277 SLInterfaceID *SL_IID_ENGINE;
278 SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
279 SLInterfaceID *SL_IID_VOLUME;
280 SLInterfaceID *SL_IID_PLAY;
282 #define OPENSL_DLSYM( dest, handle, name ) \
283 dest = dlsym( handle, name ); \
286 msg_Err( p_aout, "Failed to load symbol %s", name ); \
290 OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
291 OPENSL_DLSYM( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
292 "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
293 OPENSL_DLSYM( SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
294 OPENSL_DLSYM( SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
295 OPENSL_DLSYM( SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
298 #define CHECK_OPENSL_ERROR( msg ) \
299 if( unlikely( result != SL_RESULT_SUCCESS ) ) \
301 msg_Err( p_aout, msg" (%lu)", result ); \
306 result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
307 CHECK_OPENSL_ERROR( "Failed to create engine" );
309 // realize the engine in synchronous mode
310 result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
311 CHECK_OPENSL_ERROR( "Failed to realize engine" );
313 // get the engine interface, needed to create other objects
314 result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
315 CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
317 // create output mix, with environmental reverb specified as a non-required interface
318 const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
319 const SLboolean req1[] = { SL_BOOLEAN_FALSE };
320 result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
321 CHECK_OPENSL_ERROR( "Failed to create output mix" );
323 // realize the output mix in synchronous mode
324 result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
325 CHECK_OPENSL_ERROR( "Failed to realize output mix" );
328 // configure audio source - this defines the number of samples you can enqueue.
329 SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
330 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
334 SLDataFormat_PCM format_pcm;
335 format_pcm.formatType = SL_DATAFORMAT_PCM;
336 format_pcm.numChannels = 2;
337 format_pcm.samplesPerSec = ((SLuint32) fmt->i_rate * 1000) ;
338 format_pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
339 format_pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
340 format_pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
341 format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
343 SLDataSource audioSrc = {&loc_bufq, &format_pcm};
345 // configure audio sink
346 SLDataLocator_OutputMix loc_outmix = {
347 SL_DATALOCATOR_OUTPUTMIX,
348 p_sys->outputMixObject
350 SLDataSink audioSnk = {&loc_outmix, NULL};
352 //create audio player
353 const SLInterfaceID ids2[] = { *SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
354 static const SLboolean req2[] = { SL_BOOLEAN_TRUE };
355 result = CreateAudioPlayer( engineEngine, &p_sys->playerObject, &audioSrc,
356 &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
358 CHECK_OPENSL_ERROR( "Failed to create audio player" );
360 result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
361 CHECK_OPENSL_ERROR( "Failed to realize player object." );
363 result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
364 CHECK_OPENSL_ERROR( "Failed to get player interface." );
366 result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
367 &p_sys->playerBufferQueue );
368 CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
370 result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
372 CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
375 // set the player's state to playing
376 result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
377 CHECK_OPENSL_ERROR( "Failed to switch to playing state" );
379 vlc_mutex_init( &p_sys->lock );
380 p_sys->p_chain = NULL;
381 p_sys->pp_last = &p_sys->p_chain;
383 // we want 16bit signed data little endian.
384 fmt->i_format = VLC_CODEC_S16L;
385 fmt->i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
387 p_aout->pause = Pause;
388 p_aout->flush = Flush;
390 aout_FormatPrepare( fmt );
392 p_sys->format = *fmt;
400 /*****************************************************************************
402 *****************************************************************************/
403 static void Stop( audio_output_t *p_aout )
405 aout_sys_t *p_sys = p_aout->sys;
407 SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
408 //Flush remaining buffers if any.
409 Clear( p_sys->playerBufferQueue );
410 block_ChainRelease( p_sys->p_chain );
411 vlc_mutex_destroy( &p_sys->lock );
415 static int Open (vlc_object_t *obj)
417 audio_output_t *aout = (audio_output_t *)obj;
419 /* FIXME: set volume/mute here */
422 aout->time_get = TimeGet;