mumlib_example.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "mumlib.hpp"
  2. #include "log4cpp/Category.hh"
  3. #include "log4cpp/FileAppender.hh"
  4. #include "log4cpp/OstreamAppender.hh"
  5. #include <chrono>
  6. #include <thread>
  7. #include <mumlib/Transport.hpp>
  8. class MyCallback : public mumlib::BasicCallback {
  9. public:
  10. mumlib::Mumlib *mum;
  11. virtual void audio(int target,
  12. int sessionId,
  13. int sequenceNumber,
  14. int16_t *pcm_data,
  15. uint32_t pcm_data_size) override {
  16. mum->sendAudioData(pcm_data, pcm_data_size);
  17. }
  18. virtual void textMessage(
  19. uint32_t actor,
  20. std::vector<uint32_t> session,
  21. std::vector<uint32_t> channel_id,
  22. std::vector<uint32_t> tree_id,
  23. std::string message) override {
  24. mumlib::BasicCallback::textMessage(actor, session, channel_id, tree_id, message);
  25. mum->sendTextMessage("someone said: " + message);
  26. }
  27. };
  28. int main(int argc, char *argv[]) {
  29. log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
  30. appender1->setLayout(new log4cpp::BasicLayout());
  31. log4cpp::Category &logger = log4cpp::Category::getRoot();
  32. logger.setPriority(log4cpp::Priority::NOTICE);
  33. logger.addAppender(appender1);
  34. if (argc < 3 || argc == 4 || argc > 5) {
  35. logger.crit("Usage: %s {server} {password} [{certfile} {keyfile}]", argv[0]);
  36. return 1;
  37. }
  38. MyCallback myCallback;
  39. while (true) {
  40. try {
  41. mumlib::MumlibConfiguration conf;
  42. conf.opusEncoderBitrate = 32000;
  43. if ( argc > 3 && argc <= 5 ) {
  44. conf.cert_file = argv[3];
  45. conf.privkey_file = argv[4];
  46. }
  47. mumlib::Mumlib mum(myCallback, conf);
  48. myCallback.mum = &mum;
  49. mum.connect(argv[1], 64738, "mumlib_example", argv[2]);
  50. mum.run();
  51. } catch (mumlib::TransportException &exp) {
  52. logger.error("TransportException: %s.", exp.what());
  53. logger.notice("Attempting to reconnect in 5 s.");
  54. std::this_thread::sleep_for(std::chrono::seconds(5));
  55. }
  56. }
  57. }