houxiao
2017-01-10 43af8d48ca0d2ec219a7641d5555fe4c2479b047
RtspFace/main_face_daemon.cpp
@@ -8,257 +8,105 @@
#include "PL_Queue.h"
#include "PL_Scale.h"
#include "PL_Fork.h"
#include "PL_SensetimeFaceTrack.h"
#include "PL_DlibFaceTrack.h"
#include "PipeLinePool.h"
#include "ev_server.h"
#include "ev_proto.h"
#include "logger.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
PipeLinePool g_PipeLinePool;
#include <sys/time.h>
evclient_proc_t evclient_proc;
#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
bool ev_proc_SensetimeFaceDetect(EVClientStub& client)
{
   // The clients socket.
   int fd;
}
   // The bufferedevent for this client.
   struct bufferevent *buf_ev;
bool ev_proc(EVClientStub& client)
{
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   //#todo check cmd and size
   //#test send 01000B0000004142434445
   //LOG_DEBUG << "cmd=" << evpHeader->cmd << ", size=" << evpHeader->size << ", \t" << (char*)(evpHeader + sizeof(EVPHeader));
   //return true;
   
   EVClient() : fd(-1), buf_ev(nullptr)
   { }
};
// Set a socket to non-blocking mode.
int setnonblock(int fd)
{
   int flags;
   flags = fcntl(fd, F_GETFL);
   if (flags < 0)
      return flags;
   flags |= O_NONBLOCK;
   if (fcntl(fd, F_SETFL, flags) < 0)
      return -1;
   return 0;
}
// Called by libevent when there is data to read.
void buffered_on_read(struct bufferevent *bufev, void *arg)
{
   struct EVClient *client = (struct EVClient *)arg;
   PipeLine* pipeLine = nullptr;
   if (g_PipeLinePool.wait_free())
      pipeLine = g_PipeLinePool.get_free();
   
   // 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);
   if (pipeLine == nullptr)
   {
      LOG_WARN << "can't get free pipeline";//#todo send err packet
      return false;
   }
   
   char buff[100] = {'\0'};
   size_t readSize = bufferevent_read(bufev, buff, sizeof(buff));
   LOG_DEBUG << "readSize=" << readSize << "\t" << buff;
}
// 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)
{
}
// Called by libevent when there is an error on the underlying socket descriptor.
void buffered_on_error(struct bufferevent *bufev, short what, void *arg)
{
   struct EVClient *client = (struct EVClient *)arg;
   if (what & EVBUFFER_EOF)
   PipeMaterial pm;
   // fill
   PipeLineElem* plElem = pipeLine.pipe(&pm);
   if (! pipeLine.check_pipe_complete(plElem))
   {
      //Client disconnected, remove the read event and the free the client structure.
      LOG_INFO << "Client disconnected.";
      LOG_WARN << "pipeline not complete";
      g_PipeLinePool.release(pipeLine);//#todo send err packet
      return false;
   }
   else
   if (!plElem->gain(pm))
   {
      LOG_WARN << "Client socket error, disconnecting.";
   }
   bufferevent_free(client->buf_ev);
   close(client->fd);
   delete client;
}
// 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;
      LOG_WARN << "pipeline gain error";
      g_PipeLinePool.release(pipeLine);//#todo send err packet
      return false;
   }
   // 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)
   if (pm.type == PipeMaterial::PMT_PM_LIST)
   {
      LOG_ERROR << "malloc failed";
      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
   }
   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);
   g_PipeLinePool.release(pipeLine);
   return false;
}
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);
   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)
      PipeLine* pipeLine = new PipeLine;
      {//payer//#todo
      }
      {
         SensetimeFaceTrackConfig config;
         config.generate_face_feature = true;
         PL_SensetimeFaceTrack* sensetimeFaceTrack = (PL_SensetimeFaceTrack*)pipeLine->push_elem("PL_SensetimeFaceTrack");
         bool ret = sensetimeFaceTrack->init(&config);
         if (!ret)
         {
            LOG_ERROR << "rtspClient.init error";
            LOG_ERROR << "sensetimeFaceTrack init error";
            exit(EXIT_FAILURE);
         }
      }
      g_PipeLinePool.manage(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);
   }
   evclient_proc = ev_proc;
   return server_main(argc, argv);
   while(true)
      {