Merge "Make sure that the event callback thread is Java capable in the simulator."
diff --git a/media/libstagefright/Android.mk b/media/libstagefright/Android.mk
index 071bb9e..2a65766 100644
--- a/media/libstagefright/Android.mk
+++ b/media/libstagefright/Android.mk
@@ -85,6 +85,8 @@
 
 ifeq ($(TARGET_OS)-$(TARGET_SIMULATOR),linux-true)
         LOCAL_LDLIBS += -lpthread -ldl
+        LOCAL_SHARED_LIBRARIES += libdvm
+        LOCAL_CPPFLAGS += -DANDROID_SIMULATOR
 endif
 
 ifneq ($(TARGET_SIMULATOR),true)
diff --git a/media/libstagefright/TimedEventQueue.cpp b/media/libstagefright/TimedEventQueue.cpp
index d079e70..6307bc5 100644
--- a/media/libstagefright/TimedEventQueue.cpp
+++ b/media/libstagefright/TimedEventQueue.cpp
@@ -28,6 +28,10 @@
 
 #include <media/stagefright/MediaDebug.h>
 
+#ifdef ANDROID_SIMULATOR
+#include <jni.h>
+#endif
+
 namespace android {
 
 TimedEventQueue::TimedEventQueue()
@@ -183,8 +187,26 @@
 
 // static
 void *TimedEventQueue::ThreadWrapper(void *me) {
+
+#ifdef ANDROID_SIMULATOR
+    // The simulator runs everything as one process, so any
+    // Binder calls happen on this thread instead of a thread
+    // in another process. We therefore need to make sure that
+    // this thread can do calls into interpreted code.
+    // On the device this is not an issue because the remote
+    // thread will already be set up correctly for this.
+    JavaVM *vm;
+    int numvms;
+    JNI_GetCreatedJavaVMs(&vm, 1, &numvms);
+    JNIEnv *env;
+    vm->AttachCurrentThread(&env, NULL);
+#endif
+
     static_cast<TimedEventQueue *>(me)->threadEntry();
 
+#ifdef ANDROID_SIMULATOR
+    vm->DetachCurrentThread();
+#endif
     return NULL;
 }