RtcpContext修改时间戳单位、整理WebRTC相关代码

This commit is contained in:
ziyue 2021-06-25 14:59:27 +08:00
parent 6c01cf336e
commit 964cf39145
8 changed files with 49 additions and 52 deletions

View File

@ -18,8 +18,7 @@ void RtcpContext::clear() {
memset(this, 0, sizeof(RtcpContext)); memset(this, 0, sizeof(RtcpContext));
} }
RtcpContext::RtcpContext(uint32_t sample_rate, bool is_receiver) { RtcpContext::RtcpContext(bool is_receiver) {
_sample_rate = sample_rate;
_is_receiver = is_receiver; _is_receiver = is_receiver;
} }
@ -35,7 +34,6 @@ void RtcpContext::onRtp(uint16_t seq, uint32_t stamp, size_t bytes) {
diff = -diff; diff = -diff;
} }
//抖动单位为采样次数 //抖动单位为采样次数
diff *= (_sample_rate / 1000.0);
_jitter += (diff - _jitter) / 16.0; _jitter += (diff - _jitter) / 16.0;
} else { } else {
_jitter = 0; _jitter = 0;
@ -129,7 +127,7 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
rtcp->setNtpStamp(tv); rtcp->setNtpStamp(tv);
//转换成rtp时间戳 //转换成rtp时间戳
rtcp->rtpts = htonl(uint32_t(_last_rtp_stamp * (_sample_rate / 1000.0))); rtcp->rtpts = htonl(_last_rtp_stamp);
rtcp->packet_count = htonl((uint32_t) _packets); rtcp->packet_count = htonl((uint32_t) _packets);
rtcp->octet_count = htonl((uint32_t) _bytes); rtcp->octet_count = htonl((uint32_t) _bytes);
return RtcpHeader::toBuffer(std::move(rtcp)); return RtcpHeader::toBuffer(std::move(rtcp));

View File

@ -22,15 +22,14 @@ public:
using Ptr = std::shared_ptr<RtcpContext>; using Ptr = std::shared_ptr<RtcpContext>;
/** /**
* rtcp上下文 * rtcp上下文
* @param sample_rate 90000
* @param is_receiver rtp接收者 * @param is_receiver rtp接收者
*/ */
RtcpContext(uint32_t sample_rate, bool is_receiver); RtcpContext(bool is_receiver);
/** /**
* rtp时调用 * rtp时调用
* @param seq rtp的seq * @param seq rtp的seq
* @param stamp rtp的时间戳 * @param stamp rtp的时间戳()
* @param bytes rtp数据长度 * @param bytes rtp数据长度
*/ */
void onRtp(uint16_t seq, uint32_t stamp, size_t bytes); void onRtp(uint16_t seq, uint32_t stamp, size_t bytes);
@ -87,8 +86,6 @@ private:
bool _is_receiver; bool _is_receiver;
//时间戳抖动值 //时间戳抖动值
double _jitter = 0; double _jitter = 0;
//视频默认90000,音频为采样率
uint32_t _sample_rate;
//收到或发送的rtp的字节数 //收到或发送的rtp的字节数
size_t _bytes = 0; size_t _bytes = 0;
//收到或发送的rtp的个数 //收到或发送的rtp的个数

View File

@ -27,7 +27,7 @@ class RtcpHelper : public RtcpContext, public std::enable_shared_from_this<RtcpH
public: public:
using Ptr = std::shared_ptr<RtcpHelper>; using Ptr = std::shared_ptr<RtcpHelper>;
RtcpHelper(Socket::Ptr rtcp_sock, uint32_t sample_rate) : RtcpContext(sample_rate, true){ RtcpHelper(Socket::Ptr rtcp_sock, uint32_t sample_rate) : RtcpContext(true){
_rtcp_sock = std::move(rtcp_sock); _rtcp_sock = std::move(rtcp_sock);
_sample_rate = sample_rate; _sample_rate = sample_rate;
} }
@ -35,7 +35,7 @@ public:
void onRecvRtp(const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len){ void onRecvRtp(const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len){
//统计rtp接受情况用于发送rr包 //统计rtp接受情况用于发送rr包
auto header = (RtpHeader *) buf->data(); auto header = (RtpHeader *) buf->data();
onRtp(ntohs(header->seq), ntohl(header->stamp) * uint64_t(1000) / _sample_rate, buf->size()); onRtp(ntohs(header->seq), ntohl(header->stamp), buf->size());
sendRtcp(ntohl(header->ssrc), addr, addr_len); sendRtcp(ntohl(header->ssrc), addr, addr_len);
} }

View File

@ -205,7 +205,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _sdp_track) { for (auto &track : _sdp_track) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(track->_samplerate, true)); _rtcp_context.emplace_back(std::make_shared<RtcpContext>(true));
} }
sendSetup(0); sendSetup(0);
} }
@ -591,7 +591,7 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url,const StrC
void RtspPlayer::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_idx){ void RtspPlayer::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_idx){
auto &rtcp_ctx = _rtcp_context[track_idx]; auto &rtcp_ctx = _rtcp_context[track_idx];
rtcp_ctx->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize); rtcp_ctx->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
auto &ticker = _rtcp_send_ticker[track_idx]; auto &ticker = _rtcp_send_ticker[track_idx];
if (ticker.elapsedTime() < 3 * 1000) { if (ticker.elapsedTime() < 3 * 1000) {

View File

@ -179,7 +179,7 @@ void RtspPusher::sendAnnounce() {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _track_vec) { for (auto &track : _track_vec) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(track->_samplerate, false)); _rtcp_context.emplace_back(std::make_shared<RtcpContext>(false));
} }
_on_res_func = std::bind(&RtspPusher::handleResAnnounce, this, placeholders::_1); _on_res_func = std::bind(&RtspPusher::handleResAnnounce, this, placeholders::_1);
sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp()); sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp());
@ -360,7 +360,7 @@ void RtspPusher::updateRtcpContext(const RtpPacket::Ptr &rtp){
int track_index = getTrackIndexByTrackType(rtp->type); int track_index = getTrackIndexByTrackType(rtp->type);
auto &ticker = _rtcp_send_ticker[track_index]; auto &ticker = _rtcp_send_ticker[track_index];
auto &rtcp_ctx = _rtcp_context[track_index]; auto &rtcp_ctx = _rtcp_context[track_index];
rtcp_ctx->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize); rtcp_ctx->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
//send rtcp every 5 second //send rtcp every 5 second
if (ticker.elapsedTime() > 5 * 1000) { if (ticker.elapsedTime() > 5 * 1000) {

View File

@ -252,7 +252,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _sdp_track) { for (auto &track : _sdp_track) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(track->_samplerate, true)); _rtcp_context.emplace_back(std::make_shared<RtcpContext>(true));
} }
_push_src = std::make_shared<RtspMediaSourceImp>(_media_info._vhost, _media_info._app, _media_info._streamid); _push_src = std::make_shared<RtspMediaSourceImp>(_media_info._vhost, _media_info._app, _media_info._streamid);
_push_src->setListener(dynamic_pointer_cast<MediaSourceEvent>(shared_from_this())); _push_src->setListener(dynamic_pointer_cast<MediaSourceEvent>(shared_from_this()));
@ -413,7 +413,7 @@ void RtspSession::onAuthSuccess() {
} }
strongSelf->_rtcp_context.clear(); strongSelf->_rtcp_context.clear();
for (auto &track : strongSelf->_sdp_track) { for (auto &track : strongSelf->_sdp_track) {
strongSelf->_rtcp_context.emplace_back(std::make_shared<RtcpContext>(track->_samplerate, false)); strongSelf->_rtcp_context.emplace_back(std::make_shared<RtcpContext>(false));
} }
strongSelf->_sessionid = makeRandStr(12); strongSelf->_sessionid = makeRandStr(12);
strongSelf->_play_src = rtsp_src; strongSelf->_play_src = rtsp_src;
@ -1126,7 +1126,7 @@ void RtspSession::onBeforeRtpSorted(const RtpPacket::Ptr &rtp, int track_index){
void RtspSession::updateRtcpContext(const RtpPacket::Ptr &rtp){ void RtspSession::updateRtcpContext(const RtpPacket::Ptr &rtp){
int track_index = getTrackIndexByTrackType(rtp->type); int track_index = getTrackIndexByTrackType(rtp->type);
auto &rtcp_ctx = _rtcp_context[track_index]; auto &rtcp_ctx = _rtcp_context[track_index];
rtcp_ctx->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize); rtcp_ctx->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
auto &ticker = _rtcp_send_tickers[track_index]; auto &ticker = _rtcp_send_tickers[track_index];
//send rtcp every 5 second //send rtcp every 5 second

View File

@ -408,19 +408,19 @@ void WebRtcTransportImp::onStartWebRTC() {
info->offer_ssrc_rtx = m_offer->getRtxSSRC(); info->offer_ssrc_rtx = m_offer->getRtxSSRC();
info->plan_rtp = &m_answer.plan[0];; info->plan_rtp = &m_answer.plan[0];;
info->plan_rtx = m_answer.getRelatedRtxPlan(info->plan_rtp->pt); info->plan_rtx = m_answer.getRelatedRtxPlan(info->plan_rtp->pt);
info->rtcp_context_send = std::make_shared<RtcpContext>(info->plan_rtp->sample_rate, false); info->rtcp_context_send = std::make_shared<RtcpContext>(false);
//send ssrc --> MediaTrack //send ssrc --> MediaTrack
_rtp_info_ssrc[info->answer_ssrc_rtp] = info; _ssrc_to_track[info->answer_ssrc_rtp] = info;
//recv ssrc --> MediaTrack //recv ssrc --> MediaTrack
_rtp_info_ssrc[info->offer_ssrc_rtp] = info; _ssrc_to_track[info->offer_ssrc_rtp] = info;
//rtp pt --> MediaTrack //rtp pt --> MediaTrack
_rtp_info_pt.emplace(info->plan_rtp->pt, std::make_pair(false, info)); _pt_to_track.emplace(info->plan_rtp->pt, std::make_pair(false, info));
if (info->plan_rtx) { if (info->plan_rtx) {
//rtx pt --> MediaTrack //rtx pt --> MediaTrack
_rtp_info_pt.emplace(info->plan_rtx->pt, std::make_pair(true, info)); _pt_to_track.emplace(info->plan_rtx->pt, std::make_pair(true, info));
} }
if (m_offer->type != TrackApplication) { if (m_offer->type != TrackApplication) {
//记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id //记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id
@ -464,10 +464,10 @@ void WebRtcTransportImp::onStartWebRTC() {
} }
auto rtsp_media = rtsp_send_sdp.getMedia(m.type); auto rtsp_media = rtsp_send_sdp.getMedia(m.type);
if (rtsp_media && getCodecId(rtsp_media->plan[0].codec) == getCodecId(m.plan[0].codec)) { if (rtsp_media && getCodecId(rtsp_media->plan[0].codec) == getCodecId(m.plan[0].codec)) {
auto it = _rtp_info_pt.find(m.plan[0].pt); auto it = _pt_to_track.find(m.plan[0].pt);
CHECK(it != _rtp_info_pt.end()); CHECK(it != _pt_to_track.end());
//记录发送rtp时约定的信息届时发送rtp时需要修改pt和ssrc //记录发送rtp时约定的信息届时发送rtp时需要修改pt和ssrc
_send_rtp_info[m.type] = it->second.second; _type_to_track[m.type] = it->second.second;
} }
} }
} }
@ -558,8 +558,7 @@ SdpAttrCandidate::Ptr WebRtcTransportImp::getIceCandidate() const{
class RtpChannel : public RtpReceiver { class RtpChannel : public RtpReceiver {
public: public:
uint32_t ssrc; uint32_t rtp_ssrc;
RtcpContext::Ptr rtcp_context;
public: public:
RtpChannel(function<void(RtpPacket::Ptr rtp)> on_rtp, function<void(const FCI_NACK &nack)> on_nack) { RtpChannel(function<void(RtpPacket::Ptr rtp)> on_rtp, function<void(const FCI_NACK &nack)> on_nack) {
@ -576,11 +575,16 @@ public:
//统计rtp接受情况便于生成nack rtcp包 //统计rtp接受情况便于生成nack rtcp包
nack_ctx.received(seq); nack_ctx.received(seq);
//统计rtp收到的情况好做rr汇报 //统计rtp收到的情况好做rr汇报
rtcp_context->onRtp(seq, ntohl(rtp->stamp) * uint64_t(1000) / sample_rate, len); rtcp_context.onRtp(seq, ntohl(rtp->stamp), len);
} }
return handleOneRtp((int) type, type, sample_rate, ptr, len); return handleOneRtp((int) type, type, sample_rate, ptr, len);
} }
Buffer::Ptr createRtcpRR(RtcpHeader *sr, uint32_t ssrc) {
rtcp_context.onRtcp(sr);
return rtcp_context.createRtcpRR(ssrc, rtp_ssrc);
}
protected: protected:
void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override { void onRtpSorted(RtpPacket::Ptr rtp, int track_index) override {
_on_sort(std::move(rtp)); _on_sort(std::move(rtp));
@ -588,6 +592,7 @@ protected:
private: private:
NackContext nack_ctx; NackContext nack_ctx;
RtcpContext rtcp_context{true};
function<void(RtpPacket::Ptr rtp)> _on_sort; function<void(RtpPacket::Ptr rtp)> _on_sort;
}; };
@ -611,15 +616,14 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
case RtcpType::RTCP_SR : { case RtcpType::RTCP_SR : {
//对方汇报rtp发送情况 //对方汇报rtp发送情况
RtcpSR *sr = (RtcpSR *) rtcp; RtcpSR *sr = (RtcpSR *) rtcp;
auto it = _rtp_info_ssrc.find(sr->ssrc); auto it = _ssrc_to_track.find(sr->ssrc);
if (it != _rtp_info_ssrc.end()) { if (it != _ssrc_to_track.end()) {
auto &info = it->second; auto &info = it->second;
auto rtp_chn = info->getRtpChannel(sr->ssrc); auto rtp_chn = info->getRtpChannel(sr->ssrc);
if(!rtp_chn){ if(!rtp_chn){
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString(); WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
} else { } else {
rtp_chn->rtcp_context->onRtcp(sr); auto rr = rtp_chn->createRtcpRR(sr, info->answer_ssrc_rtp);
auto rr = rtp_chn->rtcp_context->createRtcpRR(info->answer_ssrc_rtp, sr->ssrc);
sendRtcpPacket(rr->data(), rr->size(), true); sendRtcpPacket(rr->data(), rr->size(), true);
} }
} else { } else {
@ -632,8 +636,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
//对方汇报rtp接收情况 //对方汇报rtp接收情况
RtcpRR *rr = (RtcpRR *) rtcp; RtcpRR *rr = (RtcpRR *) rtcp;
for (auto item : rr->getItemList()) { for (auto item : rr->getItemList()) {
auto it = _rtp_info_ssrc.find(item->ssrc); auto it = _ssrc_to_track.find(item->ssrc);
if (it != _rtp_info_ssrc.end()) { if (it != _ssrc_to_track.end()) {
auto &info = it->second; auto &info = it->second;
auto sr = info->rtcp_context_send->createRtcpSR(info->answer_ssrc_rtp); auto sr = info->rtcp_context_send->createRtcpSR(info->answer_ssrc_rtp);
sendRtcpPacket(sr->data(), sr->size(), true); sendRtcpPacket(sr->data(), sr->size(), true);
@ -647,12 +651,12 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
//对方汇报停止发送rtp //对方汇报停止发送rtp
RtcpBye *bye = (RtcpBye *) rtcp; RtcpBye *bye = (RtcpBye *) rtcp;
for (auto ssrc : bye->getSSRC()) { for (auto ssrc : bye->getSSRC()) {
auto it = _rtp_info_ssrc.find(*ssrc); auto it = _ssrc_to_track.find(*ssrc);
if (it == _rtp_info_ssrc.end()) { if (it == _ssrc_to_track.end()) {
WarnL << "未识别的bye rtcp包:" << rtcp->dumpString(); WarnL << "未识别的bye rtcp包:" << rtcp->dumpString();
continue; continue;
} }
_rtp_info_ssrc.erase(it); _ssrc_to_track.erase(it);
} }
onShutdown(SockException(Err_eof, "rtcp bye message received")); onShutdown(SockException(Err_eof, "rtcp bye message received"));
break; break;
@ -666,8 +670,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
switch ((RTPFBType) rtcp->report_count) { switch ((RTPFBType) rtcp->report_count) {
case RTPFBType::RTCP_RTPFB_NACK : { case RTPFBType::RTCP_RTPFB_NACK : {
RtcpFB *fb = (RtcpFB *) rtcp; RtcpFB *fb = (RtcpFB *) rtcp;
auto it = _rtp_info_ssrc.find(fb->ssrc_media); auto it = _ssrc_to_track.find(fb->ssrc_media);
if (it == _rtp_info_ssrc.end()) { if (it == _ssrc_to_track.end()) {
WarnL << "未识别的 rtcp包:" << rtcp->dumpString(); WarnL << "未识别的 rtcp包:" << rtcp->dumpString();
return; return;
} }
@ -752,11 +756,9 @@ void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, cons
onSendNack(*info, nack, ssrc); onSendNack(*info, nack, ssrc);
}); });
//rid --> rtp ssrc //rid --> rtp ssrc
ref->ssrc = ssrc; ref->rtp_ssrc = ssrc;
//rtp ssrc --> RtcpContext
ref->rtcp_context = std::make_shared<RtcpContext>(info->plan_rtp->sample_rate, true);
//rtp ssrc --> MediaTrack //rtp ssrc --> MediaTrack
_rtp_info_ssrc[ssrc] = info; _ssrc_to_track[ssrc] = info;
InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << info->plan_rtp->codec; InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << info->plan_rtp->codec;
} }
@ -766,8 +768,8 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
RtpHeader *rtp = (RtpHeader *) buf; RtpHeader *rtp = (RtpHeader *) buf;
//根据接收到的rtp的pt信息找到该流的信息 //根据接收到的rtp的pt信息找到该流的信息
auto it = _rtp_info_pt.find(rtp->pt); auto it = _pt_to_track.find(rtp->pt);
if (it == _rtp_info_pt.end()) { if (it == _pt_to_track.end()) {
WarnL << "unknown rtp pt:" << (int)rtp->pt; WarnL << "unknown rtp pt:" << (int)rtp->pt;
return; return;
} }
@ -822,7 +824,7 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
//rtx 转换为 rtp //rtx 转换为 rtp
rtp->pt = info->plan_rtp->pt; rtp->pt = info->plan_rtp->pt;
rtp->seq = htons(origin_seq); rtp->seq = htons(origin_seq);
rtp->ssrc = htonl(ref->ssrc); rtp->ssrc = htonl(ref->rtp_ssrc);
memmove((uint8_t *) buf + 2, buf, payload - (uint8_t *) buf); memmove((uint8_t *) buf + 2, buf, payload - (uint8_t *) buf);
buf += 2; buf += 2;
@ -878,14 +880,14 @@ 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 = _send_rtp_info[rtp->type]; auto &info = _type_to_track[rtp->type];
if (!info) { if (!info) {
//忽略,对方不支持该编码类型 //忽略,对方不支持该编码类型
return; return;
} }
if (!rtx) { if (!rtx) {
//统计rtp发送情况好做sr汇报 //统计rtp发送情况好做sr汇报
info->rtcp_context_send->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize); info->rtcp_context_send->onRtp(rtp->getSeq(), ntohl(rtp->getHeader()->stamp), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
info->nack_list.push_back(rtp); info->nack_list.push_back(rtp);
#if 0 #if 0
//此处模拟发送丢包 //此处模拟发送丢包

View File

@ -384,11 +384,11 @@ private:
//播放rtsp源的reader对象 //播放rtsp源的reader对象
RtspMediaSource::RingType::RingReader::Ptr _reader; RtspMediaSource::RingType::RingReader::Ptr _reader;
//根据发送rtp的track类型获取相关信息 //根据发送rtp的track类型获取相关信息
MediaTrack::Ptr _send_rtp_info[2]; MediaTrack::Ptr _type_to_track[2];
//根据接收rtp的pt获取相关信息 //根据接收rtp的pt获取相关信息
unordered_map<uint8_t/*pt*/, std::pair<bool/*is rtx*/,MediaTrack::Ptr> > _rtp_info_pt; unordered_map<uint8_t/*pt*/, std::pair<bool/*is rtx*/,MediaTrack::Ptr> > _pt_to_track;
//根据rtcp的ssrc获取相关信息只记录rtp的ssrcrtx的ssrc不记录 //根据rtcp的ssrc获取相关信息只记录rtp的ssrcrtx的ssrc不记录
unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _rtp_info_ssrc; unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track;
//发送rtp时需要修改rtp ext id //发送rtp时需要修改rtp ext id
map<RtpExtType, uint8_t> _rtp_ext_type_to_id; map<RtpExtType, uint8_t> _rtp_ext_type_to_id;
//接收rtp时需要修改rtp ext id //接收rtp时需要修改rtp ext id