[vlc-devel] [PATCH 1/3] android/audio: export DynamicsProcessing helpers

Thomas Guillem thomas at gllm.fr
Thu Dec 3 18:16:02 CET 2020


---
 modules/audio_output/android/audiotrack.c | 89 ++++++++++++++++++-----
 modules/audio_output/android/device.h     | 14 ++++
 2 files changed, 83 insertions(+), 20 deletions(-)

diff --git a/modules/audio_output/android/audiotrack.c b/modules/audio_output/android/audiotrack.c
index 8e547834ca2..83fc17a64d9 100644
--- a/modules/audio_output/android/audiotrack.c
+++ b/modules/audio_output/android/audiotrack.c
@@ -999,16 +999,7 @@ AudioTrack_New( JNIEnv *env, aout_stream_t *stream, unsigned int i_rate,
         session_id = JNI_AT_CALL_INT( getAudioSessionId );
 
     if( session_id != 0 )
-    {
-        jobject dp = JNI_CALL( NewObject, jfields.DynamicsProcessing.clazz,
-                               jfields.DynamicsProcessing.ctor, session_id );
-
-        if( !CHECK_EXCEPTION( "DynamicsProcessing", "ctor" ) )
-        {
-            p_sys->p_dp = (*env)->NewGlobalRef( env, dp );
-            (*env)->DeleteLocalRef( env, dp );
-        }
-    }
+        p_sys->p_dp = DynamicsProcessing_New( stream, session_id );
 
     return 0;
 }
@@ -1574,8 +1565,8 @@ Stop( aout_stream_t *stream )
         (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
     }
 
-    if( p_sys->p_dp )
-        (*env)->DeleteGlobalRef( env, p_sys->p_dp );
+    if( p_sys->p_dp != NULL )
+        DynamicsProcessing_Delete( stream, p_sys->p_dp );
 
     /* Release the timestamp object */
     if( p_sys->timestamp.p_obj )
@@ -2160,18 +2151,13 @@ VolumeSet( aout_stream_t *stream, float volume )
             {
                 /* DynamicsProcessing is not needed anymore (using AudioTrack
                  * volume) */
-                JNI_CALL_INT( p_sys->p_dp, jfields.DynamicsProcessing.setEnabled, false );
-                CHECK_EXCEPTION( "DynamicsProcessing", "setEnabled" );
+                DynamicsProcessing_Disable( stream, p_sys->p_dp );
             }
             else
             {
-                /* convert linear gain to dB */
-                float dB = 20.0f * log10f(gain);
-
-                JNI_CALL_VOID( p_sys->p_dp, jfields.DynamicsProcessing.setInputGainAllChannelsTo, dB );
-                int ret = JNI_CALL_INT( p_sys->p_dp, jfields.DynamicsProcessing.setEnabled, true );
+                int ret = DynamicsProcessing_SetVolume( stream, p_sys->p_dp, gain );
 
-                if( !CHECK_EXCEPTION( "DynamicsProcessing", "setEnabled" ) && ret == 0 )
+                if( ret == VLC_SUCCESS )
                     gain = 1.0; /* reset sw gain */
                 else
                     msg_Warn( stream, "failed to set gain via DynamicsProcessing, fallback to sw gain");
@@ -2191,3 +2177,66 @@ MuteSet( aout_stream_t *stream, bool mute )
     if( !p_sys->b_error && p_sys->p_audiotrack != NULL && ( env = GET_ENV() ) )
         AudioTrack_SetVolume( env, stream, mute ? 0.0f : p_sys->volume );
 }
+
+jobject
+DynamicsProcessing_New( aout_stream_t *stream, int session_id )
+{
+    JNIEnv *env;
+    if( !( env = GET_ENV() ) )
+        return NULL;
+
+    jobject dp = JNI_CALL( NewObject, jfields.DynamicsProcessing.clazz,
+                           jfields.DynamicsProcessing.ctor, session_id );
+
+    if( CHECK_EXCEPTION( "DynamicsProcessing", "ctor" ) )
+        return NULL;
+
+    jobject global_dp = (*env)->NewGlobalRef( env, dp );
+    (*env)->DeleteLocalRef( env, dp );
+
+    return global_dp;
+}
+
+void
+DynamicsProcessing_Disable( aout_stream_t *stream, jobject dp )
+{
+    JNIEnv *env;
+    if( !( env = GET_ENV() ) )
+        return;
+
+    JNI_CALL_INT( dp, jfields.DynamicsProcessing.setEnabled, false );
+    CHECK_EXCEPTION( "DynamicsProcessing", "setEnabled" );
+}
+
+int
+DynamicsProcessing_SetVolume( aout_stream_t *stream, jobject dp, float volume )
+{
+    JNIEnv *env;
+    if( !( env = GET_ENV() ) )
+        return VLC_EGENERIC;
+
+    /* convert linear gain to dB */
+    float dB = volume == 0.0f ? -144 : 20.0f * log10f( volume );
+
+    JNI_CALL_VOID( dp, jfields.DynamicsProcessing.setInputGainAllChannelsTo, dB );
+    int ret = JNI_CALL_INT( dp, jfields.DynamicsProcessing.setEnabled, volume != 1.0f );
+
+    if( CHECK_EXCEPTION( "DynamicsProcessing", "setEnabled" ) || ret != 0 )
+        return VLC_EGENERIC;
+
+    return VLC_SUCCESS;
+}
+
+void
+DynamicsProcessing_Delete( aout_stream_t *stream, jobject dp )
+{
+    JNIEnv *env;
+    if( !( env = GET_ENV() ) )
+        return;
+
+    JNI_CALL_INT( dp, jfields.DynamicsProcessing.setEnabled, false );
+    CHECK_EXCEPTION( "DynamicsProcessing", "setEnabled" );
+
+    (*env)->DeleteGlobalRef( env, dp );
+}
+
diff --git a/modules/audio_output/android/device.h b/modules/audio_output/android/device.h
index cdd392f605d..abcff255f6e 100644
--- a/modules/audio_output/android/device.h
+++ b/modules/audio_output/android/device.h
@@ -20,6 +20,8 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
+#include <jni.h>
+
 typedef struct aout_stream aout_stream_t;
 
 enum android_audio_device_type
@@ -36,6 +38,18 @@ AudioTrack_InitJNI(audio_output_t *aout);
 bool
 AudioTrack_HasEncoding(long long encoding_flags, vlc_fourcc_t format);
 
+jobject
+DynamicsProcessing_New(aout_stream_t *stream, int32_t session_id);
+
+int
+DynamicsProcessing_SetVolume(aout_stream_t *stream, jobject dp, float volume);
+
+void
+DynamicsProcessing_Disable(aout_stream_t *stream, jobject dp);
+
+void
+DynamicsProcessing_Delete(aout_stream_t *stream, jobject dp);
+
 struct aout_stream
 {
     struct vlc_object_t obj;
-- 
2.28.0



More information about the vlc-devel mailing list