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
c7225dff
Commit
c7225dff
authored
Aug 01, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
整理rtsp客户端代码
parent
5d436d0a
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
79 行增加
和
76 行删除
+79
-76
src/Rtsp/RtspPlayer.cpp
+56
-55
src/Rtsp/RtspPlayer.h
+23
-21
没有找到文件。
src/Rtsp/RtspPlayer.cpp
查看文件 @
c7225dff
...
@@ -107,13 +107,13 @@ void RtspPlayer::onConnect(const SockException &err){
...
@@ -107,13 +107,13 @@ void RtspPlayer::onConnect(const SockException &err){
sendOptions
();
sendOptions
();
}
}
void
RtspPlayer
::
onRecv
(
const
Buffer
::
Ptr
&
pB
uf
)
{
void
RtspPlayer
::
onRecv
(
const
Buffer
::
Ptr
&
b
uf
)
{
if
(
_benchmark_mode
&&
!
_play_check_timer
){
if
(
_benchmark_mode
&&
!
_play_check_timer
){
//在性能测试模式下,如果rtsp握手完毕后,不再解析rtp包
//在性能测试模式下,如果rtsp握手完毕后,不再解析rtp包
_rtp_recv_ticker
.
resetTime
();
_rtp_recv_ticker
.
resetTime
();
return
;
return
;
}
}
input
(
pBuf
->
data
(),
pB
uf
->
size
());
input
(
buf
->
data
(),
b
uf
->
size
());
}
}
void
RtspPlayer
::
onErr
(
const
SockException
&
ex
)
{
void
RtspPlayer
::
onErr
(
const
SockException
&
ex
)
{
...
@@ -218,9 +218,9 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){
...
@@ -218,9 +218,9 @@ void RtspPlayer::createUdpSockIfNecessary(int track_idx){
}
}
//发送SETUP命令
//发送SETUP命令
void
RtspPlayer
::
sendSetup
(
unsigned
int
track
Inde
x
)
{
void
RtspPlayer
::
sendSetup
(
unsigned
int
track
_id
x
)
{
_on_response
=
std
::
bind
(
&
RtspPlayer
::
handleResSETUP
,
this
,
placeholders
::
_1
,
track
Inde
x
);
_on_response
=
std
::
bind
(
&
RtspPlayer
::
handleResSETUP
,
this
,
placeholders
::
_1
,
track
_id
x
);
auto
&
track
=
_sdp_track
[
track
Inde
x
];
auto
&
track
=
_sdp_track
[
track
_id
x
];
auto
baseUrl
=
_content_base
+
"/"
+
track
->
_control_surffix
;
auto
baseUrl
=
_content_base
+
"/"
+
track
->
_control_surffix
;
switch
(
_rtp_type
)
{
switch
(
_rtp_type
)
{
case
Rtsp
:
:
RTP_TCP
:
{
case
Rtsp
:
:
RTP_TCP
:
{
...
@@ -232,11 +232,11 @@ void RtspPlayer::sendSetup(unsigned int trackIndex) {
...
@@ -232,11 +232,11 @@ void RtspPlayer::sendSetup(unsigned int trackIndex) {
}
}
break
;
break
;
case
Rtsp
:
:
RTP_UDP
:
{
case
Rtsp
:
:
RTP_UDP
:
{
createUdpSockIfNecessary
(
track
Inde
x
);
createUdpSockIfNecessary
(
track
_id
x
);
sendRtspRequest
(
"SETUP"
,
baseUrl
,
{
"Transport"
,
sendRtspRequest
(
"SETUP"
,
baseUrl
,
{
"Transport"
,
StrPrinter
<<
"RTP/AVP;unicast;client_port="
StrPrinter
<<
"RTP/AVP;unicast;client_port="
<<
_rtp_sock
[
track
Inde
x
]
->
get_local_port
()
<<
"-"
<<
_rtp_sock
[
track
_id
x
]
->
get_local_port
()
<<
"-"
<<
_rtcp_sock
[
track
Inde
x
]
->
get_local_port
()});
<<
_rtcp_sock
[
track
_id
x
]
->
get_local_port
()});
}
}
break
;
break
;
default
:
default
:
...
@@ -244,12 +244,12 @@ void RtspPlayer::sendSetup(unsigned int trackIndex) {
...
@@ -244,12 +244,12 @@ void RtspPlayer::sendSetup(unsigned int trackIndex) {
}
}
}
}
void
RtspPlayer
::
handleResSETUP
(
const
Parser
&
parser
,
unsigned
int
uiTrackInde
x
)
{
void
RtspPlayer
::
handleResSETUP
(
const
Parser
&
parser
,
unsigned
int
track_id
x
)
{
if
(
parser
.
Url
()
!=
"200"
)
{
if
(
parser
.
Url
()
!=
"200"
)
{
throw
std
::
runtime_error
(
throw
std
::
runtime_error
(
StrPrinter
<<
"SETUP:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
);
StrPrinter
<<
"SETUP:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
);
}
}
if
(
uiTrackInde
x
==
0
)
{
if
(
track_id
x
==
0
)
{
_session_id
=
parser
[
"Session"
];
_session_id
=
parser
[
"Session"
];
_session_id
.
append
(
";"
);
_session_id
.
append
(
";"
);
_session_id
=
FindField
(
_session_id
.
data
(),
nullptr
,
";"
);
_session_id
=
FindField
(
_session_id
.
data
(),
nullptr
,
";"
);
...
@@ -268,14 +268,14 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
...
@@ -268,14 +268,14 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
if
(
_rtp_type
==
Rtsp
::
RTP_TCP
)
{
if
(
_rtp_type
==
Rtsp
::
RTP_TCP
)
{
string
interleaved
=
FindField
(
FindField
((
strTransport
+
";"
).
data
(),
"interleaved="
,
";"
).
data
(),
NULL
,
"-"
);
string
interleaved
=
FindField
(
FindField
((
strTransport
+
";"
).
data
(),
"interleaved="
,
";"
).
data
(),
NULL
,
"-"
);
_sdp_track
[
uiTrackInde
x
]
->
_interleaved
=
atoi
(
interleaved
.
data
());
_sdp_track
[
track_id
x
]
->
_interleaved
=
atoi
(
interleaved
.
data
());
}
else
{
}
else
{
const
char
*
strPos
=
(
_rtp_type
==
Rtsp
::
RTP_MULTICAST
?
"port="
:
"server_port="
)
;
const
char
*
strPos
=
(
_rtp_type
==
Rtsp
::
RTP_MULTICAST
?
"port="
:
"server_port="
)
;
auto
port_str
=
FindField
((
strTransport
+
";"
).
data
(),
strPos
,
";"
);
auto
port_str
=
FindField
((
strTransport
+
";"
).
data
(),
strPos
,
";"
);
uint16_t
rtp_port
=
atoi
(
FindField
(
port_str
.
data
(),
NULL
,
"-"
).
data
());
uint16_t
rtp_port
=
atoi
(
FindField
(
port_str
.
data
(),
NULL
,
"-"
).
data
());
uint16_t
rtcp_port
=
atoi
(
FindField
(
port_str
.
data
(),
"-"
,
NULL
).
data
());
uint16_t
rtcp_port
=
atoi
(
FindField
(
port_str
.
data
(),
"-"
,
NULL
).
data
());
auto
&
pRtpSockRef
=
_rtp_sock
[
uiTrackInde
x
];
auto
&
pRtpSockRef
=
_rtp_sock
[
track_id
x
];
auto
&
pRtcpSockRef
=
_rtcp_sock
[
uiTrackInde
x
];
auto
&
pRtcpSockRef
=
_rtcp_sock
[
track_id
x
];
if
(
_rtp_type
==
Rtsp
::
RTP_MULTICAST
)
{
if
(
_rtp_type
==
Rtsp
::
RTP_MULTICAST
)
{
//udp组播
//udp组播
...
@@ -290,7 +290,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
...
@@ -290,7 +290,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
SockUtil
::
joinMultiAddr
(
fd
,
multiAddr
.
data
(),
get_local_ip
().
data
());
SockUtil
::
joinMultiAddr
(
fd
,
multiAddr
.
data
(),
get_local_ip
().
data
());
}
}
}
else
{
}
else
{
createUdpSockIfNecessary
(
uiTrackInde
x
);
createUdpSockIfNecessary
(
track_id
x
);
//udp单播
//udp单播
struct
sockaddr_in
rtpto
;
struct
sockaddr_in
rtpto
;
rtpto
.
sin_port
=
ntohs
(
rtp_port
);
rtpto
.
sin_port
=
ntohs
(
rtp_port
);
...
@@ -310,7 +310,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
...
@@ -310,7 +310,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
auto
srcIP
=
inet_addr
(
get_peer_ip
().
data
());
auto
srcIP
=
inet_addr
(
get_peer_ip
().
data
());
weak_ptr
<
RtspPlayer
>
weakSelf
=
dynamic_pointer_cast
<
RtspPlayer
>
(
shared_from_this
());
weak_ptr
<
RtspPlayer
>
weakSelf
=
dynamic_pointer_cast
<
RtspPlayer
>
(
shared_from_this
());
//设置rtp over udp接收回调处理函数
//设置rtp over udp接收回调处理函数
pRtpSockRef
->
setOnRead
([
srcIP
,
uiTrackInde
x
,
weakSelf
](
const
Buffer
::
Ptr
&
buf
,
struct
sockaddr
*
addr
,
int
addr_len
)
{
pRtpSockRef
->
setOnRead
([
srcIP
,
track_id
x
,
weakSelf
](
const
Buffer
::
Ptr
&
buf
,
struct
sockaddr
*
addr
,
int
addr_len
)
{
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
if
(
!
strongSelf
)
{
return
;
return
;
...
@@ -319,13 +319,13 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
...
@@ -319,13 +319,13 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
WarnL
<<
"收到其他地址的rtp数据:"
<<
SockUtil
::
inet_ntoa
(((
struct
sockaddr_in
*
)
addr
)
->
sin_addr
);
WarnL
<<
"收到其他地址的rtp数据:"
<<
SockUtil
::
inet_ntoa
(((
struct
sockaddr_in
*
)
addr
)
->
sin_addr
);
return
;
return
;
}
}
strongSelf
->
handleOneRtp
(
uiTrackIndex
,
strongSelf
->
_sdp_track
[
uiTrackInde
x
]
->
_type
,
strongSelf
->
handleOneRtp
(
track_idx
,
strongSelf
->
_sdp_track
[
track_id
x
]
->
_type
,
strongSelf
->
_sdp_track
[
uiTrackInde
x
]
->
_samplerate
,
(
unsigned
char
*
)
buf
->
data
(),
buf
->
size
());
strongSelf
->
_sdp_track
[
track_id
x
]
->
_samplerate
,
(
unsigned
char
*
)
buf
->
data
(),
buf
->
size
());
});
});
if
(
pRtcpSockRef
)
{
if
(
pRtcpSockRef
)
{
//设置rtcp over udp接收回调处理函数
//设置rtcp over udp接收回调处理函数
pRtcpSockRef
->
setOnRead
([
srcIP
,
uiTrackInde
x
,
weakSelf
](
const
Buffer
::
Ptr
&
buf
,
struct
sockaddr
*
addr
,
int
addr_len
)
{
pRtcpSockRef
->
setOnRead
([
srcIP
,
track_id
x
,
weakSelf
](
const
Buffer
::
Ptr
&
buf
,
struct
sockaddr
*
addr
,
int
addr_len
)
{
auto
strongSelf
=
weakSelf
.
lock
();
auto
strongSelf
=
weakSelf
.
lock
();
if
(
!
strongSelf
)
{
if
(
!
strongSelf
)
{
return
;
return
;
...
@@ -334,14 +334,14 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
...
@@ -334,14 +334,14 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int uiTrackIndex)
WarnL
<<
"收到其他地址的rtcp数据:"
<<
SockUtil
::
inet_ntoa
(((
struct
sockaddr_in
*
)
addr
)
->
sin_addr
);
WarnL
<<
"收到其他地址的rtcp数据:"
<<
SockUtil
::
inet_ntoa
(((
struct
sockaddr_in
*
)
addr
)
->
sin_addr
);
return
;
return
;
}
}
strongSelf
->
onRtcpPacket
(
uiTrackIndex
,
strongSelf
->
_sdp_track
[
uiTrackInde
x
],
(
unsigned
char
*
)
buf
->
data
(),
buf
->
size
());
strongSelf
->
onRtcpPacket
(
track_idx
,
strongSelf
->
_sdp_track
[
track_id
x
],
(
unsigned
char
*
)
buf
->
data
(),
buf
->
size
());
});
});
}
}
}
}
if
(
uiTrackInde
x
<
_sdp_track
.
size
()
-
1
)
{
if
(
track_id
x
<
_sdp_track
.
size
()
-
1
)
{
//需要继续发送SETUP命令
//需要继续发送SETUP命令
sendSetup
(
uiTrackInde
x
+
1
);
sendSetup
(
track_id
x
+
1
);
return
;
return
;
}
}
//所有setup命令发送完毕
//所有setup命令发送完毕
...
@@ -404,8 +404,8 @@ void RtspPlayer::sendPause(int type , uint32_t seekMS){
...
@@ -404,8 +404,8 @@ void RtspPlayer::sendPause(int type , uint32_t seekMS){
}
}
}
}
void
RtspPlayer
::
pause
(
bool
bPause
)
{
void
RtspPlayer
::
pause
(
bool
pause_flag
)
{
sendPause
(
bPause
?
type_pause
:
type_seek
,
getProgressMilliSecond
());
sendPause
(
pause_flag
?
type_pause
:
type_seek
,
getProgressMilliSecond
());
}
}
void
RtspPlayer
::
handleResPAUSE
(
const
Parser
&
parser
,
int
type
)
{
void
RtspPlayer
::
handleResPAUSE
(
const
Parser
&
parser
,
int
type
)
{
...
@@ -416,6 +416,7 @@ void RtspPlayer::handleResPAUSE(const Parser& parser,int type) {
...
@@ -416,6 +416,7 @@ void RtspPlayer::handleResPAUSE(const Parser& parser,int type) {
break
;
break
;
case
type_play
:
case
type_play
:
WarnL
<<
"Play failed:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
;
WarnL
<<
"Play failed:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
;
onPlayResult_l
(
SockException
(
Err_shutdown
,
StrPrinter
<<
"rtsp play failed:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
),
!
_play_check_timer
);
break
;
break
;
case
type_seek
:
case
type_seek
:
WarnL
<<
"Seek failed:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
;
WarnL
<<
"Seek failed:"
<<
parser
.
Url
()
<<
" "
<<
parser
.
Tail
()
<<
endl
;
...
@@ -442,8 +443,8 @@ void RtspPlayer::handleResPAUSE(const Parser& parser,int type) {
...
@@ -442,8 +443,8 @@ void RtspPlayer::handleResPAUSE(const Parser& parser,int type) {
iSeekTo
=
1000
*
atof
(
strStart
.
data
());
iSeekTo
=
1000
*
atof
(
strStart
.
data
());
DebugL
<<
"seekTo(ms):"
<<
iSeekTo
;
DebugL
<<
"seekTo(ms):"
<<
iSeekTo
;
}
}
//设置相对时间戳
onPlayResult_l
(
SockException
(
Err_success
,
type
==
type_seek
?
"resum rtsp success"
:
"rtsp play success"
),
type
==
type_seek
);
onPlayResult_l
(
SockException
(
Err_success
,
type
==
type_seek
?
"resum rtsp success"
:
"rtsp play success"
),
!
_play_check_timer
);
}
}
void
RtspPlayer
::
onWholeRtspPacket
(
Parser
&
parser
)
{
void
RtspPlayer
::
onWholeRtspPacket
(
Parser
&
parser
)
{
...
@@ -473,7 +474,7 @@ void RtspPlayer::onRtpPacket(const char *data, uint64_t len) {
...
@@ -473,7 +474,7 @@ void RtspPlayer::onRtpPacket(const char *data, uint64_t len) {
}
}
//此处预留rtcp处理函数
//此处预留rtcp处理函数
void
RtspPlayer
::
onRtcpPacket
(
int
iTrackidx
,
SdpTrack
::
Ptr
&
track
,
unsigned
char
*
pucData
,
unsigned
int
uiL
en
){}
void
RtspPlayer
::
onRtcpPacket
(
int
track_idx
,
SdpTrack
::
Ptr
&
track
,
unsigned
char
*
data
,
unsigned
int
l
en
){}
#if 0
#if 0
//改代码提取自FFmpeg,参考之
//改代码提取自FFmpeg,参考之
...
@@ -533,12 +534,12 @@ void RtspPlayer::onRtcpPacket(int iTrackidx, SdpTrack::Ptr &track, unsigned char
...
@@ -533,12 +534,12 @@ void RtspPlayer::onRtcpPacket(int iTrackidx, SdpTrack::Ptr &track, unsigned char
avio_w8(pb, 0);
avio_w8(pb, 0);
#endif
#endif
void
RtspPlayer
::
sendReceiverReport
(
bool
over
Tcp
,
int
iTrackInde
x
){
void
RtspPlayer
::
sendReceiverReport
(
bool
over
_tcp
,
int
track_id
x
){
static
const
char
s_cname
[]
=
"ZLMediaKitRtsp"
;
static
const
char
s_cname
[]
=
"ZLMediaKitRtsp"
;
uint8_t
aui8Rtcp
[
4
+
32
+
10
+
sizeof
(
s_cname
)
+
1
]
=
{
0
};
uint8_t
aui8Rtcp
[
4
+
32
+
10
+
sizeof
(
s_cname
)
+
1
]
=
{
0
};
uint8_t
*
pui8Rtcp_RR
=
aui8Rtcp
+
4
,
*
pui8Rtcp_SDES
=
pui8Rtcp_RR
+
32
;
uint8_t
*
pui8Rtcp_RR
=
aui8Rtcp
+
4
,
*
pui8Rtcp_SDES
=
pui8Rtcp_RR
+
32
;
auto
&
track
=
_sdp_track
[
iTrackInde
x
];
auto
&
track
=
_sdp_track
[
track_id
x
];
auto
&
counter
=
_rtcp_counter
[
iTrackInde
x
];
auto
&
counter
=
_rtcp_counter
[
track_id
x
];
aui8Rtcp
[
0
]
=
'$'
;
aui8Rtcp
[
0
]
=
'$'
;
aui8Rtcp
[
1
]
=
track
->
_interleaved
+
1
;
aui8Rtcp
[
1
]
=
track
->
_interleaved
+
1
;
...
@@ -564,13 +565,13 @@ void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
...
@@ -564,13 +565,13 @@ void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
pui8Rtcp_RR
[
15
]
=
0x00
;
pui8Rtcp_RR
[
15
]
=
0x00
;
//FIXME: max sequence received
//FIXME: max sequence received
int
cycleCount
=
getCycleCount
(
iTrackInde
x
);
int
cycleCount
=
getCycleCount
(
track_id
x
);
pui8Rtcp_RR
[
16
]
=
cycleCount
>>
8
;
pui8Rtcp_RR
[
16
]
=
cycleCount
>>
8
;
pui8Rtcp_RR
[
17
]
=
cycleCount
&
0xFF
;
pui8Rtcp_RR
[
17
]
=
cycleCount
&
0xFF
;
pui8Rtcp_RR
[
18
]
=
counter
.
pktCnt
>>
8
;
pui8Rtcp_RR
[
18
]
=
counter
.
pktCnt
>>
8
;
pui8Rtcp_RR
[
19
]
=
counter
.
pktCnt
&
0xFF
;
pui8Rtcp_RR
[
19
]
=
counter
.
pktCnt
&
0xFF
;
uint32_t
jitter
=
htonl
(
getJitterSize
(
iTrackInde
x
));
uint32_t
jitter
=
htonl
(
getJitterSize
(
track_id
x
));
//FIXME: jitter
//FIXME: jitter
memcpy
(
pui8Rtcp_RR
+
20
,
&
jitter
,
4
);
memcpy
(
pui8Rtcp_RR
+
20
,
&
jitter
,
4
);
/* last SR timestamp */
/* last SR timestamp */
...
@@ -592,10 +593,10 @@ void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
...
@@ -592,10 +593,10 @@ void RtspPlayer::sendReceiverReport(bool overTcp,int iTrackIndex){
memcpy
(
&
pui8Rtcp_SDES
[
10
],
s_cname
,
sizeof
(
s_cname
));
memcpy
(
&
pui8Rtcp_SDES
[
10
],
s_cname
,
sizeof
(
s_cname
));
pui8Rtcp_SDES
[
10
+
sizeof
(
s_cname
)]
=
0x00
;
pui8Rtcp_SDES
[
10
+
sizeof
(
s_cname
)]
=
0x00
;
if
(
overTcp
)
{
if
(
over_tcp
)
{
send
(
obtainBuffer
((
char
*
)
aui8Rtcp
,
sizeof
(
aui8Rtcp
)));
send
(
obtainBuffer
((
char
*
)
aui8Rtcp
,
sizeof
(
aui8Rtcp
)));
}
else
if
(
_rtcp_sock
[
iTrackInde
x
])
{
}
else
if
(
_rtcp_sock
[
track_id
x
])
{
_rtcp_sock
[
iTrackInde
x
]
->
send
((
char
*
)
aui8Rtcp
+
4
,
sizeof
(
aui8Rtcp
)
-
4
);
_rtcp_sock
[
track_id
x
]
->
send
((
char
*
)
aui8Rtcp
+
4
,
sizeof
(
aui8Rtcp
)
-
4
);
}
}
}
}
...
@@ -613,24 +614,24 @@ void RtspPlayer::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx){
...
@@ -613,24 +614,24 @@ void RtspPlayer::onRtpSorted(const RtpPacket::Ptr &rtppt, int trackidx){
}
}
float
RtspPlayer
::
getPacketLossRate
(
TrackType
type
)
const
{
float
RtspPlayer
::
getPacketLossRate
(
TrackType
type
)
const
{
int
iTrackIdx
=
getTrackIndexByTrackType
(
type
);
try
{
if
(
iTrackIdx
==
-
1
){
auto
track_idx
=
getTrackIndexByTrackType
(
type
);
if
(
_rtp_seq_now
[
track_idx
]
-
_rtp_seq_start
[
track_idx
]
+
1
==
0
)
{
return
0
;
}
return
1.0
-
(
double
)
_rtp_recv_count
[
track_idx
]
/
(
_rtp_seq_now
[
track_idx
]
-
_rtp_seq_start
[
track_idx
]
+
1
);
}
catch
(...)
{
uint64_t
totalRecv
=
0
;
uint64_t
totalRecv
=
0
;
uint64_t
totalSend
=
0
;
uint64_t
totalSend
=
0
;
for
(
unsigned
int
i
=
0
;
i
<
_sdp_track
.
size
();
i
++
)
{
for
(
unsigned
int
i
=
0
;
i
<
_sdp_track
.
size
();
i
++
)
{
totalRecv
+=
_rtp_recv_count
[
i
];
totalRecv
+=
_rtp_recv_count
[
i
];
totalSend
+=
(
_rtp_seq_now
[
i
]
-
_rtp_seq_start
[
i
]
+
1
);
totalSend
+=
(
_rtp_seq_now
[
i
]
-
_rtp_seq_start
[
i
]
+
1
);
}
}
if
(
totalSend
==
0
)
{
if
(
totalSend
==
0
)
{
return
0
;
return
0
;
}
}
return
1.0
-
(
double
)
totalRecv
/
totalSend
;
return
1.0
-
(
double
)
totalRecv
/
totalSend
;
}
if
(
_rtp_seq_now
[
iTrackIdx
]
-
_rtp_seq_start
[
iTrackIdx
]
+
1
==
0
){
return
0
;
}
}
return
1.0
-
(
double
)
_rtp_recv_count
[
iTrackIdx
]
/
(
_rtp_seq_now
[
iTrackIdx
]
-
_rtp_seq_start
[
iTrackIdx
]
+
1
);
}
}
uint32_t
RtspPlayer
::
getProgressMilliSecond
()
const
{
uint32_t
RtspPlayer
::
getProgressMilliSecond
()
const
{
...
@@ -705,26 +706,26 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
...
@@ -705,26 +706,26 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
SockSender
::
send
(
printer
<<
"
\r\n
"
);
SockSender
::
send
(
printer
<<
"
\r\n
"
);
}
}
void
RtspPlayer
::
onRecvRTP_l
(
const
RtpPacket
::
Ptr
&
pkt
,
const
SdpTrack
::
Ptr
&
track
)
{
void
RtspPlayer
::
onRecvRTP_l
(
const
RtpPacket
::
Ptr
&
rtp
,
const
SdpTrack
::
Ptr
&
track
)
{
_rtp_recv_ticker
.
resetTime
();
_rtp_recv_ticker
.
resetTime
();
onRecvRTP
(
pkt
,
track
);
onRecvRTP
(
rtp
,
track
);
int
iTrackIndex
=
getTrackIndexByTrackType
(
pkt
->
type
);
int
track_idx
=
getTrackIndexByTrackType
(
rtp
->
type
);
RtcpCounter
&
counter
=
_rtcp_counter
[
iTrackInde
x
];
RtcpCounter
&
counter
=
_rtcp_counter
[
track_id
x
];
counter
.
pktCnt
=
pkt
->
sequence
;
counter
.
pktCnt
=
rtp
->
sequence
;
auto
&
ticker
=
_rtcp_send_ticker
[
iTrackInde
x
];
auto
&
ticker
=
_rtcp_send_ticker
[
track_id
x
];
if
(
ticker
.
elapsedTime
()
>
5
*
1000
)
{
if
(
ticker
.
elapsedTime
()
>
5
*
1000
)
{
//send rtcp every 5 second
//send rtcp every 5 second
counter
.
lastTimeStamp
=
counter
.
timeStamp
;
counter
.
lastTimeStamp
=
counter
.
timeStamp
;
//直接保存网络字节序
//直接保存网络字节序
memcpy
(
&
counter
.
timeStamp
,
pkt
->
data
()
+
8
,
4
);
memcpy
(
&
counter
.
timeStamp
,
rtp
->
data
()
+
8
,
4
);
if
(
counter
.
lastTimeStamp
!=
0
)
{
if
(
counter
.
lastTimeStamp
!=
0
)
{
sendReceiverReport
(
_rtp_type
==
Rtsp
::
RTP_TCP
,
iTrackInde
x
);
sendReceiverReport
(
_rtp_type
==
Rtsp
::
RTP_TCP
,
track_id
x
);
ticker
.
resetTime
();
ticker
.
resetTime
();
}
}
//有些rtsp服务器需要rtcp保活,有些需要发送信令保活
//有些rtsp服务器需要rtcp保活,有些需要发送信令保活
if
(
iTrackInde
x
==
0
)
{
if
(
track_id
x
==
0
)
{
//只需要发送一次心跳信令包
//只需要发送一次心跳信令包
sendKeepAlive
();
sendKeepAlive
();
}
}
...
@@ -785,16 +786,16 @@ int RtspPlayer::getTrackIndexByInterleaved(int interleaved) const {
...
@@ -785,16 +786,16 @@ int RtspPlayer::getTrackIndexByInterleaved(int interleaved) const {
throw
SockException
(
Err_shutdown
,
StrPrinter
<<
"no such track with interleaved:"
<<
interleaved
);
throw
SockException
(
Err_shutdown
,
StrPrinter
<<
"no such track with interleaved:"
<<
interleaved
);
}
}
int
RtspPlayer
::
getTrackIndexByTrackType
(
TrackType
track
T
ype
)
const
{
int
RtspPlayer
::
getTrackIndexByTrackType
(
TrackType
track
_t
ype
)
const
{
for
(
unsigned
int
i
=
0
;
i
<
_sdp_track
.
size
();
i
++
)
{
for
(
unsigned
int
i
=
0
;
i
<
_sdp_track
.
size
();
i
++
)
{
if
(
_sdp_track
[
i
]
->
_type
==
track
T
ype
)
{
if
(
_sdp_track
[
i
]
->
_type
==
track
_t
ype
)
{
return
i
;
return
i
;
}
}
}
}
if
(
_sdp_track
.
size
()
==
1
)
{
if
(
_sdp_track
.
size
()
==
1
)
{
return
0
;
return
0
;
}
}
throw
SockException
(
Err_shutdown
,
StrPrinter
<<
"no such track with type:"
<<
(
int
)
track
T
ype
);
throw
SockException
(
Err_shutdown
,
StrPrinter
<<
"no such track with type:"
<<
(
int
)
track
_t
ype
);
}
}
}
/* namespace mediakit */
}
/* namespace mediakit */
src/Rtsp/RtspPlayer.h
查看文件 @
c7225dff
...
@@ -36,16 +36,18 @@ class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, publi
...
@@ -36,16 +36,18 @@ class RtspPlayer: public PlayerBase,public TcpClient, public RtspSplitter, publi
public
:
public
:
typedef
std
::
shared_ptr
<
RtspPlayer
>
Ptr
;
typedef
std
::
shared_ptr
<
RtspPlayer
>
Ptr
;
RtspPlayer
(
const
EventPoller
::
Ptr
&
poller
)
;
RtspPlayer
(
const
EventPoller
::
Ptr
&
poller
);
virtual
~
RtspPlayer
(
void
);
~
RtspPlayer
()
override
;
void
play
(
const
string
&
strUrl
)
override
;
void
play
(
const
string
&
strUrl
)
override
;
void
pause
(
bool
bPause
)
override
;
void
pause
(
bool
pause_flag
)
override
;
void
teardown
()
override
;
void
teardown
()
override
;
float
getPacketLossRate
(
TrackType
type
)
const
override
;
float
getPacketLossRate
(
TrackType
type
)
const
override
;
protected
:
protected
:
//派生类回调函数
//派生类回调函数
virtual
bool
onCheckSDP
(
const
string
&
s
trS
dp
)
=
0
;
virtual
bool
onCheckSDP
(
const
string
&
sdp
)
=
0
;
virtual
void
onRecvRTP
(
const
RtpPacket
::
Ptr
&
pRtppt
,
const
SdpTrack
::
Ptr
&
track
)
=
0
;
virtual
void
onRecvRTP
(
const
RtpPacket
::
Ptr
&
rtp
,
const
SdpTrack
::
Ptr
&
track
)
=
0
;
uint32_t
getProgressMilliSecond
()
const
;
uint32_t
getProgressMilliSecond
()
const
;
void
seekToMilliSecond
(
uint32_t
ms
);
void
seekToMilliSecond
(
uint32_t
ms
);
...
@@ -64,47 +66,48 @@ protected:
...
@@ -64,47 +66,48 @@ protected:
/**
/**
* rtp数据包排序后输出
* rtp数据包排序后输出
* @param rtp
pt
rtp数据包
* @param rtp rtp数据包
* @param trackidx track索引
* @param track
_
idx track索引
*/
*/
void
onRtpSorted
(
const
RtpPacket
::
Ptr
&
rtppt
,
int
trackidx
)
override
;
void
onRtpSorted
(
const
RtpPacket
::
Ptr
&
rtp
,
int
track_idx
)
override
;
/**
/**
* 收到RTCP包回调
* 收到RTCP包回调
* @param
iTrackidx
* @param
track_idx track索引
* @param track
* @param track
sdp相关信息
* @param
pucData
* @param
data rtcp内容
* @param
uiLen
* @param
len rtcp内容长度
*/
*/
virtual
void
onRtcpPacket
(
int
iTrackidx
,
SdpTrack
::
Ptr
&
track
,
unsigned
char
*
pucData
,
unsigned
int
uiL
en
);
virtual
void
onRtcpPacket
(
int
track_idx
,
SdpTrack
::
Ptr
&
track
,
unsigned
char
*
data
,
unsigned
int
l
en
);
/////////////TcpClient override/////////////
/////////////TcpClient override/////////////
void
onConnect
(
const
SockException
&
err
)
override
;
void
onConnect
(
const
SockException
&
err
)
override
;
void
onRecv
(
const
Buffer
::
Ptr
&
pB
uf
)
override
;
void
onRecv
(
const
Buffer
::
Ptr
&
b
uf
)
override
;
void
onErr
(
const
SockException
&
ex
)
override
;
void
onErr
(
const
SockException
&
ex
)
override
;
private
:
private
:
void
onRecvRTP_l
(
const
RtpPacket
::
Ptr
&
pRtppt
,
const
SdpTrack
::
Ptr
&
track
);
void
onRecvRTP_l
(
const
RtpPacket
::
Ptr
&
rtp
,
const
SdpTrack
::
Ptr
&
track
);
void
onPlayResult_l
(
const
SockException
&
ex
,
bool
handshakeCompleted
);
void
onPlayResult_l
(
const
SockException
&
ex
,
bool
handshakeCompleted
);
int
getTrackIndexByInterleaved
(
int
interleaved
)
const
;
int
getTrackIndexByInterleaved
(
int
interleaved
)
const
;
int
getTrackIndexByTrackType
(
TrackType
track
T
ype
)
const
;
int
getTrackIndexByTrackType
(
TrackType
track
_t
ype
)
const
;
void
handleResSETUP
(
const
Parser
&
parser
,
unsigned
int
uiTrackInde
x
);
void
handleResSETUP
(
const
Parser
&
parser
,
unsigned
int
track_id
x
);
void
handleResDESCRIBE
(
const
Parser
&
parser
);
void
handleResDESCRIBE
(
const
Parser
&
parser
);
bool
handleAuthenticationFailure
(
const
string
&
wwwAuthenticateParamsStr
);
bool
handleAuthenticationFailure
(
const
string
&
wwwAuthenticateParamsStr
);
void
handleResPAUSE
(
const
Parser
&
parser
,
int
type
);
void
handleResPAUSE
(
const
Parser
&
parser
,
int
type
);
bool
handleResponse
(
const
string
&
cmd
,
const
Parser
&
parser
);
bool
handleResponse
(
const
string
&
cmd
,
const
Parser
&
parser
);
void
sendOptions
();
void
sendOptions
();
void
sendSetup
(
unsigned
int
uiTrackInde
x
);
void
sendSetup
(
unsigned
int
track_id
x
);
void
sendPause
(
int
type
,
uint32_t
ms
);
void
sendPause
(
int
type
,
uint32_t
ms
);
void
sendDescribe
();
void
sendDescribe
();
void
sendKeepAlive
();
void
sendKeepAlive
();
void
sendRtspRequest
(
const
string
&
cmd
,
const
string
&
url
,
const
StrCaseMap
&
header
=
StrCaseMap
());
void
sendRtspRequest
(
const
string
&
cmd
,
const
string
&
url
,
const
StrCaseMap
&
header
=
StrCaseMap
());
void
sendRtspRequest
(
const
string
&
cmd
,
const
string
&
url
,
const
std
::
initializer_list
<
string
>
&
header
);
void
sendRtspRequest
(
const
string
&
cmd
,
const
string
&
url
,
const
std
::
initializer_list
<
string
>
&
header
);
void
sendReceiverReport
(
bool
over
Tcp
,
int
iTrackInde
x
);
void
sendReceiverReport
(
bool
over
_tcp
,
int
track_id
x
);
void
createUdpSockIfNecessary
(
int
track_idx
);
void
createUdpSockIfNecessary
(
int
track_idx
);
private
:
private
:
string
_play_url
;
string
_play_url
;
vector
<
SdpTrack
::
Ptr
>
_sdp_track
;
vector
<
SdpTrack
::
Ptr
>
_sdp_track
;
...
@@ -148,5 +151,4 @@ private:
...
@@ -148,5 +151,4 @@ private:
};
};
}
/* namespace mediakit */
}
/* namespace mediakit */
#endif
/* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */
#endif
/* SRC_RTSPPLAYER_RTSPPLAYER_H_TXT_ */
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论