PjsuaCommunicator.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef MUMSI_PJSUACOMMUNICATOR_HPP
  2. #define MUMSI_PJSUACOMMUNICATOR_HPP
  3. #include "ISamplesBuffer.hpp"
  4. #include <pjmedia.h>
  5. #include <string>
  6. #include <stdexcept>
  7. #include <log4cpp/Category.hh>
  8. #include <sndfile.hh>
  9. #include <mutex>
  10. #include <condition_variable>
  11. namespace sip {
  12. constexpr int DEFAULT_PORT = 5060;
  13. constexpr int SAMPLING_RATE = 48000;
  14. class Exception : public std::runtime_error {
  15. public:
  16. Exception(const char *title, pj_status_t status) : std::runtime_error(title) {
  17. char errorMsgBuffer[500];
  18. pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
  19. mesg += title;
  20. mesg += ": ";
  21. mesg += errorMsgBuffer;
  22. }
  23. virtual const char *what() const throw() {
  24. return mesg.c_str();
  25. }
  26. private:
  27. std::string mesg;
  28. };
  29. inline pj_str_t toPjString(std::string str) {
  30. return pj_str(const_cast<char *>(str.c_str()));
  31. }
  32. pj_status_t MediaPort_getFrameRawCallback(pjmedia_port *port, pjmedia_frame *frame);
  33. pj_status_t MediaPort_putFrameRawCallback(pjmedia_port *port, pjmedia_frame *frame);
  34. class PjsuaCommunicator : public ISamplesBuffer {
  35. public:
  36. PjsuaCommunicator(
  37. std::string host,
  38. std::string user,
  39. std::string password,
  40. unsigned int port = DEFAULT_PORT);
  41. ~PjsuaCommunicator();
  42. virtual void pushSamples(int16_t *samples, unsigned int length);
  43. virtual unsigned int pullSamples(int16_t *samples, unsigned int length, bool waitWhenEmpty);
  44. private:
  45. log4cpp::Category &logger;
  46. log4cpp::Category &callbackLogger;
  47. pjmedia_port *mediaPort;
  48. pjmedia_circ_buf *inputBuff;
  49. pjmedia_circ_buf *outputBuff;
  50. std::mutex inBuffAccessMutex;
  51. std::mutex outBuffAccessMutex;
  52. std::condition_variable outBuffCondVar;
  53. // todo make it completely stateless
  54. pjmedia_port *createMediaPort();
  55. void registerAccount(std::string host,
  56. std::string user,
  57. std::string password);
  58. pj_status_t mediaPortGetFrame(pjmedia_frame *frame);
  59. void mediaPortPutFrame(pj_int16_t *samples, pj_size_t count);
  60. /**
  61. * For PJMEDIA implementation reasons, these callbacks have to be functions, not methods.
  62. * That is the reason to use 'friend'.
  63. */
  64. friend pj_status_t MediaPort_getFrameRawCallback(pjmedia_port *port,
  65. pjmedia_frame *frame);
  66. friend pj_status_t MediaPort_putFrameRawCallback(pjmedia_port *port,
  67. pjmedia_frame *frame);
  68. };
  69. }
  70. #endif //MUMSI_PJSUACOMMUNICATOR_HPP