Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
ed3f32a9
提交
ed3f32a9
authored
8月 14, 2019
作者:
blu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
eee69bf3
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
217 行增加
和
201 行删除
+217
-201
evpuller.cpp
opencv-motion-detect/evpuller.cpp
+65
-111
common.hpp
opencv-motion-detect/inc/common.hpp
+152
-90
没有找到文件。
opencv-motion-detect/evpuller.cpp
浏览文件 @
ed3f32a9
...
@@ -20,20 +20,28 @@ namespace fs = std::filesystem;
...
@@ -20,20 +20,28 @@ namespace fs = std::filesystem;
using
namespace
std
;
using
namespace
std
;
class
PacketProduc
er
:
public
TinyThread
{
class
EvPull
er
:
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
(
pPub
lisher
,
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
;
}
}
opencv-motion-detect/inc/common.hpp
浏览文件 @
ed3f32a9
#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,91 +11,100 @@ extern "C" {
...
@@ -10,91 +11,100 @@ 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
encode
(
AVPacket
&
pkt
,
char
**
bytes
)
{
int
cnt
=
0
;
int
cnt
=
0
;
//data
//data
int
wholeSize
=
strlen
(
PS_MARK_S
)
+
sizeof
(
pkt
.
size
)
+
pkt
.
size
;
int
wholeSize
=
strlen
(
PS_MARK_S
)
+
sizeof
(
pkt
.
size
)
+
pkt
.
size
;
//side data
//side data
wholeSize
+=
sizeof
(
pkt
.
side_data_elems
);
wholeSize
+=
sizeof
(
pkt
.
side_data_elems
);
if
(
pkt
.
side_data_elems
!=
0
)
{
if
(
pkt
.
side_data_elems
!=
0
)
for
(
int
i
=
0
;
i
<
pkt
.
side_data_elems
;
i
++
)
{
{
for
(
int
i
=
0
;
i
<
pkt
.
side_data_elems
;
i
++
)
{
wholeSize
+=
pkt
.
side_data
[
i
].
size
+
sizeof
(
AVPacketSideData
);
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
));
memcpy
((
*
bytes
)
+
cnt
,
PS_MARK_S
,
strlen
(
PS_MARK_S
));
cnt
+=
strlen
(
PS_MARK_S
);
cnt
+=
strlen
(
PS_MARK_S
);
// data
// data
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
size
),
sizeof
(
pkt
.
size
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
size
),
sizeof
(
pkt
.
size
));
cnt
+=
sizeof
(
pkt
.
size
);
cnt
+=
sizeof
(
pkt
.
size
);
memcpy
((
*
bytes
)
+
cnt
,
pkt
.
data
,
pkt
.
size
);
memcpy
((
*
bytes
)
+
cnt
,
pkt
.
data
,
pkt
.
size
);
cnt
+=
pkt
.
size
;
cnt
+=
pkt
.
size
;
//side data
//side data
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
side_data_elems
),
sizeof
(
pkt
.
side_data_elems
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
side_data_elems
),
sizeof
(
pkt
.
side_data_elems
));
cnt
+=
sizeof
(
pkt
.
side_data_elems
);
cnt
+=
sizeof
(
pkt
.
side_data_elems
);
if
(
pkt
.
side_data_elems
!=
0
)
{
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
));
for
(
int
i
=
0
;
i
<
pkt
.
side_data_elems
;
i
++
)
cnt
+=
sizeof
(
pkt
.
side_data
[
i
].
size
);
{
memcpy
((
*
bytes
)
+
cnt
,
pkt
.
side_data
[
i
].
data
,
pkt
.
side_data
[
i
].
size
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
side_data
[
i
].
size
),
sizeof
(
pkt
.
side_data
[
i
].
size
));
cnt
+=
pkt
.
side_data
[
i
].
size
;
cnt
+=
sizeof
(
pkt
.
side_data
[
i
].
size
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
side_data
[
i
].
type
),
sizeof
(
pkt
.
side_data
[
i
].
type
));
memcpy
((
*
bytes
)
+
cnt
,
pkt
.
side_data
[
i
].
data
,
pkt
.
side_data
[
i
].
size
);
cnt
+=
sizeof
(
pkt
.
side_data
[
i
].
type
);
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
// other properties
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pts
),
sizeof
(
pkt
.
pts
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pts
),
sizeof
(
pkt
.
pts
));
cnt
+=
sizeof
(
pkt
.
pts
);
cnt
+=
sizeof
(
pkt
.
pts
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
dts
),
sizeof
(
pkt
.
dts
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
dts
),
sizeof
(
pkt
.
dts
));
cnt
+=
8
;
cnt
+=
8
;
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pos
),
sizeof
(
pkt
.
pos
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pos
),
sizeof
(
pkt
.
pos
));
cnt
+=
sizeof
(
pkt
.
pos
);
cnt
+=
sizeof
(
pkt
.
pos
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
duration
),
sizeof
(
pkt
.
duration
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
duration
),
sizeof
(
pkt
.
duration
));
cnt
+=
sizeof
(
pkt
.
duration
);
cnt
+=
sizeof
(
pkt
.
duration
);
// deprecated
// deprecated
//memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
//memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
//cnt+=sizeof(pkt.convergence_duration);
//cnt+=sizeof(pkt.convergence_duration);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
flags
),
sizeof
(
pkt
.
flags
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
flags
),
sizeof
(
pkt
.
flags
));
cnt
+=
sizeof
(
pkt
.
flags
);
cnt
+=
sizeof
(
pkt
.
flags
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
stream_index
),
sizeof
(
pkt
.
stream_index
));
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
stream_index
),
sizeof
(
pkt
.
stream_index
));
cnt
+=
sizeof
(
pkt
.
stream_index
);
cnt
+=
sizeof
(
pkt
.
stream_index
);
memcpy
((
*
bytes
)
+
cnt
,
&
wholeSize
,
sizeof
(
wholeSize
));
memcpy
((
*
bytes
)
+
cnt
,
&
wholeSize
,
sizeof
(
wholeSize
));
cnt
+=
sizeof
(
wholeSize
);
cnt
+=
sizeof
(
wholeSize
);
memcpy
((
*
bytes
)
+
cnt
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
memcpy
((
*
bytes
)
+
cnt
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
cnt
+=
strlen
(
PS_MARK_E
);
cnt
+=
strlen
(
PS_MARK_E
);
assert
(
cnt
==
wholeSize
);
assert
(
cnt
==
wholeSize
);
spdlog
::
debug
(
"pkt origin size {:d}, serialized size: {:d}, elems: {:d}"
,
pkt
.
size
,
wholeSize
,
pkt
.
side_data_elems
);
spdlog
::
debug
(
"pkt origin size {:d}, serialized size: {:d}, elems: {:d}"
,
pkt
.
size
,
wholeSize
,
pkt
.
side_data_elems
);
return
wholeSize
;
return
wholeSize
;
}
}
int
decode
(
char
*
bytes
,
int
len
,
AVPacket
*
pkt
)
{
int
decode
(
char
*
bytes
,
int
len
,
AVPacket
*
pkt
)
{
// allocate packet mem on heap
// allocate packet mem on heap
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
int
ret
=
0
;
int
ret
=
0
;
int
got
=
0
;
int
got
=
0
;
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
)))
{
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
)))
{
spdlog
::
error
(
"invalid packet"
);
spdlog
::
error
(
"invalid packet"
);
return
-
1
;
return
-
1
;
}
}
...
@@ -107,10 +117,11 @@ namespace AVPacketSerializer {
...
@@ -107,10 +117,11 @@ namespace AVPacketSerializer {
got
+=
pkt
->
size
;
got
+=
pkt
->
size
;
memcpy
(
&
pkt
->
side_data_elems
,
bytes
+
got
,
sizeof
(
pkt
->
side_data_elems
));
memcpy
(
&
pkt
->
side_data_elems
,
bytes
+
got
,
sizeof
(
pkt
->
side_data_elems
));
got
+=
sizeof
(
pkt
->
side_data_elems
);
got
+=
sizeof
(
pkt
->
side_data_elems
);
for
(
int
i
=
0
;
i
<
pkt
->
side_data_elems
;
i
++
)
{
for
(
int
i
=
0
;
i
<
pkt
->
side_data_elems
;
i
++
)
memcpy
(
&
(
pkt
->
side_data
[
i
].
size
),
bytes
+
got
,
sizeof
(
pkt
->
side_data
[
i
].
size
));
{
memcpy
(
&
(
pkt
->
side_data
[
i
].
size
),
bytes
+
got
,
sizeof
(
pkt
->
side_data
[
i
].
size
));
got
+=
sizeof
(
pkt
->
side_data
[
i
].
size
);
got
+=
sizeof
(
pkt
->
side_data
[
i
].
size
);
memcpy
(
pkt
->
side_data
[
i
].
data
,
bytes
+
got
,
pkt
->
side_data
[
i
].
size
);
memcpy
(
pkt
->
side_data
[
i
].
data
,
bytes
+
got
,
pkt
->
side_data
[
i
].
size
);
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
));
memcpy
(
&
(
pkt
->
side_data
[
i
].
type
),
bytes
+
got
,
sizeof
(
pkt
->
side_data
[
i
].
type
));
got
+=
sizeof
(
pkt
->
side_data
[
i
].
type
);
got
+=
sizeof
(
pkt
->
side_data
[
i
].
type
);
...
@@ -140,15 +151,19 @@ namespace AVPacketSerializer {
...
@@ -140,15 +151,19 @@ namespace AVPacketSerializer {
spdlog
::
debug
(
"wholeSize: {:d}, {:d}"
,
wholeSize
,
got
);
spdlog
::
debug
(
"wholeSize: {:d}, {:d}"
,
wholeSize
,
got
);
return
ret
;
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
encode
(
AVFormatContext
*
ctx
,
char
**
bytes
)
{
int
ret
=
0
;
int
ret
=
0
;
int
wholeSize
=
0
;
int
wholeSize
=
0
;
int
got
=
0
;
int
got
=
0
;
...
@@ -157,7 +172,8 @@ namespace AVFormatCtxSerializer {
...
@@ -157,7 +172,8 @@ namespace AVFormatCtxSerializer {
// num streams
// num streams
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
wholeSize
+=
sizeof
(
AVStream
);
wholeSize
+=
sizeof
(
AVStream
);
wholeSize
+=
sizeof
(
AVCodecParameters
);
wholeSize
+=
sizeof
(
AVCodecParameters
);
}
}
...
@@ -165,13 +181,14 @@ namespace AVFormatCtxSerializer {
...
@@ -165,13 +181,14 @@ namespace AVFormatCtxSerializer {
wholeSize
+=
strlen
(
PS_MARK_E
);
wholeSize
+=
strlen
(
PS_MARK_E
);
// alloc memory
// alloc memory
*
bytes
=
(
char
*
)
malloc
(
wholeSize
);
*
bytes
=
(
char
*
)
malloc
(
wholeSize
);
// populate
// populate
memcpy
((
*
bytes
)
+
got
,
PS_MARK_S
,
strlen
(
PS_MARK_S
));
memcpy
((
*
bytes
)
+
got
,
PS_MARK_S
,
strlen
(
PS_MARK_S
));
got
+=
strlen
(
PS_MARK_S
);
got
+=
strlen
(
PS_MARK_S
);
memcpy
((
*
bytes
)
+
got
,
(
void
*
)
&
(
ctx
->
nb_streams
),
sizeof
(
ctx
->
nb_streams
));
memcpy
((
*
bytes
)
+
got
,
(
void
*
)
&
(
ctx
->
nb_streams
),
sizeof
(
ctx
->
nb_streams
));
got
+=
sizeof
(
ctx
->
nb_streams
);
got
+=
sizeof
(
ctx
->
nb_streams
);
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
//
//
memcpy
((
*
bytes
)
+
got
,
ctx
->
streams
[
i
],
sizeof
(
AVStream
));
memcpy
((
*
bytes
)
+
got
,
ctx
->
streams
[
i
],
sizeof
(
AVStream
));
got
+=
sizeof
(
AVStream
);
got
+=
sizeof
(
AVStream
);
...
@@ -184,65 +201,110 @@ namespace AVFormatCtxSerializer {
...
@@ -184,65 +201,110 @@ namespace AVFormatCtxSerializer {
memcpy
((
*
bytes
)
+
got
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
memcpy
((
*
bytes
)
+
got
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
return
wholeSize
;
return
wholeSize
;
}
}
int
decode
(
char
*
bytes
,
int
len
,
AVFormatContext
*
pCtx
)
{
int
decode
(
char
*
bytes
,
int
len
,
AVFormatContext
*
pCtx
)
{
int
ret
=
0
;
int
ret
=
0
;
int
got
=
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
)
{
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"
);
spdlog
::
error
(
"invalid packet"
);
return
-
1
;
return
-
1
;
}
}
got
+=
strlen
(
PS_MARK_S
);
got
+=
strlen
(
PS_MARK_S
);
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
got
+=
sizeof
(
ret
);
got
+=
sizeof
(
ret
);
pCtx
->
streams
=
(
AVStream
**
)
malloc
(
sizeof
(
AVStream
*
)
*
ret
);
pCtx
->
streams
=
(
AVStream
**
)
malloc
(
sizeof
(
AVStream
*
)
*
ret
);
pCtx
->
nb_streams
=
ret
;
pCtx
->
nb_streams
=
ret
;
for
(
int
i
=
0
;
i
<
ret
;
i
++
)
{
for
(
int
i
=
0
;
i
<
ret
;
i
++
)
pCtx
->
streams
[
i
]
=
(
AVStream
*
)
malloc
(
sizeof
(
AVStream
));
{
memcpy
(
pCtx
->
streams
[
i
],
bytes
+
got
,
sizeof
(
AVStream
));
pCtx
->
streams
[
i
]
=
(
AVStream
*
)
malloc
(
sizeof
(
AVStream
));
got
+=
sizeof
(
AVStream
);
memcpy
(
pCtx
->
streams
[
i
],
bytes
+
got
,
sizeof
(
AVStream
));
pCtx
->
streams
[
i
]
->
codecpar
=
(
AVCodecParameters
*
)
malloc
(
sizeof
(
AVCodecParameters
));
got
+=
sizeof
(
AVStream
);
memcpy
(
pCtx
->
streams
[
i
]
->
codecpar
,
bytes
+
got
,
sizeof
(
AVCodecParameters
));
pCtx
->
streams
[
i
]
->
codecpar
=
(
AVCodecParameters
*
)
malloc
(
sizeof
(
AVCodecParameters
));
got
+=
sizeof
(
AVCodecParameters
);
memcpy
(
pCtx
->
streams
[
i
]
->
codecpar
,
bytes
+
got
,
sizeof
(
AVCodecParameters
));
got
+=
sizeof
(
AVCodecParameters
);
}
}
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
assert
(
ret
==
len
);
assert
(
ret
==
len
);
return
ret
;
return
ret
;
}
}
void
freeCtx
(
AVFormatContext
*
pCtx
)
{
void
freeCtx
(
AVFormatContext
*
pCtx
)
for
(
int
i
=
0
;
i
<
pCtx
->
nb_streams
;
i
++
)
{
{
for
(
int
i
=
0
;
i
<
pCtx
->
nb_streams
;
i
++
)
{
free
(
pCtx
->
streams
[
i
]
->
codecpar
);
free
(
pCtx
->
streams
[
i
]
->
codecpar
);
free
(
pCtx
->
streams
[
i
]);
free
(
pCtx
->
streams
[
i
]);
}
}
free
(
pCtx
->
streams
);
free
(
pCtx
->
streams
);
}
}
// namespace AVFormatCtxSerializer
// cloudutils
namespace
cloudutils
{
/**
* scn:
* evpuller, evmgr, evmotion, evslicer
*
* */
/*
{
"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
}
]
}
}
}
}
}
*/
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}]}}}"
;
namespace
cloudutils
{
json
registry
(
const
char
*
sn
,
const
char
*
scn
,
int
iid
)
json
registry
(
const
char
*
gn
,
const
char
*
scn
,
int
iid
)
{
{
json
jret
;
// find local info in db
// find local info in db
// request cloud info
// request cloud info
// moc
// moc
jret
[
"iid"
]
=
"MOCK_IID"
;
json
ret
=
json
::
parse
(
config
);
jret
[
"scn"
]
=
string
(
scn
);
jret
[
"ipc"
]
=
"172.31.0.51"
;
jret
[
"gn"
]
=
"MOCK_GN"
;
jret
[
"ga"
]
=
"localhost"
;
if
(
scn
!=
NULL
&&
strcmp
(
scn
,
"evmgr"
))
{
jret
[
"evmgr"
]
=
"tcp://localhost:5556"
;
}
else
{
jret
[
"evmgr"
]
=
"tcp://0.0.0.0:5556"
;
}
jret
[
"code"
]
=
0
;
return
jret
;
return
ret
;
}
}
}
}
// namespace cloudutils
#endif
#endif
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论