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
09af12a1
Commit
09af12a1
authored
Jun 11, 2022
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
操作MediaSource对象时确保线程安全
parent
97116e12
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
113 行增加
和
78 行删除
+113
-78
server/WebApi.cpp
+113
-78
没有找到文件。
server/WebApi.cpp
查看文件 @
09af12a1
...
@@ -699,22 +699,23 @@ void installWebApi() {
...
@@ -699,22 +699,23 @@ void installWebApi() {
});
});
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs
//测试url http://127.0.0.1/index/api/getMediaInfo?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs
api_regist
(
"/index/api/getMediaInfo"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/getMediaInfo"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
);
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
if
(
!
src
){
if
(
!
src
){
val
[
"online"
]
=
false
;
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
return
;
}
}
val
=
makeMediaSourceJson
(
*
src
);
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
val
[
"online"
]
=
true
;
auto
val
=
makeMediaSourceJson
(
*
src
);
val
[
"code"
]
=
API
::
Success
;
val
[
"code"
]
=
API
::
Success
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
//主动关断流,包括关断拉流、推流
//主动关断流,包括关断拉流、推流
//测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1
//测试url http://127.0.0.1/index/api/close_stream?schema=rtsp&vhost=__defaultVhost__&app=live&stream=obs&force=1
api_regist
(
"/index/api/close_stream"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/close_stream"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
);
//踢掉推流器
//踢掉推流器
...
@@ -722,16 +723,17 @@ void installWebApi() {
...
@@ -722,16 +723,17 @@ void installWebApi() {
allArgs
[
"vhost"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
allArgs
[
"stream"
]);
if
(
src
)
{
if
(
!
src
)
{
bool
flag
=
src
->
close
(
allArgs
[
"force"
].
as
<
bool
>
());
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
}
bool
force
=
allArgs
[
"force"
].
as
<
bool
>
();
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
bool
flag
=
src
->
close
(
force
);
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"msg"
]
=
flag
?
"success"
:
"close failed"
;
val
[
"msg"
]
=
flag
?
"success"
:
"close failed"
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
}
else
{
});
val
[
"result"
]
=
-
2
;
val
[
"msg"
]
=
"can not find the stream"
;
val
[
"code"
]
=
API
::
OtherFailed
;
}
});
});
//批量主动关断流,包括关断拉流、推流
//批量主动关断流,包括关断拉流、推流
...
@@ -748,8 +750,8 @@ void installWebApi() {
...
@@ -748,8 +750,8 @@ void installWebApi() {
},
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
},
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
bool
force
=
allArgs
[
"force"
].
as
<
bool
>
();
bool
force
=
allArgs
[
"force"
].
as
<
bool
>
();
for
(
auto
&
media
:
media_list
)
{
for
(
auto
&
media
:
media_list
)
{
if
(
media
->
close
(
force
))
{
if
(
media
->
close
(
force
))
{
++
count_closed
;
++
count_closed
;
}
}
}
}
...
@@ -1105,7 +1107,7 @@ void installWebApi() {
...
@@ -1105,7 +1107,7 @@ void installWebApi() {
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
],
allArgs
[
"from_mp4"
].
as
<
int
>
());
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
],
allArgs
[
"from_mp4"
].
as
<
int
>
());
if
(
!
src
)
{
if
(
!
src
)
{
throw
ApiRetException
(
"
该媒体流不存在"
,
API
::
OtherFaile
d
);
throw
ApiRetException
(
"
can not find the source stream"
,
API
::
NotFoun
d
);
}
}
MediaSourceEvent
::
SendRtpArgs
args
;
MediaSourceEvent
::
SendRtpArgs
args
;
...
@@ -1118,15 +1120,17 @@ void installWebApi() {
...
@@ -1118,15 +1120,17 @@ void installWebApi() {
args
.
pt
=
allArgs
[
"pt"
].
empty
()
?
96
:
allArgs
[
"pt"
].
as
<
int
>
();
args
.
pt
=
allArgs
[
"pt"
].
empty
()
?
96
:
allArgs
[
"pt"
].
as
<
int
>
();
args
.
use_ps
=
allArgs
[
"use_ps"
].
empty
()
?
true
:
allArgs
[
"use_ps"
].
as
<
bool
>
();
args
.
use_ps
=
allArgs
[
"use_ps"
].
empty
()
?
true
:
allArgs
[
"use_ps"
].
as
<
bool
>
();
args
.
only_audio
=
allArgs
[
"only_audio"
].
empty
()
?
false
:
allArgs
[
"only_audio"
].
as
<
bool
>
();
args
.
only_audio
=
allArgs
[
"only_audio"
].
empty
()
?
false
:
allArgs
[
"only_audio"
].
as
<
bool
>
();
TraceL
<<
"startSendRtp, pt "
<<
int
(
args
.
pt
)
<<
" ps "
<<
args
.
use_ps
<<
" audio "
<<
args
.
only_audio
;
TraceL
<<
"startSendRtp, pt "
<<
int
(
args
.
pt
)
<<
" ps "
<<
args
.
use_ps
<<
" audio "
<<
args
.
only_audio
;
src
->
startSendRtp
(
args
,
[
val
,
headerOut
,
invoker
](
uint16_t
local_port
,
const
SockException
&
ex
)
mutable
{
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
if
(
ex
)
{
src
->
startSendRtp
(
args
,
[
val
,
headerOut
,
invoker
](
uint16_t
local_port
,
const
SockException
&
ex
)
mutable
{
val
[
"code"
]
=
API
::
OtherFailed
;
if
(
ex
)
{
val
[
"msg"
]
=
ex
.
what
();
val
[
"code"
]
=
API
::
OtherFailed
;
}
val
[
"msg"
]
=
ex
.
what
();
val
[
"local_port"
]
=
local_port
;
}
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
val
[
"local_port"
]
=
local_port
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
});
});
...
@@ -1136,7 +1140,7 @@ void installWebApi() {
...
@@ -1136,7 +1140,7 @@ void installWebApi() {
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
],
allArgs
[
"from_mp4"
].
as
<
int
>
());
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
],
allArgs
[
"from_mp4"
].
as
<
int
>
());
if
(
!
src
)
{
if
(
!
src
)
{
throw
ApiRetException
(
"
该媒体流不存在"
,
API
::
OtherFaile
d
);
throw
ApiRetException
(
"
can not find the source stream"
,
API
::
NotFoun
d
);
}
}
MediaSourceEvent
::
SendRtpArgs
args
;
MediaSourceEvent
::
SendRtpArgs
args
;
...
@@ -1148,29 +1152,38 @@ void installWebApi() {
...
@@ -1148,29 +1152,38 @@ void installWebApi() {
args
.
use_ps
=
allArgs
[
"use_ps"
].
empty
()
?
true
:
allArgs
[
"use_ps"
].
as
<
bool
>
();
args
.
use_ps
=
allArgs
[
"use_ps"
].
empty
()
?
true
:
allArgs
[
"use_ps"
].
as
<
bool
>
();
args
.
only_audio
=
allArgs
[
"only_audio"
].
empty
()
?
false
:
allArgs
[
"only_audio"
].
as
<
bool
>
();
args
.
only_audio
=
allArgs
[
"only_audio"
].
empty
()
?
false
:
allArgs
[
"only_audio"
].
as
<
bool
>
();
TraceL
<<
"startSendRtpPassive, pt "
<<
int
(
args
.
pt
)
<<
" ps "
<<
args
.
use_ps
<<
" audio "
<<
args
.
only_audio
;
TraceL
<<
"startSendRtpPassive, pt "
<<
int
(
args
.
pt
)
<<
" ps "
<<
args
.
use_ps
<<
" audio "
<<
args
.
only_audio
;
src
->
startSendRtp
(
args
,
[
val
,
headerOut
,
invoker
](
uint16_t
local_port
,
const
SockException
&
ex
)
mutable
{
if
(
ex
)
{
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
val
[
"code"
]
=
API
::
OtherFailed
;
src
->
startSendRtp
(
args
,
[
val
,
headerOut
,
invoker
](
uint16_t
local_port
,
const
SockException
&
ex
)
mutable
{
val
[
"msg"
]
=
ex
.
what
();
if
(
ex
)
{
}
val
[
"code"
]
=
API
::
OtherFailed
;
val
[
"local_port"
]
=
local_port
;
val
[
"msg"
]
=
ex
.
what
();
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
}
val
[
"local_port"
]
=
local_port
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
});
});
api_regist
(
"/index/api/stopSendRtp"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/stopSendRtp"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"vhost"
,
"app"
,
"stream"
);
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
if
(
!
src
)
{
if
(
!
src
)
{
throw
ApiRetException
(
"
该媒体流不存在"
,
API
::
OtherFaile
d
);
throw
ApiRetException
(
"
can not find the stream"
,
API
::
NotFoun
d
);
}
}
//ssrc如果为空,关闭全部
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
if
(
!
src
->
stopSendRtp
(
allArgs
[
"ssrc"
]))
{
// ssrc如果为空,关闭全部
throw
ApiRetException
(
"尚未开始推流,停止失败"
,
API
::
OtherFailed
);
if
(
!
src
->
stopSendRtp
(
allArgs
[
"ssrc"
]))
{
}
val
[
"code"
]
=
API
::
OtherFailed
;
val
[
"msg"
]
=
"stopSendRtp failed"
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
return
;
}
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
api_regist
(
"/index/api/pauseRtpCheck"
,
[](
API_ARGS_MAP
)
{
api_regist
(
"/index/api/pauseRtpCheck"
,
[](
API_ARGS_MAP
)
{
...
@@ -1199,80 +1212,102 @@ void installWebApi() {
...
@@ -1199,80 +1212,102 @@ void installWebApi() {
#endif//ENABLE_RTPPROXY
#endif//ENABLE_RTPPROXY
// 开始录制hls或MP4
// 开始录制hls或MP4
api_regist
(
"/index/api/startRecord"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/startRecord"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
auto
result
=
Recorder
::
startRecord
((
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
(),
allArgs
[
"vhost"
],
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]
);
allArgs
[
"app"
],
if
(
!
src
)
{
allArgs
[
"stream"
],
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
allArgs
[
"customized_path"
],
}
allArgs
[
"max_second"
].
as
<
size_t
>
());
val
[
"result"
]
=
result
;
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
val
[
"code"
]
=
result
?
API
::
Success
:
API
::
OtherFailed
;
auto
result
=
src
->
setupRecord
((
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
(),
true
,
allArgs
[
"customized_path"
],
allArgs
[
"max_second"
].
as
<
size_t
>
());
val
[
"msg"
]
=
result
?
"success"
:
"start record failed"
;
val
[
"result"
]
=
result
;
val
[
"code"
]
=
result
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"msg"
]
=
result
?
"success"
:
"start record failed"
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
//设置录像流播放速度
//设置录像流播放速度
api_regist
(
"/index/api/setRecordSpeed"
,
[](
API_ARGS_MAP
)
{
api_regist
(
"/index/api/setRecordSpeed"
,
[](
API_ARGS_MAP
_ASYNC
)
{
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
,
"speed"
);
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
,
"speed"
);
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
allArgs
[
"stream"
]);
if
(
src
)
{
if
(
!
src
)
{
bool
flag
=
src
->
speed
(
allArgs
[
"speed"
].
as
<
float
>
());
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
}
auto
speed
=
allArgs
[
"speed"
].
as
<
float
>
();
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
bool
flag
=
src
->
speed
(
speed
);
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"msg"
]
=
flag
?
"success"
:
"set failed"
;
val
[
"msg"
]
=
flag
?
"success"
:
"set failed"
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
}
else
{
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
val
[
"result"
]
=
-
2
;
});
val
[
"msg"
]
=
"can not find the stream"
;
val
[
"code"
]
=
API
::
OtherFailed
;
}
});
});
api_regist
(
"/index/api/seekRecordStamp"
,
[](
API_ARGS_MAP
)
{
api_regist
(
"/index/api/seekRecordStamp"
,
[](
API_ARGS_MAP
_ASYNC
)
{
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
,
"stamp"
);
CHECK_ARGS
(
"schema"
,
"vhost"
,
"app"
,
"stream"
,
"stamp"
);
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
auto
src
=
MediaSource
::
find
(
allArgs
[
"schema"
],
allArgs
[
"vhost"
],
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
allArgs
[
"stream"
]);
if
(
src
)
{
if
(
!
src
)
{
bool
flag
=
src
->
seekTo
(
allArgs
[
"stamp"
].
as
<
size_t
>
());
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
}
auto
stamp
=
allArgs
[
"stamp"
].
as
<
size_t
>
();
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
bool
flag
=
src
->
seekTo
(
stamp
);
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"result"
]
=
flag
?
0
:
-
1
;
val
[
"msg"
]
=
flag
?
"success"
:
"seek failed"
;
val
[
"msg"
]
=
flag
?
"success"
:
"seek failed"
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"code"
]
=
flag
?
API
::
Success
:
API
::
OtherFailed
;
}
else
{
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
val
[
"result"
]
=
-
2
;
});
val
[
"msg"
]
=
"can not find the stream"
;
val
[
"code"
]
=
API
::
OtherFailed
;
}
});
});
// 停止录制hls或MP4
// 停止录制hls或MP4
api_regist
(
"/index/api/stopRecord"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/stopRecord"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
auto
result
=
Recorder
::
stopRecord
((
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
(),
allArgs
[
"vhost"
],
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]
);
allArgs
[
"app"
],
if
(
!
src
)
{
allArgs
[
"stream"
]);
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
val
[
"result"
]
=
result
;
}
val
[
"code"
]
=
result
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"msg"
]
=
result
?
"success"
:
"stop record failed"
;
auto
type
=
(
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
();
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
auto
result
=
src
->
setupRecord
(
type
,
false
,
""
,
0
);
val
[
"result"
]
=
result
;
val
[
"code"
]
=
result
?
API
::
Success
:
API
::
OtherFailed
;
val
[
"msg"
]
=
result
?
"success"
:
"stop record failed"
;
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
// 获取hls或MP4录制状态
// 获取hls或MP4录制状态
api_regist
(
"/index/api/isRecording"
,[](
API_ARGS_MAP
){
api_regist
(
"/index/api/isRecording"
,[](
API_ARGS_MAP
_ASYNC
){
CHECK_SECRET
();
CHECK_SECRET
();
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
CHECK_ARGS
(
"type"
,
"vhost"
,
"app"
,
"stream"
);
val
[
"status"
]
=
Recorder
::
isRecording
((
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
(),
allArgs
[
"vhost"
],
auto
src
=
MediaSource
::
find
(
allArgs
[
"vhost"
],
allArgs
[
"app"
],
allArgs
[
"stream"
]);
allArgs
[
"app"
],
if
(
!
src
)
{
allArgs
[
"stream"
]);
throw
ApiRetException
(
"can not find the stream"
,
API
::
NotFound
);
}
auto
type
=
(
Recorder
::
type
)
allArgs
[
"type"
].
as
<
int
>
();
src
->
getOwnerPoller
()
->
async
([
=
]()
mutable
{
val
[
"status"
]
=
src
->
isRecording
(
type
);
invoker
(
200
,
headerOut
,
val
.
toStyledString
());
});
});
});
//获取录像文件夹列表或mp4文件列表
//获取录像文件夹列表或mp4文件列表
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论