From 109ffe9a777658936a38d0c146579a67c60a0d17 Mon Sep 17 00:00:00 2001
From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期四, 11 五月 2017 17:48:48 +0800
Subject: [PATCH] 

---
 RtspFace/main_face_daemon.cpp |  423 +++++++++++++++++++++++-----------------------------
 1 files changed, 188 insertions(+), 235 deletions(-)

diff --git a/RtspFace/main_face_daemon.cpp b/RtspFace/main_face_daemon.cpp
index 51fa8e2..9058ee4 100644
--- a/RtspFace/main_face_daemon.cpp
+++ b/RtspFace/main_face_daemon.cpp
@@ -1,4 +1,5 @@
 #include "PipeLine.h"
+#include "MaterialBuffer.h"
 #include "PL_RTSPClient.h"
 #include "PL_RTSPServer.h"
 #include "PL_H264Decoder.h"
@@ -8,273 +9,225 @@
 #include "PL_Queue.h"
 #include "PL_Scale.h"
 #include "PL_Fork.h"
-
+#include "PL_Payer.h"
+#include "PL_Gainer.h"
 #include "PL_SensetimeFaceTrack.h"
-
+#include "PL_SensetimeFaceDetect.h"
 #include "PL_DlibFaceTrack.h"
+
+#include "PipeLinePool.h"
+
+#include "ev_server.h" 
+#include "ev_proto.h"
+#include "face_daemon_proto.h"
+
+#include "SensetimeFaceAPIWrapper/src/FaceDBPool.h"
+#include "SensetimeFaceAPIWrapper/src/faceAPI.h"
 
 #include "logger.h"
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include <sys/time.h>
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <err.h>
-
-#include <event.h>
-
-#define SERVER_PORT 5432
-#define REUSEADDR_ON 1
-
-// A struct for client specific data, also includes pointer to create a list of clients.
-struct EVClient
+template<typename TPoolPtr, typename TPoolElem>
+struct PoolElemLocker
 {
-	// The clients socket.
-	int fd;
-
-	// The bufferedevent for this client.
-	struct bufferevent *buf_ev;
-	
-	EVClient() : fd(-1), buf_ev(nullptr)
-	{ }
+	TPoolPtr pool;
+	TPoolElem elem;
+	PoolElemLocker(TPoolPtr _pool, TPoolElem _elem) : pool(_pool), elem(_elem)
+	{
+		
+	}
+	~PoolElemLocker()
+	{
+		pool->release(elem);
+		pool->notify_free();
+	}
 };
 
-// Set a socket to non-blocking mode.
-int setnonblock(int fd)
+template<typename TArrayPtr>
+struct ArrayDeleter
 {
-	int flags;
+	TArrayPtr array;
+	ArrayDeleter(TArrayPtr _array) : array(_array)
+	{
+		
+	}
+	~ArrayDeleter()
+	{
+		delete[] array;
+	}
+};
 
-	flags = fcntl(fd, F_GETFL);
-	if (flags < 0)
-		return flags;
-	flags |= O_NONBLOCK;
-	if (fcntl(fd, F_SETFL, flags) < 0)
-		return -1;
+PipeLinePool g_PipeLinePool;
 
-	return 0;
-}
+FaceDBPool g_faceAPIPool;//#todo config
 
-// Called by libevent when there is data to read.
-void buffered_on_read(struct bufferevent *bufev, void *arg)
+evclient_proc_t evclient_proc;
+
+bool send_SensetimeFaceDetectResult(EVClientStub& client, PipeMaterial& lastPm)
 {
-	struct EVClient *client = (struct EVClient *)arg;
+	//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() << std::endl;
+	//#todo send result packet
+
+	if (lastPm.buffer == nullptr || lastPm.type != PipeMaterial::PMT_BYTES || lastPm.buffSize != sizeof(SensetimeFaceDetectResult))
+	{
+		LOG_WARN << "pm not available" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
+		return false;
+	}
 	
-	// Write back the read buffer. It is important to note that
-	// bufferevent_write_buffer will drain the incoming data so it
-	// is effectively gone after we call it.
-	//LOG_DEBUG << (char*)bufev->input;
-	//bufferevent_write_buffer(bufev, bufev->input);
+	const SensetimeFaceDetectResult*  result = (const SensetimeFaceDetectResult*)lastPm.buffer;
 	
-	char buff[100] = {'\0'};
-	size_t readSize = bufferevent_read(bufev, buff, sizeof(buff));
-	LOG_DEBUG << "readSize=" << readSize << "\t" << buff;
+	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;
 }
 
