pjsua2_demo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <pjsua2.hpp>
  19. #include <iostream>
  20. #include <pj/file_access.h>
  21. #define THIS_FILE "pjsua2_demo.cpp"
  22. using namespace pj;
  23. /* Valid test number:
  24. * 0: JSON account config test
  25. * 1: call test
  26. * 2: JSON endpoint config test
  27. * 3: media player and recorder test
  28. * 4: simple registration test
  29. */
  30. #define USE_TEST 1
  31. class MyEndpoint : public Endpoint
  32. {
  33. public:
  34. MyEndpoint() : Endpoint() {};
  35. virtual pj_status_t onCredAuth(OnCredAuthParam &prm)
  36. {
  37. PJ_UNUSED_ARG(prm);
  38. std::cout << "*** Callback onCredAuth called ***" << std::endl;
  39. /* Return PJ_ENOTSUP to use
  40. * pjsip_auth_create_aka_response()/<b>libmilenage</b> (default),
  41. * if PJSIP_HAS_DIGEST_AKA_AUTH is defined.
  42. */
  43. return PJ_ENOTSUP;
  44. }
  45. };
  46. class MyAccount;
  47. class MyAudioMediaPort: public AudioMediaPort
  48. {
  49. virtual void onFrameRequested(MediaFrame &frame)
  50. {
  51. // Give the input frame here
  52. frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
  53. // frame.buf.assign(frame.size, 'c');
  54. }
  55. virtual void onFrameReceived(MediaFrame &frame)
  56. {
  57. PJ_UNUSED_ARG(frame);
  58. // Process the incoming frame here
  59. }
  60. };
  61. class MyCall : public Call
  62. {
  63. private:
  64. MyAccount *myAcc;
  65. AudioMediaPlayer *wav_player;
  66. AudioMediaPort *med_port;
  67. public:
  68. MyCall(Account &acc, int call_id = PJSUA_INVALID_ID)
  69. : Call(acc, call_id)
  70. {
  71. wav_player = NULL;
  72. med_port = NULL;
  73. myAcc = (MyAccount *)&acc;
  74. }
  75. ~MyCall()
  76. {
  77. if (wav_player)
  78. delete wav_player;
  79. if (med_port)
  80. delete med_port;
  81. }
  82. virtual void onCallState(OnCallStateParam &prm);
  83. virtual void onCallTransferRequest(OnCallTransferRequestParam &prm);
  84. virtual void onCallReplaceRequest(OnCallReplaceRequestParam &prm);
  85. virtual void onCallMediaState(OnCallMediaStateParam &prm);
  86. };
  87. class MyAccount : public Account
  88. {
  89. public:
  90. std::vector<Call *> calls;
  91. public:
  92. MyAccount()
  93. {}
  94. ~MyAccount()
  95. {
  96. std::cout << "*** Account is being deleted: No of calls="
  97. << calls.size() << std::endl;
  98. for (std::vector<Call *>::iterator it = calls.begin();
  99. it != calls.end(); )
  100. {
  101. delete (*it);
  102. it = calls.erase(it);
  103. }
  104. }
  105. void removeCall(Call *call)
  106. {
  107. for (std::vector<Call *>::iterator it = calls.begin();
  108. it != calls.end(); ++it)
  109. {
  110. if (*it == call) {
  111. calls.erase(it);
  112. break;
  113. }
  114. }
  115. }
  116. virtual void onRegState(OnRegStateParam &prm)
  117. {
  118. AccountInfo ai = getInfo();
  119. std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=")
  120. << prm.code << std::endl;
  121. }
  122. virtual void onIncomingCall(OnIncomingCallParam &iprm)
  123. {
  124. Call *call = new MyCall(*this, iprm.callId);
  125. CallInfo ci = call->getInfo();
  126. CallOpParam prm;
  127. std::cout << "*** Incoming Call: " << ci.remoteUri << " ["
  128. << ci.stateText << "]" << std::endl;
  129. calls.push_back(call);
  130. prm.statusCode = (pjsip_status_code)200;
  131. call->answer(prm);
  132. }
  133. };
  134. void MyCall::onCallState(OnCallStateParam &prm)
  135. {
  136. PJ_UNUSED_ARG(prm);
  137. CallInfo ci = getInfo();
  138. std::cout << "*** Call: " << ci.remoteUri << " [" << ci.stateText
  139. << "]" << std::endl;
  140. if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
  141. //myAcc->removeCall(this);
  142. /* Delete the call */
  143. //delete this;
  144. }
  145. }
  146. void MyCall::onCallMediaState(OnCallMediaStateParam &prm)
  147. {
  148. PJ_UNUSED_ARG(prm);
  149. CallInfo ci = getInfo();
  150. AudioMedia aud_med;
  151. AudioMedia& play_dev_med =
  152. MyEndpoint::instance().audDevManager().getPlaybackDevMedia();
  153. try {
  154. // Get the first audio media
  155. aud_med = getAudioMedia(-1);
  156. } catch(...) {
  157. std::cout << "Failed to get audio media" << std::endl;
  158. return;
  159. }
  160. if (!wav_player) {
  161. wav_player = new AudioMediaPlayer();
  162. try {
  163. wav_player->createPlayer(
  164. "../../../../tests/pjsua/wavs/input.16.wav", 0);
  165. } catch (...) {
  166. std::cout << "Failed opening wav file" << std::endl;
  167. delete wav_player;
  168. wav_player = NULL;
  169. }
  170. }
  171. // This will connect the wav file to the call audio media
  172. if (wav_player)
  173. wav_player->startTransmit(aud_med);
  174. if (!med_port) {
  175. med_port = new MyAudioMediaPort();
  176. MediaFormatAudio fmt;
  177. fmt.init(PJMEDIA_FORMAT_PCM, 16000, 1, 20000, 16);
  178. med_port->createPort("med_port", fmt);
  179. // Connect the media port to the call audio media in both directions
  180. med_port->startTransmit(aud_med);
  181. aud_med.startTransmit(*med_port);
  182. }
  183. // And this will connect the call audio media to the sound device/speaker
  184. aud_med.startTransmit(play_dev_med);
  185. }
  186. void MyCall::onCallTransferRequest(OnCallTransferRequestParam &prm)
  187. {
  188. /* Create new Call for call transfer */
  189. prm.newCall = new MyCall(*myAcc);
  190. }
  191. void MyCall::onCallReplaceRequest(OnCallReplaceRequestParam &prm)
  192. {
  193. /* Create new Call for call replace */
  194. prm.newCall = new MyCall(*myAcc);
  195. }
  196. #if USE_TEST == 1
  197. static void mainProg1(MyEndpoint &ep)
  198. {
  199. // Init library
  200. EpConfig ep_cfg;
  201. ep_cfg.logConfig.level = 4;
  202. ep.libInit( ep_cfg );
  203. // Transport
  204. TransportConfig tcfg;
  205. tcfg.port = 5060;
  206. ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
  207. // Start library
  208. ep.libStart();
  209. std::cout << "*** PJSUA2 STARTED ***" << std::endl;
  210. // Add account
  211. AccountConfig acc_cfg;
  212. acc_cfg.idUri = "sip:test1@pjsip.org";
  213. acc_cfg.regConfig.registrarUri = "sip:sip.pjsip.org";
  214. #if PJSIP_HAS_DIGEST_AKA_AUTH
  215. AuthCredInfo aci("Digest", "*", "test", PJSIP_CRED_DATA_EXT_AKA | PJSIP_CRED_DATA_PLAIN_PASSWD, "passwd");
  216. aci.akaK = "passwd";
  217. #else
  218. AuthCredInfo aci("digest", "*", "test1", 0, "test1");
  219. #endif
  220. acc_cfg.sipConfig.authCreds.push_back(aci);
  221. MyAccount *acc(new MyAccount);
  222. try {
  223. acc->create(acc_cfg);
  224. } catch (...) {
  225. std::cout << "Adding account failed" << std::endl;
  226. }
  227. pj_thread_sleep(2000);
  228. // Make outgoing call
  229. Call *call = new MyCall(*acc);
  230. acc->calls.push_back(call);
  231. CallOpParam prm(true);
  232. prm.opt.audioCount = 1;
  233. prm.opt.videoCount = 0;
  234. call->makeCall("sip:test1@pjsip.org", prm);
  235. // Hangup all calls
  236. pj_thread_sleep(4000);
  237. ep.hangupAllCalls();
  238. pj_thread_sleep(4000);
  239. // Destroy library
  240. std::cout << "*** PJSUA2 SHUTTING DOWN ***" << std::endl;
  241. delete acc; /* Will delete all calls too */
  242. }
  243. #endif
  244. #if USE_TEST == 2
  245. static void mainProg2()
  246. {
  247. string json_str;
  248. {
  249. EpConfig epCfg;
  250. JsonDocument jDoc;
  251. epCfg.uaConfig.maxCalls = 61;
  252. epCfg.uaConfig.userAgent = "Just JSON Test";
  253. epCfg.uaConfig.stunServer.push_back("stun1.pjsip.org");
  254. epCfg.uaConfig.stunServer.push_back("stun2.pjsip.org");
  255. epCfg.logConfig.filename = "THE.LOG";
  256. jDoc.writeObject(epCfg);
  257. json_str = jDoc.saveString();
  258. std::cout << json_str << std::endl << std::endl;
  259. }
  260. {
  261. EpConfig epCfg;
  262. JsonDocument rDoc;
  263. string output;
  264. rDoc.loadString(json_str);
  265. rDoc.readObject(epCfg);
  266. JsonDocument wDoc;
  267. wDoc.writeObject(epCfg);
  268. json_str = wDoc.saveString();
  269. std::cout << json_str << std::endl << std::endl;
  270. wDoc.saveFile("jsontest.js");
  271. }
  272. {
  273. EpConfig epCfg;
  274. JsonDocument rDoc;
  275. rDoc.loadFile("jsontest.js");
  276. rDoc.readObject(epCfg);
  277. pj_file_delete("jsontest.js");
  278. }
  279. }
  280. #endif
  281. #if USE_TEST == 3
  282. static void mainProg3(MyEndpoint &ep)
  283. {
  284. const char *paths[] = { "../../../../tests/pjsua/wavs/input.16.wav",
  285. "../../tests/pjsua/wavs/input.16.wav",
  286. "input.16.wav"};
  287. unsigned i;
  288. const char *filename = NULL;
  289. // Init library
  290. EpConfig ep_cfg;
  291. ep.libInit( ep_cfg );
  292. for (i=0; i<PJ_ARRAY_SIZE(paths); ++i) {
  293. if (pj_file_exists(paths[i])) {
  294. filename = paths[i];
  295. break;
  296. }
  297. }
  298. if (!filename) {
  299. PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "mainProg3()",
  300. "Could not locate input.16.wav");
  301. }
  302. // Start library
  303. ep.libStart();
  304. std::cout << "*** PJSUA2 STARTED ***" << std::endl;
  305. /* Use Null Audio Device as main media clock. This is useful for improving
  306. * media clock (see also https://trac.pjsip.org/repos/wiki/FAQ#tx-timing)
  307. * especially when sound device clock is jittery.
  308. */
  309. ep.audDevManager().setNullDev();
  310. /* And install sound device using Extra Audio Device */
  311. ExtraAudioDevice auddev2(-1, -1);
  312. try {
  313. auddev2.open();
  314. } catch (...) {
  315. std::cout << "Extra sound device failed" << std::endl;
  316. }
  317. // Create player and recorder
  318. {
  319. AudioMediaPlayer amp;
  320. amp.createPlayer(filename);
  321. AudioMediaRecorder amr;
  322. amr.createRecorder("recorder_test_output.wav");
  323. amp.startTransmit(amr);
  324. if (auddev2.isOpened())
  325. amp.startTransmit(auddev2);
  326. pj_thread_sleep(5000);
  327. }
  328. }
  329. #endif
  330. #if USE_TEST == 0
  331. static void mainProg(MyEndpoint &)
  332. {
  333. string json_str;
  334. {
  335. JsonDocument jdoc;
  336. AccountConfig accCfg;
  337. accCfg.idUri = "\"Just Test\" <sip:test@pjsip.org>";
  338. accCfg.regConfig.registrarUri = "sip:sip.pjsip.org";
  339. SipHeader h;
  340. h.hName = "X-Header";
  341. h.hValue = "User header";
  342. accCfg.regConfig.headers.push_back(h);
  343. accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tcp>");
  344. accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tls>");
  345. accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(1);
  346. accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(2);
  347. accCfg.mediaConfig.transportConfig.tlsConfig.ciphers.push_back(3);
  348. AuthCredInfo aci;
  349. aci.scheme = "digest";
  350. aci.username = "test";
  351. aci.data = "passwd";
  352. aci.realm = "*";
  353. aci.dataType = PJSIP_CRED_DATA_PLAIN_PASSWD;
  354. #if PJSIP_HAS_DIGEST_AKA_AUTH
  355. aci.dataType |= PJSIP_CRED_DATA_EXT_AKA;
  356. aci.akaK = "key";
  357. #endif
  358. accCfg.sipConfig.authCreds.push_back(aci);
  359. jdoc.writeObject(accCfg);
  360. json_str = jdoc.saveString();
  361. std::cout << "Original:" << std::endl;
  362. std::cout << json_str << std::endl << std::endl;
  363. }
  364. {
  365. JsonDocument rdoc;
  366. rdoc.loadString(json_str);
  367. AccountConfig accCfg;
  368. rdoc.readObject(accCfg);
  369. JsonDocument wdoc;
  370. wdoc.writeObject(accCfg);
  371. json_str = wdoc.saveString();
  372. std::cout << "Parsed:" << std::endl;
  373. std::cout << json_str << std::endl << std::endl;
  374. }
  375. }
  376. #endif
  377. #if USE_TEST == 4
  378. static void mainProg4(MyEndpoint &ep)
  379. {
  380. // Init library
  381. EpConfig ep_cfg;
  382. ep.libInit( ep_cfg );
  383. // Create transport
  384. TransportConfig tcfg;
  385. tcfg.port = 5060;
  386. ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
  387. ep.transportCreate(PJSIP_TRANSPORT_TCP, tcfg);
  388. // Add account
  389. AccountConfig acc_cfg;
  390. acc_cfg.idUri = "sip:localhost";
  391. MyAccount *acc(new MyAccount);
  392. acc->create(acc_cfg);
  393. // Start library
  394. ep.libStart();
  395. std::cout << "*** PJSUA2 STARTED ***" << std::endl;
  396. // Just wait for ENTER key
  397. std::cout << "Press ENTER to quit..." << std::endl;
  398. std::cin.get();
  399. delete acc;
  400. }
  401. #endif
  402. extern "C"
  403. int main()
  404. {
  405. int ret = 0;
  406. MyEndpoint ep;
  407. try {
  408. ep.libCreate();
  409. #if USE_TEST == 0
  410. mainProg(ep);
  411. #endif
  412. #if USE_TEST == 1
  413. mainProg1(ep);
  414. #endif
  415. #if USE_TEST == 2
  416. mainProg2(ep);
  417. #endif
  418. #if USE_TEST == 3
  419. mainProg3(ep);
  420. #endif
  421. #if USE_TEST == 4
  422. mainProg4(ep);
  423. #endif
  424. ret = PJ_SUCCESS;
  425. } catch (Error & err) {
  426. std::cout << "Exception: " << err.info() << std::endl;
  427. ret = 1;
  428. }
  429. try {
  430. ep.libDestroy();
  431. } catch(Error &err) {
  432. std::cout << "Exception: " << err.info() << std::endl;
  433. ret = 1;
  434. }
  435. if (ret == PJ_SUCCESS) {
  436. std::cout << "Success" << std::endl;
  437. } else {
  438. std::cout << "Error Found" << std::endl;
  439. }
  440. return ret;
  441. }