houxiao
2017-01-10 4d2fdfa71faf0e79e10135f9ef47b68cb707d49b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "PipeLine.h"
#include "MaterialBuffer.h"
#include "PL_RTSPClient.h"
#include "PL_RTSPServer.h"
#include "PL_H264Decoder.h"
#include "PL_H264Encoder.h"
#include "PL_AVFrameYUV420.h"
#include "PL_AVFrameBGRA.h"
#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_DlibFaceTrack.h"
 
#include "PipeLinePool.h"
 
#include "ev_server.h" 
#include "ev_proto.h"
#include "face_daemon_proto.h"
 
#include "logger.h"
 
PipeLinePool g_PipeLinePool;
 
evclient_proc_t evclient_proc;
 
void send_SensetimeFaceDetectResult(PipeMaterial& lastPm)
{
    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
    }
}
 
bool ev_proc_SensetimeFaceDetect(EVClientStub& client)
{
    //#test send 01000B0000004142434445
    //LOG_DEBUG << "cmd=" << evpHeader->cmd << ", size=" << evpHeader->size << ", \t" << (char*)(evpHeader + sizeof(EVPHeader));
    //return true;
    
    FDP_Image* fdpImage = (FDP_Image*)(client.recvBuff + sizeof(EVPHeader));
    
    PipeLine* pipeLine = nullptr;
    if (g_PipeLinePool.wait_free())
        pipeLine = g_PipeLinePool.get_free();
    
    if (pipeLine == nullptr)
    {
        LOG_WARN << "can't get free pipeline";
        ev_send_status_packet(client, EVPStatus::EVPS_INTERNAL_ERROR);
        return false;
    }
    
    // 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
    
    PipeMaterial pm;
    pm.type = PipeMaterial::PMT_FRAME;
    pm.buffer = &frame;
    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;
    }
    
    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);
    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";
        return false;
    }
 
    switch(evpHeader->cmd)
    {
    case EVPCommand::EVPC_USER_DEFINE + 1:
        return ev_proc_SensetimeFaceDetect(client);
    break;
    default:
        LOG_WARN << "Unknown command";
        ev_send_status_packet(client, EVPStatus::EVPS_PARAMETER_ERROR);
        return false;
    break;
    }
    
    // return false to disconnect
    return false;
}
 
int main(int argc, char** argv)
{
    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);
    
    for (int i = 0; i < 5; i++)
    {
        PipeLine* pipeLine = new PipeLine;
 
        {
            PL_Payer_Config config;
            config.copyData = true;
            PL_Gainer* ple = (PL_Gainer*)pipeLine->push_elem("PL_Gainer");
            bool ret = ple->init(&config);
            if (!ret)
            {
                LOG_ERROR << "ple init error";
                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";
                exit(EXIT_FAILURE);
            }
        }
        
        g_PipeLinePool.manage(pipeLine);
    }
    
    evclient_proc = ev_proc;
    return server_main(argc, argv);
}