PjsuaCommunicator.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "PjsuaCommunicator.hpp"
  2. #include <pjlib.h>
  3. #include <pjsua-lib/pjsua.h>
  4. #include <functional>
  5. #include <cstring>
  6. //todo wywalić
  7. #define THIS_FILE "mumsi"
  8. using namespace std;
  9. /**
  10. * This is global, because there's no way to pass it's value to onCallMediaState callback.
  11. */
  12. static int mediaPortSlot;
  13. static void onCallMediaState(pjsua_call_id call_id) {
  14. pjsua_call_info ci;
  15. pjsua_call_get_info(call_id, &ci);
  16. if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
  17. pjsua_conf_connect(ci.conf_slot, mediaPortSlot);
  18. pjsua_conf_connect(mediaPortSlot, ci.conf_slot);
  19. }
  20. }
  21. static void onIncomingCall(pjsua_acc_id acc_id,
  22. pjsua_call_id call_id,
  23. pjsip_rx_data *rdata) {
  24. pjsua_call_info ci;
  25. PJ_UNUSED_ARG(acc_id);
  26. PJ_UNUSED_ARG(rdata);
  27. pjsua_call_get_info(call_id, &ci);
  28. PJ_LOG(3, (THIS_FILE, "Incoming call from %.*s!!",
  29. (int) ci.remote_info.slen,
  30. ci.remote_info.ptr));
  31. /* Automatically answer incoming calls with 200/OK */
  32. pjsua_call_answer(call_id, 200, NULL, NULL);
  33. }
  34. static void onCallState(pjsua_call_id call_id,
  35. pjsip_event *e) {
  36. pjsua_call_info ci;
  37. PJ_UNUSED_ARG(e);
  38. pjsua_call_get_info(call_id, &ci);
  39. PJ_LOG(3, (THIS_FILE, "Call %d state=%.*s", call_id,
  40. (int) ci.state_text.slen,
  41. ci.state_text.ptr));
  42. }
  43. sip::PjsuaCommunicator::PjsuaCommunicator(
  44. std::string host,
  45. std::string user,
  46. std::string password,
  47. unsigned int port) : logger(log4cpp::Category::getInstance("SipCommunicator")),
  48. callbackLogger(log4cpp::Category::getInstance("SipCommunicatorCallback")) {
  49. pj_status_t status;
  50. status = pjsua_create();
  51. if (status != PJ_SUCCESS) {
  52. throw sip::Exception("Error in pjsua_create()", status);
  53. }
  54. pjsua_config generalConfig;
  55. pjsua_config_default(&generalConfig);
  56. generalConfig.user_agent = toPjString("Mumsi Mumble-SIP Bridge");
  57. generalConfig.max_calls = 1;
  58. generalConfig.cb.on_incoming_call = &onIncomingCall;
  59. generalConfig.cb.on_call_media_state = &onCallMediaState;
  60. generalConfig.cb.on_call_state = &onCallState;
  61. //todo zrobić coś z logami
  62. pjsua_logging_config logConfig;
  63. pjsua_logging_config_default(&logConfig);
  64. logConfig.console_level = 4;
  65. status = pjsua_init(&generalConfig, &logConfig, NULL);
  66. if (status != PJ_SUCCESS) {
  67. throw sip::Exception("Error in pjsua_init()", status);
  68. }
  69. pjsua_set_null_snd_dev();
  70. /* Add UDP transport. */
  71. pjsua_transport_config transportConfig;
  72. pjsua_transport_config_default(&transportConfig);
  73. transportConfig.port = port;
  74. status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &transportConfig, NULL);
  75. if (status != PJ_SUCCESS) {
  76. throw sip::Exception("Error creating transport", status);
  77. }
  78. pj_caching_pool cachingPool;
  79. pj_caching_pool_init(&cachingPool, &pj_pool_factory_default_policy, 0);
  80. pj_pool_t *pool = pj_pool_create(&cachingPool.factory, "wav", 32768, 8192, nullptr);
  81. // create circular buffers
  82. pjmedia_circ_buf_create(pool, 960 * 10, &inputBuff);
  83. pjmedia_circ_buf_create(pool, 960 * 10, &outputBuff);
  84. mediaPort = createMediaPort();
  85. pjsua_conf_add_port(pool, mediaPort, &mediaPortSlot);
  86. /* Initialization is done, now start sip */
  87. status = pjsua_start();
  88. if (status != PJ_SUCCESS) {
  89. throw sip::Exception("Error starting sip", status);
  90. }
  91. registerAccount(host, user, password);
  92. }
  93. sip::PjsuaCommunicator::~PjsuaCommunicator() {
  94. pjsua_destroy();
  95. }
  96. pjmedia_port *sip::PjsuaCommunicator::createMediaPort() {
  97. pjmedia_port *mp = new pjmedia_port();
  98. pj_str_t name = toPjString("Pjsuamp");
  99. pj_status_t status = pjmedia_port_info_init(&(mp->info),
  100. &name,
  101. PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'),
  102. SAMPLING_RATE,
  103. 1,
  104. 16,
  105. SAMPLING_RATE * 20 / 1000); // todo recalculate to match mumble specs
  106. if (status != PJ_SUCCESS) {
  107. throw sip::Exception("error while calling pjmedia_port_info_init().", status);
  108. }
  109. mp->get_frame = &MediaPort_getFrameRawCallback;
  110. mp->put_frame = &MediaPort_putFrameRawCallback;
  111. mp->port_data.pdata = this;
  112. return mp;
  113. }
  114. pj_status_t sip::MediaPort_getFrameRawCallback(pjmedia_port *port,
  115. pjmedia_frame *frame) {
  116. PjsuaCommunicator *communicator = static_cast<PjsuaCommunicator *>(port->port_data.pdata);
  117. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  118. return communicator->mediaPortGetFrame(frame);
  119. }
  120. pj_status_t sip::MediaPort_putFrameRawCallback(pjmedia_port *port,
  121. pjmedia_frame *frame) {
  122. PjsuaCommunicator *communicator = static_cast<PjsuaCommunicator *>(port->port_data.pdata);
  123. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  124. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
  125. frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
  126. communicator->mediaPortPutFrame(samples, count);
  127. return PJ_SUCCESS;
  128. }
  129. pj_status_t sip::PjsuaCommunicator::mediaPortGetFrame(pjmedia_frame *frame) {
  130. std::unique_lock<std::mutex> lock(inBuffAccessMutex);
  131. pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
  132. pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&mediaPort->info);
  133. pj_size_t availableSamples = pjmedia_circ_buf_get_len(inputBuff);
  134. const int samplesToRead = std::min(count, availableSamples);
  135. callbackLogger.debug("Pulling %d samples from in-buff.", samplesToRead);
  136. pjmedia_circ_buf_read(inputBuff, samples, samplesToRead);
  137. if (availableSamples < count) {
  138. callbackLogger.debug("Requested %d samples, available %d, filling remaining with zeros.", count,
  139. availableSamples);
  140. for (int i = samplesToRead; i < count; ++i) {
  141. samples[i] = 0;
  142. }
  143. }
  144. return PJ_SUCCESS;
  145. }
  146. void sip::PjsuaCommunicator::mediaPortPutFrame(pj_int16_t *samples, pj_size_t count) {
  147. std::unique_lock<std::mutex> lock(outBuffAccessMutex);
  148. callbackLogger.debug("Pushing %d samples to out-buff.", count);
  149. pjmedia_circ_buf_write(outputBuff, samples, count);
  150. lock.unlock();
  151. outBuffCondVar.notify_all();
  152. }
  153. void sip::PjsuaCommunicator::registerAccount(string host, string user, string password) {
  154. pjsua_acc_config accConfig;
  155. pjsua_acc_config_default(&accConfig);
  156. accConfig.id = toPjString(string("sip:") + user + "@" + host);
  157. accConfig.reg_uri = toPjString(string("sip:") + host);
  158. accConfig.cred_count = 1;
  159. accConfig.cred_info[0].realm = toPjString(host);
  160. accConfig.cred_info[0].scheme = toPjString("digest");
  161. accConfig.cred_info[0].username = toPjString(user);
  162. accConfig.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
  163. accConfig.cred_info[0].data = toPjString(password);
  164. pjsua_acc_id acc_id;
  165. pj_status_t status = pjsua_acc_add(&accConfig, PJ_TRUE, &acc_id);
  166. if (status != PJ_SUCCESS) {
  167. throw sip::Exception("failed to register account", status);
  168. }
  169. }
  170. void sip::PjsuaCommunicator::pushSamples(int16_t *samples, unsigned int length) {
  171. std::unique_lock<std::mutex> lock(inBuffAccessMutex);
  172. callbackLogger.debug("Pushing %d samples to in-buff.", length);
  173. pjmedia_circ_buf_write(inputBuff, samples, length);
  174. }
  175. unsigned int sip::PjsuaCommunicator::pullSamples(int16_t *samples, unsigned int length, bool waitWhenEmpty) {
  176. std::unique_lock<std::mutex> lock(outBuffAccessMutex);
  177. unsigned int availableSamples;
  178. while ((availableSamples = pjmedia_circ_buf_get_len(inputBuff)) < length) {
  179. callbackLogger.debug("Not enough samples in buffer: %d, requested %d. Waiting.", availableSamples, length);
  180. outBuffCondVar.wait(lock);
  181. }
  182. const int samplesToRead = std::min(length, availableSamples);
  183. callbackLogger.debug("Pulling %d samples from out-buff.", samplesToRead);
  184. pjmedia_circ_buf_read(inputBuff, samples, samplesToRead);
  185. return samplesToRead;
  186. }