|
| 1 | +// sherpa-onnx/csrc/online-websocket-server-app.cc |
| 2 | +// |
| 3 | +// Copyright (c) 2022-2023 Xiaomi Corporation |
| 4 | +// Copyright (c) 2025 Uniphore (Author: Manickavela A) |
| 5 | + |
| 6 | +#include "sherpa-onnx/csrc/online-websocket-server.h" |
| 7 | + |
| 8 | +#include <csignal> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include "asio.hpp" // NOLINT |
| 12 | +#include "sherpa-onnx/csrc/macros.h" |
| 13 | +#include "sherpa-onnx/csrc/online-websocket-server-impl.h" |
| 14 | +#include "sherpa-onnx/csrc/parse-options.h" |
| 15 | + |
| 16 | +static constexpr const char *kUsageMessage = R"( |
| 17 | +Automatic speech recognition with sherpa-onnx using websocket. |
| 18 | +
|
| 19 | +Usage: |
| 20 | +
|
| 21 | +./bin/sherpa-onnx-online-websocket-server --help |
| 22 | +
|
| 23 | +./bin/sherpa-onnx-online-websocket-server \ |
| 24 | + --port=6006 \ |
| 25 | + --num-work-threads=5 \ |
| 26 | + --tokens=/path/to/tokens.txt \ |
| 27 | + --encoder=/path/to/encoder.onnx \ |
| 28 | + --decoder=/path/to/decoder.onnx \ |
| 29 | + --joiner=/path/to/joiner.onnx \ |
| 30 | + --log-file=./log.txt \ |
| 31 | + --max-batch-size=5 \ |
| 32 | + --loop-interval-ms=10 |
| 33 | +
|
| 34 | +Please refer to |
| 35 | +https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html |
| 36 | +for a list of pre-trained models to download. |
| 37 | +)"; |
| 38 | + |
| 39 | +// Global pointer for signal handling |
| 40 | +static OnlineWebsocketServerApp *g_server_app = nullptr; |
| 41 | + |
| 42 | +static void SignalHandler(int32_t signal) { |
| 43 | + SHERPA_ONNX_LOGE("Caught signal %d, stopping server...", signal); |
| 44 | + if (g_server_app != nullptr) { |
| 45 | + g_server_app->Stop(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +OnlineWebsocketServerApp::OnlineWebsocketServerApp(int32_t argc, char *argv[]) |
| 50 | + : argc_(argc), argv_(argv) { |
| 51 | + g_server_app = this; |
| 52 | +} |
| 53 | + |
| 54 | +void OnlineWebsocketServerApp::Run() { |
| 55 | + sherpa_onnx::ParseOptions po(kUsageMessage); |
| 56 | + |
| 57 | + sherpa_onnx::OnlineWebsocketServerConfig config; |
| 58 | + |
| 59 | + // the server will listen on this port |
| 60 | + int32_t port = 6006; |
| 61 | + |
| 62 | + // size of the thread pool for handling network connections |
| 63 | + int32_t num_io_threads = 1; |
| 64 | + |
| 65 | + // size of the thread pool for neural network computation and decoding |
| 66 | + int32_t num_work_threads = 3; |
| 67 | + |
| 68 | + po.Register("num-io-threads", &num_io_threads, |
| 69 | + "Thread pool size for network connections."); |
| 70 | + |
| 71 | + po.Register("num-work-threads", &num_work_threads, |
| 72 | + "Thread pool size for for neural network " |
| 73 | + "computation and decoding."); |
| 74 | + |
| 75 | + po.Register("port", &port, "The port on which the server will listen."); |
| 76 | + |
| 77 | + config.Register(&po); |
| 78 | + |
| 79 | + if (argc_ == 1) { |
| 80 | + po.PrintUsage(); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + po.Read(argc_, argv_); |
| 85 | + |
| 86 | + if (po.NumArgs() != 0) { |
| 87 | + SHERPA_ONNX_LOGE("Unrecognized positional arguments!"); |
| 88 | + po.PrintUsage(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + config.Validate(); |
| 93 | + |
| 94 | + // Register signal handlers |
| 95 | + std::signal(SIGINT, SignalHandler); |
| 96 | + std::signal(SIGTERM, SignalHandler); |
| 97 | + |
| 98 | + sherpa_onnx::OnlineWebsocketServer server(io_conn_, io_work_, config); |
| 99 | + server.Run(port); |
| 100 | + |
| 101 | + SHERPA_ONNX_LOGE("Started!"); |
| 102 | + SHERPA_ONNX_LOGE("Listening on: %d", port); |
| 103 | + SHERPA_ONNX_LOGE("Number of work threads: %d", num_work_threads); |
| 104 | + |
| 105 | + // give some work to do for the io_work pool |
| 106 | + auto work_guard = asio::make_work_guard(io_work_); |
| 107 | + |
| 108 | + // decrement since the main thread is also used for network communications |
| 109 | + for (int32_t i = 0; i < num_io_threads - 1; ++i) { |
| 110 | + io_threads_.emplace_back([this]() { io_conn_.run(); }); |
| 111 | + } |
| 112 | + |
| 113 | + for (int32_t i = 0; i < num_work_threads; ++i) { |
| 114 | + work_threads_.emplace_back([this]() { io_work_.run(); }); |
| 115 | + } |
| 116 | + |
| 117 | + io_conn_.run(); |
| 118 | + |
| 119 | + for (auto &t : io_threads_) { |
| 120 | + t.join(); |
| 121 | + } |
| 122 | + |
| 123 | + for (auto &t : work_threads_) { |
| 124 | + t.join(); |
| 125 | + } |
| 126 | + |
| 127 | + g_server_app = nullptr; |
| 128 | +} |
| 129 | + |
| 130 | +void OnlineWebsocketServerApp::Stop() { |
| 131 | + if (shutdown_requested_.exchange(true)) { |
| 132 | + return; // Already requested |
| 133 | + } |
| 134 | + |
| 135 | + SHERPA_ONNX_LOGE("Stopping server..."); |
| 136 | + io_conn_.stop(); |
| 137 | + io_work_.stop(); |
| 138 | +} |
| 139 | + |
| 140 | +void StartServer(int32_t argc, char *argv[]) { |
| 141 | + OnlineWebsocketServerApp app(argc, argv); |
| 142 | + app.Run(); |
| 143 | +} |
0 commit comments