websocketmain.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <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 "websocketsrv.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. if (value_arg.isSet()) {
  17. model_path.insert({key, value_arg.getValue()});
  18. LOG(INFO) << key << " : " << value_arg.getValue();
  19. }
  20. }
  21. int main(int argc, char* argv[]) {
  22. try {
  23. google::InitGoogleLogging(argv[0]);
  24. FLAGS_logtostderr = true;
  25. TCLAP::CmdLine cmd("websocketmain", ' ', "1.0");
  26. TCLAP::ValueArg<std::string> model_dir(
  27. "", MODEL_DIR,
  28. "the asr model path, which contains model.onnx, config.yaml, am.mvn",
  29. true, "", "string");
  30. TCLAP::ValueArg<std::string> quantize(
  31. "", QUANTIZE,
  32. "false (Default), load the model of model.onnx in model_dir. If set "
  33. "true, load the model of model_quant.onnx in model_dir",
  34. false, "false", "string");
  35. TCLAP::ValueArg<std::string> vad_dir(
  36. "", VAD_DIR,
  37. "the vad model path, which contains model.onnx, vad.yaml, vad.mvn",
  38. false, "", "string");
  39. TCLAP::ValueArg<std::string> vad_quant(
  40. "", VAD_QUANT,
  41. "false (Default), load the model of model.onnx in vad_dir. If set "
  42. "true, load the model of model_quant.onnx in vad_dir",
  43. false, "false", "string");
  44. TCLAP::ValueArg<std::string> punc_dir(
  45. "", PUNC_DIR,
  46. "the punc model path, which contains model.onnx, punc.yaml", false, "",
  47. "string");
  48. TCLAP::ValueArg<std::string> punc_quant(
  49. "", PUNC_QUANT,
  50. "false (Default), load the model of model.onnx in punc_dir. If set "
  51. "true, load the model of model_quant.onnx in punc_dir",
  52. false, "false", "string");
  53. TCLAP::ValueArg<std::string> listen_ip("", "listen_ip", "listen_ip", false,
  54. "0.0.0.0", "string");
  55. TCLAP::ValueArg<int> port("", "port", "port", false, 8889, "int");
  56. TCLAP::ValueArg<int> io_thread_num("", "io_thread_num", "io_thread_num",
  57. false, 8, "int");
  58. TCLAP::ValueArg<int> decoder_thread_num(
  59. "", "decoder_thread_num", "decoder_thread_num", false, 8, "int");
  60. TCLAP::ValueArg<int> model_thread_num("", "model_thread_num",
  61. "model_thread_num", false, 1, "int");
  62. cmd.add(model_dir);
  63. cmd.add(quantize);
  64. cmd.add(vad_dir);
  65. cmd.add(vad_quant);
  66. cmd.add(punc_dir);
  67. cmd.add(punc_quant);
  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(model_dir, MODEL_DIR, model_path);
  76. GetValue(quantize, QUANTIZE, model_path);
  77. GetValue(vad_dir, VAD_DIR, model_path);
  78. GetValue(vad_quant, VAD_QUANT, model_path);
  79. GetValue(punc_dir, PUNC_DIR, model_path);
  80. GetValue(punc_quant, PUNC_QUANT, model_path);
  81. std::string s_listen_ip = listen_ip.getValue();
  82. int s_port = port.getValue();
  83. int s_io_thread_num = io_thread_num.getValue();
  84. int s_decoder_thread_num = decoder_thread_num.getValue();
  85. int s_model_thread_num = model_thread_num.getValue();
  86. asio::io_context io_decoder; // context for decoding
  87. std::vector<std::thread> decoder_threads;
  88. auto conn_guard = asio::make_work_guard(
  89. io_decoder); // make sure threads can wait in the queue
  90. // create threads pool
  91. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  92. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  93. }
  94. server server_; // server for websocket
  95. server_.init_asio(); // init asio
  96. server_.set_reuse_addr(
  97. true); // reuse address as we create multiple threads
  98. // list on port for accept
  99. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  100. WebSocketServer websocket_srv(io_decoder,
  101. &server_); // websocket server for asr engine
  102. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  103. std::cout << "asr model init finished. listen on port:" << s_port
  104. << std::endl;
  105. // Start the ASIO network io_service run loop
  106. if (s_io_thread_num == 1) {
  107. server_.run();
  108. } else {
  109. typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
  110. std::vector<thread_ptr> ts;
  111. // create threads for io network
  112. for (size_t i = 0; i < s_io_thread_num; i++) {
  113. ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(
  114. &server::run, &server_));
  115. }
  116. // wait for theads
  117. for (size_t i = 0; i < s_io_thread_num; i++) {
  118. ts[i]->join();
  119. }
  120. }
  121. // wait for theads
  122. for (auto& t : decoder_threads) {
  123. t.join();
  124. }
  125. } catch (std::exception const& e) {
  126. std::cerr << "Error: " << e.what() << std::endl;
  127. }
  128. return 0;
  129. }