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

init

上级 eee69bf3
...@@ -20,20 +20,28 @@ namespace fs = std::filesystem; ...@@ -20,20 +20,28 @@ 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:
...@@ -41,7 +49,6 @@ protected: ...@@ -41,7 +49,6 @@ protected:
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);
} }
...@@ -109,7 +116,7 @@ protected: ...@@ -109,7 +116,7 @@ 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);
} }
...@@ -117,137 +124,84 @@ protected: ...@@ -117,137 +124,84 @@ protected:
// 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 setupMq() int init()
{ {
teardownMq(); bool inited = false;
pPubContext = zmq_ctx_new();
pPublisher = zmq_socket(pPubContext, ZMQ_PUB); while(!inited) {
// TODO: read db to get sn
int rc = zmq_bind(pPublisher, "tcp://0.0.0.0:5556"); const char* sn = "ILS-2";
if(rc != 0) { // req config
spdlog::error("failed create pub"); 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;
} }
return 0; inited = true;
} }
int teardownMq()
{
if(pPublisher != NULL) {
zmq_close(pPublisher);
}
if(pPubContext != NULL) {
zmq_ctx_destroy(pPubContext);
}
return 0; return 0;
} }
};
int setupMq()
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"); teardownMq();
urlIn = (tmp == NULL?string(""): string(tmp)); pPubCtx = zmq_ctx_new();
pPub = zmq_socket(pPubCtx, ZMQ_PUB);
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"); int rc = zmq_bind(pPub, urlPub.c_str());
secsSlice = (tmp == NULL?SECS_SLICE:atoi(tmp)); if(rc != 0) {
if(secsSlice < SECS_SLICE) { spdlog::error("failed create pub: {}, {}", zmq_strerror(rc), urlPub.c_str());
secsSlice = SECS_SLICE; this_thread::sleep_for(chrono::milliseconds(100*10));
return -1;
} }
if(urlIn == "" or urlOut == "") { return 0;
spdlog::error("no input/output url");
exit(1);
}
} }
int setupStreams() int teardownMq()
{ {
int ret = 0; if(pPub != NULL) {
PacketProducer packetProducer(urlIn); zmq_close(pPub);
packetProducer.join();
// std::this_thread::sleep_for(std::chrono::milliseconds(30000));
// packetProducer.stop();
return ret;
} }
if(pPubCtx != NULL) {
public: zmq_ctx_destroy(pPubCtx);
// ctor
EdgeVideoMgr()
{
setupParams();
setupStreams();
} }
// dtor return 0;
~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;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论