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/face_daemon_proto.h                        |    5 
 RtspFace/make.sh                                    |    6 
 RtspFace/main_face_daemon.cpp                       |  120 ++++++++--
 RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.cpp |  164 ++++++++++++++
 RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.h   |   22 ++
 RtspFace/PL_SensetimeFaceDetect.h                   |   34 ++-
 RtspFace/PipeLinePool.cpp                           |   33 +-
 RtspFace/PL_SensetimeFaceDetect.cpp                 |  238 ++++-----------------
 8 files changed, 374 insertions(+), 248 deletions(-)

diff --git a/RtspFace/PL_SensetimeFaceDetect.cpp b/RtspFace/PL_SensetimeFaceDetect.cpp
index c79a5f7..12f68ff 100644
--- a/RtspFace/PL_SensetimeFaceDetect.cpp
+++ b/RtspFace/PL_SensetimeFaceDetect.cpp
@@ -2,27 +2,18 @@
 #include "MaterialBuffer.h"
 #include "logger.h"
 
+#include "SensetimeFaceAPIWrapper/src/FaceDBPool.h"
 #include <opencv2/opencv.hpp>
 #include <cv_face.h>
 
 struct PL_SensetimeFaceDetect_Internal
 {
-	//uint8_t buffer[1920*1080*4];
-	//size_t buffSize;
-	//size_t buffSizeMax;
-	MB_Frame lastFrame;
-	PipeMaterial pmList[2];
-	PL_SensetimeFaceDetectConfig config;
-	st_ff_vect_t faceFeatures;
-
 	bool payError;
-	
-	cv_handle_t handle_track;
+	PL_SensetimeFaceDetectConfig config;
+	SensetimeFaceDetectResult lastResult;
 	
 	PL_SensetimeFaceDetect_Internal() : 
-		//buffSize(0), buffSizeMax(sizeof(buffer)), 
-		lastFrame(), pmList(), config(), faceFeatures(), payError(true), 
-		handle_track(nullptr)
+		payError(true), config(), lastResult()
 	{
 	}
 	
@@ -32,20 +23,13 @@
 	
 	void reset()
 	{
-		//buffSize = 0;
 		payError = true;
-		
-		MB_Frame _lastFrame;
-		lastFrame = _lastFrame;
-		
-		PipeMaterial _pm;
-		pmList[0] = _pm;
-		pmList[1] = _pm;
 		
 		PL_SensetimeFaceDetectConfig _config;
 		config = _config;
 		
-		handle_track = nullptr;
+		SensetimeFaceDetectResult _lastResult;
+		lastResult = _lastResult;
 	}
 };
 
@@ -69,184 +53,69 @@
 	PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 	in->reset();
 	
-	PL_SensetimeFaceDetectConfig* config = (PL_SensetimeFaceDetectConfig*)args;
-	in->config = *config;
-	if (in->config.point_size == 21)
-		in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_21;
-	else if (in->config.point_size == 106)
-		in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_106;
-	else
+	if (args != nullptr)
 	{
-		LOG_ERROR << "alignment point size must be 21 or 106";
-		return false;
+		PL_SensetimeFaceDetectConfig* config = (PL_SensetimeFaceDetectConfig*)args;
+		in->config = *config;
 	}
 
-	// init handle
-	cv_result_t cv_result = cv_face_create_tracker(&(in->handle_track), nullptr, 
-								in->config.point_size_config | CV_FACE_TRACKING_TWO_THREAD);
-	if (cv_result != CV_OK)
-	{
-		LOG_ERROR << "cv_face_create_tracker failed, error code" << cv_result;
-		return false;
-	}
-
-	int val = 0;
-	cv_result = cv_face_track_set_detect_face_cnt_limit(in->handle_track, in->config.detect_face_cnt_limit, &val);
-	if (cv_result != CV_OK)
-	{
-		LOG_ERROR << "cv_face_track_set_detect_face_cnt_limit failed, error : " << cv_result;
-		return false;
-	}
-	else
-		LOG_ERROR << "detect face count limit : " << val;
-	
 	return true;
 }
 
 void PL_SensetimeFaceDetect::finit()
 {
 	PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
-	
-	// destroy track handle
-	cv_face_destroy_tracker(in->handle_track);
-	in->handle_track = nullptr;
-}
 
