123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700 |
- #include "stdafx.h"
- #include <stdint.h>
- #include "RvcWsServer.h"
- #include <boost/thread/thread.hpp>
- #include <vector>
- #include <boost/thread/lock_guard.hpp>
- #include "SpBase.h"
- #include "jpeglib.h"
- #include "Event.h"
- #include "y2k_time.h"
- namespace LivenessDetection{
- #ifndef RVC_JPEG_QUALITY
- #define RVC_JPEG_QUALITY 100 //ͼƬ����
- #endif
- #ifndef RVC_COLOR_PEPTH
- #define RVC_COLOR_PEPTH 3
- #endif
- #ifndef RVC_MAX_JPEG_SIZE
- #define RVC_MAX_JPEG_SIZE 256*1024
- #endif
- #ifndef RVC_PICTURE_TYPE_LEN
- #define RVC_PICTURE_TYPE_LEN 1
- #endif
- #ifndef RVC_MIN_VIDEO_TRANS_TIME
- #define RVC_MIN_VIDEO_TRANS_TIME 2
- #endif
- #ifndef RVC_MAX_VIDEO_TRANS_TIME
- #define RVC_MAX_VIDEO_TRANS_TIME 15
- #endif
- static int rgb2jpg_action(struct jpeg_compress_struct* pCinfo, const unsigned char *pRgbData, const int width, const int height)
- {
- int depth = RVC_COLOR_PEPTH;
- JSAMPROW row_pointer[1] = {0};
-
- pCinfo->image_width = width;
- pCinfo->image_height = height;
- pCinfo->input_components = depth;
- pCinfo->in_color_space = JCS_RGB;
-
- jpeg_set_defaults(pCinfo);
- jpeg_set_quality(pCinfo, RVC_JPEG_QUALITY, true);
- jpeg_start_compress(pCinfo, true);
-
- int row_stride = width * depth;
- while (pCinfo->next_scanline < pCinfo->image_height){
- row_pointer[0] = (JSAMPROW)(pRgbData + pCinfo->next_scanline * row_stride);
- jpeg_write_scanlines(pCinfo, row_pointer, 1);
- }
-
- jpeg_finish_compress(pCinfo);
- jpeg_destroy_compress(pCinfo);
-
- return 0;
- }
- static int bgr2rgb(const unsigned char* bgr_image , const int image_width , const int image_height , unsigned char* rgb_image)
- {
- if(!bgr_image || !rgb_image) {
- return 1;
- }
- for(int i = 0; i < image_width * image_height; i++){
- unsigned char b_value = *bgr_image++;
- unsigned char g_value = *bgr_image++;
- unsigned char r_value = *bgr_image++;
- *rgb_image++ = r_value;
- *rgb_image++ = g_value;
- *rgb_image++ = b_value;
- }
- return 0;
- }
- static int gbr2jpg(const unsigned char *pRgbData, const int width, const int height, char* pDest, unsigned long* pSize, int reverseFlag = 0)
- {
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
-
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_compress(&cinfo);
- jpeg_mem_dest(&cinfo, (unsigned char**)&pDest, (unsigned long*)pSize);
- unsigned char* prgb = new unsigned char[width*height*3]; //��ȡ��ԭʼ������bgr��ʽ
- bgr2rgb(pRgbData, width, height, prgb);
- #if defined(RVC_OS_WIN)
- rgb2jpg_action(&cinfo, prgb, width, height);
- #else
- unsigned char* reversePrgb = new unsigned char[width * height * 3];
- if (reverseFlag == 0) //���·�ת
- {
- for (int i = 0; i < height; i++) {
- memcpy(reversePrgb + (i * width * 3),
- prgb + (3 * width * (height - (i + 1))), width * 3);
- }
- } else if (reverseFlag == 1) //���ҷ�ת
- {
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- memcpy(reversePrgb + (3 * ((width * i) + j)),
- prgb + (3 * ((width * i) + (width - (j + 1)))), 3);
- }
- }
- }
- rgb2jpg_action(&cinfo, reversePrgb, width, height);
- delete reversePrgb;
- reversePrgb = NULL;
- #endif //RVC_OS_WIN
- delete prgb;
- prgb = NULL;
- return 0;
- }
- static int on_send(RvcWsServer *webserver, eVideoType eType)
- {
- int iwidth = 0;
- int iheight = 0;
- int isize = 0;
- if (webserver->m_callback.on_get_videodata){
- if (ePreview_Type == eType){
- 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);
- }
- else{
- 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);
- }
-
- if (isize > 0){
- char pDest[RVC_MAX_JPEG_SIZE] = {0};
- unsigned long size = RVC_MAX_JPEG_SIZE;
- if (ePreview_Type == eType){
- pDest[0] = 0x01;
- #if defined(RVC_OS_WIN)
- gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size);
- #else
- if (webserver->m_ecameraid == 0) //������ͷ
- {
- gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size, 0); //���·�ת
- } else if (webserver->m_ecameraid == 1) //������ͷ
- {
- gbr2jpg(webserver->m_buffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size, 1); //���ҷ�ת
- }
- #endif //RVC_OS_WIN
- }
- else{
- pDest[0] = 0x02;
- gbr2jpg(webserver->m_capbuffer, iwidth, iheight, pDest + RVC_PICTURE_TYPE_LEN, &size);
- }
- if (webserver->m_bconnected && webserver->m_bstarttrans){
- if (false == webserver->m_hdl.expired()){
- webserver->m_wsserver.send(webserver->m_hdl, (const void*)pDest, size + RVC_PICTURE_TYPE_LEN, websocketpp::frame::opcode::value::binary);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("send picture size is %u.", size+1);
- }
- }
- }
- }
-
- return 0;
- }
- static void process(RvcWsServer *webserver)
- {
- while (webserver->m_bconnected && webserver->m_bstarttrans)
- {
- #ifdef _MSC_VER
- DWORD dwRet = WaitForSingleObject(webserver->m_evt, 1000 / webserver->m_fps);
- if (dwRet == WAIT_OBJECT_0) {
- break;
- }
- else if (dwRet == WAIT_TIMEOUT) {
- if (webserver->m_bconnected && webserver->m_bstarttrans) {
- on_send(webserver, ePreview_Type);
- }
- }
- #else
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- long unsec = ts.tv_nsec + (1000 * 1000 * (1000 / webserver->m_fps)); //�߳�������ʱʱ�� 1000/webserver->m_fps ����
- ts.tv_sec += (unsec / 1000000000);
- ts.tv_nsec = (unsec % 1000000000);
- if (0 != sem_timedwait(&webserver->m_semt, &ts) && (ETIMEDOUT == errno)) {
- if (webserver->m_bconnected && webserver->m_bstarttrans) {
- on_send(webserver, ePreview_Type);
- }
- }
- else
- {
- break;
- }
- #endif // _MSC_VER
- }
- webserver->m_bstarttrans = false;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("process exit.");
- }
- #if defined(RVC_OS_WIN)
- static unsigned int __stdcall work_proc(void* arg)
- #else
- static void* work_proc(void* arg)
- #endif //RVC_OS_WIN
- {
- RvcWsServer *webserver = (RvcWsServer *)arg;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("video fps is %d.", webserver->m_fps);
- process(webserver);
- return 0;
- }
- RvcWsServer::RvcWsServer(void):m_wsserver()
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RvcWsServer()");
- m_bconnected = false;
- m_struuid = "";
- m_bstarttrans = false;
- #if defined(RVC_OS_WIN)
- m_work_thread = NULL;
- #else
- m_work_threadid = 0;
- #endif //RVC_OS_WIN
- m_cameraid = 0;
- m_fps = RVC_DEFAULT_FPS;
- m_buffer = NULL;
- m_ubuffer_size = 0;
- m_capbuffer = NULL;
- m_ucapbuffer_size = 0;
- m_ecameraid = eCamera_Env;
- }
- RvcWsServer::~RvcWsServer(void)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("~RvcWsServer()");
- m_bconnected = false;
- m_struuid = "";
- m_bstarttrans = false;
- #if defined(RVC_OS_WIN)
- m_work_thread = NULL;
- #else
- m_work_threadid = 0;
- #endif //RVC_OS_WIN
- m_cameraid = 0;
- m_fps = RVC_DEFAULT_FPS;
- if (NULL != m_buffer){
- delete m_buffer;
- m_buffer = NULL;
- }
- m_ubuffer_size = 0;
- if (NULL != m_capbuffer){
- delete m_capbuffer;
- m_capbuffer = NULL;
- }
- m_ucapbuffer_size = 0;
- }
- void on_socket_init(websocketpp::connection_hdl hdl, boost::asio::ip::tcp::socket& s)
- {
- boost::asio::ip::tcp::no_delay option(true);
- s.set_option(option);
- }
- int RvcWsServer::Init_WsServer(websocket_callback_t* pcallback, rvc_video_param_t* pparam, int iport)
- {
- int iret = -1;
- if (NULL == pcallback){
- return iret;
- }
- if (iport <= 0){
- m_listenport = RVC_LIVENESS_WS_PORT;
- }
- else{
- m_listenport = iport;
- }
-
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("WsServer listening port is %d.", m_listenport);
- m_cameraid = 0;
- m_fps = RVC_DEFAULT_FPS;
- memcpy(&m_callback, pcallback, sizeof(websocket_callback_t));
-
- #ifdef _MSC_VER
- m_evt = CreateEventA(NULL, FALSE, FALSE, NULL); //�߳�ͬ��������
- #else
- sem_init(&m_semt, 0, 0);
- #endif //_MSC_VER
- m_ubuffer_size = pparam->iwidth*pparam->iheight*3;
- m_buffer = new unsigned char[m_ubuffer_size];
- memset(m_buffer, 0, m_ubuffer_size);
- m_ucapbuffer_size = pparam->icapwidth*pparam->icapheight*3;
- m_capbuffer = new unsigned char[m_ucapbuffer_size];
- memset(m_capbuffer, 0, m_ucapbuffer_size);
- m_ecameraid = eCamera_Env;
- try {
- // Set logging settings
- m_wsserver.set_access_channels(websocketpp::log::alevel::all);
- m_wsserver.set_error_channels(websocketpp::log::elevel::all);
- // Register our message handler
- m_wsserver.set_message_handler(websocketpp::lib::bind(&RvcWsServer::on_message, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2));
- m_wsserver.set_http_handler(websocketpp::lib::bind(&RvcWsServer::on_http, this, websocketpp::lib::placeholders::_1));
- m_wsserver.set_fail_handler(websocketpp::lib::bind(&RvcWsServer::on_fail, this, websocketpp::lib::placeholders::_1));
- m_wsserver.set_open_handler(websocketpp::lib::bind(&RvcWsServer::on_open, this, websocketpp::lib::placeholders::_1));
- m_wsserver.set_close_handler(websocketpp::lib::bind(&RvcWsServer::on_close, this, websocketpp::lib::placeholders::_1));
- m_wsserver.set_validate_handler(websocketpp::lib::bind(&RvcWsServer::validate, this, websocketpp::lib::placeholders::_1));
- // Initialize ASIO
- m_wsserver.init_asio();
- m_wsserver.set_reuse_addr(true);
- m_wsserver.set_socket_init_handler(&on_socket_init);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get_max_message_size is %d.", m_wsserver.get_max_message_size());
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get_max_http_body_size is %d.", m_wsserver.get_max_http_body_size());
- m_wsserver.listen(m_listenport);
- // Start the server accept loop
- m_wsserver.start_accept();
- iret = 0;
- // Start the ASIO io_service run loop
- m_wsserver.run();
- //stop
- m_wsserver.stop();
- }
- catch (websocketpp::exception const & e) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(e.what());
- }
- catch (const std::exception & e) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)(e.what());
- }
- catch (...) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("other exception");
- }
- return iret;
- }
- bool RvcWsServer::validate(websocketpp::connection_hdl hdl)
- {
- return true;
- }
-
- void RvcWsServer::on_http(websocketpp::connection_hdl hdl)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on_http");
- server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
- std::string res = con->get_request_body();
- std::stringstream ss;
- ss << "got HTTP request with " << res.size() << " bytes of body data.";
- con->set_body(ss.str());
- con->set_status(websocketpp::http::status_code::ok);
- }
-
- void RvcWsServer::on_fail(websocketpp::connection_hdl hdl)
- {
- server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("fail handler: %d, %s." , con->get_ec().value(), con->get_ec().message());
- }
-
- void RvcWsServer::on_open(websocketpp::connection_hdl hdl)
- {
- //����websocket upgrade�ɹ�֮����open_handler�������ص�on_open��
- //��������Ի�ȡhttp����ĵ�ַ��������Ϣ��
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("open handler");
- server::connection_ptr con = m_wsserver.get_con_from_hdl(hdl);
- websocketpp::config::core::request_type requestClient = con->get_request();
- std::string strMethod = requestClient.get_method(); //����
- std::string strUri = requestClient.get_uri(); //����uri��ַ�����Խ�������
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("request client method is %s, uri is %s.", strMethod.c_str(), strUri.c_str());
- }
-
- void RvcWsServer::on_close(websocketpp::connection_hdl hdl)
- {
- m_bconnected = false;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("close handler");
- if (m_bstarttrans){
- m_bstarttrans = false;
- char strmsg[MAX_PATH] = {0};
- unsigned int utranstime = y2k_time_now() - m_utranstime;
- #if defined(RVC_OS_WIN)
- _snprintf(strmsg, MAX_PATH, "stop video trans for session close, and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, strmsg);
- if (RVC_MIN_VIDEO_TRANS_TIME >= utranstime) {
- char strinfo[MAX_PATH] = { 0 };
- _snprintf(strinfo, MAX_PATH, "auto face failed and transmit time is %us.", utranstime);
- LogWarn(Severity_Middle, Error_Debug, LOG_EVT_AUTO_FACE_FAILED, strinfo);
- }
- if (RVC_MAX_VIDEO_TRANS_TIME <= utranstime) {
- char strmessage[MAX_PATH] = { 0 };
- _snprintf(strmessage, MAX_PATH, "auto face timeout and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_AUTO_FACE_TIMEOUT, strmessage);
- }
- #else
- snprintf(strmsg, MAX_PATH, "stop video trans for session close, and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, strmsg);
- if (RVC_MIN_VIDEO_TRANS_TIME >= utranstime) {
- char strinfo[MAX_PATH] = { 0 };
- snprintf(strinfo, MAX_PATH, "auto face failed and transmit time is %us.", utranstime);
- LogWarn(Severity_Middle, Error_Debug, LOG_EVT_AUTO_FACE_FAILED, strinfo);
- }
- if (RVC_MAX_VIDEO_TRANS_TIME <= utranstime) {
- char strmessage[MAX_PATH] = { 0 };
- snprintf(strmessage, MAX_PATH, "auto face timeout and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_AUTO_FACE_TIMEOUT, strmessage);
- }
- #endif //RVC_OS_WIN
- }
- }
-
- // Define a callback to handle incoming messages
- void RvcWsServer::on_message(websocketpp::connection_hdl hdl, server::message_ptr msg)
- {
- /*
- hdl.lock().get() ������ӱ�ʶ
- msg->get_payload() ���յ�����Ϣ����
- msg->get_opcode() ���յ���Ϣ������ ���������ı�TEXT,������BINARY�ȵ�
- */
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("on_message called with hdl:0x%08x, and message: %s, and opcode: %d.", hdl.lock().get(), msg->get_payload().c_str(), msg->get_opcode());
-
- try {
- /*
- ������Ϣ
- s->send(
- hdl, //����
- msg->get_payload(), //��Ϣ
- msg->get_opcode());//��Ϣ����
- */
- if (m_bconnected == false){ //��ʼ���ӿ�
- m_struuid = "";
- if (0 == msg->get_payload().compare(0, strlen(RVC_WS_INIT_STR), RVC_WS_INIT_STR)){
- handle_initial_instructions(hdl, msg->get_payload());
- }
- }
- else{
- if (0 == msg->get_payload().compare(0, m_struuid.length(), m_struuid)){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv client request is %s.", msg->get_payload().c_str());
- 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)){
- m_bstarttrans = true;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video trans.");
- m_hdl = hdl;
- if (0 == StartVideoTransmit()){
- //m_wsserver.send(hdl, "start video transmit success.", msg->get_opcode());
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video transmit success.");
- }
- else{
- //m_wsserver.send(hdl, "start video transmit failed.", msg->get_opcode());
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start video transmit failed.");
- }
- }
- 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)){
- StopVideoTransmit();
- m_bstarttrans = false;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop video trans.");
- //m_wsserver.send(hdl, "stop video trans.", msg->get_opcode());
- }
- 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)){
- handle_change_camera_instructions(msg->get_payload());
- }
- 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)){
- StartVideoCapTransmit();
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown request.");
- }
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv invalid request is %s.", msg->get_payload().c_str());
- }
- }
- }
- catch (websocketpp::exception const & e) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("send failed because: %s", e.what());
- }
- }
- int RvcWsServer::StartVideoTransmit()
- {
- int iRet = -1;
- #if defined(RVC_OS_WIN)
- m_work_thread = (HANDLE)_beginthreadex(NULL, 0, &work_proc, this, 0, NULL);
- if (!m_work_thread)
- #else
- if (0 == pthread_create(&m_work_threadid, NULL, work_proc, (void*)this)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create start video transmit thread and thread id is %u.", m_work_threadid);
- } else
- #endif //RVC_OS_WIN
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create start video transmit thread failed.");
- return iRet;
- }
-
- iRet = 0;
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STARTVIDEOTRANS, "start video trans.");
- m_utranstime = y2k_time_now();
- return iRet;
- }
- int RvcWsServer::StopVideoTransmit()
- {
- int iRet = -1;
- if (m_bconnected && m_bstarttrans) {
- #if defined(RVC_OS_WIN)
- SetEvent(m_evt);
- if (NULL != m_work_thread) {
- WaitForSingleObject(m_work_thread, INFINITE);
- CloseHandle(m_work_thread);
- m_work_thread = NULL;
- }
- char strmsg[MAX_PATH] = { 0 };
- unsigned int utranstime = y2k_time_now() - m_utranstime;
- _snprintf(strmsg, MAX_PATH, "stop video trans for user operation, and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, strmsg);
- if (RVC_MIN_VIDEO_TRANS_TIME >= utranstime) {
- char strinfo[MAX_PATH] = { 0 };
- _snprintf(strinfo, MAX_PATH, "auto face failed and transmit time is %us.", utranstime);
- LogWarn(Severity_Middle, Error_Debug, LOG_EVT_AUTO_FACE_FAILED, strinfo);
- }
- if (RVC_MAX_VIDEO_TRANS_TIME <= utranstime) {
- char strmessage[MAX_PATH] = { 0 };
- _snprintf(strmessage, MAX_PATH, "auto face timeout and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_AUTO_FACE_TIMEOUT, strmessage);
- }
- #else
- sem_post(&m_semt);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("in stop func");
- if (0 == pthread_join(m_work_threadid, NULL)) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("thread join video transmit thread %u success!", m_work_threadid);
- m_work_threadid = 0;
- } else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("thread join video transmit thread failed!");
- }
- char strmsg[MAX_PATH] = { 0 };
- unsigned int utranstime = y2k_time_now() - m_utranstime;
- snprintf(strmsg, MAX_PATH, "stop video trans for user operation, and transmit time is %us.", utranstime);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STOPVIDEOTRANS, strmsg);
- #endif //RVC_OS_WIN
- iRet = 0;
- }
- return iRet;
- }
- int RvcWsServer::StartVideoCapTransmit()
- {
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_STARTVIDEOCAPTURE, "start video capture transmit.");
- return on_send(this, eCapture_Type);
- }
- static bool allisnum(std::string str)
- {
- for (int i = 0; i < str.size(); i++){
- int tmp = (int)str[i];
- if (tmp >= '0' && tmp <= '9'){
- continue;
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- int RvcWsServer::handle_initial_instructions(websocketpp::connection_hdl hdl, std::string strinstrut)
- {
- int iret = -1;
- m_bconnected = true;
- m_ecameraid = eCamera_Env;
- CUUID struuid = CUUID::Create(struuid);
- m_struuid = struuid.ToString();
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("new connect and uuid is %s.", m_struuid.c_str());
- m_wsserver.send(hdl, m_struuid, websocketpp::frame::opcode::value::text);
- int index = strinstrut.find_first_of(RVC_WS_CONNECT_IDENTIFIER);
- if (std::string::npos != index){
- std::string strfps = strinstrut.substr(index + strlen(RVC_WS_CONNECT_IDENTIFIER));
- if (allisnum(strfps)){
- int ifps = atoi(strfps.c_str());
- if (ifps > RVC_MAX_FPS){
- ifps = RVC_MAX_FPS;
- }
- if (ifps < RVC_MIN_FPS){
- ifps = RVC_MIN_FPS;
- }
- m_fps = ifps;
- }
- }
- iret = 0;
- return iret;
- }
- int RvcWsServer::handle_change_camera_instructions(std::string strinstrut)
- {
- int iret = -1;
- if (m_bstarttrans){
- int index = strinstrut.find_first_of(RVC_WS_CHANGE_CAMERA_STR);
- if (std::string::npos != index){
- std::string strcamera = strinstrut.substr(index + strlen(RVC_WS_CHANGE_CAMERA_STR));
- index = strcamera.find_first_of(RVC_WS_CONNECT_IDENTIFIER);
- if (std::string::npos != index){
- std::string strid = strcamera.substr(index + strlen(RVC_WS_CONNECT_IDENTIFIER));
- if (allisnum(strid)){
- int icameraid = atoi(strid.c_str());
- if (eCamera_Env == icameraid || eCamera_Opt == icameraid){
- m_ecameraid = (eCameraType)icameraid;
- }
- }
- }
- }
- iret = 0;
- }
- else{
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("current is not in data transmitting.");
- }
- return iret;
- }
- }
|