AudioFramesMixer.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include <pjmedia.h>
  3. #include <log4cpp/Category.hh>
  4. #include <boost/noncopyable.hpp>
  5. #include <mutex>
  6. #include <unordered_map>
  7. namespace mixer {
  8. constexpr int MAX_BUFFER_LENGTH = 5000;
  9. class Exception : public std::runtime_error {
  10. public:
  11. Exception(std::string title) : std::runtime_error(title) {
  12. mesg += title;
  13. }
  14. Exception(std::string title, pj_status_t status) : std::runtime_error(title) {
  15. char errorMsgBuffer[500];
  16. pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
  17. mesg += title;
  18. mesg += ": ";
  19. mesg += errorMsgBuffer;
  20. }
  21. virtual const char *what() const throw() override {
  22. return mesg.c_str();
  23. }
  24. private:
  25. std::string mesg;
  26. };
  27. class AudioFramesMixer : boost::noncopyable {
  28. public:
  29. AudioFramesMixer(pj_pool_factory &poolFactory);
  30. virtual ~AudioFramesMixer();
  31. void addFrameToBuffer(int sessionId, int sequenceNumber, int16_t *samples, int samplesLength);
  32. int getMixedSamples(int16_t *mixedSamples, int requestedLength);
  33. void clear();
  34. private:
  35. log4cpp::Category &logger;
  36. pj_pool_t *pool = nullptr;
  37. std::unordered_map<int, pjmedia_circ_buf *> buffersMap;
  38. std::mutex inBuffAccessMutex;
  39. };
  40. }