-// Called by libevent when the write buffer reaches 0. 
-// We only provide this because libevent expects it, but we don't use it.
-void buffered_on_write(struct bufferevent *bufev, void *arg)
+bool ev_proc_SensetimeFaceDetect(EVClientStub& client)
 {
+	//#test send 01000B0000004142434445
+	//LOG_DEBUG << "cmd=" << evpHeader->cmd << ", size=" << evpHeader->size << ", \t" << (char*)(evpHeader + sizeof(EVPHeader)) << std::endl;
+	//return true;
+	
+	FDP_Image* fdpImage = (FDP_Image*)(client.recvBuff + sizeof(EVPHeader));
+
+	FaceDB* _faceDB = g_faceAPIPool.get_free(fdpImage->school_id);
+	if (_faceDB == nullptr)
+	{
+		LOG_WARN << "can't get face db" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_PARAMETER_ERROR);
+		return false;
+	}
+	
+	PoolElemLocker<FaceDBPool*, int> _lock_faceAPI(&g_faceAPIPool, fdpImage->school_id);
+
+	PipeLine* pipeLine = g_PipeLinePool.get_free();
+	if (pipeLine == nullptr)
+	{
+		LOG_WARN << "can't get free pipeline" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
+		return false;
+	}
+	
+	PoolElemLocker<PipeLinePool*, PipeLine*> _lock_pipeLine(&g_PipeLinePool, pipeLine);
+
+	// fill
+	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 = &dbFrame;
+	pm.buffSize = 0;
+
+	PipeLineElem* plElem = pipeLine->pipe(&pm);
+	if (! pipeLine->check_pipe_complete(plElem))
+	{
+		LOG_WARN << "pipeline not complete" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
+		return false;
+	}
+	
+	if (!plElem->gain(pm))
+	{
+		LOG_WARN << "pipeline gain error" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
+		return false;
+	}
+	
+	// can not release pipleline unless pm not used
+	send_SensetimeFaceDetectResult(client, pm);
+
+	return false;
 }
 
-// Called by libevent when there is an error on the underlying socket descriptor.
-void buffered_on_error(struct bufferevent *bufev, short what, void *arg)
+bool ev_proc(EVClientStub& client)
 {
-	struct EVClient *client = (struct EVClient *)arg;
+	EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
+	if (evpHeader->size != client.recvBuffSize)
+	{
+		LOG_WARN << "Truncated buffer " << (evpHeader->size - client.recvBuffSize) << " bytes" << std::endl;
+		return false;
+	}
 
-	if (what & EVBUFFER_EOF)
+	switch(evpHeader->cmd)
 	{
-		//Client disconnected, remove the read event and the free the client structure.
-		LOG_INFO << "Client disconnected.";
+	case EVPCommand::EVPC_USER_DEFINE + 1:
+		return ev_proc_SensetimeFaceDetect(client);
+	break;
+	default:
+		LOG_WARN << "Unknown command" << std::endl;
+		ev_send_status_packet(client, EVPStatus::EVPS_PARAMETER_ERROR);
+		return false;
+	break;
 	}
-	else
-	{
-		LOG_WARN << "Client socket error, disconnecting.";
-	}
-	bufferevent_free(client->buf_ev);
-	close(client->fd);
-	delete client;
+	
+	// return false to disconnect
+	return false;
 }
 
