From 9a0e16f092bc4479d3608e4dc2c919c661e7fc9a Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 13 May 2025 14:49:22 +0800 Subject: [PATCH] Support sending is_eof for online websocket server. (#2204) is_final=true means an endpoint is detected. is_eof=true means all received samples have been processed by the server. --- sherpa-onnx/csrc/online-recognizer.cc | 3 ++- sherpa-onnx/csrc/online-recognizer.h | 8 +++++++- sherpa-onnx/csrc/online-websocket-server-impl.cc | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sherpa-onnx/csrc/online-recognizer.cc b/sherpa-onnx/csrc/online-recognizer.cc index 3b5b30ec..03d15968 100644 --- a/sherpa-onnx/csrc/online-recognizer.cc +++ b/sherpa-onnx/csrc/online-recognizer.cc @@ -77,7 +77,8 @@ std::string OnlineRecognizerResult::AsJsonString() const { os << "\"words\": " << VecToString(words, 0) << ", "; os << "\"start_time\": " << std::fixed << std::setprecision(2) << start_time << ", "; - os << "\"is_final\": " << (is_final ? "true" : "false"); + os << "\"is_final\": " << (is_final ? "true" : "false") << ", "; + os << "\"is_eof\": " << (is_eof ? "true" : "false"); os << "}"; return os.str(); } diff --git a/sherpa-onnx/csrc/online-recognizer.h b/sherpa-onnx/csrc/online-recognizer.h index d52b877c..09e2c5f6 100644 --- a/sherpa-onnx/csrc/online-recognizer.h +++ b/sherpa-onnx/csrc/online-recognizer.h @@ -53,9 +53,14 @@ struct OnlineRecognizerResult { /// When an endpoint is detected, it will change float start_time = 0; - /// True if the end of this segment is reached + /// True if the end of this segment is reached, i.e., an endpoint is detected + /// used only in ./online-websocket-server-impl.cc bool is_final = false; + /// used only in ./online-websocket-server-impl.cc + /// If it is true, it means the server has processed all received samples + bool is_eof = false; + /** Return a json string. * * The returned string contains: @@ -69,6 +74,7 @@ struct OnlineRecognizerResult { * "segment": x, * "start_time": x, * "is_final": true|false + * "is_eof": true|false * } */ std::string AsJsonString() const; diff --git a/sherpa-onnx/csrc/online-websocket-server-impl.cc b/sherpa-onnx/csrc/online-websocket-server-impl.cc index 11651f07..8925ebb4 100644 --- a/sherpa-onnx/csrc/online-websocket-server-impl.cc +++ b/sherpa-onnx/csrc/online-websocket-server-impl.cc @@ -210,6 +210,7 @@ void OnlineWebsocketDecoder::Decode() { if (!recognizer_->IsReady(c->s.get()) && c->eof) { result.is_final = true; + result.is_eof = true; } asio::post(server_->GetConnectionContext(),