]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/opensles_android.c
macosx: removed wrong check, which could make the video output disappear when leaving...
[vlc] / modules / audio_output / opensles_android.c
index 2560360f55c0bb36f5b89fccce1a25788aec90f2..630981aa371bd9fc54c29c4c870734953c47fc6c 100644 (file)
@@ -67,17 +67,14 @@ struct aout_sys_t
     SLObjectItf                     playerObject;
 
     SLPlayItf                       playerPlay;
-    aout_buffer_t                  *p_buffer_array[BUFF_QUEUE];
-    int                             i_toclean_buffer;
-    int                             i_toappend_buffer;
+
+    vlc_mutex_t                     lock;
+    block_t                        *p_chain;
+    block_t                       **pp_last;
 
     void                           *p_so_handle;
 };
 
-typedef SLresult (*slCreateEngine_t)(
-        SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
-        const SLInterfaceID*, const SLboolean* );
-
 /*****************************************************************************
  * Local prototypes.
  *****************************************************************************/
@@ -100,28 +97,17 @@ vlc_module_begin ()
 vlc_module_end ()
 
 
-#define CHECK_OPENSL_ERROR( res, msg )              \
-    if( unlikely( res != SL_RESULT_SUCCESS ) )      \
-    {                                               \
-        msg_Err( p_aout, msg" (%lu)", res );        \
-        goto error;                                 \
-    }
-
-#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 void Clean( aout_sys_t *p_sys )
 {
-    Destroy( p_sys->playerObject );
-    Destroy( p_sys->outputMixObject );
-    Destroy( p_sys->engineObject );
+    if( p_sys->playerObject )
+        Destroy( p_sys->playerObject );
+    if( p_sys->outputMixObject )
+        Destroy( p_sys->outputMixObject );
+    if( p_sys->engineObject )
+        Destroy( p_sys->engineObject );
 
-    dlclose( p_sys->p_so_handle );
+    if( p_sys->p_so_handle )
+        dlclose( p_sys->p_so_handle );
 
     free( p_sys );
 }
@@ -133,6 +119,16 @@ static void Play( audio_output_t *p_aout, block_t *p_buffer )
 {
     aout_sys_t *p_sys = p_aout->sys;
     int tries = 5;
+    block_t **pp_last, *p_block;;
+
+    vlc_mutex_lock( &p_sys->lock );
+
+    /* If something bad happens, we must remove this buffer from the FIFO */
+    pp_last = p_sys->pp_last;
+    p_block = *pp_last;
+
+    block_ChainLastAppend( &p_sys->pp_last, p_buffer );
+    vlc_mutex_unlock( &p_sys->lock );
 
     for (;;)
     {
@@ -141,12 +137,6 @@ static void Play( audio_output_t *p_aout, block_t *p_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");
 
@@ -159,23 +149,41 @@ static void Play( audio_output_t *p_aout, block_t *p_buffer )
 
         default:
             msg_Warn( p_aout, "Error %lu, dropping buffer", result );
-            aout_BufferFree( p_buffer );
+            vlc_mutex_lock( &p_sys->lock );
+            p_sys->pp_last = pp_last;
+            *pp_last = p_block;
+            vlc_mutex_unlock( &p_sys->lock );
+            block_Release( p_buffer );
+        case SL_RESULT_SUCCESS:
             return;
         }
     }
 }
+
 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext )
 {
+    block_t *p_block;
     aout_sys_t *p_sys = pContext;
 
     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;
+    vlc_mutex_lock( &p_sys->lock );
+
+    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;
+
+    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 )
 {
@@ -198,6 +206,9 @@ 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;
@@ -205,6 +216,14 @@ static int Open( vlc_object_t *p_this )
     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( SL_IID_ANDROIDSIMPLEBUFFERQUEUE, p_sys->p_so_handle,
                  "SL_IID_ANDROIDSIMPLEBUFFERQUEUE" );
@@ -212,27 +231,35 @@ static int Open( vlc_object_t *p_this )
     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 = Realize( p_sys->engineObject, SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize engine" );
+    CHECK_OPENSL_ERROR( "Failed to realize engine" );
 
     // get the engine interface, needed to create other objects
     result = GetInterface( p_sys->engineObject, *SL_IID_ENGINE, &engineEngine );
-    CHECK_OPENSL_ERROR( result, "Failed to get the engine interface" );
+    CHECK_OPENSL_ERROR( "Failed to get the engine interface" );
 
     // create output mix, with environmental reverb specified as a non-required interface
     const SLInterfaceID ids1[] = { *SL_IID_VOLUME };
     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
     result = CreateOutputMix( engineEngine, &p_sys->outputMixObject, 1, ids1, req1 );
-    CHECK_OPENSL_ERROR( result, "Failed to create output mix" );
+    CHECK_OPENSL_ERROR( "Failed to create output mix" );
 
     // realize the output mix in synchronous mode
     result = Realize( p_sys->outputMixObject, SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize output mix" );
+    CHECK_OPENSL_ERROR( "Failed to realize output mix" );
 
 
     // configure audio source - this defines the number of samples you can enqueue.
@@ -265,26 +292,30 @@ static int Open( vlc_object_t *p_this )
     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" );
 
     result = Realize( p_sys->playerObject, SL_BOOLEAN_FALSE );
-    CHECK_OPENSL_ERROR( result, "Failed to realize player object." );
+    CHECK_OPENSL_ERROR( "Failed to realize player object." );
 
     result = GetInterface( p_sys->playerObject, *SL_IID_PLAY, &p_sys->playerPlay );
-    CHECK_OPENSL_ERROR( result, "Failed to get player interface." );
+    CHECK_OPENSL_ERROR( "Failed to get player interface." );
 
     result = GetInterface( p_sys->playerObject, *SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
                                                   &p_sys->playerBufferQueue );
-    CHECK_OPENSL_ERROR( result, "Failed to get buff queue interface" );
+    CHECK_OPENSL_ERROR( "Failed to get buff queue interface" );
 
     result = RegisterCallback( p_sys->playerBufferQueue, PlayedCallback,
                                    (void*)p_sys);
-    CHECK_OPENSL_ERROR( result, "Failed to register buff queue callback." );
+    CHECK_OPENSL_ERROR( "Failed to register buff queue callback." );
 
 
     // set the player's state to playing
     result = SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_PLAYING );
-    CHECK_OPENSL_ERROR( result, "Failed to switch to playing state" );
+    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;
 
     // we want 16bit signed data little endian.
     p_aout->format.i_format              = VLC_CODEC_S16L;
@@ -302,7 +333,7 @@ error:
 }
 
 /*****************************************************************************
- * Close: close our file
+ * Close
  *****************************************************************************/
 static void Close( vlc_object_t *p_this )
 {
@@ -312,5 +343,7 @@ static void Close( vlc_object_t *p_this )
     SetPlayState( p_sys->playerPlay, SL_PLAYSTATE_STOPPED );
     //Flush remaining buffers if any.
     Clear( p_sys->playerBufferQueue );
+    block_ChainRelease( p_sys->p_chain );
+    vlc_mutex_destroy( &p_sys->lock );
     Clean( p_sys );
 }