PjsuaCommunicator.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. enum dtmf_modes_t {DTMF_MODE_UNAUTH, DTMF_MODE_ROOT, DTMF_MODE_STAR};
  15. namespace sip {
  16. constexpr int DEFAULT_PORT = 5060;
  17. constexpr int SAMPLING_RATE = 48000;
  18. constexpr int MAX_CALLER_PIN_LEN = 64;
  19. constexpr int MAX_PIN_FAILS = 2;
  20. class Exception : public std::runtime_error {
  21. public:
  22. Exception(const char *title) : std::runtime_error(title) {
  23. mesg += title;
  24. }
  25. Exception(std::string title) : std::runtime_error(title) {
  26. mesg += title;
  27. }
  28. Exception(const char *title, pj_status_t status) : std::runtime_error(title) {
  29. char errorMsgBuffer[500];
  30. pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
  31. mesg += title;
  32. mesg += ": ";
  33. mesg += errorMsgBuffer;
  34. }
  35. virtual const char *what() const throw() override {
  36. return mesg.c_str();
  37. }
  38. private:
  39. std::string mesg;
  40. };
  41. class _LogWriter;
  42. class _Account;
  43. class _Call;
  44. class _MumlibAudioMedia;
  45. class PjsuaCommunicator : boost::noncopyable {
  46. public:
  47. PjsuaCommunicator(IncomingConnectionValidator &validator, int frameTimeLength);
  48. void connect(
  49. std::string host,
  50. std::string user,
  51. std::string password,
  52. unsigned int port = DEFAULT_PORT);
  53. virtual ~PjsuaCommunicator();
  54. void sendPcmSamples(
  55. int sessionId,
  56. int sequenceNumber,
  57. int16_t *samples,
  58. unsigned int length);
  59. // config params we get from config.ini
  60. std::string caller_pin;
  61. std::string file_welcome;
  62. std::string file_prompt_pin;
  63. std::string file_entering_channel;
  64. std::string file_announce_new_caller;
  65. std::string file_invalid_pin;
  66. std::string file_goodbye;
  67. std::string file_mute_on;
  68. std::string file_mute_off;
  69. std::string file_menu;
  70. // TODO: move these to private?
  71. std::string got_dtmf;
  72. dtmf_modes_t dtmf_mode = DTMF_MODE_ROOT;
  73. int pin_fails = 0;
  74. std::function<void(int16_t *, int)> onIncomingPcmSamples;
  75. std::function<void(std::string)> onStateChange;
  76. std::function<void(int)> onMuteDeafChange;
  77. std::function<void(int)> onMuteChange;
  78. pj_status_t mediaPortGetFrame(pjmedia_port *port, pjmedia_frame *frame);
  79. pj_status_t mediaPortPutFrame(pjmedia_port *port, pjmedia_frame *frame);
  80. private:
  81. log4cpp::Category &logger;
  82. log4cpp::Category &pjsuaLogger;
  83. std::unique_ptr<mixer::AudioFramesMixer> mixer;
  84. std::unique_ptr<_LogWriter> logWriter;
  85. std::unique_ptr<_Account> account;
  86. std::unique_ptr<_MumlibAudioMedia> media;
  87. pj_caching_pool cachingPool;
  88. pj::Endpoint endpoint;
  89. IncomingConnectionValidator &uriValidator;
  90. void registerAccount(std::string host,
  91. std::string user,
  92. std::string password);
  93. friend class _Call;
  94. friend class _Account;
  95. friend class _MumlibAudioMedia;
  96. };
  97. }