Skip to content
Snippets Groups Projects
Commit 91085611 authored by Thomas Guillem's avatar Thomas Guillem Committed by Geoffrey Métais
Browse files

MediaList: fix dubious usage of ItemAdded event

The media will now be always valid from this event.

(cherry picked from commit e27b1274)
parent 56e16d58
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......@@ -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
*
......
......@@ -153,6 +153,7 @@ abstract class VLCObject<T extends VLCEvent> {
@Override
public void run() {
listener.onEvent(event);
event.release();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment