PjsuaCommunicator.hpp 2.5 KB

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