提交 9a6c3684 authored 作者: blu's avatar blu

init

上级 0c2c96ea
#ifndef __AVCVHELPERS_H__
#define __AVCVHELPERS_H__
extern "C" {
#include <libswscale/swscale.h>
#include <libavformat/avformat.h>
}
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <map>
using namespace std;
namespace avcvhelpers {
// AVFrame mat2frame(cv::Mat* frame)
// {
// // TODO.
// AVFrame dst;
// // cv::Size frameSize = frame->size();
// // AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_RAWVIDEO);
// // AVFormatContext* outContainer = avformat_alloc_context();
// // AVStream *outStream = avformat_new_stream(outContainer, encoder);
// // // outStream->codec->pix_fmt = AV_PIX_FMT_BGR24;
// // outStream->codecpar->format = AV_PIX_FMT_BGR24;
// // outStream->codecpar->width = frame->cols;
// // outStream->codecpar->height = frame->rows;
// // avpicture_fill((AVPicture*)&dst, frame->data, AV_PIX_FMT_BGR24, outStream->codecpar->width, outStream->codecpar->height);
// // dst.width = frameSize.width;
// // dst.height = frameSize.height;
// return dst;
// }
// struct Point {
// int format;
// int width;
// int height;
// bool operator==(Point const &other) {
// return format == other.format && width == other.width && height == other.height;
// }
// size_t operator()(Point const &pt) {
// size_t h = hash<int>{}(pt.format);
// h ^= (hash<int>{}(pt.width) <<1);
// h ^= (hash<int>{}(pt.height) <<1);
// return h;
// }
// };
void frame2mat(AVPixelFormat format, const AVFrame * frame, cv::Mat& image)
{
int width = frame->width;
int height = frame->height;
// Allocate the opencv mat and store its stride in a 1-element array
if (image.rows != height || image.cols != width || image.type() != CV_8UC3) {
image = cv::Mat(height, width, CV_8UC3);
}
int cvLinesizes[1];
cvLinesizes[0] = image.step1();
// Convert the colour format and write directly to the opencv matrix
// TODO: optimization
switch (format) {
case AV_PIX_FMT_YUVJ420P:
format = AV_PIX_FMT_YUV420P;
break;
case AV_PIX_FMT_YUVJ422P:
format = AV_PIX_FMT_YUV422P;
break;
case AV_PIX_FMT_YUVJ444P:
format = AV_PIX_FMT_YUV444P;
break;
case AV_PIX_FMT_YUVJ440P:
format = AV_PIX_FMT_YUV440P;
break;
default:
;
}
SwsContext* conversion = sws_getContext(width, height, (AVPixelFormat) format, width, height, AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
sws_freeContext(conversion);
}
}
#endif
#ifndef __ZMQ_HELPER_H__
#define __ZMQ_HELPER_H__
#include "zmq.h"
#include <vector>
#include <spdlog/spdlog.h>
using namespace std;
namespace zmqhelper {
#define EV_HEARTBEAT_SECONDS 30
#define MSG_HELLO "hello"
#define EV_MSG_META_PING "ping"
#define EV_MSG_META_PONG "pong"
#define EV_MSG_META_PEEROFFLINE "offline"
#define EV_MSG_META_RESTART "restart"
#define EV_MSG_META_UPDATE "update"
#define EV_MSG_META_EVENT "event"
#define EV_MSG_META_AVFORMATCTX "afctx"
#define EV_NUM_CACHE_PERPEER 100
//
string body2str(vector<uint8_t> body){
return string((char *)(body.data()), body.size());
}
vector<uint8_t> data2body(char* data, int len){
vector<uint8_t> v;
v.insert(v.end(), (uint8_t *)data, (uint8_t *)data+len);
return v;
}
vector<uint8_t> str2body(string const &str){
vector<uint8_t> v;
v.insert(v.end(), (uint8_t*)(str.data()), (uint8_t *)(str.data()) + str.size());
return v;
}
// proto: 1. on router [sender_id] [target_id] [body]
// 2. on dealer [sender_id] [body]
vector<vector<uint8_t> > z_recv_multiple(void *s, bool nowait=false) {
int64_t more = 1;
vector<vector<uint8_t> > body;
int cnt = 0;
int ret = 0;
while(more > 0) {
cnt++;
zmq_msg_t msg;
ret = zmq_msg_init(&msg);
if(ret < 0) {
spdlog::debug("failed to receive multiple msg on zmq_msg_init: {}", zmq_strerror(zmq_errno()));
break;
}
ret = zmq_recvmsg(s, &msg, nowait?ZMQ_DONTWAIT:0);
if(ret < 0) {
spdlog::debug("z_recv_multiple: {}", zmq_strerror(zmq_errno()));
break;
}
vector<uint8_t> v;
v.insert(v.end(), (uint8_t*)zmq_msg_data(&msg), (uint8_t*)zmq_msg_data(&msg)+ret);
body.push_back(v);
spdlog::debug("z_rcv_multiple: {}", body2str(v).substr(0, v.size()> 100? 15:v.size()));
zmq_msg_close(&msg);
size_t more_size = sizeof(more);
ret = zmq_getsockopt(s, ZMQ_RCVMORE, &more, &more_size);
if(ret < 0) {
spdlog::debug("z_recv_multiple: {}", zmq_strerror(zmq_errno()));
break;
}
}
return body;
}
// proto [sender_id(only when no identifier set in setsockopts)] [target_id] [body]
int z_send_multiple(void *s, vector<vector<uint8_t> >&body) {
int cnt = 0, ret = 0;
zmq_msg_t msg;
for(auto &i:body) {
ret = zmq_msg_init_size(&msg, i.size());
memcpy(zmq_msg_data(&msg), (void*)(i.data()), i.size());
spdlog::debug("z_send_multiple: {}", body2str(i).substr(0, i.size()>100?15:i.size()));
if(ret < 0) {
spdlog::debug("z_send_multiple: {}", zmq_strerror(zmq_errno()));
break;
}
ret = zmq_msg_send(&msg, s, cnt==(body.size()-1)?0:(ZMQ_SNDMORE));
zmq_msg_close(&msg);
if(ret < 0) {
spdlog::debug("z_send_multiple: {}", zmq_strerror(zmq_errno()));
break;
}
cnt++;
}
return ret;
}
}
#endif
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论