main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "PjsuaCommunicator.hpp"
  2. #include "MumbleCommunicator.hpp"
  3. #include "IncomingConnectionValidator.hpp"
  4. #include "MumbleChannelJoiner.hpp"
  5. #include "Configuration.hpp"
  6. #include <log4cpp/FileAppender.hh>
  7. #include <log4cpp/OstreamAppender.hh>
  8. #include <log4cpp/PatternLayout.hh>
  9. #include <execinfo.h>
  10. /*
  11. * Code from http://stackoverflow.com/a/77336/5419223
  12. */
  13. static void sigsegv_handler(int sig) {
  14. constexpr int STACK_DEPTH = 10;
  15. void *array[STACK_DEPTH];
  16. size_t size = backtrace(array, STACK_DEPTH);
  17. fprintf(stderr, "ERROR: signal %d:\n", sig);
  18. backtrace_symbols_fd(array, size, STDERR_FILENO);
  19. exit(1);
  20. }
  21. int main(int argc, char *argv[]) {
  22. signal(SIGSEGV, sigsegv_handler);
  23. log4cpp::OstreamAppender appender("console", &std::cout);
  24. log4cpp::PatternLayout layout;
  25. layout.setConversionPattern("%d [%p] %c: %m%n");
  26. appender.setLayout(&layout);
  27. log4cpp::Category &logger = log4cpp::Category::getRoot();
  28. logger.setPriority(log4cpp::Priority::NOTICE);
  29. logger.addAppender(appender);
  30. if (argc == 1) {
  31. logger.crit("No configuration file provided. Use %s {config file}", argv[0]);
  32. std::exit(1);
  33. }
  34. config::Configuration conf(argv[1]);
  35. logger.setPriority(log4cpp::Priority::getPriorityValue(conf.getString("general.logLevel")));
  36. sip::IncomingConnectionValidator connectionValidator(conf.getString("sip.validUriExpression"));
  37. boost::asio::io_service ioService;
  38. sip::PjsuaCommunicator pjsuaCommunicator(connectionValidator, conf.getInt("sip.frameLength"));
  39. mumble::MumbleCommunicator mumbleCommunicator(ioService);
  40. mumble::MumbleChannelJoiner mumbleChannelJoiner(conf.getString("mumble.channelNameExpression"));
  41. using namespace std::placeholders;
  42. pjsuaCommunicator.onIncomingPcmSamples = std::bind(
  43. &mumble::MumbleCommunicator::sendPcmSamples,
  44. &mumbleCommunicator,
  45. _1, _2);
  46. pjsuaCommunicator.onStateChange = std::bind(
  47. &mumble::MumbleCommunicator::sendTextMessage,
  48. &mumbleCommunicator, _1);
  49. pjsuaCommunicator.onMuteDeafChange = std::bind(
  50. &mumble::MumbleCommunicator::mutedeaf,
  51. &mumbleCommunicator, _1);
  52. mumbleCommunicator.onIncomingPcmSamples = std::bind(
  53. &sip::PjsuaCommunicator::sendPcmSamples,
  54. &pjsuaCommunicator,
  55. _1, _2, _3, _4);
  56. mumbleCommunicator.onIncomingChannelState = std::bind(
  57. &mumble::MumbleChannelJoiner::checkChannel,
  58. &mumbleChannelJoiner,
  59. _1, _2);
  60. mumbleCommunicator.onServerSync = std::bind(
  61. &mumble::MumbleChannelJoiner::maybeJoinChannel,
  62. &mumbleChannelJoiner,
  63. &mumbleCommunicator);
  64. mumble::MumbleCommunicatorConfig mumbleConf;
  65. mumbleConf.host = conf.getString("mumble.host");
  66. mumbleConf.port = conf.getInt("mumble.port");
  67. mumbleConf.user = conf.getString("mumble.user");
  68. mumbleConf.password = conf.getString("mumble.password");
  69. mumbleConf.opusEncoderBitrate = conf.getInt("mumble.opusEncoderBitrate");
  70. /* default to 'false' if not found */
  71. try {
  72. mumbleConf.autodeaf = conf.getBool("mumble.autodeaf");
  73. } catch (...) {
  74. mumbleConf.autodeaf = false;
  75. }
  76. /* default to <no pin> */
  77. try {
  78. pjsuaCommunicator.pin = conf.getString("mumble.pin");
  79. } catch (...) {
  80. pjsuaCommunicator.pin = "";
  81. }
  82. mumbleCommunicator.connect(mumbleConf);
  83. pjsuaCommunicator.connect(
  84. conf.getString("sip.host"),
  85. conf.getString("sip.user"),
  86. conf.getString("sip.password"),
  87. conf.getInt("sip.port"));
  88. logger.info("Application started.");
  89. ioService.run();
  90. return 0;
  91. }