Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / android-tutorial-4 / jni / tutorial-4.c
1 #include <string.h>
2 #include <stdint.h>
3 #include <jni.h>
4 #include <android/log.h>
5 #include <android/native_window.h>
6 #include <android/native_window_jni.h>
7 #include <gst/gst.h>
8 #include <gst/video/video.h>
9 #include <pthread.h>
10
11 GST_DEBUG_CATEGORY_STATIC (debug_category);
12 #define GST_CAT_DEFAULT debug_category
13
14 /*
15  * These macros provide a way to store the native pointer to CustomData, which might be 32 or 64 bits, into
16  * a jlong, which is always 64 bits, without warnings.
17  */
18 #if GLIB_SIZEOF_VOID_P == 8
19 # define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(*env)->GetLongField (env, thiz, fieldID)
20 # define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)data)
21 #else
22 # define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(jint)(*env)->GetLongField (env, thiz, fieldID)
23 # define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(jint)data)
24 #endif
25
26 /* Do not allow seeks to be performed closer than this distance. It is visually useless, and will probably
27  * confuse some demuxers. */
28 #define SEEK_MIN_DELAY (500 * GST_MSECOND)
29
30 /* Structure to contain all our information, so we can pass it to callbacks */
31 typedef struct _CustomData {
32   jobject app;                  /* Application instance, used to call its methods. A global reference is kept. */
33   GstElement *pipeline;         /* The running pipeline */
34   GMainContext *context;        /* GLib context used to run the main loop */
35   GMainLoop *main_loop;         /* GLib main loop */
36   gboolean initialized;         /* To avoid informing the UI multiple times about the initialization */
37   ANativeWindow *native_window; /* The Android native window where video will be rendered */
38   GstState state;               /* Current pipeline state */
39   GstState target_state;        /* Desired pipeline state, to be set once buffering is complete */
40   gint64 duration;              /* Cached clip duration */
41   gint64 desired_position;      /* Position to seek to, once the pipeline is running */
42   GstClockTime last_seek_time;  /* For seeking overflow prevention (throttling) */
43   gboolean is_live;             /* Live streams do not use buffering */
44 } CustomData;
45
46 /* playbin flags */
47 typedef enum {
48   GST_PLAY_FLAG_TEXT = (1 << 2)  /* We want subtitle output */
49 } GstPlayFlags;
50
51 /* These global variables cache values which are not changing during execution */
52 static pthread_t gst_app_thread;
53 static pthread_key_t current_jni_env;
54 static JavaVM *java_vm;
55 static jfieldID custom_data_field_id;
56 static jmethodID set_message_method_id;
57 static jmethodID set_current_position_method_id;
58 static jmethodID on_gstreamer_initialized_method_id;
59 static jmethodID on_media_size_changed_method_id;
60
61 /*
62  * Private methods
63  */
64
65 /* Register this thread with the VM */
66 static JNIEnv *attach_current_thread (void) {
67   JNIEnv *env;
68   JavaVMAttachArgs args;
69
70   GST_DEBUG ("Attaching thread %p", g_thread_self ());
71   args.version = JNI_VERSION_1_4;
72   args.name = NULL;
73   args.group = NULL;
74
75   if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) {
76     GST_ERROR ("Failed to attach current thread");
77     return NULL;
78   }
79
80   return env;
81 }
82
83 /* Unregister this thread from the VM */
84 static void detach_current_thread (void *env) {
85   GST_DEBUG ("Detaching thread %p", g_thread_self ());
86   (*java_vm)->DetachCurrentThread (java_vm);
87 }
88
89 /* Retrieve the JNI environment for this thread */
90 static JNIEnv *get_jni_env (void) {
91   JNIEnv *env;
92
93   if ((env = pthread_getspecific (current_jni_env)) == NULL) {
94     env = attach_current_thread ();
95     pthread_setspecific (current_jni_env, env);
96   }
97
98   return env;
99 }
100
101 /* Change the content of the UI's TextView */
102 static void set_ui_message (const gchar *message, CustomData *data) {
103   JNIEnv *env = get_jni_env ();
104   GST_DEBUG ("Setting message to: %s", message);
105   jstring jmessage = (*env)->NewStringUTF(env, message);
106   (*env)->CallVoidMethod (env, data->app, set_message_method_id, jmessage);
107   if ((*env)->ExceptionCheck (env)) {
108     GST_ERROR ("Failed to call Java method");
109     (*env)->ExceptionClear (env);
110   }
111   (*env)->DeleteLocalRef (env, jmessage);
112 }
113
114 /* Tell the application what is the current position and clip duration */
115 static void set_current_ui_position (gint position, gint duration, CustomData *data) {
116   JNIEnv *env = get_jni_env ();
117   (*env)->CallVoidMethod (env, data->app, set_current_position_method_id, position, duration);
118   if ((*env)->ExceptionCheck (env)) {
119     GST_ERROR ("Failed to call Java method");
120     (*env)->ExceptionClear (env);
121   }
122 }
123
124 /* If we have pipeline and it is running, query the current position and clip duration and inform
125  * the application */
126 static gboolean refresh_ui (CustomData *data) {
127   gint64 current = -1;
128   gint64 position;
129
130   /* We do not want to update anything unless we have a working pipeline in the PAUSED or PLAYING state */
131   if (!data || !data->pipeline || data->state < GST_STATE_PAUSED)
132     return TRUE;
133
134   /* If we didn't know it yet, query the stream duration */
135   if (!GST_CLOCK_TIME_IS_VALID (data->duration)) {
136     if (!gst_element_query_duration (data->pipeline, GST_FORMAT_TIME, &data->duration)) {
137       GST_WARNING ("Could not query current duration");
138     }
139   }
140
141   if (gst_element_query_position (data->pipeline, GST_FORMAT_TIME, &position)) {
142     /* Java expects these values in milliseconds, and GStreamer provides nanoseconds */
143     set_current_ui_position (position / GST_MSECOND, data->duration / GST_MSECOND, data);
144   }
145   return TRUE;
146 }
147
148 /* Forward declaration for the delayed seek callback */
149 static gboolean delayed_seek_cb (CustomData *data);
150
151 /* Perform seek, if we are not too close to the previous seek. Otherwise, schedule the seek for
152  * some time in the future. */
153 static void execute_seek (gint64 desired_position, CustomData *data) {
154   gint64 diff;
155
156   if (desired_position == GST_CLOCK_TIME_NONE)
157     return;
158
159   diff = gst_util_get_timestamp () - data->last_seek_time;
160
161   if (GST_CLOCK_TIME_IS_VALID (data->last_seek_time) && diff < SEEK_MIN_DELAY) {
162     /* The previous seek was too close, delay this one */
163     GSource *timeout_source;
164
165     if (data->desired_position == GST_CLOCK_TIME_NONE) {
166       /* There was no previous seek scheduled. Setup a timer for some time in the future */
167       timeout_source = g_timeout_source_new ((SEEK_MIN_DELAY - diff) / GST_MSECOND);
168       g_source_set_callback (timeout_source, (GSourceFunc)delayed_seek_cb, data, NULL);
169       g_source_attach (timeout_source, data->context);
170       g_source_unref (timeout_source);
171     }
172     /* Update the desired seek position. If multiple requests are received before it is time
173      * to perform a seek, only the last one is remembered. */
174     data->desired_position = desired_position;
175     GST_DEBUG ("Throttling seek to %" GST_TIME_FORMAT ", will be in %" GST_TIME_FORMAT,
176         GST_TIME_ARGS (desired_position), GST_TIME_ARGS (SEEK_MIN_DELAY - diff));
177   } else {
178     /* Perform the seek now */
179     GST_DEBUG ("Seeking to %" GST_TIME_FORMAT, GST_TIME_ARGS (desired_position));
180     data->last_seek_time = gst_util_get_timestamp ();
181     gst_element_seek_simple (data->pipeline, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, desired_position);
182     data->desired_position = GST_CLOCK_TIME_NONE;
183   }
184 }
185
186 /* Delayed seek callback. This gets called by the timer setup in the above function. */
187 static gboolean delayed_seek_cb (CustomData *data) {
188   GST_DEBUG ("Doing delayed seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (data->desired_position));
189   execute_seek (data->desired_position, data);
190   return FALSE;
191 }
192
193 /* Retrieve errors from the bus and show them on the UI */
194 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
195   GError *err;
196   gchar *debug_info;
197   gchar *message_string;
198
199   gst_message_parse_error (msg, &err, &debug_info);
200   message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message);
201   g_clear_error (&err);
202   g_free (debug_info);
203   set_ui_message (message_string, data);
204   g_free (message_string);
205   data->target_state = GST_STATE_NULL;
206   gst_element_set_state (data->pipeline, GST_STATE_NULL);
207 }
208
209 /* Called when the End Of the Stream is reached. Just move to the beginning of the media and pause. */
210 static void eos_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
211   data->target_state = GST_STATE_PAUSED;
212   data->is_live = (gst_element_set_state (data->pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL);
213   execute_seek (0, data);
214 }
215
216 /* Called when the duration of the media changes. Just mark it as unknown, so we re-query it in the next UI refresh. */
217 static void duration_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
218   data->duration = GST_CLOCK_TIME_NONE;
219 }
220
221 /* Called when buffering messages are received. We inform the UI about the current buffering level and
222  * keep the pipeline paused until 100% buffering is reached. At that point, set the desired state. */
223 static void buffering_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
224   gint percent;
225
226   if (data->is_live)
227     return;
228
229   gst_message_parse_buffering (msg, &percent);
230   if (percent < 100 && data->target_state >= GST_STATE_PAUSED) {
231     gchar * message_string = g_strdup_printf ("Buffering %d%%", percent);
232     gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
233     set_ui_message (message_string, data);
234     g_free (message_string);
235   } else if (data->target_state >= GST_STATE_PLAYING) {
236     gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
237   } else if (data->target_state >= GST_STATE_PAUSED) {
238     set_ui_message ("Buffering complete", data);
239   }
240 }
241
242 /* Called when the clock is lost */
243 static void clock_lost_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
244   if (data->target_state >= GST_STATE_PLAYING) {
245     gst_element_set_state (data->pipeline, GST_STATE_PAUSED);
246     gst_element_set_state (data->pipeline, GST_STATE_PLAYING);
247   }
248 }
249
250 /* Retrieve the video sink's Caps and tell the application about the media size */
251 static void check_media_size (CustomData *data) {
252   JNIEnv *env = get_jni_env ();
253   GstElement *video_sink;
254   GstPad *video_sink_pad;
255   GstCaps *caps;
256   GstVideoInfo info;
257
258   /* Retrieve the Caps at the entrance of the video sink */
259   g_object_get (data->pipeline, "video-sink", &video_sink, NULL);
260   video_sink_pad = gst_element_get_static_pad (video_sink, "sink");
261   caps = gst_pad_get_current_caps (video_sink_pad);
262
263   if (gst_video_info_from_caps (&info, caps)) {
264     info.width = info.width * info.par_n / info.par_d;
265     GST_DEBUG ("Media size is %dx%d, notifying application", info.width, info.height);
266
267     (*env)->CallVoidMethod (env, data->app, on_media_size_changed_method_id, (jint)info.width, (jint)info.height);
268     if ((*env)->ExceptionCheck (env)) {
269       GST_ERROR ("Failed to call Java method");
270       (*env)->ExceptionClear (env);
271     }
272   }
273
274   gst_caps_unref(caps);
275   gst_object_unref (video_sink_pad);
276   gst_object_unref(video_sink);
277 }
278
279 /* Notify UI about pipeline state changes */
280 static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
281   GstState old_state, new_state, pending_state;
282   gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
283   /* Only pay attention to messages coming from the pipeline, not its children */
284   if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) {
285     data->state = new_state;
286     gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state));
287     set_ui_message(message, data);
288     g_free (message);
289
290     /* The Ready to Paused state change is particularly interesting: */
291     if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) {
292       /* By now the sink already knows the media size */
293       check_media_size(data);
294
295       /* If there was a scheduled seek, perform it now that we have moved to the Paused state */
296       if (GST_CLOCK_TIME_IS_VALID (data->desired_position))
297         execute_seek (data->desired_position, data);
298     }
299   }
300 }
301
302 /* Check if all conditions are met to report GStreamer as initialized.
303  * These conditions will change depending on the application */
304 static void check_initialization_complete (CustomData *data) {
305   JNIEnv *env = get_jni_env ();
306   if (!data->initialized && data->native_window && data->main_loop) {
307     GST_DEBUG ("Initialization complete, notifying application. native_window:%p main_loop:%p", data->native_window, data->main_loop);
308
309     /* The main loop is running and we received a native window, inform the sink about it */
310     gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (data->pipeline), (guintptr)data->native_window);
311
312     (*env)->CallVoidMethod (env, data->app, on_gstreamer_initialized_method_id);
313     if ((*env)->ExceptionCheck (env)) {
314       GST_ERROR ("Failed to call Java method");
315       (*env)->ExceptionClear (env);
316     }
317     data->initialized = TRUE;
318   }
319 }
320
321 /* Main method for the native code. This is executed on its own thread. */
322 static void *app_function (void *userdata) {
323   JavaVMAttachArgs args;
324   GstBus *bus;
325   CustomData *data = (CustomData *)userdata;
326   GSource *timeout_source;
327   GSource *bus_source;
328   GError *error = NULL;
329   guint flags;
330
331   GST_DEBUG ("Creating pipeline in CustomData at %p", data);
332
333   /* Create our own GLib Main Context and make it the default one */
334   data->context = g_main_context_new ();
335   g_main_context_push_thread_default(data->context);
336
337   /* Build pipeline */
338   data->pipeline = gst_parse_launch("playbin", &error);
339   if (error) {
340     gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message);
341     g_clear_error (&error);
342     set_ui_message(message, data);
343     g_free (message);
344     return NULL;
345   }
346
347   /* Disable subtitles */
348   g_object_get (data->pipeline, "flags", &flags, NULL);
349   flags &= ~GST_PLAY_FLAG_TEXT;
350   g_object_set (data->pipeline, "flags", flags, NULL);
351
352   /* Set the pipeline to READY, so it can already accept a window handle, if we have one */
353   data->target_state = GST_STATE_READY;
354   gst_element_set_state(data->pipeline, GST_STATE_READY);
355
356   /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
357   bus = gst_element_get_bus (data->pipeline);
358   bus_source = gst_bus_create_watch (bus);
359   g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL);
360   g_source_attach (bus_source, data->context);
361   g_source_unref (bus_source);
362   g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, data);
363   g_signal_connect (G_OBJECT (bus), "message::eos", (GCallback)eos_cb, data);
364   g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, data);
365   g_signal_connect (G_OBJECT (bus), "message::duration", (GCallback)duration_cb, data);
366   g_signal_connect (G_OBJECT (bus), "message::buffering", (GCallback)buffering_cb, data);
367   g_signal_connect (G_OBJECT (bus), "message::clock-lost", (GCallback)clock_lost_cb, data);
368   gst_object_unref (bus);
369
370   /* Register a function that GLib will call 4 times per second */
371   timeout_source = g_timeout_source_new (250);
372   g_source_set_callback (timeout_source, (GSourceFunc)refresh_ui, data, NULL);
373   g_source_attach (timeout_source, data->context);
374   g_source_unref (timeout_source);
375
376   /* Create a GLib Main Loop and set it to run */
377   GST_DEBUG ("Entering main loop... (CustomData:%p)", data);
378   data->main_loop = g_main_loop_new (data->context, FALSE);
379   check_initialization_complete (data);
380   g_main_loop_run (data->main_loop);
381   GST_DEBUG ("Exited main loop");
382   g_main_loop_unref (data->main_loop);
383   data->main_loop = NULL;
384
385   /* Free resources */
386   g_main_context_pop_thread_default(data->context);
387   g_main_context_unref (data->context);
388   data->target_state = GST_STATE_NULL;
389   gst_element_set_state (data->pipeline, GST_STATE_NULL);
390   gst_object_unref (data->pipeline);
391
392   return NULL;
393 }
394
395 /*
396  * Java Bindings
397  */
398
399 /* Instruct the native code to create its internal data structure, pipeline and thread */
400 static void gst_native_init (JNIEnv* env, jobject thiz) {
401   CustomData *data = g_new0 (CustomData, 1);
402   data->desired_position = GST_CLOCK_TIME_NONE;
403   data->last_seek_time = GST_CLOCK_TIME_NONE;
404   SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data);
405   GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-4", 0, "Android tutorial 4");
406   gst_debug_set_threshold_for_name("tutorial-4", GST_LEVEL_DEBUG);
407   GST_DEBUG ("Created CustomData at %p", data);
408   data->app = (*env)->NewGlobalRef (env, thiz);
409   GST_DEBUG ("Created GlobalRef for app object at %p", data->app);
410   pthread_create (&gst_app_thread, NULL, &app_function, data);
411 }
412
413 /* Quit the main loop, remove the native thread and free resources */
414 static void gst_native_finalize (JNIEnv* env, jobject thiz) {
415   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
416   if (!data) return;
417   GST_DEBUG ("Quitting main loop...");
418   g_main_loop_quit (data->main_loop);
419   GST_DEBUG ("Waiting for thread to finish...");
420   pthread_join (gst_app_thread, NULL);
421   GST_DEBUG ("Deleting GlobalRef for app object at %p", data->app);
422   (*env)->DeleteGlobalRef (env, data->app);
423   GST_DEBUG ("Freeing CustomData at %p", data);
424   g_free (data);
425   SET_CUSTOM_DATA (env, thiz, custom_data_field_id, NULL);
426   GST_DEBUG ("Done finalizing");
427 }
428
429 /* Set playbin's URI */
430 void gst_native_set_uri (JNIEnv* env, jobject thiz, jstring uri) {
431   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
432   if (!data || !data->pipeline) return;
433   const jbyte *char_uri = (*env)->GetStringUTFChars (env, uri, NULL);
434   GST_DEBUG ("Setting URI to %s", char_uri);
435   if (data->target_state >= GST_STATE_READY)
436     gst_element_set_state (data->pipeline, GST_STATE_READY);
437   g_object_set(data->pipeline, "uri", char_uri, NULL);
438   (*env)->ReleaseStringUTFChars (env, uri, char_uri);
439   data->duration = GST_CLOCK_TIME_NONE;
440   data->is_live = (gst_element_set_state (data->pipeline, data->target_state) == GST_STATE_CHANGE_NO_PREROLL);
441 }
442
443 /* Set pipeline to PLAYING state */
444 static void gst_native_play (JNIEnv* env, jobject thiz) {
445   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
446   if (!data) return;
447   GST_DEBUG ("Setting state to PLAYING");
448   data->target_state = GST_STATE_PLAYING;
449   data->is_live = (gst_element_set_state (data->pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_NO_PREROLL);
450 }
451
452 /* Set pipeline to PAUSED state */
453 static void gst_native_pause (JNIEnv* env, jobject thiz) {
454   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
455   if (!data) return;
456   GST_DEBUG ("Setting state to PAUSED");
457   data->target_state = GST_STATE_PAUSED;
458   data->is_live = (gst_element_set_state (data->pipeline, GST_STATE_PAUSED) == GST_STATE_CHANGE_NO_PREROLL);
459 }
460
461 /* Instruct the pipeline to seek to a different position */
462 void gst_native_set_position (JNIEnv* env, jobject thiz, int milliseconds) {
463   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
464   if (!data) return;
465   gint64 desired_position = (gint64)(milliseconds * GST_MSECOND);
466   if (data->state >= GST_STATE_PAUSED) {
467     execute_seek(desired_position, data);
468   } else {
469     GST_DEBUG ("Scheduling seek to %" GST_TIME_FORMAT " for later", GST_TIME_ARGS (desired_position));
470     data->desired_position = desired_position;
471   }
472 }
473
474 /* Static class initializer: retrieve method and field IDs */
475 static jboolean gst_native_class_init (JNIEnv* env, jclass klass) {
476   custom_data_field_id = (*env)->GetFieldID (env, klass, "native_custom_data", "J");
477   set_message_method_id = (*env)->GetMethodID (env, klass, "setMessage", "(Ljava/lang/String;)V");
478   set_current_position_method_id = (*env)->GetMethodID (env, klass, "setCurrentPosition", "(II)V");
479   on_gstreamer_initialized_method_id = (*env)->GetMethodID (env, klass, "onGStreamerInitialized", "()V");
480   on_media_size_changed_method_id = (*env)->GetMethodID (env, klass, "onMediaSizeChanged", "(II)V");
481
482   if (!custom_data_field_id || !set_message_method_id || !on_gstreamer_initialized_method_id ||
483       !on_media_size_changed_method_id || !set_current_position_method_id) {
484     /* We emit this message through the Android log instead of the GStreamer log because the later
485      * has not been initialized yet.
486      */
487     __android_log_print (ANDROID_LOG_ERROR, "tutorial-4", "The calling class does not implement all necessary interface methods");
488     return JNI_FALSE;
489   }
490   return JNI_TRUE;
491 }
492
493 static void gst_native_surface_init (JNIEnv *env, jobject thiz, jobject surface) {
494   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
495   if (!data) return;
496   ANativeWindow *new_native_window = ANativeWindow_fromSurface(env, surface);
497   GST_DEBUG ("Received surface %p (native window %p)", surface, new_native_window);
498
499   if (data->native_window) {
500     ANativeWindow_release (data->native_window);
501     if (data->native_window == new_native_window) {
502       GST_DEBUG ("New native window is the same as the previous one %p", data->native_window);
503       if (data->pipeline) {
504         gst_video_overlay_expose(GST_VIDEO_OVERLAY (data->pipeline));
505         gst_video_overlay_expose(GST_VIDEO_OVERLAY (data->pipeline));
506       }
507       return;
508     } else {
509       GST_DEBUG ("Released previous native window %p", data->native_window);
510       data->initialized = FALSE;
511     }
512   }
513   data->native_window = new_native_window;
514
515   check_initialization_complete (data);
516 }
517
518 static void gst_native_surface_finalize (JNIEnv *env, jobject thiz) {
519   CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id);
520   if (!data) return;
521   GST_DEBUG ("Releasing Native Window %p", data->native_window);
522
523   if (data->pipeline) {
524     gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (data->pipeline), (guintptr)NULL);
525     gst_element_set_state (data->pipeline, GST_STATE_READY);
526   }
527
528   ANativeWindow_release (data->native_window);
529   data->native_window = NULL;
530   data->initialized = FALSE;
531 }
532
533 /* List of implemented native methods */
534 static JNINativeMethod native_methods[] = {
535   { "nativeInit", "()V", (void *) gst_native_init},
536   { "nativeFinalize", "()V", (void *) gst_native_finalize},
537   { "nativeSetUri", "(Ljava/lang/String;)V", (void *) gst_native_set_uri},
538   { "nativePlay", "()V", (void *) gst_native_play},
539   { "nativePause", "()V", (void *) gst_native_pause},
540   { "nativeSetPosition", "(I)V", (void*) gst_native_set_position},
541   { "nativeSurfaceInit", "(Ljava/lang/Object;)V", (void *) gst_native_surface_init},
542   { "nativeSurfaceFinalize", "()V", (void *) gst_native_surface_finalize},
543   { "nativeClassInit", "()Z", (void *) gst_native_class_init}
544 };
545
546 /* Library initializer */
547 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
548   JNIEnv *env = NULL;
549
550   java_vm = vm;
551
552   if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
553     __android_log_print (ANDROID_LOG_ERROR, "tutorial-4", "Could not retrieve JNIEnv");
554     return 0;
555   }
556   jclass klass = (*env)->FindClass (env, "com/gst_sdk_tutorials/tutorial_4/Tutorial4");
557   (*env)->RegisterNatives (env, klass, native_methods, G_N_ELEMENTS(native_methods));
558
559   pthread_key_create (&current_jni_env, detach_current_thread);
560
561   return JNI_VERSION_1_4;
562 }