PjsuaCommunicator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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].sendUserStateStr(mumlib::UserState::COMMENT, msgText);
  116. communicator.calls[ci.id].onStateChange(msgText);
  117. pj_thread_sleep(500); // sleep a moment to allow connection to stabilize
  118. this->playAudioFile(communicator.file_welcome);
  119. communicator.got_dtmf = "";
  120. communicator.logger.notice("MYDEBUG: pin length=%d", communicator.caller_pin.length());
  121. /*
  122. * if no pin is set, go ahead and turn off mute/deaf
  123. * otherwise, wait for pin to be entered
  124. */
  125. if ( communicator.caller_pin.length() == 0 ) {
  126. // No PIN set... enter DTMF root menu and turn off mute/deaf
  127. communicator.dtmf_mode = DTMF_MODE_ROOT;
  128. // turning off mute automatically turns off deaf
  129. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  130. pj_thread_sleep(500); // sleep a moment to allow connection to stabilize
  131. this->playAudioFile(communicator.file_announce_new_caller, true);
  132. } else {
  133. // PIN set... enter DTMF unauth menu and play PIN prompt message
  134. communicator.dtmf_mode = DTMF_MODE_UNAUTH;
  135. communicator.logger.notice("MYDEBUG: call joinDefaultChannel()");
  136. communicator.calls[ci.id].joinDefaultChannel();
  137. pj_thread_sleep(500); // pause briefly after announcement
  138. communicator.logger.notice("MYDEBUG: call play...()");
  139. this->playAudioFile(communicator.file_prompt_pin);
  140. }
  141. } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
  142. auto &acc = dynamic_cast<_Account &>(account);
  143. if (not acc.available) {
  144. auto msgText = "Call from " + address + " finished.";
  145. communicator.calls[ci.id].mixer->clear();
  146. communicator.logger.notice(msgText);
  147. communicator.calls[ci.id].sendUserStateStr(mumlib::UserState::COMMENT, msgText);
  148. communicator.calls[ci.id].onStateChange(msgText);
  149. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_DEAF, true);
  150. communicator.logger.notice("MYDEBUG: call joinDefaultChannel()");
  151. communicator.calls[ci.id].joinDefaultChannel();
  152. communicator.calls[ci.id].onDisconnect();
  153. acc.available = true;
  154. }
  155. delete this;
  156. } else {
  157. communicator.logger.notice("MYDEBUG: onCallState() call:%d state:%d",
  158. ci.id, ci.state);
  159. }
  160. }
  161. void _Call::onCallMediaState(pj::OnCallMediaStateParam &prm) {
  162. auto ci = getInfo();
  163. if (ci.media.size() != 1) {
  164. throw sip::Exception("ci.media.size is not 1");
  165. }
  166. if (ci.media[0].status == PJSUA_CALL_MEDIA_ACTIVE) {
  167. auto *aud_med = static_cast<pj::AudioMedia *>(getMedia(0));
  168. communicator.calls[ci.id].media->startTransmit(*aud_med);
  169. aud_med->startTransmit(*communicator.calls[ci.id].media);
  170. } else if (ci.media[0].status == PJSUA_CALL_MEDIA_NONE) {
  171. dynamic_cast<_Account &>(account).available = true;
  172. }
  173. }
  174. void _Call::playAudioFile(std::string file) {
  175. this->playAudioFile(file, false); // default is NOT to echo to mumble
  176. }
  177. /* TODO:
  178. * - local deafen before playing and undeafen after?
  179. */
  180. void _Call::playAudioFile(std::string file, bool in_chan) {
  181. communicator.logger.notice("Entered playAudioFile(%s)", file.c_str());
  182. pj::AudioMediaPlayer player;
  183. pj::MediaFormatAudio mfa;
  184. pj::AudioMediaPlayerInfo pinfo;
  185. int wavsize;
  186. int sleeptime;
  187. if ( ! pj_file_exists(file.c_str()) ) {
  188. communicator.logger.warn("File not found (%s)", file.c_str());
  189. return;
  190. }
  191. /* TODO: use some library to get the actual length in millisec
  192. *
  193. * This just gets the file size and divides by a constant to
  194. * estimate the length of the WAVE file in milliseconds.
  195. * This depends on the encoding bitrate, etc.
  196. */
  197. auto ci = getInfo();
  198. if (ci.media.size() != 1) {
  199. throw sip::Exception("ci.media.size is not 1");
  200. }
  201. if (ci.media[0].status == PJSUA_CALL_MEDIA_ACTIVE) {
  202. auto *aud_med = static_cast<pj::AudioMedia *>(getMedia(0));
  203. try {
  204. player.createPlayer(file, PJMEDIA_FILE_NO_LOOP);
  205. pinfo = player.getInfo();
  206. sleeptime = pinfo.sizeBytes / (pinfo.payloadBitsPerSample * 3);
  207. if ( in_chan ) { // choose the target sound output
  208. player.startTransmit(*communicator.calls[ci.id].media);
  209. } else {
  210. player.startTransmit(*aud_med);
  211. }
  212. pj_thread_sleep(sleeptime);
  213. if ( in_chan ) { // choose the target sound output
  214. player.stopTransmit(*communicator.calls[ci.id].media);
  215. } else {
  216. player.stopTransmit(*aud_med);
  217. }
  218. } catch (...) {
  219. communicator.logger.notice("Error playing file %s", file.c_str());
  220. }
  221. } else {
  222. communicator.logger.notice("Call not active - can't play file %s", file.c_str());
  223. }
  224. }
  225. void _Call::onDtmfDigit(pj::OnDtmfDigitParam &prm) {
  226. //communicator.logger.notice("DTMF digit '%s' (call %d).",
  227. // prm.digit.c_str(), getId());
  228. pj::CallOpParam param;
  229. auto ci = getInfo();
  230. /*
  231. * DTMF CALLER MENU
  232. */
  233. switch ( communicator.dtmf_mode ) {
  234. case DTMF_MODE_UNAUTH:
  235. /*
  236. * IF UNAUTH, the only thing we allow is to authorize.
  237. */
  238. switch ( prm.digit[0] ) {
  239. case '#':
  240. /*
  241. * When user presses '#', test PIN entry
  242. */
  243. if ( communicator.caller_pin.length() > 0 ) {
  244. if ( communicator.got_dtmf == communicator.caller_pin ) {
  245. communicator.logger.notice("Caller entered correct PIN");
  246. communicator.dtmf_mode = DTMF_MODE_ROOT;
  247. communicator.calls[ci.id].joinAuthChannel();
  248. this->playAudioFile(communicator.file_entering_channel);
  249. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  250. this->playAudioFile(communicator.file_announce_new_caller, true);
  251. } else {
  252. communicator.logger.notice("Caller entered wrong PIN");
  253. this->playAudioFile(communicator.file_invalid_pin);
  254. if ( communicator.pin_fails++ >= MAX_PIN_FAILS ) {
  255. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  256. pj_thread_sleep(500); // pause before next announcement
  257. this->playAudioFile(communicator.file_goodbye);
  258. pj_thread_sleep(500); // pause before next announcement
  259. this->hangup(param);
  260. }
  261. this->playAudioFile(communicator.file_prompt_pin);
  262. }
  263. communicator.got_dtmf = "";
  264. }
  265. break;
  266. case '*':
  267. /*
  268. * Allow user to reset PIN entry by pressing '*'
  269. */
  270. communicator.got_dtmf = "";
  271. this->playAudioFile(communicator.file_prompt_pin);
  272. break;
  273. default:
  274. /*
  275. * In all other cases, add input digit to stack
  276. */
  277. communicator.got_dtmf = communicator.got_dtmf + prm.digit;
  278. if ( communicator.got_dtmf.size() > MAX_CALLER_PIN_LEN ) {
  279. // just drop 'em if too long
  280. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  281. this->playAudioFile(communicator.file_goodbye);
  282. pj_thread_sleep(500); // pause before next announcement
  283. this->hangup(param);
  284. }
  285. }
  286. break;
  287. case DTMF_MODE_ROOT:
  288. /*
  289. * User already authenticated; no data entry pending
  290. */
  291. switch ( prm.digit[0] ) {
  292. case '*':
  293. /*
  294. * Switch user to 'star' menu
  295. */
  296. communicator.dtmf_mode = DTMF_MODE_STAR;
  297. break;
  298. default:
  299. /*
  300. * Default is to ignore all digits in root
  301. */
  302. communicator.logger.notice("Ignore DTMF digit '%s' in ROOT state", prm.digit.c_str());
  303. }
  304. break;
  305. case DTMF_MODE_STAR:
  306. /*
  307. * User already entered '*'; time to perform action
  308. */
  309. switch ( prm.digit[0] ) {
  310. case '5':
  311. // Mute line
  312. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, true);
  313. this->playAudioFile(communicator.file_mute_on);
  314. break;
  315. case '6':
  316. // Un-mute line
  317. this->playAudioFile(communicator.file_mute_off);
  318. communicator.calls[ci.id].sendUserState(mumlib::UserState::SELF_MUTE, false);
  319. break;
  320. case '0':
  321. // play menu
  322. this->playAudioFile(communicator.file_menu);
  323. break;
  324. default:
  325. communicator.logger.notice("Unsupported DTMF digit '%s' in state STAR", prm.digit.c_str());
  326. }
  327. /*
  328. * In any case, switch back to root after one digit
  329. */
  330. communicator.dtmf_mode = DTMF_MODE_ROOT;
  331. break;
  332. default:
  333. communicator.logger.notice("Unexpected DTMF '%s' in unknown state '%d'", prm.digit.c_str(),
  334. communicator.dtmf_mode);
  335. }
  336. }
  337. void _Account::onRegState(pj::OnRegStateParam &prm) {
  338. pj::AccountInfo ai = getInfo();
  339. communicator.logger << log4cpp::Priority::INFO
  340. << (ai.regIsActive ? "Register:" : "Unregister:") << " code=" << prm.code;
  341. }
  342. void _Account::onIncomingCall(pj::OnIncomingCallParam &iprm) {
  343. auto *call = new _Call(communicator, *this, iprm.callId);
  344. string uri = call->getInfo().remoteUri;
  345. communicator.logger.info("Incoming call from %s.", uri.c_str());
  346. pj::CallOpParam param;
  347. if (communicator.uriValidator.validateUri(uri)) {
  348. if (available) {
  349. param.statusCode = PJSIP_SC_OK;
  350. available = false;
  351. } else {
  352. /*
  353. * EXPERIMENT WITH MULTI-LINE
  354. */
  355. communicator.logger.info("MULTI-LINE Incoming call from %s.", uri.c_str());
  356. param.statusCode = PJSIP_SC_OK;
  357. available = false;
  358. //param.statusCode = PJSIP_SC_BUSY_EVERYWHERE;
  359. }
  360. call->answer(param);
  361. } else {
  362. communicator.logger.warn("Refusing call from %s.", uri.c_str());
  363. param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
  364. call->hangup(param);
  365. }
  366. }
  367. }
  368. sip::PjsuaCommunicator::PjsuaCommunicator(IncomingConnectionValidator &validator, int frameTimeLength, int maxCalls)
  369. : logger(log4cpp::Category::getInstance("SipCommunicator")),
  370. pjsuaLogger(log4cpp::Category::getInstance("Pjsua")),
  371. uriValidator(validator) {
  372. logWriter.reset(new sip::_LogWriter(pjsuaLogger));
  373. endpoint.libCreate();
  374. pj::EpConfig endpointConfig;
  375. endpointConfig.uaConfig.userAgent = "Mumsi Mumble-SIP gateway";
  376. endpointConfig.uaConfig.maxCalls = maxCalls;
  377. endpointConfig.logConfig.writer = logWriter.get();
  378. endpointConfig.logConfig.level = 5;
  379. endpointConfig.medConfig.noVad = true;
  380. endpoint.libInit(endpointConfig);
  381. for(int i=0; i<maxCalls; ++i) {
  382. calls[i].index = i;
  383. pj_caching_pool_init(&(calls[i].cachingPool), &pj_pool_factory_default_policy, 0);
  384. calls[i].mixer.reset(new mixer::AudioFramesMixer(calls[i].cachingPool.factory));
  385. calls[i].media.reset(new _MumlibAudioMedia(i, *this, frameTimeLength));
  386. }
  387. logger.info("Created Pjsua communicator with frame length %d ms.", frameTimeLength);
  388. }
  389. void sip::PjsuaCommunicator::connect(
  390. std::string host,
  391. std::string user,
  392. std::string password,
  393. unsigned int port) {
  394. pj::TransportConfig transportConfig;
  395. transportConfig.port = port;
  396. endpoint.transportCreate(PJSIP_TRANSPORT_UDP, transportConfig); // todo try catch
  397. endpoint.libStart();
  398. pj_status_t status = pjsua_set_null_snd_dev();
  399. if (status != PJ_SUCCESS) {
  400. throw sip::Exception("error in pjsua_set_null_std_dev()", status);
  401. }
  402. registerAccount(host, user, password);
  403. }
  404. sip::PjsuaCommunicator::~PjsuaCommunicator() {
  405. endpoint.libDestroy();
  406. }
  407. void sip::PjsuaCommunicator::sendPcmSamples(int callId, int sessionId, int sequenceNumber, int16_t *samples, unsigned int length) {
  408. calls[callId].mixer->addFrameToBuffer(sessionId, sequenceNumber, samples, length);
  409. }
  410. pj_status_t sip::PjsuaCommunicator::mediaPortGetFrame(pjmedia_port *port, pjmedia_frame *frame) {
  411. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  412. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  413. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&(port->info));
  414. int call_id = (int) port->port_data.ldata;
  415. const int readSamples = calls[call_id].mixer->getMixedSamples(samples, count);
  416. if (readSamples < count) {
  417. pjsuaLogger.debug("Requested %d samples, available %d, filling remaining with zeros.",
  418. count, readSamples);
  419. for (int i = readSamples; i < count; ++i) {
  420. samples[i] = 0;
  421. }
  422. }
  423. return PJ_SUCCESS;
  424. }
  425. pj_status_t sip::PjsuaCommunicator::mediaPortPutFrame(pjmedia_port *port, pjmedia_frame *frame) {
  426. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  427. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
  428. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  429. int call_id = (int) port->port_data.ldata;
  430. if (count > 0) {
  431. pjsuaLogger.debug("Calling onIncomingPcmSamples with %d samples (call_id=%d).", count, call_id);
  432. this->calls[call_id].onIncomingPcmSamples(samples, count);
  433. }
  434. return PJ_SUCCESS;
  435. }
  436. void sip::PjsuaCommunicator::registerAccount(string host, string user, string password) {
  437. string uri = "sip:" + user + "@" + host;
  438. pj::AccountConfig accountConfig;
  439. accountConfig.idUri = uri;
  440. accountConfig.regConfig.registrarUri = "sip:" + host;
  441. pj::AuthCredInfo cred("digest", "*", user, 0, password);
  442. accountConfig.sipConfig.authCreds.push_back(cred);
  443. logger.info("Registering account for URI: %s.", uri.c_str());
  444. account.reset(new _Account(*this));
  445. account->create(accountConfig);
  446. }