VvsipTask.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. vvsip is a SIP app for android.
  3. vvsip is a SIP library for softphone (SIP -rfc3261-)
  4. Copyright (C) 2003-2010 Bluegoby - <bluegoby@163.com>
  5. */
  6. package com.vvsip.ansip;
  7. import android.os.*;
  8. import android.util.*;
  9. /*
  10. * JNI接口类,单例模式
  11. */
  12. public class VvsipTask {
  13. private static VvsipTask mVvsipTask;
  14. public static int global_failure=0;
  15. public static VvsipTask getVvsipTask() {
  16. if (mVvsipTask!=null)
  17. return mVvsipTask;
  18. return new VvsipTask();
  19. }
  20. public VvsipTask() {
  21. mVvsipTask = this;
  22. }
  23. /* REGISTER related events */
  24. public static final int EXOSIP_REGISTRATION_SUCCESS=0; /**< user is successfully registred. */
  25. public static final int EXOSIP_REGISTRATION_FAILURE=1; /**< user is not registred. */
  26. /* INVITE related events within calls */
  27. public static final int EXOSIP_CALL_INVITE=2; /**< announce a new call */
  28. public static final int EXOSIP_CALL_REINVITE=3; /**< announce a new INVITE within call */
  29. public static final int EXOSIP_CALL_NOANSWER=4; /**< announce no answer within the timeout */
  30. public static final int EXOSIP_CALL_PROCEEDING=5; /**< announce processing by a remote app */
  31. public static final int EXOSIP_CALL_RINGING=6; /**< announce ringback */
  32. public static final int EXOSIP_CALL_ANSWERED=7; /**< announce start of call */
  33. public static final int EXOSIP_CALL_REDIRECTED=8; /**< announce a redirection */
  34. public static final int EXOSIP_CALL_REQUESTFAILURE=9; /**< announce a request failure */
  35. public static final int EXOSIP_CALL_SERVERFAILURE=10; /**< announce a server failure */
  36. public static final int EXOSIP_CALL_GLOBALFAILURE=11; /**< announce a global failure */
  37. public static final int EXOSIP_CALL_ACK=12; /**< ACK received for 200ok to INVITE */
  38. public static final int EXOSIP_CALL_CANCELLED=13; /**< announce that call has been cancelled */
  39. /* request related events within calls (except INVITE) */
  40. public static final int EXOSIP_CALL_MESSAGE_NEW=14; /**< announce new incoming request. */
  41. public static final int EXOSIP_CALL_MESSAGE_PROCEEDING=15; /**< announce a 1xx for request. */
  42. public static final int EXOSIP_CALL_MESSAGE_ANSWERED=16; /**< announce a 200ok */
  43. public static final int EXOSIP_CALL_MESSAGE_REDIRECTED=17; /**< announce a failure. */
  44. public static final int EXOSIP_CALL_MESSAGE_REQUESTFAILURE=18; /**< announce a failure. */
  45. public static final int EXOSIP_CALL_MESSAGE_SERVERFAILURE=19; /**< announce a failure. */
  46. public static final int EXOSIP_CALL_MESSAGE_GLOBALFAILURE=20; /**< announce a failure. */
  47. public static final int EXOSIP_CALL_CLOSED=21; /**< a BYE was received for this call */
  48. /* for both UAS & UAC events */
  49. public static final int EXOSIP_CALL_RELEASED=22; /**< call context is cleared. */
  50. /* response received for request outside calls */
  51. public static final int EXOSIP_MESSAGE_NEW=23; /**< announce new incoming request. */
  52. public static final int EXOSIP_MESSAGE_PROCEEDING=24; /**< announce a 1xx for request. */
  53. public static final int EXOSIP_MESSAGE_ANSWERED=25; /**< announce a 200ok */
  54. public static final int EXOSIP_MESSAGE_REDIRECTED=26; /**< announce a failure. */
  55. public static final int EXOSIP_MESSAGE_REQUESTFAILURE=27; /**< announce a failure. */
  56. public static final int EXOSIP_MESSAGE_SERVERFAILURE=28; /**< announce a failure. */
  57. public static final int EXOSIP_MESSAGE_GLOBALFAILURE=29; /**< announce a failure. */
  58. /* Presence and Instant Messaging */
  59. public static final int EXOSIP_SUBSCRIPTION_NOANSWER=30; /**< announce no answer */
  60. public static final int EXOSIP_SUBSCRIPTION_PROCEEDING=31; /**< announce a 1xx */
  61. public static final int EXOSIP_SUBSCRIPTION_ANSWERED=32; /**< announce a 200ok */
  62. public static final int EXOSIP_SUBSCRIPTION_REDIRECTED=33; /**< announce a redirection */
  63. public static final int EXOSIP_SUBSCRIPTION_REQUESTFAILURE=34; /**< announce a request failure */
  64. public static final int EXOSIP_SUBSCRIPTION_SERVERFAILURE=35; /**< announce a server failure */
  65. public static final int EXOSIP_SUBSCRIPTION_GLOBALFAILURE=36; /**< announce a global failure */
  66. public static final int EXOSIP_SUBSCRIPTION_NOTIFY=37; /**< announce new NOTIFY request */
  67. public static final int EXOSIP_IN_SUBSCRIPTION_NEW=38; /**< announce new incoming SUBSCRIBE.*/
  68. public static final int EXOSIP_NOTIFICATION_NOANSWER=39; /**< announce no answer */
  69. public static final int EXOSIP_NOTIFICATION_PROCEEDING=40; /**< announce a 1xx */
  70. public static final int EXOSIP_NOTIFICATION_ANSWERED=41; /**< announce a 200ok */
  71. public static final int EXOSIP_NOTIFICATION_REDIRECTED=42; /**< announce a redirection */
  72. public static final int EXOSIP_NOTIFICATION_REQUESTFAILURE=43; /**< announce a request failure */
  73. public static final int EXOSIP_NOTIFICATION_SERVERFAILURE=44; /**< announce a server failure */
  74. public static final int EXOSIP_NOTIFICATION_GLOBALFAILURE=45; /**< announce a global failure */
  75. public boolean thread_started=false;
  76. private static Handler mainActivityEventHandler;
  77. public boolean running=false;
  78. private Thread taskThread;
  79. public native int vvinit(int debug_level);
  80. public native int vvreset(int debug_level);
  81. public native int vvquit();
  82. public native int vvcodecinfomodify(int pos, int enable, String codec_name, int mode, int cng, int vbr);
  83. public native int vvvideocodecinfomodify(int pos, int enable, String codec_name, int mode, int cng, int vbr);
  84. public native int vvvideocodecattrmodify(int uploadrate, int downloadrate);
  85. /* In 3G mode, if audio bitrate is <=30, the SDP will contain only the lower bitrate codec in SDP */
  86. public native int vvcodecattrmodify(int ptime, int audio_bitrate);
  87. public native String vvoptiongetversion();
  88. public native int vvoptionsetuseragent(String useragent);
  89. public native int vvoptionsetinitialaudioport(int initialport);
  90. public native int vvoptionenablestunserver(String stunserver, int usestunserver);
  91. public native int vvoptionenableturnserver(String turnserver, int useturnserver);
  92. public native int vvoptionsetipv4forgateway(String ipv4forgateway);
  93. public native int vvoptionenablerport(int enable);
  94. public native int vvoptionsetdnscapabilities(int dnscapabilities);
  95. public native int vvoptionenablekeepalive(int interval);
  96. public native int vvoptionsetaudioprofile(String profile);
  97. public native int vvoptionsetvideoprofile(String profile);
  98. public native int vvoptionsettextprofile(String profile);
  99. public native int vvoptionsetudpftpprofile(String profile);
  100. public native int vvoptionenablesessiontimers(int sessionexpires);
  101. public native int vvoptionenablesymmetricrtp(int enable);
  102. //public native int vvoptionfindoutsoundcard(struct vvsndcard *sndcard);
  103. //public native int vvoptionfindinsoundcard(struct vvsndcard *sndcard);
  104. public native int vvoptionselectinsoundcard(int card);
  105. public native int vvoptionselectoutsoundcard(int card);
  106. //public native int vvoptionselectincustomsoundcard(MSSndCard *captcard);
  107. //public native int vvoptionselectoutcustomsoundcard(MSSndCard *playcard);
  108. public native int vvoptionsetvolumeoutsoundcard(int card, int mixer, int percent);
  109. public native int vvoptiongetvolumeoutsoundcard(int card, int mixer);
  110. public native int vvoptionsetvolumeinsoundcard(int card, int percent);
  111. public native int vvoptiongetvolumeinsoundcard(int card);
  112. public native int vvoptionsetmuteoutsoundcard(int card, int mixer, int val);
  113. public native int vvoptionsetmuteinsoundcard(int card, int val);
  114. public native int vvoptionenablewebrtcapm(int enable, int aecm, int aecm_comfort_noise, int aecm_routing_mode,
  115. int ns, int ns_level,
  116. int agc, int agc_mode, int agc_target_level_dbfs, int agc_compression_gain_db,
  117. int high_pass_filter);
  118. public native int vvoptionenableechocanceller(int enable, int framesize, int taillength);
  119. public native int vvoptionenablehalfduplex(int enable, int vadprobstart, int vadprobcontinue);
  120. public native int vvoptionenableagc(int enable, int agclevel, int maxgain);
  121. public native int vvoptionsetjittermode(int mode);
  122. public native int vvoptionvideosetjittermode(int mode);
  123. public native int vvoptionsetpassword(String realm, String login, String passwd);
  124. public native int vvoptionloadplugins(String directory);
  125. public native int vvoptionenableoptionnalencryption(int optionnalencryption);
  126. public native int vvoptionsetdscpvalue(int dscpvalue);
  127. public native int vvoptionsetaudiodscp(int dscpvalue);
  128. public native int vvoptionsetvideodscp(int dscpvalue);
  129. public native int vvoptionsettextdscp(int dscpvalue);
  130. public native int vvoptionsetudpftpdscp(int dscpvalue);
  131. public native int vvoptionadddnscache(String host, String ip);
  132. public native int vvoptionsetsupportedextensions(String supportedextensions);
  133. public native int vvoptionsetacceptedtypes(String acceptedtypes);
  134. public native int vvoptionsetallowedmethods(String allowedmethods);
  135. public native int vvoptionsetrate(int rate);
  136. public native int vvoptionsetvolumegain(float capturegain, float playbackgain);
  137. public native int vvoptionsetecholimitation(int enabled,
  138. float threshold,
  139. float speed,
  140. float force,
  141. int sustain);
  142. public native long vveventget();
  143. public native long vveventwait(int tvs, int tvms);
  144. public native void vveventrelease(long evt);
  145. public native int vveventgettype(long evt);
  146. public native int vveventgettid(long evt);
  147. public native int vveventgetcid(long evt);
  148. public native int vveventgetdid(long evt);
  149. public native int vveventgetrid(long evt);
  150. public native int vveventgetnid(long evt);
  151. public native int vveventgetsid(long evt);
  152. public native String vveventgetmethod(long evt);
  153. public native String vveventgetreason(long evt);
  154. public native int vveventgetstatuscode(long evt);
  155. public native String vveventgetrequestheader(long evt, String hname, int n);
  156. public native String vveventgetresponseheader(long evt, String hname, int n);
  157. public native String vvurigetusername(String mSipUri);
  158. public native String vvurigetdomain(String mSipUri);
  159. public native String vvurigetdisplayname(String mSipUri);
  160. //For retreiving body in REQUEST (MESSAGE, INVITE, etc..)
  161. public native byte[] vveventgetrequestbody(long evt, int n);
  162. //public native int vvnetworkguessip(int family, String address, int size);
  163. public native String vvnetworkgetnatinfo(String ip, int port);
  164. public native int vvnetworkmasquerade(String ip, int port);
  165. public native int vvoptionsettlscertificate(String certificate, String priv_key, String root_ca);
  166. public native int vvnetworkstart(String transport, int port);
  167. public native int vvregisterstart(String identity, String proxy, String outboundproxy, int expires);
  168. public native int vvregisterrefresh(int rid, int expires);
  169. public native int vvregisterstop(int rid);
  170. public native int vvsessionstart(String identity, String target, String proxy, String outboundproxy,String tags);
  171. public native int vvsessionstartwithvideo(String identity, String target, String proxy, String outboundproxy,String tags);
  172. /* public native int vvsessionstart(String identity, String target, String proxy, String outboundproxy);
  173. public native int vvsessionstartwithvideo(String identity, String target, String proxy, String outboundproxy);*/
  174. public native int vvsessionaddvideo(int did);
  175. public native int vvsessionstopvideo(int did);
  176. public native int vvsessionadaptvideobitrate(int did, int percent);
  177. public native int vvmessagegetrequestaudiortpdirection(long evt);
  178. public native int vvmessagegetrequestvideortpdirection(long evt);
  179. public native int vvmessagegetresponsevideortpdirection(long evt);
  180. public native int vvsessionstop(int cid, int did, int code);
  181. public native int vvsessionanswer(int cid, int did, int code, int enable_audio);
  182. public native float vvsessiongetaudiouploadbandwidth(int did);
  183. public native float vvsessiongetaudiodownloadbandwidth(int did);
  184. public native float vvsessiongetaudiopacketloss(int did);
  185. public native float vvsessiongetaudioremotepacketloss(int did);
  186. public native float vvsessiongetvideouploadbandwidth(int did);
  187. public native float vvsessiongetvideodownloadbandwidth(int did);
  188. public native float vvsessiongetvideopacketloss(int did);
  189. public native float vvsessiongetvideoremotepacketloss(int did);
  190. public native int vvsessionanswerrequest(int cid, int did, int code);
  191. public native int vvsessionsenddtmfwithduration(int did, String dtmf, int duration);
  192. public native int vvsessionsendinbanddtmf(int did, String dtmf);
  193. public native int vvsessionsendrtpdtmf(int did, String dtmf);
  194. public native int vvexecuteuri(String identity, String target,
  195. String proxy, String outboundproxy, String body);
  196. public native int vvmessageanswer(int tid, int code);
  197. public native void registeraudio();
  198. //EXPERIMENTAL USAGE OF MEDIACODEC API FOR SDK>=16
  199. //sidenote: validation of every device is NECESSARY.
  200. //development device is galaxy nexus
  201. public native int registermsh264mediaencoder(int version_sdk);
  202. public native int registermsh264mediadecoder(int version_sdk);
  203. public native int vvoptionenablefilter(String filter_name);
  204. public native int vvoptiondisablefilter(String filter_name);
  205. public native int setvideodisplay(Object lVideoDisplay);
  206. /* load the 'vvsip' library on startup.
  207. * The library has been unpacked into
  208. * /data/data/com.vvsip.vvsip/lib/vvsip.so
  209. */
  210. static {
  211. CheckCpu ccpu;
  212. int isArm = -1;
  213. int features = 0;
  214. try {
  215. ccpu = new CheckCpu();
  216. isArm = ccpu.getcpufamily();
  217. features = ccpu.getcpufeatures();
  218. } catch (UnsatisfiedLinkError e) {
  219. Log.e("VvsipTask", "native library is missing // re-install the application...");
  220. e.printStackTrace();
  221. global_failure=1;
  222. } catch (Exception e) {
  223. Log.e("VvsipTask", "problem loading checkcpu.so?");
  224. e.printStackTrace();
  225. global_failure=1;
  226. }
  227. if (isArm==1)
  228. {
  229. Log.i("VvsipTask", "CPU feature: " + features);
  230. if ((features & 0x001) == 0x001)
  231. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_ARM_FEATURE_ARMv7");
  232. if ((features & 0x002) == 0x002)
  233. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_ARM_FEATURE_VFPv3");
  234. if ((features & 0x004) == 0x004)
  235. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_ARM_FEATURE_NEON");
  236. try {
  237. if ((features & 0x001) == 0x001)
  238. {
  239. if ((features & 0x004) == 0x004)
  240. System.loadLibrary("vvsip-v7a-neon");
  241. else
  242. System.loadLibrary("vvsip-v7a");
  243. } else {
  244. System.loadLibrary("vvsip-v5");
  245. }
  246. } catch (UnsatisfiedLinkError e) {
  247. Log.e("VvsipTask", "native library is missing // re-install the application...");
  248. e.printStackTrace();
  249. global_failure=1;
  250. } catch (Exception e) {
  251. Log.e("VvsipTask", "problem loading arm vvsip?");
  252. e.printStackTrace();
  253. global_failure=1;
  254. }
  255. } else if (isArm==2)
  256. {
  257. Log.i("VvsipTask", "CPU feature: " + features);
  258. if ((features & 0x001) == 0x001)
  259. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_X86_FEATURE_SSSE3");
  260. if ((features & 0x002) == 0x002)
  261. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_X86_FEATURE_POPCNT");
  262. if ((features & 0x004) == 0x004)
  263. Log.i("VvsipTask", "CPU feature: ANDROID_CPU_X86_FEATURE_MOVBE");
  264. try {
  265. System.loadLibrary("vvsip-x86");
  266. } catch (UnsatisfiedLinkError e) {
  267. Log.i("VvsipTask", "native library is missing // re-install the application...");
  268. e.printStackTrace();
  269. global_failure=1;
  270. } catch (Exception e) {
  271. Log.i("VvsipTask", "problem loading x86 vvsip?");
  272. e.printStackTrace();
  273. global_failure=1;
  274. }
  275. } else {
  276. try {
  277. System.loadLibrary("vvsip");
  278. } catch (UnsatisfiedLinkError e) {
  279. Log.e("VvsipTask", "native library is missing // re-install the application...");
  280. e.printStackTrace();
  281. global_failure=1;
  282. } catch (Exception e) {
  283. Log.e("VvsipTask", "problem loading arm vvsip?");
  284. e.printStackTrace();
  285. global_failure=1;
  286. }
  287. }
  288. }
  289. public void stop() {
  290. if (thread_started==false)
  291. return;
  292. running = false;
  293. if (taskThread!=null)
  294. {
  295. try {
  296. taskThread.join(1000);
  297. } catch (InterruptedException e) {
  298. e.printStackTrace();
  299. }
  300. taskThread = null;
  301. }
  302. while (thread_started==true)
  303. {
  304. }
  305. }
  306. /*
  307. * 开始Handler,获得SIP信令消息与状态
  308. */
  309. public void start(Handler _mainActivityEventHandler) {
  310. if (running==true)
  311. return;
  312. running=true;
  313. thread_started = true;
  314. mainActivityEventHandler = _mainActivityEventHandler;
  315. taskThread = new Thread() {
  316. @Override public void run() {
  317. vvsipLoop();
  318. thread_started = false;
  319. }
  320. };
  321. taskThread.start();
  322. }
  323. private void vvsipLoop() {
  324. Log.i(getClass().getSimpleName(), "background task - start");
  325. int count=0;
  326. while (running==true) {
  327. count++;
  328. if (count%(5*3)==0) /* each 3seconds */
  329. {
  330. Message m = Message.obtain(); //new Message();
  331. m.what = -1;
  332. m.arg1 = -1;
  333. m.arg2 = -1;
  334. mainActivityEventHandler.sendMessage(m);
  335. count=0;
  336. }
  337. long evt = vveventwait(0, 200);
  338. if (evt!=0)
  339. {
  340. int type = vveventgettype(evt);
  341. //Send update to the main thread
  342. Message m = Message.obtain(); //new Message();
  343. m.what = type;
  344. m.arg1 = vveventgetcid(evt);
  345. m.arg2 = vveventgetdid(evt);
  346. Long obj = Long.valueOf(evt);
  347. m.obj = obj;
  348. mainActivityEventHandler.sendMessage(m);
  349. }
  350. }
  351. Log.i(getClass().getSimpleName(), "background task - end");
  352. }
  353. }