funasr-wss-client.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaomingwork */
  6. // client for websocket, support multiple threads
  7. // ./funasr-wss-client --server-ip <string>
  8. // --port <string>
  9. // --wav-path <string>
  10. // [--thread-num <int>]
  11. // [--is-ssl <int>] [--]
  12. // [--version] [-h]
  13. // example:
  14. // ./funasr-wss-client --server-ip 127.0.0.1 --port 10095 --wav-path test.wav --thread-num 1 --is-ssl 1
  15. #define ASIO_STANDALONE 1
  16. #include <websocketpp/client.hpp>
  17. #include <websocketpp/common/thread.hpp>
  18. #include <websocketpp/config/asio_client.hpp>
  19. #include <fstream>
  20. #include <atomic>
  21. #include <thread>
  22. #include <glog/logging.h>
  23. #include "audio.h"
  24. #include "nlohmann/json.hpp"
  25. #include "tclap/CmdLine.h"
  26. /**
  27. * Define a semi-cross platform helper method that waits/sleeps for a bit.
  28. */
  29. void WaitABit() {
  30. #ifdef WIN32
  31. Sleep(200);
  32. #else
  33. usleep(200);
  34. #endif
  35. }
  36. std::atomic<int> wav_index(0);
  37. bool IsTargetFile(const std::string& filename, const std::string target) {
  38. std::size_t pos = filename.find_last_of(".");
  39. if (pos == std::string::npos) {
  40. return false;
  41. }
  42. std::string extension = filename.substr(pos + 1);
  43. return (extension == target);
  44. }
  45. typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
  46. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
  47. using websocketpp::lib::bind;
  48. using websocketpp::lib::placeholders::_1;
  49. using websocketpp::lib::placeholders::_2;
  50. context_ptr OnTlsInit(websocketpp::connection_hdl) {
  51. context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
  52. asio::ssl::context::sslv23);
  53. try {
  54. ctx->set_options(
  55. asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 |
  56. asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);
  57. } catch (std::exception& e) {
  58. LOG(ERROR) << e.what();
  59. }
  60. return ctx;
  61. }
  62. // template for tls or not config
  63. template <typename T>
  64. class WebsocketClient {
  65. public:
  66. // typedef websocketpp::client<T> client;
  67. // typedef websocketpp::client<websocketpp::config::asio_tls_client>
  68. // wss_client;
  69. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  70. WebsocketClient(int is_ssl) : m_open(false), m_done(false) {
  71. // set up access channels to only log interesting things
  72. m_client.clear_access_channels(websocketpp::log::alevel::all);
  73. m_client.set_access_channels(websocketpp::log::alevel::connect);
  74. m_client.set_access_channels(websocketpp::log::alevel::disconnect);
  75. m_client.set_access_channels(websocketpp::log::alevel::app);
  76. // Initialize the Asio transport policy
  77. m_client.init_asio();
  78. // Bind the handlers we are using
  79. using websocketpp::lib::bind;
  80. using websocketpp::lib::placeholders::_1;
  81. m_client.set_open_handler(bind(&WebsocketClient::on_open, this, _1));
  82. m_client.set_close_handler(bind(&WebsocketClient::on_close, this, _1));
  83. m_client.set_message_handler(
  84. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  85. on_message(hdl, msg);
  86. });
  87. m_client.set_fail_handler(bind(&WebsocketClient::on_fail, this, _1));
  88. m_client.clear_access_channels(websocketpp::log::alevel::all);
  89. }
  90. void on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
  91. const std::string& payload = msg->get_payload();
  92. switch (msg->get_opcode()) {
  93. case websocketpp::frame::opcode::text:
  94. total_recv=total_recv+1;
  95. LOG(INFO)<< "Thread: " << this_thread::get_id() <<",on_message = " << payload;
  96. LOG(INFO)<< "Thread: " << this_thread::get_id() << "total_recv=" << total_recv << " total_send=" <<total_send;
  97. if(total_recv==total_send)
  98. {
  99. LOG(INFO)<< "Thread: " << this_thread::get_id() << "close client";
  100. websocketpp::lib::error_code ec;
  101. m_client.close(m_hdl, websocketpp::close::status::going_away, "", ec);
  102. if (ec){
  103. LOG(ERROR)<< "Error closing connection " << ec.message();
  104. }
  105. }
  106. }
  107. }
  108. // This method will block until the connection is complete
  109. void run(const std::string& uri, const std::vector<string>& wav_list, const std::vector<string>& wav_ids, std::string hotwords) {
  110. // Create a new connection to the given URI
  111. websocketpp::lib::error_code ec;
  112. typename websocketpp::client<T>::connection_ptr con =
  113. m_client.get_connection(uri, ec);
  114. if (ec) {
  115. m_client.get_alog().write(websocketpp::log::alevel::app,
  116. "Get Connection Error: " + ec.message());
  117. return;
  118. }
  119. // Grab a handle for this connection so we can talk to it in a thread
  120. // safe manor after the event loop starts.
  121. m_hdl = con->get_handle();
  122. // Queue the connection. No DNS queries or network connections will be
  123. // made until the io_service event loop is run.
  124. m_client.connect(con);
  125. // Create a thread to run the ASIO io_service event loop
  126. websocketpp::lib::thread asio_thread(&websocketpp::client<T>::run,
  127. &m_client);
  128. bool send_hotword = true;
  129. while(true){
  130. int i = wav_index.fetch_add(1);
  131. if (i >= wav_list.size()) {
  132. break;
  133. }
  134. total_send += 1;
  135. send_wav_data(wav_list[i], wav_ids[i], hotwords, send_hotword);
  136. if(send_hotword){
  137. send_hotword = false;
  138. }
  139. }
  140. WaitABit();
  141. asio_thread.join();
  142. }
  143. // The open handler will signal that we are ready to start sending data
  144. void on_open(websocketpp::connection_hdl) {
  145. m_client.get_alog().write(websocketpp::log::alevel::app,
  146. "Connection opened, starting data!");
  147. scoped_lock guard(m_lock);
  148. m_open = true;
  149. }
  150. // The close handler will signal that we should stop sending data
  151. void on_close(websocketpp::connection_hdl) {
  152. m_client.get_alog().write(websocketpp::log::alevel::app,
  153. "Connection closed, stopping data!");
  154. scoped_lock guard(m_lock);
  155. m_done = true;
  156. }
  157. // The fail handler will signal that we should stop sending data
  158. void on_fail(websocketpp::connection_hdl) {
  159. m_client.get_alog().write(websocketpp::log::alevel::app,
  160. "Connection failed, stopping data!");
  161. scoped_lock guard(m_lock);
  162. m_done = true;
  163. }
  164. // send wav to server
  165. void send_wav_data(string wav_path, string wav_id, string hotwords, bool send_hotword) {
  166. uint64_t count = 0;
  167. std::stringstream val;
  168. funasr::Audio audio(1);
  169. int32_t sampling_rate = 16000;
  170. std::string wav_format = "pcm";
  171. if(IsTargetFile(wav_path.c_str(), "wav")){
  172. int32_t sampling_rate = -1;
  173. if(!audio.LoadWav(wav_path.c_str(), &sampling_rate))
  174. return ;
  175. }else if(IsTargetFile(wav_path.c_str(), "pcm")){
  176. if (!audio.LoadPcmwav(wav_path.c_str(), &sampling_rate))
  177. return ;
  178. }else{
  179. wav_format = "others";
  180. if (!audio.LoadOthers2Char(wav_path.c_str()))
  181. return ;
  182. }
  183. float* buff;
  184. int len;
  185. int flag = 0;
  186. bool wait = false;
  187. while (1) {
  188. {
  189. scoped_lock guard(m_lock);
  190. // If the connection has been closed, stop generating data
  191. if (m_done) {
  192. break;
  193. }
  194. // If the connection hasn't been opened yet wait a bit and retry
  195. if (!m_open) {
  196. wait = true;
  197. } else {
  198. break;
  199. }
  200. }
  201. if (wait) {
  202. // LOG(INFO) << "wait.." << m_open;
  203. WaitABit();
  204. continue;
  205. }
  206. }
  207. websocketpp::lib::error_code ec;
  208. nlohmann::json jsonbegin;
  209. nlohmann::json chunk_size = nlohmann::json::array();
  210. chunk_size.push_back(5);
  211. chunk_size.push_back(0);
  212. chunk_size.push_back(5);
  213. jsonbegin["chunk_size"] = chunk_size;
  214. jsonbegin["chunk_interval"] = 10;
  215. jsonbegin["wav_name"] = wav_id;
  216. jsonbegin["wav_format"] = wav_format;
  217. jsonbegin["is_speaking"] = true;
  218. if(send_hotword){
  219. LOG(INFO) << "hotwords: "<< hotwords;
  220. jsonbegin["hotwords"] = hotwords;
  221. }
  222. m_client.send(m_hdl, jsonbegin.dump(), websocketpp::frame::opcode::text,
  223. ec);
  224. // fetch wav data use asr engine api
  225. if(wav_format == "pcm"){
  226. while (audio.Fetch(buff, len, flag) > 0) {
  227. short* iArray = new short[len];
  228. for (size_t i = 0; i < len; ++i) {
  229. iArray[i] = (short)(buff[i]*32768);
  230. }
  231. // send data to server
  232. int offset = 0;
  233. int block_size = 102400;
  234. while(offset < len){
  235. int send_block = 0;
  236. if (offset + block_size <= len){
  237. send_block = block_size;
  238. }else{
  239. send_block = len - offset;
  240. }
  241. m_client.send(m_hdl, iArray+offset, send_block * sizeof(short),
  242. websocketpp::frame::opcode::binary, ec);
  243. offset += send_block;
  244. }
  245. LOG(INFO) << "sended data len=" << len * sizeof(short);
  246. // The most likely error that we will get is that the connection is
  247. // not in the right state. Usually this means we tried to send a
  248. // message to a connection that was closed or in the process of
  249. // closing. While many errors here can be easily recovered from,
  250. // in this simple example, we'll stop the data loop.
  251. if (ec) {
  252. m_client.get_alog().write(websocketpp::log::alevel::app,
  253. "Send Error: " + ec.message());
  254. break;
  255. }
  256. delete[] iArray;
  257. // WaitABit();
  258. }
  259. }else{
  260. int offset = 0;
  261. int block_size = 204800;
  262. len = audio.GetSpeechLen();
  263. char* others_buff = audio.GetSpeechChar();
  264. while(offset < len){
  265. int send_block = 0;
  266. if (offset + block_size <= len){
  267. send_block = block_size;
  268. }else{
  269. send_block = len - offset;
  270. }
  271. m_client.send(m_hdl, others_buff+offset, send_block,
  272. websocketpp::frame::opcode::binary, ec);
  273. offset += send_block;
  274. }
  275. LOG(INFO) << "sended data len=" << len;
  276. // The most likely error that we will get is that the connection is
  277. // not in the right state. Usually this means we tried to send a
  278. // message to a connection that was closed or in the process of
  279. // closing. While many errors here can be easily recovered from,
  280. // in this simple example, we'll stop the data loop.
  281. if (ec) {
  282. m_client.get_alog().write(websocketpp::log::alevel::app,
  283. "Send Error: " + ec.message());
  284. }
  285. }
  286. nlohmann::json jsonresult;
  287. jsonresult["is_speaking"] = false;
  288. m_client.send(m_hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  289. ec);
  290. std::this_thread::sleep_for(std::chrono::milliseconds(20));
  291. }
  292. websocketpp::client<T> m_client;
  293. private:
  294. websocketpp::connection_hdl m_hdl;
  295. websocketpp::lib::mutex m_lock;
  296. bool m_open;
  297. bool m_done;
  298. int total_send=0;
  299. int total_recv=0;
  300. };
  301. int main(int argc, char* argv[]) {
  302. google::InitGoogleLogging(argv[0]);
  303. FLAGS_logtostderr = true;
  304. TCLAP::CmdLine cmd("funasr-wss-client", ' ', "1.0");
  305. TCLAP::ValueArg<std::string> server_ip_("", "server-ip", "server-ip", true,
  306. "127.0.0.1", "string");
  307. TCLAP::ValueArg<std::string> port_("", "port", "port", true, "10095", "string");
  308. TCLAP::ValueArg<std::string> wav_path_("", "wav-path",
  309. "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)",
  310. true, "", "string");
  311. TCLAP::ValueArg<int> thread_num_("", "thread-num", "thread-num",
  312. false, 1, "int");
  313. TCLAP::ValueArg<int> is_ssl_(
  314. "", "is-ssl", "is-ssl is 1 means use wss connection, or use ws connection",
  315. false, 1, "int");
  316. TCLAP::ValueArg<std::string> hotword_("", HOTWORD, "*.txt(one hotword perline) or hotwords seperate by space (could be: 阿里巴巴 达摩院)", false, "", "string");
  317. cmd.add(server_ip_);
  318. cmd.add(port_);
  319. cmd.add(wav_path_);
  320. cmd.add(thread_num_);
  321. cmd.add(is_ssl_);
  322. cmd.add(hotword_);
  323. cmd.parse(argc, argv);
  324. std::string server_ip = server_ip_.getValue();
  325. std::string port = port_.getValue();
  326. std::string wav_path = wav_path_.getValue();
  327. int threads_num = thread_num_.getValue();
  328. int is_ssl = is_ssl_.getValue();
  329. std::vector<websocketpp::lib::thread> client_threads;
  330. std::string uri = "";
  331. if (is_ssl == 1) {
  332. uri = "wss://" + server_ip + ":" + port;
  333. } else {
  334. uri = "ws://" + server_ip + ":" + port;
  335. }
  336. // read hotwords
  337. std::string hotword = hotword_.getValue();
  338. std::string hotwords_;
  339. if(IsTargetFile(hotword, "txt")){
  340. ifstream in(hotword);
  341. if (!in.is_open()) {
  342. LOG(ERROR) << "Failed to open file: " << hotword;
  343. return 0;
  344. }
  345. string line;
  346. while(getline(in, line))
  347. {
  348. hotwords_ +=line+HOTWORD_SEP;
  349. }
  350. in.close();
  351. }else{
  352. hotwords_ = hotword;
  353. }
  354. // read wav_path
  355. std::vector<string> wav_list;
  356. std::vector<string> wav_ids;
  357. string default_id = "wav_default_id";
  358. if(IsTargetFile(wav_path, "scp")){
  359. ifstream in(wav_path);
  360. if (!in.is_open()) {
  361. printf("Failed to open scp file");
  362. return 0;
  363. }
  364. string line;
  365. while(getline(in, line))
  366. {
  367. istringstream iss(line);
  368. string column1, column2;
  369. iss >> column1 >> column2;
  370. wav_list.emplace_back(column2);
  371. wav_ids.emplace_back(column1);
  372. }
  373. in.close();
  374. }else{
  375. wav_list.emplace_back(wav_path);
  376. wav_ids.emplace_back(default_id);
  377. }
  378. for (size_t i = 0; i < threads_num; i++) {
  379. client_threads.emplace_back([uri, wav_list, wav_ids, is_ssl, hotwords_]() {
  380. if (is_ssl == 1) {
  381. WebsocketClient<websocketpp::config::asio_tls_client> c(is_ssl);
  382. c.m_client.set_tls_init_handler(bind(&OnTlsInit, ::_1));
  383. c.run(uri, wav_list, wav_ids, hotwords_);
  384. } else {
  385. WebsocketClient<websocketpp::config::asio_client> c(is_ssl);
  386. c.run(uri, wav_list, wav_ids, hotwords_);
  387. }
  388. });
  389. }
  390. for (auto& t : client_threads) {
  391. t.join();
  392. }
  393. }