-int doFaceTrack(PL_SensetimeFaceDetect_Internal* in, 
-				uint8_t* buffer, size_t width, size_t height, size_t stride, cv_pixel_format cvPixFmt)
-{
-	//resize(bgr_frame, bgr_frame, Size(frame_width, frame_height), 0, 0, INTER_LINEAR);
-
-	int face_count = 0;
-	cv_result_t cv_result = CV_OK;
-	cv_face_t* p_face = nullptr;
-	
-	// realtime track
-	cv_result = cv_face_track(in->handle_track, buffer, cvPixFmt,
-							width, height, stride,
-							CV_FACE_UP, &p_face, &face_count);
-	if (cv_result != CV_OK)
-	{
-		LOG_ERROR << "cv_face_track failed, error : " << cv_result;
-		cv_face_release_tracker_result(p_face, face_count);
-		return -1;
-	}
-
-	// draw the video
-	//cv::Mat yuvMat(cv::Size(width,height), CV_8UC3, buffer);
-	cv::Mat yMat(cv::Size(width,height), CV_8UC1, buffer);
-	for (int i = 0; i < face_count; i++)
-	{
-		SensetimeFaceFeature faceFeature;
-		faceFeature.rect.leftTop.x = p_face[i].rect.left;
-		faceFeature.rect.leftTop.y = p_face[i].rect.top;
-		faceFeature.rect.rightBottom.x = p_face[i].rect.right;
-		faceFeature.rect.rightBottom.y = p_face[i].rect.bottom;
-		faceFeature.id = p_face[i].ID;
-		faceFeature.yaw = p_face[i].yaw;
-		faceFeature.pitch = p_face[i].pitch;
-		faceFeature.roll = p_face[i].roll;
-		faceFeature.eyeDistance = p_face[i].eye_dist;
-		
-		LOGP(DEBUG, "face: %d-----[%d, %d, %d, %d]-----id: %d", i,
-			p_face[i].rect.left, p_face[i].rect.top,
-			p_face[i].rect.right, p_face[i].rect.bottom, p_face[i].ID);
-			
-		LOGP(DEBUG, "face pose: [yaw: %.2f, pitch: %.2f, roll: %.2f, eye distance: %.2f]",
-			p_face[i].yaw,
-			p_face[i].pitch, p_face[i].roll, p_face[i].eye_dist);
-
-		if (in->config.draw_face_rect)
-		{
-			cv::Scalar scalar_color = CV_RGB(p_face[i].ID * 53 % 256,
-				p_face[i].ID * 93 % 256,
-				p_face[i].ID * 143 % 256);
-			
-			//cv::rectangle(yMat, cv::Point2f(0, 0), cv::Point2f(50, 50), scalar_color, 2);
-			//cv::rectangle(yMat, cv::Point2f(500, 500), cv::Point2f(550, 550), scalar_color, 2);
-			
-			cv::rectangle(yMat, cv::Point2f(static_cast<float>(p_face[i].rect.left),
-				static_cast<float>(p_face[i].rect.top)),
-				cv::Point2f(static_cast<float>(p_face[i].rect.right),
-				static_cast<float>(p_face[i].rect.bottom)), scalar_color, 2);
-		}
-
-
-		for (int j = 0; j < p_face[i].points_count; j++)
-		{
-			FacePoint featurePoint;
-			featurePoint.x = p_face[i].points_array[j].x;
-			featurePoint.y = p_face[i].points_array[j].y;
-			faceFeature.featurePoints.push_back(featurePoint);
-			
-			if (in->config.draw_face_feature_point)
-			{
-				cv::circle(yMat, cv::Point2f(p_face[i].points_array[j].x,
-					p_face[i].points_array[j].y), 1, cv::Scalar(255, 255, 255));
-			}
-		}
-		
-		if (in->config.generate_face_feature)
-			in->faceFeatures.push_back(faceFeature);
-	}
-
-	//if (face_count > 0)
-	//{
-	//	static size_t f=0;
-	//	char fname[50];
-	//	sprintf(fname, "face-%u.yuv420", ++f);
-	//	FILE * pFile = fopen (fname,"wb");
-	//	fwrite (yuvMat.data , sizeof(char), 1920*1080*1.5, pFile);
-	//	printf("write face file %s\n", fname);
-	//	fclose(pFile);
-	//}
-
-	// release the memory of face
-	cv_face_release_tracker_result(p_face, face_count);
-
-	return face_count;
 }
 
 bool PL_SensetimeFaceDetect::pay(const PipeMaterial& pm)
 {
 	PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
-
-	if (pm.type != PipeMaterial::PMT_FRAME)
+	
+	in->payError = true;
 	{
-		LOG_ERROR << "Only support PMT_FRAME";
+		SensetimeFaceDetectResult _lastResult;
+		in->lastResult = _lastResult;
+	}
+
+	if (pm.type != PipeMaterial::PMT_FRAME || !(in->config.payWithDbFrame))
+	{
+		LOG_ERROR << "Only support PMT_FRAME (SensetimeFaceDetectDbFrame)";
 		return false;
 	}
 	
 	if (pm.buffer == nullptr)
 		return false;
 	
-	MB_Frame* frame = (MB_Frame*)pm.buffer;
+	SensetimeFaceDetectDbFrame* frame = (SensetimeFaceDetectDbFrame*)pm.buffer;
 	if (frame->type != MB_Frame::MBFT_YUV420)
 	{
 		LOG_ERROR << "Only support MBFT_YUV420";
 		return false;
 	}
 
-	in->faceFeatures.clear();
-	int face_count = doFaceTrack(
-						in, (uint8_t*)frame->buffer, frame->width, frame->height, frame->width, CV_PIX_FMT_YUV420P);
-	if (face_count < 0)
+	faceDB* _faceDB = (faceDB*)frame->_faceDB;
+	if (_faceDB == nullptr)
 	{
-		in->payError = true;
+		LOG_ERROR << "faceDB is null";
 		return false;
 	}
-	else
-		in->payError = false;
 	
-	//in->buffer readly
-
-	in->lastFrame.type = MB_Frame::MBFT_YUV420;
-	in->lastFrame.buffer = frame->buffer;//#todo should copy
-	in->lastFrame.buffSize = frame->buffSize;
-	in->lastFrame.width = frame->width;
-	in->lastFrame.height = frame->height;
-	in->lastFrame.pts = frame->pts;
-
+	const size_t expectedYUVSize = frame->width * frame->height * 1.5;
+	if (frame->buffSize < expectedYUVSize)
+	{
+		LOG_WARN << "image size not ok";
+		return false;
+	}
+	
+	cv::Mat yuvMat(cv::Size(frame->width,frame->height), CV_8UC3, frame->buffer);
+	cv_feature_t* feature = _faceDB->extract_feature(yuvMat);
+	in->lastResult.st_id = _faceDB->search_db(feature);
+	//#todo release feature
+	
+	in->lastResult.school_id = frame->school_id;
+	
+	in->payError = false;
 	return true;
 }
 