-// This function will be called by libevent when there is a connection ready to be accepted.
-void on_accept(int fd, short ev, void *arg)
-{
-	struct sockaddr_in client_addr;
-	socklen_t client_len = sizeof(client_addr);
-	int client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len);
-	if (client_fd < 0)
-	{
-		LOG_WARN << "accept failed";
-		return;
-	}
-
-	// Set the client socket to non-blocking mode.
-	if (setnonblock(client_fd) < 0)
-		LOG_WARN << "failed to set client socket non-blocking";
-
-	// We've accepted a new client, create a client object.
-	struct EVClient* client = new EVClient;
-	if (client == NULL)
-	{
-		LOG_ERROR << "malloc failed";
-	}
-	client->fd = client_fd;
-
-	// Create the buffered event.
-	client->buf_ev = bufferevent_new(client_fd, buffered_on_read, buffered_on_write, buffered_on_error, client);
-
-	// We have to enable it before our callbacks will be called.
-	bufferevent_enable(client->buf_ev, EV_READ);
-
-	LOG_INFO << "Accepted connection from " << inet_ntoa(client_addr.sin_addr);
-}
-
-int main(int argc, char **argv)
+int main(int argc, char** argv)
 {
 	initLogger(LV_DEBUG);
-
-	// Initialize libevent.
-	event_init();
-
-	// Create our listening socket.
-	int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
-	if (listen_fd < 0)
-	{
-		LOG_ERROR << "create socket failed";
-		return EXIT_FAILURE;
-	}
 	
-	struct sockaddr_in listen_addr;
-	memset(&listen_addr, 0, sizeof(listen_addr));
-	listen_addr.sin_family = AF_INET;
-	listen_addr.sin_addr.s_addr = INADDR_ANY;
-	listen_addr.sin_port = htons(SERVER_PORT);
-	if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0)
-	{
-		LOG_ERROR << "bind failed";
-		return EXIT_FAILURE;
-	}
-	
-	if (listen(listen_fd, 5) < 0)
-	{
-		LOG_ERROR << "listen failed";
-		return EXIT_FAILURE;
-	}
-	
-	// Set the socket to non-blocking, this is essential in event based programming with libevent.
-	int reuseaddr_on = REUSEADDR_ON;
-	setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on));
-	if (setnonblock(listen_fd) < 0)
-	{
-		LOG_ERROR << "failed to set server socket to non-blocking";
-		return EXIT_FAILURE;
-	}
-
-	// We now have a listening socket, we create a read event to be notified when a client connects.
-	struct event ev_accept;
-	event_set(&ev_accept, listen_fd, EV_READ|EV_PERSIST, on_accept, NULL);
-	event_add(&ev_accept, NULL);
-
-	// Start the event loop.
-	event_dispatch();
-
-	return EXIT_SUCCESS;
-}
-
-
-
-
-
-
-int _main(int argc, char** argv)
-{
-	initLogger(LV_DEBUG);
-
-	PipeLine pipeLine;
-
 	PipeLine::register_global_elem_creator("PL_SensetimeFaceTrack", create_PL_SensetimeFaceTrack);
-
+	PipeLine::register_global_elem_creator("PL_Gainer", create_PL_Gainer);
+	
+	g_PipeLinePool = new PipeLinePool(true);
+	
+	for (int i = 0; i < 5; i++)
 	{
-		PL_RTSPClient* rtspClient = (PL_RTSPClient*)pipeLine.push_elem("PL_RTSPClient");
-		PL_RTSPClient_Config rtspConfig;
-		rtspConfig.progName = argv[0];
-		rtspConfig.rtspURL = argv[1];
-		rtspConfig.aux = true; // ffmpeg need aux, but live555 not
-		rtspConfig.verbosityLevel = 1;
-		rtspConfig.tunnelOverHTTPPortNum = 0;
-		rtspConfig.args = nullptr;
-		bool ret = rtspClient->init(&rtspConfig);
-		if (!ret)
-			{
-				LOG_ERROR << "rtspClient.init error";
-				exit(EXIT_FAILURE);
-			}
-	}
+		PipeLine* pipeLine = new PipeLine;
 
-	{
-		PL_H264Decoder* h264Decoder = (PL_H264Decoder*)pipeLine.push_elem("PL_H264Decoder");
-		bool ret = h264Decoder->init(nullptr);
-		if (!ret)
-			{
-				LOG_ERROR << "PL_H264Decoder.init error";
-				exit(EXIT_FAILURE);
-			}
-	}
-
-	{
-		PL_AVFrameYUV420* avFrameYUV420 = (PL_AVFrameYUV420*)pipeLine.push_elem("PL_AVFrameYUV420");
-		bool ret = avFrameYUV420->init(nullptr);
-		if (!ret)
-			{
-				LOG_ERROR << "PL_AVFrameYUV420.init error";
-				exit(EXIT_FAILURE);
-			}
-	}
-
-	{
-		PL_Scale_Config config;
-		config.toWidth = 800;
-		config.toHeight = 600;
-		PL_Scale* ple = (PL_Scale*)pipeLine.push_elem("PL_Scale");
-		bool ret = ple->init(&config);
-		if (!ret)
-			{
-				LOG_ERROR << "PL_Scale.init error";
-				exit(EXIT_FAILURE);
-			}
-	}
-
-	PL_SensetimeFaceTrack* sensetimeFaceTrack;
-	{
-		SensetimeFaceTrackConfig config;
-		config.generate_face_feature = true;
-		sensetimeFaceTrack = (PL_SensetimeFaceTrack*)pipeLine.push_elem("PL_SensetimeFaceTrack");
-		sensetimeFaceTrack->init(&config);
-	}
-
-	while(true)
 		{
-			//LOG_ERROR << "begin pipe";
-
-			PipeMaterial pm;
-			if (pipeLine.pipe(&pm) == sensetimeFaceTrack);
-			sensetimeFaceTrack->gain(pm);
-
-			if (pm.type == PipeMaterial::PMT_PM_LIST)
-				{
-					PipeMaterial& facePM = ((PipeMaterial*)(pm.buffer))[1];
-					st_ff_vect_t& faceFeatures = *((st_ff_vect_t*)facePM.buffer);
-					LOG_NOTICE << "faceFeatures " << faceFeatures.size();
-				}
-
-			//LOG_ERROR << "end pipe";
+			PL_Payer_Config config;
+			config.copyData = true;//#todo false
+			PL_Gainer* ple = (PL_Gainer*)pipeLine->push_elem("PL_Gainer");
+			bool ret = ple->init(&config);
+			if (!ret)
+			{
+				LOG_ERROR << "ple init error" << std::endl;
+				exit(EXIT_FAILURE);
+			}
 		}
+		
+		{
+			SensetimeFaceTrackConfig config;
+			config.draw_face_rect = false;
+			config.draw_face_feature_point = false;
+			config.generate_face_feature = true;
+			PL_SensetimeFaceTrack* ple = (PL_SensetimeFaceTrack*)pipeLine->push_elem("PL_SensetimeFaceTrack");
+			bool ret = ple->init(&config);
+			if (!ret)
+			{
+				LOG_ERROR << "ple init error" << std::endl;
+				exit(EXIT_FAILURE);
+			}
+		}
+		
+		g_PipeLinePool.manage(pipeLine);
+	}
+	
+	evclient_proc = ev_proc;
+	return server_main(argc, argv);
 }

--
Gitblit v1.8.0