Add config file support.

This commit is contained in:
Michał Słomkowski 2015-10-17 22:27:37 +02:00
parent 690f95820c
commit da3000f1ad
9 changed files with 153 additions and 26 deletions

4
.gitignore vendored
View File

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

View File

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

52
Configuration.cpp Normal file
View File

@ -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
Configuration.hpp Normal file
View File

@ -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_ */

View File

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

View File

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

View File

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

11
config.ini.example Normal file
View File

@ -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

View File

@ -5,29 +5,35 @@
#include "PjsuaCommunicator.hpp" #include "PjsuaCommunicator.hpp"
#include "MumbleCommunicator.hpp" #include "MumbleCommunicator.hpp"
#define SIP_DOMAIN "sip.antisip.com" #include "Configuration.hpp"
#define SIP_USER "melangtone"
#define SIP_PASSWD "b8DU9AZXbd9tVCWg"
#define MUMBLE_DOMAIN "1con.pl"
#define MUMBLE_USER "mumsi"
#define MUMBLE_PASSWD "kiwi"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout); log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
appender1->setLayout(new log4cpp::BasicLayout()); appender1->setLayout(new log4cpp::BasicLayout());
log4cpp::Category &logger = log4cpp::Category::getRoot(); log4cpp::Category &logger = log4cpp::Category::getRoot();
logger.setPriority(log4cpp::Priority::INFO); logger.setPriority(log4cpp::Priority::DEBUG);
logger.addAppender(appender1); 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::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( mumble::MumbleCommunicator mumbleCommunicator(
pjsuaCommunicator, 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."); logger.info("Application started.");