New file |
| | |
| | | #include "PipeLine.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_SensetimeFaceTrack.h"
|
| | |
|
| | | #include "PL_DlibFaceTrack.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
|
| | | {
|
| | | // The clients socket.
|
| | | int fd;
|
| | |
|
| | | // The bufferedevent for this client.
|
| | | struct bufferevent *buf_ev;
|
| | | |
| | | 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;
|
| | | |
| | | // 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);
|
| | | |
| | | 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)
|
| | | {
|
| | | //Client disconnected, remove the read event and the free the client structure.
|
| | | LOG_INFO << "Client disconnected.";
|
| | | }
|
| | | else
|
| | | {
|
| | | 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;
|
| | | }
|
| | |
|
| | | // 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)
|
| | | {
|
| | | 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);
|
| | |
|
| | | {
|
| | | 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);
|
| | | }
|
| | | }
|
| | |
|
| | | {
|
| | | 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";
|
| | | }
|
| | | }
|