From 2a66366be4c2715870e4859fd5a5db6e8a9dc00a Mon Sep 17 00:00:00 2001
From: chenmengzheAAA <123789350+chenmengzheAAA@users.noreply.github.com>
Date: 星期四, 14 九月 2023 19:00:17 +0800
Subject: [PATCH] Merge pull request #956 from alibaba-damo-academy/chenmengzheAAA-patch-4
---
funasr/runtime/websocket/websocket-server-2pass.cpp | 52 ++++++++++++++++++++++++++++------------------------
1 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/funasr/runtime/websocket/websocket-server-2pass.cpp b/funasr/runtime/websocket/websocket-server-2pass.cpp
index 233dfae..028879a 100644
--- a/funasr/runtime/websocket/websocket-server-2pass.cpp
+++ b/funasr/runtime/websocket/websocket-server-2pass.cpp
@@ -143,7 +143,7 @@
FunASRFreeResult(Result);
}
}
- if (is_final) {
+ if (is_final && !msg["is_eof"]) {
try {
if (tpass_online_handle) {
Result = FunTpassInferBuffer(tpass_handle, tpass_online_handle,
@@ -177,6 +177,21 @@
websocketpp::frame::opcode::text, ec);
}
FunASRFreeResult(Result);
+ }else{
+ if(msg["wav_format"] != "pcm" && msg["wav_format"] != "PCM"){
+ websocketpp::lib::error_code ec;
+ nlohmann::json jsonresult;
+ jsonresult["text"] = "ERROR. Real-time transcription service ONLY SUPPORT wav_format pcm.";
+ jsonresult["wav_name"] = wav_name;
+ jsonresult["is_final"] = true;
+ if (is_ssl) {
+ wss_server_->send(hdl, jsonresult.dump(),
+ websocketpp::frame::opcode::text, ec);
+ } else {
+ server_->send(hdl, jsonresult.dump(),
+ websocketpp::frame::opcode::text, ec);
+ }
+ }
}
}
@@ -203,12 +218,9 @@
data_msg->msg["is_eof"]=false; // if this connection is closed
data_msg->punc_cache =
std::make_shared<std::vector<std::vector<std::string>>>(2);
- // std::vector<int> chunk_size = {5, 10, 5}; //TODO, need get from client
- // FUNASR_HANDLE tpass_online_handle =
- // FunTpassOnlineInit(tpass_handle, chunk_size);
- // data_msg->tpass_online_handle = tpass_online_handle;
+ data_msg->strand_ = std::make_shared<asio::io_context::strand>(io_decoder_);
+
data_map.emplace(hdl, data_msg);
- LOG(INFO) << "on_open, active connections: " << data_map.size();
}catch (std::exception const& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
@@ -230,14 +242,12 @@
// finished and avoid access freed tpass_online_handle
unique_lock guard_decoder(*(data_msg->thread_lock));
if (data_msg->msg["access_num"]==0 && data_msg->msg["is_eof"]==true) {
- LOG(INFO) << "----------------FunTpassOnlineUninit----------------------";
FunTpassOnlineUninit(data_msg->tpass_online_handle);
data_msg->tpass_online_handle = nullptr;
data_map.erase(hdl);
}
guard_decoder.unlock();
- // remove data vector when connection is closed
}
void WebSocketServer::on_close(websocketpp::connection_hdl hdl) {
@@ -252,15 +262,12 @@
unique_lock guard_decoder(*(data_msg->thread_lock));
data_msg->msg["is_eof"]=true;
guard_decoder.unlock();
- //LOG(INFO) << "on_close, active connections: " << data_map.size();
}
// remove closed connection
void WebSocketServer::check_and_clean_connection() {
- LOG(INFO)<<"***********begin check_and_clean_connection ****************";
while(true){
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
- LOG(INFO) << "run check_and_clean_connection" <<", active connections: " << data_map.size();;
std::vector<websocketpp::connection_hdl> to_remove; // remove list
auto iter = data_map.begin();
while (iter != data_map.end()) { // loop to find closed connection
@@ -291,7 +298,6 @@
unique_lock guard_decoder(*(data_msg->thread_lock));
data_msg->msg["is_eof"]=true;
guard_decoder.unlock();
- //remove_hdl(hdl, data_map);
to_remove.push_back(hdl);
LOG(INFO)<<"connection is closed: "<<e.what();
@@ -299,8 +305,10 @@
iter++;
}
for (auto hdl : to_remove) {
- remove_hdl(hdl, data_map);
- LOG(INFO) << "remove one connection ";
+ {
+ unique_lock lock(m_lock);
+ remove_hdl(hdl, data_map);
+ }
}
}
}
@@ -353,8 +361,6 @@
if (msg_data->tpass_online_handle == NULL) {
std::vector<int> chunk_size_vec =
jsonresult["chunk_size"].get<std::vector<int>>();
- LOG(INFO)
- << "----------------FunTpassOnlineInit----------------------";
FUNASR_HANDLE tpass_online_handle =
FunTpassOnlineInit(tpass_handle, chunk_size_vec);
msg_data->tpass_online_handle = tpass_online_handle;
@@ -369,8 +375,7 @@
// if it is in final message, post the sample_data to decode
try{
- asio::post(
- io_decoder_,
+ msg_data->strand_->post(
std::bind(&WebSocketServer::do_decoder, this,
std::move(*(sample_data_p.get())), std::move(hdl),
std::ref(msg_data->msg), std::ref(*(punc_cache_p.get())),
@@ -382,7 +387,6 @@
catch (std::exception const &e)
{
LOG(ERROR)<<e.what();
-
}
}
break;
@@ -410,7 +414,7 @@
try{
// post to decode
- asio::post(io_decoder_,
+ msg_data->strand_->post(
std::bind(&WebSocketServer::do_decoder, this,
std::move(subvector), std::move(hdl),
std::ref(msg_data->msg),
@@ -446,10 +450,10 @@
LOG(ERROR) << "FunTpassInit init failed";
exit(-1);
}
- LOG(INFO) << "initAsr run check_and_clean_connection";
- std::thread clean_thread(&WebSocketServer::check_and_clean_connection,this);
- clean_thread.detach();
- LOG(INFO) << "initAsr run check_and_clean_connection finished";
+ LOG(INFO) << "initAsr run check_and_clean_connection";
+ std::thread clean_thread(&WebSocketServer::check_and_clean_connection,this);
+ clean_thread.detach();
+ LOG(INFO) << "initAsr run check_and_clean_connection finished";
} catch (const std::exception& e) {
LOG(INFO) << e.what();
--
Gitblit v1.9.1