From da3000f1adbcf3b8d55808b8f3c7b7aae99bd48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20S=C5=82omkowski?= Date: Sat, 17 Oct 2015 22:27:37 +0200 Subject: [PATCH] Add config file support. --- .gitignore | 4 +++- CMakeLists.txt | 4 +++- Configuration.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++ Configuration.hpp | 39 +++++++++++++++++++++++++++++++ MumbleCommunicator.cpp | 31 +++++++++++++++++-------- PjsuaCommunicator.cpp | 7 +++--- PjsuaCommunicator.hpp | 5 ++-- config.ini.example | 11 +++++++++ main.cpp | 26 +++++++++++++-------- 9 files changed, 153 insertions(+), 26 deletions(-) create mode 100644 Configuration.cpp create mode 100644 Configuration.hpp create mode 100644 config.ini.example diff --git a/.gitignore b/.gitignore index 249963d..417c574 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.iml -.idea/ \ No newline at end of file +.idea/ + +config.ini \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index c39653c..9f2ec93 100644 --- a/CMakeLists.txt +++ b/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 diff --git a/Configuration.cpp b/Configuration.cpp new file mode 100644 index 0000000..2702747 --- /dev/null +++ b/Configuration.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +#include "Configuration.hpp" + +using namespace config; + +namespace config { + struct ConfigurationImpl { + boost::property_tree::ptree ptree; + }; + + template + TYPE get(boost::property_tree::ptree tree, const std::string &property) { + try { + return tree.get(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 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(impl->ptree, property); +} + +bool config::Configuration::getBool(const std::string &property) { + return get(impl->ptree, property); +} + +std::string config::Configuration::getString(const std::string &property) { + return get(impl->ptree, property); +} + + diff --git a/Configuration.hpp b/Configuration.hpp new file mode 100644 index 0000000..2c5ec1c --- /dev/null +++ b/Configuration.hpp @@ -0,0 +1,39 @@ +#ifndef CONFIGURATION_HPP_ +#define CONFIGURATION_HPP_ + +#include +#include + +#include + +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 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_ */ diff --git a/MumbleCommunicator.cpp b/MumbleCommunicator.cpp index bfdc480..29d1245 100644 --- a/MumbleCommunicator.cpp +++ b/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(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; } } } diff --git a/PjsuaCommunicator.cpp b/PjsuaCommunicator.cpp index ef1756c..db1614d 100644 --- a/PjsuaCommunicator.cpp +++ b/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) { diff --git a/PjsuaCommunicator.hpp b/PjsuaCommunicator.hpp index f8f2b14..db1f381 100644 --- a/PjsuaCommunicator.hpp +++ b/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(); diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..7dfbfe4 --- /dev/null +++ b/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 diff --git a/main.cpp b/main.cpp index 729dc7e..988ab60 100644 --- a/main.cpp +++ b/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.");