funasr-wss-server.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // io server
  7. // Usage:funasr-ws-server [--model_thread_num <int>] [--decoder_thread_num <int>]
  8. // [--io_thread_num <int>] [--port <int>] [--listen_ip
  9. // <string>] [--punc-quant <string>] [--punc-dir <string>]
  10. // [--vad-quant <string>] [--vad-dir <string>] [--quantize
  11. // <string>] --model-dir <string> [--] [--version] [-h]
  12. #include "websocket-server.h"
  13. using namespace std;
  14. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key,
  15. std::map<std::string, std::string>& model_path) {
  16. model_path.insert({key, value_arg.getValue()});
  17. LOG(INFO) << key << " : " << value_arg.getValue();
  18. }
  19. int main(int argc, char* argv[]) {
  20. try {
  21. google::InitGoogleLogging(argv[0]);
  22. FLAGS_logtostderr = true;
  23. TCLAP::CmdLine cmd("funasr-ws-server", ' ', "1.0");
  24. TCLAP::ValueArg<std::string> model_dir(
  25. "", MODEL_DIR,
  26. "default: /workspace/models/asr, the asr model path, which contains model.onnx, config.yaml, am.mvn",
  27. false, "/workspace/models/asr", "string");
  28. TCLAP::ValueArg<std::string> quantize(
  29. "", QUANTIZE,
  30. "true (Default), load the model of model.onnx in model_dir. If set "
  31. "true, load the model of model_quant.onnx in model_dir",
  32. false, "true", "string");
  33. TCLAP::ValueArg<std::string> vad_dir(
  34. "", VAD_DIR,
  35. "default: /workspace/models/vad, the vad model path, which contains model.onnx, vad.yaml, vad.mvn",
  36. false, "/workspace/models/vad", "string");
  37. TCLAP::ValueArg<std::string> vad_quant(
  38. "", VAD_QUANT,
  39. "true (Default), load the model of model.onnx in vad_dir. If set "
  40. "true, load the model of model_quant.onnx in vad_dir",
  41. false, "true", "string");
  42. TCLAP::ValueArg<std::string> punc_dir(
  43. "", PUNC_DIR,
  44. "default: /workspace/models/punc, the punc model path, which contains model.onnx, punc.yaml",
  45. false, "/workspace/models/punc",
  46. "string");
  47. TCLAP::ValueArg<std::string> punc_quant(
  48. "", PUNC_QUANT,
  49. "true (Default), load the model of model.onnx in punc_dir. If set "
  50. "true, load the model of model_quant.onnx in punc_dir",
  51. false, "true", "string");
  52. TCLAP::ValueArg<std::string> listen_ip("", "listen_ip", "listen_ip", false,
  53. "0.0.0.0", "string");
  54. TCLAP::ValueArg<int> port("", "port", "port", false, 8889, "int");
  55. TCLAP::ValueArg<int> io_thread_num("", "io_thread_num", "io_thread_num",
  56. false, 8, "int");
  57. TCLAP::ValueArg<int> decoder_thread_num(
  58. "", "decoder_thread_num", "decoder_thread_num", false, 8, "int");
  59. TCLAP::ValueArg<int> model_thread_num("", "model_thread_num",
  60. "model_thread_num", false, 1, "int");
  61. TCLAP::ValueArg<std::string> certfile("", "certfile",
  62. "default: ../../../ssl_key/server.crt, path of certficate for WSS connection. if it is empty, it will be in WS mode.",
  63. false, "../../../ssl_key/server.crt", "string");
  64. TCLAP::ValueArg<std::string> keyfile("", "keyfile",
  65. "default: ../../../ssl_key/server.key, path of keyfile for WSS connection",
  66. false, "../../../ssl_key/server.key", "string");
  67. cmd.add(certfile);
  68. cmd.add(keyfile);
  69. cmd.add(model_dir);
  70. cmd.add(quantize);
  71. cmd.add(vad_dir);
  72. cmd.add(vad_quant);
  73. cmd.add(punc_dir);
  74. cmd.add(punc_quant);
  75. cmd.add(listen_ip);
  76. cmd.add(port);
  77. cmd.add(io_thread_num);
  78. cmd.add(decoder_thread_num);
  79. cmd.add(model_thread_num);
  80. cmd.parse(argc, argv);
  81. std::map<std::string, std::string> model_path;
  82. GetValue(model_dir, MODEL_DIR, model_path);
  83. GetValue(quantize, QUANTIZE, model_path);
  84. GetValue(vad_dir, VAD_DIR, model_path);
  85. GetValue(vad_quant, VAD_QUANT, model_path);
  86. GetValue(punc_dir, PUNC_DIR, model_path);
  87. GetValue(punc_quant, PUNC_QUANT, model_path);
  88. std::string s_listen_ip = listen_ip.getValue();
  89. int s_port = port.getValue();
  90. int s_io_thread_num = io_thread_num.getValue();
  91. int s_decoder_thread_num = decoder_thread_num.getValue();
  92. int s_model_thread_num = model_thread_num.getValue();
  93. asio::io_context io_decoder; // context for decoding
  94. asio::io_context io_server; // context for server
  95. std::vector<std::thread> decoder_threads;
  96. std::string s_certfile = certfile.getValue();
  97. std::string s_keyfile = keyfile.getValue();
  98. bool is_ssl = false;
  99. if (!s_certfile.empty()) {
  100. is_ssl = true;
  101. }
  102. auto conn_guard = asio::make_work_guard(
  103. io_decoder); // make sure threads can wait in the queue
  104. auto server_guard = asio::make_work_guard(
  105. io_server); // make sure threads can wait in the queue
  106. // create threads pool
  107. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  108. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  109. }
  110. server server_; // server for websocket
  111. wss_server wss_server_;
  112. if (is_ssl) {
  113. wss_server_.init_asio(&io_server); // init asio
  114. wss_server_.set_reuse_addr(
  115. true); // reuse address as we create multiple threads
  116. // list on port for accept
  117. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  118. WebSocketServer websocket_srv(
  119. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  120. s_keyfile); // websocket server for asr engine
  121. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  122. } else {
  123. server_.init_asio(&io_server); // init asio
  124. server_.set_reuse_addr(
  125. true); // reuse address as we create multiple threads
  126. // list on port for accept
  127. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  128. WebSocketServer websocket_srv(
  129. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  130. s_keyfile); // websocket server for asr engine
  131. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  132. }
  133. std::cout << "asr model init finished. listen on port:" << s_port
  134. << std::endl;
  135. // Start the ASIO network io_service run loop
  136. std::vector<std::thread> ts;
  137. // create threads for io network
  138. for (size_t i = 0; i < s_io_thread_num; i++) {
  139. ts.emplace_back([&io_server]() { io_server.run(); });
  140. }
  141. // wait for theads
  142. for (size_t i = 0; i < s_io_thread_num; i++) {
  143. ts[i].join();
  144. }
  145. // wait for theads
  146. for (auto& t : decoder_threads) {
  147. t.join();
  148. }
  149. } catch (std::exception const& e) {
  150. std::cerr << "Error: " << e.what() << std::endl;
  151. }
  152. return 0;
  153. }