websocket-server.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include "websocket-server.h"
  12. #include <thread>
  13. #include <utility>
  14. #include <vector>
  15. extern std::string hotwords;
  16. context_ptr WebSocketServer::on_tls_init(tls_mode mode,
  17. websocketpp::connection_hdl hdl,
  18. std::string& s_certfile,
  19. std::string& s_keyfile) {
  20. namespace asio = websocketpp::lib::asio;
  21. LOG(INFO) << "on_tls_init called with hdl: " << hdl.lock().get();
  22. LOG(INFO) << "using TLS mode: "
  23. << (mode == MOZILLA_MODERN ? "Mozilla Modern"
  24. : "Mozilla Intermediate");
  25. context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
  26. asio::ssl::context::sslv23);
  27. try {
  28. if (mode == MOZILLA_MODERN) {
  29. // Modern disables TLSv1
  30. ctx->set_options(
  31. asio::ssl::context::default_workarounds |
  32. asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3 |
  33. asio::ssl::context::no_tlsv1 | asio::ssl::context::single_dh_use);
  34. } else {
  35. ctx->set_options(asio::ssl::context::default_workarounds |
  36. asio::ssl::context::no_sslv2 |
  37. asio::ssl::context::no_sslv3 |
  38. asio::ssl::context::single_dh_use);
  39. }
  40. ctx->use_certificate_chain_file(s_certfile);
  41. ctx->use_private_key_file(s_keyfile, asio::ssl::context::pem);
  42. } catch (std::exception& e) {
  43. LOG(INFO) << "Exception: " << e.what();
  44. }
  45. return ctx;
  46. }
  47. // feed buffer to asr engine for decoder
  48. void WebSocketServer::do_decoder(const std::vector<char>& buffer,
  49. websocketpp::connection_hdl& hdl,
  50. websocketpp::lib::mutex& thread_lock,
  51. std::vector<std::vector<float>> &hotwords_embedding,
  52. std::string wav_name,
  53. bool itn,
  54. int audio_fs,
  55. std::string wav_format) {
  56. scoped_lock guard(thread_lock);
  57. try {
  58. int num_samples = buffer.size(); // the size of the buf
  59. if (!buffer.empty() && hotwords_embedding.size() > 0) {
  60. std::string asr_result;
  61. std::string stamp_res;
  62. try{
  63. FUNASR_RESULT Result = FunOfflineInferBuffer(
  64. asr_hanlde, buffer.data(), buffer.size(), RASR_NONE, NULL,
  65. hotwords_embedding, audio_fs, wav_format, itn);
  66. asr_result = ((FUNASR_RECOG_RESULT*)Result)->msg; // get decode result
  67. stamp_res = ((FUNASR_RECOG_RESULT*)Result)->stamp;
  68. FunASRFreeResult(Result);
  69. }catch (std::exception const& e) {
  70. LOG(ERROR) << e.what();
  71. return;
  72. }
  73. websocketpp::lib::error_code ec;
  74. nlohmann::json jsonresult; // result json
  75. jsonresult["text"] = asr_result; // put result in 'text'
  76. jsonresult["mode"] = "offline";
  77. jsonresult["is_final"] = false;
  78. if(stamp_res != ""){
  79. jsonresult["timestamp"] = stamp_res;
  80. }
  81. jsonresult["wav_name"] = wav_name;
  82. // send the json to client
  83. if (is_ssl) {
  84. wss_server_->send(hdl, jsonresult.dump(),
  85. websocketpp::frame::opcode::text, ec);
  86. } else {
  87. server_->send(hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  88. ec);
  89. }
  90. LOG(INFO) << "buffer.size=" << buffer.size() << ",result json=" << jsonresult.dump();
  91. }else{
  92. LOG(INFO) << "Sent empty msg";
  93. websocketpp::lib::error_code ec;
  94. nlohmann::json jsonresult; // result json
  95. jsonresult["text"] = ""; // put result in 'text'
  96. jsonresult["mode"] = "offline";
  97. jsonresult["is_final"] = false;
  98. jsonresult["wav_name"] = wav_name;
  99. // send the json to client
  100. if (is_ssl) {
  101. wss_server_->send(hdl, jsonresult.dump(),
  102. websocketpp::frame::opcode::text, ec);
  103. } else {
  104. server_->send(hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  105. ec);
  106. }
  107. }
  108. } catch (std::exception const& e) {
  109. std::cerr << "Error: " << e.what() << std::endl;
  110. }
  111. }
  112. void WebSocketServer::on_open(websocketpp::connection_hdl hdl) {
  113. scoped_lock guard(m_lock); // for threads safty
  114. std::shared_ptr<FUNASR_MESSAGE> data_msg =
  115. std::make_shared<FUNASR_MESSAGE>(); // put a new data vector for new
  116. // connection
  117. data_msg->samples = std::make_shared<std::vector<char>>();
  118. data_msg->thread_lock = std::make_shared<websocketpp::lib::mutex>();
  119. data_msg->msg = nlohmann::json::parse("{}");
  120. data_msg->msg["wav_format"] = "pcm";
  121. data_msg->msg["wav_name"] = "wav-default-id";
  122. data_msg->msg["itn"] = true;
  123. data_msg->msg["audio_fs"] = 16000;
  124. data_map.emplace(hdl, data_msg);
  125. LOG(INFO) << "on_open, active connections: " << data_map.size();
  126. }
  127. void WebSocketServer::on_close(websocketpp::connection_hdl hdl) {
  128. scoped_lock guard(m_lock);
  129. std::shared_ptr<FUNASR_MESSAGE> data_msg = nullptr;
  130. auto it_data = data_map.find(hdl);
  131. if (it_data != data_map.end()) {
  132. data_msg = it_data->second;
  133. } else {
  134. return;
  135. }
  136. unique_lock guard_decoder(*(data_msg->thread_lock));
  137. data_msg->msg["is_eof"]=true;
  138. guard_decoder.unlock();
  139. LOG(INFO) << "on_close, active connections: " << data_map.size();
  140. }
  141. void remove_hdl(
  142. websocketpp::connection_hdl hdl,
  143. std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,
  144. std::owner_less<websocketpp::connection_hdl>>& data_map) {
  145. std::shared_ptr<FUNASR_MESSAGE> data_msg = nullptr;
  146. auto it_data = data_map.find(hdl);
  147. if (it_data != data_map.end()) {
  148. data_msg = it_data->second;
  149. } else {
  150. return;
  151. }
  152. unique_lock guard_decoder(*(data_msg->thread_lock));
  153. if (data_msg->msg["is_eof"]==true) {
  154. data_map.erase(hdl);
  155. LOG(INFO) << "remove one connection";
  156. }
  157. guard_decoder.unlock();
  158. }
  159. void WebSocketServer::check_and_clean_connection() {
  160. while(true){
  161. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  162. std::vector<websocketpp::connection_hdl> to_remove; // remove list
  163. auto iter = data_map.begin();
  164. while (iter != data_map.end()) { // loop to find closed connection
  165. websocketpp::connection_hdl hdl = iter->first;
  166. try{
  167. if (is_ssl) {
  168. wss_server::connection_ptr con = wss_server_->get_con_from_hdl(hdl);
  169. if (con->get_state() != 1) { // session::state::open ==1
  170. to_remove.push_back(hdl);
  171. }
  172. } else {
  173. server::connection_ptr con = server_->get_con_from_hdl(hdl);
  174. if (con->get_state() != 1) { // session::state::open ==1
  175. to_remove.push_back(hdl);
  176. }
  177. }
  178. }
  179. catch (std::exception const &e)
  180. {
  181. // if connection is close, we set is_eof = true
  182. std::shared_ptr<FUNASR_MESSAGE> data_msg = nullptr;
  183. auto it_data = data_map.find(hdl);
  184. if (it_data != data_map.end()) {
  185. data_msg = it_data->second;
  186. } else {
  187. continue;
  188. }
  189. unique_lock guard_decoder(*(data_msg->thread_lock));
  190. data_msg->msg["is_eof"]=true;
  191. guard_decoder.unlock();
  192. to_remove.push_back(hdl);
  193. LOG(INFO)<<"connection is closed: "<<e.what();
  194. }
  195. iter++;
  196. }
  197. for (auto hdl : to_remove) {
  198. {
  199. unique_lock lock(m_lock);
  200. remove_hdl(hdl, data_map);
  201. }
  202. }
  203. }
  204. }
  205. void WebSocketServer::on_message(websocketpp::connection_hdl hdl,
  206. message_ptr msg) {
  207. unique_lock lock(m_lock);
  208. // find the sample data vector according to one connection
  209. std::shared_ptr<FUNASR_MESSAGE> msg_data = nullptr;
  210. auto it_data = data_map.find(hdl);
  211. if (it_data != data_map.end()) {
  212. msg_data = it_data->second;
  213. } else{
  214. lock.unlock();
  215. return;
  216. }
  217. std::shared_ptr<std::vector<char>> sample_data_p = msg_data->samples;
  218. std::shared_ptr<websocketpp::lib::mutex> thread_lock_p = msg_data->thread_lock;
  219. lock.unlock();
  220. if (sample_data_p == nullptr) {
  221. LOG(INFO) << "error when fetch sample data vector";
  222. return;
  223. }
  224. const std::string& payload = msg->get_payload(); // get msg type
  225. unique_lock guard_decoder(*(thread_lock_p)); // mutex for one connection
  226. switch (msg->get_opcode()) {
  227. case websocketpp::frame::opcode::text: {
  228. nlohmann::json jsonresult = nlohmann::json::parse(payload);
  229. if (jsonresult["wav_name"] != nullptr) {
  230. msg_data->msg["wav_name"] = jsonresult["wav_name"];
  231. }
  232. if (jsonresult["wav_format"] != nullptr) {
  233. msg_data->msg["wav_format"] = jsonresult["wav_format"];
  234. }
  235. if(msg_data->hotwords_embedding == NULL){
  236. if (jsonresult["hotwords"] != nullptr) {
  237. msg_data->msg["hotwords"] = jsonresult["hotwords"];
  238. if (!msg_data->msg["hotwords"].empty()) {
  239. std::string hw = msg_data->msg["hotwords"];
  240. hw = hw + " " + hotwords;
  241. LOG(INFO) << "hotwords: " << hw;
  242. std::vector<std::vector<float>> new_hotwords_embedding = CompileHotwordEmbedding(asr_hanlde, hw);
  243. msg_data->hotwords_embedding =
  244. std::make_shared<std::vector<std::vector<float>>>(new_hotwords_embedding);
  245. }
  246. } else {
  247. if (hotwords.empty()) {
  248. std::string hw = "";
  249. LOG(INFO)<<"hotwords: " << hw;
  250. std::vector<std::vector<float>> new_hotwords_embedding= CompileHotwordEmbedding(asr_hanlde, hw);
  251. msg_data->hotwords_embedding =
  252. std::make_shared<std::vector<std::vector<float>>>(new_hotwords_embedding);
  253. }else {
  254. std::string hw = hotwords;
  255. LOG(INFO) << "hotwords: " << hw;
  256. std::vector<std::vector<float>> new_hotwords_embedding= CompileHotwordEmbedding(asr_hanlde, hw);
  257. msg_data->hotwords_embedding =
  258. std::make_shared<std::vector<std::vector<float>>>(new_hotwords_embedding);
  259. }
  260. }
  261. }
  262. if (jsonresult.contains("audio_fs")) {
  263. msg_data->msg["audio_fs"] = jsonresult["audio_fs"];
  264. }
  265. if (jsonresult.contains("itn")) {
  266. msg_data->msg["itn"] = jsonresult["itn"];
  267. }
  268. if (jsonresult["is_speaking"] == false ||
  269. jsonresult["is_finished"] == true) {
  270. LOG(INFO) << "client done";
  271. // for offline, send all receive data to decoder engine
  272. std::vector<std::vector<float>> hotwords_embedding_(*(msg_data->hotwords_embedding));
  273. asio::post(io_decoder_,
  274. std::bind(&WebSocketServer::do_decoder, this,
  275. std::move(*(sample_data_p.get())),
  276. std::move(hdl),
  277. std::ref(*thread_lock_p),
  278. std::move(hotwords_embedding_),
  279. msg_data->msg["wav_name"],
  280. msg_data->msg["itn"],
  281. msg_data->msg["audio_fs"],
  282. msg_data->msg["wav_format"]));
  283. }
  284. break;
  285. }
  286. case websocketpp::frame::opcode::binary: {
  287. // recived binary data
  288. const auto* pcm_data = static_cast<const char*>(payload.data());
  289. int32_t num_samples = payload.size();
  290. if (isonline) {
  291. // TODO
  292. } else {
  293. // for offline, we add receive data to end of the sample data vector
  294. sample_data_p->insert(sample_data_p->end(), pcm_data,
  295. pcm_data + num_samples);
  296. }
  297. break;
  298. }
  299. default:
  300. break;
  301. }
  302. }
  303. // init asr model
  304. void WebSocketServer::initAsr(std::map<std::string, std::string>& model_path,
  305. int thread_num) {
  306. try {
  307. // init model with api
  308. asr_hanlde = FunOfflineInit(model_path, thread_num);
  309. LOG(INFO) << "model successfully inited";
  310. LOG(INFO) << "initAsr run check_and_clean_connection";
  311. std::thread clean_thread(&WebSocketServer::check_and_clean_connection,this);
  312. clean_thread.detach();
  313. LOG(INFO) << "initAsr run check_and_clean_connection finished";
  314. } catch (const std::exception& e) {
  315. LOG(INFO) << e.what();
  316. }
  317. }