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
130a0689
Commit
130a0689
authored
Apr 03, 2021
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
整理并注释代码
parent
cfda7d8b
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
47 行增加
和
31 行删除
+47
-31
webrtc/WebRtcTransport.cpp
+47
-31
没有找到文件。
webrtc/WebRtcTransport.cpp
查看文件 @
130a0689
...
@@ -164,12 +164,16 @@ void WebRtcTransport::inputSockData(char *buf, size_t len, RTC::TransportTuple *
...
@@ -164,12 +164,16 @@ void WebRtcTransport::inputSockData(char *buf, size_t len, RTC::TransportTuple *
if
(
is_rtp
(
buf
))
{
if
(
is_rtp
(
buf
))
{
if
(
_srtp_session_recv
->
DecryptSrtp
((
uint8_t
*
)
buf
,
&
len
))
{
if
(
_srtp_session_recv
->
DecryptSrtp
((
uint8_t
*
)
buf
,
&
len
))
{
onRtp
(
buf
,
len
);
onRtp
(
buf
,
len
);
}
else
{
WarnL
;
}
}
return
;
return
;
}
}
if
(
is_rtcp
(
buf
))
{
if
(
is_rtcp
(
buf
))
{
if
(
_srtp_session_recv
->
DecryptSrtcp
((
uint8_t
*
)
buf
,
&
len
))
{
if
(
_srtp_session_recv
->
DecryptSrtcp
((
uint8_t
*
)
buf
,
&
len
))
{
onRtcp
(
buf
,
len
);
onRtcp
(
buf
,
len
);
}
else
{
WarnL
;
}
}
return
;
return
;
}
}
...
@@ -230,6 +234,18 @@ void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sock
...
@@ -230,6 +234,18 @@ void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sock
_socket
->
send
(
ptr
,
(
struct
sockaddr
*
)(
dst
),
sizeof
(
struct
sockaddr
),
flush
);
_socket
->
send
(
ptr
,
(
struct
sockaddr
*
)(
dst
),
sizeof
(
struct
sockaddr
),
flush
);
}
}
///////////////////////////////////////////////////////////////////
bool
WebRtcTransportImp
::
canSendRtp
()
const
{
auto
&
sdp
=
getSdp
(
SdpType
::
answer
);
return
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendrecv
||
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendonly
;
}
bool
WebRtcTransportImp
::
canRecvRtp
()
const
{
auto
&
sdp
=
getSdp
(
SdpType
::
answer
);
return
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendrecv
||
sdp
.
media
[
0
].
direction
==
RtpDirection
::
recvonly
;
}
void
WebRtcTransportImp
::
onStartWebRTC
()
{
void
WebRtcTransportImp
::
onStartWebRTC
()
{
if
(
canRecvRtp
())
{
if
(
canRecvRtp
())
{
_push_src
=
std
::
make_shared
<
RtspMediaSourceImp
>
(
DEFAULT_VHOST
,
"live"
,
"push"
);
_push_src
=
std
::
make_shared
<
RtspMediaSourceImp
>
(
DEFAULT_VHOST
,
"live"
,
"push"
);
...
@@ -242,6 +258,7 @@ void WebRtcTransportImp::onStartWebRTC() {
...
@@ -242,6 +258,7 @@ void WebRtcTransportImp::onStartWebRTC() {
if
(
!
hit_pan
)
{
if
(
!
hit_pan
)
{
continue
;
continue
;
}
}
//获取offer端rtp的ssrc和pt相关信息
auto
&
ref
=
_rtp_receiver
[
plan
.
pt
];
auto
&
ref
=
_rtp_receiver
[
plan
.
pt
];
_ssrc_info
[
m
.
rtp_ssrc
.
ssrc
]
=
&
ref
;
_ssrc_info
[
m
.
rtp_ssrc
.
ssrc
]
=
&
ref
;
ref
.
plan
=
&
plan
;
ref
.
plan
=
&
plan
;
...
@@ -273,22 +290,13 @@ void WebRtcTransportImp::onStartWebRTC() {
...
@@ -273,22 +290,13 @@ void WebRtcTransportImp::onStartWebRTC() {
}
}
}
}
bool
WebRtcTransportImp
::
canSendRtp
()
const
{
auto
&
sdp
=
getSdp
(
SdpType
::
answer
);
return
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendrecv
||
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendonly
;
}
bool
WebRtcTransportImp
::
canRecvRtp
()
const
{
auto
&
sdp
=
getSdp
(
SdpType
::
answer
);
return
sdp
.
media
[
0
].
direction
==
RtpDirection
::
sendrecv
||
sdp
.
media
[
0
].
direction
==
RtpDirection
::
recvonly
;
}
void
WebRtcTransportImp
::
onCheckSdp
(
SdpType
type
,
RtcSession
&
sdp
)
const
{
void
WebRtcTransportImp
::
onCheckSdp
(
SdpType
type
,
RtcSession
&
sdp
)
const
{
WebRtcTransport
::
onCheckSdp
(
type
,
sdp
);
WebRtcTransport
::
onCheckSdp
(
type
,
sdp
);
if
(
type
!=
SdpType
::
answer
||
!
canSendRtp
())
{
if
(
type
!=
SdpType
::
answer
||
!
canSendRtp
())
{
return
;
return
;
}
}
//添加answer sdp的ssrc信息,并且记录发送rtp的pt
for
(
auto
&
m
:
sdp
.
media
)
{
for
(
auto
&
m
:
sdp
.
media
)
{
if
(
m
.
type
==
TrackApplication
)
{
if
(
m
.
type
==
TrackApplication
)
{
continue
;
continue
;
...
@@ -311,18 +319,14 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
...
@@ -311,18 +319,14 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
WebRtcTransport
::
onRtcConfigure
(
configure
);
WebRtcTransport
::
onRtcConfigure
(
configure
);
_rtsp_send_sdp
.
loadFrom
(
_src
->
getSdp
(),
false
);
_rtsp_send_sdp
.
loadFrom
(
_src
->
getSdp
(),
false
);
configure
.
audio
.
enable
=
false
;
//根据rtsp流的相关信息,设置rtc最佳编码
configure
.
video
.
enable
=
false
;
for
(
auto
&
m
:
_rtsp_send_sdp
.
media
)
{
for
(
auto
&
m
:
_rtsp_send_sdp
.
media
)
{
switch
(
m
.
type
)
{
switch
(
m
.
type
)
{
case
TrackVideo
:
{
case
TrackVideo
:
{
configure
.
video
.
enable
=
true
;
configure
.
video
.
preferred_codec
.
insert
(
configure
.
video
.
preferred_codec
.
begin
(),
getCodecId
(
m
.
plan
[
0
].
codec
));
configure
.
video
.
preferred_codec
.
insert
(
configure
.
video
.
preferred_codec
.
begin
(),
getCodecId
(
m
.
plan
[
0
].
codec
));
break
;
break
;
}
}
case
TrackAudio
:
{
case
TrackAudio
:
{
configure
.
audio
.
enable
=
true
;
configure
.
audio
.
preferred_codec
.
insert
(
configure
.
audio
.
preferred_codec
.
begin
(),
getCodecId
(
m
.
plan
[
0
].
codec
));
configure
.
audio
.
preferred_codec
.
insert
(
configure
.
audio
.
preferred_codec
.
begin
(),
getCodecId
(
m
.
plan
[
0
].
codec
));
break
;
break
;
}
}
...
@@ -331,21 +335,27 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
...
@@ -331,21 +335,27 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
}
}
}
}
//添加接收端口candidate信息
configure
.
addCandidate
(
*
getIceCandidate
());
configure
.
addCandidate
(
*
getIceCandidate
());
}
}
SdpAttrCandidate
::
Ptr
WebRtcTransportImp
::
getIceCandidate
()
const
{
SdpAttrCandidate
::
Ptr
WebRtcTransportImp
::
getIceCandidate
()
const
{
auto
candidate
=
std
::
make_shared
<
SdpAttrCandidate
>
();
auto
candidate
=
std
::
make_shared
<
SdpAttrCandidate
>
();
candidate
->
foundation
=
"udpcandidate"
;
candidate
->
foundation
=
"udpcandidate"
;
//rtp端口
candidate
->
component
=
1
;
candidate
->
component
=
1
;
candidate
->
transport
=
"udp"
;
candidate
->
transport
=
"udp"
;
//优先级,单candidate时随便
candidate
->
priority
=
100
;
candidate
->
priority
=
100
;
//todo 此处修改为配置文件
candidate
->
address
=
SockUtil
::
get_local_ip
();
candidate
->
address
=
SockUtil
::
get_local_ip
();
candidate
->
port
=
_socket
->
get_local_port
();
candidate
->
port
=
_socket
->
get_local_port
();
candidate
->
type
=
"host"
;
candidate
->
type
=
"host"
;
return
candidate
;
return
candidate
;
}
}
///////////////////////////////////////////////////////////////////
class
RtpReceiverImp
:
public
RtpReceiver
{
class
RtpReceiverImp
:
public
RtpReceiver
{
public
:
public
:
RtpReceiverImp
(
function
<
void
(
RtpPacket
::
Ptr
rtp
)
>
cb
,
function
<
void
(
const
RtpPacket
::
Ptr
&
rtp
)
>
cb_before
=
nullptr
){
RtpReceiverImp
(
function
<
void
(
RtpPacket
::
Ptr
rtp
)
>
cb
,
function
<
void
(
const
RtpPacket
::
Ptr
&
rtp
)
>
cb_before
=
nullptr
){
...
@@ -402,11 +412,28 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
...
@@ -402,11 +412,28 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
}
}
break
;
break
;
}
}
case
RtcpType
:
:
RTCP_BYE
:
{
//todo 此处应该销毁对象
break
;
}
default
:
break
;
default
:
break
;
}
}
}
}
}
}
void
WebRtcTransportImp
::
onRtp
(
const
char
*
buf
,
size_t
len
)
{
RtpHeader
*
rtp
=
(
RtpHeader
*
)
buf
;
//根据接收到的rtp的pt信息,找到该流的信息
auto
it
=
_rtp_receiver
.
find
(
rtp
->
pt
);
if
(
it
==
_rtp_receiver
.
end
())
{
WarnL
;
return
;
}
auto
&
info
=
it
->
second
;
//解析并排序rtp
info
.
receiver
->
inputRtp
(
info
.
media
->
type
,
info
.
plan
->
sample_rate
,
(
uint8_t
*
)
buf
,
len
);
}
int
makeRtcpPli
(
char
*
packet
,
int
len
)
{
int
makeRtcpPli
(
char
*
packet
,
int
len
)
{
if
(
packet
==
NULL
||
len
!=
12
)
if
(
packet
==
NULL
||
len
!=
12
)
return
-
1
;
return
-
1
;
...
@@ -419,24 +446,16 @@ int makeRtcpPli(char *packet, int len) {
...
@@ -419,24 +446,16 @@ int makeRtcpPli(char *packet, int len) {
return
12
;
return
12
;
}
}
void
WebRtcTransportImp
::
onRtp
(
const
char
*
buf
,
size_t
len
)
{
///////////////////////////////////////////////////////////////////
RtpHeader
*
rtp
=
(
RtpHeader
*
)
buf
;
auto
it
=
_rtp_receiver
.
find
(
rtp
->
pt
);
if
(
it
==
_rtp_receiver
.
end
())
{
WarnL
;
return
;
}
auto
&
info
=
it
->
second
;
info
.
receiver
->
inputRtp
(
info
.
media
->
type
,
info
.
plan
->
sample_rate
,
(
uint8_t
*
)
buf
,
len
);
}
void
WebRtcTransportImp
::
onSortedRtp
(
const
RtpPayloadInfo
&
info
,
RtpPacket
::
Ptr
rtp
)
{
void
WebRtcTransportImp
::
onSortedRtp
(
const
RtpPayloadInfo
&
info
,
RtpPacket
::
Ptr
rtp
)
{
if
(
!
info
.
is_common_rtp
){
if
(
!
info
.
is_common_rtp
){
//todo rtx/red/ulpfec类型的rtp先未处理
WarnL
;
WarnL
;
return
;
return
;
}
}
if
(
_pli_ticker
.
elapsedTime
()
>
2000
)
{
if
(
_pli_ticker
.
elapsedTime
()
>
2000
)
{
//todo 发送pli
//todo
定期
发送pli
_pli_ticker
.
resetTime
();
_pli_ticker
.
resetTime
();
char
rtcpbuf
[
12
];
char
rtcpbuf
[
12
];
makeRtcpPli
(
rtcpbuf
,
12
);
makeRtcpPli
(
rtcpbuf
,
12
);
...
@@ -447,7 +466,7 @@ void WebRtcTransportImp::onSortedRtp(const RtpPayloadInfo &info, RtpPacket::Ptr
...
@@ -447,7 +466,7 @@ void WebRtcTransportImp::onSortedRtp(const RtpPayloadInfo &info, RtpPacket::Ptr
}
}
void
WebRtcTransportImp
::
onBeforeSortedRtp
(
const
RtpPayloadInfo
&
info
,
const
RtpPacket
::
Ptr
&
rtp
)
{
void
WebRtcTransportImp
::
onBeforeSortedRtp
(
const
RtpPayloadInfo
&
info
,
const
RtpPacket
::
Ptr
&
rtp
)
{
//
todo rtcp相关
//
统计rtp收到的情况,好做rr汇报
info
.
rtcp_context_recv
->
onRtp
(
rtp
->
getSeq
(),
rtp
->
getStampMS
(),
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
);
info
.
rtcp_context_recv
->
onRtp
(
rtp
->
getSeq
(),
rtp
->
getStampMS
(),
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
);
}
}
...
@@ -460,11 +479,8 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){
...
@@ -460,11 +479,8 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){
//设置pt
//设置pt
rtp
->
getHeader
()
->
pt
=
_send_rtp_pt
[
rtp
->
type
];
rtp
->
getHeader
()
->
pt
=
_send_rtp_pt
[
rtp
->
type
];
sendRtpPacket
(
rtp
->
data
()
+
RtpPacket
::
kRtpTcpHeaderSize
,
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
,
flush
);
sendRtpPacket
(
rtp
->
data
()
+
RtpPacket
::
kRtpTcpHeaderSize
,
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
,
flush
);
//统计rtp发送情况,好做sr汇报
_rtp_receiver
[
_send_rtp_pt
[
rtp
->
type
]].
rtcp_context_send
->
onRtp
(
rtp
->
getSeq
(),
rtp
->
getStampMS
(),
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
);
_rtp_receiver
[
_send_rtp_pt
[
rtp
->
type
]].
rtcp_context_send
->
onRtp
(
rtp
->
getSeq
(),
rtp
->
getStampMS
(),
rtp
->
size
()
-
RtpPacket
::
kRtpTcpHeaderSize
);
//还原pt
//还原pt
rtp
->
getHeader
()
->
pt
=
tmp
;
rtp
->
getHeader
()
->
pt
=
tmp
;
}
}
///////////////////////////////////////////////////////////////////
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论