[Android] MediaList: fix dubious usage of ItemAdded event

Thomas Guillem git at videolan.org
Tue Nov 14 10:03:49 CET 2017


vlc-android | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Nov 14 10:01:35 2017 +0100| [e27b1274612a150e4279358122e14dce3e9a2bfb] | committer: Thomas Guillem

MediaList: fix dubious usage of ItemAdded event

The media will now be always valid from this event.

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

 libvlc/src/org/videolan/libvlc/MediaList.java | 24 ++++++++++++++++--------
 libvlc/src/org/videolan/libvlc/VLCEvent.java  | 12 ++++++++----
 libvlc/src/org/videolan/libvlc/VLCObject.java |  1 +
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/MediaList.java b/libvlc/src/org/videolan/libvlc/MediaList.java
index 003ca2652..c6a87573a 100644
--- a/libvlc/src/org/videolan/libvlc/MediaList.java
+++ b/libvlc/src/org/videolan/libvlc/MediaList.java
@@ -36,19 +36,27 @@ public class MediaList extends VLCObject<MediaList.Event> {
         public static final int EndReached             = 0x204;
 
         /**
-         * The media can be already released. If it's released, cached attributes are still
-         * available (like media.getMrl()).
-         * You should call {@link Media#retain()} and check the return value
-         * before calling media native methods.
+         * In case of ItemDeleted, the media will be already released. If it's released, cached
+         * attributes are still available (like {@link Media#getUri()}}).
          */
         public final Media media;
+        private final boolean retain;
         public final int index;
 
-        protected Event(int type, Media media, int index) {
+        protected Event(int type, Media media, boolean retain, int index) {
             super(type);
+            if (retain && (media == null || !media.retain()))
+                throw new IllegalStateException("invalid media reference");
             this.media = media;
+            this.retain = retain;
             this.index = index;
         }
+
+        @Override
+        void release() {
+            if (retain)
+                media.release();
+        }
     }
 
     public interface EventListener extends VLCEvent.Listener<MediaList.Event> {}
@@ -133,18 +141,18 @@ public class MediaList extends VLCObject<MediaList.Event> {
             index = (int) arg1;
             if (index != -1) {
                 final Media media = insertMediaFromEvent(index);
-                event = new Event(eventType, media, index);
+                event = new Event(eventType, media, true, index);
             }
             break;
         case Event.ItemDeleted:
             index = (int) arg1;
             if (index != -1) {
                 final Media media = removeMediaFromEvent(index);
-                event = new Event(eventType, media, index);
+                event = new Event(eventType, media, false, index);
             }
             break;
         case Event.EndReached:
-            event = new Event(eventType, null, -1);
+            event = new Event(eventType, null, false, -1);
             break;
         }
         mLocked = false;
diff --git a/libvlc/src/org/videolan/libvlc/VLCEvent.java b/libvlc/src/org/videolan/libvlc/VLCEvent.java
index acbad6abd..7fc0c78e9 100644
--- a/libvlc/src/org/videolan/libvlc/VLCEvent.java
+++ b/libvlc/src/org/videolan/libvlc/VLCEvent.java
@@ -26,29 +26,33 @@ abstract class VLCEvent {
     protected final long arg2;
     protected final float argf1;
 
-    protected VLCEvent(int type) {
+    VLCEvent(int type) {
         this.type = type;
         this.arg1 = this.arg2 = 0;
         this.argf1 = 0.0f;
     }
-    protected VLCEvent(int type, long arg1) {
+    VLCEvent(int type, long arg1) {
         this.type = type;
         this.arg1 = arg1;
         this.arg2 = 0;
         this.argf1 = 0.0f;
     }
-    protected VLCEvent(int type, long arg1, long arg2) {
+    VLCEvent(int type, long arg1, long arg2) {
         this.type = type;
         this.arg1 = arg1;
         this.arg2 = arg2;
         this.argf1 = 0.0f;
     }
-    protected VLCEvent(int type, float argf) {
+    VLCEvent(int type, float argf) {
         this.type = type;
         this.arg1 = this.arg2 = 0;
         this.argf1 = argf;
     }
 
+    void release() {
+        /* do nothing */
+    }
+
     /**
      * Listener for libvlc events
      *
diff --git a/libvlc/src/org/videolan/libvlc/VLCObject.java b/libvlc/src/org/videolan/libvlc/VLCObject.java
index 6afa99bd3..a22195127 100644
--- a/libvlc/src/org/videolan/libvlc/VLCObject.java
+++ b/libvlc/src/org/videolan/libvlc/VLCObject.java
@@ -153,6 +153,7 @@ abstract class VLCObject<T extends VLCEvent> {
             @Override
             public void run() {
                 listener.onEvent(event);
+                event.release();
             }
         }
 



More information about the Android mailing list