From 97dbd476ee99f36286cd866b2208ceead3019b31 Mon Sep 17 00:00:00 2001
From: houxiao <houxiao@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期三, 11 一月 2017 17:30:51 +0800
Subject: [PATCH] face daemon develop finish

---
 RtspFace/main_face_daemon.cpp |  120 ++++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 92 insertions(+), 28 deletions(-)

diff --git a/RtspFace/main_face_daemon.cpp b/RtspFace/main_face_daemon.cpp
index 917aa4a..ad8df4d 100644
--- a/RtspFace/main_face_daemon.cpp
+++ b/RtspFace/main_face_daemon.cpp
@@ -12,6 +12,7 @@
 #include "PL_Payer.h"
 #include "PL_Gainer.h"
 #include "PL_SensetimeFaceTrack.h"
+#include "PL_SensetimeFaceDetect.h"
 #include "PL_DlibFaceTrack.h"
 
 #include "PipeLinePool.h"
@@ -20,21 +21,77 @@
 #include "ev_proto.h"
 #include "face_daemon_proto.h"
 
+#include "SensetimeFaceAPIWrapper/src/FaceDBPool.h"
+
 #include "logger.h"
+
+template<typename TPoolPtr, typename TPoolElem>
+struct PoolElemLocker
+{
+	TPoolPtr pool;
+	TPoolElem elem;
+	PoolElemLocker(TPoolPtr _pool, TPoolElem _elem) : pool(_pool), elem(_elem)
+	{
+		
+	}
+	~PoolElemLocker()
+	{
+		pool->release(elem);
+		pool->notify_free();
+	}
+};
+
+template<typename TArrayPtr>
+struct ArrayDeleter
+{
+	TArrayPtr array;
+	ArrayDeleter(TArrayPtr _array) : array(_array)
+	{
+		
+	}
+	~ArrayDeleter()
+	{
+		delete[] array;
+	}
+};
 
 PipeLinePool g_PipeLinePool;
 
+FaceDBPool g_FaceDBPool;//#todo config
+
 evclient_proc_t evclient_proc;
 
-void send_SensetimeFaceDetectResult(PipeMaterial& lastPm)
+bool send_SensetimeFaceDetectResult(EVClientStub& client, PipeMaterial& lastPm)
 {
-	if (lastPm.type == PipeMaterial::PMT_PM_LIST)
+	//if (lastPm.type != PipeMaterial::PMT_PM_LIST)
+
+	//PipeMaterial& facePM = ((PipeMaterial*)(lastPm.buffer))[1];
+	//st_ff_vect_t& faceFeatures = *((st_ff_vect_t*)facePM.buffer);
+	//LOG_NOTICE << "faceFeatures " << faceFeatures.size();
+	//#todo send result packet
+
+	if (lastPm.buffer == nullptr || lastPm.type != PipeMaterial::PMT_BYTES || lastPm.buffSize != sizeof(SensetimeFaceDetectResult))
 	{
-		PipeMaterial& facePM = ((PipeMaterial*)(lastPm.buffer))[1];
-		st_ff_vect_t& faceFeatures = *((st_ff_vect_t*)facePM.buffer);
-		LOG_NOTICE << "faceFeatures " << faceFeatures.size();
-		//#todo send result packet
+		LOG_WARN << "pm not available";
+		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
+		return false;
 	}
+	
+	const SensetimeFaceDetectResult*  result = (const SensetimeFaceDetectResult*)lastPm.buffer;
+	
+	client.sendBuffSize = sizeof(EVPHeader)+sizeof(SensetimeFaceDetectResult);
+	client.sendBuff = new uint8_t[client.sendBuffSize];
+	client.deleteSendBuff = true;
+	
+	EVPHeader* evpHeader = new (client.sendBuff) EVPHeader;
+	evpHeader->cmd = FaceDaemonCommand::FDC_SENSETIMEFACEDETECT_RESULT;
+	evpHeader->size = client.sendBuffSize;
+	
+	SensetimeFaceDetectResult* evpSub = new (client.sendBuff + sizeof(EVPHeader)) SensetimeFaceDetectResult;
+	evpSub->school_id = result->school_id;
+	evpSub->st_id = result->st_id;
+	
+	return true;
 }
 
 bool ev_proc_SensetimeFaceDetect(EVClientStub& client)
