Browse Source

Add config file support.

Michał Słomkowski 8 years ago
parent
commit
da3000f1ad
9 changed files with 153 additions and 26 deletions
  1. 3 1
      .gitignore
  2. 3 1
      CMakeLists.txt
  3. 52 0
      Configuration.cpp
  4. 39 0
      Configuration.hpp
  5. 22 9
      MumbleCommunicator.cpp
  6. 4 3
      PjsuaCommunicator.cpp
  7. 3 2
      PjsuaCommunicator.hpp
  8. 11 0
      config.ini.example
  9. 16 10
      main.cpp

+ 3 - 1
.gitignore

@@ -1,2 +1,4 @@
 *.iml
-.idea/
+.idea/
+
+config.ini

+ 3 - 1
CMakeLists.txt

@@ -26,7 +26,9 @@ set(SOURCE_FILES
         PjsuaCommunicator.hpp
         MumbleCommunicator.cpp
         MumbleCommunicator.hpp
-        ISamplesBuffer.hpp)
+        ISamplesBuffer.hpp
+        Configuration.cpp
+        Configuration.hpp)
 
 #set(TEST_SOURCE_FILES
 #        SoundSampleQueueTest.cpp

+ 52 - 0
Configuration.cpp

@@ -0,0 +1,52 @@
+#include <fstream>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/ini_parser.hpp>
+
+#include "Configuration.hpp"
+
+using namespace config;
+
+namespace config {
+    struct ConfigurationImpl {
+        boost::property_tree::ptree ptree;
+    };
+
+    template<typename TYPE>
+    TYPE get(boost::property_tree::ptree tree, const std::string &property) {
+        try {
+            return tree.get<TYPE>(property);
+        } catch (boost::property_tree::ptree_bad_path) {
+            throw ConfigException(std::string(typeid(TYPE).name()) + " key \'" + property + "\' not found");
+        }
+    }
+}
+
+config::Configuration::Configuration(const std::string fileName)
+        : impl(new ConfigurationImpl()) {
+    boost::property_tree::read_ini(fileName, impl->ptree);
+}
+
+config::Configuration::Configuration(std::vector<std::string> const fileNames)
+        : impl(new ConfigurationImpl()) {
+    for (auto &name : fileNames) {
+        boost::property_tree::read_ini(name, impl->ptree);
+    }
+}
+
+config::Configuration::~Configuration() {
+    delete impl;
+}
+
+int config::Configuration::getInt(const std::string &property) {
+    return get<int>(impl->ptree, property);
+}
+
+bool config::Configuration::getBool(const std::string &property) {
+    return get<bool>(impl->ptree, property);
+}
+
+std::string config::Configuration::getString(const std::string &property) {
+    return get<std::string>(impl->ptree, property);
+}
+
+

+ 39 - 0
Configuration.hpp

@@ -0,0 +1,39 @@
+#ifndef CONFIGURATION_HPP_
+#define CONFIGURATION_HPP_
+
+#include <string>
+#include <stdexcept>
+
+#include <boost/noncopyable.hpp>
+
+namespace config {
+
+    class ConfigException : public std::runtime_error {
+    public:
+        ConfigException(const std::string &message)
+                : std::runtime_error(message) {
+        }
+    };
+
+    struct ConfigurationImpl;
+
+    class Configuration : boost::noncopyable {
+    public:
+        Configuration(const std::string fileName);
+
+        Configuration(const std::vector<std::string> fileNames);
+
+        ~Configuration();
+
+        int getInt(const std::string &property);
+
+        bool getBool(const std::string &property);
+
+        std::string getString(const std::string &property);
+
+    private:
+        ConfigurationImpl *impl;
+    };
+}
+
+#endif /* CONFIGURATION_HPP_ */

+ 22 - 9
MumbleCommunicator.cpp

@@ -57,6 +57,18 @@ static void mumble_serversync_callback(char *welcome_text,
     printf("%s\n", welcome_text);
 }
 
