Split out documentation into subfolders.
[platform/upstream/gstreamer.git] / examples / tutorials / android-tutorial-2 / jni / tutorial-2.c
1 #include <string.h>
2 #include <jni.h>
3 #include <android/log.h>
4 #include <gst/gst.h>
5 #include <pthread.h>
6
7 GST_DEBUG_CATEGORY_STATIC (debug_category);
8 #define GST_CAT_DEFAULT debug_category
9
10 /*
11  * These macros provide a way to store the native pointer to CustomData, which might be 32 or 64 bits, into
12  * a jlong, which is always 64 bits, without warnings.
13  */
14 #if GLIB_SIZEOF_VOID_P == 8
15 # define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(*env)->GetLongField (env, thiz, fieldID)
16 # define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)data)
17 #else
18 # define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(jint)(*env)->GetLongField (env, thiz, fieldID)
19 # define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(jint)data)
20 #endif
21
22 /* Structure to contain all our information, so we can pass it to callbacks */
23 typedef struct _CustomData {
24   jobject app;           /* Application instance, used to call its methods. A global reference is kept. */
25   GstElement *pipeline;  /* The running pipeline */
26   GMainContext *context; /* GLib context used to run the main loop */
27   GMainLoop *main_loop;  /* GLib main loop */
28   gboolean initialized;  /* To avoid informing the UI multiple times about the initialization */
29 } CustomData;
30
31 /* These global variables cache values which are not changing during execution */
32 static pthread_t gst_app_thread;
33 static pthread_key_t current_jni_env;
34 static JavaVM *java_vm;
35 static jfieldID custom_data_field_id;
36 static jmethodID set_message_method_id;
37 static jmethodID on_gstreamer_initialized_method_id;
38
39 /*
40  * Private methods
41  */
42
43 /* Register this thread with the VM */
44 static JNIEnv *attach_current_thread (void) {
45   JNIEnv *env;
46   JavaVMAttachArgs args;
47
48   GST_DEBUG ("Attaching thread %p", g_thread_self ());
49   args.version = JNI_VERSION_1_4;
50   args.name = NULL;
51   args.group = NULL;
52
53   if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) {
54     GST_ERROR ("Failed to attach current thread");
55     return NULL;
56   }
57
58   return env;
59 }
60
61 /* Unregister this thread from the VM */
62 static void detach_current_thread (void *env) {
63   GST_DEBUG ("Detaching thread %p", g_thread_self ());
64   (*java_vm)->DetachCurrentThread (java_vm);
65 }
66
67 /* Retrieve the JNI environment for this thread */
68 static JNIEnv *get_jni_env (void) {
69   JNIEnv *env;
70
71   if ((env = pthread_getspecific (current_jni_env)) == NULL) {
72     env = attach_current_thread ();
73     pthread_setspecific (current_jni_env, env);
74   }
75
76   return env;
77 }
78
79 /* Change the content of the UI's TextView */
80 static void set_ui_message (const gchar *message, CustomData *data) {
81   JNIEnv *env = get_jni_env ();
82   GST_DEBUG ("Setting message to: %s", message);
83   jstring jmessage = (*env)->NewStringUTF(env, message);
84   (*env)->CallVoidMethod (env, data->app, set_message_method_id, jmessage);
85   if ((*env)->ExceptionCheck (env)) {
86     GST_ERROR ("Failed to call Java method");
87     (*env)->ExceptionClear (env);
88   }
89   (*env)->DeleteLocalRef (env, jmessage);
90 }
91
92 /* Retrieve errors from the bus and show them on the UI */
93 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
94   GError *err;
95   gchar *debug_info;
96   gchar *message_string;
97
98   gst_message_parse_error (msg, &err, &debug_info);
99   message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
100   g_clear_error (&err);
101   g_free (debug_info);
102   set_ui_message (message_string, data);
103   g_free (message_string);
104   gst_element_set_state (data->pipeline, GST_STATE_NULL);
105 }
106
107 /* Notify UI about pipeline state changes */
108 static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
109   GstState old_state, new_state, pending_state;
110   gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
111   /* Only pay attention to messages coming from the pipeline, not its children */
112   if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) {
113     gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state));
114     set_ui_message(message, data);
115     g_free (message);
116   }
117 }
118
119 /* Check if all conditions are met to report GStreamer as initialized.
120  * These conditions will change depending on the application */
121 static void check_initialization_complete (CustomData *data) {
122   JNIEnv *env = get_jni_env ();
123   if (!data->initialized && data->main_loop) {
124     GST_DEBUG ("Initialization complete, notifying application. main_loop:%p", data->main_loop);
125     (*env)->CallVoidMethod (env, data->app, on_gstreamer_initialized_method_id);
126     if ((*env)->ExceptionCheck (env)) {
127       GST_ERROR ("Failed to call Java method");
128       (*env)->ExceptionClear (env);
129     }
130     data->initialized = TRUE;
131   }
132 }
133
134 /* Main method for the native code. This is executed on its own thread. */
135 static void *app_function (void *userdata) {
136   JavaVMAttachArgs args;
137   GstBus *bus;
138   CustomData *data = (CustomData *)userdata;
139   GSource *bus_source;
140   GError *error = NULL;
141
142   GST_DEBUG ("Creating pipeline in CustomData at %p", data);
143
144   /* Create our own GLib Main Context and make it the default one */
145   data->context = g_main_context_new ();
146   g_main_context_push_thread_default(data->context);
147
148   /* Build pipeline */
149   data->pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error);
150   if (error) {
151     gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
152     g_clear_error (&error);
153     set_ui_message(message, data);
154     g_free (message);
155     return NULL;
156   }
157
158   /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
159   bus = gst_element_get_bus (data->pipeline);
160   bus_source = gst_bus_create_watch (bus);
161   g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL);
162   g_source_attach (bus_source, data->context);
163   g_source_unref (bus_source);
164   g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, data);
165   g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, data);
166   gst_object_unref (bus);
167
168   /* Create a GLib Main Loop and set it to run */
169   GST_DEBUG ("Entering main loop... (CustomData:%p)", data);
170   data->main_loop = g_main_loop_new (data->context, FALSE);
171   check_initialization_complete (data);
172   g_main_loop_run (data->main_loop);
173   GST_DEBUG ("Exited main loop");
174   g_main_loop_unref (data->main_loop);
175   data->main_loop = NULL;
176
177   /* Free resources */
178   g_main_context_pop_thread_default(data->context);
179   g_main_context_unref (data->context);
180   gst_element_set_state (data->pipeline, GST_STATE_NULL);
181   gst_object_unref (data->pipeline);
182
183   return NULL;
184 }
185
186 /*
187  * Java Bindings
188  */
189
190 /* Instruct the native code to create its internal data structure, pipeline and thread */
191 static void gst_native_init (JNIEnv* env, jobject thiz) {
192   CustomData *data = g_new0 (CustomData, 1);
193   SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data);
194   GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "Android tutorial 2");
195   gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG);
196   GST_DEBUG ("Created CustomData at %p", data);
197   data->app = (*env)->NewGlobalRef (env, thiz);
198   GST_DEBUG ("Created GlobalRef for app object at %p", data->app);
199   pthread_create (&gst_app_thread, NULL, &app_function, data);
200 }
201
202 /* Quit the main loop, remove the native thread and free resources */
203 static void gst_native_finalize (JNIEnv* env, jobject thiz) {
204   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
205   if (!data) return;
206   GST_DEBUG ("Quitting main loop...");
207   g_main_loop_quit (data->main_loop);
208   GST_DEBUG ("Waiting for thread to finish...");
209   pthread_join (gst_app_thread, NULL);
210   GST_DEBUG ("Deleting GlobalRef for app object at %p", data->app);
211   (*env)->DeleteGlobalRef (env, data->app);
212   GST_DEBUG ("Freeing CustomData at %p", data);
213   g_free (data);
214   SET_CUSTOM_DATA (env, thiz, custom_data_field_id, NULL);
215   GST_DEBUG ("Done finalizing");
216 }
217
218 /* Set pipeline to PLAYING state */
219 static void gst_native_play (JNIEnv* env, jobject thiz) {
220   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
221   if (!data) return;
222   GST_DEBUG ("Setting state to PLAYING");
223   gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
224 }
225
226 /* Set pipeline to PAUSED state */
227 static void gst_native_pause (JNIEnv* env, jobject thiz) {
228   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
229   if (!data) return;
230   GST_DEBUG ("Setting state to PAUSED");
231   gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
232 }
233
234 /* Static class initializer: retrieve method and field IDs */
235 static jboolean gst_native_class_init (JNIEnv* env, jclass klass) {
236   custom_data_field_id = (*env)->GetFieldID (env, klass, "native_custom_data", "J");
237   set_message_method_id = (*env)->GetMethodID (env, klass, "setMessage", "(Ljava/lang/String;)V");
238   on_gstreamer_initialized_method_id = (*env)->GetMethodID (env, klass, "onGStreamerInitialized", "()V");
239
240   if (!custom_data_field_id || !set_message_method_id || !on_gstreamer_initialized_method_id) {
241     /* We emit this message through the Android log instead of the GStreamer log because the later
242      * has not been initialized yet.
243      */
244     __android_log_print (ANDROID_LOG_ERROR, "tutorial-2", "The calling class does not implement all necessary interface methods");
245     return JNI_FALSE;
246   }
247   return JNI_TRUE;
248 }
249
250 /* List of implemented native methods */
251 static JNINativeMethod native_methods[] = {
252   { "nativeInit", "()V", (void *) gst_native_init},
253   { "nativeFinalize", "()V", (void *) gst_native_finalize},
254   { "nativePlay", "()V", (void *) gst_native_play},
255   { "nativePause", "()V", (void *) gst_native_pause},
256   { "nativeClassInit", "()Z", (void *) gst_native_class_init}
257 };
258
259 /* Library initializer */
260 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
261   JNIEnv *env = NULL;
262
263   java_vm = vm;
264
265   if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
266     __android_log_print (ANDROID_LOG_ERROR, "tutorial-2", "Could not retrieve JNIEnv");
267     return 0;
268   }
269   jclass klass = (*env)->FindClass (env, "org/freedesktop/gstreamer/tutorials/tutorial_2/Tutorial2");
270   (*env)->RegisterNatives (env, klass, native_methods, G_N_ELEMENTS(native_methods));
271
272   pthread_key_create (&current_jni_env, detach_current_thread);
273
274   return JNI_VERSION_1_4;
275 }