funasr-ws-server.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 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("funasr-ws-server", ' ', "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. TCLAP::ValueArg<std::string> certfile("", "certfile", "certfile", false, "",
  63. "string");
  64. TCLAP::ValueArg<std::string> keyfile("", "keyfile", "keyfile", false, "",
  65. "string");
  66. cmd.add(certfile);
  67. cmd.add(keyfile);
  68. cmd.add(model_dir);
  69. cmd.add(quantize);
  70. cmd.add(vad_dir);
  71. cmd.add(vad_quant);
  72. cmd.add(punc_dir);
  73. cmd.add(punc_quant);
  74. cmd.add(listen_ip);
  75. cmd.add(port);
  76. cmd.add(io_thread_num);
  77. cmd.add(decoder_thread_num);
  78. cmd.add(model_thread_num);
  79. cmd.parse(argc, argv);
  80. std::map<std::string, std::string> model_path;
  81. GetValue(model_dir, MODEL_DIR, model_path);
  82. GetValue(quantize, QUANTIZE, model_path);
  83. GetValue(vad_dir, VAD_DIR, model_path);
  84. GetValue(vad_quant, VAD_QUANT, model_path);
  85. GetValue(punc_dir, PUNC_DIR, model_path);
  86. GetValue(punc_quant, PUNC_QUANT, model_path);
  87. std::string s_listen_ip = listen_ip.getValue();
  88. int s_port = port.getValue();
  89. int s_io_thread_num = io_thread_num.getValue();
  90. int s_decoder_thread_num = decoder_thread_num.getValue();
  91. int s_model_thread_num = model_thread_num.getValue();
  92. asio::io_context io_decoder; // context for decoding
  93. asio::io_context io_server; // context for server
  94. std::vector<std::thread> decoder_threads;
  95. std::string s_certfile = certfile.getValue();
  96. std::string s_keyfile = keyfile.getValue();
  97. bool is_ssl = false;
  98. if (!s_certfile.empty()) {
  99. is_ssl = true;
  100. }
  101. auto conn_guard = asio::make_work_guard(
  102. io_decoder); // make sure threads can wait in the queue
  103. auto server_guard = asio::make_work_guard(
  104. io_server); // make sure threads can wait in the queue
  105. // create threads pool
  106. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  107. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  108. }
  109. server server_; // server for websocket
  110. wss_server wss_server_;
  111. if (is_ssl) {
  112. wss_server_.init_asio(&io_server); // init asio
  113. wss_server_.set_reuse_addr(
  114. true); // reuse address as we create multiple threads
  115. // list on port for accept
  116. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  117. WebSocketServer websocket_srv(
  118. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  119. s_keyfile); // websocket server for asr engine
  120. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  121. } else {
  122. server_.init_asio(&io_server); // init asio
  123. server_.set_reuse_addr(
  124. true); // reuse address as we create multiple threads
  125. // list on port for accept
  126. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  127. WebSocketServer websocket_srv(
  128. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  129. s_keyfile); // websocket server for asr engine
  130. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  131. }
  132. std::cout << "asr model init finished. listen on port:" << s_port
  133. << std::endl;
  134. // Start the ASIO network io_service run loop
  135. std::vector<std::thread> ts;
  136. // create threads for io network
  137. for (size_t i = 0; i < s_io_thread_num; i++) {
  138. ts.emplace_back([&io_server]() { io_server.run(); });
  139. }
  140. // wait for theads
  141. for (size_t i = 0; i < s_io_thread_num; i++) {
  142. ts[i].join();
  143. }
  144. // wait for theads
  145. for (auto& t : decoder_threads) {
  146. t.join();
  147. }
  148. } catch (std::exception const& e) {
  149. std::cerr << "Error: " << e.what() << std::endl;
  150. }
  151. return 0;
  152. }