Prechádzať zdrojové kódy

Z991239-4806 #comment 优化自动人脸图像传输流程

80274480 2 rokov pred
rodič
commit
75a6d29701

+ 17 - 17
Module/mod_livenessdetection/RvcWsServer.cpp

@@ -88,7 +88,6 @@ static int gbr2jpg(const unsigned char *pRgbData, const int width, const int hei
 
 	unsigned char* prgb = new unsigned char[width*height*3]; //»ñÈ¡µÄԭʼÊý¾ÝÊÇbgr¸ñʽ
 
-
 	bgr2rgb(pRgbData, width, height, prgb);
 
 	unsigned char* reversePrgb = new unsigned char[width * height * 3];
@@ -210,7 +209,7 @@ static void process(RvcWsServer *webserver)
 }
 
 
-static unsigned int __stdcall work_proc(void *arg)
+static void* work_proc(void* arg)
 {
 	RvcWsServer *webserver = (RvcWsServer *)arg;
 
@@ -228,7 +227,7 @@ RvcWsServer::RvcWsServer(void):m_wsserver()
 	m_bconnected = false;
 	m_struuid = "";
 	m_bstarttrans = false;
-	m_work_thread = NULL;
+	m_work_threadid = 0;
 	m_cameraid = 0;
 	m_fps = RVC_DEFAULT_FPS;
 	m_buffer = NULL;
@@ -245,7 +244,7 @@ RvcWsServer::~RvcWsServer(void)
 	m_bconnected = false;
 	m_struuid = "";
 	m_bstarttrans = false;
-	m_work_thread = NULL;
+	m_work_threadid = 0;
 	m_cameraid = 0;
 	m_fps = RVC_DEFAULT_FPS; 
 	if (NULL != m_buffer){
@@ -473,8 +472,12 @@ void RvcWsServer::on_message(websocketpp::connection_hdl hdl, server::message_pt
 int RvcWsServer::StartVideoTransmit()
 {
 	int iRet = -1;
-	m_work_thread = (HANDLE)_beginthreadex(NULL, 0, &work_proc, this, 0, NULL);
-	if (!m_work_thread) {
+
+	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 {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create start video transmit thread failed.");
 		return iRet;
 	}
 	
@@ -490,20 +493,17 @@ int RvcWsServer::StopVideoTransmit()
 	int iRet = -1;
 
 	if (m_bconnected && m_bstarttrans){
-#ifdef _MSC_VER
-		SetEvent(m_evt);
-#else
 		sem_post(&m_semt);
-#endif	//_MSC_VER
 
-		Dbg("in stop func");
-		if (NULL != m_work_thread){
-			WaitForSingleObject(m_work_thread, INFINITE);
-			CloseHandle(m_work_thread);
-			m_work_thread = NULL;
-		}
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("in stop func");
 
-		Dbg("after m_work_thread.");
+		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};
 		snprintf(strmsg, MAX_PATH, "stop video trans for user operation, and transmit time is %us.", y2k_time_now() - m_utranstime);

+ 1 - 2
Module/mod_livenessdetection/RvcWsServer.h

@@ -113,8 +113,7 @@ public:
 	volatile bool m_bconnected;			// connect flag
 	volatile bool m_bstarttrans;		// start transmit
 	volatile eCameraType m_ecameraid;			// camera id
-	HANDLE m_work_thread;
-	HANDLE m_evt;
+	pthread_t m_work_threadid;
 	sem_t m_semt;
 	websocket_callback_t m_callback;
 	server m_wsserver;					// websocket server,用于和业务层传输视频流,默认端口9100

+ 61 - 56
Module/mod_livenessdetection/mod_livenessdetection.cpp

@@ -4,14 +4,39 @@
 #include "mod_livenessdetection.h"
 #include "../mod_interactivecontrol/Event.h"
 
+
+static int __on_get_videodata(eVideoType eType, eCameraType ecameraid, int* width, int* height, unsigned char* bmpdata, int isize, void* user_data)
+{
+	CLivenessDetectionEntity* pThis = static_cast<CLivenessDetectionEntity*>(user_data);
+
+	int iret = pThis->on_get_videodata(eType, ecameraid, width, height, bmpdata, isize);
+
+	return iret;
+}
+
+static void* start_wsserver(void* arg)
+{
+	CLivenessDetectionEntity* liveness_entity = (CLivenessDetectionEntity*)arg;
+
+	websocket_callback_t t_callback = { 0 };
+	t_callback.user_data = liveness_entity;
+	t_callback.on_get_videodata = &__on_get_videodata;
+	rvc_video_param_t t_param = { 0 };
+	t_param.iwidth = REC_COMMON_VIDEO_PREVIEW_WIDTH;
+	t_param.iheight = REC_COMMON_VIDEO_PREVIEW_HEIGHT;
+	t_param.icapwidth = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
+	t_param.icapheight = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
+	int iRet = liveness_entity->GetWsServer()->Init_WsServer(&t_callback, &t_param, liveness_entity->GetWsPort());
+
+	return &iRet;
+}
+
+
 CLivenessDetectionEntity::CLivenessDetectionEntity(): m_bStarted(FALSE), m_nCaptureType(-1)
 {
-#ifdef RVC_OS_WIN
-	m_pFsm = NULL;
-#endif
 	m_WsServer = new RvcWsServer();
 	m_pFaceVideo = new RvcFaceVideo();
-	m_hWsServerThread = NULL;
+	m_hWsServerThreadId = 0;
 	m_iWsPort = RVC_LIVENESS_WS_PORT;
 	m_iCapType = 1;
 }
@@ -23,10 +48,13 @@ CLivenessDetectionEntity::~CLivenessDetectionEntity()
 		m_WsServer = NULL;
 	}
 
-	DWORD exitCode = 0;
-	if (m_hWsServerThread){
-		TerminateThread(m_hWsServerThread, exitCode);
-		m_hWsServerThread = NULL;
+	pthread_cancel(m_hWsServerThreadId);
+	if (0 == pthread_join(m_hWsServerThreadId, NULL)) {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("thread join web socket server thread %u success!", m_hWsServerThreadId);
+		m_hWsServerThreadId = 0;
+	}
+	else {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("thread join web socket server thread failed!");
 	}
 }
 
@@ -41,40 +69,37 @@ void CLivenessDetectionEntity::OnPreStart( CAutoArray<CSimpleStringA> strArgs,CS
 	pTransactionContext->SendAnswer(Error);
 }
 
-void CLivenessDetectionEntity::OnPreClose( EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext )
-{
-	ErrorCodeEnum Error = __OnClose(Error_Succeed);
-	pTransactionContext->SendAnswer(Error);
-}
 
-static int __on_get_videodata(eVideoType eType, eCameraType ecameraid, int* width, int* height, unsigned char* bmpdata, int isize, void* user_data)
+void CLivenessDetectionEntity::OnStarted()
 {
-	CLivenessDetectionEntity *pThis = static_cast<CLivenessDetectionEntity *>(user_data);
-
-	int iret = pThis->on_get_videodata(eType, ecameraid, width, height, bmpdata, isize);
+	if (Error_Succeed == GetEntityConfigure()) {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[LivenessDetectionFSM] Get Entity Configure Success!");
+	}
+	else {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[LivenessDetectionFSM] Get Entity Configure Failed!");
+	}
 
-	return iret;
+	if (0 == pthread_create(&m_hWsServerThreadId, NULL, start_wsserver, (void*)this)) {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create web socket server thread and thread id is %u.", m_hWsServerThreadId);
+	}
+	else {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create web socket server thread failed.");
+	}
 }
 
-static unsigned int __stdcall start_wsserver(void* arg)
+void CLivenessDetectionEntity::OnPreClose( EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext )
 {
-	CLivenessDetectionEntity* liveness_entity = (CLivenessDetectionEntity*)arg;
-
-	websocket_callback_t t_callback = { 0 };
-	t_callback.user_data = liveness_entity;
-	t_callback.on_get_videodata = &__on_get_videodata;
-	rvc_video_param_t t_param = { 0 };
-	t_param.iwidth = REC_COMMON_VIDEO_PREVIEW_WIDTH;
-	t_param.iheight = REC_COMMON_VIDEO_PREVIEW_HEIGHT;
-	t_param.icapwidth = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
-	t_param.icapheight = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
-	liveness_entity->GetWsServer()->Init_WsServer(&t_callback, &t_param, liveness_entity->GetWsPort());
-
-	return 0;
+	ErrorCodeEnum Error = __OnClose(Error_Succeed);
+	pTransactionContext->SendAnswer(Error);
 }
 
+
 ErrorCodeEnum CLivenessDetectionEntity::__OnStart(ErrorCodeEnum preOperationError)
 {
+	if (preOperationError != Error_Succeed) {
+		return preOperationError;
+	}
+
 	m_eDeviceType = eStand2sType;
 	//is Pad Version
 	CSmartPointer<IEntityFunction> spFunction = GetFunction();
@@ -87,10 +112,6 @@ ErrorCodeEnum CLivenessDetectionEntity::__OnStart(ErrorCodeEnum preOperationErro
 		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is standard");
 		m_eDeviceType = eStand2sType;
 	}
-
-	if (preOperationError != Error_Succeed) {
-		return preOperationError;
-	}
 		
 	ErrorCodeEnum Error = Error_Succeed;
 	nActiveCamera = CAMERA_TYPE_ENV;
@@ -107,35 +128,19 @@ ErrorCodeEnum CLivenessDetectionEntity::__OnStart(ErrorCodeEnum preOperationErro
 	CSimpleStringA strValue;
 	GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
 	m_iCameraState = strValue[0];
-	if (strValue[0] == 'E')
-	{
+	if (strValue[0] == 'E'){
 		nActiveCamera = CAMERA_TYPE_OPT;
 	}
-	else if (strValue[0] == 'O')
-	{
+	else if (strValue[0] == 'O'){
 		nActiveCamera = CAMERA_TYPE_ENV;
 	}
-	else if(strValue[0] == 'B')   ///////显示贴图
-	{
+	else if(strValue[0] == 'B') {
 		nActiveCamera = CAMERA_TYPE_ERROR;
 	}
-	else if (strValue[0] == 'N')
-	{
+	else if (strValue[0] == 'N'){
 		nActiveCamera = CAMERA_TYPE_ENV;
 	}
 
-	if (Error_Succeed == GetEntityConfigure()) {
-		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[LivenessDetectionFSM] Get Entity Configure Success!");
-	}
-	else {
-		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[LivenessDetectionFSM] Get Entity Configure Failed!");
-	}
-
-	m_hWsServerThread = (HANDLE)_beginthreadex(NULL, 0, &start_wsserver, this, 0, NULL);
-	if (NULL != m_hWsServerThread) {
-		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("create web socket server success.");
-	}
-
 	return Error;
 }
 

+ 4 - 2
Module/mod_livenessdetection/mod_livenessdetection.h

@@ -41,6 +41,8 @@ public:
 	virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext);
 	virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext);
 
+	virtual void OnStarted();
+
 	virtual int GetActiveCamera();
 
 	ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError);
@@ -82,8 +84,8 @@ private:
 	int m_nCaptureType;
 	RvcWsServer* m_WsServer;
 	RvcFaceVideo* m_pFaceVideo;
-
-	HANDLE m_hWsServerThread;
+	 
+	pthread_t m_hWsServerThreadId;
 	int m_iWsPort;
 	int m_iCapType;
 };