PjsuaCommunicator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include "PjsuaCommunicator.hpp"
  2. #include <pjlib.h>
  3. #include <pjsua-lib/pjsua.h>
  4. #include <boost/algorithm/string.hpp>
  5. #include <boost/format.hpp>
  6. #include "main.hpp"
  7. using namespace std;
  8. namespace sip {
  9. using namespace log4cpp;
  10. class _LogWriter : public pj::LogWriter {
  11. public:
  12. _LogWriter(Category &logger)
  13. : logger(logger) { }
  14. virtual void write(const pj::LogEntry &entry) override {
  15. auto message = entry.msg.substr(0, entry.msg.size() - 1); // remove newline
  16. logger << prioritiesMap.at(entry.level) << message;
  17. }
  18. private:
  19. log4cpp::Category &logger;
  20. std::map<int, Priority::Value> prioritiesMap = {
  21. {1, Priority::ERROR},
  22. {2, Priority::WARN},
  23. {3, Priority::NOTICE},
  24. {4, Priority::INFO},
  25. {5, Priority::DEBUG},
  26. {6, Priority::DEBUG}
  27. };
  28. };
  29. class _MumlibAudioMedia : public pj::AudioMedia {
  30. public:
  31. _MumlibAudioMedia(int call_id, sip::PjsuaCommunicator &comm, int frameTimeLength)
  32. : communicator(comm) {
  33. createMediaPort(call_id, frameTimeLength);
  34. registerMediaPort(&mediaPort);
  35. }
  36. ~_MumlibAudioMedia() {
  37. unregisterMediaPort();
  38. }
  39. private:
  40. pjmedia_port mediaPort;
  41. sip::PjsuaCommunicator &communicator;
  42. static pj_status_t callback_getFrame(pjmedia_port *port, pjmedia_frame *frame) {
  43. auto *communicator = static_cast<sip::PjsuaCommunicator *>(port->port_data.pdata);
  44. return communicator->mediaPortGetFrame(port, frame);
  45. }
  46. static pj_status_t callback_putFrame(pjmedia_port *port, pjmedia_frame *frame) {
  47. auto *communicator = static_cast<sip::PjsuaCommunicator *>(port->port_data.pdata);
  48. return communicator->mediaPortPutFrame(port, frame);
  49. }
  50. void createMediaPort(int call_id, int frameTimeLength) {
  51. auto name = pj_str((char *) "MumsiMediaPort");
  52. if (frameTimeLength != 10
  53. and frameTimeLength != 20
  54. and frameTimeLength != 40
  55. and frameTimeLength != 60) {
  56. throw sip::Exception(
  57. (boost::format("valid frame time length value: %d. valid values are: 10, 20, 40, 60") %
  58. frameTimeLength).str());
  59. }
  60. pj_status_t status = pjmedia_port_info_init(&(mediaPort.info),
  61. &name,
  62. PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'),
  63. SAMPLING_RATE,
  64. 1,
  65. 16,
  66. SAMPLING_RATE * frameTimeLength / 1000);
  67. if (status != PJ_SUCCESS) {
  68. throw sip::Exception("error while calling pjmedia_port_info_init()", status);
  69. }
  70. mediaPort.port_data.pdata = &communicator;
  71. // track call id in port_data
  72. mediaPort.port_data.ldata = (long) call_id;
  73. mediaPort.get_frame = &callback_getFrame;
  74. mediaPort.put_frame = &callback_putFrame;
  75. }
  76. };
  77. class _Call : public pj::Call {
  78. public:
  79. _Call(sip::PjsuaCommunicator &comm, pj::Account &acc, int call_id = PJSUA_INVALID_ID)
  80. : pj::Call(acc, call_id),
  81. communicator(comm),
  82. account(acc) { }
  83. virtual void onCallState(pj::OnCallStateParam &prm) override;
  84. virtual void onCallMediaState(pj::OnCallMediaStateParam &prm) override;
  85. virtual void onDtmfDigit(pj::OnDtmfDigitParam &prm) override;
  86. virtual void playAudioFile(std::string file);
  87. virtual void playAudioFile(std::string file, bool in_chan);
  88. private:
  89. sip::PjsuaCommunicator &communicator;
  90. pj::Account &account;
  91. };
  92. class _Account : public pj::Account {
  93. public:
  94. _Account(sip::PjsuaCommunicator &comm)
  95. : communicator(comm) { }
  96. virtual void onRegState(pj::OnRegStateParam &prm) override;
  97. virtual void onIncomingCall(pj::OnIncomingCallParam &iprm) override;
  98. private:
  99. sip::PjsuaCommunicator &communicator;
  100. bool available = true;
  101. friend class _Call;
  102. };
  103. void _Call::onCallState(pj::OnCallStateParam &prm) {
  104. auto ci = getInfo();
  105. communicator.logger.info("Call %d state=%s.", ci.id, ci.stateText.c_str());
  106. string address = ci.remoteUri;
  107. boost::replace_all(address, "<", "");
  108. boost::replace_all(address, ">", "");
  109. if (ci.state == PJSIP_INV_STATE_CONFIRMED) {
  110. auto msgText = "Incoming call from " + address + ".";
  111. // first, login to Mumble (only matters if MUM_DELAYED_CONNECT)
  112. communicator.calls[ci.id].onConnect();
  113. pj_thread_sleep(500); // sleep a moment to allow connection to stabilize
  114. communicator.logger.notice(msgText);
  115. communicator.calls[ci.id].onStateChange(msgText);
  116. pj_thread_sleep(500); // sleep a moment to allow connection to stabilize
  117. this->playAudioFile(communicator.file_welcome);
  118. communicator.got_dtmf = "";
  119. communicator.logger.notice("MYDEBUG: pin length=%d", communicator.caller_pin.length());
  120. /*
  121. * if no pin is set, go ahead and turn off mute/deaf
  122. * otherwise, wait for pin to be entered
  123. */
  124. if ( communicator.caller_pin.length() == 0 ) {
  125. // No PIN set... enter DTMF root menu and turn off mute/deaf
  126. communicator.dtmf_mode = DTMF_MODE_ROOT;
  127. // turning off mute automatically turns off deaf
  128. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  129. pj_thread_sleep(500); // sleep a moment to allow connection to stabilize
  130. this->playAudioFile(communicator.file_announce_new_caller, true);
  131. } else {
  132. // PIN set... enter DTMF unauth menu and play PIN prompt message
  133. communicator.dtmf_mode = DTMF_MODE_UNAUTH;
  134. communicator.logger.notice("MYDEBUG: call joinDefaultChannel()");
  135. communicator.calls[ci.id].joinDefaultChannel();
  136. pj_thread_sleep(500); // pause briefly after announcement
  137. communicator.logger.notice("MYDEBUG: call play...()");
  138. this->playAudioFile(communicator.file_prompt_pin);
  139. }
  140. } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
  141. auto &acc = dynamic_cast<_Account &>(account);
  142. if (not acc.available) {
  143. auto msgText = "Call from " + address + " finished.";
  144. communicator.calls[ci.id].mixer->clear();
  145. communicator.logger.notice(msgText);
  146. communicator.calls[ci.id].onStateChange(msgText);
  147. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_DEAF, true);
  148. communicator.logger.notice("MYDEBUG: call joinDefaultChannel()");
  149. communicator.calls[ci.id].joinDefaultChannel();
  150. communicator.calls[ci.id].onDisconnect();
  151. acc.available = true;
  152. }
  153. delete this;
  154. } else {
  155. communicator.logger.notice("MYDEBUG: onCallState() call:%d state:%d",
  156. ci.id, ci.state);
  157. }
  158. }
  159. void _Call::onCallMediaState(pj::OnCallMediaStateParam &prm) {
  160. auto ci = getInfo();
  161. if (ci.media.size() != 1) {
  162. throw sip::Exception("ci.media.size is not 1");
  163. }
  164. if (ci.media[0].status == PJSUA_CALL_MEDIA_ACTIVE) {
  165. auto *aud_med = static_cast<pj::AudioMedia *>(getMedia(0));
  166. communicator.calls[ci.id].media->startTransmit(*aud_med);
  167. aud_med->startTransmit(*communicator.calls[ci.id].media);
  168. } else if (ci.media[0].status == PJSUA_CALL_MEDIA_NONE) {
  169. dynamic_cast<_Account &>(account).available = true;
  170. }
  171. }
  172. void _Call::playAudioFile(std::string file) {
  173. this->playAudioFile(file, false); // default is NOT to echo to mumble
  174. }
  175. /* TODO:
  176. * - local deafen before playing and undeafen after?
  177. */
  178. void _Call::playAudioFile(std::string file, bool in_chan) {
  179. communicator.logger.notice("Entered playAudioFile(%s)", file.c_str());
  180. pj::AudioMediaPlayer player;
  181. pj::MediaFormatAudio mfa;
  182. pj::AudioMediaPlayerInfo pinfo;
  183. int wavsize;
  184. int sleeptime;
  185. if ( ! pj_file_exists(file.c_str()) ) {
  186. communicator.logger.warn("File not found (%s)", file.c_str());
  187. return;
  188. }
  189. /* TODO: use some library to get the actual length in millisec
  190. *
  191. * This just gets the file size and divides by a constant to
  192. * estimate the length of the WAVE file in milliseconds.
  193. * This depends on the encoding bitrate, etc.
  194. */
  195. auto ci = getInfo();
  196. if (ci.media.size() != 1) {
  197. throw sip::Exception("ci.media.size is not 1");
  198. }
  199. if (ci.media[0].status == PJSUA_CALL_MEDIA_ACTIVE) {
  200. auto *aud_med = static_cast<pj::AudioMedia *>(getMedia(0));
  201. try {
  202. player.createPlayer(file, PJMEDIA_FILE_NO_LOOP);
  203. pinfo = player.getInfo();
  204. sleeptime = pinfo.sizeBytes / (pinfo.payloadBitsPerSample * 3);
  205. if ( in_chan ) { // choose the target sound output
  206. player.startTransmit(*communicator.calls[ci.id].media);
  207. } else {
  208. player.startTransmit(*aud_med);
  209. }
  210. pj_thread_sleep(sleeptime);
  211. if ( in_chan ) { // choose the target sound output
  212. player.stopTransmit(*communicator.calls[ci.id].media);
  213. } else {
  214. player.stopTransmit(*aud_med);
  215. }
  216. } catch (...) {
  217. communicator.logger.notice("Error playing file %s", file.c_str());
  218. }
  219. } else {
  220. communicator.logger.notice("Call not active - can't play file %s", file.c_str());
  221. }
  222. }
  223. void _Call::onDtmfDigit(pj::OnDtmfDigitParam &prm) {
  224. //communicator.logger.notice("DTMF digit '%s' (call %d).",
  225. // prm.digit.c_str(), getId());
  226. pj::CallOpParam param;
  227. auto ci = getInfo();
  228. /*
  229. * DTMF CALLER MENU
  230. */
  231. switch ( communicator.dtmf_mode ) {
  232. case DTMF_MODE_UNAUTH:
  233. /*
  234. * IF UNAUTH, the only thing we allow is to authorize.
  235. */
  236. switch ( prm.digit[0] ) {
  237. case '#':
  238. /*
  239. * When user presses '#', test PIN entry
  240. */
  241. if ( communicator.caller_pin.length() > 0 ) {
  242. if ( communicator.got_dtmf == communicator.caller_pin ) {
  243. communicator.logger.notice("Caller entered correct PIN");
  244. communicator.dtmf_mode = DTMF_MODE_ROOT;
  245. communicator.calls[ci.id].joinAuthChannel();
  246. this->playAudioFile(communicator.file_entering_channel);
  247. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  248. this->playAudioFile(communicator.file_announce_new_caller, true);
  249. } else {
  250. communicator.logger.notice("Caller entered wrong PIN");
  251. this->playAudioFile(communicator.file_invalid_pin);
  252. if ( communicator.pin_fails++ >= MAX_PIN_FAILS ) {
  253. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  254. pj_thread_sleep(500); // pause before next announcement
  255. this->playAudioFile(communicator.file_goodbye);
  256. pj_thread_sleep(500); // pause before next announcement
  257. this->hangup(param);
  258. }
  259. this->playAudioFile(communicator.file_prompt_pin);
  260. }
  261. communicator.got_dtmf = "";
  262. }
  263. break;
  264. case '*':
  265. /*
  266. * Allow user to reset PIN entry by pressing '*'
  267. */
  268. communicator.got_dtmf = "";
  269. this->playAudioFile(communicator.file_prompt_pin);
  270. break;
  271. default:
  272. /*
  273. * In all other cases, add input digit to stack
  274. */
  275. communicator.got_dtmf = communicator.got_dtmf + prm.digit;
  276. if ( communicator.got_dtmf.size() > MAX_CALLER_PIN_LEN ) {
  277. // just drop 'em if too long
  278. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  279. this->playAudioFile(communicator.file_goodbye);
  280. pj_thread_sleep(500); // pause before next announcement
  281. this->hangup(param);
  282. }
  283. }
  284. break;
  285. case DTMF_MODE_ROOT:
  286. /*
  287. * User already authenticated; no data entry pending
  288. */
  289. switch ( prm.digit[0] ) {
  290. case '*':
  291. /*
  292. * Switch user to 'star' menu
  293. */
  294. communicator.dtmf_mode = DTMF_MODE_STAR;
  295. break;
  296. default:
  297. /*
  298. * Default is to ignore all digits in root
  299. */
  300. communicator.logger.notice("Ignore DTMF digit '%s' in ROOT state", prm.digit.c_str());
  301. }
  302. break;
  303. case DTMF_MODE_STAR:
  304. /*
  305. * User already entered '*'; time to perform action
  306. */
  307. switch ( prm.digit[0] ) {
  308. case '5':
  309. // Mute line
  310. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, true);
  311. this->playAudioFile(communicator.file_mute_on);
  312. break;
  313. case '6':
  314. // Un-mute line
  315. this->playAudioFile(communicator.file_mute_off);
  316. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  317. break;
  318. case '0':
  319. // play menu
  320. this->playAudioFile(communicator.file_menu);
  321. break;
  322. default:
  323. communicator.logger.notice("Unsupported DTMF digit '%s' in state STAR", prm.digit.c_str());
  324. }
  325. /*
  326. * In any case, switch back to root after one digit
  327. */
  328. communicator.dtmf_mode = DTMF_MODE_ROOT;
  329. break;
  330. default:
  331. communicator.logger.notice("Unexpected DTMF '%s' in unknown state '%d'", prm.digit.c_str(),
  332. communicator.dtmf_mode);
  333. }
  334. }
  335. void _Account::onRegState(pj::OnRegStateParam &prm) {
  336. pj::AccountInfo ai = getInfo();
  337. communicator.logger << log4cpp::Priority::INFO
  338. << (ai.regIsActive ? "Register:" : "Unregister:") << " code=" << prm.code;
  339. }
  340. void _Account::onIncomingCall(pj::OnIncomingCallParam &iprm) {
  341. auto *call = new _Call(communicator, *this, iprm.callId);
  342. string uri = call->getInfo().remoteUri;
  343. communicator.logger.info("Incoming call from %s.", uri.c_str());
  344. pj::CallOpParam param;
  345. if (communicator.uriValidator.validateUri(uri)) {
  346. if (available) {
  347. param.statusCode = PJSIP_SC_OK;
  348. available = false;
  349. } else {
  350. /*
  351. * EXPERIMENT WITH MULTI-LINE
  352. */
  353. communicator.logger.info("MULTI-LINE Incoming call from %s.", uri.c_str());
  354. param.statusCode = PJSIP_SC_OK;
  355. available = false;
  356. //param.statusCode = PJSIP_SC_BUSY_EVERYWHERE;
  357. }
  358. call->answer(param);
  359. } else {
  360. communicator.logger.warn("Refusing call from %s.", uri.c_str());
  361. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  362. call->hangup(param);
  363. }
  364. }
  365. }
  366. sip::PjsuaCommunicator::PjsuaCommunicator(IncomingConnectionValidator &validator, int frameTimeLength, int maxCalls)
  367. : logger(log4cpp::Category::getInstance("SipCommunicator")),
  368. pjsuaLogger(log4cpp::Category::getInstance("Pjsua")),
  369. uriValidator(validator) {
  370. logWriter.reset(new sip::_LogWriter(pjsuaLogger));
  371. endpoint.libCreate();
  372. pj::EpConfig endpointConfig;
  373. endpointConfig.uaConfig.userAgent = "Mumsi Mumble-SIP gateway";
  374. endpointConfig.uaConfig.maxCalls = maxCalls;
  375. endpointConfig.logConfig.writer = logWriter.get();
  376. endpointConfig.logConfig.level = 5;
  377. endpointConfig.medConfig.noVad = true;
  378. endpoint.libInit(endpointConfig);
  379. for(int i=0; i<maxCalls; ++i) {
  380. calls[i].index = i;
  381. pj_caching_pool_init(&(calls[i].cachingPool), &pj_pool_factory_default_policy, 0);
  382. calls[i].mixer.reset(new mixer::AudioFramesMixer(calls[i].cachingPool.factory));
  383. calls[i].media.reset(new _MumlibAudioMedia(i, *this, frameTimeLength));
  384. }
  385. logger.info("Created Pjsua communicator with frame length %d ms.", frameTimeLength);
  386. }
  387. void sip::PjsuaCommunicator::connect(
  388. std::string host,
  389. std::string user,
  390. std::string password,
  391. unsigned int port) {
  392. pj::TransportConfig transportConfig;
  393. transportConfig.port = port;
  394. endpoint.transportCreate(PJSIP_TRANSPORT_UDP, transportConfig); // todo try catch
  395. endpoint.libStart();
  396. pj_status_t status = pjsua_set_null_snd_dev();
  397. if (status != PJ_SUCCESS) {
  398. throw sip::Exception("error in pjsua_set_null_std_dev()", status);
  399. }
  400. registerAccount(host, user, password);
  401. }
  402. sip::PjsuaCommunicator::~PjsuaCommunicator() {
  403. endpoint.libDestroy();
  404. }
  405. void sip::PjsuaCommunicator::sendPcmSamples(int callId, int sessionId, int sequenceNumber, int16_t *samples, unsigned int length) {
  406. calls[callId].mixer->addFrameToBuffer(sessionId, sequenceNumber, samples, length);
  407. }
  408. pj_status_t sip::PjsuaCommunicator::mediaPortGetFrame(pjmedia_port *port, pjmedia_frame *frame) {
  409. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  410. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  411. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&(port->info));
  412. int call_id = (int) port->port_data.ldata;
  413. const int readSamples = calls[call_id].mixer->getMixedSamples(samples, count);
  414. if (readSamples < count) {
  415. pjsuaLogger.debug("Requested %d samples, available %d, filling remaining with zeros.",
  416. count, readSamples);
  417. for (int i = readSamples; i < count; ++i) {
  418. samples[i] = 0;
  419. }
  420. }
  421. return PJ_SUCCESS;
  422. }
  423. pj_status_t sip::PjsuaCommunicator::mediaPortPutFrame(pjmedia_port *port, pjmedia_frame *frame) {
  424. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  425. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
  426. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  427. int call_id = (int) port->port_data.ldata;
  428. if (count > 0) {
  429. pjsuaLogger.debug("Calling onIncomingPcmSamples with %d samples (call_id=%d).", count, call_id);
  430. this->calls[call_id].onIncomingPcmSamples(samples, count);
  431. }
  432. return PJ_SUCCESS;
  433. }
  434. void sip::PjsuaCommunicator::registerAccount(string host, string user, string password) {
  435. string uri = "sip:" + user + "@" + host;
  436. pj::AccountConfig accountConfig;
  437. accountConfig.idUri = uri;
  438. accountConfig.regConfig.registrarUri = "sip:" + host;
  439. pj::AuthCredInfo cred("digest", "*", user, 0, password);
  440. accountConfig.sipConfig.authCreds.push_back(cred);
  441. logger.info("Registering account for URI: %s.", uri.c_str());
  442. account.reset(new _Account(*this));
  443. account->create(accountConfig);
  444. }