整理并注释代码

This commit is contained in:
xiongziliang 2021-04-03 08:45:18 +08:00
parent cfda7d8ba6
commit 130a06897f

View File

@ -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
_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() {
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() {
} }
} }
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 {
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 {
} }
} }
//添加接收端口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) {
} }
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) {
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
} }
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){
//设置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;
} }
///////////////////////////////////////////////////////////////////