Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
f6a306e0
提交
f6a306e0
authored
8月 07, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
30634e96
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
160 行增加
和
33 行删除
+160
-33
Makefile
opencv-motion-detect/Makefile
+2
-2
rtsp-relay.cpp
opencv-motion-detect/rtsp-relay.cpp
+158
-31
没有找到文件。
opencv-motion-detect/Makefile
浏览文件 @
f6a306e0
CC
=
gcc
CPP
=
g++
CPPFLAGS
=
-g
-Wall
-std
=
g++1z
CPPFLAGS
=
-g
-Wall
-std
=
g
nu
++1z
CFLAGS
=
-g
-Wall
LIBOPENCV
=
`
pkg-config opencv
--cflags
--libs
`
...
...
@@ -14,7 +14,7 @@ libzmq:
cd
vendor/libzmq
&&
[
-f
$(CURDIR)
/vendor/lib/pkgconfig/libzmq.pc
]
||
./autogen.sh
&&
./configure
--prefix
=
$(CURDIR)
/vendor
cd
vendor/libzmq
&&
make
-j
4
&&
make
install
rtspr
:
rtsp-relay.cpp
$(CPP)
$(CPPFLAGS)
-o
rtspr rtsp-relay.cpp
$(LIBFFMPEG)
$
(
pkg-config
--cflags
--libs
vendor/lib/pkgconfig/libzmq.pc
)
$(CPP)
$(CPPFLAGS)
-o
rtspr rtsp-relay.cpp
$(LIBFFMPEG)
`
pkg-config
--cflags
--libs
vendor/lib/pkgconfig/libzmq.pc
`
rtsp
:
rtsp.cpp
$(CPP)
$(CFLAGS)
-o
rtsp rtsp.cpp
$(LIBFFMPEG)
...
...
opencv-motion-detect/rtsp-relay.cpp
浏览文件 @
f6a306e0
...
...
@@ -7,6 +7,10 @@ extern "C" {
#include <libavutil/timestamp.h>
#include <stdlib.h>
#include <string>
#include <thread>
#include <iostream>
#include <chrono>
#include <future>
#ifdef OS_LINUX
#include <filesystem>
...
...
@@ -21,6 +25,98 @@ using namespace std;
using
json
=
nlohmann
::
json
;
using
namespace
moodycamel
;
class
Stoppable
{
std
::
promise
<
void
>
exitSignal
;
std
::
future
<
void
>
futureObj
;
int
state
=
0
;
thread
th
;
protected
:
// Task need to provide defination for this function
// It will be called by thread function
virtual
void
run
()
=
0
;
public
:
Stoppable
()
:
futureObj
(
exitSignal
.
get_future
())
{
}
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
()
{
_run
();
if
(
th
.
joinable
()){
th
.
detach
();
}
}
};
class
MyTask
:
public
Stoppable
{
protected
:
// Function to be executed by thread function
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)
...
...
@@ -32,16 +128,18 @@ private:
bool
bPush
=
true
,
bRecord
=
false
;
string
urlIn
,
urlOut
,
pathSlice
;
unordered_map
<
string
,
string
>
envParams
=
unordered_map
<
string
,
string
>
();
BlockingConcurrentQueue
<
AVPacket
*>
remuxPktQueue
;
BlockingConcurrentQueue
<
AVPacket
*>
slicePktQueue
;
BlockingConcurrentQueue
<
AVPacket
*>
framePktQueue
;
// 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_
ERROR
,
fmt
,
args
);
av_log
(
NULL
,
AV_LOG_
FATAL
,
fmt
,
args
);
va_end
(
args
);
throw
fmt
;
}
...
...
@@ -67,14 +165,13 @@ private:
#ifdef __LINUX___
if
(
!
fs
::
exists
(
pathSlice
.
c_str
()))
{
if
(
!
fs
::
create_directory
(
pathSlice
.
c_str
()))
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"can't create directory: %s"
,
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
));
...
...
@@ -85,26 +182,55 @@ private:
}
if
(
urlIn
==
""
or
urlOut
==
""
)
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"no input/output url"
);
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
)
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"Could not open input file '%s'"
,
urlIn
.
c_str
());
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_
ERROR
,
"Failed to retrieve input stream information"
);
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_
ERROR
,
"failed create avformatcontext for output: %s"
,
av_err2str
(
ret
));
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"failed create avformatcontext for output: %s"
,
av_err2str
(
ret
));
}
numStreams
=
pAVFormatInput
->
nb_streams
;
...
...
@@ -112,7 +238,7 @@ private:
if
(
!
streamList
)
{
ret
=
AVERROR
(
ENOMEM
);
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"failed create avformatcontext for output: %s"
,
av_err2str
(
AVERROR
(
ENOMEM
)));
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"failed create avformatcontext for output: %s"
,
av_err2str
(
AVERROR
(
ENOMEM
)));
}
// find all video & audio streams for remuxing
...
...
@@ -129,13 +255,13 @@ private:
streamList
[
i
]
=
streamIdx
++
;
out_stream
=
avformat_new_stream
(
pAVFormatRemux
,
NULL
);
if
(
!
out_stream
)
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"Failed allocating output stream
\n
"
);
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_
ERROR
,
"Failed to copy codec parameters
\n
"
);
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"Failed to copy codec parameters
\n
"
);
}
}
...
...
@@ -144,26 +270,26 @@ private:
// find best video stream
idxVideo
=
av_find_best_stream
(
pAVFormatInput
,
AVMEDIA_TYPE_VIDEO
,
-
1
,
-
1
,
&
pCodec
,
0
);
if
(
idxVideo
<
0
)
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"failed find best video stream"
);
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"failed find best video stream"
);
}
if
(
!
(
pAVFormatRemux
->
oformat
->
flags
&
AVFMT_NOFILE
))
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"Failed allocating output stream
\n
"
);
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_
ERROR
,
"Could not open output file '%s'"
,
urlOut
.
c_str
());
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_
ERROR
,
"failed set output pOptsRemux"
);
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_
ERROR
,
"Error occurred when opening output file
\n
"
);
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"Error occurred when opening output file
\n
"
);
}
while
(
1
)
{
AVStream
*
in_stream
,
*
out_stream
;
...
...
@@ -186,7 +312,7 @@ private:
ret
=
av_interleaved_write_frame
(
pAVFormatRemux
,
&
packet
);
if
(
ret
<
0
)
{
logThrow
(
NULL
,
AV_LOG_
ERROR
,
"Error muxing packet
\n
"
);
logThrow
(
NULL
,
AV_LOG_
FATAL
,
"Error muxing packet
\n
"
);
break
;
}
av_packet_unref
(
&
packet
);
...
...
@@ -201,6 +327,7 @@ public:
VideoProcessor
()
{
setupParams
();
setupMq
();
setupStreams
();
}
// dtor
...
...
@@ -241,17 +368,17 @@ int main(int argc, char **argv)
// urlOutput = argv[2];
// if ((ret = avformat_open_input(&pAVFormatInput, urlInput, NULL, NULL)) < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Could not open input file '%s'", urlInput);
// logThrow(NULL, AV_LOG_
FATAL
, "Could not open input file '%s'", urlInput);
// goto end;
// }
// if ((ret = avformat_find_stream_info(pAVFormatInput, NULL)) < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Failed to retrieve input stream information");
// logThrow(NULL, AV_LOG_
FATAL
, "Failed to retrieve input stream information");
// goto end;
// }
// ret = avformat_alloc_output_context2(&pAVFormatRemux, NULL, "rtsp", urlOutput);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "failed create avformatcontext for output: %s", av_err2str(ret));
// logThrow(NULL, AV_LOG_
FATAL
, "failed create avformatcontext for output: %s", av_err2str(ret));
// goto end;
// }
...
...
@@ -277,13 +404,13 @@ int main(int argc, char **argv)
// streamList[i] = streamIdx++;
// out_stream = avformat_new_stream(pAVFormatRemux, NULL);
// if (!out_stream) {
// logThrow(NULL, AV_LOG_
ERROR
, "Failed allocating output stream\n");
// logThrow(NULL, AV_LOG_
FATAL
, "Failed allocating output stream\n");
// ret = AVERROR_UNKNOWN;
// goto end;
// }
// ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Failed to copy codec parameters\n");
// logThrow(NULL, AV_LOG_
FATAL
, "Failed to copy codec parameters\n");
// goto end;
// }
// }
...
...
@@ -293,7 +420,7 @@ int main(int argc, char **argv)
// // find best video stream
// idxVideo = av_find_best_stream(pAVFormatInput, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0);
// if(idxVideo < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "failed find best video stream");
// logThrow(NULL, AV_LOG_
FATAL
, "failed find best video stream");
// goto end;
// }
...
...
@@ -303,10 +430,10 @@ int main(int argc, char **argv)
// // wherever you want.
// if (!(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) {
// logThrow(NULL, AV_LOG_
ERROR
, "Failed allocating output stream\n");
// logThrow(NULL, AV_LOG_
FATAL
, "Failed allocating output stream\n");
// ret = avio_open2(&pAVFormatRemux->pb, urlOutput, AVIO_FLAG_WRITE, NULL, &pOptsRemux);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Could not open output file '%s'", urlOutput);
// logThrow(NULL, AV_LOG_
FATAL
, "Could not open output file '%s'", urlOutput);
// goto end;
// }
// }
...
...
@@ -317,14 +444,14 @@ int main(int argc, char **argv)
// // rtsp tcp
// if(av_dict_set(&pOptsRemux, "rtsp_transport", "tcp", 0) < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "failed set output pOptsRemux");
// logThrow(NULL, AV_LOG_
FATAL
, "failed set output pOptsRemux");
// ret = AVERROR_UNKNOWN;
// goto end;
// }
// ret = avformat_write_header(pAVFormatRemux, &pOptsRemux);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Error occurred when opening output file\n");
// logThrow(NULL, AV_LOG_
FATAL
, "Error occurred when opening output file\n");
// goto end;
// }
// while (1) {
...
...
@@ -347,7 +474,7 @@ int main(int argc, char **argv)
// ret = av_interleaved_write_frame(pAVFormatRemux, &packet);
// if (ret < 0) {
// logThrow(NULL, AV_LOG_
ERROR
, "Error muxing packet\n");
// logThrow(NULL, AV_LOG_
FATAL
, "Error muxing packet\n");
// break;
// }
// av_packet_unref(&packet);
...
...
@@ -362,7 +489,7 @@ int main(int argc, char **argv)
// avformat_free_context(pAVFormatRemux);
// av_freep(&streamList);
// if (ret < 0 && ret != AVERROR_EOF) {
// logThrow(NULL, AV_LOG_
ERROR
, "Error occurred: %s\n", av_err2str(ret));
// logThrow(NULL, AV_LOG_
FATAL
, "Error occurred: %s\n", av_err2str(ret));
// return 1;
// }
// return 0;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论