#endif
typedef struct _CustomData {
+ jobject app;
GstElement *pipeline;
GMainLoop *main_loop;
} CustomData;
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;
}
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) {
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);
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) {
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");
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[] = {
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;
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");