]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/opensles_android.c
audiounit_ios: merge playback performance improvements from the Mac auhal module
[vlc] / modules / audio_output / opensles_android.c
index a9208b580181e34080698d084cbba549bb6cfa0f..eb18ebd2d72876a8aad5af0636e4754b5e1f85af 100644 (file)
 #include <SLES/OpenSLES_Android.h>
 
 #define OPENSLES_BUFFERS 255 /* maximum number of buffers */
-#define OPENSLES_BUFLEN  4   /* ms */
+#define OPENSLES_BUFLEN  10   /* ms */
 /*
- * 4ms of precision when mesasuring latency should be plenty enough,
- * with 255 buffers we can buffer ~1s of audio.
+ * 10ms of precision when mesasuring latency should be enough,
+ * with 255 buffers we can buffer 2.55s of audio.
  */
 
 #define CHECK_OPENSL_ERROR(msg)                \
@@ -193,6 +193,7 @@ static void Flush(audio_output_t *aout, bool drain)
         sys->p_buffer_chain = NULL;
         sys->pp_buffer_last = &sys->p_buffer_chain;
 
+        sys->samples = 0;
         sys->started = false;
 
         vlc_mutex_unlock(&sys->lock);
@@ -201,6 +202,9 @@ static void Flush(audio_output_t *aout, bool drain)
 
 static int VolumeSet(audio_output_t *aout, float vol)
 {
+    if (!aout->sys->volumeItf)
+        return -1;
+
     /* Convert UI volume to linear factor (cube) */
     vol = vol * vol * vol;
 
@@ -217,6 +221,9 @@ static int VolumeSet(audio_output_t *aout, float vol)
 
 static int MuteSet(audio_output_t *aout, bool mute)
 {
+    if (!aout->sys->volumeItf)
+        return -1;
+
     SLresult r = SetMute(aout->sys->volumeItf, mute);
     return (r == SL_RESULT_SUCCESS) ? 0 : -1;
 }
@@ -345,16 +352,6 @@ static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext
 /*****************************************************************************
  *
  *****************************************************************************/
-static void Clean(aout_sys_t *sys)
-{
-    if (sys->playerObject)
-        Destroy(sys->playerObject);
-    if (sys->outputMixObject)
-        Destroy(sys->outputMixObject);
-    if (sys->engineObject)
-        Destroy(sys->engineObject);
-}
-
 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
     SLresult       result;
@@ -391,6 +388,14 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     result = CreateAudioPlayer(sys->engineEngine, &sys->playerObject, &audioSrc,
                                     &audioSnk, sizeof(ids2) / sizeof(*ids2),
                                     ids2, req2);
+    if (unlikely(result != SL_RESULT_SUCCESS)) {
+        /* Try again with a more sensible samplerate */
+        fmt->i_rate = 44100;
+        format_pcm.samplesPerSec = ((SLuint32) 44100 * 1000) ;
+        result = CreateAudioPlayer(sys->engineEngine, &sys->playerObject, &audioSrc,
+                &audioSnk, sizeof(ids2) / sizeof(*ids2),
+                ids2, req2);
+    }
     CHECK_OPENSL_ERROR("Failed to create audio player");
 
     result = Realize(sys->playerObject, SL_BOOLEAN_FALSE);
@@ -426,6 +431,7 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 
     sys->p_buffer_chain = NULL;
     sys->pp_buffer_last = &sys->p_buffer_chain;
+    sys->samples = 0;
 
     // we want 16bit signed data native endian.
     fmt->i_format              = VLC_CODEC_S16N;
@@ -438,7 +444,11 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     return VLC_SUCCESS;
 
 error:
-    Clean(sys);
+    if (sys->playerObject) {
+        Destroy(sys->playerObject);
+        sys->playerObject = NULL;
+    }
+
     return VLC_EGENERIC;
 }
 
@@ -452,6 +462,9 @@ static void Stop(audio_output_t *aout)
 
     free(sys->buf);
     block_ChainRelease(sys->p_buffer_chain);
+
+    Destroy(sys->playerObject);
+    sys->playerObject = NULL;
 }
 
 /*****************************************************************************
@@ -462,7 +475,8 @@ static void Close(vlc_object_t *obj)
     audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
 
-    Clean(sys);
+    Destroy(sys->outputMixObject);
+    Destroy(sys->engineObject);
     dlclose(sys->p_so_handle);
     vlc_mutex_destroy(&sys->lock);
     free(sys);
@@ -545,7 +559,10 @@ static int Open (vlc_object_t *obj)
     return VLC_SUCCESS;
 
 error:
-    Clean(sys);
+    if (sys->outputMixObject)
+        Destroy(sys->outputMixObject);
+    if (sys->engineObject)
+        Destroy(sys->engineObject);
     if (sys->p_so_handle)
         dlclose(sys->p_so_handle);
     free(sys);