PjsuaCommunicator.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #include "IncomingConnectionValidator.hpp"
  3. #include "AudioFramesMixer.hpp"
  4. #include <pjmedia.h>
  5. #include <pjsua-lib/pjsua.h>
  6. #include <pjsua2.hpp>
  7. #undef isblank
  8. #include <log4cpp/Category.hh>
  9. #include <boost/noncopyable.hpp>
  10. #include <string>
  11. #include <stdexcept>
  12. #include <climits>
  13. #include <bits/unique_ptr.h>
  14. // for userState enum
  15. #include <mumlib.hpp>
  16. #include "main.hpp"
  17. enum dtmf_modes_t {DTMF_MODE_UNAUTH, DTMF_MODE_ROOT, DTMF_MODE_STAR};
  18. namespace sip {
  19. constexpr int DEFAULT_PORT = 5060;
  20. constexpr int SAMPLING_RATE = 48000;
  21. constexpr int MAX_CALLER_PIN_LEN = 64;
  22. constexpr int MAX_PIN_FAILS = 2;
  23. class Exception : public std::runtime_error {
  24. public:
  25. Exception(const char *title) : std::runtime_error(title) {
  26. mesg += title;
  27. }
  28. Exception(std::string title) : std::runtime_error(title) {
  29. mesg += title;
  30. }
  31. Exception(const char *title, pj_status_t status) : std::runtime_error(title) {
  32. char errorMsgBuffer[500];
  33. pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
  34. mesg += title;
  35. mesg += ": ";
  36. mesg += errorMsgBuffer;
  37. }
  38. virtual const char *what() const throw() override {
  39. return mesg.c_str();
  40. }
  41. private:
  42. std::string mesg;
  43. };
  44. class _LogWriter;
  45. class _Account;
  46. class _Call;
  47. class _MumlibAudioMedia;
  48. struct call {
  49. unsigned index;
  50. std::unique_ptr<mixer::AudioFramesMixer> mixer;
  51. std::unique_ptr<sip::_MumlibAudioMedia> media;
  52. pj_caching_pool cachingPool;
  53. std::function<void(std::string)> onStateChange;
  54. std::function<void(int16_t *, int)> onIncomingPcmSamples;
  55. std::function<void(int)> onMuteDeafChange;
  56. std::function<void(mumlib::UserState field, bool val)> sendUserState;
  57. std::function<void(mumlib::UserState field, std::string val)> sendUserStateStr;
  58. std::function<void()> onConnect;
  59. std::function<void()> onDisconnect;
  60. std::function<void()> onCallerAuth;
  61. std::function<void()> joinAuthChannel; // DEPRECATE ?
  62. std::function<void(std::string channelNameRegex)> joinOtherChannel;
  63. std::function<void()> joinDefaultChannel;
  64. };
  65. class PjsuaCommunicator : boost::noncopyable {
  66. public:
  67. PjsuaCommunicator(IncomingConnectionValidator &validator, int frameTimeLength, int maxCalls);
  68. void connect(
  69. std::string host,
  70. std::string user,
  71. std::string password,
  72. unsigned int port = DEFAULT_PORT);
  73. virtual ~PjsuaCommunicator();
  74. void sendPcmSamples(
  75. int callId,
  76. int sessionId,
  77. int sequenceNumber,
  78. int16_t *samples,
  79. unsigned int length);
  80. // config params we get from config.ini
  81. std::string caller_pin;
  82. std::string file_welcome;
  83. std::string file_prompt_pin;
  84. std::string file_entering_channel;
  85. std::string file_announce_new_caller;
  86. std::string file_invalid_pin;
  87. std::string file_goodbye;
  88. std::string file_mute_on;
  89. std::string file_mute_off;
  90. std::string file_menu;
  91. int max_calls;
  92. // TODO: move these to private?
  93. std::string got_dtmf;
  94. dtmf_modes_t dtmf_mode = DTMF_MODE_ROOT;
  95. int pin_fails = 0;
  96. pj_status_t mediaPortGetFrame(pjmedia_port *port, pjmedia_frame *frame);
  97. pj_status_t mediaPortPutFrame(pjmedia_port *port, pjmedia_frame *frame);
  98. call calls[MY_MAX_CALLS];
  99. std::unordered_map<std::string, std::string> pins;
  100. private:
  101. log4cpp::Category &logger;
  102. log4cpp::Category &pjsuaLogger;
  103. std::unique_ptr<_LogWriter> logWriter;
  104. std::unique_ptr<_Account> account;
  105. pj::Endpoint endpoint;
  106. IncomingConnectionValidator &uriValidator;
  107. void registerAccount(std::string host,
  108. std::string user,
  109. std::string password);
  110. friend class _Call;
  111. friend class _Account;
  112. friend class _MumlibAudioMedia;
  113. };
  114. }