From ba4182c47049ce824b72322ed16350eac819767e Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Mon, 10 Sep 2012 16:10:48 +0200 Subject: [PATCH] Add method to test Java UI code being called from C callbacks --- .../android-tutorial-1/jni/tutorial-1.c | 20 ++++++++++++++++--- .../tutorial_1/Tutorial1.java | 15 ++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c index 91747fc9a4..0baf3a37a2 100755 --- a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c +++ b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c @@ -19,6 +19,7 @@ GST_DEBUG_CATEGORY_STATIC (debug_category); #endif typedef struct _CustomData { + jobject app; GstElement *pipeline; GMainLoop *main_loop; } CustomData; @@ -27,11 +28,12 @@ static pthread_t gst_app_thread; static pthread_key_t current_jni_env; static JavaVM *java_vm; static jfieldID custom_data_field_id; +static jmethodID set_message_method_id; /* * Private methods */ -static JNIEnv * gst_attach_current_thread (void) { +static JNIEnv *gst_attach_current_thread (void) { JNIEnv *env; JavaVMAttachArgs args; @@ -65,7 +67,13 @@ static JNIEnv *gst_get_jni_env (void) { } static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) { - GST_DEBUG ("Message: %s", GST_MESSAGE_TYPE_NAME (msg)); + GST_DEBUG ("Message: %s", GST_MESSAGE_TYPE_NAME (msg)); + JNIEnv *env = gst_get_jni_env (); + (*env)->CallVoidMethod (env, data->app, set_message_method_id, (*env)->NewStringUTF(env, GST_MESSAGE_TYPE_NAME (msg))); + if ((*env)->ExceptionCheck (env)) { + GST_ERROR ("Failed to call Java method"); + (*env)->ExceptionClear (env); + } } static void *gst_app_function (void *userdata) { @@ -76,7 +84,7 @@ static void *gst_app_function (void *userdata) { GST_DEBUG ("Creating pipeline in CustomData at %p", data); - data->pipeline = gst_parse_launch ("videotestsrc num-buffers=10000 ! fakesink", NULL); + data->pipeline = gst_parse_launch ("videotestsrc num-buffers=1000 ! fakesink", NULL); /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (data->pipeline); @@ -105,6 +113,8 @@ void gst_native_init (JNIEnv* env, jobject thiz) { SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data); GST_DEBUG ("Created CustomData at %p", data); pthread_create (&gst_app_thread, NULL, &gst_app_function, data); + data->app = (*env)->NewGlobalRef (env, thiz); + GST_DEBUG ("Created GlobalRef for app objet at %p", data->app); } void gst_native_finalize (JNIEnv* env, jobject thiz) { @@ -113,6 +123,8 @@ void gst_native_finalize (JNIEnv* env, jobject thiz) { g_main_loop_quit (data->main_loop); GST_DEBUG ("Waiting for thread to finish..."); pthread_join (gst_app_thread, NULL); + GST_DEBUG ("Deleting GlobalRef at %p", data->app); + (*env)->DeleteGlobalRef (env, data->app); GST_DEBUG ("Freeing CustomData at %p", data); g_free (data); GST_DEBUG ("Done finalizing"); @@ -133,6 +145,8 @@ void gst_native_pause (JNIEnv* env, jobject thiz) { void gst_class_init (JNIEnv* env, jclass klass) { custom_data_field_id = (*env)->GetFieldID (env, klass, "native_custom_data", "J"); GST_DEBUG ("The FieldID for the native_custom_data field is %p", custom_data_field_id); + set_message_method_id = (*env)->GetMethodID (env, klass, "setMessage", "(Ljava/lang/String;)V"); + GST_DEBUG ("The MethodID for the setMessage method is %p", set_message_method_id); } static JNINativeMethod native_methods[] = { diff --git a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java index 0e50affd5e..e32c1a165b 100755 --- a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java +++ b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java @@ -16,6 +16,7 @@ package com.gst_sdk_tutorials.tutorial_1; import android.app.Activity; +import android.util.Log; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; @@ -61,6 +62,20 @@ public class Tutorial1 extends Activity super.onDestroy(); } + private void setMessage (final String message) { + final TextView tv = (TextView)this.findViewById(R.id.textview_message); + Log.d ("GStreamer", "Received message " + message); + try { + runOnUiThread (new Runnable() {@Override public void run() + { + tv.setText (message); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + static { System.loadLibrary("gstreamer_android"); System.loadLibrary("tutorial-1");