diff options
Diffstat (limited to 'tutorials/android-tutorial-2')
13 files changed, 488 insertions, 0 deletions
diff --git a/tutorials/android-tutorial-2/AndroidManifest.xml b/tutorials/android-tutorial-2/AndroidManifest.xml new file mode 100644 index 0000000..5f8dbcc --- /dev/null +++ b/tutorials/android-tutorial-2/AndroidManifest.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="org.freedesktop.gstreamer.tutorials.tutorial_2" + android:versionCode="1" + android:versionName="1.0"> + <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="14"/> + <application android:label="@string/app_name" + android:icon="@drawable/gstreamer_logo_2"> + <activity android:name=".Tutorial2" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/tutorials/android-tutorial-2/jni/Android.mk b/tutorials/android-tutorial-2/jni/Android.mk new file mode 100644 index 0000000..42ed3c6 --- /dev/null +++ b/tutorials/android-tutorial-2/jni/Android.mk @@ -0,0 +1,32 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := tutorial-2 +LOCAL_SRC_FILES := tutorial-2.c +LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -llog +include $(BUILD_SHARED_LIBRARY) + +ifndef GSTREAMER_ROOT_ANDROID +$(error GSTREAMER_ROOT_ANDROID is not defined!) +endif + +ifeq ($(TARGET_ARCH_ABI),armeabi) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm +else ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/armv7 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/arm64 +else ifeq ($(TARGET_ARCH_ABI),x86) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86 +else ifeq ($(TARGET_ARCH_ABI),x86_64) +GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)/x86_64 +else +$(error Target arch ABI not supported: $(TARGET_ARCH_ABI)) +endif + +GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build/ +include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk +GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_SYS) +include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk diff --git a/tutorials/android-tutorial-2/jni/Application.mk b/tutorials/android-tutorial-2/jni/Application.mk new file mode 100644 index 0000000..b8848e8 --- /dev/null +++ b/tutorials/android-tutorial-2/jni/Application.mk @@ -0,0 +1 @@ +APP_ABI = armeabi armeabi-v7a arm64-v8a x86 x86_64 diff --git a/tutorials/android-tutorial-2/jni/tutorial-2.c b/tutorials/android-tutorial-2/jni/tutorial-2.c new file mode 100644 index 0000000..5fa780c --- /dev/null +++ b/tutorials/android-tutorial-2/jni/tutorial-2.c @@ -0,0 +1,275 @@ +#include <string.h> +#include <jni.h> +#include <android/log.h> +#include <gst/gst.h> +#include <pthread.h> + +GST_DEBUG_CATEGORY_STATIC (debug_category); +#define GST_CAT_DEFAULT debug_category + +/* + * These macros provide a way to store the native pointer to CustomData, which might be 32 or 64 bits, into + * a jlong, which is always 64 bits, without warnings. + */ +#if GLIB_SIZEOF_VOID_P == 8 +# define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(*env)->GetLongField (env, thiz, fieldID) +# define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)data) +#else +# define GET_CUSTOM_DATA(env, thiz, fieldID) (CustomData *)(jint)(*env)->GetLongField (env, thiz, fieldID) +# define SET_CUSTOM_DATA(env, thiz, fieldID, data) (*env)->SetLongField (env, thiz, fieldID, (jlong)(jint)data) +#endif + +/* Structure to contain all our information, so we can pass it to callbacks */ +typedef struct _CustomData { + jobject app; /* Application instance, used to call its methods. A global reference is kept. */ + GstElement *pipeline; /* The running pipeline */ + GMainContext *context; /* GLib context used to run the main loop */ + GMainLoop *main_loop; /* GLib main loop */ + gboolean initialized; /* To avoid informing the UI multiple times about the initialization */ +} CustomData; + +/* These global variables cache values which are not changing during execution */ +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; +static jmethodID on_gstreamer_initialized_method_id; + +/* + * Private methods + */ + +/* Register this thread with the VM */ +static JNIEnv *attach_current_thread (void) { + JNIEnv *env; + JavaVMAttachArgs args; + + GST_DEBUG ("Attaching thread %p", g_thread_self ()); + args.version = JNI_VERSION_1_4; + args.name = NULL; + args.group = NULL; + + if ((*java_vm)->AttachCurrentThread (java_vm, &env, &args) < 0) { + GST_ERROR ("Failed to attach current thread"); + return NULL; + } + + return env; +} + +/* Unregister this thread from the VM */ +static void detach_current_thread (void *env) { + GST_DEBUG ("Detaching thread %p", g_thread_self ()); + (*java_vm)->DetachCurrentThread (java_vm); +} + +/* Retrieve the JNI environment for this thread */ +static JNIEnv *get_jni_env (void) { + JNIEnv *env; + + if ((env = pthread_getspecific (current_jni_env)) == NULL) { + env = attach_current_thread (); + pthread_setspecific (current_jni_env, env); + } + + return env; +} + +/* Change the content of the UI's TextView */ +static void set_ui_message (const gchar *message, CustomData *data) { + JNIEnv *env = get_jni_env (); + GST_DEBUG ("Setting message to: %s", message); + jstring jmessage = (*env)->NewStringUTF(env, message); + (*env)->CallVoidMethod (env, data->app, set_message_method_id, jmessage); + if ((*env)->ExceptionCheck (env)) { + GST_ERROR ("Failed to call Java method"); + (*env)->ExceptionClear (env); + } + (*env)->DeleteLocalRef (env, jmessage); +} + +/* Retrieve errors from the bus and show them on the UI */ +static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) { + GError *err; + gchar *debug_info; + gchar *message_string; + + gst_message_parse_error (msg, &err, &debug_info); + message_string = g_strdup_printf ("Error received from element %s: %s", GST_OBJECT_NAME (msg->src), err->message); + g_clear_error (&err); + g_free (debug_info); + set_ui_message (message_string, data); + g_free (message_string); + gst_element_set_state (data->pipeline, GST_STATE_NULL); +} + +/* Notify UI about pipeline state changes */ +static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) { + GstState old_state, new_state, pending_state; + gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state); + /* Only pay attention to messages coming from the pipeline, not its children */ + if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->pipeline)) { + gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state)); + set_ui_message(message, data); + g_free (message); + } +} + +/* Check if all conditions are met to report GStreamer as initialized. + * These conditions will change depending on the application */ +static void check_initialization_complete (CustomData *data) { + JNIEnv *env = get_jni_env (); + if (!data->initialized && data->main_loop) { + GST_DEBUG ("Initialization complete, notifying application. main_loop:%p", data->main_loop); + (*env)->CallVoidMethod (env, data->app, on_gstreamer_initialized_method_id); + if ((*env)->ExceptionCheck (env)) { + GST_ERROR ("Failed to call Java method"); + (*env)->ExceptionClear (env); + } + data->initialized = TRUE; + } +} + +/* Main method for the native code. This is executed on its own thread. */ +static void *app_function (void *userdata) { + JavaVMAttachArgs args; + GstBus *bus; + CustomData *data = (CustomData *)userdata; + GSource *bus_source; + GError *error = NULL; + + GST_DEBUG ("Creating pipeline in CustomData at %p", data); + + /* Create our own GLib Main Context and make it the default one */ + data->context = g_main_context_new (); + g_main_context_push_thread_default(data->context); + + /* Build pipeline */ + data->pipeline = gst_parse_launch("audiotestsrc ! audioconvert ! audioresample ! autoaudiosink", &error); + if (error) { + gchar *message = g_strdup_printf("Unable to build pipeline: %s", error->message); + g_clear_error (&error); + set_ui_message(message, data); + g_free (message); + return NULL; + } + + /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ + bus = gst_element_get_bus (data->pipeline); + bus_source = gst_bus_create_watch (bus); + g_source_set_callback (bus_source, (GSourceFunc) gst_bus_async_signal_func, NULL, NULL); + g_source_attach (bus_source, data->context); + g_source_unref (bus_source); + g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, data); + g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, data); + gst_object_unref (bus); + + /* Create a GLib Main Loop and set it to run */ + GST_DEBUG ("Entering main loop... (CustomData:%p)", data); + data->main_loop = g_main_loop_new (data->context, FALSE); + check_initialization_complete (data); + g_main_loop_run (data->main_loop); + GST_DEBUG ("Exited main loop"); + g_main_loop_unref (data->main_loop); + data->main_loop = NULL; + + /* Free resources */ + g_main_context_pop_thread_default(data->context); + g_main_context_unref (data->context); + gst_element_set_state (data->pipeline, GST_STATE_NULL); + gst_object_unref (data->pipeline); + + return NULL; +} + +/* + * Java Bindings + */ + +/* Instruct the native code to create its internal data structure, pipeline and thread */ +static void gst_native_init (JNIEnv* env, jobject thiz) { + CustomData *data = g_new0 (CustomData, 1); + SET_CUSTOM_DATA (env, thiz, custom_data_field_id, data); + GST_DEBUG_CATEGORY_INIT (debug_category, "tutorial-2", 0, "Android tutorial 2"); + gst_debug_set_threshold_for_name("tutorial-2", GST_LEVEL_DEBUG); + GST_DEBUG ("Created CustomData at %p", data); + data->app = (*env)->NewGlobalRef (env, thiz); + GST_DEBUG ("Created GlobalRef for app object at %p", data->app); + pthread_create (&gst_app_thread, NULL, &app_function, data); +} + +/* Quit the main loop, remove the native thread and free resources */ +static void gst_native_finalize (JNIEnv* env, jobject thiz) { + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) return; + GST_DEBUG ("Quitting main loop..."); + g_main_loop_quit (data->main_loop); + GST_DEBUG ("Waiting for thread to finish..."); + pthread_join (gst_app_thread, NULL); + GST_DEBUG ("Deleting GlobalRef for app object at %p", data->app); + (*env)->DeleteGlobalRef (env, data->app); + GST_DEBUG ("Freeing CustomData at %p", data); + g_free (data); + SET_CUSTOM_DATA (env, thiz, custom_data_field_id, NULL); + GST_DEBUG ("Done finalizing"); +} + +/* Set pipeline to PLAYING state */ +static void gst_native_play (JNIEnv* env, jobject thiz) { + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) return; + GST_DEBUG ("Setting state to PLAYING"); + gst_element_set_state (data->pipeline, GST_STATE_PLAYING); +} + +/* Set pipeline to PAUSED state */ +static void gst_native_pause (JNIEnv* env, jobject thiz) { + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + if (!data) return; + GST_DEBUG ("Setting state to PAUSED"); + gst_element_set_state (data->pipeline, GST_STATE_PAUSED); +} + +/* Static class initializer: retrieve method and field IDs */ +static jboolean gst_native_class_init (JNIEnv* env, jclass klass) { + custom_data_field_id = (*env)->GetFieldID (env, klass, "native_custom_data", "J"); + set_message_method_id = (*env)->GetMethodID (env, klass, "setMessage", "(Ljava/lang/String;)V"); + on_gstreamer_initialized_method_id = (*env)->GetMethodID (env, klass, "onGStreamerInitialized", "()V"); + + if (!custom_data_field_id || !set_message_method_id || !on_gstreamer_initialized_method_id) { + /* We emit this message through the Android log instead of the GStreamer log because the later + * has not been initialized yet. + */ + __android_log_print (ANDROID_LOG_ERROR, "tutorial-2", "The calling class does not implement all necessary interface methods"); + return JNI_FALSE; + } + return JNI_TRUE; +} + +/* List of implemented native methods */ +static JNINativeMethod native_methods[] = { + { "nativeInit", "()V", (void *) gst_native_init}, + { "nativeFinalize", "()V", (void *) gst_native_finalize}, + { "nativePlay", "()V", (void *) gst_native_play}, + { "nativePause", "()V", (void *) gst_native_pause}, + { "nativeClassInit", "()Z", (void *) gst_native_class_init} +}; + +/* Library initializer */ +jint JNI_OnLoad(JavaVM *vm, void *reserved) { + JNIEnv *env = NULL; + + java_vm = vm; + + if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) { + __android_log_print (ANDROID_LOG_ERROR, "tutorial-2", "Could not retrieve JNIEnv"); + return 0; + } + jclass klass = (*env)->FindClass (env, "org/freedesktop/gstreamer/tutorials/tutorial_2/Tutorial2"); + (*env)->RegisterNatives (env, klass, native_methods, G_N_ELEMENTS(native_methods)); + + pthread_key_create (¤t_jni_env, detach_current_thread); + + return JNI_VERSION_1_4; +} diff --git a/tutorials/android-tutorial-2/res/drawable-hdpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-hdpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..a390d09 --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-hdpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/drawable-ldpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-ldpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..7386cb9 --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-ldpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/drawable-mdpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-mdpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..59e97ea --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-mdpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/drawable-xhdpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-xhdpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..e383899 --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-xhdpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/drawable-xxhdpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-xxhdpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..d3ea6f0 --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-xxhdpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/drawable-xxxhdpi/gstreamer_logo_2.png b/tutorials/android-tutorial-2/res/drawable-xxxhdpi/gstreamer_logo_2.png Binary files differnew file mode 100644 index 0000000..9493e75 --- /dev/null +++ b/tutorials/android-tutorial-2/res/drawable-xxxhdpi/gstreamer_logo_2.png diff --git a/tutorials/android-tutorial-2/res/layout/main.xml b/tutorials/android-tutorial-2/res/layout/main.xml new file mode 100644 index 0000000..2399033 --- /dev/null +++ b/tutorials/android-tutorial-2/res/layout/main.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:gravity="center_vertical"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/textview_message"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="16dip"
+ android:gravity="center_horizontal" />
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_horizontal"
+ android:orientation="horizontal" >
+
+ <ImageButton
+ android:id="@+id/button_play"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:contentDescription="@string/button_play"
+ android:src="@android:drawable/ic_media_play"
+ android:text="@string/button_play" />
+
+ <ImageButton
+ android:id="@+id/button_stop"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:contentDescription="@string/button_stop"
+ android:src="@android:drawable/ic_media_pause"
+ android:text="@string/button_stop" />
+ </LinearLayout>
+
+</LinearLayout>
\ No newline at end of file diff --git a/tutorials/android-tutorial-2/res/values/strings.xml b/tutorials/android-tutorial-2/res/values/strings.xml new file mode 100644 index 0000000..25216d1 --- /dev/null +++ b/tutorials/android-tutorial-2/res/values/strings.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <string name="app_name">GStreamer tutorial 2</string> + <string name="button_play">Play</string> + <string name="button_stop">Stop</string> +</resources> diff --git a/tutorials/android-tutorial-2/src/org/freedesktop/gstreamer/tutorials/tutorial_2/Tutorial2.java b/tutorials/android-tutorial-2/src/org/freedesktop/gstreamer/tutorials/tutorial_2/Tutorial2.java new file mode 100644 index 0000000..c1f6068 --- /dev/null +++ b/tutorials/android-tutorial-2/src/org/freedesktop/gstreamer/tutorials/tutorial_2/Tutorial2.java @@ -0,0 +1,119 @@ +package org.freedesktop.gstreamer.tutorials.tutorial_2; + +import android.app.Activity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + +import org.freedesktop.gstreamer.GStreamer; + +public class Tutorial2 extends Activity { + private native void nativeInit(); // Initialize native code, build pipeline, etc + private native void nativeFinalize(); // Destroy pipeline and shutdown native code + private native void nativePlay(); // Set pipeline to PLAYING + private native void nativePause(); // Set pipeline to PAUSED + private static native boolean nativeClassInit(); // Initialize native class: cache Method IDs for callbacks + private long native_custom_data; // Native code will use this to keep private data + + private boolean is_playing_desired; // Whether the user asked to go to PLAYING + + // Called when the activity is first created. + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + // Initialize GStreamer and warn if it fails + try { + GStreamer.init(this); + } catch (Exception e) { + Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); + return; + } + + setContentView(R.layout.main); + + ImageButton play = (ImageButton) this.findViewById(R.id.button_play); + play.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + is_playing_desired = true; + nativePlay(); + } + }); + + ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop); + pause.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + is_playing_desired = false; + nativePause(); + } + }); + + if (savedInstanceState != null) { + is_playing_desired = savedInstanceState.getBoolean("playing"); + Log.i ("GStreamer", "Activity created. Saved state is playing:" + is_playing_desired); + } else { + is_playing_desired = false; + Log.i ("GStreamer", "Activity created. There is no saved state, playing: false"); + } + + // Start with disabled buttons, until native code is initialized + this.findViewById(R.id.button_play).setEnabled(false); + this.findViewById(R.id.button_stop).setEnabled(false); + + nativeInit(); + } + + protected void onSaveInstanceState (Bundle outState) { + Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired); + outState.putBoolean("playing", is_playing_desired); + } + + protected void onDestroy() { + nativeFinalize(); + super.onDestroy(); + } + + // Called from native code. This sets the content of the TextView from the UI thread. + private void setMessage(final String message) { + final TextView tv = (TextView) this.findViewById(R.id.textview_message); + runOnUiThread (new Runnable() { + public void run() { + tv.setText(message); + } + }); + } + + // Called from native code. Native code calls this once it has created its pipeline and + // the main loop is running, so it is ready to accept commands. + private void onGStreamerInitialized () { + Log.i ("GStreamer", "Gst initialized. Restoring state, playing:" + is_playing_desired); + // Restore previous playing state + if (is_playing_desired) { + nativePlay(); + } else { + nativePause(); + } + + // Re-enable buttons, now that GStreamer is initialized + final Activity activity = this; + runOnUiThread(new Runnable() { + public void run() { + activity.findViewById(R.id.button_play).setEnabled(true); + activity.findViewById(R.id.button_stop).setEnabled(true); + } + }); + } + + static { + System.loadLibrary("gstreamer_android"); + System.loadLibrary("tutorial-2"); + nativeClassInit(); + } + +} |