websocketsrv.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaomingwork */
  6. // websocket server for asr engine
  7. // take some ideas from https://github.com/k2-fsa/sherpa-onnx
  8. // online-websocket-server-impl.cc, thanks. The websocket server has two threads
  9. // pools, one for handle network data and one for asr decoder.
  10. // now only support offline engine.
  11. #ifndef WEBSOCKETSRV_SERVER_H_
  12. #define WEBSOCKETSRV_SERVER_H_
  13. #include <iostream>
  14. #include <map>
  15. #include <memory>
  16. #include <string>
  17. #include <thread>
  18. #include <utility>
  19. #define ASIO_STANDALONE 1 // not boost
  20. #include <glog/logging.h>
  21. #include <fstream>
  22. #include <functional>
  23. #include <websocketpp/common/thread.hpp>
  24. #include <websocketpp/config/asio_no_tls.hpp>
  25. #include <websocketpp/server.hpp>
  26. #include "asio.hpp"
  27. #include "com-define.h"
  28. #include "funasrruntime.h"
  29. #include "nlohmann/json.hpp"
  30. #include "tclap/CmdLine.h"
  31. typedef websocketpp::server<websocketpp::config::asio> server;
  32. typedef server::message_ptr message_ptr;
  33. using websocketpp::lib::bind;
  34. using websocketpp::lib::placeholders::_1;
  35. using websocketpp::lib::placeholders::_2;
  36. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  37. typedef websocketpp::lib::unique_lock<websocketpp::lib::mutex> unique_lock;
  38. typedef struct {
  39. std::string msg;
  40. float snippet_time;
  41. } FUNASR_RECOG_RESULT;
  42. typedef struct {
  43. nlohmann::json msg;
  44. std::shared_ptr<std::vector<char>> samples;
  45. } FUNASR_MESSAGE;
  46. class WebSocketServer {
  47. public:
  48. WebSocketServer(asio::io_context& io_decoder, server* server_)
  49. : io_decoder_(io_decoder), server_(server_) {
  50. // set message handle
  51. server_->set_message_handler(
  52. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  53. on_message(hdl, msg);
  54. });
  55. // set open handle
  56. server_->set_open_handler(
  57. [this](websocketpp::connection_hdl hdl) { on_open(hdl); });
  58. // set close handle
  59. server_->set_close_handler(
  60. [this](websocketpp::connection_hdl hdl) { on_close(hdl); });
  61. // begin accept
  62. server_->start_accept();
  63. // not print log
  64. server_->clear_access_channels(websocketpp::log::alevel::all);
  65. }
  66. void do_decoder(const std::vector<char>& buffer,
  67. websocketpp::connection_hdl& hdl, const nlohmann::json& msg);
  68. void initAsr(std::map<std::string, std::string>& model_path, int thread_num);
  69. void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
  70. void on_open(websocketpp::connection_hdl hdl);
  71. void on_close(websocketpp::connection_hdl hdl);
  72. private:
  73. void check_and_clean_connection();
  74. asio::io_context& io_decoder_; // threads for asr decoder
  75. // std::ofstream fout;
  76. FUNASR_HANDLE asr_hanlde; // asr engine handle
  77. bool isonline = false; // online or offline engine, now only support offline
  78. server* server_; // websocket server
  79. // use map to keep the received samples data from one connection in offline
  80. // engine. if for online engline, a data struct is needed(TODO)
  81. std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,
  82. std::owner_less<websocketpp::connection_hdl>>
  83. data_map;
  84. websocketpp::lib::mutex m_lock; // mutex for sample_map
  85. };
  86. #endif // WEBSOCKETSRV_SERVER_H_