]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/opensles_android.c
OSS: trivial fixes
[vlc] / modules / audio_output / opensles_android.c
index 3ba34f6b5e04ce0c711cc4541f93aa0f78a5b20b..147d44ad812991dabe3b1a9ea0a70ce8e1852412 100644 (file)
@@ -1,22 +1,23 @@
 /*****************************************************************************
  * opensles_android.c : audio output for android native code
  *****************************************************************************
- * Copyright © 2011 VideoLAN
+ * Copyright © 2011-2012 VideoLAN
  *
  * Authors: Dominique Martinet <asmadeus@codewreck.org>
  *          Hugo Beauzée-Luyssen <beauze.h@gmail.com>
+ *          Rafaël Carré <funman@videolanorg>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
+ * You should have received a copy of the GNU Lesser General Public License
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 #include <vlc_aout.h>
 #include <assert.h>
 #include <dlfcn.h>
+#include <math.h>
 
 // For native audio
 #include <SLES/OpenSLES.h>
 #include <SLES/OpenSLES_Android.h>
 
-// Maximum number of buffers to enqueue.
-#define BUFF_QUEUE  42
+#define Destroy(a) (*a)->Destroy(a);
+#define SetPlayState(a, b) (*a)->SetPlayState(a, b)
+#define RegisterCallback(a, b, c) (*a)->RegisterCallback(a, b, c)
+#define GetInterface(a, b, c) (*a)->GetInterface(a, b, c)
+#define Realize(a, b) (*a)->Realize(a, b)
+#define CreateOutputMix(a, b, c, d, e) (*a)->CreateOutputMix(a, b, c, d, e)
+#define CreateAudioPlayer(a, b, c, d, e, f, g) \
+    (*a)->CreateAudioPlayer(a, b, c, d, e, f, g)
+#define Enqueue(a, b, c) (*a)->Enqueue(a, b, c)
+#define Clear(a) (*a)->Clear(a)
+#define GetState(a, b) (*a)->GetState(a, b)
+#define SetPositionUpdatePeriod(a, b) (*a)->SetPositionUpdatePeriod(a, b)
+#define SetVolumeLevel(a, b) (*a)->SetVolumeLevel(a, b)
+#define SetMute(a, b) (*a)->SetMute(a, b)
 
 /*****************************************************************************
  * aout_sys_t: audio output method descriptor
 struct aout_sys_t
 {
     SLObjectItf                     engineObject;
-    SLEngineItf                     engineEngine;
     SLObjectItf                     outputMixObject;
     SLAndroidSimpleBufferQueueItf   playerBufferQueue;
     SLObjectItf                     playerObject;
+    SLVolumeItf                     volumeItf;
+
     SLPlayItf                       playerPlay;
-    aout_buffer_t                 * p_buffer_array[BUFF_QUEUE];
-    int                             i_toclean_buffer;
-    int                             i_toappend_buffer;
-    SLInterfaceID                 * SL_IID_ENGINE;
-    SLInterfaceID                 * SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
-    SLInterfaceID                 * SL_IID_VOLUME;
-    SLInterfaceID                 * SL_IID_PLAY;
-    void                          * p_so_handle;
-};
 
-typedef SLresult (*slCreateEngine_t)(
-        SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
-        const SLInterfaceID*, const SLboolean* );
+    vlc_mutex_t                     lock;
+    mtime_t                         length;
+
+    int                             buffers;
+    mtime_t                         last_callback;
+
+    /* audio buffered through opensles */
+    block_t                        *p_chain;
+    block_t                       **pp_last;
+
+    /* audio not yet buffered through opensles */
+    block_t                        *p_buffer_chain;
+    block_t                       **pp_buffer_last;
+
+    void                           *p_so_handle;
+    audio_sample_format_t           format;
+};
 
 /*****************************************************************************
  * Local prototypes.
  *****************************************************************************/
 static int  Open        ( vlc_object_t * );
