Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
evsuits
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
OpsTeam
evsuits
Commits
4d8be15f
提交
4d8be15f
authored
9月 04, 2019
作者:
bruce.lu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
0c0a296c
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
468 行增加
和
391 行删除
+468
-391
database.cpp
opencv-motion-detect/database.cpp
+3
-1
evcloudsvc.cpp
opencv-motion-detect/evcloudsvc.cpp
+48
-6
av_common.hpp
opencv-motion-detect/inc/av_common.hpp
+296
-0
common.hpp
opencv-motion-detect/inc/common.hpp
+2
-384
utils.hpp
opencv-motion-detect/inc/utils.hpp
+119
-0
没有找到文件。
opencv-motion-detect/database.cpp
浏览文件 @
4d8be15f
#pragma GCC diagnostic ignored "-Wunused-variable"
#include "inc/database.h"
#include "inc/database.h"
//#include "inc/json.hpp"
//#include "inc/json.hpp"
#include "spdlog/spdlog.h"
#include "spdlog/spdlog.h"
...
@@ -358,7 +360,7 @@ togo_end:
...
@@ -358,7 +360,7 @@ togo_end:
exit
(
1
);
exit
(
1
);
}
else
{
}
else
{
// replace sn
// replace sn
in
t
idx
=
0
;
size_
t
idx
=
0
;
for
(
auto
&
j
:
_sn_tmpl
)
{
for
(
auto
&
j
:
_sn_tmpl
)
{
idx
=
0
;
idx
=
0
;
while
(
true
)
{
while
(
true
)
{
...
...
opencv-motion-detect/evcloudsvc.cpp
浏览文件 @
4d8be15f
...
@@ -11,6 +11,7 @@ update: 2019/09/02
...
@@ -11,6 +11,7 @@ update: 2019/09/02
#include "inc/database.h"
#include "inc/database.h"
#include "inc/json.hpp"
#include "inc/json.hpp"
#include "inc/spdlog/spdlog.h"
#include "inc/spdlog/spdlog.h"
#include "utils.hpp"
using
namespace
std
;
using
namespace
std
;
using
namespace
httplib
;
using
namespace
httplib
;
...
@@ -21,11 +22,19 @@ using namespace nlohmann;
...
@@ -21,11 +22,19 @@ using namespace nlohmann;
class
HttpSrv
{
class
HttpSrv
{
private
:
private
:
Server
svr
;
Server
svr
;
// sn:module -> sn_of_evmgr
json
configMap
;
json
configMap
;
protected
:
protected
:
public
:
public
:
void
run
(){
void
run
(){
// load configmap
json
cnfm
;
LVDB
::
getValue
(
cnfm
,
"configmap"
);
if
(
cnfm
.
size
!=
0
){
this
->
configMap
=
cnfm
;
}
svr
.
Get
(
"/config"
,
[
this
](
const
Request
&
req
,
Response
&
res
){
svr
.
Get
(
"/config"
,
[
this
](
const
Request
&
req
,
Response
&
res
){
json
ret
;
json
ret
;
ret
[
"code"
]
=
0
;
ret
[
"code"
]
=
0
;
...
@@ -71,21 +80,54 @@ class HttpSrv{
...
@@ -71,21 +80,54 @@ class HttpSrv{
ret
[
"msg"
]
=
"evcloudsvc invalid config body received: "
+
req
.
body
;
ret
[
"msg"
]
=
"evcloudsvc invalid config body received: "
+
req
.
body
;
spdlog
::
error
(
ret
[
"msg"
]);
spdlog
::
error
(
ret
[
"msg"
]);
}
else
{
}
else
{
//LVDB::setLocalConfig(newConfig);
//this->configMap = newConfig;
json
&
data
=
newConfig
[
"data"
];
json
&
data
=
newConfig
[
"data"
];
for
(
auto
&
[
k
,
v
]
:
data
.
items
())
{
for
(
auto
&
[
k
,
v
]
:
data
.
items
())
{
// this is one evmgr
if
(
v
.
count
(
k
)
==
0
||
v
[
k
].
size
()
==
0
)
{
if
(
v
.
count
(
k
)
==
0
||
v
[
k
].
size
()
==
0
)
{
ret
[
"code"
]
=
2
;
ret
[
"code"
]
=
2
;
ret
[
"msg"
]
=
"evcloudsvc invalid value for key "
+
k
;
ret
[
"msg"
]
=
"evcloudsvc invalid value for key "
+
k
;
spdlog
::
error
(
ret
[
"msg"
]);
spdlog
::
error
(
ret
[
"msg"
]);
continue
;
}
else
{
}
else
{
//
// find all modules
LVDB
::
setLocalConfig
(
v
,
k
);
if
(
v
.
count
(
"ipcs"
)
==
0
||
v
[
"ipcs"
].
size
()
==
0
)
{
this
->
configMap
[
k
]
=
v
;
spdlog
::
error
(
"invalid ipcs in config body"
);
LVDB
::
setValue
(
this
->
configMap
,
"configmap"
)
continue
;
}
else
{
json
&
ipcs
=
v
[
"ipcs"
];
for
(
auto
&
ipc
:
ipcs
)
{
if
(
ipc
.
count
(
"modules"
)
==
0
||
ipc
[
"modules"
].
size
()
==
0
)
{
spdlog
::
error
(
"invalid modules in ipcs config body"
);
continue
;
}
else
{
json
&
modules
=
ipc
[
"modules"
];
for
(
auto
&
[
mn
,
ml
]
:
modules
.
items
())
{
if
(
ml
.
count
(
"sn"
)
!=
0
&&
ml
[
"sn"
].
size
()
!=
0
){
string
modKey
;
//ml
if
(
mn
==
"evml"
&&
ml
.
count
(
"type"
)
!=
0
&&
ml
[
"type"
].
size
()
!=
0
)
{
modKey
=
ml
[
"sn"
]
+
":evml:"
+
ml
[
"type"
];
}
else
{
modKey
=
ml
[
"sn"
]
+
":"
+
mn
;
}
this
->
configMap
[
modKey
]
=
v
;
}
}
// for modules
}
}
// for ipc
}
}
}
}
// update
auto
lastupdated
=
chrono
::
duration_cast
<
chrono
::
seconds
>
(
chrono
::
system_clock
::
now
().
time_since_epoch
()).
count
();
json
evmgrData
;
v
[
"lastupdated"
]
=
lastupdated
;
evmgrData
[
k
]
=
v
;
//save
LVDB
::
setLocalConfig
(
evmgrData
,
k
);
}
// for evmgr
// save configmap
LVDB
::
setValue
(
this
->
configMap
,
"configmap"
);
}
}
...
...
opencv-motion-detect/inc/av_common.hpp
0 → 100644
浏览文件 @
4d8be15f
/*
module: av_common
description:
author: Bruce.Lu <lzbgt@icloud.com>
update: 2019/08/23
*/
#ifndef __EV_AV_COMMON_H__
#define __EV_AV_COMMON_H__
extern
"C"
{
#include <libavformat/avformat.h>
#include <libavutil/time.h>
}
#include <libavutil/timestamp.h>
#include <spdlog/spdlog.h>
#include <json.hpp>
#include <sstream>
using
json
=
nlohmann
::
json
;
#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 PS_MARK_E "DEADBEEF"
#define PS_MARK_S "BEEFDEAD"
#define EV_LOG_PACKET_CNT (18*60*5)
void
avlogThrow
(
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
;
}
// AVPacketSerializer
namespace
AVPacketSerializer
{
int
encode
(
AVPacket
&
pkt
,
char
**
bytes
)
{
int
cnt
=
0
;
//data
int
wholeSize
=
strlen
(
PS_MARK_S
)
+
sizeof
(
pkt
.
size
)
+
pkt
.
size
;
//side data
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
wholeSize
+=
sizeof
(
pkt
.
pts
)
*
4
+
sizeof
(
pkt
.
flags
)
+
sizeof
(
pkt
.
stream_index
)
+
sizeof
(
wholeSize
)
+
strlen
(
PS_MARK_E
);
*
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
,
&
(
pkt
.
pts
),
sizeof
(
pkt
.
pts
));
cnt
+=
sizeof
(
pkt
.
pts
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
dts
),
sizeof
(
pkt
.
dts
));
cnt
+=
8
;
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pos
),
sizeof
(
pkt
.
pos
));
cnt
+=
sizeof
(
pkt
.
pos
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
duration
),
sizeof
(
pkt
.
duration
));
cnt
+=
sizeof
(
pkt
.
duration
);
// deprecated
//memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
//cnt+=sizeof(pkt.convergence_duration);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
flags
),
sizeof
(
pkt
.
flags
));
cnt
+=
sizeof
(
pkt
.
flags
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
stream_index
),
sizeof
(
pkt
.
stream_index
));
cnt
+=
sizeof
(
pkt
.
stream_index
);
memcpy
((
*
bytes
)
+
cnt
,
&
wholeSize
,
sizeof
(
wholeSize
));
cnt
+=
sizeof
(
wholeSize
);
memcpy
((
*
bytes
)
+
cnt
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
cnt
+=
strlen
(
PS_MARK_E
);
assert
(
cnt
==
wholeSize
);
av_log
(
NULL
,
AV_LOG_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
)
{
// allocate packet mem on heap
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
int
ret
=
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
)))
{
spdlog
::
error
(
"invalid packet"
);
return
-
1
;
}
//skip mark_s
got
+=
strlen
(
PS_MARK_S
);
memcpy
(
&
(
pkt
->
size
),
bytes
+
got
,
sizeof
(
pkt
->
size
));
got
+=
sizeof
(
pkt
->
size
);
av_new_packet
(
pkt
,
pkt
->
size
);
memcpy
(
pkt
->
data
,
bytes
+
got
,
pkt
->
size
);
got
+=
pkt
->
size
;
memcpy
(
&
pkt
->
side_data_elems
,
bytes
+
got
,
sizeof
(
pkt
->
side_data_elems
));
got
+=
sizeof
(
pkt
->
side_data_elems
);
for
(
int
i
=
0
;
i
<
pkt
->
side_data_elems
;
i
++
)
{
memcpy
(
&
(
pkt
->
side_data
[
i
].
size
),
bytes
+
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
);
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
)
{
free
(
data
);
}
// AVFormatCtxSerializer
namespace
AVFormatCtxSerializer
{
/**
* memory layerout
* PS_MARK_S | NUM_STREAMS | AVSTREAM+AVCODEPAR | WHOLESIZE | PS_MARK_E
* */
int
encode
(
AVFormatContext
*
ctx
,
char
**
bytes
)
{
int
ret
=
0
;
int
wholeSize
=
0
;
int
got
=
0
;
// calc total size
wholeSize
+=
strlen
(
PS_MARK_S
);
// num streams
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
spdlog
::
info
(
"encode sizeof streams: {:d}, {:d}"
,
sizeof
(
ctx
->
nb_streams
),
ctx
->
nb_streams
);
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
wholeSize
+=
sizeof
(
AVStream
);
wholeSize
+=
sizeof
(
AVCodecParameters
);
//extradata
wholeSize
+=
sizeof
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
);
if
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
!=
0
){
wholeSize
+=
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
;
}
}
wholeSize
+=
sizeof
(
wholeSize
);
wholeSize
+=
strlen
(
PS_MARK_E
);
// 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
);
//extra
memcpy
((
*
bytes
)
+
got
,
&
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
),
sizeof
(
int
));
got
+=
sizeof
(
int
);
memcpy
((
*
bytes
)
+
got
,
ctx
->
streams
[
i
]
->
codecpar
->
extradata
,
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
);
got
+=
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
;
}
memcpy
((
*
bytes
)
+
got
,
&
wholeSize
,
sizeof
(
wholeSize
));
got
+=
sizeof
(
wholeSize
);
memcpy
((
*
bytes
)
+
got
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
got
+=
strlen
(
PS_MARK_E
);
assert
(
wholeSize
==
got
);
spdlog
::
info
(
"encode wholesize: {}"
,
got
);
return
wholeSize
;
}
int
decode
(
char
*
bytes
,
int
len
,
AVFormatContext
*
pCtx
)
{
int
ret
=
0
;
int
got
=
0
;
memcpy
(
&
ret
,
bytes
+
len
-
strlen
(
PS_MARK_E
)
-
sizeof
(
ret
),
sizeof
(
ret
));
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
)
||
ret
!=
len
)
{
spdlog
::
error
(
"invalid avformatctx: {} {}"
,
ret
,
len
);
return
-
1
;
}
spdlog
::
info
(
"decode len: {}"
,
ret
);
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
);
// extra
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
int
));
got
+=
sizeof
(
int
);
if
(
ret
!=
0
)
{
pCtx
->
streams
[
i
]
->
codecpar
->
extradata_size
=
ret
;
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
=
(
uint8_t
*
)
malloc
(
ret
);
memcpy
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
,
bytes
+
got
,
ret
);
got
+=
ret
;
}
}
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
got
+=
sizeof
(
ret
);
got
+=
strlen
(
PS_MARK_E
);
spdlog
::
debug
(
"avformatctx decode: {:d} {:d} {:d}"
,
ret
,
len
,
got
);
assert
(
ret
==
len
);
return
ret
;
}
void
freeCtx
(
AVFormatContext
*
pCtx
)
{
for
(
int
i
=
0
;
i
<
pCtx
->
nb_streams
;
i
++
)
{
free
(
pCtx
->
streams
[
i
]
->
codecpar
);
if
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata_size
!=
0
)
{
free
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
);
}
free
(
pCtx
->
streams
[
i
]);
}
free
(
pCtx
->
streams
);
}
}
// namespace AVFormatCtxSerializer
#endif
\ No newline at end of file
opencv-motion-detect/inc/common.hpp
浏览文件 @
4d8be15f
...
@@ -8,391 +8,9 @@ update: 2019/08/23
...
@@ -8,391 +8,9 @@ update: 2019/08/23
#ifndef __COMMON_H__
#ifndef __COMMON_H__
#define __COMMON_H__
#define __COMMON_H__
extern
"C"
{
#include <libavformat/avformat.h>
#include <libavutil/time.h>
}
#include <libavutil/timestamp.h>
#include <spdlog/spdlog.h>
#include <json.hpp>
#include <sstream>
using
json
=
nlohmann
::
json
;
#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 PS_MARK_E "DEADBEEF"
#define PS_MARK_S "BEEFDEAD"
#define EV_LOG_PACKET_CNT (18*60*5)
void
avlogThrow
(
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
;
}
// AVPacketSerializer
namespace
AVPacketSerializer
{
int
encode
(
AVPacket
&
pkt
,
char
**
bytes
)
{
int
cnt
=
0
;
//data
int
wholeSize
=
strlen
(
PS_MARK_S
)
+
sizeof
(
pkt
.
size
)
+
pkt
.
size
;
//side data
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
wholeSize
+=
sizeof
(
pkt
.
pts
)
*
4
+
sizeof
(
pkt
.
flags
)
+
sizeof
(
pkt
.
stream_index
)
+
sizeof
(
wholeSize
)
+
strlen
(
PS_MARK_E
);
*
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
,
&
(
pkt
.
pts
),
sizeof
(
pkt
.
pts
));
cnt
+=
sizeof
(
pkt
.
pts
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
dts
),
sizeof
(
pkt
.
dts
));
cnt
+=
8
;
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
pos
),
sizeof
(
pkt
.
pos
));
cnt
+=
sizeof
(
pkt
.
pos
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
duration
),
sizeof
(
pkt
.
duration
));
cnt
+=
sizeof
(
pkt
.
duration
);
// deprecated
//memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
//cnt+=sizeof(pkt.convergence_duration);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
flags
),
sizeof
(
pkt
.
flags
));
cnt
+=
sizeof
(
pkt
.
flags
);
memcpy
((
*
bytes
)
+
cnt
,
&
(
pkt
.
stream_index
),
sizeof
(
pkt
.
stream_index
));
cnt
+=
sizeof
(
pkt
.
stream_index
);
memcpy
((
*
bytes
)
+
cnt
,
&
wholeSize
,
sizeof
(
wholeSize
));
cnt
+=
sizeof
(
wholeSize
);
memcpy
((
*
bytes
)
+
cnt
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
cnt
+=
strlen
(
PS_MARK_E
);
assert
(
cnt
==
wholeSize
);
av_log
(
NULL
,
AV_LOG_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
)
{
// allocate packet mem on heap
//AVPacket *pkt = (AVPacket*)malloc(sizeof(AVPacket));
int
ret
=
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
)))
{
spdlog
::
error
(
"invalid packet"
);
return
-
1
;
}
//skip mark_s
got
+=
strlen
(
PS_MARK_S
);
memcpy
(
&
(
pkt
->
size
),
bytes
+
got
,
sizeof
(
pkt
->
size
));
got
+=
sizeof
(
pkt
->
size
);
av_new_packet
(
pkt
,
pkt
->
size
);
memcpy
(
pkt
->
data
,
bytes
+
got
,
pkt
->
size
);
got
+=
pkt
->
size
;
memcpy
(
&
pkt
->
side_data_elems
,
bytes
+
got
,
sizeof
(
pkt
->
side_data_elems
));
got
+=
sizeof
(
pkt
->
side_data_elems
);
for
(
int
i
=
0
;
i
<
pkt
->
side_data_elems
;
i
++
)
{
memcpy
(
&
(
pkt
->
side_data
[
i
].
size
),
bytes
+
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
);
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
)
{
free
(
data
);
}
// AVFormatCtxSerializer
namespace
AVFormatCtxSerializer
{
/**
* memory layerout
* PS_MARK_S | NUM_STREAMS | AVSTREAM+AVCODEPAR | WHOLESIZE | PS_MARK_E
* */
int
encode
(
AVFormatContext
*
ctx
,
char
**
bytes
)
{
int
ret
=
0
;
int
wholeSize
=
0
;
int
got
=
0
;
// calc total size
wholeSize
+=
strlen
(
PS_MARK_S
);
// num streams
wholeSize
+=
sizeof
(
ctx
->
nb_streams
);
spdlog
::
info
(
"encode sizeof streams: {:d}, {:d}"
,
sizeof
(
ctx
->
nb_streams
),
ctx
->
nb_streams
);
for
(
int
i
=
0
;
i
<
ctx
->
nb_streams
;
i
++
)
{
wholeSize
+=
sizeof
(
AVStream
);
wholeSize
+=
sizeof
(
AVCodecParameters
);
//extradata
wholeSize
+=
sizeof
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
);
if
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
!=
0
){
wholeSize
+=
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
;
}
}
wholeSize
+=
sizeof
(
wholeSize
);
wholeSize
+=
strlen
(
PS_MARK_E
);
// 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
);
//extra
memcpy
((
*
bytes
)
+
got
,
&
(
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
),
sizeof
(
int
));
got
+=
sizeof
(
int
);
memcpy
((
*
bytes
)
+
got
,
ctx
->
streams
[
i
]
->
codecpar
->
extradata
,
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
);
got
+=
ctx
->
streams
[
i
]
->
codecpar
->
extradata_size
;
}
memcpy
((
*
bytes
)
+
got
,
&
wholeSize
,
sizeof
(
wholeSize
));
got
+=
sizeof
(
wholeSize
);
memcpy
((
*
bytes
)
+
got
,
PS_MARK_E
,
strlen
(
PS_MARK_E
));
got
+=
strlen
(
PS_MARK_E
);
assert
(
wholeSize
==
got
);
spdlog
::
info
(
"encode wholesize: {}"
,
got
);
return
wholeSize
;
}
int
decode
(
char
*
bytes
,
int
len
,
AVFormatContext
*
pCtx
)
{
int
ret
=
0
;
int
got
=
0
;
memcpy
(
&
ret
,
bytes
+
len
-
strlen
(
PS_MARK_E
)
-
sizeof
(
ret
),
sizeof
(
ret
));
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
)
||
ret
!=
len
)
{
spdlog
::
error
(
"invalid avformatctx: {} {}"
,
ret
,
len
);
return
-
1
;
}
spdlog
::
info
(
"decode len: {}"
,
ret
);
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
);
// extra
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
int
));
got
+=
sizeof
(
int
);
if
(
ret
!=
0
)
{
pCtx
->
streams
[
i
]
->
codecpar
->
extradata_size
=
ret
;
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
=
(
uint8_t
*
)
malloc
(
ret
);
memcpy
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
,
bytes
+
got
,
ret
);
got
+=
ret
;
}
}
memcpy
(
&
ret
,
bytes
+
got
,
sizeof
(
ret
));
got
+=
sizeof
(
ret
);
got
+=
strlen
(
PS_MARK_E
);
spdlog
::
debug
(
"avformatctx decode: {:d} {:d} {:d}"
,
ret
,
len
,
got
);
assert
(
ret
==
len
);
return
ret
;
}
void
freeCtx
(
AVFormatContext
*
pCtx
)
{
for
(
int
i
=
0
;
i
<
pCtx
->
nb_streams
;
i
++
)
{
free
(
pCtx
->
streams
[
i
]
->
codecpar
);
if
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata_size
!=
0
)
{
free
(
pCtx
->
streams
[
i
]
->
codecpar
->
extradata
);
}
free
(
pCtx
->
streams
[
i
]);
}
free
(
pCtx
->
streams
);
}
}
// namespace AVFormatCtxSerializer
// cloudutils
namespace
cloudutils
{
/**
* scn:
* evpuller, evmgr, evmotion, evslicer
*
* */
/*
{
"time":0,
"lastupdated": 0,
"code":0,
"data":{
"ILSEVMGR1":{
"lastupdated": 0,
"sn":"ILSEVMGR1",
"addr":"172.31.0.76",
"addr-cloud":"172.31.0.76",
"proto":"zmq",
"port-cloud":5556,
"port-router":5550,
"status":1,
"ipcs":[
{
"addr":"172.31.0.51",
"proto":"rtsp",
"user":"admin",
"password":"FWBWTU",
"port": 554,
"status":1,
"modules":{
"evpuller":[
{
"sn":"ILSEVPULLER1",
"addr":"172.31.0.76",
"iid":1,
"port-pub":5556,
"status":1
}
],
"evpusher":[
{
"sn":"ILSEVPUSHER1",
"iid":1,
"urlDest":"rtsp://40.73.41.176:554/test1",
"user":"",
"password":"",
"token":"",
"enabled":1,
"status":1
}
],
"evslicer":[
{
"sn":"ILSEVSLICER1",
"iid":1,
"path":"slices",
"enabled":1,
"status":1
}
],
"evml":[
{
"type":"motion",
"sn":"ILSEVMLMOTION1",
"iid":1,
"enabled":1,
"status":1
}
]
}
}
]
}
}
}
*/
// const char *config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"127.0.0.1\",\"addr-cloud\":\"127.0.0.1\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"iLabService\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"127.0.0.1\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}";
json
registry
(
const
char
*
sn
,
const
json
&
config
)
{
// find local info in db
// request cloud info
// moc
return
json
();
}
vector
<
string
>
split
(
const
std
::
string
&
s
,
char
delimiter
)
{
std
::
vector
<
std
::
string
>
tokens
;
std
::
string
token
;
std
::
istringstream
tokenStream
(
s
);
while
(
getline
(
tokenStream
,
token
,
delimiter
))
{
tokens
.
push_back
(
token
);
}
return
tokens
;
}
}
// namespace cloudutils
#include "utils.hpp"
#include "av_common.hpp"
#endif
#endif
opencv-motion-detect/inc/utils.hpp
0 → 100644
浏览文件 @
4d8be15f
#ifndef __EV_UTILS_H__
#define __EV_UTILS_H__
#include <vector>
#include <chrono>
#include <thread>
#include <map>
#include <sstream>
#include "json.hpp"
#include "spdlog/spdlog.h"
using
namespace
std
;
using
namespace
nlohmann
;
// cloudutils
namespace
cloudutils
{
/**
* scn:
* evpuller, evmgr, evmotion, evslicer
*
* */
/*
{
"time":0,
"lastupdated": 0,
"code":0,
"data":{
"ILSEVMGR1":{
"lastupdated": 0,
"sn":"ILSEVMGR1",
"addr":"172.31.0.76",
"addr-cloud":"172.31.0.76",
"proto":"zmq",
"port-cloud":5556,
"port-router":5550,
"status":1,
"ipcs":[
{
"addr":"172.31.0.51",
"proto":"rtsp",
"user":"admin",
"password":"FWBWTU",
"port": 554,
"status":1,
"modules":{
"evpuller":[
{
"sn":"ILSEVPULLER1",
"addr":"172.31.0.76",
"iid":1,
"port-pub":5556,
"status":1
}
],
"evpusher":[
{
"sn":"ILSEVPUSHER1",
"iid":1,
"urlDest":"rtsp://40.73.41.176:554/test1",
"user":"",
"password":"",
"token":"",
"enabled":1,
"status":1
}
],
"evslicer":[
{
"sn":"ILSEVSLICER1",
"iid":1,
"path":"slices",
"enabled":1,
"status":1
}
],
"evml":[
{
"type":"motion",
"sn":"ILSEVMLMOTION1",
"iid":1,
"enabled":1,
"status":1
}
]
}
}
]
}
}
}
*/
// const char *config = "{\"time\":0,\"code\":0,\"data\":{\"ILSEVMGR1\":{\"sn\":\"ILSEVMGR1\",\"addr\":\"127.0.0.1\",\"addr-cloud\":\"127.0.0.1\",\"proto\":\"zmq\",\"port-cloud\":5556,\"port-router\":5550,\"status\":1,\"ipcs\":[{\"addr\":\"172.31.0.51\",\"proto\":\"rtsp\",\"user\":\"admin\",\"password\":\"iLabService\",\"status\":1,\"modules\":{\"evpuller\":[{\"sn\":\"ILSEVPULLER1\",\"addr\":\"127.0.0.1\",\"iid\":1,\"port-pub\":5556,\"status\":1}],\"evpusher\":[{\"sn\":\"ILSEVPUSHER1\",\"iid\":1,\"urlDest\":\"rtsp://40.73.41.176:554/test1\",\"user\":\"\",\"password\":\"\",\"token\":\"\",\"enabled\":1,\"status\":1}],\"evslicer\":[{\"sn\":\"ILSEVSLICER1\",\"iid\":1,\"path\":\"slices\",\"enabled\":1,\"status\":1}],\"evml\":[{\"type\":\"motion\",\"sn\":\"ILSEVMLMOTION1\",\"iid\":1,\"enabled\":1,\"status\":1}]}}]}}}";
json
registry
(
const
char
*
sn
,
const
json
&
config
)
{
// find local info in db
// request cloud info
// moc
return
json
();
}
vector
<
string
>
split
(
const
std
::
string
&
s
,
char
delimiter
)
{
std
::
vector
<
std
::
string
>
tokens
;
std
::
string
token
;
std
::
istringstream
tokenStream
(
s
);
while
(
getline
(
tokenStream
,
token
,
delimiter
))
{
tokens
.
push_back
(
token
);
}
return
tokens
;
}
}
// namespace cloudutils
#endif
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论