mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
优化代码
This commit is contained in:
parent
89ef183647
commit
edf30a04cf
@ -399,37 +399,37 @@ void WebRtcTransportImp::onStartWebRTC() {
|
|||||||
//获取ssrc和pt相关信息,届时收到rtp和rtcp时分别可以根据pt和ssrc找到相关的信息
|
//获取ssrc和pt相关信息,届时收到rtp和rtcp时分别可以根据pt和ssrc找到相关的信息
|
||||||
for (auto &m_answer : getSdp(SdpType::answer).media) {
|
for (auto &m_answer : getSdp(SdpType::answer).media) {
|
||||||
auto m_offer = getSdp(SdpType::offer).getMedia(m_answer.type);
|
auto m_offer = getSdp(SdpType::offer).getMedia(m_answer.type);
|
||||||
auto info = std::make_shared<MediaTrack>();
|
auto track = std::make_shared<MediaTrack>();
|
||||||
|
|
||||||
info->media = &m_answer;
|
track->media = &m_answer;
|
||||||
info->answer_ssrc_rtp = m_answer.getRtpSSRC();
|
track->answer_ssrc_rtp = m_answer.getRtpSSRC();
|
||||||
info->answer_ssrc_rtx = m_answer.getRtxSSRC();
|
track->answer_ssrc_rtx = m_answer.getRtxSSRC();
|
||||||
info->offer_ssrc_rtp = m_offer->getRtpSSRC();
|
track->offer_ssrc_rtp = m_offer->getRtpSSRC();
|
||||||
info->offer_ssrc_rtx = m_offer->getRtxSSRC();
|
track->offer_ssrc_rtx = m_offer->getRtxSSRC();
|
||||||
info->plan_rtp = &m_answer.plan[0];;
|
track->plan_rtp = &m_answer.plan[0];;
|
||||||
info->plan_rtx = m_answer.getRelatedRtxPlan(info->plan_rtp->pt);
|
track->plan_rtx = m_answer.getRelatedRtxPlan(track->plan_rtp->pt);
|
||||||
info->rtcp_context_send = std::make_shared<RtcpContext>(false);
|
track->rtcp_context_send = std::make_shared<RtcpContext>(false);
|
||||||
|
|
||||||
//send ssrc --> MediaTrack
|
//send ssrc --> MediaTrack
|
||||||
_ssrc_to_track[info->answer_ssrc_rtp] = info;
|
_ssrc_to_track[track->answer_ssrc_rtp] = track;
|
||||||
_ssrc_to_track[info->answer_ssrc_rtx] = info;
|
_ssrc_to_track[track->answer_ssrc_rtx] = track;
|
||||||
|
|
||||||
//recv ssrc --> MediaTrack
|
//recv ssrc --> MediaTrack
|
||||||
_ssrc_to_track[info->offer_ssrc_rtp] = info;
|
_ssrc_to_track[track->offer_ssrc_rtp] = track;
|
||||||
_ssrc_to_track[info->offer_ssrc_rtx] = info;
|
_ssrc_to_track[track->offer_ssrc_rtx] = track;
|
||||||
|
|
||||||
//rtp pt --> MediaTrack
|
//rtp pt --> MediaTrack
|
||||||
_pt_to_track.emplace(info->plan_rtp->pt, std::make_pair(false, info));
|
_pt_to_track.emplace(track->plan_rtp->pt, std::make_pair(false, track));
|
||||||
if (info->plan_rtx) {
|
if (track->plan_rtx) {
|
||||||
//rtx pt --> MediaTrack
|
//rtx pt --> MediaTrack
|
||||||
_pt_to_track.emplace(info->plan_rtx->pt, std::make_pair(true, info));
|
_pt_to_track.emplace(track->plan_rtx->pt, std::make_pair(true, track));
|
||||||
}
|
}
|
||||||
if (m_offer->type != TrackApplication) {
|
if (m_offer->type != TrackApplication) {
|
||||||
//记录rtp ext类型与id的关系,方便接收或发送rtp时修改rtp ext id
|
//记录rtp ext类型与id的关系,方便接收或发送rtp时修改rtp ext id
|
||||||
info->rtp_ext_ctx = std::make_shared<RtpExtContext>(*m_offer);
|
track->rtp_ext_ctx = std::make_shared<RtpExtContext>(*m_offer);
|
||||||
info->rtp_ext_ctx->setOnGetRtp([this, info](uint8_t pt, uint32_t ssrc, const string &rid) {
|
track->rtp_ext_ctx->setOnGetRtp([this, track](uint8_t pt, uint32_t ssrc, const string &rid) {
|
||||||
//ssrc --> MediaTrack
|
//ssrc --> MediaTrack
|
||||||
_ssrc_to_track[ssrc] = info;
|
_ssrc_to_track[ssrc] = track;
|
||||||
InfoL << "get rtp, pt:" << (int) pt << ", ssrc:" << ssrc << ", rid:" << rid;
|
InfoL << "get rtp, pt:" << (int) pt << ", ssrc:" << ssrc << ", rid:" << rid;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -608,12 +608,12 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
|||||||
RtcpSR *sr = (RtcpSR *) rtcp;
|
RtcpSR *sr = (RtcpSR *) rtcp;
|
||||||
auto it = _ssrc_to_track.find(sr->ssrc);
|
auto it = _ssrc_to_track.find(sr->ssrc);
|
||||||
if (it != _ssrc_to_track.end()) {
|
if (it != _ssrc_to_track.end()) {
|
||||||
auto &info = it->second;
|
auto &track = it->second;
|
||||||
auto rtp_chn = info->getRtpChannel(sr->ssrc);
|
auto rtp_chn = track->getRtpChannel(sr->ssrc);
|
||||||
if(!rtp_chn){
|
if(!rtp_chn){
|
||||||
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
|
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
|
||||||
} else {
|
} else {
|
||||||
auto rr = rtp_chn->createRtcpRR(sr, info->answer_ssrc_rtp);
|
auto rr = rtp_chn->createRtcpRR(sr, track->answer_ssrc_rtp);
|
||||||
sendRtcpPacket(rr->data(), rr->size(), true);
|
sendRtcpPacket(rr->data(), rr->size(), true);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -628,8 +628,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
|||||||
for (auto item : rr->getItemList()) {
|
for (auto item : rr->getItemList()) {
|
||||||
auto it = _ssrc_to_track.find(item->ssrc);
|
auto it = _ssrc_to_track.find(item->ssrc);
|
||||||
if (it != _ssrc_to_track.end()) {
|
if (it != _ssrc_to_track.end()) {
|
||||||
auto &info = it->second;
|
auto &track = it->second;
|
||||||
auto sr = info->rtcp_context_send->createRtcpSR(info->answer_ssrc_rtp);
|
auto sr = track->rtcp_context_send->createRtcpSR(track->answer_ssrc_rtp);
|
||||||
sendRtcpPacket(sr->data(), sr->size(), true);
|
sendRtcpPacket(sr->data(), sr->size(), true);
|
||||||
} else {
|
} else {
|
||||||
WarnL << "未识别的rr rtcp包:" << rtcp->dumpString();
|
WarnL << "未识别的rr rtcp包:" << rtcp->dumpString();
|
||||||
@ -665,9 +665,9 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
|||||||
WarnL << "未识别的 rtcp包:" << rtcp->dumpString();
|
WarnL << "未识别的 rtcp包:" << rtcp->dumpString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto &info = it->second;
|
auto &track = it->second;
|
||||||
auto &fci = fb->getFci<FCI_NACK>();
|
auto &fci = fb->getFci<FCI_NACK>();
|
||||||
info->nack_list.for_each_nack(fci, [&](const RtpPacket::Ptr &rtp) {
|
track->nack_list.for_each_nack(fci, [&](const RtpPacket::Ptr &rtp) {
|
||||||
//rtp重传
|
//rtp重传
|
||||||
onSendRtp(rtp, true, true);
|
onSendRtp(rtp, true, true);
|
||||||
});
|
});
|
||||||
@ -684,15 +684,15 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, const MediaTrack::Ptr &info) {
|
void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, const MediaTrack::Ptr &track) {
|
||||||
//rid --> RtpReceiverImp
|
//rid --> RtpReceiverImp
|
||||||
auto &ref = info->rtp_channel[rid];
|
auto &ref = track->rtp_channel[rid];
|
||||||
ref = std::make_shared<RtpChannel>([info, this, rid](RtpPacket::Ptr rtp) mutable {
|
ref = std::make_shared<RtpChannel>([track, this, rid](RtpPacket::Ptr rtp) mutable {
|
||||||
onSortedRtp(*info, rid, std::move(rtp));
|
onSortedRtp(*track, rid, std::move(rtp));
|
||||||
}, [info, this, ssrc](const FCI_NACK &nack) mutable {
|
}, [track, this, ssrc](const FCI_NACK &nack) mutable {
|
||||||
onSendNack(*info, nack, ssrc);
|
onSendNack(*track, nack, ssrc);
|
||||||
});
|
});
|
||||||
InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << info->plan_rtp->codec;
|
InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << track->plan_rtp->codec;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
|
void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
|
||||||
@ -708,33 +708,33 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
|
|||||||
}
|
}
|
||||||
bool is_rtx = it->second.first;
|
bool is_rtx = it->second.first;
|
||||||
auto ssrc = ntohl(rtp->ssrc);
|
auto ssrc = ntohl(rtp->ssrc);
|
||||||
auto &info = it->second.second;
|
auto &track = it->second.second;
|
||||||
|
|
||||||
//修改ext id至统一
|
//修改ext id至统一
|
||||||
string rid;
|
string rid;
|
||||||
info->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid);
|
track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid);
|
||||||
|
|
||||||
auto &ref = info->rtp_channel[rid];
|
auto &ref = track->rtp_channel[rid];
|
||||||
if (!ref) {
|
if (!ref) {
|
||||||
if (is_rtx) {
|
if (is_rtx) {
|
||||||
//再接收到对应的rtp前,丢弃rtx包
|
//再接收到对应的rtp前,丢弃rtx包
|
||||||
WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ssrc << ", codec:" << info->plan_rtp->codec << ", seq:" << ntohs(rtp->seq);
|
WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ssrc << ", codec:" << track->plan_rtp->codec << ", seq:" << ntohs(rtp->seq);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
createRtpChannel(rid, ssrc, info);
|
createRtpChannel(rid, ssrc, track);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_rtx) {
|
if (!is_rtx) {
|
||||||
//这是普通的rtp数据
|
//这是普通的rtp数据
|
||||||
#if 0
|
#if 0
|
||||||
auto seq = ntohs(rtp->seq);
|
auto seq = ntohs(rtp->seq);
|
||||||
if (info->media->type == TrackVideo && seq % 100 == 0) {
|
if (track->media->type == TrackVideo && seq % 100 == 0) {
|
||||||
//此处模拟接受丢包
|
//此处模拟接受丢包
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
//解析并排序rtp
|
//解析并排序rtp
|
||||||
ref->inputRtp(info->media->type, info->plan_rtp->sample_rate, (uint8_t *) buf, len, false);
|
ref->inputRtp(track->media->type, track->plan_rtp->sample_rate, (uint8_t *) buf, len, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -749,19 +749,19 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
|
|||||||
//前两个字节是原始的rtp的seq
|
//前两个字节是原始的rtp的seq
|
||||||
auto origin_seq = payload[0] << 8 | payload[1];
|
auto origin_seq = payload[0] << 8 | payload[1];
|
||||||
//rtx 转换为 rtp
|
//rtx 转换为 rtp
|
||||||
rtp->pt = info->plan_rtp->pt;
|
rtp->pt = track->plan_rtp->pt;
|
||||||
rtp->seq = htons(origin_seq);
|
rtp->seq = htons(origin_seq);
|
||||||
rtp->ssrc = htonl(ref->getSSRC());
|
rtp->ssrc = htonl(ref->getSSRC());
|
||||||
|
|
||||||
memmove((uint8_t *) buf + 2, buf, payload - (uint8_t *) buf);
|
memmove((uint8_t *) buf + 2, buf, payload - (uint8_t *) buf);
|
||||||
buf += 2;
|
buf += 2;
|
||||||
len -= 2;
|
len -= 2;
|
||||||
ref->inputRtp(info->media->type, info->plan_rtp->sample_rate, (uint8_t *) buf, len, true);
|
ref->inputRtp(track->media->type, track->plan_rtp->sample_rate, (uint8_t *) buf, len, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRtcTransportImp::onSendNack(MediaTrack &info, const FCI_NACK &nack, uint32_t ssrc) {
|
void WebRtcTransportImp::onSendNack(MediaTrack &track, const FCI_NACK &nack, uint32_t ssrc) {
|
||||||
auto rtcp = RtcpFB::create(RTPFBType::RTCP_RTPFB_NACK, &nack, FCI_NACK::kSize);
|
auto rtcp = RtcpFB::create(RTPFBType::RTCP_RTPFB_NACK, &nack, FCI_NACK::kSize);
|
||||||
rtcp->ssrc = htons(info.answer_ssrc_rtp);
|
rtcp->ssrc = htons(track.answer_ssrc_rtp);
|
||||||
rtcp->ssrc_media = htonl(ssrc);
|
rtcp->ssrc_media = htonl(ssrc);
|
||||||
DebugL << htonl(ssrc) << " " << nack.getPid();
|
DebugL << htonl(ssrc) << " " << nack.getPid();
|
||||||
sendRtcpPacket((char *) rtcp.get(), rtcp->getSize(), true);
|
sendRtcpPacket((char *) rtcp.get(), rtcp->getSize(), true);
|
||||||
@ -769,8 +769,8 @@ void WebRtcTransportImp::onSendNack(MediaTrack &info, const FCI_NACK &nack, uint
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void WebRtcTransportImp::onSortedRtp(MediaTrack &info, const string &rid, RtpPacket::Ptr rtp) {
|
void WebRtcTransportImp::onSortedRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) {
|
||||||
if (info.media->type == TrackVideo && _pli_ticker.elapsedTime() > 2000) {
|
if (track.media->type == TrackVideo && _pli_ticker.elapsedTime() > 2000) {
|
||||||
//定期发送pli请求关键帧,方便非rtc等协议
|
//定期发送pli请求关键帧,方便非rtc等协议
|
||||||
_pli_ticker.resetTime();
|
_pli_ticker.resetTime();
|
||||||
sendRtcpPli(rtp->getSSRC());
|
sendRtcpPli(rtp->getSSRC());
|
||||||
@ -807,15 +807,15 @@ void WebRtcTransportImp::onSortedRtp(MediaTrack &info, const string &rid, RtpPac
|
|||||||
///////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool rtx){
|
void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool rtx){
|
||||||
auto &info = _type_to_track[rtp->type];
|
auto &track = _type_to_track[rtp->type];
|
||||||
if (!info) {
|
if (!track) {
|
||||||
//忽略,对方不支持该编码类型
|
//忽略,对方不支持该编码类型
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!rtx) {
|
if (!rtx) {
|
||||||
//统计rtp发送情况,好做sr汇报
|
//统计rtp发送情况,好做sr汇报
|
||||||
info->rtcp_context_send->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
|
track->rtcp_context_send->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
|
||||||
info->nack_list.push_back(rtp);
|
track->nack_list.push_back(rtp);
|
||||||
#if 0
|
#if 0
|
||||||
//此处模拟发送丢包
|
//此处模拟发送丢包
|
||||||
if (rtp->type == TrackVideo && rtp->getSeq() % 100 == 0) {
|
if (rtp->type == TrackVideo && rtp->getSeq() % 100 == 0) {
|
||||||
@ -825,7 +825,7 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool r
|
|||||||
} else {
|
} else {
|
||||||
WarnL << "send rtx rtp:" << rtp->getSeq();
|
WarnL << "send rtx rtp:" << rtp->getSeq();
|
||||||
}
|
}
|
||||||
pair<bool/*rtx*/, MediaTrack *> ctx{rtx, info.get()};
|
pair<bool/*rtx*/, MediaTrack *> ctx{rtx, track.get()};
|
||||||
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush, &ctx);
|
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush, &ctx);
|
||||||
_bytes_usage += rtp->size() - RtpPacket::kRtpTcpHeaderSize;
|
_bytes_usage += rtp->size() - RtpPacket::kRtpTcpHeaderSize;
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,7 @@ private:
|
|||||||
|
|
||||||
void onSortedRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp);
|
void onSortedRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp);
|
||||||
void onSendNack(MediaTrack &track, const FCI_NACK &nack, uint32_t ssrc);
|
void onSendNack(MediaTrack &track, const FCI_NACK &nack, uint32_t ssrc);
|
||||||
void createRtpChannel(const string &rid, uint32_t ssrc, const MediaTrack::Ptr &info);
|
void createRtpChannel(const string &rid, uint32_t ssrc, const MediaTrack::Ptr &track);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t _rtx_seq[2] = {0, 0};
|
uint16_t _rtx_seq[2] = {0, 0};
|
||||||
|
Loading…
Reference in New Issue
Block a user