websocketmain.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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:websocketmain [--model_thread_num <int>] [--decoder_thread_num
  8. // <int>] [--io_thread_num <int>] [--port <int>]
  9. // [--listen_ip <string>] [--wav-scp <string>]
  10. // [--wav-path <string>] [--punc-config <string>]
  11. // [--punc-model <string>] --am-config <string>
  12. // --am-cmvn <string> --am-model <string>
  13. // [--vad-config <string>] [--vad-cmvn <string>]
  14. // [--vad-model <string>] [--] [--version] [-h]
  15. #include "websocketsrv.h"
  16. using namespace std;
  17. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key,
  18. std::map<std::string, std::string>& model_path) {
  19. if (value_arg.isSet()) {
  20. model_path.insert({key, value_arg.getValue()});
  21. LOG(INFO) << key << " : " << value_arg.getValue();
  22. }
  23. }
  24. int main(int argc, char* argv[]) {
  25. try {
  26. google::InitGoogleLogging(argv[0]);
  27. FLAGS_logtostderr = true;
  28. TCLAP::CmdLine cmd("websocketmain", ' ', "1.0");
  29. TCLAP::ValueArg<std::string> vad_model("", VAD_MODEL_PATH, "vad model path",
  30. false, "", "string");
  31. TCLAP::ValueArg<std::string> vad_cmvn("", VAD_CMVN_PATH, "vad cmvn path",
  32. false, "", "string");
  33. TCLAP::ValueArg<std::string> vad_config(
  34. "", VAD_CONFIG_PATH, "vad config path", false, "", "string");
  35. TCLAP::ValueArg<std::string> am_model("", AM_MODEL_PATH, "am model path",
  36. true, "", "string");
  37. TCLAP::ValueArg<std::string> am_cmvn("", AM_CMVN_PATH, "am cmvn path", true,
  38. "", "string");
  39. TCLAP::ValueArg<std::string> am_config("", AM_CONFIG_PATH, "am config path",
  40. true, "", "string");
  41. TCLAP::ValueArg<std::string> punc_model(
  42. "", PUNC_MODEL_PATH, "punc model path", false, "", "string");
  43. TCLAP::ValueArg<std::string> punc_config(
  44. "", PUNC_CONFIG_PATH, "punc config path", false, "", "string");
  45. TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "wave file path", false,
  46. "", "string");
  47. TCLAP::ValueArg<std::string> wav_scp("", WAV_SCP, "wave scp path", false,
  48. "", "string");
  49. TCLAP::ValueArg<std::string> listen_ip("", "listen_ip", "listen_ip", false,
  50. "0.0.0.0", "string");
  51. TCLAP::ValueArg<int> port("", "port", "port", false, 8889, "int");
  52. TCLAP::ValueArg<int> io_thread_num("", "io_thread_num", "io_thread_num",
  53. false, 8, "int");
  54. TCLAP::ValueArg<int> decoder_thread_num(
  55. "", "decoder_thread_num", "decoder_thread_num", false, 8, "int");
  56. TCLAP::ValueArg<int> model_thread_num("", "model_thread_num",
  57. "model_thread_num", false, 1, "int");
  58. cmd.add(vad_model);
  59. cmd.add(vad_cmvn);
  60. cmd.add(vad_config);
  61. cmd.add(am_model);
  62. cmd.add(am_cmvn);
  63. cmd.add(am_config);
  64. cmd.add(punc_model);
  65. cmd.add(punc_config);
  66. cmd.add(wav_path);
  67. cmd.add(wav_scp);
  68. cmd.add(listen_ip);
  69. cmd.add(port);
  70. cmd.add(io_thread_num);
  71. cmd.add(decoder_thread_num);
  72. cmd.add(model_thread_num);
  73. cmd.parse(argc, argv);
  74. std::map<std::string, std::string> model_path;
  75. GetValue(vad_model, VAD_MODEL_PATH, model_path);
  76. GetValue(vad_cmvn, VAD_CMVN_PATH, model_path);
  77. GetValue(vad_config, VAD_CONFIG_PATH, model_path);
  78. GetValue(am_model, AM_MODEL_PATH, model_path);
  79. GetValue(am_cmvn, AM_CMVN_PATH, model_path);
  80. GetValue(am_config, AM_CONFIG_PATH, model_path);
  81. GetValue(punc_model, PUNC_MODEL_PATH, model_path);
  82. GetValue(punc_config, PUNC_CONFIG_PATH, model_path);
  83. GetValue(wav_path, WAV_PATH, model_path);
  84. GetValue(wav_scp, WAV_SCP, model_path);
  85. std::string s_listen_ip = listen_ip.getValue();
  86. int s_port = port.getValue();
  87. int s_io_thread_num = io_thread_num.getValue();
  88. int s_decoder_thread_num = decoder_thread_num.getValue();
  89. int s_model_thread_num = model_thread_num.getValue();
  90. asio::io_context io_decoder; // context for decoding
  91. std::vector<std::thread> decoder_threads;
  92. auto conn_guard = asio::make_work_guard(
  93. io_decoder); // make sure threads can wait in the queue
  94. // create threads pool
  95. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  96. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  97. }
  98. server server_; // server for websocket
  99. server_.init_asio(); // init asio
  100. server_.set_reuse_addr(
  101. true); // reuse address as we create multiple threads
  102. // list on port for accept
  103. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  104. WebSocketServer websocket_srv(io_decoder,
  105. &server_); // websocket server for asr engine
  106. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  107. std::cout << "asr model init finished. listen on port:" << s_port
  108. << std::endl;
  109. // Start the ASIO network io_service run loop
  110. if (s_io_thread_num == 1) {
  111. server_.run();
  112. } else {
  113. typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
  114. std::vector<thread_ptr> ts;
  115. // create threads for io network
  116. for (size_t i = 0; i < s_io_thread_num; i++) {
  117. ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(
  118. &server::run, &server_));
  119. }
  120. // wait for theads
  121. for (size_t i = 0; i < s_io_thread_num; i++) {
  122. ts[i]->join();
  123. }
  124. }
  125. // wait for theads
  126. for (auto& t : decoder_threads) {
  127. t.join();
  128. }
  129. } catch (std::exception const& e) {
  130. std::cerr << "Error: " << e.what() << std::endl;
  131. }
  132. return 0;
  133. }