From f7446956c9ffcea6cdfa13d2b0aff0c45e655497 Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Wed, 12 Sep 2012 17:39:14 +0200 Subject: [PATCH] Add a SurfaceView and tell native code about it. Still no sink though. --- .../tutorials/android-tutorial-1/jni/Android.mk | 1 + .../tutorials/android-tutorial-1/jni/tutorial-1.c | 25 +++++++- .../android-tutorial-1/res/layout/main.xml | 5 ++ .../gst_sdk_tutorials/tutorial_1/Tutorial1.java | 72 ++++++++++++++-------- 4 files changed, 78 insertions(+), 25 deletions(-) diff --git a/gst-sdk/tutorials/android-tutorial-1/jni/Android.mk b/gst-sdk/tutorials/android-tutorial-1/jni/Android.mk index 4e8fbd2..7bf074d 100755 --- a/gst-sdk/tutorials/android-tutorial-1/jni/Android.mk +++ b/gst-sdk/tutorials/android-tutorial-1/jni/Android.mk @@ -19,6 +19,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := tutorial-1 LOCAL_SRC_FILES := tutorial-1.c LOCAL_SHARED_LIBRARIES := gstreamer_android +LOCAL_LDLIBS := -landroid include $(BUILD_SHARED_LIBRARY) GSTREAMER_PLUGINS = coreelements audiotestsrc videotestsrc ogg theora vorbis ffmpegcolorspace playback app audioconvert audiorate audioresample adder coreindexers gdp gio uridecodebin videorate videoscale typefindfunctions libvisual pango subparse diff --git a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c index e6dd32a..8bd7256 100755 --- a/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c +++ b/gst-sdk/tutorials/android-tutorial-1/jni/tutorial-1.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include @@ -22,6 +24,7 @@ typedef struct _CustomData { jobject app; GstElement *pipeline; GMainLoop *main_loop; + ANativeWindow *native_window; } CustomData; static pthread_t gst_app_thread; @@ -151,12 +154,32 @@ void gst_class_init (JNIEnv* env, jclass klass) { GST_DEBUG ("The MethodID for the setMessage method is %p", set_message_method_id); } +void gst_native_surface_init (JNIEnv *env, jobject thiz, jobject surface) { + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + GST_DEBUG ("Received surface %p", surface); + if (data->native_window) { + GST_DEBUG ("Releasing previous native window %p", data->native_window); + ANativeWindow_release (data->native_window); + } + data->native_window = ANativeWindow_fromSurface(env, surface); + GST_DEBUG ("Got Native Window %p", data->native_window); +} + +void gst_native_surface_finalize (JNIEnv *env, jobject thiz) { + CustomData *data = GET_CUSTOM_DATA (env, thiz, custom_data_field_id); + GST_DEBUG ("Releasing Native Window %p", data->native_window); + ANativeWindow_release (data->native_window); + data->native_window = NULL; +} + 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}, - { "classInit", "()V", (void *) gst_class_init} + { "classInit", "()V", (void *) gst_class_init}, + { "nativeSurfaceInit", "(Ljava/lang/Object;)V", (void *) gst_native_surface_init}, + { "nativeSurfaceFinalize", "()V", (void *) gst_native_surface_finalize} }; jint JNI_OnLoad(JavaVM *vm, void *reserved) { diff --git a/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml b/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml index a6b9824..651740a 100755 --- a/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml +++ b/gst-sdk/tutorials/android-tutorial-1/res/layout/main.xml @@ -31,4 +31,9 @@ android:text="@string/button_stop" /> + + \ No newline at end of file diff --git a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java index 809bd2c..7df52e9 100755 --- a/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java +++ b/gst-sdk/tutorials/android-tutorial-1/src/com/gst_sdk_tutorials/tutorial_1/Tutorial1.java @@ -18,18 +18,22 @@ package com.gst_sdk_tutorials.tutorial_1; import android.app.Activity; import android.util.Log; import android.os.Bundle; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.TextView; -public class Tutorial1 extends Activity -{ +public class Tutorial1 extends Activity implements SurfaceHolder.Callback { private native void nativeInit(); private native void nativeFinalize(); private native void nativePlay(); private native void nativePause(); private static native void classInit(); + private native void nativeSurfaceInit(Object surface); + private native void nativeSurfaceFinalize(); private long native_custom_data; /* Called when the activity is first created. @@ -40,40 +44,44 @@ public class Tutorial1 extends Activity setContentView(R.layout.main); - ImageButton play = (ImageButton)this.findViewById(R.id.button_play); + ImageButton play = (ImageButton) this.findViewById(R.id.button_play); play.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - nativePlay(); - } + public void onClick(View v) { + nativePlay(); + } }); - ImageButton pause = (ImageButton)this.findViewById(R.id.button_stop); + ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop); pause.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - nativePause(); - } + public void onClick(View v) { + nativePause(); + } }); + SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video); + SurfaceHolder sh = sv.getHolder(); + sh.addCallback(this); + nativeInit(); } - protected void onDestroy () { - nativeFinalize(); - super.onDestroy(); + protected void onDestroy() { + nativeFinalize(); + 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() + 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() {public void run() { - tv.setText (message); - } - }); - } catch (Exception e) { - e.printStackTrace(); - } + tv.setText(message); + } + }); + } catch (Exception e) { + e.printStackTrace(); + } } static { @@ -81,4 +89,20 @@ public class Tutorial1 extends Activity System.loadLibrary("tutorial-1"); classInit(); } + + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + Log.d("GStreamer", "Surface changed to format " + format + " width " + + width + " height " + height); + nativeSurfaceInit (holder.getSurface()); + } + + public void surfaceCreated(SurfaceHolder holder) { + Log.d("GStreamer", "Surface created: " + holder.getSurface()); + } + + public void surfaceDestroyed(SurfaceHolder holder) { + Log.d("GStreamer", "Surface destroyed"); + nativeSurfaceFinalize (); + } } -- 2.7.4