@@ -44,11 +101,18 @@
 	//return true;
 	
 	FDP_Image* fdpImage = (FDP_Image*)(client.recvBuff + sizeof(EVPHeader));
+
+	faceAPI* _faceDB = g_FaceDBPool.get_free(fdpImage->school_id);
+	if (_faceDB == nullptr)
+	{
+		LOG_WARN << "can't get face db";
+		ev_send_status_packet(client, EVPStatus::EVPS_PARAMETER_ERROR);
+		return false;
+	}
 	
-	PipeLine* pipeLine = nullptr;
-	if (g_PipeLinePool.wait_free())
-		pipeLine = g_PipeLinePool.get_free();
-	
+	PoolElemLocker<FaceDBPool*, int> _lock_faceDB(&g_FaceDBPool, fdpImage->school_id);
+
+	PipeLine* pipeLine = g_PipeLinePool.get_free();
 	if (pipeLine == nullptr)
 	{
 		LOG_WARN << "can't get free pipeline";
@@ -56,28 +120,29 @@
 		return false;
 	}
 	
+	PoolElemLocker<PipeLinePool*, PipeLine*> _lock_pipeLine(&g_PipeLinePool, pipeLine);
+
 	// fill
-	MB_Frame frame;
-	frame.type = (MB_Frame::MBFType)(fdpImage->mb_type);
-	frame.buffer = fdpImage->buff;
-	frame.buffSize = client.recvBuffSize - sizeof(EVPHeader) - sizeof(FDP_Image);
-	frame.width = fdpImage->width;
-	frame.height = fdpImage->height;
-	
-	PL_SensetimeFaceDetectPipeArgs args;
-	//#todo get db
-	
+	SensetimeFaceDetectDbFrame dbFrame;
+	dbFrame.type = (MB_Frame::MBFType)(fdpImage->mb_type);
+	dbFrame.buffSize = client.recvBuffSize - sizeof(EVPHeader) - sizeof(FDP_Image);
+	dbFrame.buffer = new uint8_t[dbFrame.buffSize];
+	ArrayDeleter<uint8_t*> _del_img((uint8_t*)dbFrame.buffer);
+	memcpy(dbFrame.buffer, fdpImage->buff, dbFrame.buffSize);
+	dbFrame.width = fdpImage->width;
+	dbFrame.height = fdpImage->height;
+	dbFrame.school_id = fdpImage->school_id;
+	dbFrame._faceDB = _faceDB;
+
 	PipeMaterial pm;
 	pm.type = PipeMaterial::PMT_FRAME;
-	pm.buffer = &frame;
+	pm.buffer = &dbFrame;
 	pm.buffSize = 0;
-	pm.args = &args;
 
 	PipeLineElem* plElem = pipeLine->pipe(&pm);
 	if (! pipeLine->check_pipe_complete(plElem))
 	{
 		LOG_WARN << "pipeline not complete";
-		g_PipeLinePool.release(pipeLine);
 		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
 		return false;
 	}
@@ -85,14 +150,13 @@
 	if (!plElem->gain(pm))
 	{
 		LOG_WARN << "pipeline gain error";
-		g_PipeLinePool.release(pipeLine);
 		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
 		return false;
 	}
 	
-	send_SensetimeFaceDetectResult(pm);
-	
-	g_PipeLinePool.release(pipeLine);
+	// can not release pipleline unless pm not used
+	send_SensetimeFaceDetectResult(client, pm);
+
 	return false;
 }
 
@@ -136,7 +200,7 @@
 
 		{
 			PL_Payer_Config config;
-			config.copyData = true;
+			config.copyData = true;//#todo false
 			PL_Gainer* ple = (PL_Gainer*)pipeLine->push_elem("PL_Gainer");
 			bool ret = ple->init(&config);
 			if (!ret)

--
Gitblit v1.8.0