- Timestamp:
- Mar 22, 2015 8:53:03 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/pjsua/android/src/org/pjsip/pjsua/MainActivity.java
r4496 r5014 26 26 import android.os.Bundle; 27 27 import android.util.Log; 28 import android.view.SurfaceHolder; 29 import android.view.SurfaceView; 28 30 import android.os.Handler; 29 31 import android.os.Message; … … 31 33 32 34 class CONST { 33 34 35 36 37 38 39 40 41 42 43 }; 35 public static final String LIB_FILENAME = "pjsua"; 36 public static final String TAG = "pjsua"; 37 public static final Boolean AUTOKILL_ON_FINISH = true; 38 public enum MSG_TYPE { 39 STR_DEBUG, 40 STR_INFO, 41 STR_ERROR, 42 CLI_STOP, 43 CLI_RESTART, 44 QUIT 45 }; 44 46 } 45 47 46 48 class LOG { 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 49 public static void DEBUG(Handler h, String str) { 50 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_DEBUG.ordinal(), 51 str); 52 msg.sendToTarget(); 53 } 54 public static void INFO(Handler h, String str) { 55 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_INFO.ordinal(), 56 str); 57 msg.sendToTarget(); 58 } 59 public static void ERROR(Handler h, String str) { 60 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_ERROR.ordinal(), 61 str); 62 msg.sendToTarget(); 63 } 62 64 } 63 65 64 public class MainActivity extends Activity { 65 private MyHandler ui_handler = new MyHandler(this); 66 private static MyCallback callback; 67 68 private static class MyHandler extends Handler { 69 private final WeakReference<MainActivity> mTarget; 70 71 public MyHandler(MainActivity target) { 72 mTarget = new WeakReference<MainActivity>(target); 66 public class MainActivity extends Activity implements SurfaceHolder.Callback { 67 private MyHandler ui_handler = new MyHandler(this); 68 private static MyCallback callback; 69 70 private static class MyHandler extends Handler { 71 private final WeakReference<MainActivity> mTarget; 72 73 public MyHandler(MainActivity target) { 74 mTarget = new WeakReference<MainActivity>(target); 75 } 76 77 @Override 78 public void handleMessage(Message m) { 79 MainActivity target = mTarget.get(); 80 if (target == null) 81 return; 82 83 if (m.what == CONST.MSG_TYPE.STR_DEBUG.ordinal()) { 84 Log.d(CONST.TAG, (String)m.obj); 85 } else if (m.what == CONST.MSG_TYPE.STR_INFO.ordinal()) { 86 target.updateStatus((String)m.obj); 87 Log.i(CONST.TAG, (String)m.obj); 88 } else if (m.what == CONST.MSG_TYPE.STR_ERROR.ordinal()) { 89 target.updateStatus((String)m.obj); 90 Log.e(CONST.TAG, (String)m.obj); 91 } else if (m.what == CONST.MSG_TYPE.CLI_STOP.ordinal()) { 92 pjsua.pjsuaDestroy(); 93 LOG.INFO(this, "Telnet Unavailable"); 94 } else if (m.what == CONST.MSG_TYPE.CLI_RESTART.ordinal()) { 95 int status = pjsua.pjsuaRestart(); 96 if (status != 0) { 97 LOG.INFO(this, "Failed restarting telnet"); 73 98 } 74 75 @Override 76 public void handleMessage(Message m) { 77 MainActivity target = mTarget.get(); 78 if (target == null) 79 return; 80 81 if (m.what == CONST.MSG_TYPE.STR_DEBUG.ordinal()) { 82 Log.d(CONST.TAG, (String)m.obj); 83 } else if (m.what == CONST.MSG_TYPE.STR_INFO.ordinal()) { 84 target.updateStatus((String)m.obj); 85 Log.i(CONST.TAG, (String)m.obj); 86 } else if (m.what == CONST.MSG_TYPE.STR_ERROR.ordinal()) { 87 target.updateStatus((String)m.obj); 88 Log.e(CONST.TAG, (String)m.obj); 89 } else if (m.what == CONST.MSG_TYPE.CLI_STOP.ordinal()) { 90 pjsua.pjsuaDestroy(); 91 LOG.INFO(this, "Telnet Unavailable"); 92 } else if (m.what == CONST.MSG_TYPE.CLI_RESTART.ordinal()) { 93 int status = pjsua.pjsuaRestart(); 94 if (status != 0) { 95 LOG.INFO(this, "Failed restarting telnet"); 96 } 97 } else if (m.what == CONST.MSG_TYPE.QUIT.ordinal()) { 98 target.finish(); 99 System.gc(); 100 android.os.Process.killProcess(android.os.Process.myPid()); 101 } 102 } 103 } 104 105 /** Callback object **/ 106 private static class MyCallback extends PjsuaAppCallback { 107 private WeakReference<Handler> ui_handler; 108 109 public MyCallback(Handler in_ui_handler) { 110 set_ui_handler(in_ui_handler); 111 } 112 113 public void set_ui_handler(Handler in_ui_handler) { 114 ui_handler = new WeakReference<Handler>(in_ui_handler); 115 } 116 117 @Override 118 public void onStarted(String msg) { 119 Handler ui = ui_handler.get(); 120 LOG.INFO(ui, msg); 121 } 122 123 @Override 124 public void onStopped(int restart) { 125 Handler ui = ui_handler.get(); 126 /** Use timer to stopped/restart **/ 127 if (restart != 0) { 128 LOG.INFO(ui, "Telnet Restarting"); 129 Message msg = Message.obtain(ui, 130 CONST.MSG_TYPE.CLI_RESTART.ordinal()); 131 ui.sendMessageDelayed(msg, 100); 132 } else { 133 LOG.INFO(ui, "Telnet Stopping"); 134 Message msg = Message.obtain(ui, 135 CONST.MSG_TYPE.CLI_STOP.ordinal()); 136 ui.sendMessageDelayed(msg, 100); 137 } 138 } 139 } 140 141 private void updateStatus(String output) { 142 TextView tStatus = (TextView) findViewById(R.id.textStatus); 143 tStatus.setText(output); 144 } 99 } else if (m.what == CONST.MSG_TYPE.QUIT.ordinal()) { 100 target.finish(); 101 System.gc(); 102 android.os.Process.killProcess(android.os.Process.myPid()); 103 } 104 } 105 } 106 107 /** Callback object **/ 108 private static class MyCallback extends PjsuaAppCallback { 109 private WeakReference<Handler> ui_handler; 110 111 public MyCallback(Handler in_ui_handler) { 112 set_ui_handler(in_ui_handler); 113 } 114 115 public void set_ui_handler(Handler in_ui_handler) { 116 ui_handler = new WeakReference<Handler>(in_ui_handler); 117 } 118 119 @Override 120 public void onStarted(String msg) { 121 Handler ui = ui_handler.get(); 122 LOG.INFO(ui, msg); 123 } 124 125 @Override 126 public void onStopped(int restart) { 127 Handler ui = ui_handler.get(); 128 /** Use timer to stopped/restart **/ 129 if (restart != 0) { 130 LOG.INFO(ui, "Telnet Restarting"); 131 Message msg = Message.obtain(ui, 132 CONST.MSG_TYPE.CLI_RESTART.ordinal()); 133 ui.sendMessageDelayed(msg, 100); 134 } else { 135 LOG.INFO(ui, "Telnet Stopping"); 136 Message msg = Message.obtain(ui, 137 CONST.MSG_TYPE.CLI_STOP.ordinal()); 138 ui.sendMessageDelayed(msg, 100); 139 } 140 } 141 } 142 143 private void updateStatus(String output) { 144 TextView tStatus = (TextView) findViewById(R.id.textStatus); 145 tStatus.setText(output); 146 } 147 148 @Override 149 protected void onCreate(Bundle savedInstanceState) { 150 LOG.DEBUG(ui_handler, "=== Activity::onCreate() ==="); 151 super.onCreate(savedInstanceState); 152 153 init_view(); 154 155 init_lib(); 156 } 157 158 @Override 159 protected void onStart() { 160 LOG.DEBUG(ui_handler, "=== Activity::onStart() ==="); 161 super.onStart(); 162 } 163 164 @Override 165 protected void onRestart() { 166 LOG.DEBUG(ui_handler, "=== Activity::onRestart() ==="); 167 super.onRestart(); 168 } 169 170 @Override 171 protected void onResume() { 172 LOG.DEBUG(ui_handler, "=== Activity::onResume() ==="); 173 super.onResume(); 174 } 175 176 @Override 177 protected void onPause() { 178 LOG.DEBUG(ui_handler, "=== Activity::onPause() ==="); 179 super.onPause(); 180 } 181 182 @Override 183 protected void onStop() { 184 LOG.DEBUG(ui_handler, "=== Activity::onStop() ==="); 185 super.onStop(); 186 } 187 188 @Override 189 protected void onDestroy() { 190 LOG.DEBUG(ui_handler, "=== Activity::onDestroy() ==="); 191 super.onDestroy(); 192 } 193 194 @Override 195 protected void onSaveInstanceState(Bundle outState) { 196 super.onSaveInstanceState(outState); 197 } 198 199 @Override 200 protected void onRestoreInstanceState(Bundle savedInstanceState) { 201 super.onRestoreInstanceState(savedInstanceState); 202 } 203 204 private void init_view() { 205 setContentView(R.layout.activity_main); 206 } 207 208 private int init_lib() { 209 LOG.INFO(ui_handler, "Loading module..."); 210 211 // Try loading video dependency libs 212 try { 213 System.loadLibrary("openh264"); 214 System.loadLibrary("yuv"); 215 } catch (UnsatisfiedLinkError e) { 216 LOG.ERROR(ui_handler, "UnsatisfiedLinkError: " + e.getMessage()); 217 LOG.ERROR(ui_handler, "This could be safely ignored if you "+ 218 "don't need video."); 219 } 220 221 // Load pjsua 222 try { 223 System.loadLibrary(CONST.LIB_FILENAME); 224 } catch (UnsatisfiedLinkError e) { 225 LOG.ERROR(ui_handler, "UnsatisfiedLinkError: " + e.getMessage()); 226 return -1; 227 } 228 229 // Wait for GDB to init, for native debugging only 230 if (false && (getApplicationInfo().flags & 231 ApplicationInfo.FLAG_DEBUGGABLE) != 0) 232 { 233 try { 234 Thread.sleep(5000); 235 } catch (InterruptedException e) { 236 LOG.ERROR(ui_handler, "InterruptedException: " + 237 e.getMessage()); 238 } 239 } 240 241 // Set callback object 242 if (callback == null) 243 callback = new MyCallback(ui_handler); 244 245 pjsua.setCallbackObject(callback); 246 247 SurfaceView surfaceView = (SurfaceView) 248 findViewById(R.id.surfaceViewIncomingCall); 249 surfaceView.getHolder().addCallback(this); 250 251 LOG.INFO(ui_handler, "Starting module.."); 252 253 int rc = pjsua.pjsuaStart(); 254 255 if (rc != 0) { 256 LOG.INFO(ui_handler, "Failed starting telnet"); 257 } 258 259 return 0; 260 } 145 261 146 @Override 147 protected void onCreate(Bundle savedInstanceState) { 148 LOG.DEBUG(ui_handler, "=== Activity::onCreate() ==="); 149 super.onCreate(savedInstanceState); 150 151 init_view(); 152 153 init_lib(); 154 } 155 156 @Override 157 protected void onStart() { 158 LOG.DEBUG(ui_handler, "=== Activity::onStart() ==="); 159 super.onStart(); 262 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 263 { 264 pjsua.setIncomingVideoRenderer(holder.getSurface()); 265 } 266 267 public void surfaceCreated(SurfaceHolder holder) 268 { 269 pjsua.setIncomingVideoRenderer(holder.getSurface()); 270 } 271 272 public void surfaceDestroyed(SurfaceHolder holder) 273 { 274 pjsua.setIncomingVideoRenderer(null); 160 275 } 161 276 162 @Override163 protected void onRestart() {164 LOG.DEBUG(ui_handler, "=== Activity::onRestart() ===");165 super.onRestart();166 }167 168 @Override169 protected void onResume() {170 LOG.DEBUG(ui_handler, "=== Activity::onResume() ===");171 super.onResume();172 }173 174 @Override175 protected void onPause() {176 LOG.DEBUG(ui_handler, "=== Activity::onPause() ===");177 super.onPause();178 }179 180 @Override181 protected void onStop() {182 LOG.DEBUG(ui_handler, "=== Activity::onStop() ===");183 super.onStop();184 }185 186 @Override187 protected void onDestroy() {188 LOG.DEBUG(ui_handler, "=== Activity::onDestroy() ===");189 super.onDestroy();190 }191 192 @Override193 protected void onSaveInstanceState(Bundle outState) {194 super.onSaveInstanceState(outState);195 }196 197 @Override198 protected void onRestoreInstanceState(Bundle savedInstanceState) {199 super.onRestoreInstanceState(savedInstanceState);200 }201 202 private void init_view() {203 setContentView(R.layout.activity_main);204 }205 206 private int init_lib() {207 LOG.INFO(ui_handler, "Loading module...");208 try {209 System.loadLibrary(CONST.LIB_FILENAME);210 } catch (UnsatisfiedLinkError e) {211 LOG.ERROR(ui_handler, "UnsatisfiedLinkError: " + e.getMessage());212 return -1;213 }214 215 // Wait for GDB to init216 if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0)217 {218 try {219 Thread.sleep(5000);220 } catch (InterruptedException e) {221 LOG.ERROR(ui_handler, "InterruptedException: " +222 e.getMessage());223 }224 }225 226 // Set callback object227 if (callback == null)228 callback = new MyCallback(ui_handler);229 230 pjsua.setCallbackObject(callback);231 232 LOG.INFO(ui_handler, "Starting module..");233 234 int rc = pjsua.pjsuaStart();235 236 if (rc != 0) {237 LOG.INFO(ui_handler, "Failed starting telnet");238 }239 240 return 0;241 }242 277 }
Note: See TracChangeset
for help on using the changeset viewer.