Browse Source

Add text message call state info.

Michał Słomkowski 8 years ago
parent
commit
058b8778cd
5 changed files with 52 additions and 12 deletions
  1. 4 0
      MumbleCommunicator.cpp
  2. 2 0
      MumbleCommunicator.hpp
  3. 35 11
      PjsuaCommunicator.cpp
  4. 6 0
      PjsuaCommunicator.hpp
  5. 5 1
      main.cpp

+ 4 - 0
MumbleCommunicator.cpp

@@ -44,3 +44,7 @@ void mumble::MumbleCommunicator::sendPcmSamples(int16_t *samples, unsigned int l
 mumble::MumbleCommunicator::~MumbleCommunicator() {
     mum->disconnect();
 }
+
+void mumble::MumbleCommunicator::sendTextMessage(std::string message) {
+    mum->sendTextMessage(message);
+}

+ 2 - 0
MumbleCommunicator.hpp

@@ -32,6 +32,8 @@ namespace mumble {
 
         virtual void sendPcmSamples(int16_t *samples, unsigned int length);
 
+        void sendTextMessage(std::string message);
+
     public:
         boost::asio::io_service &ioService;
 

+ 35 - 11
PjsuaCommunicator.cpp

@@ -3,13 +3,15 @@
 #include <pjlib.h>
 #include <pjsua-lib/pjsua.h>
 #include <boost/algorithm/string/replace.hpp>
+#include <boost/format.hpp>
 
 using namespace std;
 
 /**
- * This is global, because there's no way to pass it's value to onCallMediaState callback.
+ * These are global, because there's no way to pass it's value to onCallMediaState callback.
  */
 static int mediaPortSlot;
+static sip::PjsuaCommunicator *pjsuaCommunicator = nullptr;
 
 static log4cpp::Category &pjLogger = log4cpp::Category::getInstance("PjSip");
 
@@ -34,13 +36,19 @@ static void onIncomingCall(pjsua_acc_id acc_id,
     PJ_UNUSED_ARG(rdata);
 
     pjsua_call_get_info(call_id, &ci);
+    pjsua_call_set_user_data(call_id, pjsuaCommunicator);
 
-    pjLogger.notice("Incoming call from %s.", ci.remote_info.ptr);
+    pjLogger.info("Incoming call from %s.", ci.remote_info.ptr);
 
     /* Automatically answer incoming calls with 200/OK */
     pjsua_call_answer(call_id, 200, NULL, NULL);
 }
 
+static void onDtmfDigit(pjsua_call_id callId, int digit) {
+
+    pjLogger.notice("DTMF digit '%c' (call %d).", digit, callId);
+}
+
 static void onCallState(pjsua_call_id call_id,
                         pjsip_event *e) {
     pjsua_call_info ci;
@@ -48,7 +56,23 @@ static void onCallState(pjsua_call_id call_id,
     PJ_UNUSED_ARG(e);
 
     pjsua_call_get_info(call_id, &ci);
-    pjLogger.notice("Call %d state=%s.", call_id, ci.state_text.ptr);
+    sip::PjsuaCommunicator *communicator
+            = reinterpret_cast<sip::PjsuaCommunicator *>(pjsua_call_get_user_data(call_id));
+
+    pjLogger.info("Call %d state=%s.", call_id, ci.state_text.ptr);
+
+    string address = string(ci.remote_info.ptr);
+    address = address.substr(5, address.size() - 5 - 1);
+
+    if (ci.state == PJSIP_INV_STATE_CONFIRMED) {
+        auto msgText = "Start call from " + address + ".";
+        pjLogger.notice(msgText);
+        communicator->onStateChange(msgText);
+    } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
+        auto msgText = "End call from " + address + ".";
+        pjLogger.notice(msgText);
+        communicator->onStateChange(msgText);
+    }
 }
 
 static void pjLogToLog4CppBridgeFunction(int level, const char *data, int len) {
@@ -74,6 +98,11 @@ sip::PjsuaCommunicator::PjsuaCommunicator()
           callbackLogger(log4cpp::Category::getInstance("SipCommunicatorCallback")) {
     pj_status_t status;
 
+    if (pjsuaCommunicator != nullptr) {
+        throw sip::Exception("this is a singleton class");
+    }
+    pjsuaCommunicator = this;
+
     status = pjsua_create();
     if (status != PJ_SUCCESS) {
         throw sip::Exception("Error in pjsua_create()", status);
@@ -90,6 +119,7 @@ sip::PjsuaCommunicator::PjsuaCommunicator()
     generalConfig.max_calls = 1;
 
     generalConfig.cb.on_incoming_call = &onIncomingCall;
+    generalConfig.cb.on_dtmf_digit = &onDtmfDigit;
     generalConfig.cb.on_call_media_state = &onCallMediaState;
     generalConfig.cb.on_call_state = &onCallState;
 
@@ -161,7 +191,8 @@ pjmedia_port *sip::PjsuaCommunicator::createMediaPort() {
                                                 SAMPLING_RATE,
                                                 1,
                                                 16,
-                                                SAMPLING_RATE * 20 / 1000); // todo recalculate to match mumble specs
+                                                SAMPLING_RATE * 20 /
+                                                1000); // todo recalculate to match mumble specs
 
     if (status != PJ_SUCCESS) {
         throw sip::Exception("error while calling pjmedia_port_info_init().", status);
@@ -254,13 +285,6 @@ void sip::PjsuaCommunicator::registerAccount(string host, string user, string pa
     accConfig.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
     accConfig.cred_info[0].data = toPjString(password);
 
-    logger.error("id:%s", accConfig.id.ptr);
-    logger.error("reg_uri:%s", accConfig.reg_uri.ptr);
-    logger.error("realm:%s", accConfig.cred_info[0].realm.ptr);
-    logger.error("scheme:%s", accConfig.cred_info[0].scheme.ptr);
-    logger.error("username:%s", accConfig.cred_info[0].username.ptr);
-    logger.error("data:%s", accConfig.cred_info[0].data.ptr);
-
     pjsua_acc_id acc_id;
 
     status = pjsua_acc_add(&accConfig, PJ_TRUE, &acc_id);

+ 6 - 0
PjsuaCommunicator.hpp

@@ -18,6 +18,10 @@ namespace sip {
 
     class Exception : public std::runtime_error {
     public:
+        Exception(const char *title) : std::runtime_error(title) {
+            mesg += title;
+        }
+
         Exception(const char *title, pj_status_t status) : std::runtime_error(title) {
             char errorMsgBuffer[500];
             pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
@@ -57,6 +61,8 @@ namespace sip {
 
         virtual void sendPcmSamples(int16_t *samples, unsigned int length);
 
+        std::function<void(std::string)> onStateChange;
+
     private:
         log4cpp::Category &logger;
         log4cpp::Category &callbackLogger;

+ 5 - 1
main.cpp

@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) {
     log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
     appender1->setLayout(new log4cpp::BasicLayout());
     log4cpp::Category &logger = log4cpp::Category::getRoot();
-    logger.setPriority(log4cpp::Priority::DEBUG);
+    logger.setPriority(log4cpp::Priority::NOTICE);
     logger.addAppender(appender1);
 
     if (argc == 1) {
@@ -34,6 +34,10 @@ int main(int argc, char *argv[]) {
             &mumbleCommunicator,
             _1, _2);
 
+    pjsuaCommunicator.onStateChange = std::bind(
+            &mumble::MumbleCommunicator::sendTextMessage,
+            &mumbleCommunicator, _1);
+
     mumbleCommunicator.onIncomingPcmSamples = std::bind(
             &sip::PjsuaCommunicator::sendPcmSamples,
             &pjsuaCommunicator,