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

init

上级 f6a306e0
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/Cellar/opencv/4.1.0_2/include/opencv4",
"/usr/local/Cellar/ffmpeg/4.1.4_1/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/local/Cellar/"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}
\ No newline at end of file
{
"C_Cpp.intelliSenseEngineFallback": "Enabled",
"python.pythonPath": "/opt/apps/conda/anaconda3/bin/python"
}
\ No newline at end of file
{
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
}
}
],
"version": "2.0.0"
}
\ No newline at end of file
#pragma GCC diagnostic ignored "-Wunused-private-field" #pragma GCC diagnostic ignored "-Wunused-private-field"
#pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunused-variable"
extern "C" {
#include <libavformat/avformat.h>
}
#include <libavutil/timestamp.h>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
#include <thread> #include <thread>
...@@ -17,208 +14,38 @@ extern "C" { ...@@ -17,208 +14,38 @@ extern "C" {
namespace fs = std::filesystem; namespace fs = std::filesystem;
#endif #endif
#include "vendor/include/zmq.h"
#include "inc/json.hpp" #include "inc/json.hpp"
#include "inc/blockingconcurrentqueue.hpp" #include "inc/blockingconcurrentqueue.hpp"
#include "vendor/include/zmq.h" #include "inc/tinythread.hpp"
#include "inc/common.hpp"
using namespace std; using namespace std;
using json = nlohmann::json; using json = nlohmann::json;
using namespace moodycamel; using namespace moodycamel;
class PacketPusher: public TinyThread {
class Stoppable private:
{ void *pPubContext = NULL; // for packets publishing
std::promise<void> exitSignal; void *pPublisher = NULL;
std::future<void> futureObj; AVFormatContext *pAVFormatInput = NULL, *pAVFormatRemux = NULL;
int state = 0; string urlIn;
thread th; int *streamList = NULL, numStreams = 0;
protected:
// Task need to provide defination for this function
// It will be called by thread function
virtual void run() = 0;
public: public:
Stoppable() : PacketPusher(string urlIn):urlIn(urlIn){
futureObj(exitSignal.get_future())
{ setupMq();
}
Stoppable(Stoppable && obj) : exitSignal(std::move(obj.exitSignal)), futureObj(std::move(obj.futureObj))
{
std::cout << "Move Constructor is called" << std::endl;
}
Stoppable & operator=(Stoppable && obj)
{
std::cout << "Move Assignment is called" << std::endl;
exitSignal = std::move(obj.exitSignal);
futureObj = std::move(obj.futureObj);
return *this;
}
// Thread function to be executed by thread
private:
void _run()
{
if(state == 0) {
th =thread([&](){
this->run();
});
state = 1;
}
}
public:
//Checks if thread is requested to stop
bool checkStop()
{
// checks if value in future object is available
if (futureObj.wait_for(std::chrono::milliseconds(0)) == std::future_status::timeout)
return false;
return true;
}
// Request the thread to stop by setting value in promise object
void stop()
{
exitSignal.set_value();
}
void join() {
_run();
if(th.joinable()){
th.join();
}
} }
void detach() { ~PacketPusher(){
_run();
if(th.joinable()){
th.detach();
}
} }
};
class MyTask: public Stoppable
{
protected: protected:
// Function to be executed by thread function // Function to be executed by thread function
void run() void run()
{
std::cout << "Task Start" << std::endl;
// Check if thread is requested to stop ?
while (checkStop() == false)
{
std::cout << "Doing Some Work" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << "Task End" << std::endl;
}
};
class VideoProcessor {
private:
#define SECS_SLICE (60*5/2)
AVFormatContext *pAVFormatInput = NULL, *pAVFormatRemux = NULL, *pAVFormatSlice = 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 *pPubContext = NULL; // for packets publishing
void *pPublisher = NULL;
void *pRepContext = NULL; // for msg from edge gateway
void *pReqContext = NULL; // for msg to edge gateway
private:
void logThrow(void * avcl, int lvl, const char *fmt, ...) {
(void) avcl;
(void) lvl;
va_list args;
va_start( args, fmt );
av_log(NULL, AV_LOG_FATAL, fmt, args);
va_end( args );
throw fmt;
}
void setupParams()
{ {
char *tmp = getenv("URL_IN"); int ret = 0;
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;
}
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())) {
logThrow(NULL, AV_LOG_FATAL, "can't create directory: %s", 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 == "") {
logThrow(NULL, AV_LOG_FATAL, "no input/output url");
exit(1);
}
}
int teardownMq() {
if(pPublisher != NULL) {
zmq_close(pPublisher);
}
if(pPubContext != NULL) {
zmq_ctx_destroy(pPubContext);
}
return 0;
}
int setupMq(){
teardownMq();
pPubContext = zmq_ctx_new();
pPublisher = zmq_socket(pPubContext, ZMQ_PUB);
int rc = zmq_bind(pPublisher, "tcp://*:5556");
if(rc != 0) {
logThrow(NULL, AV_LOG_FATAL, "failed create pub");
}
MyTask task;
task.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
task.stop();
std::this_thread::sleep_for(std::chrono::milliseconds(9993000));
return 0;
}
int setupStreams()
{
int ret = 0, i, streamIdx = 0;
if ((ret = avformat_open_input(&pAVFormatInput, urlIn.c_str(), NULL, NULL)) < 0) { if ((ret = avformat_open_input(&pAVFormatInput, urlIn.c_str(), NULL, NULL)) < 0) {
logThrow(NULL, AV_LOG_FATAL, "Could not open input file '%s'", urlIn.c_str()); logThrow(NULL, AV_LOG_FATAL, "Could not open input file '%s'", urlIn.c_str());
} }
...@@ -228,13 +55,8 @@ private: ...@@ -228,13 +55,8 @@ private:
pAVFormatInput->flags = AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS; pAVFormatInput->flags = AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS;
ret = avformat_alloc_output_context2(&pAVFormatRemux, NULL, "rtsp", urlOut.c_str());
if (ret < 0) {
logThrow(NULL, AV_LOG_FATAL, "failed create avformatcontext for output: %s", av_err2str(ret));
}
numStreams = pAVFormatInput->nb_streams; numStreams = pAVFormatInput->nb_streams;
streamList = (int *)av_mallocz_array(numStreams, sizeof(*streamList)); int *streamList = (int *)av_mallocz_array(numStreams, sizeof(*streamList));
if (!streamList) { if (!streamList) {
ret = AVERROR(ENOMEM); ret = AVERROR(ENOMEM);
...@@ -242,7 +64,8 @@ private: ...@@ -242,7 +64,8 @@ private:
} }
// find all video & audio streams for remuxing // find all video & audio streams for remuxing
for (i = 0; i < pAVFormatInput->nb_streams; i++) { int i = 0, streamIdx = 0;
for (; i < pAVFormatInput->nb_streams; i++) {
AVStream *out_stream; AVStream *out_stream;
AVStream *in_stream = pAVFormatInput->streams[i]; AVStream *in_stream = pAVFormatInput->streams[i];
AVCodecParameters *in_codecpar = in_stream->codecpar; AVCodecParameters *in_codecpar = in_stream->codecpar;
...@@ -265,33 +88,7 @@ private: ...@@ -265,33 +88,7 @@ private:
} }
} }
av_dump_format(pAVFormatRemux, 0, urlOut.c_str(), 1); while (checkStop() == false) {
// find best video stream
idxVideo = av_find_best_stream(pAVFormatInput, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0);
if(idxVideo < 0) {
logThrow(NULL, AV_LOG_FATAL, "failed find best video stream");
}
if (!(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) {
logThrow(NULL, AV_LOG_FATAL, "Failed allocating output stream\n");
ret = avio_open2(&pAVFormatRemux->pb, urlOut.c_str(), AVIO_FLAG_WRITE, NULL, &pOptsRemux);
if (ret < 0) {
logThrow(NULL, AV_LOG_FATAL, "Could not open output file '%s'", urlOut.c_str());
}
}
// rtsp tcp
if(av_dict_set(&pOptsRemux, "rtsp_transport", "tcp", 0) < 0) {
logThrow(NULL, AV_LOG_FATAL, "failed set output pOptsRemux");
ret = AVERROR_UNKNOWN;
}
ret = avformat_write_header(pAVFormatRemux, &pOptsRemux);
if (ret < 0) {
logThrow(NULL, AV_LOG_FATAL, "Error occurred when opening output file\n");
}
while (1) {
AVStream *in_stream, *out_stream; AVStream *in_stream, *out_stream;
AVPacket packet; AVPacket packet;
ret = av_read_frame(pAVFormatInput, &packet); ret = av_read_frame(pAVFormatInput, &packet);
...@@ -319,6 +116,211 @@ private: ...@@ -319,6 +116,211 @@ private:
} }
av_write_trailer(pAVFormatRemux); av_write_trailer(pAVFormatRemux);
std::cout << "Pusher Start" << std::endl;
// Check if thread is requested to stop ?
while (checkStop() == false) {
std::cout << "Doing Some Work" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
std::cout << "Task End" << std::endl;
}
int setupMq()
{
teardownMq();
pPubContext = zmq_ctx_new();
pPublisher = zmq_socket(pPubContext, ZMQ_PUB);
int rc = zmq_bind(pPublisher, "tcp://*:5556");
if(rc != 0) {
logThrow(NULL, AV_LOG_FATAL, "failed create pub");
}
return 0;
}
int teardownMq()
{
if(pPublisher != NULL) {
zmq_close(pPublisher);
}
if(pPubContext != NULL) {
zmq_ctx_destroy(pPubContext);
}
return 0;
}
};
class VideoProcessor {
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;
}
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())) {
logThrow(NULL, AV_LOG_FATAL, "can't create directory: %s", 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 == "") {
logThrow(NULL, AV_LOG_FATAL, "no input/output url");
exit(1);
}
}
int setupStreams()
{
PacketPusher pusher(urlIn);
pusher.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
pusher.stop();
std::this_thread::sleep_for(std::chrono::milliseconds(9993000));
int ret = 0, i, streamIdx = 0;
// if ((ret = avformat_open_input(&pAVFormatInput, urlIn.c_str(), NULL, NULL)) < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Could not open input file '%s'", urlIn.c_str());
// }
// if ((ret = avformat_find_stream_info(pAVFormatInput, NULL)) < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Failed to retrieve input stream information");
// }
// pAVFormatInput->flags = AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS;
// ret = avformat_alloc_output_context2(&pAVFormatRemux, NULL, "rtsp", urlOut.c_str());
// if (ret < 0) {
// logThrow(NULL, AV_LOG_FATAL, "failed create avformatcontext for output: %s", av_err2str(ret));
// }
// numStreams = pAVFormatInput->nb_streams;
// streamList = (int *)av_mallocz_array(numStreams, sizeof(*streamList));
// if (!streamList) {
// ret = AVERROR(ENOMEM);
// logThrow(NULL, AV_LOG_FATAL, "failed create avformatcontext for output: %s", av_err2str(AVERROR(ENOMEM)));
// }
// // find all video & audio streams for remuxing
// for (i = 0; i < pAVFormatInput->nb_streams; i++) {
// AVStream *out_stream;
// AVStream *in_stream = pAVFormatInput->streams[i];
// AVCodecParameters *in_codecpar = in_stream->codecpar;
// if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
// in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
// in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
// streamList[i] = -1;
// continue;
// }
// streamList[i] = streamIdx++;
// out_stream = avformat_new_stream(pAVFormatRemux, NULL);
// if (!out_stream) {
// logThrow(NULL, AV_LOG_FATAL, "Failed allocating output stream\n");
// ret = AVERROR_UNKNOWN;
// }
// ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Failed to copy codec parameters\n");
// }
// }
// av_dump_format(pAVFormatRemux, 0, urlOut.c_str(), 1);
// // find best video stream
// idxVideo = av_find_best_stream(pAVFormatInput, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0);
// if(idxVideo < 0) {
// logThrow(NULL, AV_LOG_FATAL, "failed find best video stream");
// }
// if (!(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) {
// logThrow(NULL, AV_LOG_FATAL, "Failed allocating output stream\n");
// ret = avio_open2(&pAVFormatRemux->pb, urlOut.c_str(), AVIO_FLAG_WRITE, NULL, &pOptsRemux);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Could not open output file '%s'", urlOut.c_str());
// }
// }
// // rtsp tcp
// if(av_dict_set(&pOptsRemux, "rtsp_transport", "tcp", 0) < 0) {
// logThrow(NULL, AV_LOG_FATAL, "failed set output pOptsRemux");
// ret = AVERROR_UNKNOWN;
// }
// ret = avformat_write_header(pAVFormatRemux, &pOptsRemux);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Error occurred when opening output file\n");
// }
// while (1) {
// AVStream *in_stream, *out_stream;
// AVPacket packet;
// ret = av_read_frame(pAVFormatInput, &packet);
// if (ret < 0)
// break;
// in_stream = pAVFormatInput->streams[packet.stream_index];
// if (packet.stream_index >= numStreams || streamList[packet.stream_index] < 0) {
// av_packet_unref(&packet);
// continue;
// }
// packet.stream_index = streamList[packet.stream_index];
// out_stream = pAVFormatRemux->streams[packet.stream_index];
// /* copy packet */
// packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
// packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
// packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
// packet.pos = -1;
// ret = av_interleaved_write_frame(pAVFormatRemux, &packet);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_FATAL, "Error muxing packet\n");
// break;
// }
// av_packet_unref(&packet);
// }
// av_write_trailer(pAVFormatRemux);
return ret; return ret;
} }
...@@ -327,11 +329,11 @@ public: ...@@ -327,11 +329,11 @@ public:
VideoProcessor() VideoProcessor()
{ {
setupParams(); setupParams();
setupMq();
setupStreams(); setupStreams();
} }
// dtor // dtor
~VideoProcessor() { ~VideoProcessor()
{
avformat_close_input(&pAVFormatInput); avformat_close_input(&pAVFormatInput);
/* close output */ /* close output */
if (pAVFormatRemux && !(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) if (pAVFormatRemux && !(pAVFormatRemux->oformat->flags & AVFMT_NOFILE))
...@@ -344,7 +346,7 @@ public: ...@@ -344,7 +346,7 @@ public:
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
auto vp = VideoProcessor(); auto vp = VideoProcessor();
// AVFormatContext *pAVFormatInput = NULL, *pAVFormatRemux = NULL; // AVFormatContext *pAVFormatInput = NULL, *pAVFormatRemux = NULL;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论