Merging gst-examples
[platform/upstream/gstreamer.git] / subprojects / gst-examples / vulkan / android / src / org / freedesktop / gstreamer / vulkan / Vulkan1.java
1 package org.freedesktop.gstreamer.vulkan;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6 import android.view.SurfaceHolder;
7 import android.view.SurfaceView;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10 import android.widget.ImageButton;
11 import android.widget.TextView;
12 import android.widget.Toast;
13
14 import org.freedesktop.gstreamer.GStreamer;
15 import org.freedesktop.gstreamer.vulkan.vulkan_1.R;
16
17 public class Vulkan1 extends Activity implements SurfaceHolder.Callback {
18     private native void nativeInit();     // Initialize native code, build pipeline, etc
19     private native void nativeFinalize(); // Destroy pipeline and shutdown native code
20     private native void nativePlay();     // Set pipeline to PLAYING
21     private native void nativePause();    // Set pipeline to PAUSED
22     private static native boolean nativeClassInit(); // Initialize native class: cache Method IDs for callbacks
23     private native void nativeSurfaceInit(Object surface);
24     private native void nativeSurfaceFinalize();
25     private long native_custom_data;      // Native code will use this to keep private data
26
27     private boolean is_playing_desired;   // Whether the user asked to go to PLAYING
28
29     // Called when the activity is first created.
30     @Override
31     public void onCreate(Bundle savedInstanceState)
32     {
33         super.onCreate(savedInstanceState);
34
35         // Initialize GStreamer and warn if it fails
36         try {
37             GStreamer.init(this);
38         } catch (Exception e) {
39             Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
40             finish();
41             return;
42         }
43
44         setContentView(R.layout.main);
45
46         ImageButton play = (ImageButton) this.findViewById(R.id.button_play);
47         play.setOnClickListener(new OnClickListener() {
48             public void onClick(View v) {
49                 is_playing_desired = true;
50                 nativePlay();
51             }
52         });
53
54         ImageButton pause = (ImageButton) this.findViewById(R.id.button_stop);
55         pause.setOnClickListener(new OnClickListener() {
56             public void onClick(View v) {
57                 is_playing_desired = false;
58                 nativePause();
59             }
60         });
61
62         SurfaceView sv = (SurfaceView) this.findViewById(R.id.surface_video);
63         SurfaceHolder sh = sv.getHolder();
64         sh.addCallback(this);
65
66         if (savedInstanceState != null) {
67             is_playing_desired = savedInstanceState.getBoolean("playing");
68             Log.i ("GStreamer", "Activity created. Saved state is playing:" + is_playing_desired);
69         } else {
70             is_playing_desired = false;
71             Log.i ("GStreamer", "Activity created. There is no saved state, playing: false");
72         }
73
74         // Start with disabled buttons, until native code is initialized
75         this.findViewById(R.id.button_play).setEnabled(false);
76         this.findViewById(R.id.button_stop).setEnabled(false);
77
78         nativeInit();
79     }
80
81     protected void onSaveInstanceState (Bundle outState) {
82         Log.d ("GStreamer", "Saving state, playing:" + is_playing_desired);
83         outState.putBoolean("playing", is_playing_desired);
84     }
85
86     protected void onDestroy() {
87         nativeFinalize();
88         super.onDestroy();
89     }
90
91     // Called from native code. This sets the content of the TextView from the UI thread.
92     private void setMessage(final String message) {
93         final TextView tv = (TextView) this.findViewById(R.id.textview_message);
94         runOnUiThread (new Runnable() {
95           public void run() {
96             tv.setText(message);
97           }
98         });
99     }
100
101     // Called from native code. Native code calls this once it has created its pipeline and
102     // the main loop is running, so it is ready to accept commands.
103     private void onGStreamerInitialized () {
104         Log.i ("GStreamer", "Gst initialized. Restoring state, playing:" + is_playing_desired);
105         // Restore previous playing state
106         if (is_playing_desired) {
107             nativePlay();
108         } else {
109             nativePause();
110         }
111
112         // Re-enable buttons, now that GStreamer is initialized
113         final Activity activity = this;
114         runOnUiThread(new Runnable() {
115             public void run() {
116                 activity.findViewById(R.id.button_play).setEnabled(true);
117                 activity.findViewById(R.id.button_stop).setEnabled(true);
118             }
119         });
120     }
121
122     static {
123         System.loadLibrary("gstreamer_android");
124         System.loadLibrary("vulkan-1");
125         nativeClassInit();
126     }
127
128     public void surfaceChanged(SurfaceHolder holder, int format, int width,
129             int height) {
130         Log.d("GStreamer", "Surface changed to format " + format + " width "
131                 + width + " height " + height);
132         nativeSurfaceInit (holder.getSurface());
133     }
134
135     public void surfaceCreated(SurfaceHolder holder) {
136         Log.d("GStreamer", "Surface created: " + holder.getSurface());
137     }
138
139     public void surfaceDestroyed(SurfaceHolder holder) {
140         Log.d("GStreamer", "Surface destroyed");
141         nativeSurfaceFinalize ();
142     }
143
144 }