提交 ed3f32a9 authored 作者: blu's avatar blu

init

上级 eee69bf3
...@@ -20,28 +20,35 @@ namespace fs = std::filesystem; ...@@ -20,28 +20,35 @@ namespace fs = std::filesystem;
using namespace std; using namespace std;
class PacketProducer: public TinyThread { class EvPuller: public TinyThread {
private: private:
void *pPubContext = NULL; // for packets publishing void *pPubCtx = NULL; // for packets publishing
void *pPublisher = NULL; void *pPub = NULL;
void *pRepCtx = NULL; // for packets REP
void *pRep = NULL;
AVFormatContext *pAVFormatInput = NULL; AVFormatContext *pAVFormatInput = NULL;
string urlIn; string urlIn, urlPub;
int *streamList = NULL, numStreams = 0; int *streamList = NULL, numStreams = 0;
public: public:
PacketProducer(string urlIn):urlIn(urlIn){ EvPuller()
setupMq(); {
} int ret = 0;
do {
init();
ret = setupMq();
}while(ret < 0);
}
~PacketProducer(){ ~EvPuller()
} {
}
protected: protected:
// Function to be executed by thread function // Function to be executed by thread function
void run() void run()
{ {
int ret = 0; int ret = 0;
setupMq();
if ((ret = avformat_open_input(&pAVFormatInput, urlIn.c_str(), NULL, NULL)) < 0) { if ((ret = avformat_open_input(&pAVFormatInput, urlIn.c_str(), NULL, NULL)) < 0) {
spdlog::error("Could not open input file {}", urlIn); spdlog::error("Could not open input file {}", urlIn);
} }
...@@ -83,7 +90,7 @@ protected: ...@@ -83,7 +90,7 @@ protected:
AVStream *in_stream; AVStream *in_stream;
AVPacket packet; AVPacket packet;
zmq_msg_t msg; zmq_msg_t msg;
ret = av_read_frame(pAVFormatInput, &packet); ret = av_read_frame(pAVFormatInput, &packet);
if (ret < 0) { if (ret < 0) {
spdlog::error("failed read packet: {}", av_err2str(ret)); spdlog::error("failed read packet: {}", av_err2str(ret));
...@@ -109,29 +116,66 @@ protected: ...@@ -109,29 +116,66 @@ protected:
char * data = NULL; char * data = NULL;
int size = AVPacketSerializer::encode(packet, &data); int size = AVPacketSerializer::encode(packet, &data);
zmq_msg_init_data(&msg, (void*)data, size, mqPacketFree, NULL); zmq_msg_init_data(&msg, (void*)data, size, mqPacketFree, NULL);
zmq_send_const(pPublisher, zmq_msg_data(&msg), size, 0); zmq_send_const(pPub, zmq_msg_data(&msg), size, 0);
av_packet_unref(&packet); av_packet_unref(&packet);
} }
// TODO: // TODO:
if(ret < 0 && !bStopSig) { if(ret < 0 && !bStopSig) {
// reconnect // reconnect
}else { }
else {
std::cout << "Task End" << std::endl; std::cout << "Task End" << std::endl;
} }
} }
private: private:
int init()
{
bool inited = false;
while(!inited) {
// TODO: read db to get sn
const char* sn = "ILS-2";
// req config
json jr = cloudutils::registry(sn, "evpuller", 0);
bool bcnt = false;
try {
spdlog::info("registry: {:s}", jr.dump());
string ipc = jr["data"]["ipc"];
string user = jr["data"]["username"];
string passwd = jr["data"]["password"];
json data = jr["data"]["services"]["evpuller"];
urlIn = "rtsp://" + user + ":" + passwd + "@"+ ipc + "/h264/ch1/sub/av_stream";
urlPub = string("tcp://") +data["addr"].get<string>() + ":" + to_string(data["port-pub"]);
}
catch(exception &e) {
bcnt = true;
spdlog::error(e.what());
}
if(bcnt) {
this_thread::sleep_for(chrono::milliseconds(1000*10));
continue;
}
inited = true;
}
return 0;
}
int setupMq() int setupMq()
{ {
teardownMq(); teardownMq();
pPubContext = zmq_ctx_new(); pPubCtx = zmq_ctx_new();
pPublisher = zmq_socket(pPubContext, ZMQ_PUB); pPub = zmq_socket(pPubCtx, ZMQ_PUB);
int rc = zmq_bind(pPublisher, "tcp://0.0.0.0:5556"); int rc = zmq_bind(pPub, urlPub.c_str());
if(rc != 0) { if(rc != 0) {
spdlog::error("failed create pub"); spdlog::error("failed create pub: {}, {}", zmq_strerror(rc), urlPub.c_str());
this_thread::sleep_for(chrono::milliseconds(100*10));
return -1;
} }
return 0; return 0;
...@@ -139,11 +183,11 @@ protected: ...@@ -139,11 +183,11 @@ protected:
int teardownMq() int teardownMq()
{ {
if(pPublisher != NULL) { if(pPub != NULL) {
zmq_close(pPublisher); zmq_close(pPub);
} }
if(pPubContext != NULL) { if(pPubCtx != NULL) {
zmq_ctx_destroy(pPubContext); zmq_ctx_destroy(pPubCtx);
} }
return 0; return 0;
} }
...@@ -151,103 +195,13 @@ protected: ...@@ -151,103 +195,13 @@ protected:
class EdgeVideoMgr {
private:
#define SECS_SLICE (60*5/2)
AVFormatContext *pAVFormatInput = NULL, *pAVFormatRemux = NULL;
AVCodec *pCodec = NULL;
AVDictionary *pOptsRemux = NULL, *pOptsInput = NULL, *pOptsOutput = NULL;
int idxVideo = -1, idxAudio = -1, numStreams = 0, numSlices = 6, secsSlice = SECS_SLICE;
int *streamList = NULL;
bool bPush = true, bRecord = false;
string urlIn, urlOut, pathSlice;
unordered_map<string, string> envParams = unordered_map<string, string>();
// mq
void *pRepContext = NULL; // for msg from edge gateway
void *pReqContext = NULL; // for msg to edge gateway
private:
void setupParams()
{
char *tmp = getenv("URL_IN");
urlIn = (tmp == NULL?string(""): string(tmp));
tmp= getenv("URL_OUT");
urlOut = (tmp == NULL?string(""): string(tmp));
tmp = getenv("SLICE_NUM");
numSlices = (tmp == NULL?6:atoi(tmp));
if(numSlices <=2) {
numSlices = 6;
}
spdlog::info("in: {}", urlIn);
tmp = getenv("SLICE_PATH");
pathSlice = (tmp == NULL?string("slices"):string(tmp));
// OSX XCode doesn't ship with the filesystem header as of version 10.x
#ifdef __LINUX___
if (!fs::exists(pathSlice.c_str())) {
if (!fs::create_directory(pathSlice.c_str())) {
spdlog::error("can't create directory: {}", pathSlice.c_str());
exit(1);
}
fs::permissions(pathSlice.c_str(), fs::perms::all);
}
#endif
tmp = getenv("PUSH");
bPush = (tmp == NULL?false: (string(tmp) == string("false")?false:true));
tmp = getenv("SLICE_SECS");
secsSlice = (tmp == NULL?SECS_SLICE:atoi(tmp));
if(secsSlice < SECS_SLICE) {
secsSlice = SECS_SLICE;
}
if(urlIn == "" or urlOut == "") {
spdlog::error("no input/output url");
exit(1);
}
}
int setupStreams()
{
int ret = 0;
PacketProducer packetProducer(urlIn);
packetProducer.join();
// std::this_thread::sleep_for(std::chrono::milliseconds(30000));
// packetProducer.stop();
return ret;
}
public:
// ctor
EdgeVideoMgr()
{
setupParams();
setupStreams();
}
// dtor
~EdgeVideoMgr()
{
avformat_close_input(&pAVFormatInput);
/* close output */
if (pAVFormatRemux && !(pAVFormatRemux->oformat->flags & AVFMT_NOFILE))
avio_closep(&pAVFormatRemux->pb);
avformat_free_context(pAVFormatRemux);
av_freep(&streamList);
}
};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
av_log_set_level(AV_LOG_INFO); av_log_set_level(AV_LOG_INFO);
spdlog::set_level(spdlog::level::info); spdlog::set_level(spdlog::level::info);
DB::exec(NULL, NULL, NULL ,NULL); DB::exec(NULL, NULL, NULL,NULL);
spdlog::info("hello"); auto evp = EvPuller();
auto vp = EdgeVideoMgr(); evp.join();
return 0; return 0;
} }
#ifndef __COMMON_H__ #ifndef __COMMON_H__
#define __COMMON_H__ #define __COMMON_H__
extern "C" { extern "C"
{
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavutil/time.h> #include <libavutil/time.h>
} }
...@@ -10,239 +11,300 @@ extern "C" { ...@@ -10,239 +11,300 @@ extern "C" {
using json = nlohmann::json; using json = nlohmann::json;
#undef av_err2str #undef av_err2str
#define av_err2str(errnum) av_make_error_string((char*)__builtin_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum) #define av_err2str(errnum) av_make_error_string((char *)__builtin_alloca(AV_ERROR_MAX_STRING_SIZE), AV_ERROR_MAX_STRING_SIZE, errnum)
#define PS_MARK_E "DEADBEEF" #define PS_MARK_E "DEADBEEF"
#define PS_MARK_S "BEEFDEAD" #define PS_MARK_S "BEEFDEAD"
void avlogThrow(void * avcl, int lvl, const char *fmt, ...) void avlogThrow(void *avcl, int lvl, const char *fmt, ...)
{ {
(void) avcl; (void)avcl;
(void) lvl; (void)lvl;
va_list args; va_list args;
va_start( args, fmt ); va_start(args, fmt);
av_log(NULL, AV_LOG_FATAL, fmt, args); av_log(NULL, AV_LOG_FATAL, fmt, args);
va_end( args ); va_end(args);
throw fmt; throw fmt;
} }
namespace AVPacketSerializer { // AVPacketSerializer
int encode(AVPacket &pkt, char **bytes) { namespace AVPacketSerializer
int cnt = 0; {
//data int encode(AVPacket &pkt, char **bytes)
int wholeSize = strlen(PS_MARK_S) + sizeof(pkt.size) + pkt.size; {
//side data int cnt = 0;
wholeSize +=sizeof(pkt.side_data_elems); //data
if(pkt.side_data_elems != 0) { int wholeSize = strlen(PS_MARK_S) + sizeof(pkt.size) + pkt.size;
for(int i = 0; i < pkt.side_data_elems; i++) { //side data
wholeSize += pkt.side_data[i].size + sizeof(AVPacketSideData); wholeSize += sizeof(pkt.side_data_elems);
} if (pkt.side_data_elems != 0)
{
for (int i = 0; i < pkt.side_data_elems; i++)
{
wholeSize += pkt.side_data[i].size + sizeof(AVPacketSideData);
} }
}
// 4 + 8: wholeSize + DEADBEAF // 4 + 8: wholeSize + DEADBEAF
wholeSize += sizeof(pkt.pts) * 4 + sizeof(pkt.flags) + sizeof(pkt.stream_index) + sizeof(wholeSize) + strlen(PS_MARK_E); wholeSize += sizeof(pkt.pts) * 4 + sizeof(pkt.flags) + sizeof(pkt.stream_index) + sizeof(wholeSize) + strlen(PS_MARK_E);
*bytes = (char*)malloc(wholeSize); *bytes = (char *)malloc(wholeSize);
memcpy((*bytes)+cnt, PS_MARK_S, strlen(PS_MARK_S));
cnt += strlen(PS_MARK_S);
// data
memcpy((*bytes)+cnt, &(pkt.size), sizeof(pkt.size));
cnt +=sizeof(pkt.size);
memcpy((*bytes )+cnt, pkt.data, pkt.size);
cnt += pkt.size;
//side data
memcpy((*bytes )+cnt, &(pkt.side_data_elems), sizeof(pkt.side_data_elems));
cnt += sizeof(pkt.side_data_elems);
if(pkt.side_data_elems != 0) {
for(int i = 0; i < pkt.side_data_elems; i++) {
memcpy((*bytes )+cnt, &(pkt.side_data[i].size), sizeof(pkt.side_data[i].size));
cnt+=sizeof(pkt.side_data[i].size);
memcpy((*bytes )+cnt, pkt.side_data[i].data, pkt.side_data[i].size);
cnt+=pkt.side_data[i].size;
memcpy((*bytes )+cnt, &(pkt.side_data[i].type), sizeof(pkt.side_data[i].type));
cnt+=sizeof(pkt.side_data[i].type);
}
}
// other properties memcpy((*bytes) + cnt, PS_MARK_S, strlen(PS_MARK_S));
memcpy((*bytes )+cnt, &(pkt.pts), sizeof(pkt.pts)); cnt += strlen(PS_MARK_S);
cnt+=sizeof(pkt.pts); // data
memcpy((*bytes )+cnt, &(pkt.dts), sizeof(pkt.dts)); memcpy((*bytes) + cnt, &(pkt.size), sizeof(pkt.size));
cnt+=8; cnt += sizeof(pkt.size);
memcpy((*bytes )+cnt, &(pkt.pos), sizeof(pkt.pos)); memcpy((*bytes) + cnt, pkt.data, pkt.size);
cnt+=sizeof(pkt.pos); cnt += pkt.size;
memcpy((*bytes )+cnt, &(pkt.duration), sizeof(pkt.duration)); //side data
cnt+=sizeof(pkt.duration); memcpy((*bytes) + cnt, &(pkt.side_data_elems), sizeof(pkt.side_data_elems));
// deprecated cnt += sizeof(pkt.side_data_elems);
//memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration)); if (pkt.side_data_elems != 0)
//cnt+=sizeof(pkt.convergence_duration); {
memcpy((*bytes )+cnt, &(pkt.flags), sizeof(pkt.flags)); for (int i = 0; i < pkt.side_data_elems; i++)
cnt+=sizeof(pkt.flags); {
memcpy((*bytes )+cnt, &(pkt.stream_index), sizeof(pkt.stream_index)); memcpy((*bytes) + cnt, &(pkt.side_data[i].size), sizeof(pkt.side_data[i].size));
cnt+=sizeof(pkt.stream_index); cnt += sizeof(pkt.side_data[i].size);
memcpy((*bytes )+cnt,&wholeSize, sizeof(wholeSize)); memcpy((*bytes) + cnt, pkt.side_data[i].data, pkt.side_data[i].size);
cnt+=sizeof(wholeSize); cnt += pkt.side_data[i].size;
memcpy((*bytes )+cnt, PS_MARK_E, strlen(PS_MARK_E)); memcpy((*bytes) + cnt, &(pkt.side_data[i].type), sizeof(pkt.side_data[i].type));
cnt+=strlen(PS_MARK_E); cnt += sizeof(pkt.side_data[i].type);
assert(cnt == wholeSize); }
spdlog::debug("pkt origin size {:d}, serialized size: {:d}, elems: {:d}", pkt.size, wholeSize, pkt.side_data_elems);
return wholeSize;
} }
int decode(char * bytes, int len, AVPacket *pkt) { // other properties
// allocate packet mem on heap memcpy((*bytes) + cnt, &(pkt.pts), sizeof(pkt.pts));
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket)); cnt += sizeof(pkt.pts);
int ret = 0; memcpy((*bytes) + cnt, &(pkt.dts), sizeof(pkt.dts));
int got = 0; cnt += 8;
if(memcmp(PS_MARK_E, bytes + len - strlen(PS_MARK_E), strlen(PS_MARK_E)) != 0 || memcmp(PS_MARK_S, bytes, strlen(PS_MARK_S))) { memcpy((*bytes) + cnt, &(pkt.pos), sizeof(pkt.pos));
spdlog::error("invalid packet"); cnt += sizeof(pkt.pos);
return -1; memcpy((*bytes) + cnt, &(pkt.duration), sizeof(pkt.duration));
} cnt += sizeof(pkt.duration);
//skip mark_s // deprecated
got += strlen(PS_MARK_S); //memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
memcpy(&(pkt->size), bytes + got, sizeof(pkt->size)); //cnt+=sizeof(pkt.convergence_duration);
got += sizeof(pkt->size); memcpy((*bytes) + cnt, &(pkt.flags), sizeof(pkt.flags));
av_new_packet(pkt, pkt->size); cnt += sizeof(pkt.flags);
memcpy(pkt->data, bytes + got, pkt->size); memcpy((*bytes) + cnt, &(pkt.stream_index), sizeof(pkt.stream_index));
got += pkt->size; cnt += sizeof(pkt.stream_index);
memcpy(&pkt->side_data_elems, bytes + got, sizeof(pkt->side_data_elems)); memcpy((*bytes) + cnt, &wholeSize, sizeof(wholeSize));
got += sizeof(pkt->side_data_elems); cnt += sizeof(wholeSize);
for(int i = 0; i < pkt->side_data_elems; i++) { memcpy((*bytes) + cnt, PS_MARK_E, strlen(PS_MARK_E));
memcpy(&(pkt->side_data[i].size), bytes+got, sizeof(pkt->side_data[i].size)); cnt += strlen(PS_MARK_E);
got += sizeof(pkt->side_data[i].size); assert(cnt == wholeSize);
memcpy(pkt->side_data[i].data,bytes + got ,pkt->side_data[i].size); spdlog::debug("pkt origin size {:d}, serialized size: {:d}, elems: {:d}", pkt.size, wholeSize, pkt.side_data_elems);
got += pkt->side_data[i].size; return wholeSize;
memcpy(&(pkt->side_data[i].type), bytes + got, sizeof(pkt->side_data[i].type)); }
got += sizeof(pkt->side_data[i].type);
}
// props int decode(char *bytes, int len, AVPacket *pkt)
memcpy(&(pkt->pts), bytes + got, sizeof(pkt->pts)); {
got += sizeof(pkt->pts); // allocate packet mem on heap
memcpy(&(pkt->dts), bytes + got, sizeof(pkt->dts)); //AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
got += sizeof(pkt->dts); int ret = 0;
memcpy(&(pkt->pos), bytes + got, sizeof(pkt->pos)); int got = 0;
got += sizeof(pkt->pos); if (memcmp(PS_MARK_E, bytes + len - strlen(PS_MARK_E), strlen(PS_MARK_E)) != 0 || memcmp(PS_MARK_S, bytes, strlen(PS_MARK_S)))
memcpy(&(pkt->duration), bytes + got, sizeof(pkt->duration)); {
got += sizeof(pkt->duration); spdlog::error("invalid packet");
// deprecated return -1;
//memcpy(&(pkt->convergence_duration), bytes + got, sizeof(pkt->convergence_duration)); }
//got += sizeof(pkt->convergence_duration); //skip mark_s
memcpy(&(pkt->flags), bytes + got, sizeof(pkt->flags)); got += strlen(PS_MARK_S);
got += sizeof(pkt->flags); memcpy(&(pkt->size), bytes + got, sizeof(pkt->size));
memcpy(&(pkt->stream_index), bytes + got, sizeof(pkt->stream_index)); got += sizeof(pkt->size);
got += sizeof(pkt->stream_index); av_new_packet(pkt, pkt->size);
memcpy(pkt->data, bytes + got, pkt->size);
int wholeSize = 0; got += pkt->size;
memcpy(&wholeSize, bytes + got, sizeof(wholeSize)); memcpy(&pkt->side_data_elems, bytes + got, sizeof(pkt->side_data_elems));
got += sizeof(wholeSize); got += sizeof(pkt->side_data_elems);
got += 8; for (int i = 0; i < pkt->side_data_elems; i++)
spdlog::debug("wholeSize: {:d}, {:d}", wholeSize, got); {
memcpy(&(pkt->side_data[i].size), bytes + got, sizeof(pkt->side_data[i].size));
return ret; got += sizeof(pkt->side_data[i].size);
memcpy(pkt->side_data[i].data, bytes + got, pkt->side_data[i].size);
got += pkt->side_data[i].size;
memcpy(&(pkt->side_data[i].type), bytes + got, sizeof(pkt->side_data[i].type));
got += sizeof(pkt->side_data[i].type);
} }
// props
memcpy(&(pkt->pts), bytes + got, sizeof(pkt->pts));
got += sizeof(pkt->pts);
memcpy(&(pkt->dts), bytes + got, sizeof(pkt->dts));
got += sizeof(pkt->dts);
memcpy(&(pkt->pos), bytes + got, sizeof(pkt->pos));
got += sizeof(pkt->pos);
memcpy(&(pkt->duration), bytes + got, sizeof(pkt->duration));
got += sizeof(pkt->duration);
// deprecated
//memcpy(&(pkt->convergence_duration), bytes + got, sizeof(pkt->convergence_duration));
//got += sizeof(pkt->convergence_duration);
memcpy(&(pkt->flags), bytes + got, sizeof(pkt->flags));
got += sizeof(pkt->flags);
memcpy(&(pkt->stream_index), bytes + got, sizeof(pkt->stream_index));
got += sizeof(pkt->stream_index);
int wholeSize = 0;
memcpy(&wholeSize, bytes + got, sizeof(wholeSize));
got += sizeof(wholeSize);
got += 8;
spdlog::debug("wholeSize: {:d}, {:d}", wholeSize, got);
return ret;
} }
} // namespace AVPacketSerializer
void mqPacketFree(void *data, void*hint) { void mqPacketFree(void *data, void *hint)
{
free(data); free(data);
} }
namespace AVFormatCtxSerializer { // AVFormatCtxSerializer
int encode(AVFormatContext *ctx, char **bytes) { namespace AVFormatCtxSerializer
int ret = 0; {
int wholeSize = 0; int encode(AVFormatContext *ctx, char **bytes)
int got = 0; {
// calc total size int ret = 0;
wholeSize += strlen(PS_MARK_S); int wholeSize = 0;
// num streams int got = 0;
wholeSize += sizeof(ctx->nb_streams); // calc total size
wholeSize += sizeof(ctx->nb_streams); wholeSize += strlen(PS_MARK_S);
for(int i = 0; i < ctx->nb_streams; i++) { // num streams
wholeSize += sizeof(AVStream); wholeSize += sizeof(ctx->nb_streams);
wholeSize += sizeof(AVCodecParameters); wholeSize += sizeof(ctx->nb_streams);
} for (int i = 0; i < ctx->nb_streams; i++)
wholeSize += sizeof(wholeSize); {
wholeSize += strlen(PS_MARK_E); wholeSize += sizeof(AVStream);
wholeSize += sizeof(AVCodecParameters);
// alloc memory }
*bytes = (char *) malloc(wholeSize); wholeSize += sizeof(wholeSize);
// populate wholeSize += strlen(PS_MARK_E);
memcpy((*bytes) + got, PS_MARK_S, strlen(PS_MARK_S));
got += strlen(PS_MARK_S);
memcpy((*bytes) + got, (void*)&(ctx->nb_streams), sizeof(ctx->nb_streams));
got += sizeof(ctx->nb_streams);
for(int i = 0; i < ctx->nb_streams; i++) {
//
memcpy((*bytes) + got, ctx->streams[i], sizeof(AVStream));
got += sizeof(AVStream);
//
memcpy((*bytes) + got, ctx->streams[i]->codecpar, sizeof(AVCodecParameters));
got += sizeof(AVCodecParameters);
}
memcpy((*bytes) + got, &wholeSize, sizeof(wholeSize));
got += sizeof(wholeSize);
memcpy((*bytes) + got, PS_MARK_E, strlen(PS_MARK_E));
return wholeSize; // alloc memory
*bytes = (char *)malloc(wholeSize);
// populate
memcpy((*bytes) + got, PS_MARK_S, strlen(PS_MARK_S));
got += strlen(PS_MARK_S);
memcpy((*bytes) + got, (void *)&(ctx->nb_streams), sizeof(ctx->nb_streams));
got += sizeof(ctx->nb_streams);
for (int i = 0; i < ctx->nb_streams; i++)
{
//
memcpy((*bytes) + got, ctx->streams[i], sizeof(AVStream));
got += sizeof(AVStream);
//
memcpy((*bytes) + got, ctx->streams[i]->codecpar, sizeof(AVCodecParameters));
got += sizeof(AVCodecParameters);
} }
memcpy((*bytes) + got, &wholeSize, sizeof(wholeSize));
got += sizeof(wholeSize);
memcpy((*bytes) + got, PS_MARK_E, strlen(PS_MARK_E));
int decode(char *bytes, int len, AVFormatContext *pCtx) { return wholeSize;
int ret = 0; }
int got = 0;
if(memcmp(PS_MARK_S, bytes+got, strlen(PS_MARK_S)) !=0 && memcmp(PS_MARK_E, bytes+len-strlen(PS_MARK_E), strlen(PS_MARK_E)) != 0) {
spdlog::error("invalid packet");
return -1;
}
got+=strlen(PS_MARK_S);
memcpy(&ret, bytes+got, sizeof(ret));
got +=sizeof(ret);
pCtx->streams = (AVStream **)malloc(sizeof(AVStream *) * ret);
pCtx->nb_streams = ret;
for(int i = 0; i < ret; i++) {
pCtx->streams[i] = (AVStream*)malloc(sizeof(AVStream));
memcpy(pCtx->streams[i], bytes+got, sizeof(AVStream));
got+=sizeof(AVStream);
pCtx->streams[i]->codecpar = (AVCodecParameters*)malloc(sizeof(AVCodecParameters));
memcpy(pCtx->streams[i]->codecpar, bytes+got, sizeof(AVCodecParameters));
got +=sizeof(AVCodecParameters);
}
memcpy(&ret, bytes+got, sizeof(ret)); int decode(char *bytes, int len, AVFormatContext *pCtx)
assert(ret == len); {
return ret; int ret = 0;
int got = 0;
if (memcmp(PS_MARK_S, bytes + got, strlen(PS_MARK_S)) != 0 && memcmp(PS_MARK_E, bytes + len - strlen(PS_MARK_E), strlen(PS_MARK_E)) != 0)
{
spdlog::error("invalid packet");
return -1;
}
got += strlen(PS_MARK_S);
memcpy(&ret, bytes + got, sizeof(ret));
got += sizeof(ret);
pCtx->streams = (AVStream **)malloc(sizeof(AVStream *) * ret);
pCtx->nb_streams = ret;
for (int i = 0; i < ret; i++)
{
pCtx->streams[i] = (AVStream *)malloc(sizeof(AVStream));
memcpy(pCtx->streams[i], bytes + got, sizeof(AVStream));
got += sizeof(AVStream);
pCtx->streams[i]->codecpar = (AVCodecParameters *)malloc(sizeof(AVCodecParameters));
memcpy(pCtx->streams[i]->codecpar, bytes + got, sizeof(AVCodecParameters));
got += sizeof(AVCodecParameters);
} }
void freeCtx(AVFormatContext *pCtx) { memcpy(&ret, bytes + got, sizeof(ret));
for(int i = 0; i < pCtx->nb_streams; i++) { assert(ret == len);
free(pCtx->streams[i]->codecpar); return ret;
free(pCtx->streams[i]); }
}
free(pCtx->streams); void freeCtx(AVFormatContext *pCtx)
{
for (int i = 0; i < pCtx->nb_streams; i++)
{
free(pCtx->streams[i]->codecpar);
free(pCtx->streams[i]);
} }
free(pCtx->streams);
} }
} // namespace AVFormatCtxSerializer
namespace cloudutils { // cloudutils
json registry(const char *gn, const char *scn, int iid) { namespace cloudutils
json jret; {
// find local info in db /**
// request cloud info * scn:
* evpuller, evmgr, evmotion, evslicer
// moc *
jret["iid"] = "MOCK_IID"; * */
jret["scn"] = string(scn); /*
jret["ipc"] = "172.31.0.51"; {
jret["gn"] = "MOCK_GN"; "code":0,
jret["ga"] = "localhost"; "time":0,
if(scn != NULL && strcmp(scn, "evmgr")) { "data":{
jret["evmgr"] = "tcp://localhost:5556"; "ipc":"192.168.0.23",
}else{ "username":"admin",
jret["evmgr"] = "tcp://0.0.0.0:5556"; "password":"FWBWTU",
} "services":{
jret["code"] = 0; "evmgr":{
"sn":"ILS-1",
"addr":"0.0.0.0",
"port-pub":5556,
"port-rep":5557,
"iid":1
},
"evpuller":{
"sn":"ILS-2",
"addr":"0.0.0.0",
"port-pub":5556,
"port-rep":5557,
"iid":2
},
"evslicer":[
{
"sn":"ILS-3",
"addr":"192.168.0.25",
"iid":3
}
],
"evml":[
{
"feature":"motion",
"sn":"ILS-4",
"addr":"192.168.0.26",
"iid":4
}
]
}
}
}
*/
const char *config = "{\"code\":0,\"time\":0,\"data\":{\"ipc\":\"192.168.0.23\",\"username\":\"admin\",\"password\":\"FWBWTU\",\"services\":{\"evmgr\":{\"sn\":\"ILS-1\",\"addr\":\"0.0.0.0\",\"port-pub\":5556,\"port-rep\":5557,\"iid\":1},\"evpuller\":{\"sn\":\"ILS-2\",\"addr\":\"0.0.0.0\",\"port-pub\":5556,\"port-rep\":5557,\"iid\":2},\"evslicer\":[{\"sn\":\"ILS-3\",\"addr\":\"192.168.0.25\",\"iid\":3}],\"evml\":[{\"feature\":\"motion\",\"sn\":\"ILS-4\",\"addr\":\"192.168.0.26\",\"iid\":4}]}}}";
return jret; json registry(const char *sn, const char *scn, int iid)
} {
// find local info in db
// request cloud info
// moc
json ret = json::parse(config);
return ret;
} }
} // namespace cloudutils
#endif #endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论