xuxiuxi
2017-05-11 109ffe9a777658936a38d0c146579a67c60a0d17
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,70 +9,180 @@
#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"
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_faceAPIPool;//#todo config
evclient_proc_t evclient_proc;
bool ev_proc_SensetimeFaceDetect(EVClientStub& client)
bool send_SensetimeFaceDetectResult(EVClientStub& client, PipeMaterial& lastPm)
{
}
   //if (lastPm.type != PipeMaterial::PMT_PM_LIST)
bool ev_proc(EVClientStub& client)
{
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   //#todo check cmd and size
   //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
   //#test send 01000B0000004142434445
   //LOG_DEBUG << "cmd=" << evpHeader->cmd << ", size=" << evpHeader->size << ", \t" << (char*)(evpHeader + sizeof(EVPHeader));
   //return true;
   PipeLine* pipeLine = nullptr;
   if (g_PipeLinePool.wait_free())
      pipeLine = g_PipeLinePool.get_free();
   if (pipeLine == nullptr)
   if (lastPm.buffer == nullptr || lastPm.type != PipeMaterial::PMT_BYTES || lastPm.buffSize != sizeof(SensetimeFaceDetectResult))
   {
      LOG_WARN << "can't get free pipeline";//#todo send err packet
      LOG_WARN << "pm not available" << std::endl;
      ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
      return false;
   }
   
   PipeMaterial pm;
   // fill
   const SensetimeFaceDetectResult*  result = (const SensetimeFaceDetectResult*)lastPm.buffer;
   
   PipeLineElem* plElem = pipeLine.pipe(&pm);
   if (! pipeLine.check_pipe_complete(plElem))
   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)
{
   //#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 << "pipeline not complete";
      g_PipeLinePool.release(pipeLine);//#todo send err packet
      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";
      g_PipeLinePool.release(pipeLine);//#todo send err packet
      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;
}
bool ev_proc(EVClientStub& client)
{
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   if (evpHeader->size != client.recvBuffSize)
   {
      LOG_WARN << "Truncated buffer " << (evpHeader->size - client.recvBuffSize) << " bytes" << std::endl;
      return false;
   }
   if (pm.type == PipeMaterial::PMT_PM_LIST)
   switch(evpHeader->cmd)
   {
      PipeMaterial& facePM = ((PipeMaterial*)(pm.buffer))[1];
      st_ff_vect_t& faceFeatures = *((st_ff_vect_t*)facePM.buffer);
      LOG_NOTICE << "faceFeatures " << faceFeatures.size();
      //#todo send result packet
   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;
   }
   
   g_PipeLinePool.release(pipeLine);
   // return false to disconnect
   return false;
}
@@ -80,6 +191,7 @@
   initLogger(LV_DEBUG);
   
   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);
   
@@ -87,17 +199,28 @@
   {
      PipeLine* pipeLine = new PipeLine;
      {//payer//#todo
      {
         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* sensetimeFaceTrack = (PL_SensetimeFaceTrack*)pipeLine->push_elem("PL_SensetimeFaceTrack");
         bool ret = sensetimeFaceTrack->init(&config);
         PL_SensetimeFaceTrack* ple = (PL_SensetimeFaceTrack*)pipeLine->push_elem("PL_SensetimeFaceTrack");
         bool ret = ple->init(&config);
         if (!ret)
         {
            LOG_ERROR << "sensetimeFaceTrack init error";
            LOG_ERROR << "ple init error" << std::endl;
            exit(EXIT_FAILURE);
         }
      }
@@ -107,22 +230,4 @@
   
   evclient_proc = ev_proc;
   return server_main(argc, argv);
   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";
      }
}