+void mumble_cryptsetup_callback(uint32_t key_size,
+                                uint8_t *key,
+                                uint32_t client_nonce_size,
+                                uint8_t *client_nonce,
+                                uint32_t server_nonce_size,
+                                uint8_t *server_nonce,
+                                void *userData) {
+    mumble::MumbleCommunicator *mumbleCommunicator = static_cast<mumble::MumbleCommunicator *>(userData);
+    printf("received crypto nonce\n");
+}
+
+
 static int verify_cert(uint8_t *, uint32_t) {
     // Accept every cert
     return 1;
@@ -98,6 +110,7 @@ mumble::MumbleCommunicator::MumbleCommunicator(
     config.ssl_verification_callback = verify_cert;
     config.audio_callback = mumble_audio_callback;
     config.serversync_callback = mumble_serversync_callback;
+    config.cryptsetup_callback = mumble_cryptsetup_callback;
 
     mumble = mumble_connect(nullptr, &config);
 
@@ -112,7 +125,7 @@ mumble::MumbleCommunicator::~MumbleCommunicator() {
 
 void mumble::MumbleCommunicator::loop() {
 
-    //senderThread.reset(new std::thread(&MumbleCommunicator::senderThreadFunction, this));
+    senderThread.reset(new std::thread(&MumbleCommunicator::senderThreadFunction, this));
 
     while (!quit) {
         int status = mumble_tick(mumble);
@@ -131,7 +144,7 @@ void mumble::MumbleCommunicator::senderThreadFunction() {
         opus_int16 pcmData[1024];
         unsigned char outputBuffer[1024];
 
-        int pcmLength = samplesBuffer.pullSamples(pcmData, 960, true);
+        int pcmLength = samplesBuffer.pullSamples(pcmData, 480, true);
 
         logger.debug("Pop %d samples from inputQueue.", pcmLength);
 
@@ -140,13 +153,13 @@ void mumble::MumbleCommunicator::senderThreadFunction() {
         if (encodedSamples < 1) {
             logger.warn("opus_encode returned %d: %s", encodedSamples, opus_strerror(encodedSamples));
         } else {
-//            logger.debug("Sending %d bytes of Opus audio data (seq %d).", encodedSamples,
-//                         outgoingAudioSequenceNumber);
-//
-//            //todo to powinno dać się bezpiecznie wykonać w osobnym wątku
-//            mumble_send_audio_data(mumble, outgoingAudioSequenceNumber, outputBuffer, encodedSamples);
-//
-//            outgoingAudioSequenceNumber += 1;
+            logger.debug("Sending %d bytes of Opus audio data (seq %d).", encodedSamples,
+                         outgoingAudioSequenceNumber);
+
+            //todo to powinno dać się bezpiecznie wykonać w osobnym wątku
+            mumble_send_audio_data(mumble, outgoingAudioSequenceNumber, outputBuffer, encodedSamples);
+
+            outgoingAudioSequenceNumber += 1;
         }
     }
 }

+ 4 - 3
PjsuaCommunicator.cpp

@@ -64,8 +64,9 @@ static void onCallState(pjsua_call_id call_id,
 sip::PjsuaCommunicator::PjsuaCommunicator(
         std::string host,
         std::string user,
-        std::string password) : logger(log4cpp::Category::getInstance("SipCommunicator")),
-                                callbackLogger(log4cpp::Category::getInstance("SipCommunicatorCallback")) {
+        std::string password,
+        unsigned int port) : logger(log4cpp::Category::getInstance("SipCommunicator")),
+                             callbackLogger(log4cpp::Category::getInstance("SipCommunicatorCallback")) {
 
     pj_status_t status;
 
@@ -101,7 +102,7 @@ sip::PjsuaCommunicator::PjsuaCommunicator(
     pjsua_transport_config transportConfig;
     pjsua_transport_config_default(&transportConfig);
 
-    transportConfig.port = sip::SIP_PORT;
+    transportConfig.port = port;
 
     status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &transportConfig, NULL);
     if (status != PJ_SUCCESS) {

+ 3 - 2
PjsuaCommunicator.hpp

@@ -14,7 +14,7 @@
 
 namespace sip {
 
-    constexpr int SIP_PORT = 5060;
+    constexpr int DEFAULT_PORT = 5060;
     constexpr int SAMPLING_RATE = 48000;
 
     class Exception : public std::runtime_error {
@@ -49,7 +49,8 @@ namespace sip {
         PjsuaCommunicator(
                 std::string host,
                 std::string user,
-                std::string password);
+                std::string password,
+                unsigned int port = DEFAULT_PORT);
 
         ~PjsuaCommunicator();
 

+ 11 - 0
config.ini.example

@@ -0,0 +1,11 @@
+[sip]
+host = sip.example.org
+port = 5060
+user = mumsi
+password = foobar
+
+[mumble]
+host = example.org
+port = 64738
+user = mumsi
+password = foobar

+ 16 - 10
main.cpp

@@ -5,29 +5,35 @@
 #include "PjsuaCommunicator.hpp"
 #include "MumbleCommunicator.hpp"
 
-#define SIP_DOMAIN "sip.antisip.com"
-#define SIP_USER "melangtone"
-#define SIP_PASSWD "b8DU9AZXbd9tVCWg"
-
-#define MUMBLE_DOMAIN "1con.pl"
-#define MUMBLE_USER "mumsi"
-#define MUMBLE_PASSWD "kiwi"
+#include "Configuration.hpp"
 
 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::INFO);
+    logger.setPriority(log4cpp::Priority::DEBUG);
     logger.addAppender(appender1);
 
+    if (argc == 1) {
+        logger.crit("No configuration file provided. Use %s {config file}", argv[0]);
+        return 1;
+    }
+
+    config::Configuration conf(argv[1]);
 
     sip::PjsuaCommunicator pjsuaCommunicator(
-            SIP_DOMAIN, SIP_USER, SIP_PASSWD);
+            conf.getString("sip.host"),
+            conf.getString("sip.user"),
+            conf.getString("sip.password"),
+            conf.getInt("sip.port"));
 
     mumble::MumbleCommunicator mumbleCommunicator(
             pjsuaCommunicator,
-            MUMBLE_USER, MUMBLE_PASSWD, MUMBLE_DOMAIN);
+            conf.getString("mumble.user"),
+            conf.getString("mumble.password"),
+            conf.getString("mumble.host"),
+            conf.getInt("mumble.port"));
 
     logger.info("Application started.");