Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr) |
| 3 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 4 | * |
| 5 | * Author: Regis Montoya <r3gis.3R@gmail.com> |
| 6 | * Author: Emeric Vigier <emeric.vigier@savoirfairelinux.com> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * If you own a pjsip commercial license you can also redistribute it |
| 13 | * and/or modify it under the terms of the GNU Lesser General Public License |
| 14 | * as an android library. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 24 | package com.savoirfairelinux.sflphone.service; |
| 25 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 26 | import java.lang.ref.WeakReference; |
| 27 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 28 | import android.app.Service; |
| 29 | import android.content.Intent; |
| 30 | import android.os.Binder; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.HandlerThread; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 33 | import android.os.IBinder; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.widget.Toast; |
| 38 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 39 | import com.savoirfairelinux.sflphone.service.ManagerImpl; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 40 | import com.savoirfairelinux.sflphone.client.SFLphoneApplication; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 41 | import com.savoirfairelinux.sflphone.service.ISipService; |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 42 | |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 43 | import java.util.HashMap; |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 44 | import java.util.ArrayList; |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 45 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 46 | public class SipService extends Service { |
| 47 | |
| 48 | static final String TAG = "SipService"; |
| 49 | static final int DELAY = 5000; /* 5 sec */ |
| 50 | private boolean runFlag = false; |
| 51 | private SipServiceThread sipServiceThread; |
Emeric Vigier | 84e05da | 2012-09-20 14:53:05 -0400 | [diff] [blame] | 52 | private SFLphoneApplication sflphoneApp; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 53 | private SipServiceExecutor mExecutor; |
| 54 | private static HandlerThread executorThread; |
| 55 | private CallManagerJNI callManagerJNI; |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 56 | private CallManagerCallBack callManagerCallBack; |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 57 | private ConfigurationManagerJNI configurationManagerJNI; |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 58 | private ManagerImpl managerImpl; |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 59 | private boolean isPjSipStackStarted = false; |
| 60 | |
| 61 | /* Implement public interface for the service */ |
| 62 | private final ISipService.Stub mBinder = new ISipService.Stub() { |
| 63 | |
| 64 | @Override |
| 65 | public void placeCall(final String accountID, final String callID, final String to) { |
| 66 | getExecutor().execute(new SipRunnable() { |
| 67 | @Override |
| 68 | protected void doRun() throws SameThreadException { |
| 69 | Log.i(TAG, "SipService.placeCall() thread running..."); |
| 70 | callManagerJNI.placeCall(accountID, callID, to); |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void refuse(final String callID) { |
| 77 | getExecutor().execute(new SipRunnable() { |
| 78 | @Override |
| 79 | protected void doRun() throws SameThreadException { |
| 80 | Log.i(TAG, "SipService.refuse() thread running..."); |
| 81 | callManagerJNI.refuse(callID); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void accept(final String callID) { |
| 88 | getExecutor().execute(new SipRunnable() { |
| 89 | @Override |
| 90 | protected void doRun() throws SameThreadException { |
| 91 | Log.i(TAG, "SipService.placeCall() thread running..."); |
| 92 | callManagerJNI.accept(callID); |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void hangUp(final String callID) { |
| 99 | getExecutor().execute(new SipRunnable() { |
| 100 | @Override |
| 101 | protected void doRun() throws SameThreadException { |
| 102 | Log.i(TAG, "SipService.hangUp() thread running..."); |
| 103 | callManagerJNI.hangUp(callID); |
| 104 | } |
| 105 | }); |
| 106 | } |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | public void setAudioPlugin(final String audioPlugin) { |
| 110 | getExecutor().execute(new SipRunnable() { |
| 111 | @Override |
| 112 | protected void doRun() throws SameThreadException { |
| 113 | Log.i(TAG, "SipService.setAudioPlugin() thread running..."); |
| 114 | configurationManagerJNI.setAudioPlugin(audioPlugin); |
| 115 | } |
| 116 | }); |
| 117 | } |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 118 | |
| 119 | @Override |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 120 | public ArrayList<String> getAccountList() { |
| 121 | StringVect swigvect = configurationManagerJNI.getAccountList(); |
| 122 | ArrayList<String> nativelist = new ArrayList<String>(); |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 123 | |
| 124 | for(int i = 0; i < swigvect.size(); i++) |
| 125 | nativelist.add(swigvect.get(i)); |
| 126 | |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 127 | return nativelist; |
| 128 | } |
| 129 | |
| 130 | @Override |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 131 | public HashMap<String,String> getAccountDetails(final String accountID) { |
| 132 | StringMap swigmap = configurationManagerJNI.getAccountDetails(accountID); |
| 133 | |
| 134 | HashMap<String, String> nativemap = new HashMap<String, String>(); |
Alexandre Savard | 6b85e7e | 2012-09-27 15:43:14 -0400 | [diff] [blame] | 135 | |
| 136 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ALIAS, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ALIAS)); |
| 137 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME)); |
| 138 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_USERNAME, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_USERNAME)); |
| 139 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ROUTESET, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ROUTESET)); |
| 140 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE)); |
| 141 | nativemap.put(ServiceConstants.CONFIG_LOCAL_INTERFACE, swigmap.get(ServiceConstants.CONFIG_LOCAL_INTERFACE)); |
| 142 | nativemap.put(ServiceConstants.CONFIG_STUN_SERVER, swigmap.get(ServiceConstants.CONFIG_STUN_SERVER)); |
| 143 | nativemap.put(ServiceConstants.CONFIG_TLS_ENABLE, swigmap.get(ServiceConstants.CONFIG_TLS_ENABLE)); |
| 144 | nativemap.put(ServiceConstants.CONFIG_SRTP_ENABLE, swigmap.get(ServiceConstants.CONFIG_SRTP_ENABLE)); |
| 145 | |
| 146 | /* |
| 147 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_TYPE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_TYPE)); |
| 148 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ALIAS, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ALIAS)); |
| 149 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_MAILBOX, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_MAILBOX)); |
| 150 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ENABLE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ENABLE)); |
| 151 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE)); |
| 152 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATUS, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATUS)); |
| 153 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE)); |
| 154 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC)); |
| 155 | nativemap.put(ServiceConstants.CONFIG_CREDENTIAL_NUMBER, swigmap.get(ServiceConstants.CONFIG_CREDENTIAL_NUMBER)); |
| 156 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_DTMF_TYPE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_DTMF_TYPE)); |
| 157 | nativemap.put(ServiceConstants.CONFIG_RINGTONE_PATH, swigmap.get(ServiceConstants.CONFIG_RINGTONE_PATH)); |
| 158 | nativemap.put(ServiceConstants.CONFIG_RINGTONE_ENABLED, swigmap.get(ServiceConstants.CONFIG_RINGTONE_ENABLED)); |
| 159 | nativemap.put(ServiceConstants.CONFIG_KEEP_ALIVE_ENABLED, swigmap.get(ServiceConstants.CONFIG_KEEP_ALIVE_ENABLED)); |
| 160 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_PASSWORD, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_PASSWORD)); |
| 161 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REALM, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REALM)); |
| 162 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_DEFAULT_REALM, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_DEFAULT_REALM)); |
| 163 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_USERAGENT, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_USERAGENT)); |
| 164 | nativemap.put(ServiceConstants.CONFIG_ACCOUNT_AUTOANSWER, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_AUTOANSWER)); |
| 165 | nativemap.put(ServiceConstants.CONFIG_INTERFACE, swigmap.get(ServiceConstants.CONFIG_INTERFACE)); |
| 166 | nativemap.put(ServiceConstants.CONFIG_PUBLISHED_SAMEAS_LOCAL, swigmap.get(ServiceConstants.CONFIG_PUBLISHED_SAMEAS_LOCAL)); |
| 167 | nativemap.put(ServiceConstants.CONFIG_DEFAULT_INTERFACE, swigmap.get(ServiceConstants.CONFIG_DEFAULT_INTERFACE)); |
| 168 | nativemap.put(ServiceConstants.CONFIG_DISPLAY_NAME, swigmap.get(ServiceConstants.CONFIG_DISPLAY_NAME)); |
| 169 | nativemap.put(ServiceConstants.CONFIG_DEFAULT_ADDRESS, swigmap.get(ServiceConstants.CONFIG_DEFAULT_ADDRESS)); |
| 170 | nativemap.put(ServiceConstants.CONFIG_SRTP_KEY_EXCHANGE, swigmap.get(ServiceConstants.CONFIG_SRTP_KEY_EXCHANGE)); |
| 171 | nativemap.put(ServiceConstants.CONFIG_SRTP_ENCRYPTION_ALGO, swigmap.get(ServiceConstants.CONFIG_SRTP_ENCRYPTION_ALGO)); |
| 172 | nativemap.put(ServiceConstants.CONFIG_SRTP_RTP_FALLBACK, swigmap.get(ServiceConstants.CONFIG_SRTP_RTP_FALLBACK)); |
| 173 | nativemap.put(ServiceConstants.CONFIG_ZRTP_HELLO_HASH, swigmap.get(ServiceConstants.CONFIG_ZRTP_HELLO_HASH)); |
| 174 | nativemap.put(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS, swigmap.get(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS)); |
| 175 | nativemap.put(ServiceConstants.CONFIG_ZRTP_NOT_SUPP_WARNING, swigmap.get(ServiceConstants.CONFIG_ZRTP_NOT_SUPP_WARNING)); |
| 176 | nativemap.put(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS_ONCE, swigmap.get(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS_ONCE)); |
| 177 | nativemap.put(ServiceConstants.CONFIG_TLS_LISTENER_PORT, swigmap.get(ServiceConstants.CONFIG_TLS_LISTENER_PORT)); |
| 178 | nativemap.put(ServiceConstants.CONFIG_TLS_CA_LIST_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_CA_LIST_FILE)); |
| 179 | nativemap.put(ServiceConstants.CONFIG_TLS_CERTIFICATE_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_CERTIFICATE_FILE)); |
| 180 | nativemap.put(ServiceConstants.CONFIG_TLS_PRIVATE_KEY_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_PRIVATE_KEY_FILE)); |
| 181 | nativemap.put(ServiceConstants.CONFIG_TLS_PASSWORD, swigmap.get(ServiceConstants.CONFIG_TLS_PASSWORD)); |
| 182 | nativemap.put(ServiceConstants.CONFIG_TLS_METHOD, swigmap.get(ServiceConstants.CONFIG_TLS_METHOD)); |
| 183 | nativemap.put(ServiceConstants.CONFIG_TLS_CIPHERS, swigmap.get(ServiceConstants.CONFIG_TLS_CIPHERS)); |
| 184 | nativemap.put(ServiceConstants.CONFIG_TLS_SERVER_NAME, swigmap.get(ServiceConstants.CONFIG_TLS_SERVER_NAME)); |
| 185 | nativemap.put(ServiceConstants.CONFIG_TLS_VERIFY_SERVER, swigmap.get(ServiceConstants.CONFIG_TLS_VERIFY_SERVER)); |
| 186 | nativemap.put(ServiceConstants.CONFIG_TLS_VERIFY_CLIENT, swigmap.get(ServiceConstants.CONFIG_TLS_VERIFY_CLIENT)); |
| 187 | nativemap.put(ServiceConstants.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, swigmap.get(ServiceConstants.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE)); |
| 188 | nativemap.put(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC, swigmap.get(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC)); |
| 189 | nativemap.put(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC, swigmap.get(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC)); |
| 190 | */ |
Alexandre Savard | 713a34d | 2012-09-26 15:50:41 -0400 | [diff] [blame] | 191 | |
| 192 | return nativemap; |
| 193 | } |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | /** |
| 197 | * Class used for the client Binder. Because we know this service always |
| 198 | * runs in the same process as its clients, we don't need to deal with IPC. |
| 199 | */ |
| 200 | public class LocalBinder extends Binder { |
| 201 | public SipService getService() { |
| 202 | // Return this instance of LocalService so clients can call public methods |
| 203 | return SipService.this; |
| 204 | } |
| 205 | } |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 206 | |
| 207 | /* called once by startService() */ |
| 208 | @Override |
| 209 | public void onCreate() { |
| 210 | Log.i(TAG, "onCreated"); |
| 211 | super.onCreate(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 212 | sflphoneApp = (SFLphoneApplication) getApplication(); |
| 213 | sipServiceThread = new SipServiceThread(); |
| 214 | getExecutor().execute(new StartRunnable()); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* called for each startService() */ |
| 218 | @Override |
| 219 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 220 | Log.i(TAG, "onStarted"); |
| 221 | super.onStartCommand(intent, flags, startId); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 222 | |
| 223 | runFlag = true; |
| 224 | sipServiceThread.start(); |
| 225 | sflphoneApp.setServiceRunning(true); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 226 | Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 227 | |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 228 | Log.i(TAG, "onStarted"); |
| 229 | return START_STICKY; /* started and stopped explicitly */ |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void onDestroy() { |
| 234 | /* called once by stopService() */ |
| 235 | super.onDestroy(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 236 | runFlag = false; |
| 237 | sipServiceThread.interrupt(); |
| 238 | sipServiceThread = null; |
| 239 | sflphoneApp.setServiceRunning(false); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 240 | Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show(); |
| 241 | |
| 242 | Log.i(TAG, "onDestroyed"); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public IBinder onBind(Intent arg0) { |
| 247 | Log.i(TAG, "onBound"); |
| 248 | return mBinder; |
| 249 | } |
| 250 | |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 251 | private static Looper createLooper() { |
| 252 | if(executorThread == null) { |
| 253 | Log.d(TAG, "Creating new handler thread"); |
| 254 | // ADT gives a fake warning due to bad parse rule. |
| 255 | executorThread = new HandlerThread("SipService.Executor"); |
| 256 | executorThread.start(); |
| 257 | } |
| 258 | return executorThread.getLooper(); |
| 259 | } |
| 260 | |
| 261 | public SipServiceExecutor getExecutor() { |
| 262 | // create mExecutor lazily |
| 263 | if (mExecutor == null) { |
| 264 | mExecutor = new SipServiceExecutor(this); |
| 265 | } |
| 266 | return mExecutor; |
| 267 | } |
| 268 | |
| 269 | // Executes immediate tasks in a single executorThread. |
| 270 | public static class SipServiceExecutor extends Handler { |
| 271 | WeakReference<SipService> handlerService; |
| 272 | |
| 273 | SipServiceExecutor(SipService s) { |
| 274 | super(createLooper()); |
| 275 | handlerService = new WeakReference<SipService>(s); |
| 276 | } |
| 277 | |
| 278 | public void execute(Runnable task) { |
| 279 | // TODO: add wakelock |
| 280 | Message.obtain(this, 0/* don't care */, task).sendToTarget(); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void handleMessage(Message msg) { |
| 285 | if (msg.obj instanceof Runnable) { |
| 286 | executeInternal((Runnable) msg.obj); |
| 287 | } else { |
| 288 | Log.w(TAG, "can't handle msg: " + msg); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private void executeInternal(Runnable task) { |
| 293 | try { |
| 294 | task.run(); |
| 295 | } catch (Throwable t) { |
| 296 | Log.e(TAG, "run task: " + task, t); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private void startPjSipStack() throws SameThreadException { |
| 302 | if (isPjSipStackStarted) |
| 303 | return; |
| 304 | |
| 305 | try { |
| 306 | System.loadLibrary("gnustl_shared"); |
| 307 | System.loadLibrary("expat"); |
| 308 | System.loadLibrary("yaml"); |
| 309 | System.loadLibrary("ccgnu2"); |
| 310 | System.loadLibrary("crypto"); |
| 311 | System.loadLibrary("ssl"); |
| 312 | System.loadLibrary("ccrtp1"); |
| 313 | System.loadLibrary("dbus"); |
| 314 | System.loadLibrary("dbus-c++-1"); |
| 315 | System.loadLibrary("samplerate"); |
| 316 | System.loadLibrary("codec_ulaw"); |
| 317 | System.loadLibrary("codec_alaw"); |
| 318 | System.loadLibrary("speexresampler"); |
| 319 | System.loadLibrary("sflphone"); |
| 320 | isPjSipStackStarted = true; |
| 321 | } catch (UnsatisfiedLinkError e) { |
| 322 | Log.e(TAG, "Problem with the current Pj stack...", e); |
| 323 | isPjSipStackStarted = false; |
| 324 | return; |
| 325 | } catch (Exception e) { |
| 326 | Log.e(TAG, "Problem with the current Pj stack...", e); |
| 327 | } |
| 328 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 329 | /* get unique instance of managerImpl */ |
| 330 | managerImpl = SFLPhoneservice.instance(); |
| 331 | Log.i(TAG, "ManagerImpl::instance() = " + managerImpl); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 332 | /* set static AppPath before calling manager.init */ |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 333 | managerImpl.setPath(sflphoneApp.getAppPath()); |
| 334 | callManagerJNI = new CallManagerJNI(); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 335 | Log.i(TAG, "startPjSipStack() callManagerJNI = " + callManagerJNI); |
| 336 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 337 | callManagerCallBack = new CallManagerCallBack(); |
| 338 | SFLPhoneservice.setCallbackObject(callManagerCallBack); |
| 339 | Log.i(TAG, "callManagerCallBack = " + callManagerCallBack); |
| 340 | |
Alexandre Savard | c1b08fe | 2012-09-25 16:24:47 -0400 | [diff] [blame] | 341 | configurationManagerJNI = new ConfigurationManagerJNI(); |
| 342 | |
Emeric Vigier | 0007dee | 2012-09-24 11:35:58 -0400 | [diff] [blame] | 343 | managerImpl.init(""); |
Emeric Vigier | 6119d78 | 2012-09-21 18:04:14 -0400 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
| 347 | // Enforce same thread contract to ensure we do not call from somewhere else |
| 348 | public class SameThreadException extends Exception { |
| 349 | private static final long serialVersionUID = -905639124232613768L; |
| 350 | |
| 351 | public SameThreadException() { |
| 352 | super("Should be launched from a single worker thread"); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | public abstract static class SipRunnable implements Runnable { |
| 357 | protected abstract void doRun() throws SameThreadException; |
| 358 | |
| 359 | public void run() { |
| 360 | try { |
| 361 | doRun(); |
| 362 | }catch(SameThreadException e) { |
| 363 | Log.e(TAG, "Not done from same thread"); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | class StartRunnable extends SipRunnable { |
| 369 | @Override |
| 370 | protected void doRun() throws SameThreadException { |
| 371 | startPjSipStack(); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | private class SipServiceThread extends Thread { |
| 376 | |
| 377 | public SipServiceThread() { |
| 378 | super("sipServiceThread"); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public void run() { |
Emeric Vigier | 12d61d8 | 2012-09-19 15:08:18 -0400 | [diff] [blame] | 383 | Log.i(TAG, "SipService thread running..."); |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 384 | SipService sipService = SipService.this; |
| 385 | while(sipService.runFlag) { |
| 386 | try { |
Emeric Vigier | eaf2c49 | 2012-09-19 14:38:20 -0400 | [diff] [blame] | 387 | Thread.sleep(DELAY); |
| 388 | } catch (InterruptedException e) { |
| 389 | sipService.runFlag = false; |
| 390 | Log.w(TAG, "service thread interrupted!"); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |