游雁
2023-05-25 b18f7d121f2f17df8bf2d0c2bbb223bc5ddbcc0f
funasr/runtime/websocket/websocketmain.cpp
@@ -5,14 +5,11 @@
/* 2022-2023 by zhaomingwork */
// io server
// Usage:websocketmain  [--model_thread_num <int>] [--decoder_thread_num
//                        <int>] [--io_thread_num <int>] [--port <int>]
//                        [--listen_ip <string>] [--wav-scp <string>]
//                        [--wav-path <string>] [--punc-config <string>]
//                        [--punc-model <string>] --am-config <string>
//                        --am-cmvn <string> --am-model <string>
//                        [--vad-config <string>] [--vad-cmvn <string>]
//                        [--vad-model <string>] [--] [--version] [-h]
// Usage:websocketmain  [--model_thread_num <int>] [--decoder_thread_num <int>]
//                    [--io_thread_num <int>] [--port <int>] [--listen_ip
//                    <string>] [--punc-quant <string>] [--punc-dir <string>]
//                    [--vad-quant <string>] [--vad-dir <string>] [--quantize
//                    <string>] --model-dir <string> [--] [--version] [-h]
#include "websocketsrv.h"
using namespace std;
@@ -29,29 +26,33 @@
    FLAGS_logtostderr = true;
    TCLAP::CmdLine cmd("websocketmain", ' ', "1.0");
    TCLAP::ValueArg<std::string> vad_model("", VAD_MODEL_PATH, "vad model path",
                                           false, "", "string");
    TCLAP::ValueArg<std::string> vad_cmvn("", VAD_CMVN_PATH, "vad cmvn path",
                                          false, "", "string");
    TCLAP::ValueArg<std::string> vad_config(
        "", VAD_CONFIG_PATH, "vad config path", false, "", "string");
    TCLAP::ValueArg<std::string> am_model("", AM_MODEL_PATH, "am model path",
                                          true, "", "string");
    TCLAP::ValueArg<std::string> am_cmvn("", AM_CMVN_PATH, "am cmvn path", true,
                                         "", "string");
    TCLAP::ValueArg<std::string> am_config("", AM_CONFIG_PATH, "am config path",
                                           true, "", "string");
    TCLAP::ValueArg<std::string> punc_model(
        "", PUNC_MODEL_PATH, "punc model path", false, "", "string");
    TCLAP::ValueArg<std::string> punc_config(
        "", PUNC_CONFIG_PATH, "punc config path", false, "", "string");
    TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "wave file path", false,
                                          "", "string");
    TCLAP::ValueArg<std::string> wav_scp("", WAV_SCP, "wave scp path", false,
                                         "", "string");
    TCLAP::ValueArg<std::string> model_dir(
        "", MODEL_DIR,
        "the asr model path, which contains model.onnx, config.yaml, am.mvn",
        true, "", "string");
    TCLAP::ValueArg<std::string> quantize(
        "", QUANTIZE,
        "false (Default), load the model of model.onnx in model_dir. If set "
        "true, load the model of model_quant.onnx in model_dir",
        false, "false", "string");
    TCLAP::ValueArg<std::string> vad_dir(
        "", VAD_DIR,
        "the vad model path, which contains model.onnx, vad.yaml, vad.mvn",
        false, "", "string");
    TCLAP::ValueArg<std::string> vad_quant(
        "", VAD_QUANT,
        "false (Default), load the model of model.onnx in vad_dir. If set "
        "true, load the model of model_quant.onnx in vad_dir",
        false, "false", "string");
    TCLAP::ValueArg<std::string> punc_dir(
        "", PUNC_DIR,
        "the punc model path, which contains model.onnx, punc.yaml", false, "",
        "string");
    TCLAP::ValueArg<std::string> punc_quant(
        "", PUNC_QUANT,
        "false (Default), load the model of model.onnx in punc_dir. If set "
        "true, load the model of model_quant.onnx in punc_dir",
        false, "false", "string");
    TCLAP::ValueArg<std::string> listen_ip("", "listen_ip", "listen_ip", false,
                                           "0.0.0.0", "string");
@@ -63,16 +64,21 @@
    TCLAP::ValueArg<int> model_thread_num("", "model_thread_num",
                                          "model_thread_num", false, 1, "int");
    cmd.add(vad_model);
    cmd.add(vad_cmvn);
    cmd.add(vad_config);
    cmd.add(am_model);
    cmd.add(am_cmvn);
    cmd.add(am_config);
    cmd.add(punc_model);
    cmd.add(punc_config);
    cmd.add(wav_path);
    cmd.add(wav_scp);
    TCLAP::ValueArg<std::string> certfile("", "certfile", "certfile", false, "",
                                          "string");
    TCLAP::ValueArg<std::string> keyfile("", "keyfile", "keyfile", false, "",
                                         "string");
    cmd.add(certfile);
    cmd.add(keyfile);
    cmd.add(model_dir);
    cmd.add(quantize);
    cmd.add(vad_dir);
    cmd.add(vad_quant);
    cmd.add(punc_dir);
    cmd.add(punc_quant);
    cmd.add(listen_ip);
    cmd.add(port);
    cmd.add(io_thread_num);
@@ -81,18 +87,12 @@
    cmd.parse(argc, argv);
    std::map<std::string, std::string> model_path;
    GetValue(vad_model, VAD_MODEL_PATH, model_path);
    GetValue(vad_cmvn, VAD_CMVN_PATH, model_path);
    GetValue(vad_config, VAD_CONFIG_PATH, model_path);
    GetValue(am_model, AM_MODEL_PATH, model_path);
    GetValue(am_cmvn, AM_CMVN_PATH, model_path);
    GetValue(am_config, AM_CONFIG_PATH, model_path);
    GetValue(punc_model, PUNC_MODEL_PATH, model_path);
    GetValue(punc_config, PUNC_CONFIG_PATH, model_path);
    GetValue(wav_path, WAV_PATH, model_path);
    GetValue(wav_scp, WAV_SCP, model_path);
    //  /funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/"
    GetValue(model_dir, MODEL_DIR, model_path);
    GetValue(quantize, QUANTIZE, model_path);
    GetValue(vad_dir, VAD_DIR, model_path);
    GetValue(vad_quant, VAD_QUANT, model_path);
    GetValue(punc_dir, PUNC_DIR, model_path);
    GetValue(punc_quant, PUNC_QUANT, model_path);
    std::string s_listen_ip = listen_ip.getValue();
    int s_port = port.getValue();
@@ -101,10 +101,17 @@
    int s_model_thread_num = model_thread_num.getValue();
    // asio::io_context io_thread;  // context for network
    asio::io_context io_decoder;  // context for decoding
    std::vector<std::thread> decoder_threads;
    std::string s_certfile = certfile.getValue();
    std::string s_keyfile = keyfile.getValue();
    bool is_ssl = false;
    if (!s_certfile.empty()) {
      is_ssl = true;
    }
    auto conn_guard = asio::make_work_guard(
        io_decoder);  // make sure threads can wait in the queue
@@ -114,30 +121,55 @@
      decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
    }
    server server_;       // server for websocket
    server_.init_asio();  // init asio
    server_.set_reuse_addr(
        true);  // reuse address as we create multiple threads
    server server_;  // server for websocket
    wss_server wss_server_;
    if (is_ssl) {
      wss_server_.init_asio();  // init asio
      wss_server_.set_reuse_addr(
          true);  // reuse address as we create multiple threads
    // list on port for accept
    server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
      // list on port for accept
      wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
      WebSocketServer websocket_srv(
          io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
          s_keyfile);  // websocket server for asr engine
      websocket_srv.initAsr(model_path, s_model_thread_num);  // init asr model
    WebSocketServer websocket_srv(io_decoder,
                                  &server_);  // websocket server for asr engine
    websocket_srv.initAsr(model_path, s_model_thread_num);  // init asr model
    } else {
      server_.init_asio();  // init asio
      server_.set_reuse_addr(
          true);  // reuse address as we create multiple threads
      // list on port for accept
      server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
      WebSocketServer websocket_srv(
          io_decoder, is_ssl, &server_, nullptr, s_certfile,
          s_keyfile);  // websocket server for asr engine
      websocket_srv.initAsr(model_path, s_model_thread_num);  // init asr model
    }
    std::cout << "asr model init finished. listen on port:" << s_port
              << std::endl;
    // Start the ASIO network io_service run loop
    if (s_io_thread_num == 1) {
      server_.run();
      if (is_ssl) {
        wss_server_.run();
      } else {
        server_.run();
      }
    } else {
      typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
      std::vector<thread_ptr> ts;
      // create threads for io network
      for (size_t i = 0; i < s_io_thread_num; i++) {
        ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(
            &server::run, &server_));
        if (is_ssl) {
          ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(
              &wss_server::run, &wss_server_));
        } else {
          ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(
              &server::run, &server_));
        }
      }
      // wait for theads
      for (size_t i = 0; i < s_io_thread_num; i++) {