From 9fcb3cc06b4e324f0913d2f61b89becc2baeef1b Mon Sep 17 00:00:00 2001
From: hnluo <haoneng.lhn@alibaba-inc.com>
Date: 星期一, 11 九月 2023 17:40:03 +0800
Subject: [PATCH] Merge pull request #932 from alibaba-damo-academy/dev_lhn

---
 funasr/runtime/websocket/websocket-server-2pass.cpp |   42 ++++++++++++++++++++++++++++--------------
 1 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/funasr/runtime/websocket/websocket-server-2pass.cpp b/funasr/runtime/websocket/websocket-server-2pass.cpp
index e6e6c51..028879a 100644
--- a/funasr/runtime/websocket/websocket-server-2pass.cpp
+++ b/funasr/runtime/websocket/websocket-server-2pass.cpp
@@ -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,8 +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);
+  	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;
   }
@@ -226,7 +242,6 @@
   // 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);
@@ -247,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
@@ -293,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);
+      }
     }
   }
 }
@@ -360,8 +374,8 @@
 
         // 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())),
@@ -400,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),
@@ -436,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