Add method to test Java UI code being called from C callbacks
authorXavi Artigas <xartigas@fluendo.com>
Mon, 10 Sep 2012 14:10:48 +0000 (16:10 +0200)
committerXavi Artigas <xartigas@fluendo.com>
Mon, 10 Sep 2012 14:10:48 +0000 (16:10 +0200)
gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c
gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java

index 91747fc..0baf3a3 100755 (executable)
@@ -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[] = {
index 0e50aff..e32c1a1 100755 (executable)
@@ -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");