-static void Close       ( vlc_object_t * );
-static void Play        ( audio_output_t *, block_t * );
-static void PlayedCallback ( SLAndroidSimpleBufferQueueItf caller,  void *pContext);
 
 /*****************************************************************************
  * Module descriptor
@@ -90,49 +107,216 @@ vlc_module_begin ()
 
     set_capability( "audio output", 170 )
     add_shortcut( "opensles", "android" )
-    set_callbacks( Open, Close )
+    set_callbacks( Open, NULL )
 vlc_module_end ()
 
 
-#define CHECK_OPENSL_ERROR( res, msg )              \
-    if( unlikely( res != SL_RESULT_SUCCESS ) )      \
-    {                                               \
-        msg_Err( p_aout, msg" (%lu)", res );        \
-        goto error;                                 \
+static void Clean( aout_sys_t *p_sys )
+{
+    if( p_sys->playerObject )
+        Destroy( p_sys->playerObject );
+    if( p_sys->outputMixObject )
+        Destroy( p_sys->outputMixObject );
+    if( p_sys->engineObject )
+        Destroy( p_sys->engineObject );
+
+    if( p_sys->p_so_handle )
+        dlclose( p_sys->p_so_handle );
+
+    free( p_sys );
+}
+
+static void Flush(audio_output_t *p_aout, bool drain)
+{
+    aout_sys_t *p_sys = p_aout->sys;
+
+    if (drain) {
+        mtime_t delay;
+        vlc_mutex_lock( &p_sys->lock );
+        delay = p_sys->length;
+        vlc_mutex_unlock( &p_sys->lock );
+        msleep(delay);
+    } else {
+        vlc_mutex_lock( &p_sys->lock );
+        SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
+        Clear( p_sys->playerBufferQueue );
+        SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
+
+        p_sys->length = 0;
+
+        /* release audio data not yet written to opensles */
+        block_ChainRelease( p_sys->p_buffer_chain );
+        p_sys->p_buffer_chain = NULL;
+        p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
+
+        /* release audio data written to opensles, but not yet
+         * played on hardware */
+        block_ChainRelease( p_sys->p_chain );
+        p_sys->p_chain = NULL;
+        p_sys->pp_last = &p_sys->p_chain;
+
+        vlc_mutex_unlock( &p_sys->lock );
     }
+}
 
