MumbleCommunicator.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // called by Mumlib when receiving audio from mumble server
  10. virtual void audio(
  11. int target,
  12. int sessionId,
  13. int sequenceNumber,
  14. int16_t *pcm_data,
  15. uint32_t pcm_data_size) override {
  16. communicator->onIncomingPcmSamples(communicator->callId, sessionId, sequenceNumber, pcm_data, pcm_data_size);
  17. }
  18. virtual void channelState(
  19. std::string name,
  20. int32_t channel_id,
  21. int32_t parent,
  22. std::string description,
  23. std::vector<uint32_t> links,
  24. std::vector<uint32_t> inks_add,
  25. std::vector<uint32_t> links_remove,
  26. bool temporary,
  27. int32_t position) override {
  28. communicator->onIncomingChannelState(name, channel_id);
  29. }
  30. virtual void serverSync(
  31. std::string welcome_text,
  32. int32_t session,
  33. int32_t max_bandwidth,
  34. int64_t permissions) override {
  35. communicator->onServerSync();
  36. };
  37. /*
  38. virtual void onUserState(
  39. int32_t session,
  40. int32_t actor,
  41. std::string name,
  42. int32_t user_id,
  43. int32_t channel_id,
  44. int32_t mute,
  45. int32_t deaf,
  46. int32_t suppress,
  47. int32_t self_mute,
  48. int32_t self_deaf,
  49. std::string comment,
  50. int32_t priority_speaker,
  51. int32_t recording
  52. ) override {
  53. communicator->onUserState();
  54. };
  55. */
  56. };
  57. }
  58. mumble::MumbleCommunicator::MumbleCommunicator(boost::asio::io_service &ioService)
  59. : ioService(ioService),
  60. logger(log4cpp::Category::getInstance("MumbleCommunicator")) {
  61. }
  62. void mumble::MumbleCommunicator::connect(MumbleCommunicatorConfig &config) {
  63. callback.reset(new MumlibCallback());
  64. mumbleConf = config;
  65. mumConfig = mumlib::MumlibConfiguration();
  66. mumConfig.opusEncoderBitrate = config.opusEncoderBitrate;
  67. mum.reset(new mumlib::Mumlib(*callback, ioService, mumConfig));
  68. callback->communicator = this;
  69. callback->mum = mum;
  70. // IMPORTANT: comment these out when experimenting with onConnect
  71. if ( ! MUM_DELAYED_CONNECT ) {
  72. mum->connect(config.host, config.port, config.user, config.password);
  73. if ( mumbleConf.autodeaf ) {
  74. mum->sendUserState(mumlib::UserState::SELF_DEAF, true);
  75. }
  76. }
  77. }
  78. void mumble::MumbleCommunicator::onConnect() {
  79. if ( MUM_DELAYED_CONNECT ) {
  80. mum->connect(mumbleConf.host, mumbleConf.port, mumbleConf.user, mumbleConf.password);
  81. }
  82. if ( mumbleConf.comment.size() > 0 ) {
  83. mum->sendUserState(mumlib::UserState::COMMENT, mumbleConf.comment);
  84. }
  85. if ( mumbleConf.autodeaf ) {
  86. mum->sendUserState(mumlib::UserState::SELF_DEAF, true);
  87. }
  88. }
  89. void mumble::MumbleCommunicator::onDisconnect() {
  90. if ( MUM_DELAYED_CONNECT ) {
  91. mum->disconnect();
  92. } else {
  93. }
  94. }
  95. void mumble::MumbleCommunicator::onCallerAuth() {
  96. //onServerSync();
  97. }
  98. void mumble::MumbleCommunicator::sendPcmSamples(int16_t *samples, unsigned int length) {
  99. mum->sendAudioData(samples, length);
  100. }
  101. mumble::MumbleCommunicator::~MumbleCommunicator() {
  102. mum->disconnect();
  103. }
  104. void mumble::MumbleCommunicator::sendTextMessage(std::string message) {
  105. mum->sendTextMessage(message);
  106. }
  107. /*
  108. void mumble::MumbleCommunicator::onUserState(
  109. int32_t session,
  110. int32_t actor,
  111. std::string name,
  112. int32_t user_id,
  113. int32_t channel_id,
  114. int32_t mute,
  115. int32_t deaf,
  116. int32_t suppress,
  117. int32_t self_mute,
  118. int32_t self_deaf,
  119. std::string comment,
  120. int32_t priority_speaker,
  121. int32_t recording) {
  122. logger::notice("Entered onUserState(...)");
  123. userState.mute = mute;
  124. userState.deaf = deaf;
  125. userState.suppress = suppress;
  126. userState.self_mute = self_mute;
  127. userState.self_deaf = self_deaf;
  128. userState.priority_speaker = priority_speaker;
  129. userState.recording = recording;
  130. }
  131. */
  132. void mumble::MumbleCommunicator::joinChannel(int channel_id) {
  133. mum->joinChannel(channel_id);
  134. if ( mumbleConf.autodeaf ) {
  135. mum->sendUserState(mumlib::UserState::SELF_DEAF, true);
  136. }
  137. }
  138. void mumble::MumbleCommunicator::sendUserState(mumlib::UserState field, bool val) {
  139. mum->sendUserState(field, val);
  140. }
  141. void mumble::MumbleCommunicator::sendUserState(mumlib::UserState field, std::string val) {
  142. mum->sendUserState(field, val);
  143. }