@@ -254,35 +123,22 @@
 {
 	PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 
+	pm.former = this;
+	
 	if (in->payError)
 	{
-		pm.former = this;
 		return false;
 	}
-
-	if (!in->config.generate_face_feature)
+	
+	if (! in->config.resultStructOnly)
 	{
-		pm.type = PipeMaterial::PMT_FRAME;
-		pm.buffer = &(in->lastFrame);
-		pm.buffSize = 0;
-	}
-	else
-	{
-		in->pmList[0].type = PipeMaterial::PMT_FRAME;
-		in->pmList[0].buffer = &(in->lastFrame);
-		in->pmList[0].buffSize = 0;
-		in->pmList[0].former = this;
-		
-		in->pmList[1].type = PipeMaterial::PMT_BYTES;
-		in->pmList[1].buffer = &(in->faceFeatures);
-		in->pmList[1].buffSize = 0;
-		in->pmList[1].former = this;
-		
-		pm.type = PipeMaterial::PMT_PM_LIST;
-		pm.buffer = in->pmList;
-		pm.buffSize = sizeof(in->pmList) / sizeof(PipeMaterial);
+		LOG_ERROR << "Only support resultStructOnly";
+		return false;
 	}
 	
-	pm.former = this;
+	pm.type = PipeMaterial::PMT_BYTES;
+	pm.buffer = &(in->lastResult);
+	pm.buffSize = sizeof(SensetimeFaceDetectResult);
+
 	return true;
 }
diff --git a/RtspFace/PL_SensetimeFaceDetect.h b/RtspFace/PL_SensetimeFaceDetect.h
index c636515..7a3fd0c 100644
--- a/RtspFace/PL_SensetimeFaceDetect.h
+++ b/RtspFace/PL_SensetimeFaceDetect.h
@@ -2,28 +2,38 @@
 #define _PL_SENSETIMEFACEDETECT_H_
 
 #include "PipeLine.h"
-#include <vector>
+#include "MaterialBuffer.h"
+//#include "PL_SensetimeFaceTrack.h" // for st_ff_vect_t
 
