RvcWsServer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #include <stdint.h>
  2. #include "RvcWsServer.h"
  3. #include <boost/thread/thread.hpp>
  4. #include <vector>
  5. #include <boost/thread/lock_guard.hpp>
  6. #include "SpBase.h"
  7. #include "jpeglib.h"
  8. #include "Event.h"
  9. #include "y2k_time.h"
  10. #include "CommEntityUtil.hpp"
  11. namespace LivenessDetection {
  12. #ifndef RVC_JPEG_QUALITY
  13. #define RVC_JPEG_QUALITY 100 //图片质量
  14. #endif
  15. #ifndef RVC_COLOR_PEPTH
  16. #define RVC_COLOR_PEPTH 3
  17. #endif
  18. #ifndef RVC_MAX_JPEG_SIZE
  19. #define RVC_MAX_JPEG_SIZE 256*1024
  20. #endif
  21. #ifndef RVC_PICTURE_TYPE_LEN
  22. #define RVC_PICTURE_TYPE_LEN 1
  23. #endif
  24. #ifndef RVC_MIN_VIDEO_TRANS_TIME
  25. #define RVC_MIN_VIDEO_TRANS_TIME 2
  26. #endif
  27. #ifndef RVC_MAX_VIDEO_TRANS_TIME
  28. #define RVC_MAX_VIDEO_TRANS_TIME 15
  29. #endif
  30. static int rgb2jpg_action(struct jpeg_compress_struct* pCinfo, const unsigned char* pRgbData, const int width, const int height)
  31. {
  32. int depth = RVC_COLOR_PEPTH;
  33. JSAMPROW row_pointer[1] = { 0 };
  34. pCinfo->image_width = width;
  35. pCinfo->image_height = height;
  36. pCinfo->input_components = depth;
  37. pCinfo->in_color_space = JCS_RGB;
  38. jpeg_set_defaults(pCinfo);
  39. jpeg_set_quality(pCinfo, RVC_JPEG_QUALITY, true);
  40. jpeg_start_compress(pCinfo, true);
  41. int row_stride = width * depth;
  42. while (pCinfo->next_scanline < pCinfo->image_height) {
  43. row_pointer[0] = (JSAMPROW)(pRgbData + pCinfo->next_scanline * row_stride);
  44. jpeg_write_scanlines(pCinfo, row_pointer, 1);
  45. }
  46. jpeg_finish_compress(pCinfo);
  47. jpeg_destroy_compress(pCinfo);
  48. return 0;
  49. }
  50. static int bgr2rgb(const unsigned char* bgr_image, const int image_width, const int image_height, unsigned char* rgb_image)
  51. {
  52. if (!bgr_image || !rgb_image) {
  53. return 1;
  54. }
  55. for (int i = 0; i < image_width * image_height; i++) {
  56. unsigned char b_value = *bgr_image++;
  57. unsigned char g_value = *bgr_image++;
  58. unsigned char r_value = *bgr_image++;
  59. *rgb_image++ = r_value;
  60. *rgb_image++ = g_value;
  61. *rgb_image++ = b_value;
  62. }
  63. return 0;
  64. }
  65. static int gbr2jpg(const unsigned char* pRgbData, const int width, const int height, char* pDest, unsigned long* pSize, int reverseFlag = 0)
  66. {
  67. struct jpeg_compress_struct cinfo;
  68. struct jpeg_error_mgr jerr;
  69. cinfo.err = jpeg_std_error(&jerr);
  70. jpeg_create_compress(&cinfo);
  71. jpeg_mem_dest(&cinfo, (unsigned char**)&pDest, (unsigned long*)pSize);
  72. unsigned char* prgb = new unsigned char[width * height * 3];
  73. bgr2rgb(pRgbData, width, height, prgb);
  74. #if defined(RVC_OS_WIN)
  75. rgb2jpg_action(&cinfo, prgb, width, height);
  76. #else
  77. unsigned char* reversePrgb = new unsigned char[width * height * 3];
  78. if (reverseFlag == 0)
  79. {
  80. for (int i = 0; i < height; i++) {
  81. memcpy(reversePrgb + (i * width * 3),
  82. prgb + (3 * width * (height - (i + 1))), width * 3);
  83. }
  84. }
  85. else if (reverseFlag == 1)
  86. {
  87. for (int i = 0; i < height; i++) {
  88. for (int j = 0; j < width; j++) {
  89. memcpy(reversePrgb + (3 * ((width * i) + j)),
  90. prgb + (3 * ((width * i) + (width - (j + 1)))), 3);
  91. }
  92. }
  93. }
  94. rgb2jpg_action(&cinfo, reversePrgb, width, height);
  95. delete reversePrgb;
  96. reversePrgb = NULL;
  97. #endif //RVC_OS_WIN
  98. delete prgb;
  99. prgb = NULL;
  100. return 0;
  101. }
  102. static int on_send(RvcWsServer* webserver, eVideoType eType)
  103. {
  104. int iwidth = 0;
  105. int iheight = 0;
  106. int isize = 0;
  107. if (webserver->m_callback.on_get_videodata) {
  108. if (ePreview_Type == eType) {
  109. isize = webserver->m_callback.on_get_videodata(ePreview_Type, webserver->m_ecameraid, &iwidth, &iheight, webserver->m_buffer, webserver->m_ubuffer_size, webserver->m_callback.user_data);
  110. }
  111. else {
  112. isize = webserver->m_callback.on_get_videodata(eCapture_Type, webserver->m_ecameraid, &iwidth, &iheight, webserver->m_capbuffer, webserver->m_ucapbuffer_size, webserver->m_callback.user_data);
  113. }
  114. if (isize > 0) {
  115. char pDest[RVC_MAX_JPEG_SIZE] = { 0 };
  116. unsigned long size = RVC_MAX_JPEG_SIZE;
  117. if (ePreview_Type == eType) {
  118. pDest[0] = 0x01;
  119. #if defined(RVC_OS_WIN)
  120. gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size);
  121. #else
  122. if (webserver->m_ecameraid == 0)
  123. {
  124. gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size, 0);
  125. }
  126. else if (webserver->m_ecameraid == 1)
  127. {
  128. gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size, 1);
  129. }
  130. #endif //RVC_OS_WIN
  131. }
  132. else {
  133. pDest[0] = 0x02;
  134. gbr2jpg(webserver->m_capbuffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size);
  135. }
  136. if (webserver->m_bconnected && webserver->m_bstarttrans) {
  137. if (false == webserver->m_hdl.expired()) {
  138. webserver->m_wsserver.send(webserver->m_hdl, (const void*)pDest, size + RVC_PICTURE_TYPE_LEN, websocketpp::frame::opcode::value::binary);
  139. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("send picture size is %u.", size+1);
  140. }
  141. }
  142. }
  143. else {
  144. if (ePreview_Type == eType) {
  145. if (false == webserver->m_preview_error_log) {
  146. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E06")("视频流传输获取预览图像失败");
  147. webserver->m_preview_error_log = true;
  148. }
  149. }
  150. else {
  151. if (false == webserver->m_capture_error_log) {
  152. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E07")("视频流传输获取抓拍图像失败");
  153. webserver->m_capture_error_log = true;
  154. }
  155. }
  156. }
  157. }
  158. return 0;
  159. }
  160. static void process(RvcWsServer* webserver)
  161. {
  162. while (webserver->m_bconnected && webserver->m_bstarttrans)
  163. {
  164. #ifdef RVC_OS_WIN
  165. DWORD dwRet = WaitForSingleObject(webserver->m_evt, 1000 / webserver->m_fps);
  166. if (dwRet == WAIT_OBJECT_0) {
  167. break;
  168. }
  169. else if (dwRet == WAIT_TIMEOUT) {
  170. if (webserver->m_bconnected && webserver->m_bstarttrans) {
  171. on_send(webserver, ePreview_Type);
  172. }
  173. }
  174. #else
  175. struct timespec ts;
  176. clock_gettime(CLOCK_REALTIME, &ts);
  177. long unsec = ts.tv_nsec + (1000 * 1000 * (1000 / webserver->m_fps));
  178. ts.tv_sec += (unsec / 1000000000);
  179. ts.tv_nsec = (unsec % 1000000000);
  180. if (0 != sem_timedwait(&webserver->m_semt, &ts) && (ETIMEDOUT == errno)) {
  181. if (webserver->m_bconnected && webserver->m_bstarttrans) {
  182. on_send(webserver, ePreview_Type);
  183. }
  184. }
  185. else
  186. {
  187. break;
  188. }
  189. #endif // RVC_OS_WIN
  190. }
  191. webserver->m_bstarttrans = false;
  192. }
  193. #if defined(RVC_OS_WIN)
  194. static unsigned int __stdcall work_proc(void* arg)
  195. #else
  196. static void* work_proc(void* arg)
  197. #endif //RVC_OS_WIN
  198. {
  199. RvcWsServer* webserver = (RvcWsServer*)arg;
  200. process(webserver);
  201. return 0;
  202. }
  203. RvcWsServer::RvcWsServer(void) :m_wsserver()
  204. {
  205. m_bconnected = false;
  206. m_struuid = "";
  207. m_bstarttrans = false;
  208. #if defined(RVC_OS_WIN)
  209. m_work_thread = NULL;
  210. #else
  211. m_work_threadid = 0;
  212. #endif //RVC_OS_WIN
  213. m_cameraid = 0;
  214. m_fps = RVC_DEFAULT_FPS;
  215. m_buffer = NULL;
  216. m_ubuffer_size = 0;
  217. m_capbuffer = NULL;
  218. m_ucapbuffer_size = 0;
  219. m_ecameraid = eCamera_Env;
  220. m_lstarttime = 0;
  221. m_preview_error_log = false;
  222. m_capture_error_log = false;
  223. }
  224. RvcWsServer::~RvcWsServer(void)
  225. {
  226. m_bconnected = false;
  227. m_struuid = "";
  228. m_bstarttrans = false;
  229. #if defined(RVC_OS_WIN)
  230. m_work_thread = NULL;
  231. #else
  232. m_work_threadid = 0;
  233. #endif //RVC_OS_WIN
  234. m_cameraid = 0;
  235. m_fps = RVC_DEFAULT_FPS;
  236. if (NULL != m_buffer) {
  237. delete m_buffer;
  238. m_buffer = NULL;
  239. }
  240. m_ubuffer_size = 0;
  241. if (NULL != m_capbuffer) {
  242. delete m_capbuffer;
  243. m_capbuffer = NULL;
  244. }
  245. m_ucapbuffer_size = 0;
  246. m_preview_error_log = false;
  247. m_capture_error_log = false;
  248. }
  249. void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket& s)
  250. {
  251. boost::asio::ip::tcp::no_delay option(true);
  252. s.set_option(option);
  253. }
  254. int RvcWsServer::Init_WsServer(websocket_callback_t* pcallback, rvc_video_param_t* pparam, int iport)
  255. {
  256. int iret = -1;
  257. if (NULL == pcallback) {
  258. return iret;
  259. }
  260. if (iport <= 0) {
  261. m_listenport = RVC_LIVENESS_WS_PORT;
  262. }
  263. else {
  264. m_listenport = iport;
  265. }
  266. m_cameraid = 0;
  267. m_fps = RVC_DEFAULT_FPS;
  268. memcpy(&m_callback, pcallback, sizeof(websocket_callback_t));
  269. #ifdef _MSC_VER
  270. m_evt = CreateEventA(NULL, FALSE, FALSE, NULL);
  271. #else
  272. sem_init(&m_semt, 0, 0);
  273. #endif //_MSC_VER
  274. m_ubuffer_size = pparam->iwidth * pparam->iheight * 3;
  275. m_buffer = new unsigned char[m_ubuffer_size];
  276. memset(m_buffer, 0, m_ubuffer_size);
  277. m_ucapbuffer_size = pparam->icapwidth * pparam->icapheight * 3;
  278. m_capbuffer = new unsigned char[m_ucapbuffer_size];
  279. memset(m_capbuffer, 0, m_ucapbuffer_size);
  280. m_ecameraid = eCamera_Env;
  281. try {
  282. // Set logging settings
  283. m_wsserver.set_access_channels(websocketpp::log::alevel::all);
  284. m_wsserver.set_error_channels(websocketpp::log::elevel::all);
  285. // Register our message handler
  286. m_wsserver.set_message_handler(websocketpp::lib::bind(&RvcWsServer::on_message, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2));
  287. m_wsserver.set_http_handler(websocketpp::lib::bind(&RvcWsServer::on_http, this, websocketpp::lib::placeholders::_1));
  288. m_wsserver.set_fail_handler(websocketpp::lib::bind(&RvcWsServer::on_fail, this, websocketpp::lib::placeholders::_1));
  289. m_wsserver.set_open_handler(websocketpp::lib::bind(&RvcWsServer::on_open, this, websocketpp::lib::placeholders::_1));
  290. m_wsserver.set_close_handler(websocketpp::lib::bind(&RvcWsServer::on_close, this, websocketpp::lib::placeholders::_1));
  291. m_wsserver.set_validate_handler(websocketpp::lib::bind(&RvcWsServer::validate, this, websocketpp::lib::placeholders::_1));
  292. // Initialize ASIO
  293. m_wsserver.init_asio();
  294. m_wsserver.set_reuse_addr(true);
  295. m_wsserver.set_socket_init_handler(&on_socket_init);
  296. m_wsserver.listen(m_listenport);
  297. // Start the server accept loop
  298. m_wsserver.start_accept();
  299. iret = 0;
  300. // Start the ASIO io_service run loop
  301. m_wsserver.run();
  302. //stop
  303. m_wsserver.stop();
  304. }
  305. catch (websocketpp::exception const& e) {
  306. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(e.what());
  307. }
  308. catch (const std::exception& e) {
  309. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(e.what());
  310. }
  311. catch (...) {
  312. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("other exception");
  313. }
  314. return iret;
  315. }
  316. bool RvcWsServer::validate(websocketpp::connection_hdl hdl)
  317. {
  318. return true;
  319. }
  320. void RvcWsServer::on_http(websocketpp::connection_hdl hdl)
  321. {
  322. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on_http");
  323. server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
  324. std::string res = con->get_request_body();
  325. std::stringstream ss;
  326. ss << "got HTTP request with " << res.size() << " bytes of body data.";
  327. con->set_body(ss.str());
  328. con->set_status(websocketpp::http::status_code::ok);
  329. }
  330. void RvcWsServer::on_fail(websocketpp::connection_hdl hdl)
  331. {
  332. server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
  333. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail handler: %d, %s.", con->get_ec().value(), con->get_ec().message());
  334. }
  335. void RvcWsServer::on_open(websocketpp::connection_hdl hdl)
  336. {
  337. server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
  338. websocketpp::config::core::request_type requestClient = con->get_request();
  339. std::string strMethod = requestClient.get_method();
  340. std::string strUri = requestClient.get_uri();
  341. }
  342. void RvcWsServer::on_close(websocketpp::connection_hdl hdl)
  343. {
  344. m_bconnected = false;
  345. if (m_bstarttrans) {
  346. m_bstarttrans = false;
  347. unsigned int utranstime = y2k_time_now() - m_utranstime;
  348. LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, CSimpleStringA::Format("stop video trans for session close, and transmit time is %us.", utranstime).GetData());
  349. if (RVC_MIN_VIDEO_TRANS_TIME >= utranstime) {
  350. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_AUTO_FACE_FAILED, CSimpleStringA::Format("auto face failed and transmit time is %us.", utranstime).GetData());
  351. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E04")("流媒体传输时长不大于2秒.");
  352. }
  353. if (RVC_MAX_VIDEO_TRANS_TIME <= utranstime) {
  354. LogWarn(Severity_Low, Error_Debug, LOG_EVT_AUTO_FACE_TIMEOUT, CSimpleStringA::Format("auto face timeout and transmit time is %us.", utranstime).GetData());
  355. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E05")("流媒体传输时长不小于15秒.");
  356. }
  357. }
  358. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402314L6")("断开流媒体传输连接");
  359. m_preview_error_log = false;
  360. m_capture_error_log = false;
  361. }
  362. // Define a callback to handle incoming messages
  363. void RvcWsServer::on_message(websocketpp::connection_hdl hdl, server::message_ptr msg)
  364. {
  365. try {
  366. if (m_bconnected == false) {
  367. m_struuid = "";
  368. if (0 == msg->get_payload().compare(0, strlen(RVC_WS_INIT_STR), RVC_WS_INIT_STR)) {
  369. handle_initial_instructions(hdl, msg->get_payload());
  370. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402314L1")("流媒体传输连接初始化");
  371. }
  372. }
  373. else {
  374. if (0 == msg->get_payload().compare(0, m_struuid.length(), m_struuid)) {
  375. if (0 == msg->get_payload().compare(m_struuid.length() + strlen(RVC_WS_CONNECT_IDENTIFIER), strlen(RVC_WS_START_TRANS_STR), RVC_WS_START_TRANS_STR)) {
  376. m_bstarttrans = true;
  377. m_hdl = hdl;
  378. if (0 != StartVideoTransmit()) {
  379. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode("QLR0402314L2").setResultCode("RTA3E01")("start video transmit failed.");
  380. }
  381. else {
  382. m_lstarttime = SP::Module::Comm::RVCGetTickCount();
  383. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402314L2")("start video transmit success.");
  384. }
  385. }
  386. else if (0 == msg->get_payload().compare(m_struuid.length() + strlen(RVC_WS_CONNECT_IDENTIFIER), strlen(RVC_WS_STOP_TRANS_STR), RVC_WS_STOP_TRANS_STR)) {
  387. long costtime = SP::Module::Comm::RVCGetTickCount() - m_lstarttime;
  388. if (0 != StopVideoTransmit()) {
  389. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setCostTime(costtime).setLogCode("QLR0402314L5").setResultCode("RTA3E02")("stop video transmit failed.");
  390. }
  391. else {
  392. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setCostTime(costtime).setLogCode("QLR0402314L5")("stop video transmit success.");
  393. }
  394. m_bstarttrans = false;
  395. }
  396. else if (0 == msg->get_payload().compare(m_struuid.length() + strlen(RVC_WS_CONNECT_IDENTIFIER), strlen(RVC_WS_CHANGE_CAMERA_STR), RVC_WS_CHANGE_CAMERA_STR)) {
  397. if (0 != handle_change_camera_instructions(msg->get_payload())) {
  398. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode("QLR0402314L3").setResultCode("RTA3E03")("change transmit camera failed.");
  399. }
  400. else {
  401. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402314L3")("change transmit camera success.");
  402. }
  403. }
  404. else if (0 == msg->get_payload().compare(m_struuid.length() + strlen(RVC_WS_CONNECT_IDENTIFIER), strlen(RVC_WS_START_CAPTURE_STR), RVC_WS_START_CAPTURE_STR)) {
  405. StartVideoCapTransmit();
  406. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode("QLR0402314L4")("start video capture transmit success.");
  407. }
  408. else{
  409. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown request.");
  410. }
  411. }
  412. else {
  413. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv invalid request is %s.", msg->get_payload().c_str());
  414. }
  415. }
  416. }
  417. catch (websocketpp::exception const& e) {
  418. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("send failed because: %s", e.what());
  419. }
  420. }
  421. int RvcWsServer::StartVideoTransmit()
  422. {
  423. int iRet = -1;
  424. #if defined(RVC_OS_WIN)
  425. m_work_thread = (HANDLE)_beginthreadex(NULL, 0, &work_proc, this, 0, NULL);
  426. if (!m_work_thread) {
  427. #else
  428. if (0 != pthread_create(&m_work_threadid, NULL, work_proc, (void*)this)) {
  429. #endif //RVC_OS_WIN
  430. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create start video transmit thread failed.");
  431. return iRet;
  432. }
  433. iRet = 0;
  434. LogWarn(Severity_Low, Error_Debug, LOG_EVT_STARTVIDEOTRANS, "start video trans.");
  435. m_utranstime = y2k_time_now();
  436. return iRet;
  437. }
  438. int RvcWsServer::StopVideoTransmit()
  439. {
  440. int iRet = -1;
  441. if (m_bconnected && m_bstarttrans) {
  442. #if defined(RVC_OS_WIN)
  443. SetEvent(m_evt);
  444. if (NULL != m_work_thread) {
  445. WaitForSingleObject(m_work_thread, INFINITE);
  446. CloseHandle(m_work_thread);
  447. m_work_thread = NULL;
  448. }
  449. #else
  450. sem_post(&m_semt);
  451. if (0 == pthread_join(m_work_threadid, NULL)) {
  452. m_work_threadid = 0;
  453. }
  454. else {
  455. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("thread join video transmit thread failed!");
  456. }
  457. #endif //RVC_OS_WIN
  458. unsigned int utranstime = y2k_time_now() - m_utranstime;
  459. LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, CSimpleStringA::Format("stop video trans for user operation, and transmit time is %us.", utranstime).GetData());
  460. if (RVC_MIN_VIDEO_TRANS_TIME >= utranstime) {
  461. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_AUTO_FACE_FAILED, CSimpleStringA::Format("auto face failed and transmit time is %us.", utranstime).GetData());
  462. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E04")("流媒体传输时长不大于2秒.");
  463. }
  464. if (RVC_MAX_VIDEO_TRANS_TIME <= utranstime) {
  465. LogWarn(Severity_Low, Error_Debug, LOG_EVT_AUTO_FACE_TIMEOUT, CSimpleStringA::Format("auto face timeout and transmit time is %us.", utranstime).GetData());
  466. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode("RTA3E05")("流媒体传输时长不小于15秒.");
  467. }
  468. }
  469. iRet = 0;
  470. return iRet;
  471. }
  472. int RvcWsServer::StartVideoCapTransmit()
  473. {
  474. LogWarn(Severity_Low, Error_Debug, LOG_EVT_STARTVIDEOCAPTURE, "start video capture transmit.");
  475. return on_send(this, eCapture_Type);
  476. }
  477. static bool allisnum(std::string str)
  478. {
  479. for (int i = 0; i < str.size(); i++) {
  480. int tmp = (int)str[i];
  481. if (tmp >= '0' && tmp <= '9') {
  482. continue;
  483. }
  484. else
  485. {
  486. return false;
  487. }
  488. }
  489. return true;
  490. }
  491. int RvcWsServer::handle_initial_instructions(websocketpp::connection_hdl hdl, std::string strinstrut)
  492. {
  493. int iret = -1;
  494. m_bconnected = true;
  495. m_ecameraid = eCamera_Env;
  496. CUUID struuid = CUUID::Create(struuid);
  497. m_struuid = struuid.ToString();
  498. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("new connect and uuid is %s.", m_struuid.c_str());
  499. m_wsserver.send(hdl, m_struuid, websocketpp::frame::opcode::value::text);
  500. int index = strinstrut.find_first_of(RVC_WS_CONNECT_IDENTIFIER);
  501. if (std::string::npos != index) {
  502. std::string strfps = strinstrut.substr(index + strlen(RVC_WS_CONNECT_IDENTIFIER));
  503. if (allisnum(strfps)) {
  504. int ifps = atoi(strfps.c_str());
  505. if (ifps > RVC_MAX_FPS) {
  506. ifps = RVC_MAX_FPS;
  507. }
  508. if (ifps < RVC_MIN_FPS) {
  509. ifps = RVC_MIN_FPS;
  510. }
  511. m_fps = ifps;
  512. }
  513. }
  514. iret = 0;
  515. return iret;
  516. }
  517. int RvcWsServer::handle_change_camera_instructions(std::string strinstrut)
  518. {
  519. int iret = -1;
  520. if (m_bstarttrans) {
  521. int index = strinstrut.find_first_of(RVC_WS_CHANGE_CAMERA_STR);
  522. if (std::string::npos != index) {
  523. std::string strcamera = strinstrut.substr(index + strlen(RVC_WS_CHANGE_CAMERA_STR));
  524. index = strcamera.find_first_of(RVC_WS_CONNECT_IDENTIFIER);
  525. if (std::string::npos != index) {
  526. std::string strid = strcamera.substr(index + strlen(RVC_WS_CONNECT_IDENTIFIER));
  527. if (allisnum(strid)) {
  528. int icameraid = atoi(strid.c_str());
  529. if (eCamera_Env == icameraid || eCamera_Opt == icameraid) {
  530. m_ecameraid = (eCameraType)icameraid;
  531. }
  532. }
  533. }
  534. }
  535. iret = 0;
  536. }
  537. else {
  538. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("current is not in data transmitting.");
  539. }
  540. return iret;
  541. }
  542. }