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