[Android] Add an option to select the audio output

Sébastien Toque git at videolan.org
Thu May 10 00:29:30 CEST 2012


android | branch: master | Sébastien Toque <xilasz at gmail.com> | Thu May 10 00:28:55 2012 +0200| [622aeb5966b74f1145282d7540e8a2e2594201f4] | committer: Sébastien Toque

Add an option to select the audio output
choices are :
- AudioTrack (natif) <= default
- AudioTrack (java)
- OpenSL ES          <= only available in 2.3+

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=622aeb5966b74f1145282d7540e8a2e2594201f4
---

 vlc-android/jni/libvlcjni.c                        |   17 +++++++++-
 vlc-android/res/values-fr/strings.xml              |    5 +++
 vlc-android/res/values/strings.xml                 |   13 ++++++++
 vlc-android/res/xml/preferences.xml                |    5 +++
 vlc-android/src/org/videolan/vlc/LibVLC.java       |   33 +++++++++++++++++++-
 vlc-android/src/org/videolan/vlc/Util.java         |   15 +++++++++
 .../org/videolan/vlc/gui/PreferencesActivity.java  |   16 +++++++++
 7 files changed, 101 insertions(+), 3 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index b6c0d18..e5b54a2 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -36,6 +36,10 @@
 #define LOG_TAG "VLC/JNI/main"
 #include "log.h"
 
+#define AOUT_AUDIOTRACK      0
+#define AOUT_AUDIOTRACK_JAVA 1
+#define AOUT_OPENSLES        2
+
 libvlc_media_t *new_media(jint instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx)
 {
     libvlc_instance_t *libvlc = (libvlc_instance_t*)instance;
@@ -220,8 +224,13 @@ void Java_org_videolan_vlc_LibVLC_detachSurface(JNIEnv *env, jobject thiz) {
 
 void Java_org_videolan_vlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
 {
+    //only use OpenSLES if java side says we can
+    jclass cls = (*env)->GetObjectClass(env, thiz);
+    jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
+    bool use_opensles = (*env)->CallIntMethod(env, thiz, methodId) == AOUT_OPENSLES;
+
     /* Don't add any invalid options, otherwise it causes LibVLC to crash */
-    static const char *argv[] = {
+    const char *argv[] = {
         "-I", "dummy",
         "-vv",
         "--no-osd",
@@ -232,6 +241,7 @@ void Java_org_videolan_vlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
         "--control", "logger",
         "--logmode", "android",
         "--ffmpeg-fast",
+        use_opensles ? "--aout=opensles" : "--aout=android_audiotrack"
     };
     libvlc_instance_t *instance = libvlc_new(sizeof(argv) / sizeof(*argv), argv);
 
@@ -444,7 +454,10 @@ void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
 
     libvlc_media_player_set_media(mp, m);
 
-    if ( currentSdk( env, thiz )  < 9 ) //On newer version, we can use SLES
+    //if AOUT_AUDIOTRACK_JAVA, we use amem
+    jclass cls = (*env)->GetObjectClass(env, thiz);
+    jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
+    if ( (*env)->CallIntMethod(env, thiz, methodId) == AOUT_AUDIOTRACK_JAVA )
     {
         libvlc_audio_set_callbacks(mp, aout_play, NULL, NULL, NULL, NULL,
                                    (void*) myJavaLibVLC);
diff --git a/vlc-android/res/values-fr/strings.xml b/vlc-android/res/values-fr/strings.xml
index 4395c7f..23808f0 100644
--- a/vlc-android/res/values-fr/strings.xml
+++ b/vlc-android/res/values-fr/strings.xml
@@ -86,4 +86,9 @@
     <string name="track_samplerate_info">Fréquence d\'échantillonage: %1$d Hz\n</string>
     <string name="track_resolution_info">Résolution: %1$dx%2$d\n</string>
     <string name="track_framerate_info">Débit d\'images: %1$.3f\n</string>
+
+    <string name="aout">Sortie audio</string>
+    <string name="aout_audiotrack">AudioTrack (natif)</string>
+    <string name="aout_audiotrack_java">AudioTrack (java)</string>
+    <string name="aout_opensles">OpenSL ES</string>
 </resources>
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 0ce6559..5643240 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -93,4 +93,17 @@
     <string name="open_mrl_dialog_title">Open network stream</string>
     <string name="open_mrl_dialog_msg">Enter network MRL: e.g. http://, mms:// or rtsp://</string>
 
+    <string name="aout">Audio output</string>
+    <string name="aout_audiotrack">AudioTrack (native)</string>
+    <string name="aout_audiotrack_java">AudioTrack (java)</string>
+    <string name="aout_opensles">OpenSL ES</string>
+    <string-array name="aouts_froyo">
+        <item>@string/aout_audiotrack</item>
+        <item>@string/aout_audiotrack_java</item>
+    </string-array>
+    <string-array name="aouts">
+        <item>@string/aout_audiotrack</item>
+        <item>@string/aout_audiotrack_java</item>
+        <item>@string/aout_opensles</item>
+    </string-array>
 </resources>
diff --git a/vlc-android/res/xml/preferences.xml b/vlc-android/res/xml/preferences.xml
index d196de6..a250a33 100644
--- a/vlc-android/res/xml/preferences.xml
+++ b/vlc-android/res/xml/preferences.xml
@@ -37,6 +37,11 @@
         </PreferenceScreen>
     </PreferenceCategory>
     <PreferenceCategory android:title="@string/advanced_prefs_category">
+        <ListPreference
+            android:key="aout"
+            android:defaultValue="@string/aout_audiotrack"
+            android:title="@string/aout" >
+        </ListPreference>
         <PreferenceScreen android:title="@string/advanced_debugging">
             <Preference android:title="@string/quit" android:key="quit_app" android:enabled="true"/>
             <Preference android:title="@string/clear_media_db" android:key="clear_media_db" android:enabled="true" />
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index 6eaf8ba..f062fb1 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -27,13 +27,19 @@ import android.util.Log;
 import android.view.Surface;
 import android.preference.PreferenceManager;
 import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.res.Resources;
 import android.os.Build;
 
 public class LibVLC {
     private static final String TAG = "VLC/LibVLC";
+    private static final int AOUT_AUDIOTRACK = 0;
+    private static final int AOUT_AUDIOTRACK_JAVA = 1;
+    private static final int AOUT_OPENSLES = 2;
 
     private static LibVLC sInstance;
     private static boolean sUseIomx = false;
+    private static int sAout = AOUT_AUDIOTRACK;
 
     /** libVLC instance C pointer */
     private int mLibVlcInstance = 0; // Read-only, reserved for JNI
@@ -132,8 +138,33 @@ public class LibVLC {
         sUseIomx = enable;
     }
 
+    public int getAout() {
+        return sAout;
+    }
+
     public static synchronized void useIOMX(Context context) {
-        sUseIomx = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("enable_iomx", false);
+        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
+        sUseIomx = pref.getBoolean("enable_iomx", false);
+        setAout(context, pref.getString("aout", "error"), false);
+    }
+
+    public static synchronized void setAout(Context context, String aoutPref, boolean reset) {
+        Resources res = context.getResources();
+        if (aoutPref.equals(res.getString(R.string.aout_audiotrack_java)))
+            sAout = AOUT_AUDIOTRACK_JAVA;
+        else if (aoutPref.equals(res.getString(R.string.aout_opensles)) && Util.isGingerbread())
+            sAout = AOUT_OPENSLES;
+        else
+            sAout = AOUT_AUDIOTRACK;
+
+        if (reset && sInstance != null) {
+            try {
+                sInstance.destroy();
+                sInstance.init();
+            } catch (LibVlcException lve) {
+                Log.e(TAG, "Unable to reinit libvlc: " + lve);
+            }
+        }
     }
 
     /**
diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index 4be2c4d..68db351 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -166,4 +166,19 @@ public class Util {
                 ? R.drawable.background_item1
                 : R.drawable.background_item2);
     }
+
+    public static boolean isGingerbread()
+    {
+        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;
+    }
+
+    public static boolean isHoneycomb()
+    {
+        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB;
+    }
+
+    public static boolean isICS()
+    {
+        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
index 8064161..0282ff1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
@@ -24,13 +24,16 @@ import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.DatabaseManager;
 import org.videolan.vlc.LibVLC;
 import org.videolan.vlc.R;
+import org.videolan.vlc.Util;
 
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
+import android.preference.ListPreference;
 import android.preference.Preference;
+import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceActivity;
 import android.widget.Toast;
@@ -105,6 +108,19 @@ public class PreferencesActivity extends PreferenceActivity {
                     }
                 });
 
+        // Audio output
+        ListPreference aoutPref = (ListPreference) findPreference("aout");
+        int aoutEntriesId = Util.isGingerbread() ? R.array.aouts : R.array.aouts_froyo;
+        aoutPref.setEntries(aoutEntriesId);
+        aoutPref.setEntryValues(aoutEntriesId);
+        aoutPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+            @Override
+            public boolean onPreferenceChange(Preference preference, Object newValue) {
+                LibVLC.setAout(PreferencesActivity.this, (String) newValue, true);
+                return true;
+            }
+        });
+
         // Attach debugging items
         Preference quitAppPref = (Preference) findPreference("quit_app");
         quitAppPref.setOnPreferenceClickListener(



More information about the Android mailing list