blob: 3eb275850b9f003e9600a8c8f2920b3c3057ed31 [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;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040042import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler;
Emeric Vigiereaf2c492012-09-19 14:38:20 -040043
Alexandre Savard8b7d4332012-09-30 20:02:11 -040044import java.util.Map;
Alexandre Savard713a34d2012-09-26 15:50:41 -040045import java.util.HashMap;
Alexandre Savard6b85e7e2012-09-27 15:43:14 -040046import java.util.ArrayList;
Alexandre Savard713a34d2012-09-26 15:50:41 -040047
Emeric Vigiereaf2c492012-09-19 14:38:20 -040048public class SipService extends Service {
49
50 static final String TAG = "SipService";
51 static final int DELAY = 5000; /* 5 sec */
52 private boolean runFlag = false;
53 private SipServiceThread sipServiceThread;
Emeric Vigier84e05da2012-09-20 14:53:05 -040054 private SFLphoneApplication sflphoneApp;
Emeric Vigier6119d782012-09-21 18:04:14 -040055 private SipServiceExecutor mExecutor;
56 private static HandlerThread executorThread;
57 private CallManagerJNI callManagerJNI;
Emeric Vigier0007dee2012-09-24 11:35:58 -040058 private CallManagerCallBack callManagerCallBack;
Alexandre Savardc1b08fe2012-09-25 16:24:47 -040059 private ConfigurationManagerJNI configurationManagerJNI;
Alexandre Savardfccd1dc2012-10-17 17:31:38 -040060 private ConfigurationManagerCallback configurationManagerCallback;
Emeric Vigier0007dee2012-09-24 11:35:58 -040061 private ManagerImpl managerImpl;
Emeric Vigier6119d782012-09-21 18:04:14 -040062 private boolean isPjSipStackStarted = false;
Alexandre Savard4f11d7a2012-10-18 13:15:36 -040063
Emeric Vigier6119d782012-09-21 18:04:14 -040064
65 /* Implement public interface for the service */
66 private final ISipService.Stub mBinder = new ISipService.Stub() {
67
68 @Override
69 public void placeCall(final String accountID, final String callID, final String to) {
70 getExecutor().execute(new SipRunnable() {
71 @Override
72 protected void doRun() throws SameThreadException {
73 Log.i(TAG, "SipService.placeCall() thread running...");
74 callManagerJNI.placeCall(accountID, callID, to);
75 }
76 });
77 }
78
79 @Override
80 public void refuse(final String callID) {
81 getExecutor().execute(new SipRunnable() {
82 @Override
83 protected void doRun() throws SameThreadException {
84 Log.i(TAG, "SipService.refuse() thread running...");
85 callManagerJNI.refuse(callID);
86 }
87 });
88 }
89
90 @Override
91 public void accept(final String callID) {
92 getExecutor().execute(new SipRunnable() {
93 @Override
94 protected void doRun() throws SameThreadException {
95 Log.i(TAG, "SipService.placeCall() thread running...");
96 callManagerJNI.accept(callID);
97 }
98 });
99 }
100
101 @Override
102 public void hangUp(final String callID) {
103 getExecutor().execute(new SipRunnable() {
104 @Override
105 protected void doRun() throws SameThreadException {
106 Log.i(TAG, "SipService.hangUp() thread running...");
107 callManagerJNI.hangUp(callID);
108 }
109 });
110 }
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400111
112 @Override
113 public void setAudioPlugin(final String audioPlugin) {
114 getExecutor().execute(new SipRunnable() {
115 @Override
116 protected void doRun() throws SameThreadException {
117 Log.i(TAG, "SipService.setAudioPlugin() thread running...");
118 configurationManagerJNI.setAudioPlugin(audioPlugin);
119 }
Alexandre Savard31d27c62012-10-04 16:05:08 -0400120 });
121 }
122
123 @Override
124 public String getCurrentAudioOutputPlugin() {
125 class CurrentAudioPlugin extends SipRunnableWithReturn {
126 @Override
127 protected String doRun() throws SameThreadException {
128 Log.i(TAG, "SipService.getCurrentAudioOutputPlugin() thread running...");
129 return configurationManagerJNI.getCurrentAudioOutputPlugin();
130 }
131 };
132
133 CurrentAudioPlugin runInstance = new CurrentAudioPlugin();
134 getExecutor().execute(runInstance);
135 while(!runInstance.isDone()) {}
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400136 return (String) runInstance.getVal();
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400137 }
Alexandre Savard713a34d2012-09-26 15:50:41 -0400138
139 @Override
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400140 public ArrayList<String> getAccountList() {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400141 class AccountList extends SipRunnableWithReturn {
142 @Override
143 protected StringVect doRun() throws SameThreadException {
144 Log.i(TAG, "SipService.getAccountList() thread running...");
145 return configurationManagerJNI.getAccountList();
146 }
147 };
148 AccountList runInstance = new AccountList();
149 getExecutor().execute(runInstance);
150 while(!runInstance.isDone()) {}
151 StringVect swigvect = (StringVect) runInstance.getVal();
152
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400153 ArrayList<String> nativelist = new ArrayList<String>();
Alexandre Savard52a72522012-09-27 16:40:13 -0400154
155 for(int i = 0; i < swigvect.size(); i++)
156 nativelist.add(swigvect.get(i));
157
Alexandre Savard6b85e7e2012-09-27 15:43:14 -0400158 return nativelist;
159 }
160
161 @Override
Alexandre Savard713a34d2012-09-26 15:50:41 -0400162 public HashMap<String,String> getAccountDetails(final String accountID) {
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400163 class AccountDetails extends SipRunnableWithReturn {
164 private String id;
165 AccountDetails(String accountId) { id = accountId; }
166 @Override
167 protected StringMap doRun() throws SameThreadException {
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400168 Log.i(TAG, "SipService.getAccountDetails() thread running...");
Alexandre Savard7a902bc2012-10-04 16:32:35 -0400169 return configurationManagerJNI.getAccountDetails(id);
170 }
171 };
172
173 AccountDetails runInstance = new AccountDetails(accountID);
174 getExecutor().execute(runInstance);
175 while(!runInstance.isDone()) {}
176 StringMap swigmap = (StringMap) runInstance.getVal();
Alexandre Savard713a34d2012-09-26 15:50:41 -0400177
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400178 HashMap<String, String> nativemap = AccountDetailsHandler.convertSwigToNative(swigmap);
Alexandre Savard713a34d2012-09-26 15:50:41 -0400179
180 return nativemap;
181 }
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400182
183 @Override
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400184 public void setAccountDetails(final String accountId, Map map) {
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400185 HashMap<String,String> nativemap = (HashMap<String,String>) map;
186
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400187 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig(nativemap);
Alexandre Savard718d49f2012-10-02 15:17:13 -0400188
Alexandre Savard7a2b2202012-10-04 17:07:33 -0400189 getExecutor().execute(new SipRunnable() {
190 @Override
191 protected void doRun() throws SameThreadException {
192 Log.i(TAG, "SipService.setAccountDetails() thread running...");
193 configurationManagerJNI.setAccountDetails(accountId, swigmap);
194 }
195 });
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400196 }
197
Alexandre Savard46036572012-10-05 13:56:49 -0400198 @Override
199 public String addAccount(Map map) {
200 class AddAccount extends SipRunnableWithReturn {
201 StringMap map;
202 AddAccount(StringMap m) { map = m; }
203 @Override
204 protected String doRun() throws SameThreadException {
205 Log.i(TAG, "SipService.getAccountDetails() thread running...");
206 return configurationManagerJNI.addAccount(map);
207 }
208 };
209
210 final StringMap swigmap = AccountDetailsHandler.convertFromNativeToSwig((HashMap<String,String>)map);
211
212 AddAccount runInstance = new AddAccount(swigmap);
213 getExecutor().execute(runInstance);
214 while(!runInstance.isDone()) {}
215 String accountId = (String) runInstance.getVal();
216
217 return accountId;
218 }
219
220 @Override
221 public void removeAccount(final String accountId) {
222 getExecutor().execute(new SipRunnable() {
223 @Override
224 protected void doRun() throws SameThreadException {
225 Log.i(TAG, "SipService.setAccountDetails() thread running...");
226 configurationManagerJNI.removeAccount(accountId);
227 }
228 });
229 }
Emeric Vigier6119d782012-09-21 18:04:14 -0400230 };
231
232 /**
233 * Class used for the client Binder. Because we know this service always
234 * runs in the same process as its clients, we don't need to deal with IPC.
235 */
236 public class LocalBinder extends Binder {
237 public SipService getService() {
238 // Return this instance of LocalService so clients can call public methods
239 return SipService.this;
240 }
241 }
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400242
243 /* called once by startService() */
244 @Override
245 public void onCreate() {
246 Log.i(TAG, "onCreated");
247 super.onCreate();
Emeric Vigier6119d782012-09-21 18:04:14 -0400248 sflphoneApp = (SFLphoneApplication) getApplication();
249 sipServiceThread = new SipServiceThread();
250 getExecutor().execute(new StartRunnable());
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400251 }
252
253 /* called for each startService() */
254 @Override
255 public int onStartCommand(Intent intent, int flags, int startId) {
256 Log.i(TAG, "onStarted");
257 super.onStartCommand(intent, flags, startId);
Emeric Vigier6119d782012-09-21 18:04:14 -0400258
259 runFlag = true;
260 sipServiceThread.start();
261 sflphoneApp.setServiceRunning(true);
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400262 Toast.makeText(this, "Sflphone Service started", Toast.LENGTH_SHORT).show();
Emeric Vigier6119d782012-09-21 18:04:14 -0400263
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400264 Log.i(TAG, "onStarted");
265 return START_STICKY; /* started and stopped explicitly */
266 }
267
268 @Override
269 public void onDestroy() {
270 /* called once by stopService() */
271 super.onDestroy();
Emeric Vigier6119d782012-09-21 18:04:14 -0400272 runFlag = false;
273 sipServiceThread.interrupt();
274 sipServiceThread = null;
275 sflphoneApp.setServiceRunning(false);
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400276 Toast.makeText(this, "Sflphone Service stopped", Toast.LENGTH_SHORT).show();
277
278 Log.i(TAG, "onDestroyed");
279 }
280
281 @Override
282 public IBinder onBind(Intent arg0) {
283 Log.i(TAG, "onBound");
284 return mBinder;
285 }
286
Emeric Vigier6119d782012-09-21 18:04:14 -0400287 private static Looper createLooper() {
288 if(executorThread == null) {
289 Log.d(TAG, "Creating new handler thread");
290 // ADT gives a fake warning due to bad parse rule.
291 executorThread = new HandlerThread("SipService.Executor");
292 executorThread.start();
293 }
294 return executorThread.getLooper();
295 }
296
297 public SipServiceExecutor getExecutor() {
298 // create mExecutor lazily
299 if (mExecutor == null) {
300 mExecutor = new SipServiceExecutor(this);
301 }
302 return mExecutor;
303 }
304
305 // Executes immediate tasks in a single executorThread.
306 public static class SipServiceExecutor extends Handler {
307 WeakReference<SipService> handlerService;
308
309 SipServiceExecutor(SipService s) {
310 super(createLooper());
311 handlerService = new WeakReference<SipService>(s);
312 }
313
314 public void execute(Runnable task) {
315 // TODO: add wakelock
316 Message.obtain(this, 0/* don't care */, task).sendToTarget();
317 }
318
319 @Override
320 public void handleMessage(Message msg) {
321 if (msg.obj instanceof Runnable) {
322 executeInternal((Runnable) msg.obj);
323 } else {
324 Log.w(TAG, "can't handle msg: " + msg);
325 }
326 }
327
328 private void executeInternal(Runnable task) {
329 try {
330 task.run();
331 } catch (Throwable t) {
332 Log.e(TAG, "run task: " + task, t);
333 }
334 }
335 }
336
337 private void startPjSipStack() throws SameThreadException {
338 if (isPjSipStackStarted)
339 return;
340
341 try {
342 System.loadLibrary("gnustl_shared");
343 System.loadLibrary("expat");
344 System.loadLibrary("yaml");
345 System.loadLibrary("ccgnu2");
346 System.loadLibrary("crypto");
347 System.loadLibrary("ssl");
348 System.loadLibrary("ccrtp1");
349 System.loadLibrary("dbus");
350 System.loadLibrary("dbus-c++-1");
351 System.loadLibrary("samplerate");
352 System.loadLibrary("codec_ulaw");
353 System.loadLibrary("codec_alaw");
354 System.loadLibrary("speexresampler");
355 System.loadLibrary("sflphone");
356 isPjSipStackStarted = true;
357 } catch (UnsatisfiedLinkError e) {
358 Log.e(TAG, "Problem with the current Pj stack...", e);
359 isPjSipStackStarted = false;
360 return;
361 } catch (Exception e) {
362 Log.e(TAG, "Problem with the current Pj stack...", e);
363 }
364
Emeric Vigier0007dee2012-09-24 11:35:58 -0400365 /* get unique instance of managerImpl */
366 managerImpl = SFLPhoneservice.instance();
Alexandre Savard718d49f2012-10-02 15:17:13 -0400367
Emeric Vigier6119d782012-09-21 18:04:14 -0400368 /* set static AppPath before calling manager.init */
Emeric Vigier0007dee2012-09-24 11:35:58 -0400369 managerImpl.setPath(sflphoneApp.getAppPath());
Emeric Vigier6119d782012-09-21 18:04:14 -0400370
Alexandre Savard718d49f2012-10-02 15:17:13 -0400371 callManagerJNI = new CallManagerJNI();
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400372 callManagerCallBack = new CallManagerCallBack(this);
Emeric Vigier0007dee2012-09-24 11:35:58 -0400373 SFLPhoneservice.setCallbackObject(callManagerCallBack);
Emeric Vigier0007dee2012-09-24 11:35:58 -0400374
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400375 configurationManagerJNI = new ConfigurationManagerJNI();
Alexandre Savard4f11d7a2012-10-18 13:15:36 -0400376 configurationManagerCallback = new ConfigurationManagerCallback(this);
Alexandre Savardfccd1dc2012-10-17 17:31:38 -0400377 SFLPhoneservice.setConfigurationCallbackObject(configurationManagerCallback);
Alexandre Savardc1b08fe2012-09-25 16:24:47 -0400378
Emeric Vigier0007dee2012-09-24 11:35:58 -0400379 managerImpl.init("");
Emeric Vigier6119d782012-09-21 18:04:14 -0400380 return;
381 }
382
383 // Enforce same thread contract to ensure we do not call from somewhere else
384 public class SameThreadException extends Exception {
385 private static final long serialVersionUID = -905639124232613768L;
386
387 public SameThreadException() {
388 super("Should be launched from a single worker thread");
389 }
390 }
391
392 public abstract static class SipRunnable implements Runnable {
393 protected abstract void doRun() throws SameThreadException;
394
395 public void run() {
396 try {
397 doRun();
398 }catch(SameThreadException e) {
399 Log.e(TAG, "Not done from same thread");
400 }
401 }
402 }
403
Alexandre Savard31d27c62012-10-04 16:05:08 -0400404 public abstract static class SipRunnableWithReturn implements Runnable {
405 Object obj = null;
406 boolean done = false;
407
408 protected abstract Object doRun() throws SameThreadException;
409
410 public Object getVal() {
411 return obj;
412 }
413
414 public boolean isDone() {
415 return done;
416 }
417
418 public void run() {
419 try {
420 obj = doRun();
421 done = true;
422 }catch(SameThreadException e) {
423 Log.e(TAG, "Not done from same thread");
424 }
425 }
426 }
427
Emeric Vigier6119d782012-09-21 18:04:14 -0400428 class StartRunnable extends SipRunnable {
429 @Override
430 protected void doRun() throws SameThreadException {
431 startPjSipStack();
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400432 }
433 }
434
435 private class SipServiceThread extends Thread {
436
437 public SipServiceThread() {
438 super("sipServiceThread");
439 }
440
441 @Override
442 public void run() {
Emeric Vigier12d61d82012-09-19 15:08:18 -0400443 Log.i(TAG, "SipService thread running...");
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400444 SipService sipService = SipService.this;
445 while(sipService.runFlag) {
446 try {
Emeric Vigiereaf2c492012-09-19 14:38:20 -0400447 Thread.sleep(DELAY);
448 } catch (InterruptedException e) {
449 sipService.runFlag = false;
450 Log.w(TAG, "service thread interrupted!");
451 }
452 }
453 }
454 }
455}