blob: c121846636667f347dc5ccd28540f1362d54f537 [file] [log] [blame]
Emeric Vigier6119d782012-09-21 18:04:14 -04001/**
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 Vigiereaf2c492012-09-19 14:38:20 -040024package com.savoirfairelinux.sflphone.service;
25
Emeric Vigier6119d782012-09-21 18:04:14 -040026import java.lang.ref.WeakReference;
27
Emeric Vigiereaf2c492012-09-19 14:38:20 -040028import android.app.Service;
29import android.content.Intent;
30import android.os.Binder;
Emeric Vigier6119d782012-09-21 18:04:14 -040031import android.os.Handler;
32import android.os.HandlerThread;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040033import android.os.IBinder;
Emeric Vigier6119d782012-09-21 18:04:14 -040034import android.os.Looper;
35import android.os.Message;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040036import android.util.Log;
37import android.widget.Toast;
38
Emeric Vigier0007dee2012-09-24 11:35:58 -040039import com.savoirfairelinux.sflphone.service.ManagerImpl;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040040import com.savoirfairelinux.sflphone.client.SFLphoneApplication;
Emeric Vigier6119d782012-09-21 18:04:14 -040041import com.savoirfairelinux.sflphone.service.ISipService;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040042
Alexandre Savard8b7d4332012-09-30 20:02:11 -040043import java.util.Map;
Alexandre Savard713a34d2012-09-26 15:50:41 -040044import java.util.HashMap;
Alexandre Savard6b85e7e2012-09-27 15:43:14 -040045import java.util.ArrayList;
Alexandre Savard713a34d2012-09-26 15:50:41 -040046
Emeric Vigiereaf2c492012-09-19 14:38:20 -040047public class SipService extends Service {
48
49 static final String TAG = "SipService";
50 static final int DELAY = 5000; /* 5 sec */
51 private boolean runFlag = false;
52 private SipServiceThread sipServiceThread;
Emeric Vigier84e05da2012-09-20 14:53:05 -040053 private SFLphoneApplication sflphoneApp;
Emeric Vigier6119d782012-09-21 18:04:14 -040054 private SipServiceExecutor mExecutor;
55 private static HandlerThread executorThread;
56 private CallManagerJNI callManagerJNI;
Emeric Vigier0007dee2012-09-24 11:35:58 -040057 private CallManagerCallBack callManagerCallBack;
Alexandre Savardc1b08fe2012-09-25 16:24:47 -040058 private ConfigurationManagerJNI configurationManagerJNI;
Emeric Vigier0007dee2012-09-24 11:35:58 -040059 private ManagerImpl managerImpl;
Emeric Vigier6119d782012-09-21 18:04:14 -040060 private boolean isPjSipStackStarted = false;
61
62 /* Implement public interface for the service */
63 private final ISipService.Stub mBinder = new ISipService.Stub() {
64
65 @Override
66 public void placeCall(final String accountID, final String callID, final String to) {
67 getExecutor().execute(new SipRunnable() {
68 @Override
69 protected void doRun() throws SameThreadException {
70 Log.i(TAG, "SipService.placeCall() thread running...");
71 callManagerJNI.placeCall(accountID, callID, to);
72 }
73 });
74 }
75
76 @Override
77 public void refuse(final String callID) {
78 getExecutor().execute(new SipRunnable() {
79 @Override
80 protected void doRun() throws SameThreadException {
81 Log.i(TAG, "SipService.refuse() thread running...");
82 callManagerJNI.refuse(callID);
83 }
84 });
85 }
86
87 @Override
88 public void accept(final String callID) {
89 getExecutor().execute(new SipRunnable() {
90 @Override
91 protected void doRun() throws SameThreadException {
92 Log.i(TAG, "SipService.placeCall() thread running...");
93 callManagerJNI.accept(callID);
94 }
95 });
96 }
97
98 @Override
99 public void hangUp(final String callID) {
100 getExecutor().execute(new SipRunnable() {
101 @Override
102 protected void doRun() throws SameThreadException {
103 Log.i(TAG, "SipService.hangUp() thread running...");
104 callManagerJNI.hangUp(callID);
105 }
106 });
107 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400108
109 @Override
110 public void setAudioPlugin(final String audioPlugin) {
111 getExecutor().execute(new SipRunnable() {
112 @Override
113 protected void doRun() throws SameThreadException {
114 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
115 configurationManagerJNI.setAudioPlugin(audioPlugin);
116 }
117 });
118 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400119
120 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400121 public ArrayList<String> getAccountList() {
122 StringVect swigvect = configurationManagerJNI.getAccountList();
123 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400124
125 for(int i = 0; i < swigvect.size(); i++)
126 nativelist.add(swigvect.get(i));
127
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400128 return nativelist;
129 }
130
131 @Override
Alexandre Savard713a34d2012-09-26 15:50:41 -0400132 public HashMap<String,String> getAccountDetails(final String accountID) {
133 StringMap swigmap = configurationManagerJNI.getAccountDetails(accountID);
134
135 HashMap<String, String> nativemap = new HashMap<String, String>();
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400136
137 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ALIAS, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ALIAS));
138 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME));
139 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_USERNAME, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_USERNAME));
140 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_ROUTESET, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_ROUTESET));
141 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE));
142 nativemap.put(ServiceConstants.CONFIG_LOCAL_INTERFACE, swigmap.get(ServiceConstants.CONFIG_LOCAL_INTERFACE));
143 nativemap.put(ServiceConstants.CONFIG_STUN_SERVER, swigmap.get(ServiceConstants.CONFIG_STUN_SERVER));
144 nativemap.put(ServiceConstants.CONFIG_TLS_ENABLE, swigmap.get(ServiceConstants.CONFIG_TLS_ENABLE));
145 nativemap.put(ServiceConstants.CONFIG_SRTP_ENABLE, swigmap.get(ServiceConstants.CONFIG_SRTP_ENABLE));
146
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400147 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));
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400155
156 /*
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400157 nativemap.put(ServiceConstants.CONFIG_CREDENTIAL_NUMBER, swigmap.get(ServiceConstants.CONFIG_CREDENTIAL_NUMBER));
158 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_DTMF_TYPE, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_DTMF_TYPE));
159 nativemap.put(ServiceConstants.CONFIG_RINGTONE_PATH, swigmap.get(ServiceConstants.CONFIG_RINGTONE_PATH));
160 nativemap.put(ServiceConstants.CONFIG_RINGTONE_ENABLED, swigmap.get(ServiceConstants.CONFIG_RINGTONE_ENABLED));
161 nativemap.put(ServiceConstants.CONFIG_KEEP_ALIVE_ENABLED, swigmap.get(ServiceConstants.CONFIG_KEEP_ALIVE_ENABLED));
162 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_PASSWORD, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_PASSWORD));
163 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_REALM, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_REALM));
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400164 */
165
166 /*
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400167 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_DEFAULT_REALM, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_DEFAULT_REALM));
168 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_USERAGENT, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_USERAGENT));
169 nativemap.put(ServiceConstants.CONFIG_ACCOUNT_AUTOANSWER, swigmap.get(ServiceConstants.CONFIG_ACCOUNT_AUTOANSWER));
170 nativemap.put(ServiceConstants.CONFIG_INTERFACE, swigmap.get(ServiceConstants.CONFIG_INTERFACE));
171 nativemap.put(ServiceConstants.CONFIG_PUBLISHED_SAMEAS_LOCAL, swigmap.get(ServiceConstants.CONFIG_PUBLISHED_SAMEAS_LOCAL));
172 nativemap.put(ServiceConstants.CONFIG_DEFAULT_INTERFACE, swigmap.get(ServiceConstants.CONFIG_DEFAULT_INTERFACE));
173 nativemap.put(ServiceConstants.CONFIG_DISPLAY_NAME, swigmap.get(ServiceConstants.CONFIG_DISPLAY_NAME));
174 nativemap.put(ServiceConstants.CONFIG_DEFAULT_ADDRESS, swigmap.get(ServiceConstants.CONFIG_DEFAULT_ADDRESS));
175 nativemap.put(ServiceConstants.CONFIG_SRTP_KEY_EXCHANGE, swigmap.get(ServiceConstants.CONFIG_SRTP_KEY_EXCHANGE));
176 nativemap.put(ServiceConstants.CONFIG_SRTP_ENCRYPTION_ALGO, swigmap.get(ServiceConstants.CONFIG_SRTP_ENCRYPTION_ALGO));
177 nativemap.put(ServiceConstants.CONFIG_SRTP_RTP_FALLBACK, swigmap.get(ServiceConstants.CONFIG_SRTP_RTP_FALLBACK));
178 nativemap.put(ServiceConstants.CONFIG_ZRTP_HELLO_HASH, swigmap.get(ServiceConstants.CONFIG_ZRTP_HELLO_HASH));
179 nativemap.put(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS, swigmap.get(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS));
180 nativemap.put(ServiceConstants.CONFIG_ZRTP_NOT_SUPP_WARNING, swigmap.get(ServiceConstants.CONFIG_ZRTP_NOT_SUPP_WARNING));
181 nativemap.put(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS_ONCE, swigmap.get(ServiceConstants.CONFIG_ZRTP_DISPLAY_SAS_ONCE));
182 nativemap.put(ServiceConstants.CONFIG_TLS_LISTENER_PORT, swigmap.get(ServiceConstants.CONFIG_TLS_LISTENER_PORT));
183 nativemap.put(ServiceConstants.CONFIG_TLS_CA_LIST_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_CA_LIST_FILE));
184 nativemap.put(ServiceConstants.CONFIG_TLS_CERTIFICATE_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_CERTIFICATE_FILE));
185 nativemap.put(ServiceConstants.CONFIG_TLS_PRIVATE_KEY_FILE, swigmap.get(ServiceConstants.CONFIG_TLS_PRIVATE_KEY_FILE));
186 nativemap.put(ServiceConstants.CONFIG_TLS_PASSWORD, swigmap.get(ServiceConstants.CONFIG_TLS_PASSWORD));
187 nativemap.put(ServiceConstants.CONFIG_TLS_METHOD, swigmap.get(ServiceConstants.CONFIG_TLS_METHOD));
188 nativemap.put(ServiceConstants.CONFIG_TLS_CIPHERS, swigmap.get(ServiceConstants.CONFIG_TLS_CIPHERS));
189 nativemap.put(ServiceConstants.CONFIG_TLS_SERVER_NAME, swigmap.get(ServiceConstants.CONFIG_TLS_SERVER_NAME));
190 nativemap.put(ServiceConstants.CONFIG_TLS_VERIFY_SERVER, swigmap.get(ServiceConstants.CONFIG_TLS_VERIFY_SERVER));
191 nativemap.put(ServiceConstants.CONFIG_TLS_VERIFY_CLIENT, swigmap.get(ServiceConstants.CONFIG_TLS_VERIFY_CLIENT));
192 nativemap.put(ServiceConstants.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, swigmap.get(ServiceConstants.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE));
193 nativemap.put(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC, swigmap.get(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_SEC));
194 nativemap.put(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC, swigmap.get(ServiceConstants.CONFIG_TLS_NEGOTIATION_TIMEOUT_MSEC));
195 */
Alexandre Savard713a34d2012-09-26 15:50:41 -0400196
197 return nativemap;
198 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400199
200 @Override
201 public void setAccountDetails(String accountId, Map map) {
202 HashMap<String,String> nativemap = (HashMap<String,String>) map;
203
204 StringMap swigmap = new StringMap();
205
206 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_ALIAS, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_ALIAS));
207 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_HOSTNAME));
208 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_USERNAME, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_USERNAME));
209 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_ROUTESET, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_ROUTESET));
210 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE));
211 swigmap.set(ServiceConstants.CONFIG_LOCAL_INTERFACE, nativemap.get(ServiceConstants.CONFIG_LOCAL_INTERFACE));
212 swigmap.set(ServiceConstants.CONFIG_STUN_SERVER, nativemap.get(ServiceConstants.CONFIG_STUN_SERVER));
213 swigmap.set(ServiceConstants.CONFIG_TLS_ENABLE, nativemap.get(ServiceConstants.CONFIG_TLS_ENABLE));
214 swigmap.set(ServiceConstants.CONFIG_SRTP_ENABLE, nativemap.get(ServiceConstants.CONFIG_SRTP_ENABLE));
215
216 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_TYPE, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_TYPE));
217 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_ALIAS, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_ALIAS));
218 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_MAILBOX, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_MAILBOX));
219 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_ENABLE, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_ENABLE));
220 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_EXPIRE));
221 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATUS, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATUS));
222 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE));
223 swigmap.set(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC, nativemap.get(ServiceConstants.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC));
224
225 configurationManagerJNI.setAccountDetails(accountId, swigmap);
226 }
227
Emeric Vigier6119d782012-09-21 18:04:14 -0400228 };
229
230 /**
231 * Class used for the client Binder. Because we know this service always
232 * runs in the same process as its clients, we don't need to deal with IPC.
233 */
234 public class LocalBinder extends Binder {
235 public SipService getService() {
236 // Return this instance of LocalService so clients can call public methods
237 return SipService.this;
238 }
239 }
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400240
241 /* called once by startService() */
242 @Override
243 public void onCreate() {
244 Log.i(TAG, "onCreated");
245 super.onCreate();
Emeric Vigier6119d782012-09-21 18:04:14 -0400246 sflphoneApp = (SFLphoneApplication) getApplication();
247 sipServiceThread = new SipServiceThread();
248 getExecutor().execute(new StartRunnable());
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400249 }
250
251 /* called for each startService() */
252 @Override
253 public int onStartCommand(Intent intent, int flags, int startId) {
254 Log.i(TAG, "onStarted");
255 super.onStartCommand(intent, flags, startId);
Emeric Vigier6119d782012-09-21 18:04:14 -0400256
257 runFlag = true;
258 sipServiceThread.start();
259 sflphoneApp.setServiceRunning(true);
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400260 Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
Emeric Vigier6119d782012-09-21 18:04:14 -0400261
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400262 Log.i(TAG, "onStarted");
263 return START_STICKY; /* started and stopped explicitly */
264 }
265
266 @Override
267 public void onDestroy() {
268 /* called once by stopService() */
269 super.onDestroy();
Emeric Vigier6119d782012-09-21 18:04:14 -0400270 runFlag = false;
271 sipServiceThread.interrupt();
272 sipServiceThread = null;
273 sflphoneApp.setServiceRunning(false);
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400274 Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
275
276 Log.i(TAG, "onDestroyed");
277 }
278
279 @Override
280 public IBinder onBind(Intent arg0) {
281 Log.i(TAG, "onBound");
282 return mBinder;
283 }
284
Emeric Vigier6119d782012-09-21 18:04:14 -0400285 private static Looper createLooper() {
286 if(executorThread == null) {
287 Log.d(TAG, "Creating new handler thread");
288 // ADT gives a fake warning due to bad parse rule.
289 executorThread = new HandlerThread("SipService.Executor");
290 executorThread.start();
291 }
292 return executorThread.getLooper();
293 }
294
295 public SipServiceExecutor getExecutor() {
296 // create mExecutor lazily
297 if (mExecutor == null) {
298 mExecutor = new SipServiceExecutor(this);
299 }
300 return mExecutor;
301 }
302
303 // Executes immediate tasks in a single executorThread.
304 public static class SipServiceExecutor extends Handler {
305 WeakReference<SipService> handlerService;
306
307 SipServiceExecutor(SipService s) {
308 super(createLooper());
309 handlerService = new WeakReference<SipService>(s);
310 }
311
312 public void execute(Runnable task) {
313 // TODO: add wakelock
314 Message.obtain(this, 0/* don't care */, task).sendToTarget();
315 }
316
317 @Override
318 public void handleMessage(Message msg) {
319 if (msg.obj instanceof Runnable) {
320 executeInternal((Runnable) msg.obj);
321 } else {
322 Log.w(TAG, "can't handle msg: " + msg);
323 }
324 }
325
326 private void executeInternal(Runnable task) {
327 try {
328 task.run();
329 } catch (Throwable t) {
330 Log.e(TAG, "run task: " + task, t);
331 }
332 }
333 }
334
335 private void startPjSipStack() throws SameThreadException {
336 if (isPjSipStackStarted)
337 return;
338
339 try {
340 System.loadLibrary("gnustl_shared");
341 System.loadLibrary("expat");
342 System.loadLibrary("yaml");
343 System.loadLibrary("ccgnu2");
344 System.loadLibrary("crypto");
345 System.loadLibrary("ssl");
346 System.loadLibrary("ccrtp1");
347 System.loadLibrary("dbus");
348 System.loadLibrary("dbus-c++-1");
349 System.loadLibrary("samplerate");
350 System.loadLibrary("codec_ulaw");
351 System.loadLibrary("codec_alaw");
352 System.loadLibrary("speexresampler");
353 System.loadLibrary("sflphone");
354 isPjSipStackStarted = true;
355 } catch (UnsatisfiedLinkError e) {
356 Log.e(TAG, "Problem with the current Pj stack...", e);
357 isPjSipStackStarted = false;
358 return;
359 } catch (Exception e) {
360 Log.e(TAG, "Problem with the current Pj stack...", e);
361 }
362
Emeric Vigier0007dee2012-09-24 11:35:58 -0400363 /* get unique instance of managerImpl */
364 managerImpl = SFLPhoneservice.instance();
365 Log.i(TAG, "ManagerImpl::instance() = " + managerImpl);
Emeric Vigier6119d782012-09-21 18:04:14 -0400366 /* set static AppPath before calling manager.init */
Emeric Vigier0007dee2012-09-24 11:35:58 -0400367 managerImpl.setPath(sflphoneApp.getAppPath());
368 callManagerJNI = new CallManagerJNI();
Emeric Vigier6119d782012-09-21 18:04:14 -0400369 Log.i(TAG, "startPjSipStack() callManagerJNI = " + callManagerJNI);
370
Emeric Vigier0007dee2012-09-24 11:35:58 -0400371 callManagerCallBack = new CallManagerCallBack();
372 SFLPhoneservice.setCallbackObject(callManagerCallBack);
373 Log.i(TAG, "callManagerCallBack = " + callManagerCallBack);
374
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400375 configurationManagerJNI = new ConfigurationManagerJNI();
376
Emeric Vigier0007dee2012-09-24 11:35:58 -0400377 managerImpl.init("");
Emeric Vigier6119d782012-09-21 18:04:14 -0400378 return;
379 }
380
381 // Enforce same thread contract to ensure we do not call from somewhere else
382 public class SameThreadException extends Exception {
383 private static final long serialVersionUID = -905639124232613768L;
384
385 public SameThreadException() {
386 super("Should be launched from a single worker thread");
387 }
388 }
389
390 public abstract static class SipRunnable implements Runnable {
391 protected abstract void doRun() throws SameThreadException;
392
393 public void run() {
394 try {
395 doRun();
396 }catch(SameThreadException e) {
397 Log.e(TAG, "Not done from same thread");
398 }
399 }
400 }
401
402 class StartRunnable extends SipRunnable {
403 @Override
404 protected void doRun() throws SameThreadException {
405 startPjSipStack();
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400406 }
407 }
408
409 private class SipServiceThread extends Thread {
410
411 public SipServiceThread() {
412 super("sipServiceThread");
413 }
414
415 @Override
416 public void run() {
Emeric Vigier12d61d82012-09-19 15:08:18 -0400417 Log.i(TAG, "SipService thread running...");
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400418 SipService sipService = SipService.this;
419 while(sipService.runFlag) {
420 try {
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400421 Thread.sleep(DELAY);
422 } catch (InterruptedException e) {
423 sipService.runFlag = false;
424 Log.w(TAG, "service thread interrupted!");
425 }
426 }
427 }
428 }
429}