Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
74d074ac
Commit
74d074ac
authored
May 20, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善Hook与API
parent
2f6773f1
全部展开
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
124 行增加
和
33 行删除
+124
-33
server/WebApi.cpp
+101
-18
server/WebHook.cpp
+0
-0
server/main.cpp
+13
-8
src/Common/config.h
+1
-1
src/Http/HttpClient.h
+7
-4
src/Rtsp/RtspSession.cpp
+2
-2
没有找到文件。
server/WebApi.cpp
查看文件 @
74d074ac
...
...
@@ -13,14 +13,19 @@
#include "Http/HttpRequester.h"
#include "Http/HttpSession.h"
#include "Network/TcpServer.h"
#include "Player/PlayerProxy.h"
using
namespace
Json
;
using
namespace
toolkit
;
using
namespace
mediakit
;
typedef
map
<
string
,
variant
,
StrCaseCompare
>
ApiArgsType
;
#define API_ARGS HttpSession::KeyValue &headerIn, \
HttpSession::KeyValue &headerOut, \
HttpSession::KeyValu
e &allArgs, \
ApiArgsTyp
e &allArgs, \
Json::Value &val
#define API_REGIST(field, name, ...) \
...
...
@@ -49,7 +54,7 @@ typedef enum {
#define API_FIELD "api."
const
char
kApiDebug
[]
=
API_FIELD
"apiDebug"
;
static
onceToken
token
([]()
{
mINI
::
Instance
()[
kApiDebug
]
=
"
0
"
;
mINI
::
Instance
()[
kApiDebug
]
=
"
1
"
;
});
}
//namespace API
...
...
@@ -72,20 +77,34 @@ public:
//获取HTTP请求中url参数、content参数
static
HttpSession
::
KeyValue
getAllArgs
(
const
Parser
&
parser
)
{
HttpSession
::
KeyValue
allArgs
;
{
//TraceL << parser.FullUrl() << "\r\n" << parser.Content();
auto
&
urlArgs
=
parser
.
getUrlArgs
();
static
ApiArgsType
getAllArgs
(
const
Parser
&
parser
)
{
ApiArgsType
allArgs
;
if
(
parser
[
"Content-Type"
].
find
(
"application/x-www-form-urlencoded"
)
==
0
){
auto
contentArgs
=
parser
.
parseArgs
(
parser
.
Content
());
for
(
auto
&
pr
:
contentArgs
)
{
allArgs
.
emplace
(
pr
.
first
,
HttpSession
::
urlDecode
(
pr
.
second
)
);
allArgs
[
pr
.
first
]
=
HttpSession
::
urlDecode
(
pr
.
second
);
}
for
(
auto
&
pr
:
urlArgs
)
{
allArgs
.
emplace
(
pr
.
first
,
HttpSession
::
urlDecode
(
pr
.
second
));
}
else
if
(
parser
[
"Content-Type"
].
find
(
"application/json"
)
==
0
){
try
{
stringstream
ss
(
parser
.
Content
());
Value
jsonArgs
;
ss
>>
jsonArgs
;
auto
keys
=
jsonArgs
.
getMemberNames
();
for
(
auto
key
=
keys
.
begin
();
key
!=
keys
.
end
();
++
key
){
allArgs
[
*
key
]
=
jsonArgs
[
*
key
].
asString
();
}
}
catch
(
std
::
exception
&
ex
){
WarnL
<<
ex
.
what
();
}
}
else
if
(
!
parser
[
"Content-Type"
].
empty
()){
WarnL
<<
"invalid Content-Type:"
<<
parser
[
"Content-Type"
];
}
auto
&
urlArgs
=
parser
.
getUrlArgs
();
for
(
auto
&
pr
:
urlArgs
)
{
allArgs
[
pr
.
first
]
=
HttpSession
::
urlDecode
(
pr
.
second
);
}
return
allArgs
;
return
std
::
move
(
allArgs
)
;
}
static
inline
void
addHttpListener
(){
...
...
@@ -107,7 +126,7 @@ static inline void addHttpListener(){
val
[
"code"
]
=
API
::
Success
;
HttpSession
::
KeyValue
&
headerIn
=
parser
.
getValues
();
HttpSession
::
KeyValue
headerOut
;
HttpSession
::
KeyValue
allArgs
=
getAllArgs
(
parser
);
auto
allArgs
=
getAllArgs
(
parser
);
headerOut
[
"Content-Type"
]
=
"application/json; charset=utf-8"
;
if
(
api_debug
){
auto
newInvoker
=
[
invoker
,
parser
,
allArgs
](
const
string
&
codeOut
,
...
...
@@ -115,13 +134,13 @@ static inline void addHttpListener(){
const
string
&
contentOut
){
stringstream
ss
;
for
(
auto
&
pr
:
allArgs
){
ss
<<
pr
.
first
<<
" : "
<<
pr
.
second
<<
"
\r\n
"
;
ss
<<
pr
.
first
<<
" : "
<<
(
string
)
pr
.
second
<<
"
\r\n
"
;
}
DebugL
<<
"request:
\r\n
"
<<
parser
.
Method
()
<<
" "
<<
parser
.
FullUrl
()
<<
"
\r\n
"
<<
"content:
\r\n
"
<<
parser
.
Content
()
<<
"
\r\n
"
<<
"args:
\r\n
"
<<
ss
.
str
()
<<
"response:
\r\n
"
DebugL
<<
"
\r\n
#
request:
\r\n
"
<<
parser
.
Method
()
<<
" "
<<
parser
.
FullUrl
()
<<
"
\r\n
"
<<
"
#
content:
\r\n
"
<<
parser
.
Content
()
<<
"
\r\n
"
<<
"
#
args:
\r\n
"
<<
ss
.
str
()
<<
"
#
response:
\r\n
"
<<
contentOut
<<
"
\r\n
"
;
invoker
(
codeOut
,
headerOut
,
contentOut
);
...
...
@@ -263,7 +282,7 @@ void installWebApi() {
item
[
"vhost"
]
=
vhost
;
item
[
"app"
]
=
app
;
item
[
"stream"
]
=
stream
;
val
[
"data"
]
[
"array"
]
.
append
(
item
);
val
[
"data"
].
append
(
item
);
});
});
...
...
@@ -303,6 +322,33 @@ void installWebApi() {
});
static
unordered_map
<
uint64_t
,
PlayerProxy
::
Ptr
>
s_proxyMap
;
static
recursive_mutex
s_proxyMapMtx
;
API_REGIST
(
api
,
addStreamProxy
,{
//添加拉流代理
PlayerProxy
::
Ptr
player
(
new
PlayerProxy
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
],
allArgs
[
"enable_hls"
],
allArgs
[
"enable_mp4"
]
));
//指定RTP over TCP(播放rtsp时有效)
(
*
player
)[
kRtpType
]
=
allArgs
[
"rtp_type"
].
as
<
int
>
();
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,重试次数在配置文件中配置,默认一直重试
player
->
play
(
allArgs
[
"url"
]);
val
[
"data"
][
"id"
]
=
player
.
get
();
lock_guard
<
recursive_mutex
>
lck
(
s_proxyMapMtx
);
s_proxyMap
[(
uint64_t
)
player
.
get
()]
=
player
;
});
API_REGIST
(
api
,
delStreamProxy
,{
lock_guard
<
recursive_mutex
>
lck
(
s_proxyMapMtx
);
val
[
"data"
][
"flag"
]
=
s_proxyMap
.
erase
(
allArgs
[
"id"
].
as
<
uint64_t
>
())
==
1
;
});
////////////以下是注册的Hook API////////////
API_REGIST
(
hook
,
on_publish
,{
//开始推流事件
...
...
@@ -321,4 +367,40 @@ void installWebApi() {
val
[
"code"
]
=
0
;
val
[
"msg"
]
=
"success"
;
});
API_REGIST
(
hook
,
on_rtsp_realm
,{
//rtsp是否需要鉴权
val
[
"code"
]
=
0
;
val
[
"realm"
]
=
"zlmediakit_reaml"
;
});
API_REGIST
(
hook
,
on_rtsp_auth
,{
//rtsp鉴权密码,密码等于用户名
//rtsp可以有双重鉴权!后面还会触发on_play事件
val
[
"code"
]
=
0
;
val
[
"encrypted"
]
=
false
;
val
[
"passwd"
]
=
allArgs
[
"user_name"
];
});
API_REGIST
(
hook
,
on_stream_changed
,{
//媒体注册或反注册事件
val
[
"code"
]
=
0
;
val
[
"msg"
]
=
"success"
;
});
API_REGIST
(
hook
,
on_stream_not_found
,{
//媒体未找到事件
val
[
"code"
]
=
0
;
val
[
"msg"
]
=
"success"
;
});
API_REGIST
(
hook
,
on_record_mp4
,{
//录制mp4分片完毕事件
val
[
"code"
]
=
0
;
val
[
"msg"
]
=
"success"
;
});
}
\ No newline at end of file
server/WebHook.cpp
查看文件 @
74d074ac
差异被折叠。
点击展开。
server/main.cpp
查看文件 @
74d074ac
...
...
@@ -188,30 +188,31 @@ static void inline listen_shell_input(){
EventPollerPool
::
Instance
().
getFirstPoller
()
->
addEvent
(
STDIN_FILENO
,
Event_Read
|
Event_Error
|
Event_LT
,
oninput
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
{
CMD_main
cmd_main
;
try
{
cmd_main
.
operator
()(
argc
,
argv
);
cmd_main
.
operator
()(
argc
,
argv
);
}
catch
(
std
::
exception
&
ex
)
{
cout
<<
ex
.
what
()
<<
endl
;
return
-
1
;
}
bool
bDaemon
=
cmd_main
.
hasKey
(
"daemon"
);
LogLevel
logLevel
=
(
LogLevel
)
cmd_main
[
"level"
].
as
<
int
>
();
logLevel
=
MIN
(
MAX
(
logLevel
,
LTrace
),
LError
);
LogLevel
logLevel
=
(
LogLevel
)
cmd_main
[
"level"
].
as
<
int
>
();
logLevel
=
MIN
(
MAX
(
logLevel
,
LTrace
),
LError
);
string
ini_file
=
cmd_main
[
"config"
];
string
ssl_file
=
cmd_main
[
"ssl"
];
int
threads
=
cmd_main
[
"threads"
];
//设置日志
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
(
"ConsoleChannel"
,
logLevel
));
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
(
"ConsoleChannel"
,
logLevel
));
#if defined(__linux__) || defined(__linux)
Logger
::
Instance
().
add
(
std
::
make_shared
<
SysLogChannel
>
(
"SysLogChannel"
,
logLevel
));
#else
Logger
::
Instance
().
add
(
std
::
make_shared
<
FileChannel
>
(
"FileChannel"
,
exePath
()
+
".log"
,
logLevel
));
Logger
::
Instance
().
add
(
std
::
make_shared
<
FileChannel
>
(
"FileChannel"
,
exePath
()
+
".log"
,
logLevel
));
#endif
if
(
bDaemon
)
{
if
(
bDaemon
)
{
//启动守护进程
System
::
startDaemon
();
}
...
...
@@ -267,16 +268,20 @@ int main(int argc,char *argv[]) {
installWebHook
();
InfoL
<<
"已启动http hook 接口"
;
if
(
!
bDaemon
)
{
if
(
!
bDaemon
)
{
//交互式shell输入
listen_shell_input
();
}
//设置退出信号处理函数
static
semaphore
sem
;
signal
(
SIGINT
,
[](
int
)
{
InfoL
<<
"SIGINT:exit"
;
sem
.
post
();
});
// 设置退出信号
signal
(
SIGINT
,
[](
int
)
{
InfoL
<<
"SIGINT:exit"
;
sem
.
post
();
});
// 设置退出信号
signal
(
SIGHUP
,
[](
int
)
{
mediakit
::
loadIniConfig
();
});
sem
.
wait
();
}
return
0
;
}
src/Common/config.h
查看文件 @
74d074ac
...
...
@@ -86,7 +86,7 @@ extern const char kBroadcastOnGetRtspRealm[];
//请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败
//获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码
extern
const
char
kBroadcastOnRtspAuth
[];
#define BroadcastOnRtspAuthArgs const MediaInfo &args,const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,TcpSession &sender
#define BroadcastOnRtspAuthArgs const MediaInfo &args,const string &
realm,const string &
user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,TcpSession &sender
//鉴权结果回调对象
//如果errMessage为空则代表鉴权成功
...
...
src/Http/HttpClient.h
查看文件 @
74d074ac
...
...
@@ -37,13 +37,14 @@
#include "HttpRequestSplitter.h"
#include "HttpCookie.h"
#include "HttpChunkedSplitter.h"
#include "strCoding.h"
using
namespace
std
;
using
namespace
toolkit
;
namespace
mediakit
{
class
HttpArgs
:
public
StrCaseMap
{
class
HttpArgs
:
public
map
<
string
,
variant
,
StrCaseCompare
>
{
public
:
HttpArgs
(){}
virtual
~
HttpArgs
(){}
...
...
@@ -52,7 +53,7 @@ public:
for
(
auto
&
pr
:
*
this
){
ret
.
append
(
pr
.
first
);
ret
.
append
(
"="
);
ret
.
append
(
pr
.
second
);
ret
.
append
(
strCoding
::
UrlUTF8Encode
(
pr
.
second
)
);
ret
.
append
(
"&"
);
}
if
(
ret
.
size
()){
...
...
@@ -96,7 +97,8 @@ private:
class
HttpMultiFormBody
:
public
HttpBody
{
public
:
typedef
std
::
shared_ptr
<
HttpMultiFormBody
>
Ptr
;
HttpMultiFormBody
(
const
StrCaseMap
&
args
,
const
string
&
filePath
,
const
string
&
boundary
,
uint32_t
sliceSize
=
4
*
1024
){
template
<
typename
MapType
>
HttpMultiFormBody
(
const
MapType
&
args
,
const
string
&
filePath
,
const
string
&
boundary
,
uint32_t
sliceSize
=
4
*
1024
){
_fp
=
fopen
(
filePath
.
data
(),
"rb"
);
if
(
!
_fp
){
throw
std
::
invalid_argument
(
StrPrinter
<<
"打开文件失败:"
<<
filePath
<<
" "
<<
get_uv_errmsg
());
...
...
@@ -156,7 +158,8 @@ public:
}
public
:
static
string
multiFormBodyPrefix
(
const
StrCaseMap
&
args
,
const
string
&
boundary
,
const
string
&
fileName
){
template
<
typename
MapType
>
static
string
multiFormBodyPrefix
(
const
MapType
&
args
,
const
string
&
boundary
,
const
string
&
fileName
){
string
MPboundary
=
string
(
"--"
)
+
boundary
;
_StrPrinter
body
;
for
(
auto
&
pr
:
args
){
...
...
src/Rtsp/RtspSession.cpp
查看文件 @
74d074ac
...
...
@@ -422,7 +422,7 @@ void RtspSession::onAuthBasic(const weak_ptr<RtspSession> &weakSelf,const string
}
//此时必须提供明文密码
if
(
!
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastOnRtspAuth
,
strongSelf
->
_mediaInfo
,
user
,
true
,
invoker
,
*
strongSelf
)){
if
(
!
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastOnRtspAuth
,
strongSelf
->
_mediaInfo
,
realm
,
user
,
true
,
invoker
,
*
strongSelf
)){
//表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之
WarnL
<<
"请监听kBroadcastOnRtspAuth事件!"
;
//但是我们还是忽略认证以便完成播放
...
...
@@ -503,7 +503,7 @@ void RtspSession::onAuthDigest(const weak_ptr<RtspSession> &weakSelf,const strin
};
//此时可以提供明文或md5加密的密码
if
(
!
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastOnRtspAuth
,
strongSelf
->
_mediaInfo
,
username
,
false
,
invoker
,
*
strongSelf
)){
if
(
!
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastOnRtspAuth
,
strongSelf
->
_mediaInfo
,
realm
,
username
,
false
,
invoker
,
*
strongSelf
)){
//表明该流需要认证却没监听请求密码事件,这一般是大意的程序所为,警告之
WarnL
<<
"请监听kBroadcastOnRtspAuth事件!"
;
//但是我们还是忽略认证以便完成播放
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论