MumbleCommunicator.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "MumbleCommunicator.hpp"
  2. #include <cstring>
  3. #include <functional>
  4. namespace mumble {
  5. class MumlibCallback : public mumlib::BasicCallback {
  6. public:
  7. std::shared_ptr<mumlib::Mumlib> mum;
  8. MumbleCommunicator *communicator;
  9. virtual void audio(
  10. int target,
  11. int sessionId,
  12. int sequenceNumber,
  13. int16_t *pcm_data,
  14. uint32_t pcm_data_size) override {
  15. communicator->onIncomingPcmSamples(sessionId, sequenceNumber, pcm_data, pcm_data_size);
  16. }
  17. virtual void channelState(
  18. std::string name,
  19. int32_t channel_id,
  20. int32_t parent,
  21. std::string description,
  22. std::vector<uint32_t> links,
  23. std::vector<uint32_t> inks_add,
  24. std::vector<uint32_t> links_remove,
  25. bool temporary,
  26. int32_t position) override {
  27. communicator->onIncomingChannelState(name, channel_id);
  28. }
  29. virtual void serverSync(
  30. std::string welcome_text,
  31. int32_t session,
  32. int32_t max_bandwidth,
  33. int64_t permissions) override {
  34. communicator->onServerSync();
  35. };
  36. };
  37. }
  38. mumble::MumbleCommunicator::MumbleCommunicator(boost::asio::io_service &ioService)
  39. : ioService(ioService),
  40. logger(log4cpp::Category::getInstance("MumbleCommunicator")) {
  41. }
  42. void mumble::MumbleCommunicator::connect(MumbleCommunicatorConfig &config) {
  43. callback.reset(new MumlibCallback());
  44. mumbleConf = config;
  45. mumConfig = mumlib::MumlibConfiguration();
  46. mumConfig.opusEncoderBitrate = config.opusEncoderBitrate;
  47. mum.reset(new mumlib::Mumlib(*callback, ioService, mumConfig));
  48. callback->communicator = this;
  49. callback->mum = mum;
  50. mum->connect(config.host, config.port, config.user, config.password);
  51. mum->self_deaf(1);
  52. }
  53. void mumble::MumbleCommunicator::sendPcmSamples(int16_t *samples, unsigned int length) {
  54. mum->sendAudioData(samples, length);
  55. }
  56. mumble::MumbleCommunicator::~MumbleCommunicator() {
  57. mum->disconnect();
  58. }
  59. void mumble::MumbleCommunicator::sendTextMessage(std::string message) {
  60. mum->sendTextMessage(message);
  61. }
  62. void mumble::MumbleCommunicator::joinChannel(int channel_id) {
  63. mum->joinChannel(channel_id);
  64. if ( mumbleConf.autodeaf ) {
  65. //mum->self_mute(1);
  66. mum->self_deaf(1);
  67. }
  68. }
  69. void mumble::MumbleCommunicator::mutedeaf(int status) {
  70. if ( mumbleConf.autodeaf ) {
  71. if ( status ) {
  72. mum->self_deaf(status);
  73. } else {
  74. mum->self_mute(status);
  75. }
  76. }
  77. }