-#define OPENSL_DLSYM( dest, handle, name )                   \
-    dest = (typeof(dest))dlsym( handle, name );              \
-    if( dest == NULL )                                       \
-    {                                                        \
-        msg_Err( p_aout, "Failed to load symbol %s", name ); \
-        goto error;                                          \
+static int VolumeSet(audio_output_t *aout, float vol)
+{
+    /* Convert UI volume to linear factor (cube) */
+    vol = vol * vol * vol;
+
+    /* millibels from linear amplification */
+    int mb = lroundf(2000.f * log10f(vol));
+    if (mb < SL_MILLIBEL_MIN)
+        mb = SL_MILLIBEL_MIN;
+    else if (mb > 0)
+        mb = 0; /* maximum supported level could be higher: GetMaxVolumeLevel */
+
+    SLresult r = SetVolumeLevel(aout->sys->volumeItf, mb);
+    return (r == SL_RESULT_SUCCESS) ? 0 : -1;
+}
+
+static int MuteSet(audio_output_t *aout, bool mute)
+{
+    SLresult r = SetMute(aout->sys->volumeItf, mute);
+    return (r == SL_RESULT_SUCCESS) ? 0 : -1;
+}
+
+static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
+{
+    (void)date;
+    aout_sys_t *p_sys = p_aout->sys;
+    SetPlayState( p_sys->playerPlay,
+        pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING );
+}
+
+static int TimeGet(audio_output_t* p_aout, mtime_t* restrict drift)
+{
+    aout_sys_t *p_sys = p_aout->sys;
+
+    vlc_mutex_lock( &p_sys->lock );
+    mtime_t delay = p_sys->length;
+    mtime_t last_callback = p_sys->last_callback;
+    vlc_mutex_unlock( &p_sys->lock );
+
+    if (last_callback != 0)
+        delay += last_callback - mdate();
+
+    SLAndroidSimpleBufferQueueState st;
+    SLresult res = GetState(p_sys->playerBufferQueue, &st);
+    if (unlikely(res != SL_RESULT_SUCCESS)) {
+        msg_Err(p_aout, "Could not query buffer queue state in TimeGet (%lu)", res);
+        return -1;
     }
 
-static void Clear( aout_sys_t *p_sys )
+    if (delay == 0 || st.count == 0)
+        return -1;
+
+    *drift = delay;
+    return 0;
+}
+
+static int WriteBuffer(audio_output_t *p_aout)
 {
-    // Destroy buffer queue audio player object
-    // and invalidate all associated interfaces
-    (*p_sys->playerObject)->Destroy( p_sys->playerObject );
+    aout_sys_t *p_sys = p_aout->sys;
 
-    // destroy output mix object, and invalidate all associated interfaces
-    (*p_sys->outputMixObject)->Destroy( p_sys->outputMixObject );
+    block_t *b = p_sys->p_buffer_chain;
+    if (!b)
+        return false;
+
+    if (!b->i_length)
+        b->i_length = (mtime_t)(b->i_buffer / 2 / p_sys->format.i_channels) * CLOCK_FREQ / p_sys->format.i_rate;
+
+    /* If something bad happens, we must remove this buffer from the FIFO */
+    block_t **pp_last_saved = p_sys->pp_last;
+    block_t *p_last_saved = *pp_last_saved;
+    block_t *next_saved = b->p_next;
+    b->p_next = NULL;
+
+    /* Put this block in the list of audio already written to opensles */
+    block_ChainLastAppend( &p_sys->pp_last, b );
+
+    mtime_t len = b->i_length;
+    p_sys->length += len;
+    block_t *next = b->p_next;
+
+    vlc_mutex_unlock( &p_sys->lock );
+    SLresult r = Enqueue( p_sys->playerBufferQueue, b->p_buffer, b->i_buffer );
+    vlc_mutex_lock( &p_sys->lock );
+
+    if (r == SL_RESULT_SUCCESS) {
+        /* Remove that block from the list of audio not yet written */
+        p_sys->buffers++;
+        p_sys->p_buffer_chain = next;
+        if (!p_sys->p_buffer_chain)
+            p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
+    } else {
+        /* Remove that block from the list of audio already written */
+        msg_Err( p_aout, "error %lu when writing %d bytes, %d/255 buffers occupied %s",
+                r, b->i_buffer, p_sys->buffers,
+                (r == SL_RESULT_BUFFER_INSUFFICIENT) ? " (buffer insufficient)" : "");
+
+        p_sys->pp_last = pp_last_saved;
+        *pp_last_saved = p_last_saved;
+
+        b->p_next = next_saved;
+
+        p_sys->length -= len;
+        next = NULL; /* We'll try again next time */
+    }
 
-    // destroy engine object, and invalidate all associated interfaces
-    (*p_sys->engineObject)->Destroy( p_sys->engineObject );
+    return next != NULL;
+}
 
-    dlclose( p_sys->p_so_handle );
+/*****************************************************************************
+ * Play: play a sound
+ *****************************************************************************/
+static void Play( audio_output_t *p_aout, block_t *p_buffer )
+{
+    aout_sys_t *p_sys = p_aout->sys;
 
-    free( p_sys );
+    p_buffer->p_next = NULL; /* Make sur our linked list doesn't use old references */
+    vlc_mutex_lock(&p_sys->lock);
+    block_ChainLastAppend( &p_sys->pp_buffer_last, p_buffer );
+    while (WriteBuffer(p_aout))
+        ;
+    vlc_mutex_unlock( &p_sys->lock );
 }
 
+static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
+{
+    (void)caller;
+    block_t *p_block;
+    audio_output_t *p_aout = pContext;
+    aout_sys_t *p_sys = p_aout->sys;
+
+    assert (caller == p_sys->playerBufferQueue);
+
+    vlc_mutex_lock( &p_sys->lock );
+    p_sys->buffers--;
+    p_sys->last_callback = mdate();
+
+    p_block = p_sys->p_chain;
+    assert( p_block );
+
+    p_sys->p_chain = p_sys->p_chain->p_next;
+    /* if we exhausted our fifo, we must reset the pointer to the last
+     * appended block */
+    if (!p_sys->p_chain)
+        p_sys->pp_last = &p_sys->p_chain;
+
+    p_sys->length -= p_block->i_length;
+
+    vlc_mutex_unlock( &p_sys->lock );
+
+    block_Release( p_block );
+}
 /*****************************************************************************
- * Open: open a dummy audio device
+ * Open
  *****************************************************************************/
-static int Open( vlc_object_t *p_this )
+static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
 {
-    audio_output_t     *p_aout = (audio_output_t *)p_this;
-    SLresult            result;
+    SLresult       result;
+    SLEngineItf    engineEngine;
 
     /* Allocate structure */
     p_aout->sys = calloc( 1, sizeof( aout_sys_t ) );
@@ -149,52 +333,72 @@ static int Open( vlc_object_t *p_this )
         goto error;
     }
 
+    typedef SLresult (*slCreateEngine_t)(
+            SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
+            const SLInterfaceID*, const SLboolean* );
     slCreateEngine_t    slCreateEnginePtr = NULL;
 
+    SLInterfaceID *SL_IID_ENGINE;
+    SLInterfaceID *SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
+    SLInterfaceID *SL_IID_VOLUME;
+    SLInterfaceID *SL_IID_PLAY;
+
+#define OPENSL_DLSYM( dest, handle, name )                   \
+    dest = dlsym( handle, name );                            \
+    if( dest == NULL )                                       \
+    {                                                        \
+        msg_Err( p_aout, "Failed to load symbol %s", name ); \
+        goto error;                                          \
+    }
+
     OPENSL_DLSYM( slCreateEnginePtr, p_sys->p_so_handle, "slCreateEngine" );
-    OPENSL_DLSYM( p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
+    OPENSL_DLSYM( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
-    OPENSL_DLSYM( p_sys->SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
-    OPENSL_DLSYM( p_sys->SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
-    OPENSL_DLSYM( p_sys->SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
+    OPENSL_DLSYM( SL_IID_ENGINE, p_sys->p_so_handle, "SL_IID_ENGINE" );
+    OPENSL_DLSYM( SL_IID_PLAY, p_sys->p_so_handle, "SL_IID_PLAY" );
+    OPENSL_DLSYM( SL_IID_VOLUME, p_sys->p_so_handle, "SL_IID_VOLUME" );
+
+
+#define CHECK_OPENSL_ERROR( msg )                   \
+    if( unlikely( result != SL_RESULT_SUCCESS ) )   \
+    {                                               \
+        msg_Err( p_aout, msg" (%lu)", result );     \
+        goto error;                                 \
+    }
 
     // create engine
     result = slCreateEnginePtr( &p_sys->engineObject, 0, NULL, 0, NULL, NULL );
-    CHECK_OPENSL_ERROR( result, "Failed to create engine" );
+    CHECK_OPENSL_ERROR( "Failed to create engine" );
 
     // realize the engine in synchronous mode
-    result = (*p_sys->engineObject)->Realize( p_sys->engineObject,
-                                             SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize engine" );
+    result = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
+    CHECK_OPENSL_ERROR( "Failed to realize engine" );
 
     // get the engine interface, needed to create other objects
-    result = (*p_sys->engineObject)->GetInterface( p_sys->engineObject,
-                                        *p_sys->SL_IID_ENGINE, &p_sys->engineEngine );
-    CHECK_OPENSL_ERROR( result, "Failed to get the engine interface" );
+    result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
+    CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
 
     // create output mix, with environmental reverb specified as a non-required interface
-    const SLInterfaceID ids1[] = { *p_sys->SL_IID_VOLUME };
+    const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
-    result = (*p_sys->engineEngine)->CreateOutputMix( p_sys->engineEngine,
-                                        &p_sys->outputMixObject, 1, ids1, req1 );
-    CHECK_OPENSL_ERROR( result, "Failed to create output mix" );
+    result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
+    CHECK_OPENSL_ERROR( "Failed to create output mix" );
 
     // realize the output mix in synchronous mode
-    result = (*p_sys->outputMixObject)->Realize( p_sys->outputMixObject,
-                                                 SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize output mix" );
+    result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
+    CHECK_OPENSL_ERROR( "Failed to realize output mix" );
 
 
     // configure audio source - this defines the number of samples you can enqueue.
     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
-        BUFF_QUEUE
+        255 // Maximum number of buffers to enqueue.
     };
 
     SLDataFormat_PCM format_pcm;
     format_pcm.formatType       = SL_DATAFORMAT_PCM;
     format_pcm.numChannels      = 2;
-    format_pcm.samplesPerSec    = ((SLuint32) p_aout->format.i_rate * 1000) ;
+    format_pcm.samplesPerSec    = ((SLuint32) fmt->i_rate * 1000) ;
     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
@@ -210,117 +414,84 @@ static int Open( vlc_object_t *p_this )
     SLDataSink audioSnk = {&loc_outmix, NULL};
 
     //create audio player
-    const SLInterfaceID ids2[] = { *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
-    const SLboolean     req2[] = { SL_BOOLEAN_TRUE };
-    result = (*p_sys->engineEngine)->CreateAudioPlayer( p_sys->engineEngine,
-                                    &p_sys->playerObject, &audioSrc,
+    const SLInterfaceID ids2[] = { *SL_IID_ANDROIDSIMPLEBUFFERQUEUE, *SL_IID_VOLUME };
+    static const SLboolean req2[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
+    result = CreateAudioPlayer( engineEngine, &p_sys->playerObject, &audioSrc,
                                     &audioSnk, sizeof( ids2 ) / sizeof( *ids2 ),
                                     ids2, req2 );
-    CHECK_OPENSL_ERROR( result, "Failed to create audio player" );
+    CHECK_OPENSL_ERROR( "Failed to create audio player" );
 
-    // realize the player
-    result = (*p_sys->playerObject)->Realize( p_sys->playerObject,
-                                              SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize player object." );
+    result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
+    CHECK_OPENSL_ERROR( "Failed to realize player object." );
 
-    // get the play interface
-    result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
-                                                  *p_sys->SL_IID_PLAY, &p_sys->playerPlay );
-    CHECK_OPENSL_ERROR( result, "Failed to get player interface." );
+    result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
+    CHECK_OPENSL_ERROR( "Failed to get player interface." );
 
-    // get the buffer queue interface
-    result = (*p_sys->playerObject)->GetInterface( p_sys->playerObject,
-                                                  *p_sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
-                                                  &p_sys->playerBufferQueue );
-    CHECK_OPENSL_ERROR( result, "Failed to get buff queue interface" );
+    result = GetInterface( p_sys->playerObject, *SL_IID_VOLUME, &p_sys->volumeItf );
+    CHECK_OPENSL_ERROR( "failed to get volume interface." );
 
-    result = (*p_sys->playerBufferQueue)->RegisterCallback( p_sys->playerBufferQueue,
-                                                            PlayedCallback,
-                                                            (void*)p_sys);
-    CHECK_OPENSL_ERROR( result, "Failed to register buff queue callback." );
+    result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
+                                                  &p_sys->playerBufferQueue );
+    CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
 
+    result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
+                                   (void*)p_aout);
+    CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
 
     // set the player's state to playing
-    result = (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay,
-                                                 SL_PLAYSTATE_PLAYING );
-    CHECK_OPENSL_ERROR( result, "Failed to switch to playing state" );
+    result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
+    CHECK_OPENSL_ERROR( "Failed to switch to playing state" );
+
+    vlc_mutex_init( &p_sys->lock );
+    p_sys->p_chain = NULL;
+    p_sys->pp_last = &p_sys->p_chain;
+    p_sys->p_buffer_chain = NULL;
+    p_sys->pp_buffer_last = &p_sys->p_buffer_chain;
 
     // we want 16bit signed data little endian.
-    p_aout->format.i_format              = VLC_CODEC_S16L;
-    p_aout->format.i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
-    p_aout->pf_play                      = Play;
-    p_aout->pf_pause                     = NULL;
-    p_aout->pf_flush                     = NULL;
+    fmt->i_format              = VLC_CODEC_S16L;
+    fmt->i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+    p_aout->play               = Play;
+    p_aout->pause              = Pause;
+    p_aout->flush              = Flush;
+    p_aout->mute_set           = MuteSet;
+    p_aout->volume_set         = VolumeSet;
+
+    SetPositionUpdatePeriod( p_sys->playerPlay, AOUT_MIN_PREPARE_TIME * 1000 / CLOCK_FREQ);
+
+    aout_FormatPrepare( fmt );
 
-    aout_FormatPrepare( &p_aout->format );
+    p_sys->format = *fmt;
 
     return VLC_SUCCESS;
 error:
-    Clear( p_sys );
+    Clean( p_sys );
     return VLC_EGENERIC;
 }
 
 /*****************************************************************************
- * Close: close our file
+ * Close
  *****************************************************************************/
-static void Close( vlc_object_t *p_this )
+static void Stop( audio_output_t *p_aout )
 {
-    audio_output_t *p_aout = (audio_output_t*)p_this;
     aout_sys_t     *p_sys = p_aout->sys;
 
-    (*p_sys->playerPlay)->SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
+    SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
     //Flush remaining buffers if any.
-    (*p_sys->playerBufferQueue)->Clear( p_sys->playerBufferQueue );
-    Clear( p_sys );
-}
-
-/*****************************************************************************
- * Play: play a sound
- *****************************************************************************/
-static void Play( audio_output_t *p_aout, block_t *p_buffer )
-{
-    aout_sys_t *p_sys = p_aout->sys;
-    int tries = 5;
-
-    for (;;)
-    {
-        SLresult result = (*p_sys->playerBufferQueue)->Enqueue(
-                            p_sys->playerBufferQueue, p_buffer->p_buffer,
-                            p_buffer->i_buffer );
-
-        switch (result)
-        {
-        case SL_RESULT_SUCCESS:
-            p_sys->p_buffer_array[p_sys->i_toappend_buffer] = p_buffer;
-            if( ++p_sys->i_toappend_buffer == BUFF_QUEUE )
-                p_sys->i_toappend_buffer = 0;
-            return;
-
-        case SL_RESULT_BUFFER_INSUFFICIENT:
-            msg_Err( p_aout, "buffer insufficient");
-
-            if (tries--)
-            {
-                // Wait a bit to retry.
-                msleep(CLOCK_FREQ);
-                continue;
-            }
-
-        default:
-            msg_Warn( p_aout, "Error %lu, dropping buffer", result );
-            aout_BufferFree( p_buffer );
-            return;
-        }
-    }
+    Clear( p_sys->playerBufferQueue );
+    block_ChainRelease( p_sys->p_chain );
+    block_ChainRelease( p_sys->p_buffer_chain);
+    vlc_mutex_destroy( &p_sys->lock );
+    Clean( p_sys );
 }
 
-static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
+static int Open (vlc_object_t *obj)
 {
-    aout_sys_t *p_sys = pContext;
+    audio_output_t *aout = (audio_output_t *)obj;
 
-    assert (caller == p_sys->playerBufferQueue);
-
-    aout_BufferFree( p_sys->p_buffer_array[p_sys->i_toclean_buffer] );
-    if( ++p_sys->i_toclean_buffer == BUFF_QUEUE )
-        p_sys->i_toclean_buffer = 0;
+    /* FIXME: set volume/mute here */
+    aout->start = Start;
+    aout->stop = Stop;
+    aout->time_get = TimeGet;
+    return VLC_SUCCESS;
 }