-struct PL_SensetimeFaceDetectPipeArgs
+struct SensetimeFaceDetectDbFrame : public MB_Frame
 {
-	void* faceDB;
+	int school_id;
+	void* _faceDB;
 	
-	PL_SensetimeFaceDetectPipeArgs() : faceDB(nullptr)
+	SensetimeFaceDetectDbFrame() : MB_Frame(), school_id(-1), _faceDB(nullptr)
+	{ }
+};
+
+struct SensetimeFaceDetectResult
+{
+	int school_id;
+	int st_id;
+	//st_ff_vect_t features;
+	
+	SensetimeFaceDetectResult() : school_id(-1), st_id(-1)
 	{ }
 };
 
 struct PL_SensetimeFaceDetectConfig
 {
-	int point_size; // 21 / 106
-	int point_size_config; // CV_DETECT_ENABLE_ALIGN_21 / CV_DETECT_ENABLE_ALIGN_106
-	int detect_face_cnt_limit; // -1
-	bool draw_face_rect;
-	bool draw_face_feature_point;
-	bool generate_face_feature; // for PL_SensetimeFaceFeatureEmit
+	// #todo currently only support default config
+	bool payWithDbFrame;
+	bool resultStructOnly;
+	
+	//std::string dbPath;
 	
 	PL_SensetimeFaceDetectConfig() : 
-		point_size(21), point_size_config(-1), detect_face_cnt_limit(-1), 
-		draw_face_rect(true), draw_face_feature_point(true), generate_face_feature(false)
+		payWithDbFrame(true), resultStructOnly(true)
 	{ }
 };
 
diff --git a/RtspFace/PipeLinePool.cpp b/RtspFace/PipeLinePool.cpp
index f02eef1..cba5a1b 100644
--- a/RtspFace/PipeLinePool.cpp
+++ b/RtspFace/PipeLinePool.cpp
@@ -20,6 +20,19 @@
 	} \
 }
 
+struct MutexLocker
+{
+	pthread_mutex_t* mut;
+	MutexLocker(void* _mut) : mut((pthread_mutex_t*)_mut)
+	{
+		PLP_MUTEX_LOCK(mut,);
+	}
+	~MutexLocker()
+	{
+		PLP_MUTEX_UNLOCK(mut,);
+	}
+};
+
 PipeLinePool::PipeLinePool(bool _multithread_safe) : 
 	multithread_safe(_multithread_safe), tsafe_mutex(nullptr), pl_mutex(nullptr), 
 	pipelines(), pipelines_free()
@@ -32,6 +45,7 @@
 		pl_mutex = new pthread_mutex_t;
 		pthread_mutex_init((pthread_mutex_t*)pl_mutex, NULL);
 		
+		// in ctor pool is empty
 		PLP_MUTEX_LOCK(pl_mutex,);
 	}
 }
@@ -64,31 +78,26 @@
 	if (pl == nullptr)
 		return;
 
-	PLP_MUTEX_LOCK(tsafe_mutex,);
+	MutexLocker _ml(tsafe_mutex);
 	
 	if (pipelines.find(pl) != pipelines.end())
 		return;
 	
 	pipelines.insert(pl);
 	pipelines_free.insert(pl);
-	
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
 }
 
 void PipeLinePool::unmanage(PipeLine* pl)
 {
-	PLP_MUTEX_LOCK(tsafe_mutex,);
+	MutexLocker _ml(tsafe_mutex);
 
 	pipelines.erase(pl);
 	pipelines_free.erase(pl);
-	
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
 }
 
 PipeLine* PipeLinePool::get_free()
 {
-
-	PLP_MUTEX_LOCK(tsafe_mutex, nullptr);
+	MutexLocker _ml(tsafe_mutex);
 	
 	if (pipelines_free.empty())
 		return nullptr;
@@ -97,23 +106,19 @@
 	PipeLine* pl = *iter;
 	pipelines_free.erase(iter);
 	
-	PLP_MUTEX_UNLOCK(tsafe_mutex, nullptr);
-	
 	return pl;
 }
 
 void PipeLinePool::release(PipeLine* pl)
 {
+	MutexLocker _ml(tsafe_mutex);
+	
 	if (pipelines.find(pl) == pipelines.end())
 		return;
 	if (pipelines_free.find(pl) != pipelines.end())
 		return;
 
-	PLP_MUTEX_LOCK(tsafe_mutex,);
-	
 	pipelines_free.insert(pl);
-
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
 }
 
 bool PipeLinePool::wait_free()
