websocket-server.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 WEBSOCKET_SERVER_H_
  12. #define WEBSOCKET_SERVER_H_
  13. #include <iostream>
  14. #include <map>
  15. #include <memory>
  16. #include <string>
  17. #include <thread>
  18. #include <utility>
  19. #include <unordered_map>
  20. #define ASIO_STANDALONE 1 // not boost
  21. #include <glog/logging.h>
  22. #include "util/text-utils.h"
  23. #include <fstream>
  24. #include <functional>
  25. #include <websocketpp/common/thread.hpp>
  26. #include <websocketpp/config/asio.hpp>
  27. #include <websocketpp/server.hpp>
  28. #include "asio.hpp"
  29. #include "com-define.h"
  30. #include "funasrruntime.h"
  31. #include "nlohmann/json.hpp"
  32. #include "tclap/CmdLine.h"
  33. typedef websocketpp::server<websocketpp::config::asio> server;
  34. typedef websocketpp::server<websocketpp::config::asio_tls> wss_server;
  35. typedef server::message_ptr message_ptr;
  36. using websocketpp::lib::bind;
  37. using websocketpp::lib::placeholders::_1;
  38. using websocketpp::lib::placeholders::_2;
  39. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  40. typedef websocketpp::lib::unique_lock<websocketpp::lib::mutex> unique_lock;
  41. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
  42. context_ptr;
  43. typedef struct {
  44. std::string msg="";
  45. std::string stamp="";
  46. std::string tpass_msg="";
  47. float snippet_time=0;
  48. } FUNASR_RECOG_RESULT;
  49. typedef struct {
  50. nlohmann::json msg;
  51. std::shared_ptr<std::vector<char>> samples;
  52. std::shared_ptr<std::vector<std::vector<float>>> hotwords_embedding=NULL;
  53. std::shared_ptr<websocketpp::lib::mutex> thread_lock; // lock for each connection
  54. FUNASR_DEC_HANDLE decoder_handle=NULL;
  55. } FUNASR_MESSAGE;
  56. // See https://wiki.mozilla.org/Security/Server_Side_TLS for more details about
  57. // the TLS modes. The code below demonstrates how to implement both the modern
  58. enum tls_mode { MOZILLA_INTERMEDIATE = 1, MOZILLA_MODERN = 2 };
  59. class WebSocketServer {
  60. public:
  61. WebSocketServer(asio::io_context& io_decoder, bool is_ssl, server* server,
  62. wss_server* wss_server, std::string& s_certfile,
  63. std::string& s_keyfile)
  64. : io_decoder_(io_decoder),
  65. is_ssl(is_ssl),
  66. server_(server),
  67. wss_server_(wss_server){
  68. if (is_ssl) {
  69. std::cout << "certfile path is " << s_certfile << std::endl;
  70. wss_server->set_tls_init_handler(
  71. bind<context_ptr>(&WebSocketServer::on_tls_init, this,
  72. MOZILLA_INTERMEDIATE, ::_1, s_certfile, s_keyfile));
  73. wss_server_->set_message_handler(
  74. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  75. on_message(hdl, msg);
  76. });
  77. // set open handle
  78. wss_server_->set_open_handler(
  79. [this](websocketpp::connection_hdl hdl) { on_open(hdl); });
  80. // set close handle
  81. wss_server_->set_close_handler(
  82. [this](websocketpp::connection_hdl hdl) { on_close(hdl); });
  83. // begin accept
  84. wss_server_->start_accept();
  85. // not print log
  86. wss_server_->clear_access_channels(websocketpp::log::alevel::all);
  87. } else {
  88. // set message handle
  89. server_->set_message_handler(
  90. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  91. on_message(hdl, msg);
  92. });
  93. // set open handle
  94. server_->set_open_handler(
  95. [this](websocketpp::connection_hdl hdl) { on_open(hdl); });
  96. // set close handle
  97. server_->set_close_handler(
  98. [this](websocketpp::connection_hdl hdl) { on_close(hdl); });
  99. // begin accept
  100. server_->start_accept();
  101. // not print log
  102. server_->clear_access_channels(websocketpp::log::alevel::all);
  103. }
  104. }
  105. void do_decoder(const std::vector<char>& buffer,
  106. websocketpp::connection_hdl& hdl,
  107. nlohmann::json& msg,
  108. websocketpp::lib::mutex& thread_lock,
  109. std::vector<std::vector<float>> &hotwords_embedding,
  110. std::string wav_name,
  111. bool itn,
  112. int audio_fs,
  113. std::string wav_format,
  114. FUNASR_DEC_HANDLE& decoder_handle);
  115. void initAsr(std::map<std::string, std::string>& model_path, int thread_num);
  116. void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
  117. void on_open(websocketpp::connection_hdl hdl);
  118. void on_close(websocketpp::connection_hdl hdl);
  119. context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl,
  120. std::string& s_certfile, std::string& s_keyfile);
  121. private:
  122. void check_and_clean_connection();
  123. asio::io_context& io_decoder_; // threads for asr decoder
  124. // std::ofstream fout;
  125. FUNASR_HANDLE asr_handle; // asr engine handle
  126. bool isonline = false; // online or offline engine, now only support offline
  127. bool is_ssl = true;
  128. server* server_; // websocket server
  129. wss_server* wss_server_; // websocket server
  130. // use map to keep the received samples data from one connection in offline
  131. // engine. if for online engline, a data struct is needed(TODO)
  132. std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,
  133. std::owner_less<websocketpp::connection_hdl>>
  134. data_map;
  135. websocketpp::lib::mutex m_lock; // mutex for sample_map
  136. };
  137. // std::unordered_map<std::string, int>& hws_map, int fst_inc_wts, std::string& nn_hotwords
  138. #endif // WEBSOCKET_SERVER_H_