]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/opensles_android.c
opensles: remove obsolete code
[vlc] / modules / audio_output / opensles_android.c
index 27793725a91dd421e4181bb2249079c8b614e655..41fd78d31a18ace741fe30ce9eaa481d244be07b 100644 (file)
@@ -7,17 +7,17 @@
  *          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.
  *****************************************************************************/
@@ -40,9 +40,6 @@
 #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)
@@ -71,18 +68,24 @@ struct aout_sys_t
     SLPlayItf                       playerPlay;
 
     vlc_mutex_t                     lock;
+    mtime_t                         length;
+
+    /* audio buffered through opensles */
     block_t                        *p_chain;
     block_t                       **pp_last;
-    mtime_t                         length;
+
+    /* 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 * );
 
 /*****************************************************************************
  * Module descriptor
@@ -96,7 +99,7 @@ vlc_module_begin ()
 
     set_capability( "audio output", 170 )
     add_shortcut( "opensles", "android" )
-    set_callbacks( Open, Close )
+    set_callbacks( Open, NULL )
 vlc_module_end ()
 
 
@@ -115,19 +118,37 @@ static void Clean( aout_sys_t *p_sys )
     free( p_sys );
 }
 
-static void Flush(audio_output_t *p_aout, bool wait)
+static void Flush(audio_output_t *p_aout, bool drain)
 {
-    (void)wait; /* FIXME */
     aout_sys_t *p_sys = p_aout->sys;
 
-    vlc_mutex_lock( &p_sys->lock );
-    SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
-    Clear( p_sys->playerBufferQueue );
-    SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
-    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 );
+    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 );
+    }
 }
 
 static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
@@ -138,72 +159,92 @@ static void Pause(audio_output_t *p_aout, bool pause, mtime_t date)
         pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING );
 }
 
-/*****************************************************************************
- * Play: play a sound
- *****************************************************************************/
-static void Play( audio_output_t *p_aout, block_t *p_buffer )
+static int TimeGet(audio_output_t* p_aout, mtime_t* restrict drift)
 {
     aout_sys_t *p_sys = p_aout->sys;
-    int tries = 5;
-    block_t **pp_last, *p_block;
+
+    vlc_mutex_lock( &p_sys->lock );
+    mtime_t delay = p_sys->length;
+    vlc_mutex_unlock( &p_sys->lock );
 
     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 (%lu)", res);
-        st.count = 0;
+        msg_Err(p_aout, "Could not query buffer queue state in TimeGet (%lu)", res);
+        return -1;
     }
 
-    if (!p_buffer->i_length) {
-        p_buffer->i_length = (mtime_t)(p_buffer->i_buffer / 2 / p_aout->format.i_channels) * CLOCK_FREQ / p_aout->format.i_rate;
-    }
+    if (delay == 0 || st.count == 0)
+        return -1;
 
-    vlc_mutex_lock( &p_sys->lock );
+    *drift = delay;
+    return 0;
+}
 
-    mtime_t delay = p_sys->length;
-    p_sys->length += p_buffer->i_length;
+static int WriteBuffer(audio_output_t *p_aout)
+{
+    aout_sys_t *p_sys = p_aout->sys;
+
+    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 */
-    pp_last = p_sys->pp_last;
-    p_block = *pp_last;
+    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;
 
-    block_ChainLastAppend( &p_sys->pp_last, p_buffer );
     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 (delay && st.count) {
-        aout_TimeReport(p_aout, p_buffer->i_pts - delay);
-    }
+    if (r == SL_RESULT_SUCCESS) {
+        /* Remove that block from the list of audio not yet written */
+        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%s (%d bytes)", r, (r == SL_RESULT_BUFFER_INSUFFICIENT)
+                ? " buffer insufficient"
+                : "", b->i_buffer);
 
-    for (;;)
-    {
-        SLresult result = Enqueue( p_sys->playerBufferQueue, p_buffer->p_buffer,
-                            p_buffer->i_buffer );
-
-        switch (result)
-        {
-        case SL_RESULT_BUFFER_INSUFFICIENT:
-            msg_Err( p_aout, "buffer insufficient");
-
-            if (tries--)
-            {
-                // Wait a bit to retry.
-                // FIXME: buffer in aout_sys_t and retry at next Play() call
-                msleep(CLOCK_FREQ);
-                continue;
-            }
-
-        default:
-            msg_Warn( p_aout, "Error %lu, dropping buffer", result );
-            vlc_mutex_lock( &p_sys->lock );
-            p_sys->pp_last = pp_last;
-            *pp_last = p_block;
-            p_sys->length -= p_buffer->i_length;
-            vlc_mutex_unlock( &p_sys->lock );
-            block_Release( p_buffer );
-        case SL_RESULT_SUCCESS:
-            return;
-        }
+        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 */
     }
+
+    return next != NULL;
+}
+
+/*****************************************************************************
+ * Play: play a sound
+ *****************************************************************************/
+static void Play( audio_output_t *p_aout, block_t *p_buffer )
+{
+    aout_sys_t *p_sys = p_aout->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 )
@@ -235,9 +276,8 @@ static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext
 /*****************************************************************************
  * 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;
     SLEngineItf    engineEngine;
 
@@ -315,13 +355,13 @@ static int Open( vlc_object_t *p_this )
     // 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;
@@ -366,15 +406,19 @@ static int Open( vlc_object_t *p_this )
     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                     = Pause;
-    p_aout->pf_flush                     = Flush;
+    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;
 
-    aout_FormatPrepare( &p_aout->format );
+    aout_FormatPrepare( fmt );
+
+    p_sys->format = *fmt;
 
     return VLC_SUCCESS;
 error:
@@ -385,15 +429,26 @@ error:
 /*****************************************************************************
  * 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;
 
     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
     //Flush remaining buffers if any.
     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 int Open (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+
+    /* FIXME: set volume/mute here */
+    aout->start = Start;
+    aout->stop = Stop;
+    aout->time_get = TimeGet;
+    return VLC_SUCCESS;
+}