[Android] jni/java_event_thread: set it synchronous
Thomas Guillem
git at videolan.org
Wed Feb 4 15:39:52 CET 2015
vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Tue Feb 3 13:17:49 2015 +0100| [7f40b27ab8120a444c9375a652fdffe2a472fa2f] | committer: Thomas Guillem
jni/java_event_thread: set it synchronous
Wait for the event to be handled by java side before returning.
It fixes event mismatch between java and jni, specially when inserting media at
a specified index or when removing them.
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=7f40b27ab8120a444c9375a652fdffe2a472fa2f
---
libvlc/jni/java_event_thread.c | 24 +++++++++++++++++++-----
libvlc/jni/java_event_thread.h | 4 +++-
libvlc/jni/libvlcjni-vlcobject.c | 4 ++--
3 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/libvlc/jni/java_event_thread.c b/libvlc/jni/java_event_thread.c
index 74ad650..d0868de 100644
--- a/libvlc/jni/java_event_thread.c
+++ b/libvlc/jni/java_event_thread.c
@@ -43,6 +43,7 @@ typedef TAILQ_HEAD(, event_queue_elm) EVENT_QUEUE;
struct java_event_thread {
bool b_run;
+ bool b_sync;
pthread_mutex_t lock;
pthread_cond_t cond;
pthread_t thread;
@@ -79,7 +80,6 @@ JavaEventThread_thread(void *data)
continue;
p_jevent = &event_elm->event;
- TAILQ_REMOVE(&p_java_event_thread->queue, event_elm, next);
pthread_mutex_unlock(&p_java_event_thread->lock);
@@ -87,9 +87,11 @@ JavaEventThread_thread(void *data)
fields.VLCObject.dispatchEventFromNativeID,
p_jevent->type, p_jevent->arg1, p_jevent->arg2);
- free(event_elm);
-
pthread_mutex_lock(&p_java_event_thread->lock);
+
+ free(event_elm);
+ TAILQ_REMOVE(&p_java_event_thread->queue, event_elm, next);
+ pthread_cond_signal(&p_java_event_thread->cond);
}
end:
p_java_event_thread->b_run = false;
@@ -110,7 +112,7 @@ end:
}
java_event_thread *
-JavaEventThread_create(jweak jobj)
+JavaEventThread_create(jweak jobj, bool b_sync)
{
java_event_thread *p_java_event_thread = calloc(1, sizeof(java_event_thread));
if (!p_java_event_thread)
@@ -122,6 +124,7 @@ JavaEventThread_create(jweak jobj)
p_java_event_thread->jobj = jobj;
p_java_event_thread->b_run = true;
+ p_java_event_thread->b_sync = b_sync;
pthread_create(&p_java_event_thread->thread, NULL,
JavaEventThread_thread, p_java_event_thread);
@@ -161,8 +164,19 @@ JavaEventThread_add(java_event_thread *p_java_event_thread,
goto error;
event_elm->event = *p_java_event;
- TAILQ_INSERT_TAIL(&p_java_event_thread->queue, event_elm, next);
+ if (p_java_event_thread->b_sync)
+ TAILQ_INSERT_HEAD(&p_java_event_thread->queue, event_elm, next);
+ else
+ TAILQ_INSERT_TAIL(&p_java_event_thread->queue, event_elm, next);
pthread_cond_signal(&p_java_event_thread->cond);
+
+ if (p_java_event_thread->b_sync) {
+ while (p_java_event_thread->b_run &&
+ (event_elm == TAILQ_FIRST(&p_java_event_thread->queue)))
+ pthread_cond_wait(&p_java_event_thread->cond,
+ &p_java_event_thread->lock);
+ }
+
pthread_mutex_unlock(&p_java_event_thread->lock);
return 0;
error:
diff --git a/libvlc/jni/java_event_thread.h b/libvlc/jni/java_event_thread.h
index bec5b9f..6562ac7 100644
--- a/libvlc/jni/java_event_thread.h
+++ b/libvlc/jni/java_event_thread.h
@@ -35,7 +35,9 @@ struct java_event
long arg2;
};
-java_event_thread *JavaEventThread_create(jweak jobj);
+/* if b_sync is true, calls to JavaEventThread_add will return only when events
+ * are handled by Java Side */
+java_event_thread *JavaEventThread_create(jweak jobj, bool b_sync);
void JavaEventThread_destroy(java_event_thread *p_java_event_thread);
int JavaEventThread_add(java_event_thread *p_java_event_thread,
java_event *p_java_event);
diff --git a/libvlc/jni/libvlcjni-vlcobject.c b/libvlc/jni/libvlcjni-vlcobject.c
index 2b1da7c..d67a94c 100644
--- a/libvlc/jni/libvlcjni-vlcobject.c
+++ b/libvlc/jni/libvlcjni-vlcobject.c
@@ -118,7 +118,7 @@ VLCJniObject_eventCallback(const libvlc_event_t *ev, void *data)
{
vlcjni_object *p_obj = data;
java_event jevent;
-
+
jevent.type = -1;
jevent.arg1 = jevent.arg2 = 0;
@@ -128,7 +128,7 @@ VLCJniObject_eventCallback(const libvlc_event_t *ev, void *data)
if (!p_obj->p_owner->p_java_event_thread)
{
p_obj->p_owner->p_java_event_thread =
- JavaEventThread_create(p_obj->p_owner->thiz);
+ JavaEventThread_create(p_obj->p_owner->thiz, true);
if (!p_obj->p_owner->p_java_event_thread)
return;
}
More information about the Android
mailing list