[Android] LibVLC: MediaPlayer: fix plug events thread safety

Thomas Guillem git at videolan.org
Fri Mar 30 16:51:24 CEST 2018


vlc-android | branch: 3.0.x | Thomas Guillem <thomas at gllm.fr> | Mon Mar 26 14:58:50 2018 +0200| [f5c48430ee67a654f884e5abdd9e9b972a66103f] | committer: Geoffrey Métais

LibVLC: MediaPlayer: fix plug events thread safety

Unregister it before selecting a new output/device.
Register it after selecting a new output/device.

(cherry picked from commit 47b50ec352496c7626346f34b07bcbc08394648b)

> https://code.videolan.org/videolan/vlc-android/commit/f5c48430ee67a654f884e5abdd9e9b972a66103f
---

 libvlc/src/org/videolan/libvlc/MediaPlayer.java | 55 +++++++++++++++++--------
 1 file changed, 37 insertions(+), 18 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/MediaPlayer.java b/libvlc/src/org/videolan/libvlc/MediaPlayer.java
index a3459e104..222e9e052 100644
--- a/libvlc/src/org/videolan/libvlc/MediaPlayer.java
+++ b/libvlc/src/org/videolan/libvlc/MediaPlayer.java
@@ -690,6 +690,10 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
         nativeSetAspectRatio(aspect);
     }
 
+    private boolean isAudioTrack() {
+        return mAudioOutput != null && mAudioOutput.equals("android_audiotrack");
+    }
+
     /**
      * Update the video viewpoint information
      *
@@ -717,31 +721,46 @@ public class MediaPlayer extends VLCObject<MediaPlayer.Event> {
      *
      * @return true on success.
      */
-    public boolean setAudioOutput(String aout) {
+    public synchronized boolean setAudioOutput(String aout) {
+        mAudioOutput = aout;
+        /* If The user forced an output different than AudioTrack, don't listen to audio
+         * plug events and let the user decide */
+        mListenAudioPlug = isAudioTrack();
+        if (!mListenAudioPlug)
+            registerAudioPlug(false);
+
         final boolean ret = nativeSetAudioOutput(aout);
-        if (ret) {
-            synchronized (this) {
-                mAudioOutput = aout;
-                /* The user forced an output, don't listen to audio plug events and let the user decide */
-                mListenAudioPlug = false;
-                registerAudioPlug(false);
-            }
+
+        if (!ret) {
+            mAudioOutput = null;
+            mListenAudioPlug = false;
         }
+
+        if (mListenAudioPlug)
+            registerAudioPlug(true);
+
         return ret;
     }
 
-    private boolean setAudioOutputDeviceInternal(String id, boolean fromUser) {
+    private synchronized boolean setAudioOutputDeviceInternal(String id, boolean fromUser) {
+        mAudioOutputDevice = id;
+        if (fromUser) {
+            /* The user forced a device, don't listen to audio plug events and let the user decide */
+            mListenAudioPlug = mAudioOutputDevice == null && isAudioTrack();
+            if (!mListenAudioPlug)
+                registerAudioPlug(false);
+        }
+
         final boolean ret = nativeSetAudioOutputDevice(id);
-        if (ret) {
-            synchronized (this) {
-                mAudioOutputDevice = id;
-                if (fromUser) {
-                    /* The user forced a device, don't listen to audio plug events and let the user decide */
-                    mListenAudioPlug = false;
-                    registerAudioPlug(false);
-                }
-            }
+
+        if (!ret) {
+            mAudioOutputDevice = null;
+            mListenAudioPlug = false;
         }
+
+        if (mListenAudioPlug)
+            registerAudioPlug(true);
+
         return ret;
     }
 



More information about the Android mailing list