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
b5c1726c
Commit
b5c1726c
authored
Mar 14, 2018
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rtmp未找到流后延迟重试
parent
36f688f1
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
134 行增加
和
96 行删除
+134
-96
src/Rtmp/RtmpSession.cpp
+127
-96
src/Rtmp/RtmpSession.h
+7
-0
没有找到文件。
src/Rtmp/RtmpSession.cpp
查看文件 @
b5c1726c
...
@@ -75,6 +75,12 @@ void RtmpSession::onManager() {
...
@@ -75,6 +75,12 @@ void RtmpSession::onManager() {
shutdown
();
shutdown
();
}
}
}
}
if
(
m_delayTask
){
if
(
time
(
NULL
)
>
m_iTaskTimeLine
){
m_delayTask
();
m_delayTask
=
nullptr
;
}
}
}
}
void
RtmpSession
::
onRecv
(
const
Buffer
::
Ptr
&
pBuf
)
{
void
RtmpSession
::
onRecv
(
const
Buffer
::
Ptr
&
pBuf
)
{
...
@@ -196,128 +202,146 @@ void RtmpSession::onCmd_deleteStream(AMFDecoder &dec) {
...
@@ -196,128 +202,146 @@ void RtmpSession::onCmd_deleteStream(AMFDecoder &dec) {
throw
std
::
runtime_error
(
StrPrinter
<<
"Stop publishing."
<<
endl
);
throw
std
::
runtime_error
(
StrPrinter
<<
"Stop publishing."
<<
endl
);
}
}
void
RtmpSession
::
doPlay
(
AMFDecoder
&
dec
)
{
void
RtmpSession
::
doPlayResponse
(
const
string
&
err
,
bool
tryDelay
)
{
auto
onRes
=
[
this
](
const
string
&
err
)
{
//获取流对象
auto
src
=
dynamic_pointer_cast
<
RtmpMediaSource
>
(
MediaSource
::
find
(
RTMP_SCHEMA
,
auto
src
=
dynamic_pointer_cast
<
RtmpMediaSource
>
(
MediaSource
::
find
(
RTMP_SCHEMA
,
m_mediaInfo
.
m_vhost
,
m_mediaInfo
.
m_vhost
,
m_mediaInfo
.
m_app
,
m_mediaInfo
.
m_app
,
m_mediaInfo
.
m_streamid
,
m_mediaInfo
.
m_streamid
,
true
));
true
));
bool
authSuccess
=
err
.
empty
();
if
(
src
){
bool
ok
=
(
src
.
operator
bool
()
&&
authSuccess
);
if
(
!
src
->
ready
()){
if
(
ok
){
//流未准备好那么相当于没有
ok
=
ok
&&
src
->
ready
()
;
src
=
nullptr
;
}
}
if
(
ok
)
{
}
//stream begin
sendUserControl
(
CONTROL_STREAM_BEGIN
,
STREAM_MEDIA
);
}
// onStatus(NetStream.Play.Reset)
AMFValue
status
(
AMF_OBJECT
);
status
.
set
(
"level"
,
ok
?
"status"
:
"error"
);
status
.
set
(
"code"
,
ok
?
"NetStream.Play.Reset"
:
(
authSuccess
?
"NetStream.Play.StreamNotFound"
:
"NetStream.Play.BadAuth"
));
status
.
set
(
"description"
,
ok
?
"Resetting and playing."
:
(
authSuccess
?
"No such stream."
:
err
.
data
()));
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
status
.
set
(
"clientid"
,
"0"
);
sendReply
(
"onStatus"
,
nullptr
,
status
);
if
(
!
ok
)
{
WarnL
<<
"onPlayed:"
<<
(
authSuccess
?
"No such stream:"
:
err
.
data
())
<<
" "
<<
m_mediaInfo
.
m_vhost
<<
" "
<<
m_mediaInfo
.
m_app
<<
" "
<<
m_mediaInfo
.
m_streamid
<<
endl
;
shutdown
();
return
;
}
// onStatus(NetStream.Play.Start)
status
.
clear
();
status
.
set
(
"level"
,
"status"
);
status
.
set
(
"code"
,
"NetStream.Play.Start"
);
status
.
set
(
"description"
,
"Started playing."
);
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
status
.
set
(
"clientid"
,
"0"
);
sendReply
(
"onStatus"
,
nullptr
,
status
);
// |RtmpSampleAccess(true, true)
//是否鉴权成功
AMFEncoder
invoke
;
bool
authSuccess
=
err
.
empty
();
invoke
<<
"|RtmpSampleAccess"
<<
true
<<
true
;
if
(
authSuccess
&&
!
src
&&
tryDelay
){
sendResponse
(
MSG_DATA
,
invoke
.
data
());
//校验成功,但是流不存在而导致的不能播放,我们看看该流延时几秒后是否确实不能播放
doDelay
(
3
,[
this
](){
//onStatus(NetStream.Data.Start)
//延时后就不再延时重试了
invoke
.
clear
();
doPlayResponse
(
""
,
false
);
AMFValue
obj
(
AMF_OBJECT
);
});
obj
.
set
(
"code"
,
"NetStream.Data.Start"
);
return
;
invoke
<<
"onStatus"
<<
obj
;
}
sendResponse
(
MSG_DATA
,
invoke
.
data
());
//onStatus(NetStream.Play.PublishNotify)
status
.
clear
();
status
.
set
(
"level"
,
"status"
);
status
.
set
(
"code"
,
"NetStream.Play.PublishNotify"
);
status
.
set
(
"description"
,
"Now published."
);
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
status
.
set
(
"clientid"
,
"0"
);
sendReply
(
"onStatus"
,
nullptr
,
status
);
// onMetaData
///////回复流程///////
invoke
.
clear
();
invoke
<<
"onMetaData"
<<
src
->
getMetaData
();
sendResponse
(
MSG_DATA
,
invoke
.
data
());
src
->
getConfigFrame
([
&
](
const
RtmpPacket
::
Ptr
&
pkt
)
{
//是否播放成功
//DebugL<<"send initial frame";
bool
ok
=
(
src
.
operator
bool
()
&&
authSuccess
);
onSendMedia
(
pkt
);
if
(
ok
)
{
});
//stream begin
sendUserControl
(
CONTROL_STREAM_BEGIN
,
STREAM_MEDIA
);
}
// onStatus(NetStream.Play.Reset)
AMFValue
status
(
AMF_OBJECT
);
status
.
set
(
"level"
,
ok
?
"status"
:
"error"
);
status
.
set
(
"code"
,
ok
?
"NetStream.Play.Reset"
:
(
authSuccess
?
"NetStream.Play.StreamNotFound"
:
"NetStream.Play.BadAuth"
));
status
.
set
(
"description"
,
ok
?
"Resetting and playing."
:
(
authSuccess
?
"No such stream."
:
err
.
data
()));
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
status
.
set
(
"clientid"
,
"0"
);
sendReply
(
"onStatus"
,
nullptr
,
status
);
if
(
!
ok
)
{
WarnL
<<
"onPlayed:"
<<
(
authSuccess
?
"No such stream:"
:
err
.
data
())
<<
" "
<<
m_mediaInfo
.
m_vhost
<<
" "
<<
m_mediaInfo
.
m_app
<<
" "
<<
m_mediaInfo
.
m_streamid
<<
endl
;
shutdown
();
return
;
}
m_pRingReader
=
src
->
getRing
()
->
attach
();
// onStatus(NetStream.Play.Start)
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
status
.
clear
();
SockUtil
::
setNoDelay
(
_sock
->
rawFD
(),
false
);
status
.
set
(
"level"
,
"status"
);
m_pRingReader
->
setReadCB
([
weakSelf
](
const
RtmpPacket
::
Ptr
&
pkt
)
{
status
.
set
(
"code"
,
"NetStream.Play.Start"
);
auto
strongSelf
=
weakSelf
.
lock
();
status
.
set
(
"description"
,
"Started playing."
);
if
(
!
strongSelf
)
{
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
return
;
status
.
set
(
"clientid"
,
"0"
);
}
sendReply
(
"onStatus"
,
nullptr
,
status
);
strongSelf
->
async
([
weakSelf
,
pkt
]()
{
auto
strongSelf
=
weakSelf
.
lock
();
// |RtmpSampleAccess(true, true)
if
(
!
strongSelf
)
{
AMFEncoder
invoke
;
return
;
invoke
<<
"|RtmpSampleAccess"
<<
true
<<
true
;
}
sendResponse
(
MSG_DATA
,
invoke
.
data
());
strongSelf
->
onSendMedia
(
pkt
);
});
//onStatus(NetStream.Data.Start)
});
invoke
.
clear
();
m_pRingReader
->
setDetachCB
([
weakSelf
]()
{
AMFValue
obj
(
AMF_OBJECT
);
obj
.
set
(
"code"
,
"NetStream.Data.Start"
);
invoke
<<
"onStatus"
<<
obj
;
sendResponse
(
MSG_DATA
,
invoke
.
data
());
//onStatus(NetStream.Play.PublishNotify)
status
.
clear
();
status
.
set
(
"level"
,
"status"
);
status
.
set
(
"code"
,
"NetStream.Play.PublishNotify"
);
status
.
set
(
"description"
,
"Now published."
);
status
.
set
(
"details"
,
m_mediaInfo
.
m_streamid
);
status
.
set
(
"clientid"
,
"0"
);
sendReply
(
"onStatus"
,
nullptr
,
status
);
// onMetaData
invoke
.
clear
();
invoke
<<
"onMetaData"
<<
src
->
getMetaData
();
sendResponse
(
MSG_DATA
,
invoke
.
data
());
src
->
getConfigFrame
([
&
](
const
RtmpPacket
::
Ptr
&
pkt
)
{
//DebugL<<"send initial frame";
onSendMedia
(
pkt
);
});
m_pRingReader
=
src
->
getRing
()
->
attach
();
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
SockUtil
::
setNoDelay
(
_sock
->
rawFD
(),
false
);
m_pRingReader
->
setReadCB
([
weakSelf
](
const
RtmpPacket
::
Ptr
&
pkt
)
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
;
}
strongSelf
->
async
([
weakSelf
,
pkt
]()
{
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
if
(
!
strongSelf
)
{
return
;
return
;
}
}
strongSelf
->
safeShutdown
(
);
strongSelf
->
onSendMedia
(
pkt
);
});
});
m_pPlayerSrc
=
src
;
});
if
(
src
->
getRing
()
->
readerCount
()
==
1
)
{
m_pRingReader
->
setDetachCB
([
weakSelf
]()
{
src
->
seekTo
(
0
);
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
return
;
}
}
};
strongSelf
->
safeShutdown
();
});
m_pPlayerSrc
=
src
;
if
(
src
->
getRing
()
->
readerCount
()
==
1
)
{
src
->
seekTo
(
0
);
}
}
void
RtmpSession
::
doPlay
(
AMFDecoder
&
dec
){
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
weak_ptr
<
RtmpSession
>
weakSelf
=
dynamic_pointer_cast
<
RtmpSession
>
(
shared_from_this
());
Broadcast
::
AuthInvoker
invoker
=
[
weakSelf
,
onRes
](
const
string
&
err
){
Broadcast
::
AuthInvoker
invoker
=
[
weakSelf
](
const
string
&
err
){
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
){
if
(
!
strongSelf
){
return
;
return
;
}
}
strongSelf
->
async
([
weakSelf
,
onRes
,
err
](){
strongSelf
->
async
([
weakSelf
,
err
](){
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
){
if
(
!
strongSelf
){
return
;
return
;
}
}
onRes
(
err
);
strongSelf
->
doPlayResponse
(
err
,
true
);
});
});
};
};
auto
flag
=
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastMediaPlayed
,
m_mediaInfo
,
invoker
,
*
this
);
auto
flag
=
NoticeCenter
::
Instance
().
emitEvent
(
Broadcast
::
kBroadcastMediaPlayed
,
m_mediaInfo
,
invoker
,
*
this
);
if
(
!
flag
){
if
(
!
flag
){
//该事件无人监听,默认不鉴权
//该事件无人监听,默认不鉴权
onRes
(
""
);
doPlayResponse
(
""
,
true
);
}
}
}
}
void
RtmpSession
::
onCmd_play2
(
AMFDecoder
&
dec
)
{
void
RtmpSession
::
onCmd_play2
(
AMFDecoder
&
dec
)
{
...
@@ -411,6 +435,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) {
...
@@ -411,6 +435,7 @@ void RtmpSession::onRtmpChunk(RtmpPacket &chunkData) {
if
(
!
m_pPublisherSrc
)
{
if
(
!
m_pPublisherSrc
)
{
throw
std
::
runtime_error
(
"Not a rtmp publisher!"
);
throw
std
::
runtime_error
(
"Not a rtmp publisher!"
);
}
}
chunkData
.
timeStamp
=
m_stampTicker
[
chunkData
.
typeId
%
2
].
elapsedTime
();
m_pPublisherSrc
->
onGetMedia
(
std
::
make_shared
<
RtmpPacket
>
(
chunkData
));
m_pPublisherSrc
->
onGetMedia
(
std
::
make_shared
<
RtmpPacket
>
(
chunkData
));
if
(
!
m_bPublisherSrcRegisted
&&
m_pPublisherSrc
->
ready
()){
if
(
!
m_bPublisherSrcRegisted
&&
m_pPublisherSrc
->
ready
()){
m_bPublisherSrcRegisted
=
true
;
m_bPublisherSrcRegisted
=
true
;
...
@@ -457,5 +482,11 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
...
@@ -457,5 +482,11 @@ void RtmpSession::onSendMedia(const RtmpPacket::Ptr &pkt) {
sendRtmp
(
pkt
->
typeId
,
pkt
->
streamId
,
pkt
->
strBuf
,
modifiedStamp
,
pkt
->
chunkId
,
true
);
sendRtmp
(
pkt
->
typeId
,
pkt
->
streamId
,
pkt
->
strBuf
,
modifiedStamp
,
pkt
->
chunkId
,
true
);
}
}
void
RtmpSession
::
doDelay
(
int
delaySec
,
const
std
::
function
<
void
()
>
&
fun
)
{
m_delayTask
=
fun
;
m_iTaskTimeLine
=
time
(
NULL
)
+
delaySec
;
}
}
/* namespace Rtmp */
}
/* namespace Rtmp */
}
/* namespace ZL */
}
/* namespace ZL */
src/Rtmp/RtmpSession.h
查看文件 @
b5c1726c
...
@@ -57,6 +57,7 @@ private:
...
@@ -57,6 +57,7 @@ private:
MediaInfo
m_mediaInfo
;
MediaInfo
m_mediaInfo
;
double
m_dNowReqID
=
0
;
double
m_dNowReqID
=
0
;
Ticker
m_ticker
;
//数据接收时间
Ticker
m_ticker
;
//数据接收时间
SmoothTicker
m_stampTicker
[
2
];
//时间戳生产器
typedef
void
(
RtmpSession
::*
rtmpCMDHandle
)(
AMFDecoder
&
dec
);
typedef
void
(
RtmpSession
::*
rtmpCMDHandle
)(
AMFDecoder
&
dec
);
static
unordered_map
<
string
,
rtmpCMDHandle
>
g_mapCmd
;
static
unordered_map
<
string
,
rtmpCMDHandle
>
g_mapCmd
;
...
@@ -77,6 +78,7 @@ private:
...
@@ -77,6 +78,7 @@ private:
void
onCmd_play
(
AMFDecoder
&
dec
);
void
onCmd_play
(
AMFDecoder
&
dec
);
void
onCmd_play2
(
AMFDecoder
&
dec
);
void
onCmd_play2
(
AMFDecoder
&
dec
);
void
doPlay
(
AMFDecoder
&
dec
);
void
doPlay
(
AMFDecoder
&
dec
);
void
doPlayResponse
(
const
string
&
err
,
bool
tryDelay
);
void
onCmd_seek
(
AMFDecoder
&
dec
);
void
onCmd_seek
(
AMFDecoder
&
dec
);
void
onCmd_pause
(
AMFDecoder
&
dec
);
void
onCmd_pause
(
AMFDecoder
&
dec
);
void
setMetaData
(
AMFDecoder
&
dec
);
void
setMetaData
(
AMFDecoder
&
dec
);
...
@@ -104,6 +106,11 @@ private:
...
@@ -104,6 +106,11 @@ private:
safeShutdown
();
safeShutdown
();
return
true
;
return
true
;
}
}
void
doDelay
(
int
delaySec
,
const
std
::
function
<
void
()
>
&
fun
);
std
::
function
<
void
()
>
m_delayTask
;
uint32_t
m_iTaskTimeLine
=
0
;
};
};
}
/* namespace Rtmp */
}
/* namespace Rtmp */
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论