diff --git a/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.cpp b/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.cpp
index c53b469..9a39a6f 100644
--- a/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.cpp
+++ b/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.cpp
@@ -1,2 +1,166 @@
 #include "FaceDBPool.h"
+#include "logger.h"
+#include "faceAPI.h"
 
+#include <pthread.h>
+#include <map>
+
+#define PLP_MUTEX_LOCK(mut,_ret) if (mut != nullptr) {\
+	int ret = pthread_mutex_lock((pthread_mutex_t*)mut); \
+	if(ret != 0) \
+	{ \
+		LOG_ERROR << "pthread_mutex_lock " << #mut <<  ": " << ret; \
+		return _ret; \
+	} \
+}
+
+#define PLP_MUTEX_UNLOCK(mut,_ret) if (mut != nullptr) {\
+	int ret = pthread_mutex_unlock((pthread_mutex_t*)mut); \
+	if(ret != 0) \
+	{ \
+		LOG_ERROR << "pthread_mutex_unlock " << #mut <<  ": " << ret; \
+		return _ret; \
+	} \
+}
+
+struct MutexLocker
+{
+	pthread_mutex_t* mut;
+	MutexLocker(pthread_mutex_t* _mut) : mut(_mut)
+	{
+		PLP_MUTEX_LOCK(mut,);
+	}
+	~MutexLocker()
+	{
+		PLP_MUTEX_UNLOCK(mut,);
+	}
+};
+
+struct ThreadSafeFaceDB
+{
+	int dbid;
+	pthread_mutex_t db_mutex;
+	faceAPI* api;
+	
+	ThreadSafeFaceDB() : dbid(-1), db_mutex(), api(nullptr)
+	{
+		pthread_mutex_init(db_mutex, NULL);
+	}
+	
+	~ThreadSafeFaceDB()
+	{
+		pthread_mutex_destroy(db_mutex);
+	}
+};
+
+typedef std::map<int, ThreadSafeFaceDB*> facedb_map_t;
+
+FaceDBPool::FaceDBPool() : 
+	pool_mutex(nullptr), face_db_map(nullptr)
+{
+	pool_mutex = new pthread_mutex_t;
+	pthread_mutex_init((pthread_mutex_t*)pool_mutex, NULL);
+	
+	face_db_map = new facedb_map_t();
+}
+
+FaceDBPool::~FaceDBPool()
+{
+	facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
+	for (facedb_map_t::iterator iter = _face_db_map->begin(); iter != _face_db_map->end(); ++iter)
+	{
+		iter->second.api->db_save();
+		delete iter->second.api;
+		delete iter->second;
+	}
+	
+	_face_db_map.clear();
+	
+	pthread_mutex_destroy((pthread_mutex_t*)pool_mutex);
+	delete (pthread_mutex_t*)pool_mutex;
+	pool_mutex = nullptr;
+}
+
+void FaceDBPool::manage(int dbid, faceAPI* db)
+{
+	if (dbid < 0 || db == nullptr)
+		return;
+
+	MutexLocker _ml(pool_mutex);
+	
+	facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
+	
+	if (_face_db_map.find(dbid) != _face_db_map.end())
+		return;
+	
+	ThreadSafeFaceDB* tsfdb = new ThreadSafeFaceDB;
+	tsfdb.dbid = dbid;
+	tsfdb.api = db;
+	
+	_face_db_map.insert(std::make_pair(dbid, tsfdb));
+}
+
+void FaceDBPool::unmanage(int dbid)
+{
+	MutexLocker _ml(pool_mutex);
+
+	facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
+	facedb_map_t::iterator iter = _face_db_map.find(dbid);
+	if (iter == _face_db_map.end())
+		return;
+	
+	iter->second.api->db_save();
+	delete iter->second.api;
+	delete iter->second;
+	
+	_face_db_map.erase(iter);
+}
+
+faceAPI* FaceDBPool::get_free(int dbid)
+{
+	ThreadSafeFaceDB* tsfdb = nullptr;
+	{
+		//avoid dead lock
+		MutexLocker _ml(pool_mutex);
+		
+		facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
+		if (_face_db_map.empty())
+			return nullptr;
+		
+		facedb_map_t::iterator iter = _face_db_map.find(dbid);
+		if (iter == _face_db_map.end())
+			return nullptr;
+		
+		tsfdb = iter->second;
+	}
+
+	if (tsfdb != nullptr)
+	{
+		PLP_MUTEX_LOCK(tsfdb->db_mutex, nullptr);
+		return tsfdb->api;
+	}
+}
+
+void FaceDBPool::release(int dbid)
+{
+	ThreadSafeFaceDB* tsfdb = nullptr;
+	{
+		//avoid dead lock
+		MutexLocker _ml(pool_mutex);
+		
+		facedb_map_t* _face_db_map = (facedb_map_t*)face_db_map;
+		if (_face_db_map.empty())
+			return;
+		
+		facedb_map_t::iterator iter = _face_db_map.find(dbid);
+		if (iter == _face_db_map.end())
+			return nullptr;
+		
+		tsfdb = iter->second;
+	}
+	
+	if (tsfdb != nullptr)
+	{
+		PLP_MUTEX_UNLOCK(tsfdb->db_mutex,);
+	}
+}
diff --git a/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.h b/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.h
index d4bfdcd..dea0b55 100644
--- a/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.h
+++ b/RtspFace/SensetimeFaceAPIWrapper/src/FaceDBPool.h
@@ -1,4 +1,26 @@
 #ifndef _FACEDBPOOL_H_
 #define _FACEDBPOOL_H_
 
