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