funasr-wss-client.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <glog/logging.h>
  22. #include "audio.h"
  23. #include "nlohmann/json.hpp"
  24. #include "tclap/CmdLine.h"
  25. /**
  26. * Define a semi-cross platform helper method that waits/sleeps for a bit.
  27. */
  28. void WaitABit() {
  29. #ifdef WIN32
  30. Sleep(1000);
  31. #else
  32. sleep(1);
  33. #endif
  34. }
  35. std::atomic<int> wav_index(0);
  36. bool IsTargetFile(const std::string& filename, const std::string target) {
  37. std::size_t pos = filename.find_last_of(".");
  38. if (pos == std::string::npos) {
  39. return false;
  40. }
  41. std::string extension = filename.substr(pos + 1);
  42. return (extension == target);
  43. }
  44. typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
  45. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context> context_ptr;
  46. using websocketpp::lib::bind;
  47. using websocketpp::lib::placeholders::_1;
  48. using websocketpp::lib::placeholders::_2;
  49. context_ptr OnTlsInit(websocketpp::connection_hdl) {
  50. context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
  51. asio::ssl::context::sslv23);
  52. try {
  53. ctx->set_options(
  54. asio::ssl::context::default_workarounds | asio::ssl::context::no_sslv2 |
  55. asio::ssl::context::no_sslv3 | asio::ssl::context::single_dh_use);
  56. } catch (std::exception& e) {
  57. LOG(ERROR) << e.what();
  58. }
  59. return ctx;
  60. }
  61. // template for tls or not config
  62. template <typename T>
  63. class WebsocketClient {
  64. public:
  65. // typedef websocketpp::client<T> client;
  66. // typedef websocketpp::client<websocketpp::config::asio_tls_client>
  67. // wss_client;
  68. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  69. WebsocketClient(int is_ssl) : m_open(false), m_done(false) {
  70. // set up access channels to only log interesting things
  71. m_client.clear_access_channels(websocketpp::log::alevel::all);
  72. m_client.set_access_channels(websocketpp::log::alevel::connect);
  73. m_client.set_access_channels(websocketpp::log::alevel::disconnect);
  74. m_client.set_access_channels(websocketpp::log::alevel::app);
  75. // Initialize the Asio transport policy
  76. m_client.init_asio();
  77. // Bind the handlers we are using
  78. using websocketpp::lib::bind;
  79. using websocketpp::lib::placeholders::_1;
  80. m_client.set_open_handler(bind(&WebsocketClient::on_open, this, _1));
  81. m_client.set_close_handler(bind(&WebsocketClient::on_close, this, _1));
  82. m_client.set_message_handler(
  83. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  84. on_message(hdl, msg);
  85. });
  86. m_client.set_fail_handler(bind(&WebsocketClient::on_fail, this, _1));
  87. m_client.clear_access_channels(websocketpp::log::alevel::all);
  88. }
  89. void on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
  90. const std::string& payload = msg->get_payload();
  91. switch (msg->get_opcode()) {
  92. case websocketpp::frame::opcode::text:
  93. total_num=total_num+1;
  94. LOG(INFO)<<total_num<<",on_message = " << payload;
  95. if((total_num+1)==wav_index)
  96. {
  97. websocketpp::lib::error_code ec;
  98. m_client.close(m_hdl, websocketpp::close::status::going_away, "", ec);
  99. if (ec){
  100. LOG(ERROR)<< "Error closing connection " << ec.message();
  101. }
  102. }
  103. }
  104. }
  105. // This method will block until the connection is complete
  106. void run(const std::string& uri, const std::vector<string>& wav_list, const std::vector<string>& wav_ids) {
  107. // Create a new connection to the given URI
  108. websocketpp::lib::error_code ec;
  109. typename websocketpp::client<T>::connection_ptr con =
  110. m_client.get_connection(uri, ec);
  111. if (ec) {
  112. m_client.get_alog().write(websocketpp::log::alevel::app,
  113. "Get Connection Error: " + ec.message());
  114. return;
  115. }
  116. // Grab a handle for this connection so we can talk to it in a thread
  117. // safe manor after the event loop starts.
  118. m_hdl = con->get_handle();
  119. // Queue the connection. No DNS queries or network connections will be
  120. // made until the io_service event loop is run.
  121. m_client.connect(con);
  122. // Create a thread to run the ASIO io_service event loop
  123. websocketpp::lib::thread asio_thread(&websocketpp::client<T>::run,
  124. &m_client);
  125. while(true){
  126. int i = wav_index.fetch_add(1);
  127. if (i >= wav_list.size()) {
  128. break;
  129. }
  130. send_wav_data(wav_list[i], wav_ids[i]);
  131. }
  132. WaitABit();
  133. asio_thread.join();
  134. }
  135. // The open handler will signal that we are ready to start sending data
  136. void on_open(websocketpp::connection_hdl) {
  137. m_client.get_alog().write(websocketpp::log::alevel::app,
  138. "Connection opened, starting data!");
  139. scoped_lock guard(m_lock);
  140. m_open = true;
  141. }
  142. // The close handler will signal that we should stop sending data
  143. void on_close(websocketpp::connection_hdl) {
  144. m_client.get_alog().write(websocketpp::log::alevel::app,
  145. "Connection closed, stopping data!");
  146. scoped_lock guard(m_lock);
  147. m_done = true;
  148. }
  149. // The fail handler will signal that we should stop sending data
  150. void on_fail(websocketpp::connection_hdl) {
  151. m_client.get_alog().write(websocketpp::log::alevel::app,
  152. "Connection failed, stopping data!");
  153. scoped_lock guard(m_lock);
  154. m_done = true;
  155. }
  156. // send wav to server
  157. void send_wav_data(string wav_path, string wav_id) {
  158. uint64_t count = 0;
  159. std::stringstream val;
  160. funasr::Audio audio(1);
  161. int32_t sampling_rate = 16000;
  162. if(IsTargetFile(wav_path.c_str(), "wav")){
  163. int32_t sampling_rate = -1;
  164. if(!audio.LoadWav(wav_path.c_str(), &sampling_rate))
  165. return ;
  166. }else if(IsTargetFile(wav_path.c_str(), "pcm")){
  167. if (!audio.LoadPcmwav(wav_path.c_str(), &sampling_rate))
  168. return ;
  169. }else{
  170. printf("Wrong wav extension");
  171. exit(-1);
  172. }
  173. float* buff;
  174. int len;
  175. int flag = 0;
  176. bool wait = false;
  177. while (1) {
  178. {
  179. scoped_lock guard(m_lock);
  180. // If the connection has been closed, stop generating data
  181. if (m_done) {
  182. break;
  183. }
  184. // If the connection hasn't been opened yet wait a bit and retry
  185. if (!m_open) {
  186. wait = true;
  187. } else {
  188. break;
  189. }
  190. }
  191. if (wait) {
  192. // LOG(INFO) << "wait.." << m_open;
  193. WaitABit();
  194. continue;
  195. }
  196. }
  197. websocketpp::lib::error_code ec;
  198. nlohmann::json jsonbegin;
  199. nlohmann::json chunk_size = nlohmann::json::array();
  200. chunk_size.push_back(5);
  201. chunk_size.push_back(0);
  202. chunk_size.push_back(5);
  203. jsonbegin["chunk_size"] = chunk_size;
  204. jsonbegin["chunk_interval"] = 10;
  205. jsonbegin["wav_name"] = wav_id;
  206. jsonbegin["is_speaking"] = true;
  207. m_client.send(m_hdl, jsonbegin.dump(), websocketpp::frame::opcode::text,
  208. ec);
  209. // fetch wav data use asr engine api
  210. while (audio.Fetch(buff, len, flag) > 0) {
  211. short iArray[len];
  212. // convert float -1,1 to short -32768,32767
  213. for (size_t i = 0; i < len; ++i) {
  214. iArray[i] = (short)(buff[i] * 32767);
  215. }
  216. // send data to server
  217. m_client.send(m_hdl, iArray, len * sizeof(short),
  218. websocketpp::frame::opcode::binary, ec);
  219. LOG(INFO) << "sended data len=" << len * sizeof(short);
  220. // The most likely error that we will get is that the connection is
  221. // not in the right state. Usually this means we tried to send a
  222. // message to a connection that was closed or in the process of
  223. // closing. While many errors here can be easily recovered from,
  224. // in this simple example, we'll stop the data loop.
  225. if (ec) {
  226. m_client.get_alog().write(websocketpp::log::alevel::app,
  227. "Send Error: " + ec.message());
  228. break;
  229. }
  230. // WaitABit();
  231. }
  232. nlohmann::json jsonresult;
  233. jsonresult["is_speaking"] = false;
  234. m_client.send(m_hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
  235. ec);
  236. // WaitABit();
  237. }
  238. websocketpp::client<T> m_client;
  239. private:
  240. websocketpp::connection_hdl m_hdl;
  241. websocketpp::lib::mutex m_lock;
  242. bool m_open;
  243. bool m_done;
  244. int total_num=0;
  245. };
  246. int main(int argc, char* argv[]) {
  247. google::InitGoogleLogging(argv[0]);
  248. FLAGS_logtostderr = true;
  249. TCLAP::CmdLine cmd("funasr-wss-client", ' ', "1.0");
  250. TCLAP::ValueArg<std::string> server_ip_("", "server-ip", "server-ip", true,
  251. "127.0.0.1", "string");
  252. TCLAP::ValueArg<std::string> port_("", "port", "port", true, "10095", "string");
  253. TCLAP::ValueArg<std::string> wav_path_("", "wav-path",
  254. "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)",
  255. true, "", "string");
  256. TCLAP::ValueArg<int> thread_num_("", "thread-num", "thread-num",
  257. false, 1, "int");
  258. TCLAP::ValueArg<int> is_ssl_(
  259. "", "is-ssl", "is-ssl is 1 means use wss connection, or use ws connection",
  260. false, 1, "int");
  261. cmd.add(server_ip_);
  262. cmd.add(port_);
  263. cmd.add(wav_path_);
  264. cmd.add(thread_num_);
  265. cmd.add(is_ssl_);
  266. cmd.parse(argc, argv);
  267. std::string server_ip = server_ip_.getValue();
  268. std::string port = port_.getValue();
  269. std::string wav_path = wav_path_.getValue();
  270. int threads_num = thread_num_.getValue();
  271. int is_ssl = is_ssl_.getValue();
  272. std::vector<websocketpp::lib::thread> client_threads;
  273. std::string uri = "";
  274. if (is_ssl == 1) {
  275. uri = "wss://" + server_ip + ":" + port;
  276. } else {
  277. uri = "ws://" + server_ip + ":" + port;
  278. }
  279. // read wav_path
  280. std::vector<string> wav_list;
  281. std::vector<string> wav_ids;
  282. string default_id = "wav_default_id";
  283. if(IsTargetFile(wav_path, "wav") || IsTargetFile(wav_path, "pcm")){
  284. wav_list.emplace_back(wav_path);
  285. wav_ids.emplace_back(default_id);
  286. }
  287. else if(IsTargetFile(wav_path, "scp")){
  288. ifstream in(wav_path);
  289. if (!in.is_open()) {
  290. printf("Failed to open scp file");
  291. return 0;
  292. }
  293. string line;
  294. while(getline(in, line))
  295. {
  296. istringstream iss(line);
  297. string column1, column2;
  298. iss >> column1 >> column2;
  299. wav_list.emplace_back(column2);
  300. wav_ids.emplace_back(column1);
  301. }
  302. in.close();
  303. }else{
  304. printf("Please check the wav extension!");
  305. exit(-1);
  306. }
  307. for (size_t i = 0; i < threads_num; i++) {
  308. client_threads.emplace_back([uri, wav_list, wav_ids, is_ssl]() {
  309. if (is_ssl == 1) {
  310. WebsocketClient<websocketpp::config::asio_tls_client> c(is_ssl);
  311. c.m_client.set_tls_init_handler(bind(&OnTlsInit, ::_1));
  312. c.run(uri, wav_list, wav_ids);
  313. } else {
  314. WebsocketClient<websocketpp::config::asio_client> c(is_ssl);
  315. c.run(uri, wav_list, wav_ids);
  316. }
  317. });
  318. }
  319. for (auto& t : client_threads) {
  320. t.join();
  321. }
  322. }