+#include "faceAPI.h"
+
+class FaceDBPool
+{
+public:
+	FaceDBPool();
+	~FaceDBPool();
+	
+	void manage(int dbid, faceAPI* db);
+	void unmanage(int dbid);
+	
+	faceAPI* get_free(int dbid);
+	void release(int dbid);
+	
+	bool wait_free() { return true; }
+	bool notify_free() { return true; }
+
+private:
+	void* pool_mutex;
+	void* face_db_map;
+};
+
 #endif
diff --git a/RtspFace/face_daemon_proto.h b/RtspFace/face_daemon_proto.h
index cf3d4a4..0a63cec 100644
--- a/RtspFace/face_daemon_proto.h
+++ b/RtspFace/face_daemon_proto.h
@@ -13,7 +13,7 @@
 	{
 		FDC__FIRST,
 		FDC_SENSETIMEFACEDETECT = EVPCommand::EVPC_USER_DEFINE + 1,
-		FDC_SENSETIMEFACEDETECT_RESULT
+		FDC_SENSETIMEFACEDETECT_RESULT,
 		FDC__LAST,
 	};
 };
@@ -29,7 +29,8 @@
 
 struct FDP_FaceDetectResult
 {
-	int32_t stid; // sensetime id
+	int32_t school_id;
+	int32_t st_id; // sensetime id
 };
 
 #pragma pack()
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)
diff --git a/RtspFace/make.sh b/RtspFace/make.sh
index eed3ccf..68552da 100644
--- a/RtspFace/make.sh
+++ b/RtspFace/make.sh
@@ -66,6 +66,7 @@
 g++ PL_Gainer.cpp $CFLAGS $CPPFLAGS
 
 g++ PL_SensetimeFaceTrack.cpp $CFLAGS $CPPFLAGS
+g++ PL_SensetimeFaceDetect.cpp $CFLAGS $CPPFLAGS
 
 g++ PL_DlibFaceTrack.cpp -O3 $CFLAGS $CPPFLAGS
 
@@ -73,13 +74,16 @@
 g++ $FFMPEGRTSPSERVER_BASE/LiveRTSPServer.cpp $CFLAGS $CPPFLAGS
 g++ $FFMPEGRTSPSERVER_BASE/LiveServerMediaSubsession.cpp $CFLAGS $CPPFLAGS
 
+g++ ./SensetimeFaceAPIWrapper/src/FaceDBPool.cpp $CFLAGS $CPPFLAGS
+
 g++ -g -std=c++11 \
   main.o PipeLine.o PipeLinePool.o \
   ev_server.o \
   PL_RTSPClient.o PL_H264Decoder.o PL_H264Encoder.o PL_AVFrameYUV420.o PL_AVFrameBGRA.o PL_Queue.o PL_Scale.o PL_Fork.o PL_Payer.o PL_Gainer.o \
-  PL_SensetimeFaceTrack.o \
+  PL_SensetimeFaceTrack.o PL_SensetimeFaceDetect.o \
   PL_DlibFaceTrack.o \
   $FFMPEGRTSPSERVER_OBJ PL_RTSPServer.o \
+  FaceDBPool.o \
   $LDFLAGS -o rtsp_face
 
 #export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBX264_BASE/lib:$FFMPEG_BASE/lib:$SENSETIMEFACESDK_BASE/libs/linux-x86_64:$LIBLOG4CPP_BASE/lib:$DLIB_BASE/build/dlib

--
Gitblit v1.8.0