MumbleCommunicator.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <cstring>
  2. #include <functional>
  3. #include "MumbleCommunicator.hpp"
  4. void mumble::MumbleCommunicator::receiveAudioFrameCallback(uint8_t *audio_data, uint32_t audio_data_size) {
  5. int dataPointer = 1;
  6. opus_int16 pcmData[1024];
  7. if (audio_data[0] == 0x80) {
  8. int64_t sessionId;
  9. int64_t sequenceNumber;
  10. int64_t opusDataLength;
  11. bool lastPacket;
  12. dataPointer += mumble_parse_variant(&sessionId, &audio_data[dataPointer]);
  13. dataPointer += mumble_parse_variant(&sequenceNumber, &audio_data[dataPointer]);
  14. dataPointer += mumble_parse_variant(&opusDataLength, &audio_data[dataPointer]);
  15. lastPacket = (opusDataLength & 0x2000) != 0;
  16. opusDataLength = opusDataLength & 0x1fff;
  17. unsigned int iAudioBufferSize;
  18. unsigned int iFrameSize = mumble::SAMPLE_RATE / 100;
  19. iAudioBufferSize = iFrameSize;
  20. iAudioBufferSize *= 12;
  21. int decodedSamples = opus_decode(opusDecoder,
  22. reinterpret_cast<const unsigned char *>(&audio_data[dataPointer]),
  23. opusDataLength,
  24. pcmData,
  25. iAudioBufferSize,
  26. 0);
  27. fileHandle.write(pcmData, decodedSamples);
  28. logger.debug("Received %d bytes of Opus data (seq %ld), decoded to %d bytes. Push it to outputQueue.",
  29. opusDataLength, sequenceNumber, decodedSamples);
  30. samplesBuffer.pushSamples(pcmData, decodedSamples);
  31. } else {
  32. logger.warn("Received %d bytes of non-recognisable audio data.", audio_data_size);
  33. }
  34. }
  35. static void mumble_audio_callback(uint8_t *audio_data, uint32_t audio_data_size, void *userData) {
  36. mumble::MumbleCommunicator *mumbleCommunicator = static_cast<mumble::MumbleCommunicator *>(userData);
  37. mumbleCommunicator->receiveAudioFrameCallback(audio_data, audio_data_size);
  38. }
  39. static void mumble_serversync_callback(char *welcome_text,
  40. int32_t session,
  41. int32_t max_bandwidth,
  42. int64_t permissions,
  43. void *usterData) {
  44. printf("%s\n", welcome_text);
  45. }
  46. static int verify_cert(uint8_t *, uint32_t) {
  47. // Accept every cert
  48. return 1;
  49. }
  50. mumble::MumbleCommunicator::MumbleCommunicator(
  51. ISamplesBuffer &samplesBuffer,
  52. std::string user,
  53. std::string password,
  54. std::string host,
  55. int port) : samplesBuffer(samplesBuffer),
  56. outgoingAudioSequenceNumber(1),
  57. logger(log4cpp::Category::getInstance("MumbleCommunicator")) {
  58. quit = false;
  59. opusDecoder = opus_decoder_create(SAMPLE_RATE, 1, nullptr); //todo grab error
  60. opusEncoder = opus_encoder_create(SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, nullptr);
  61. opus_encoder_ctl(opusEncoder, OPUS_SET_VBR(0));
  62. fileHandle = SndfileHandle("capture_mumble.wav", SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, SAMPLE_RATE);
  63. struct mumble_config config;
  64. std::memset(&config, 0, sizeof(config));
  65. config.user_data = this;
  66. config.size = sizeof(config);
  67. config.host = const_cast<char *>(host.c_str());
  68. if (port > 0) {
  69. config.port = const_cast<char *>(std::to_string(port).c_str());
  70. }
  71. config.server_password = const_cast<char *>(password.c_str());
  72. config.username = const_cast<char *>(user.c_str());
  73. config.user_cert_filename = nullptr;
  74. config.user_privkey_filename = nullptr;
  75. config.ssl_verification_callback = verify_cert;
  76. config.audio_callback = mumble_audio_callback;
  77. config.serversync_callback = mumble_serversync_callback;
  78. mumble = mumble_connect(nullptr, &config);
  79. if (mumble == nullptr) {
  80. throw mumble::Exception("couldn't establish mumble connection");
  81. }
  82. }
  83. mumble::MumbleCommunicator::~MumbleCommunicator() {
  84. mumble_close(mumble);
  85. }
  86. void mumble::MumbleCommunicator::loop() {
  87. senderThread.reset(new std::thread(&MumbleCommunicator::senderThreadFunction, this));
  88. while (!quit) {
  89. int status = mumble_tick(mumble);
  90. logger.debug("tick");
  91. if (status < 0) {
  92. throw mumble::Exception("mumble_tick status " + status);
  93. }
  94. //todo Other processing here?
  95. }
  96. }
  97. void mumble::MumbleCommunicator::senderThreadFunction() {
  98. while (!quit) {
  99. opus_int16 pcmData[1024];
  100. unsigned char outputBuffer[1024];
  101. int pcmLength = samplesBuffer.pullSamples(pcmData, 960, true);
  102. logger.debug("Pop %d samples from inputQueue.", pcmLength);
  103. int encodedSamples = opus_encode(opusEncoder, pcmData, pcmLength, outputBuffer, sizeof(outputBuffer));
  104. if (encodedSamples < 1) {
  105. logger.warn("opus_encode returned %d: %s", encodedSamples, opus_strerror(encodedSamples));
  106. } else {
  107. // logger.debug("Sending %d bytes of Opus audio data (seq %d).", encodedSamples,
  108. // outgoingAudioSequenceNumber);
  109. //
  110. // //todo to powinno dać się bezpiecznie wykonać w osobnym wątku
  111. // mumble_send_audio_data(mumble, outgoingAudioSequenceNumber, outputBuffer, encodedSamples);
  112. //
  113. // outgoingAudioSequenceNumber += 1;
  114. }
  115. }
  116. }