AI automatically translates all comments in the code into English (#3917)

This commit is contained in:
alex
2024-09-19 14:53:50 +08:00
committed by GitHub
parent 046de691cb
commit 4152dcd409
279 changed files with 10602 additions and 3038 deletions

View File

@@ -16,25 +16,35 @@ using namespace toolkit;
namespace mediakit {
// RTC配置项目
// RTC配置项目 [AUTO-TRANSLATED:19940011]
// RTC configuration project
namespace Rtc {
#define RTC_FIELD "rtc."
//~ nack接收端, rtp发送端
// rtp重发缓存列队最大长度单位毫秒
// ~ nack接收端, rtp发送端 [AUTO-TRANSLATED:e1065811]
// ~ nack receiver, rtp sender
// rtp重发缓存列队最大长度单位毫秒 [AUTO-TRANSLATED:80bccd26]
// rtp retransmission cache queue maximum length, in milliseconds
const string kMaxRtpCacheMS = RTC_FIELD "maxRtpCacheMS";
// rtp重发缓存列队最大长度单位个数
// rtp重发缓存列队最大长度单位个数 [AUTO-TRANSLATED:7d710bf5]
// rtp retransmission cache queue maximum length, in number
const string kMaxRtpCacheSize = RTC_FIELD "maxRtpCacheSize";
//~ nack发送端rtp接收端
//最大保留的rtp丢包状态个数
// ~ nack发送端rtp接收端 [AUTO-TRANSLATED:bb169205]
// ~ nack sender, rtp receiver
// 最大保留的rtp丢包状态个数 [AUTO-TRANSLATED:3aeb53f8]
// Maximum number of rtp packet loss states to keep
const string kNackMaxSize = RTC_FIELD "nackMaxSize";
// rtp丢包状态最长保留时间
// rtp丢包状态最长保留时间 [AUTO-TRANSLATED:f9306375]
// Maximum retention time for rtp packet loss state
const string kNackMaxMS = RTC_FIELD "nackMaxMS";
// nack最多请求重传次数
// nack最多请求重传次数 [AUTO-TRANSLATED:300be0d0]
// Maximum number of nack retransmission requests
const string kNackMaxCount = RTC_FIELD "nackMaxCount";
// nack重传频率rtt的倍数
// nack重传频率rtt的倍数 [AUTO-TRANSLATED:924d53d2]
// Nack retransmission frequency, multiple of rtt
const string kNackIntervalRatio = RTC_FIELD "nackIntervalRatio";
// nack包中rtp个数减小此值可以让nack包响应更灵敏
// nack包中rtp个数减小此值可以让nack包响应更灵敏 [AUTO-TRANSLATED:12393868]
// Number of rtp in nack packet, reducing this value can make nack packet response more sensitive
const string kNackRtpSize = RTC_FIELD "nackRtpSize";
static onceToken token([]() {
@@ -53,22 +63,26 @@ void NackList::pushBack(RtpPacket::Ptr rtp) {
GET_CONFIG(uint32_t, max_rtp_cache_ms, Rtc::kMaxRtpCacheMS);
GET_CONFIG(uint32_t, max_rtp_cache_size, Rtc::kMaxRtpCacheSize);
// 记录rtp
// 记录rtp [AUTO-TRANSLATED:f08e12e2]
// Record rtp
auto seq = rtp->getSeq();
_nack_cache_seq.emplace_back(seq);
_nack_cache_pkt.emplace(seq, std::move(rtp));
// 限制rtp缓存最大个数
// 限制rtp缓存最大个数 [AUTO-TRANSLATED:a6bb50f5]
// Limit the maximum number of rtp cache
if (_nack_cache_seq.size() > max_rtp_cache_size) {
popFront();
}
if (++_cache_ms_check < 100) {
// 每100个rtp包检测下缓存长度节省cpu资源
// 每100个rtp包检测下缓存长度节省cpu资源 [AUTO-TRANSLATED:6399c705]
// Check the cache length every 100 rtp packets to save cpu resources
return;
}
_cache_ms_check = 0;
// 限制rtp缓存最大时长
// 限制rtp缓存最大时长 [AUTO-TRANSLATED:83e5be93]
// Limit the maximum duration of rtp cache
while (getCacheMS() >= max_rtp_cache_ms) {
popFront();
}
@@ -78,7 +92,8 @@ void NackList::forEach(const FCI_NACK &nack, const function<void(const RtpPacket
auto seq = nack.getPid();
for (auto bit : nack.getBitArray()) {
if (bit) {
// 丢包
// 丢包 [AUTO-TRANSLATED:ac2c9d55]
// Packet loss
RtpPacket::Ptr *ptr = getRtp(seq);
if (ptr) {
func(*ptr);
@@ -121,7 +136,8 @@ uint32_t NackList::getCacheMS() {
if (back_stamp >= front_stamp) {
return back_stamp - front_stamp;
}
// ntp时间戳回退了非法数据丢掉
// ntp时间戳回退了非法数据丢掉 [AUTO-TRANSLATED:79ddf252]
// Ntp timestamp has been rolled back, illegal data, discard
_nack_cache_seq.pop_front();
}
return 0;
@@ -132,7 +148,8 @@ int64_t NackList::getNtpStamp(uint16_t seq) {
if (it == _nack_cache_pkt.end()) {
return -1;
}
// 使用ntp时间戳不会回退
// 使用ntp时间戳不会回退 [AUTO-TRANSLATED:2d509f8f]
// Use ntp timestamp, will not roll back
return it->second->getStampMS(true);
}
@@ -144,27 +161,31 @@ NackContext::NackContext() {
void NackContext::received(uint16_t seq, bool is_rtx) {
if (!_started) {
// 记录第一个seq
// 记录第一个seq [AUTO-TRANSLATED:410c831f]
// Record the first seq
_started = true;
_nack_seq = seq - 1;
}
if (seq < _nack_seq && _nack_seq != UINT16_MAX && seq < 1024 && _nack_seq > UINT16_MAX - 1024) {
// seq回环,清空回环前状态
// seq回环,清空回环前状态 [AUTO-TRANSLATED:4cb8027e]
// Seq loop, clear the state before the loop
makeNack(UINT16_MAX, true);
_seq.emplace(seq);
return;
}
if (is_rtx || (seq < _nack_seq && _nack_seq != UINT16_MAX)) {
// seq非回环回退包猜测其为重传包清空其nack状态
// seq非回环回退包猜测其为重传包清空其nack状态 [AUTO-TRANSLATED:74c7b706]
// Seq non-loop rollback packet, guess it is a retransmission packet, clear its nack state
clearNackStatus(seq);
return;
}
auto pr = _seq.emplace(seq);
if (!pr.second) {
// seq重复, 忽略
// seq重复, 忽略 [AUTO-TRANSLATED:95ec10db]
// Seq duplicate, ignore
return;
}
@@ -172,33 +193,40 @@ void NackContext::received(uint16_t seq, bool is_rtx) {
auto min_seq = *_seq.begin();
auto diff = max_seq - min_seq;
if (diff > (UINT16_MAX >> 1)) {
// 回环后收到回环前的大值seq, 忽略掉
// 回环后收到回环前的大值seq, 忽略掉 [AUTO-TRANSLATED:6a30b91f]
// After the loop, receive a large seq value before the loop, ignore it
_seq.erase(max_seq);
return;
}
if (min_seq == (uint16_t)(_nack_seq + 1) && _seq.size() == (size_t)diff + 1) {
// 都是连续的seq未丢包
// 都是连续的seq未丢包 [AUTO-TRANSLATED:62d3ffbd]
// All are continuous seq, no packet loss
_seq.clear();
_nack_seq = max_seq;
} else {
// seq不连续有丢包
// seq不连续有丢包 [AUTO-TRANSLATED:ba1bfbc2]
// Seq is not continuous, there is packet loss
makeNack(max_seq, false);
}
}
void NackContext::makeNack(uint16_t max_seq, bool flush) {
// 尝试移除前面部分连续的seq
// 尝试移除前面部分连续的seq [AUTO-TRANSLATED:04593c1b]
// Try to remove the continuous seq in front
eraseFrontSeq();
// 最多生成5个nack包防止seq大幅跳跃导致一直循环
// 最多生成5个nack包防止seq大幅跳跃导致一直循环 [AUTO-TRANSLATED:9cc5da25]
// Generate at most 5 nack packets to prevent seq from jumping significantly and causing continuous loops
auto max_nack = 5u;
GET_CONFIG(uint32_t, nack_rtpsize, Rtc::kNackRtpSize);
// kNackRtpSize must between 0 and 16
nack_rtpsize = std::min<uint32_t>(nack_rtpsize, FCI_NACK::kBitSize);
while (_nack_seq != max_seq && max_nack--) {
// 一次不能发送超过16+1个rtp的状态
// 一次不能发送超过16+1个rtp的状态 [AUTO-TRANSLATED:1954831a]
// Cannot send more than 16+1 rtp states at a time
uint16_t nack_rtp_count = std::min<uint16_t>(FCI_NACK::kBitSize, max_seq - (uint16_t)(_nack_seq + 1));
if (!flush && nack_rtp_count < nack_rtpsize) {
// 非flush状态下seq个数不足以发送一次nack
// 非flush状态下seq个数不足以发送一次nack [AUTO-TRANSLATED:94f561c1]
// In non-flush state, the number of seq is not enough to send a nack
break;
}
vector<bool> vec;
@@ -208,9 +236,11 @@ void NackContext::makeNack(uint16_t max_seq, bool flush) {
}
doNack(FCI_NACK(_nack_seq + 1, vec), true);
_nack_seq += nack_rtp_count + 1;
// 返回第一个比_last_max_seq大的元素
// 返回第一个比_last_max_seq大的元素 [AUTO-TRANSLATED:425c4e63]
// Return the first element greater than _last_max_seq
auto it = _seq.upper_bound(_nack_seq);
// 移除 <=_last_max_seq 的seq
// 移除 <=_last_max_seq 的seq [AUTO-TRANSLATED:a64ff3fd]
// Remove seq <= _last_max_seq
_seq.erase(_seq.begin(), it);
}
}
@@ -231,10 +261,12 @@ void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
}
void NackContext::eraseFrontSeq() {
// 前面部分seq是连续的未丢包移除之
// 前面部分seq是连续的未丢包移除之 [AUTO-TRANSLATED:ef3eed87]
// The previous part of the sequence is continuous and has no packet loss, remove it.
for (auto it = _seq.begin(); it != _seq.end();) {
if (*it != (uint16_t)(_nack_seq + 1)) {
// seq不连续丢包了
// seq不连续丢包了 [AUTO-TRANSLATED:dcee49fe]
// The sequence is not continuous, there is packet loss.
break;
}
_nack_seq = *it;
@@ -247,11 +279,13 @@ void NackContext::clearNackStatus(uint16_t seq) {
if (it == _nack_send_status.end()) {
return;
}
//收到重传包与第一个nack包间的时间约等于rtt时间
// 收到重传包与第一个nack包间的时间约等于rtt时间 [AUTO-TRANSLATED:f702811e]
// The time between receiving the retransmitted packet and the first nack packet is approximately equal to the rtt time.
auto rtt = getCurrentMillisecond() - it->second.first_stamp;
_nack_send_status.erase(it);
// 限定rtt在合理有效范围内
// 限定rtt在合理有效范围内 [AUTO-TRANSLATED:42fbed04]
// Limit the rtt within a reasonable and valid range.
GET_CONFIG(uint32_t, nack_maxms, Rtc::kNackMaxMS);
GET_CONFIG(uint32_t, nack_maxcount, Rtc::kNackMaxCount);
_rtt = max<int>(10, min<int>(rtt, nack_maxms / nack_maxcount));
@@ -269,7 +303,8 @@ void NackContext::recordNack(const FCI_NACK &nack) {
}
++i;
}
// 记录太多了,移除一部分早期的记录
// 记录太多了,移除一部分早期的记录 [AUTO-TRANSLATED:6f4ea62d]
// There are too many records, remove some of the earlier records.
GET_CONFIG(uint32_t, nack_maxsize, Rtc::kNackMaxSize);
while (_nack_send_status.size() > nack_maxsize) {
_nack_send_status.erase(_nack_send_status.begin());
@@ -284,21 +319,26 @@ uint64_t NackContext::reSendNack() {
GET_CONFIG(float, nack_intervalratio, Rtc::kNackIntervalRatio);
for (auto it = _nack_send_status.begin(); it != _nack_send_status.end();) {
if (now - it->second.first_stamp > nack_maxms) {
// 该rtp丢失太久了不再要求重传
// 该rtp丢失太久了不再要求重传 [AUTO-TRANSLATED:a0a1e471]
// This rtp has been lost for too long, no longer require retransmission.
it = _nack_send_status.erase(it);
continue;
}
if (now - it->second.update_stamp < nack_intervalratio * _rtt) {
// 距离上次nack不足2倍的rtt不用再发送nack
// 距离上次nack不足2倍的rtt不用再发送nack [AUTO-TRANSLATED:0e7edf4d]
// The distance from the last nack is less than 2 times the rtt, no need to send nack again.
++it;
continue;
}
// 此rtp需要请求重传
// 此rtp需要请求重传 [AUTO-TRANSLATED:c29d8eb5]
// This rtp needs to request retransmission.
nack_rtp.emplace(it->first);
// 更新nack发送时间戳
// 更新nack发送时间戳 [AUTO-TRANSLATED:16ef9fac]
// Update the nack sending timestamp.
it->second.update_stamp = now;
if (++(it->second.nack_count) == nack_maxcount) {
// nack次数太多移除之
// nack次数太多移除之 [AUTO-TRANSLATED:1b684a9c]
// Too many nack times, remove it.
it = _nack_send_status.erase(it);
continue;
}
@@ -316,12 +356,14 @@ uint64_t NackContext::reSendNack() {
}
auto inc = *it - pid;
if (inc > (ssize_t)FCI_NACK::kBitSize) {
// 新的nack包
// 新的nack包 [AUTO-TRANSLATED:aec9b818]
// New nack packet.
doNack(FCI_NACK(pid, vec), false);
pid = -1;
continue;
}
// 这个包丢了
// 这个包丢了 [AUTO-TRANSLATED:60f91f2f]
// This packet is lost.
vec[inc - 1] = true;
++it;
}
@@ -329,7 +371,8 @@ uint64_t NackContext::reSendNack() {
doNack(FCI_NACK(pid, vec), false);
}
// 没有任何包需要重传时返回0否则返回下次重传间隔(不得低于5ms)
// 没有任何包需要重传时返回0否则返回下次重传间隔(不得低于5ms) [AUTO-TRANSLATED:c326264d]
// Return 0 when there are no packets to retransmit, otherwise return the next retransmission interval (not less than 5ms).
return _nack_send_status.empty() ? 0 : _rtt;
}

View File

@@ -20,12 +20,16 @@
namespace mediakit {
// RTC配置项目
// RTC配置项目 [AUTO-TRANSLATED:19940011]
// RTC configuration project
namespace Rtc {
//~ nack发送端rtp接收端
// 最大保留的rtp丢包状态个数
// ~ nack发送端rtp接收端 [AUTO-TRANSLATED:bb169205]
// ~ nack sender, rtp receiver
// 最大保留的rtp丢包状态个数 [AUTO-TRANSLATED:70eee442]
// Maximum number of retained rtp packet loss states
extern const std::string kNackMaxSize;
// rtp丢包状态最长保留时间
// rtp丢包状态最长保留时间 [AUTO-TRANSLATED:f9306375]
// Maximum retention time for rtp packet loss states
extern const std::string kNackMaxMS;
} // namespace Rtc
@@ -69,7 +73,8 @@ private:
int _rtt = 50;
onNack _cb;
std::set<uint16_t> _seq;
// 最新nack包中的rtp seq值
// 最新nack包中的rtp seq值 [AUTO-TRANSLATED:6984d95a]
// RTP seq value in the latest nack packet
uint16_t _nack_seq = 0;
struct NackStatus {

View File

@@ -136,7 +136,8 @@ void appendExt(map<uint8_t, RtpExt> &ret, uint8_t *ptr, const uint8_t *end) {
while (ptr < end) {
auto ext = reinterpret_cast<Type *>(ptr);
if (ext->getId() == (uint8_t) RtpExtType::padding) {
//padding忽略
// padding忽略 [AUTO-TRANSLATED:a7fda608]
// padding, ignore
++ptr;
continue;
}
@@ -329,7 +330,8 @@ uint8_t RtpExt::getAudioLevel(bool *vad) const{
}
//http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
//Wire format: 1-byte extension, 3 bytes of data. total 4 bytes extra per packet (plus shared 4 bytes for all extensions present: 2 byte magic word 0xBEDE, 2 byte # of extensions). Will in practice replace the “toffset” extension so we should see no long term increase in traffic as a result.
// Wire format: 1-byte extension, 3 bytes of data. total 4 bytes extra per packet (plus shared 4 bytes for all extensions present: 2 byte magic word 0xBEDE, 2 byte # of extensions). Will in practice replace the “toffset” extension so we should see no long term increase in traffic as a result. [AUTO-TRANSLATED:178290be]
// Wire format: 1-byte extension, 3 bytes of data. total 4 bytes extra per packet (plus shared 4 bytes for all extensions present: 2 byte magic word 0xBEDE, 2 byte # of extensions). Will in practice replace the “toffset” extension so we should see no long term increase in traffic as a result.
//
//Encoding: Timestamp is in seconds, 24 bit 6.18 fixed point, yielding 64s wraparound and 3.8us resolution (one increment for each 477 bytes going out on a 1Gbps interface).
//
@@ -374,7 +376,8 @@ string RtpExt::getSdesMid() const {
//https://tools.ietf.org/html/draft-ietf-avtext-rid-06
//用于simulcast
// 用于simulcast [AUTO-TRANSLATED:59b2682f]
// Used for simulcast
//3.1. RTCP 'RtpStreamId' SDES Extension
//
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
@@ -463,7 +466,8 @@ void RtpExt::getVideoTiming(uint8_t &flags,
//Values:
//0x00: Unspecified. Default value. Treated the same as an absence of an extension.
//0x01: Screenshare. Video stream is of a screenshare type.
//0x02: 摄像头?
// 0x02: 摄像头? [AUTO-TRANSLATED:ce2acbbb]
// 0x02: Camera?
//Notes: Extension shoud be present only in the last packet of key-frames.
// If attached to other packets it should be ignored.
// If extension is absent, Unspecified value is assumed.
@@ -584,12 +588,14 @@ RtpExt RtpExtContext::changeRtpExtId(const RtpHeader *header, bool is_recv, stri
if (is_recv) {
auto it = _rtp_ext_id_to_type.find(pr.first);
if (it == _rtp_ext_id_to_type.end()) {
//TraceL << "接收rtp时,忽略不识别的rtp ext, id=" << (int) pr.first;
// TraceL << "接收rtp时,忽略不识别的rtp ext, id=" << (int) pr.first; [AUTO-TRANSLATED:284d8a38]
// TraceL << "Receiving rtp, ignoring unrecognized rtp ext, id=" << (int) pr.first;
pr.second.clearExt();
continue;
}
pr.second.setType(it->second);
//重新赋值ext id为 ext type作为后面处理ext的统一中间类型
// 重新赋值ext id为 ext type作为后面处理ext的统一中间类型 [AUTO-TRANSLATED:ab825878]
// Reassign ext id to ext type, as a unified intermediate type for processing ext later
pr.second.setExtId((uint8_t) it->second);
switch (it->second) {
case RtpExtType::sdes_rtp_stream_id : rid = pr.second.getRtpStreamId(); break;
@@ -600,11 +606,13 @@ RtpExt RtpExtContext::changeRtpExtId(const RtpHeader *header, bool is_recv, stri
pr.second.setType((RtpExtType) pr.first);
auto it = _rtp_ext_type_to_id.find((RtpExtType) pr.first);
if (it == _rtp_ext_type_to_id.end()) {
//TraceL << "发送rtp时, 忽略不被客户端支持rtp ext:" << pr.second.dumpString();
// TraceL << "发送rtp时, 忽略不被客户端支持rtp ext:" << pr.second.dumpString(); [AUTO-TRANSLATED:5d9fd8cc]
// TraceL << "Sending rtp, ignoring rtp ext not supported by client:" << pr.second.dumpString();
pr.second.clearExt();
continue;
}
//重新赋值ext id为客户端sdp声明的类型
// 重新赋值ext id为客户端sdp声明的类型 [AUTO-TRANSLATED:06d60796]
// Reassign ext id to the type declared in client sdp
pr.second.setExtId(it->second);
}
if (pr.second.getType() == type) {
@@ -620,10 +628,12 @@ RtpExt RtpExtContext::changeRtpExtId(const RtpHeader *header, bool is_recv, stri
}
auto ssrc = ntohl(header->ssrc);
if (rid.empty()) {
//获取rid
// 获取rid [AUTO-TRANSLATED:8ae4dffa]
// Get rid
rid = _ssrc_to_rid[ssrc];
} else {
//设置rid
// 设置rid [AUTO-TRANSLATED:5e34819b]
// Set rid
auto it = _ssrc_to_rid.find(ssrc);
if (it == _ssrc_to_rid.end() || it->second != rid) {
_ssrc_to_rid[ssrc] = rid;

View File

@@ -47,7 +47,8 @@ enum class RtpExtType : uint8_t {
class RtcMedia;
//使用次对象的方法前需保证RtpHeader内存未释放
// 使用次对象的方法前需保证RtpHeader内存未释放 [AUTO-TRANSLATED:0378877f]
// Ensure that the RtpHeader memory has not been released before using the methods of this object
class RtpExt {
public:
template<typename Type>
@@ -129,9 +130,11 @@ private:
private:
OnGetRtp _cb;
//发送rtp时需要修改rtp ext id
// 发送rtp时需要修改rtp ext id [AUTO-TRANSLATED:b92a494b]
// Modify the rtp ext id when sending rtp
std::map<RtpExtType, uint8_t> _rtp_ext_type_to_id;
//接收rtp时需要修改rtp ext id
// 接收rtp时需要修改rtp ext id [AUTO-TRANSLATED:685e7a01]
// Modify the rtp ext id when receiving rtp
std::unordered_map<uint8_t, RtpExtType> _rtp_ext_id_to_type;
//ssrc --> rid
std::unordered_map<uint32_t/*simulcast ssrc*/, std::string/*rid*/> _ssrc_to_rid;

View File

@@ -554,7 +554,8 @@ void SdpAttrRtpMap::parse(const string &str) {
if (sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32 "/%" SCNd32, &pt, buf, &sample_rate, &channel) != 4) {
CHECK_SDP(sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32, &pt, buf, &sample_rate) == 3);
if (getTrackType(getCodecId(buf)) == TrackAudio) {
// 未指定通道数时且为音频时那么通道数默认为1
// 未指定通道数时且为音频时那么通道数默认为1 [AUTO-TRANSLATED:bd128fbd]
// If the number of channels is not specified and it is audio, the number of channels defaults to 1
channel = 1;
}
}
@@ -655,7 +656,8 @@ void SdpAttrSSRCGroup::parse(const string &str) {
string SdpAttrSSRCGroup::toString() const {
if (value.empty()) {
value = type;
// 最少要求2个ssrc
// 最少要求2个ssrc [AUTO-TRANSLATED:968acb83]
// At least 2 SSRCs are required
CHECK(ssrcs.size() >= 2);
for (auto &ssrc : ssrcs) {
value += ' ';
@@ -842,12 +844,15 @@ void RtcSession::loadFrom(const string &str) {
for (auto &group : ssrc_groups) {
if (group.isFID()) {
have_rtx_ssrc = true;
// ssrc-group:FID字段必须包含rtp/rtx的ssrc
// ssrc-group:FID字段必须包含rtp/rtx的ssrc [AUTO-TRANSLATED:3da97d7d]
// The ssrc-group:FID field must contain the SSRCs of rtp/rtx
CHECK(group.ssrcs.size() == 2);
// 根据rtp ssrc找到对象
// 根据rtp ssrc找到对象 [AUTO-TRANSLATED:c0a56b42]
// Find the object based on the RTP SSRC
auto it = rtc_ssrc_map.find(group.ssrcs[0]);
CHECK(it != rtc_ssrc_map.end());
// 设置rtx ssrc
// 设置rtx ssrc [AUTO-TRANSLATED:422e2a55]
// Set the RTX SSRC
it->second.rtx_ssrc = group.ssrcs[1];
rtc_media.rtp_rtx_ssrc.emplace_back(it->second);
} else if (group.isSIM()) {
@@ -857,7 +862,8 @@ void RtcSession::loadFrom(const string &str) {
}
if (!have_rtx_ssrc) {
// 按照sdp顺序依次添加ssrc
// 按照sdp顺序依次添加ssrc [AUTO-TRANSLATED:0996ba7e]
// Add SSRCs in the order of SDP
for (auto &attr : ssrc_attr) {
if (attr.attribute == "cname") {
rtc_media.rtp_rtx_ssrc.emplace_back(rtc_ssrc_map[attr.ssrc]);
@@ -871,7 +877,8 @@ void RtcSession::loadFrom(const string &str) {
// a=rid:m send
// a=rid:l send
// a=simulcast:send h;m;l
// 风格的simulcast
// 风格的simulcast [AUTO-TRANSLATED:94ac2d55]
// Style of simulcast
unordered_set<string> rid_map;
for (auto &rid : simulcast.rids) {
rid_map.emplace(rid);
@@ -880,56 +887,67 @@ void RtcSession::loadFrom(const string &str) {
CHECK(rid.direction == simulcast.direction);
CHECK(rid_map.find(rid.rid) != rid_map.end());
}
// simulcast最少要求2种方案
// simulcast最少要求2种方案 [AUTO-TRANSLATED:31732a7a]
// Simulcast requires at least 2 schemes
CHECK(simulcast.rids.size() >= 2);
rtc_media.rtp_rids = simulcast.rids;
}
if (ssrc_group_sim) {
// 指定了a=ssrc-group:SIM
// 指定了a=ssrc-group:SIM [AUTO-TRANSLATED:5732661e]
// a=ssrc-group:SIM is specified
for (auto ssrc : ssrc_group_sim->ssrcs) {
auto it = rtc_ssrc_map.find(ssrc);
CHECK(it != rtc_ssrc_map.end());
rtc_media.rtp_ssrc_sim.emplace_back(it->second);
}
} else if (!rtc_media.rtp_rids.empty()) {
// 未指定a=ssrc-group:SIM, 但是指定了a=simulcast, 那么只能根据ssrc顺序来对应rid顺序
// 未指定a=ssrc-group:SIM, 但是指定了a=simulcast, 那么只能根据ssrc顺序来对应rid顺序 [AUTO-TRANSLATED:b198a817]
// a=ssrc-group:SIM is not specified, but a=simulcast is specified, so the RID order can only be matched according to the SSRC order
rtc_media.rtp_ssrc_sim = rtc_media.rtp_rtx_ssrc;
}
if (!rtc_media.supportSimulcast()) {
// 不支持simulcast的情况下最多一组ssrc
// 不支持simulcast的情况下最多一组ssrc [AUTO-TRANSLATED:3ea8ed65]
// In the case of not supporting simulcast, there is at most one group of SSRCs
CHECK(rtc_media.rtp_rtx_ssrc.size() <= 1);
} else {
// simulcast的情况下要么没有指定ssrc要么指定的ssrc个数与rid个数一致
// simulcast的情况下要么没有指定ssrc要么指定的ssrc个数与rid个数一致 [AUTO-TRANSLATED:1d45ce03]
// In the case of simulcast, either no SSRC is specified or the number of specified SSRCs is consistent with the number of RIDs
// CHECK(rtc_media.rtp_ssrc_sim.empty() || rtc_media.rtp_ssrc_sim.size() == rtc_media.rtp_rids.size());
}
auto rtpmap_arr = media.getAllItem<SdpAttrRtpMap>('a', "rtpmap");
auto rtcpfb_arr = media.getAllItem<SdpAttrRtcpFb>('a', "rtcp-fb");
auto fmtp_aar = media.getAllItem<SdpAttrFmtp>('a', "fmtp");
// 方便根据pt查找rtpmap,一个pt必有一条
// 方便根据pt查找rtpmap,一个pt必有一条 [AUTO-TRANSLATED:c3673faa]
// Convenient to find rtpmap based on pt, one pt must have one
map<uint8_t, SdpAttrRtpMap &> rtpmap_map;
// 方便根据pt查找rtcp-fb,一个pt可能有多条或0条
// 方便根据pt查找rtcp-fb,一个pt可能有多条或0条 [AUTO-TRANSLATED:38361f68]
// Convenient to find rtcp-fb based on pt, one pt may have multiple or 0
multimap<uint8_t, SdpAttrRtcpFb &> rtcpfb_map;
// 方便根据pt查找fmtp一个pt最多一条
// 方便根据pt查找fmtp一个pt最多一条 [AUTO-TRANSLATED:be5d652d]
// Convenient to find fmtp based on pt, one pt has at most one
map<uint8_t, SdpAttrFmtp &> fmtp_map;
for (auto &rtpmap : rtpmap_arr) {
// 添加失败,有多条
// 添加失败,有多条 [AUTO-TRANSLATED:717782c0]
// Add failed, there are multiple
CHECK(rtpmap_map.emplace(rtpmap.pt, rtpmap).second, "该pt存在多条a=rtpmap:", (int)rtpmap.pt);
}
for (auto &rtpfb : rtcpfb_arr) {
rtcpfb_map.emplace(rtpfb.pt, rtpfb);
}
for (auto &fmtp : fmtp_aar) {
// 添加失败,有多条
// 添加失败,有多条 [AUTO-TRANSLATED:717782c0]
// Add failed, there are multiple
CHECK(fmtp_map.emplace(fmtp.pt, fmtp).second, "该pt存在多条a=fmtp:", (int)fmtp.pt);
}
for (auto &item : mline.fmts) {
auto pt = atoi(item.c_str());
CHECK(pt < 0xFF, "invalid payload type: ", item);
// 遍历所有编码方案的pt
// 遍历所有编码方案的pt [AUTO-TRANSLATED:40f2db36]
// Traverse the pt of all encoding schemes
rtc_media.plan.emplace_back();
auto &plan = rtc_media.plan.back();
auto rtpmap_it = rtpmap_map.find(pt);
@@ -1126,14 +1144,16 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const {
rtp_map->codec = p.codec;
rtp_map->sample_rate = p.sample_rate;
rtp_map->channel = p.channel;
// 添加a=rtpmap
// 添加a=rtpmap [AUTO-TRANSLATED:8bef5d64]
// Add a=rtpmap
sdp_media.addAttr(std::move(rtp_map));
for (auto &fb : p.rtcp_fb) {
auto rtcp_fb = std::make_shared<SdpAttrRtcpFb>();
rtcp_fb->pt = p.pt;
rtcp_fb->rtcp_type = fb;
// 添加a=rtcp-fb
// 添加a=rtcp-fb [AUTO-TRANSLATED:11754b43]
// Add a=rtcp-fb
sdp_media.addAttr(std::move(rtcp_fb));
}
@@ -1141,13 +1161,15 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const {
auto fmtp = std::make_shared<SdpAttrFmtp>();
fmtp->pt = p.pt;
fmtp->fmtp = p.fmtp;
// 添加a=fmtp
// 添加a=fmtp [AUTO-TRANSLATED:594a4425]
// Add a=fmtp
sdp_media.addAttr(std::move(fmtp));
}
}
{
// 添加a=msid字段
// 添加a=msid字段 [AUTO-TRANSLATED:cf2c1471]
// Add a=msid field
if (!m.rtp_rtx_ssrc.empty()) {
if (!m.rtp_rtx_ssrc[0].msid.empty()) {
auto msid = std::make_shared<SdpAttrMsid>();
@@ -1159,14 +1181,17 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const {
{
for (auto &ssrc : m.rtp_rtx_ssrc) {
// 添加a=ssrc字段
// 添加a=ssrc字段 [AUTO-TRANSLATED:75ca5225]
// Add a=ssrc field
CHECK(!ssrc.empty());
addSdpAttrSSRC(ssrc, sdp_media, ssrc.ssrc);
if (ssrc.rtx_ssrc) {
addSdpAttrSSRC(ssrc, sdp_media, ssrc.rtx_ssrc);
// 生成a=ssrc-group:FID字段
// 有rtx ssrc
// 生成a=ssrc-group:FID字段 [AUTO-TRANSLATED:22b1f966]
// Generate a=ssrc-group:FID field
// 有rtx ssrc [AUTO-TRANSLATED:fece8076]
// There is rtx ssrc
auto group = std::make_shared<SdpAttrSSRCGroup>();
group->type = "FID";
group->ssrcs.emplace_back(ssrc.ssrc);
@@ -1178,12 +1203,14 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const {
{
if (m.rtp_ssrc_sim.size() >= 2) {
// simulcast 要求 2~3路
// simulcast 要求 2~3路 [AUTO-TRANSLATED:3237ffca]
// Simulcast requires 2~3 channels
auto group = std::make_shared<SdpAttrSSRCGroup>();
for (auto &ssrc : m.rtp_ssrc_sim) {
group->ssrcs.emplace_back(ssrc.ssrc);
}
// 添加a=ssrc-group:SIM字段
// 添加a=ssrc-group:SIM字段 [AUTO-TRANSLATED:46b04aae]
// Add a=ssrc-group:SIM field
group->type = "SIM";
sdp_media.addAttr(std::move(group));
}
@@ -1299,15 +1326,18 @@ void RtcMedia::checkValid() const {
bool send_rtp = (direction == RtpDirection::sendonly || direction == RtpDirection::sendrecv);
if (!supportSimulcast()) {
// 非simulcast时检查有没有指定rtp ssrc
// 非simulcast时检查有没有指定rtp ssrc [AUTO-TRANSLATED:e2d53f8a]
// When not simulcast, check if the RTP SSRC is specified
CHECK(!rtp_rtx_ssrc.empty() || !send_rtp);
}
#if 0
//todo 发现Firefox(88.0)在mac平台下开启rtx后没有指定ssrc
// todo 发现Firefox(88.0)在mac平台下开启rtx后没有指定ssrc [AUTO-TRANSLATED:9112d91a]
// todo Found that Firefox (88.0) on the mac platform does not specify ssrc when rtx is enabled
auto rtx_plan = getPlan("rtx");
if (rtx_plan) {
//开启rtx后必须指定rtx_ssrc
// 开启rtx后必须指定rtx_ssrc [AUTO-TRANSLATED:c527f68d]
// RTX must be specified after rtx_ssrc is enabled
CHECK(rtp_rtx_ssrc.size() >= 2 || !send_rtp);
}
#endif
@@ -1416,7 +1446,8 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type) {
ice_renomination = false;
switch (type) {
case TrackAudio: {
// 此处调整偏好的编码格式优先级
// 此处调整偏好的编码格式优先级 [AUTO-TRANSLATED:b8719e66]
// Adjust the priority of preferred encoding formats here
GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecA, toCodecArray);
CHECK(!s_preferred_codec.empty(), "rtc音频偏好codec不能为空");
preferred_codec = s_preferred_codec;
@@ -1426,14 +1457,16 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type) {
{ RtpExtType::csrc_audio_level, RtpDirection::sendrecv },
{ RtpExtType::abs_send_time, RtpDirection::sendrecv },
{ RtpExtType::transport_cc, RtpDirection::sendrecv },
// rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
// rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放 [AUTO-TRANSLATED:221df025]
// When rtx retransmits rtp, ignore the rtp ext of sdes_mid type. It is found that Firefox cannot play when receiving rtx if there is an ext of sdes_mid
//{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{ RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv },
{ RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv } };
break;
}
case TrackVideo: {
// 此处调整偏好的编码格式优先级
// 此处调整偏好的编码格式优先级 [AUTO-TRANSLATED:b8719e66]
// Adjust the priority of preferred encoding formats here
GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecV, toCodecArray);
CHECK(!s_preferred_codec.empty(), "rtc视频偏好codec不能为空");
preferred_codec = s_preferred_codec;
@@ -1441,7 +1474,8 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type) {
rtcp_fb = { SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb, "nack", "ccm fir", "nack pli" };
extmap = { { RtpExtType::abs_send_time, RtpDirection::sendrecv },
{ RtpExtType::transport_cc, RtpDirection::sendrecv },
// rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
// rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放 [AUTO-TRANSLATED:221df025]
// When rtx retransmits rtp, ignore the rtp ext of sdes_mid type. It is found that Firefox cannot play when receiving rtx if there is an ext of sdes_mid
//{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{ RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv },
{ RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv },
@@ -1449,7 +1483,8 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type) {
{ RtpExtType::color_space, RtpDirection::sendrecv },
{ RtpExtType::video_content_type, RtpDirection::sendrecv },
{ RtpExtType::playout_delay, RtpDirection::sendrecv },
// 手机端推webrtc 会带有旋转角度rtc协议能正常播放 其他协议拉流画面旋转
// 手机端推webrtc 会带有旋转角度rtc协议能正常播放 其他协议拉流画面旋转 [AUTO-TRANSLATED:3f2f9e0e]
// Mobile push webrtc will have a rotation angle, rtc protocol can play normally, other protocols pull stream picture rotation
//{RtpExtType::video_orientation, RtpDirection::sendrecv},
{ RtpExtType::toffset, RtpDirection::sendrecv },
{ RtpExtType::framemarking, RtpDirection::sendrecv } };
@@ -1549,7 +1584,8 @@ shared_ptr<RtcSession> RtcConfigure::createAnswer(const RtcSession &offer) const
matchMedia(ret, m);
}
// 设置音视频端口复用
// 设置音视频端口复用 [AUTO-TRANSLATED:ffe27d17]
// Set audio and video port multiplexing
if (!offer.group.mids.empty()) {
for (auto &m : ret->media) {
// The remote end has rejected (port 0) the m-section, so it should not be putting its mid in the group attribute.
@@ -1565,7 +1601,8 @@ static RtpDirection matchDirection(RtpDirection offer_direction, RtpDirection su
switch (offer_direction) {
case RtpDirection::sendonly: {
if (supported != RtpDirection::recvonly && supported != RtpDirection::sendrecv) {
// 我们不支持接收
// 我们不支持接收 [AUTO-TRANSLATED:e4ef4034]
// We do not support receiving
return RtpDirection::inactive;
}
return RtpDirection::recvonly;
@@ -1573,13 +1610,15 @@ static RtpDirection matchDirection(RtpDirection offer_direction, RtpDirection su
case RtpDirection::recvonly: {
if (supported != RtpDirection::sendonly && supported != RtpDirection::sendrecv) {
// 我们不支持发送
// 我们不支持发送 [AUTO-TRANSLATED:6505a226]
// We do not support sending
return RtpDirection::inactive;
}
return RtpDirection::sendonly;
}
// 对方支持发送接收,那么最终能力根据配置来决定
// 对方支持发送接收,那么最终能力根据配置来决定 [AUTO-TRANSLATED:d234d603]
// The other party supports sending and receiving, so the final capability is determined by the configuration
case RtpDirection::sendrecv: return (supported == RtpDirection::invalid ? RtpDirection::inactive : supported);
case RtpDirection::inactive: return RtpDirection::inactive;
default: return RtpDirection::invalid;
@@ -1633,20 +1672,24 @@ RETRY:
}
const RtcCodecPlan *selected_plan = nullptr;
for (auto &plan : offer_media.plan) {
// 先检查编码格式是否为偏好
// 先检查编码格式是否为偏好 [AUTO-TRANSLATED:b7fb32a0]
// First check if the encoding format is preferred
if (check_codec && getCodecId(plan.codec) != codec) {
continue;
}
// 命中偏好的编码格式,然后检查规格
// 命中偏好的编码格式,然后检查规格 [AUTO-TRANSLATED:a859c839]
// Hit the preferred encoding format, then check the specifications
if (check_profile && !onCheckCodecProfile(plan, codec)) {
continue;
}
// 找到中意的codec
// 找到中意的codec [AUTO-TRANSLATED:4b5eebfd]
// Find the desired codec
selected_plan = &plan;
break;
}
if (!selected_plan) {
// offer中该媒体的所有的codec都不支持
// offer中该媒体的所有的codec都不支持 [AUTO-TRANSLATED:3b57b86f]
// All codecs for this media in the offer are not supported
continue;
}
RtcMedia answer_media;
@@ -1672,23 +1715,27 @@ RETRY:
answer_media.role = mathDtlsRole(offer_media.role);
// 如果codec匹配失败那么禁用该track
// 如果codec匹配失败那么禁用该track [AUTO-TRANSLATED:037de9a8]
// If the codec matching fails, then disable the track
answer_media.direction = check_codec ? matchDirection(offer_media.direction, configure.direction) : RtpDirection::inactive;
if (answer_media.direction == RtpDirection::invalid) {
continue;
}
if (answer_media.direction == RtpDirection::sendrecv) {
// 如果是收发双向那么我们拷贝offer sdp的ssrc确保ssrc一致
// 如果是收发双向那么我们拷贝offer sdp的ssrc确保ssrc一致 [AUTO-TRANSLATED:d4a621f2]
// If it is bidirectional, then we copy the offer sdp ssrc to ensure ssrc consistency
answer_media.rtp_rtx_ssrc = offer_media.rtp_rtx_ssrc;
}
// 添加媒体plan
// 添加媒体plan [AUTO-TRANSLATED:3f730050]
// Add media plan
answer_media.plan.emplace_back(*selected_plan);
onSelectPlan(answer_media.plan.back(), codec);
set<uint8_t> pt_selected = { selected_plan->pt };
// 添加rtx,red,ulpfec plan
// 添加rtx,red,ulpfec plan [AUTO-TRANSLATED:1abff0c1]
// Add rtx, red, ulpfec plan
if (configure.support_red || configure.support_rtx || configure.support_ulpfec) {
for (auto &plan : offer_media.plan) {
if (!strcasecmp(plan.codec.data(), "rtx")) {
@@ -1715,7 +1762,8 @@ RETRY:
}
}
// 对方和我方都支持的扩展,那么我们才支持
// 对方和我方都支持的扩展,那么我们才支持 [AUTO-TRANSLATED:a6cd98b2]
// We only support extensions that are supported by both the other party and us
for (auto &ext : offer_media.extmap) {
auto it = configure.extmap.find(RtpExt::getExtType(ext.ext));
if (it != configure.extmap.end()) {
@@ -1732,16 +1780,19 @@ RETRY:
auto &rtcp_fb_ref = answer_media.plan[0].rtcp_fb;
rtcp_fb_ref.clear();
// 对方和我方都支持的rtcpfb那么我们才支持
// 对方和我方都支持的rtcpfb那么我们才支持 [AUTO-TRANSLATED:f10450bb]
// We only support rtcpfb that is supported by both the other party and us
for (auto &fp : selected_plan->rtcp_fb) {
if (configure.rtcp_fb.find(fp) != configure.rtcp_fb.end()) {
// 对方该rtcp被我们支持
// 对方该rtcp被我们支持 [AUTO-TRANSLATED:3b16e666]
// The other party's rtcp is supported by us
rtcp_fb_ref.emplace(fp);
}
}
#if 0
//todo 此处为添加无效的planwebrtc sdp通过调节plan pt顺序选择匹配的codec意味着后面的codec其实放在sdp中是无意义的
// todo 此处为添加无效的planwebrtc sdp通过调节plan pt顺序选择匹配的codec意味着后面的codec其实放在sdp中是无意义的 [AUTO-TRANSLATED:502d0cb2]
// todo This is to add an invalid plan. WebRTC sdp selects the matching codec by adjusting the plan pt order, which means that the subsequent codecs are actually meaningless in the sdp
for (auto &plan : offer_media.plan) {
if (pt_selected.find(plan.pt) == pt_selected.end()) {
answer_media.plan.emplace_back(plan);
@@ -1753,13 +1804,15 @@ RETRY:
}
if (check_profile) {
// 如果是由于检查profile导致匹配失败那么重试一次且不检查profile
// 如果是由于检查profile导致匹配失败那么重试一次且不检查profile [AUTO-TRANSLATED:897fa4ae]
// If the matching fails due to profile check, retry once and do not check profile
check_profile = false;
goto RETRY;
}
if (check_codec) {
// 如果是由于检查codec导致匹配失败那么重试一次且不检查codec
// 如果是由于检查codec导致匹配失败那么重试一次且不检查codec [AUTO-TRANSLATED:fbd85968]
// If the matching fails due to codec check, retry once and do not check codec
check_codec = false;
goto RETRY;
}
@@ -1798,15 +1851,18 @@ static const string kMode { "packetization-mode" };
bool RtcConfigure::onCheckCodecProfile(const RtcCodecPlan &plan, CodecId codec) const {
if (_rtsp_audio_plan && codec == getCodecId(_rtsp_audio_plan->codec)) {
if (plan.sample_rate != _rtsp_audio_plan->sample_rate || plan.channel != _rtsp_audio_plan->channel) {
// 音频采样率和通道数必须相同
// 音频采样率和通道数必须相同 [AUTO-TRANSLATED:6e591932]
// Audio sampling rate and number of channels must be the same
return false;
}
return true;
}
if (_rtsp_video_plan && codec == CodecH264 && getCodecId(_rtsp_video_plan->codec) == CodecH264) {
// h264时profile-level-id
// h264时profile-level-id [AUTO-TRANSLATED:94a5f360]
// When h264, profile-level-id
if (strcasecmp(_rtsp_video_plan->fmtp[kProfile].data(), const_cast<RtcCodecPlan &>(plan).fmtp[kProfile].data())) {
// profile-level-id 不匹配
// profile-level-id 不匹配 [AUTO-TRANSLATED:814ec4c4]
// profile-level-id does not match
return false;
}
return true;
@@ -1819,10 +1875,16 @@ bool RtcConfigure::onCheckCodecProfile(const RtcCodecPlan &plan, CodecId codec)
Single NAI Unit Mode = 0. // Single NAI mode (Only nals from 1-23 are allowed)
Non Interleaved Mode = 1// Non-interleaved Mode: 1-2324 (STAP-A)28 (FU-A) are allowed
Interleaved Mode = 2, // 25 (STAP-B)26 (MTAP16)27 (MTAP24)28 (EU-A)and 29 (EU-B) are allowed.
Single NAI Unit Mode = 0. // Single NAI mode (Only nals from 1-23 are allowed)
Non Interleaved Mode = 1// Non-interleaved Mode: 1-2324 (STAP-A)28 (FU-A) are allowed
Interleaved Mode = 2, // 25 (STAP-B)26 (MTAP16)27 (MTAP24)28 (EU-A)and 29 (EU-B) are allowed.
*
* [AUTO-TRANSLATED:b1526114]
**/
void RtcConfigure::onSelectPlan(RtcCodecPlan &plan, CodecId codec) const {
if (_rtsp_video_plan && codec == CodecH264 && getCodecId(_rtsp_video_plan->codec) == CodecH264) {
// h264时设置packetization-mod为一致
// h264时设置packetization-mod为一致 [AUTO-TRANSLATED:59a00889]
// When h264, set packetization-mod to be consistent
auto mode = _rtsp_video_plan->fmtp[kMode];
GET_CONFIG(bool, h264_stap_a, Rtp::kH264StapA);
plan.fmtp[kMode] = mode.empty() ? std::to_string(h264_stap_a) : mode;

View File

@@ -57,23 +57,30 @@ namespace mediakit {
enum class RtpDirection {
invalid = -1,
// 只发送
// 只发送 [AUTO-TRANSLATED:d7e7fdb7]
// Send only
sendonly,
// 只接收
// 只接收 [AUTO-TRANSLATED:f75ca789]
// Receive only
recvonly,
// 同时发送接收
// 同时发送接收 [AUTO-TRANSLATED:7f900ba1]
// Send and receive simultaneously
sendrecv,
// 禁止发送数据
// 禁止发送数据 [AUTO-TRANSLATED:6045b47e]
// Prohibit sending data
inactive
};
enum class DtlsRole {
invalid = -1,
// 客户端
// 客户端 [AUTO-TRANSLATED:915417a2]
// Client
active,
// 服务端
// 服务端 [AUTO-TRANSLATED:03a80b18]
// Server
passive,
// 既可作做客户端也可以做服务端
// 既可作做客户端也可以做服务端 [AUTO-TRANSLATED:5ab1162e]
// Can be used as both client and server
actpass,
};
@@ -169,7 +176,8 @@ public:
// 5.8. Bandwidth ("b=")
// b=<bwtype>:<bandwidth>
// AS、CT
// AS、CT [AUTO-TRANSLATED:65298206]
// AS, CT
std::string bwtype { "AS" };
uint32_t bandwidth { 0 };
@@ -185,10 +193,14 @@ public:
// m=<media> <port> <proto> <fmt> ...
TrackType type;
uint16_t port;
// RTP/AVP应用场景为视频/音频的 RTP 协议。参考 RFC 3551
// RTP/SAVP:应用场景为视频/音频的 SRTP 协议。参考 RFC 3711
// RTP/AVPF: 应用场景为视频/音频的 RTP 协议,支持 RTCP-based Feedback。参考 RFC 4585
// RTP/SAVPF: 应用场景为视频/音频的 SRTP 协议,支持 RTCP-based Feedback。参考 RFC 5124
// RTP/AVP应用场景为视频/音频的 RTP 协议。参考 RFC 3551 [AUTO-TRANSLATED:7a9d7e86]
// RTP/AVP: The application scenario is the RTP protocol for video/audio. Refer to RFC 3551
// RTP/SAVP应用场景为视频/音频的 SRTP 协议。参考 RFC 3711 [AUTO-TRANSLATED:7989a619]
// RTP/SAVP: The application scenario is the SRTP protocol for video/audio. Refer to RFC 3711
// RTP/AVPF: 应用场景为视频/音频的 RTP 协议,支持 RTCP-based Feedback。参考 RFC 4585 [AUTO-TRANSLATED:71241e80]
// RTP/AVPF: The application scenario is the RTP protocol for video/audio, supporting RTCP-based Feedback. Refer to RFC 4585
// RTP/SAVPF: 应用场景为视频/音频的 SRTP 协议,支持 RTCP-based Feedback。参考 RFC 5124 [AUTO-TRANSLATED:69015267]
// RTP/SAVPF: The application scenario is the SRTP protocol for video/audio, supporting RTCP-based Feedback. Refer to RFC 5124
std::string proto;
std::vector<std::string> fmts;
@@ -349,11 +361,16 @@ public:
class SdpAttrRtcpFb : public SdpItem {
public:
// a=rtcp-fb:98 nack pli
// a=rtcp-fb:120 nack 支持 nack 重传nack (Negative-Acknowledgment) 。
// a=rtcp-fb:120 nack pli 支持 nack 关键帧重传PLI (Picture Loss Indication) 。
// a=rtcp-fb:120 ccm fir 支持编码层关键帧请求CCM (Codec Control Message)FIR (Full Intra Request ),通常与 nack pli 有同样的效果,但是 nack pli
// 是用于重传时的关键帧请求。 a=rtcp-fb:120 goog-remb 支持 REMB (Receiver Estimated Maximum Bitrate) 。 a=rtcp-fb:120 transport-cc 支持 TCC (Transport
// Congest Control) 。
// a=rtcp-fb:120 nack 支持 nack 重传nack (Negative-Acknowledgment) 。 [AUTO-TRANSLATED:08d5c4e2]
// a=rtcp-fb:120 nack supports nack retransmission, nack (Negative-Acknowledgment).
// a=rtcp-fb:120 nack pli 支持 nack 关键帧重传PLI (Picture Loss Indication) 。 [AUTO-TRANSLATED:c331c1dd]
// a=rtcp-fb:120 nack pli supports nack keyframe retransmission, PLI (Picture Loss Indication).
// a=rtcp-fb:120 ccm fir 支持编码层关键帧请求CCM (Codec Control Message)FIR (Full Intra Request ),通常与 nack pli 有同样的效果,但是 nack pli [AUTO-TRANSLATED:7090fdc9]
// a=rtcp-fb:120 ccm fir supports keyframe requests for the coding layer, CCM (Codec Control Message), FIR (Full Intra Request), which usually has the same effect as nack pli, but nack pli
// 是用于重传时的关键帧请求。 a=rtcp-fb:120 goog-remb 支持 REMB (Receiver Estimated Maximum Bitrate) 。 a=rtcp-fb:120 transport-cc 支持 TCC (Transport [AUTO-TRANSLATED:ffac8e91]
// is used for keyframe requests during retransmission. a=rtcp-fb:120 goog-remb supports REMB (Receiver Estimated Maximum Bitrate). a=rtcp-fb:120 transport-cc supports TCC (Transport
// Congest Control) 。 [AUTO-TRANSLATED:dcf53e31]
// Congest Control).
uint8_t pt;
std::string rtcp_type;
void parse(const std::string &str) override;
@@ -379,12 +396,18 @@ public:
// a=ssrc:3245185839 label:0cf7e597-36a2-4480-9796-69bf0955eef5
// a=ssrc:<ssrc-id> <attribute>
// a=ssrc:<ssrc-id> <attribute>:<value>
// cname 是必须的msid/mslabel/label 这三个属性都是 WebRTC 自创的,或者说 Google 自创的,可以参考 https://tools.ietf.org/html/draft-ietf-mmusic-msid-17
// 理解它们三者的关系需要先了解三个概念RTP stream / MediaStreamTrack / MediaStream
// 一个 a=ssrc 代表一个 RTP stream
// 一个 MediaStreamTrack 通常包含一个或多个 RTP stream,例如一个视频 MediaStreamTrack 中通常包含两个 RTP stream一个用于常规传输一个用于 nack 重传;
// 一个 MediaStream 通常包含一个或多个 MediaStreamTrack ,例如 simulcast 场景下,一个 MediaStream 通常会包含三个不同编码质量的 MediaStreamTrack
// 这种标记方式并不被 Firefox 认可,在 Firefox 生成的 SDP 中一个 a=ssrc 通常只有一行,例如:
// cname 是必须的msid/mslabel/label 这三个属性都是 WebRTC 自创的,或者说 Google 自创的,可以参考 https://tools.ietf.org/html/draft-ietf-mmusic-msid-17 [AUTO-TRANSLATED:d8cb1baf]
// cname is required, msid/mslabel/label these three attributes are all created by WebRTC, or Google created, you can refer to https://tools.ietf.org/html/draft-ietf-mmusic-msid-17,
// 理解它们三者的关系需要先了解三个概念RTP stream / MediaStreamTrack / MediaStream [AUTO-TRANSLATED:7d385cf5]
// understanding the relationship between the three requires understanding three concepts: RTP stream / MediaStreamTrack / MediaStream:
// 一个 a=ssrc 代表一个 RTP stream [AUTO-TRANSLATED:ee1ecc6f]
// One a=ssrc represents one RTP stream;
// 一个 MediaStreamTrack 通常包含一个或多个 RTP stream例如一个视频 MediaStreamTrack 中通常包含两个 RTP stream一个用于常规传输一个用于 nack 重传; [AUTO-TRANSLATED:e8ddf0fd]
// A MediaStreamTrack usually contains one or more RTP streams, for example, a video MediaStreamTrack usually contains two RTP streams, one for regular transmission and one for nack retransmission;
// 一个 MediaStream 通常包含一个或多个 MediaStreamTrack ,例如 simulcast 场景下,一个 MediaStream 通常会包含三个不同编码质量的 MediaStreamTrack [AUTO-TRANSLATED:31318d43]
// A MediaStream usually contains one or more MediaStreamTrack, for example, in a simulcast scenario, a MediaStream usually contains three MediaStreamTrack of different encoding quality;
// 这种标记方式并不被 Firefox 认可,在 Firefox 生成的 SDP 中一个 a=ssrc 通常只有一行,例如: [AUTO-TRANSLATED:8c2c424c]
// This marking method is not recognized by Firefox, in the SDP generated by Firefox, one a=ssrc usually has only one line, for example:
// a=ssrc:3245185839 cname:Cx4i/VTR51etgjT7
uint32_t ssrc;
@@ -397,11 +420,14 @@ public:
class SdpAttrSSRCGroup : public SdpItem {
public:
// a=ssrc-group 定义参考 RFC 5576(https://tools.ietf.org/html/rfc5576) ,用于描述多个 ssrc 之间的关联,常见的有两种:
// a=ssrc-group 定义参考 RFC 5576(https://tools.ietf.org/html/rfc5576) ,用于描述多个 ssrc 之间的关联,常见的有两种: [AUTO-TRANSLATED:a87cbcc6]
// a=ssrc-group definition refers to RFC 5576(https://tools.ietf.org/html/rfc5576), used to describe the association between multiple ssrcs, there are two common types:
// a=ssrc-group:FID 2430709021 3715850271
// FID (Flow Identification) 最初用在 FEC 的关联中WebRTC 中通常用于关联一组常规 RTP stream 和 重传 RTP stream 。
// FID (Flow Identification) 最初用在 FEC 的关联中WebRTC 中通常用于关联一组常规 RTP stream 和 重传 RTP stream 。 [AUTO-TRANSLATED:f2c0fcbb]
// FID (Flow Identification) was originally used in FEC association, and in WebRTC it is usually used to associate a group of regular RTP streams and retransmission RTP streams.
// a=ssrc-group:SIM 360918977 360918978 360918980
// 在 Chrome 独有的 SDP munging 风格的 simulcast 中使用,将三组编码质量由低到高的 MediaStreamTrack 关联在一起。
// 在 Chrome 独有的 SDP munging 风格的 simulcast 中使用,将三组编码质量由低到高的 MediaStreamTrack 关联在一起。 [AUTO-TRANSLATED:61bf7596]
// Used in Chrome's unique SDP munging style simulcast, associating three groups of MediaStreamTrack from low to high encoding quality.
std::string type { "FID" };
std::vector<uint32_t> ssrcs;
@@ -434,7 +460,8 @@ public:
// a=candidate:4 1 udp 2 192.168.1.7 58107 typ host
// a=candidate:<foundation> <component-id> <transport> <priority> <address> <port> typ <cand-type>
std::string foundation;
// 传输媒体的类型,1代表RTP;2代表 RTCP。
// 传输媒体的类型,1代表RTP;2代表 RTCP。 [AUTO-TRANSLATED:9ec924a6]
// The type of media to be transmitted, 1 represents RTP; 2 represents RTCP.
uint32_t component;
std::string transport { "udp" };
uint32_t priority;
@@ -567,7 +594,8 @@ public:
//////////////////////////////////////////////////////////////////
// ssrc相关信息
// ssrc相关信息 [AUTO-TRANSLATED:954c641d]
// ssrc related information
class RtcSSRC {
public:
uint32_t ssrc { 0 };
@@ -580,23 +608,27 @@ public:
bool empty() const { return ssrc == 0 && cname.empty(); }
};
// rtc传输编码方案
// rtc传输编码方案 [AUTO-TRANSLATED:8b911508]
// rtc transmission encoding scheme
class RtcCodecPlan {
public:
using Ptr = std::shared_ptr<RtcCodecPlan>;
uint8_t pt;
std::string codec;
uint32_t sample_rate;
// 音频时有效
// 音频时有效 [AUTO-TRANSLATED:5b230fc8]
// Valid for audio
uint32_t channel = 0;
// rtcp反馈
// rtcp反馈 [AUTO-TRANSLATED:580378bd]
// RTCP feedback
std::set<std::string> rtcp_fb;
std::map<std::string /*key*/, std::string /*value*/, StrCaseCompare> fmtp;
std::string getFmtp(const char *key) const;
};
// rtc 媒体描述
// rtc 媒体描述 [AUTO-TRANSLATED:b1711a11]
// RTC media description
class RtcMedia {
public:
TrackType type { TrackType::TrackInvalid };

View File

@@ -39,7 +39,8 @@ void TwccContext::onRtp(uint32_t ssrc, uint16_t twcc_ext_seq, uint64_t stamp_ms)
}
if (needSendTwcc()) {
//其他匹配条件立即发送twcc
// 其他匹配条件立即发送twcc [AUTO-TRANSLATED:959d22b6]
// Send twcc immediately if other matching conditions are met
onSendTwcc(ssrc);
}
}
@@ -58,25 +59,30 @@ int TwccContext::checkSeqStatus(uint16_t twcc_ext_seq) const {
auto max = _rtp_recv_status.rbegin()->first;
auto delta = (int32_t) twcc_ext_seq - (int32_t) max;
if (delta > 0 && delta < 0xFFFF / 2) {
//正常增长
// 正常增长 [AUTO-TRANSLATED:7699c37d]
// Normal growth
return (int) ExtSeqStatus::normal;
}
if (delta < -0xFF00) {
//回环
// 回环 [AUTO-TRANSLATED:82956e03]
// Loop
TraceL << "rtp twcc ext seq looped:" << max << " -> " << twcc_ext_seq;
return (int) ExtSeqStatus::looped;
}
if (delta > 0xFF00) {
//回环后收到前面大的乱序的包,无法处理,丢弃
// 回环后收到前面大的乱序的包,无法处理,丢弃 [AUTO-TRANSLATED:512cc269]
// Discard packets that are out of order and large after looping back, as they cannot be processed
TraceL << "rtp twcc ext seq jumped after looped:" << max << " -> " << twcc_ext_seq;
return (int) ExtSeqStatus::jumped;
}
auto min = _rtp_recv_status.begin()->first;
if (min <= twcc_ext_seq || twcc_ext_seq <= max) {
//正常回退
// 正常回退 [AUTO-TRANSLATED:c8c6803f]
// Normal rollback
return (int) ExtSeqStatus::normal;
}
//seq莫名的大幅增加或减少无法处理丢弃
// seq莫名的大幅增加或减少无法处理丢弃 [AUTO-TRANSLATED:44dbbd28]
// Discard packets with a large increase or decrease in seq, as they cannot be processed
TraceL << "rtp twcc ext seq jumped:" << max << " -> " << twcc_ext_seq;
return (int) ExtSeqStatus::jumped;
}
@@ -85,9 +91,11 @@ void TwccContext::onSendTwcc(uint32_t ssrc) {
auto max = _rtp_recv_status.rbegin()->first;
auto begin = _rtp_recv_status.begin();
auto min = begin->first;
//参考时间戳的最小单位是64ms
// 参考时间戳的最小单位是64ms [AUTO-TRANSLATED:2e701a8c]
// The minimum unit of the reference timestamp is 64ms
auto ref_time = begin->second >> 6;
//还原基准时间戳
// 还原基准时间戳 [AUTO-TRANSLATED:bab53195]
// Restore the baseline timestamp
auto last_time = ref_time << 6;
FCI_TWCC::TwccPacketStatus status;
for (auto seq = min; seq <= max; ++seq) {
@@ -95,7 +103,8 @@ void TwccContext::onSendTwcc(uint32_t ssrc) {
SymbolStatus symbol = SymbolStatus::not_received;
auto it = _rtp_recv_status.find(seq);
if (it != _rtp_recv_status.end()) {
//recv delta,单位为250us,1ms等于4x250us
// recv delta,单位为250us,1ms等于4x250us [AUTO-TRANSLATED:46a0e186]
// recv delta, unit is 250us, 1ms equals 4x250us
delta = (int16_t) (4 * ((int64_t) it->second - (int64_t) last_time));
if (delta < 0 || delta > 0xFF) {
symbol = SymbolStatus::large_delta;

View File

@@ -21,9 +21,11 @@ namespace mediakit {
class TwccContext {
public:
using onSendTwccCB = std::function<void(uint32_t ssrc, std::string fci)>;
//每个twcc rtcp包最多表明的rtp ext seq增量
// 每个twcc rtcp包最多表明的rtp ext seq增量 [AUTO-TRANSLATED:530d1e35]
// Maximum RTP ext seq increment indicated by each twcc rtcp packet
static constexpr size_t kMaxSeqSize = 20;
//每个twcc rtcp包发送的最大时间间隔单位毫秒
// 每个twcc rtcp包发送的最大时间间隔单位毫秒 [AUTO-TRANSLATED:e45656da]
// Maximum time interval for sending each twcc rtcp packet, in milliseconds
static constexpr size_t kMaxTimeDelta = 256;
void onRtp(uint32_t ssrc, uint16_t twcc_ext_seq, uint64_t stamp_ms);

View File

@@ -39,7 +39,8 @@ void WebRtcEchoTest::onRtcp(const char *buf, size_t len) {
sendRtcpPacket(buf, len, true, nullptr);
}
//修改mline的a=msid属性目的是在echo test的情况下如果offer和answer的msid相同chrome会忽略远端的track
// 修改mline的a=msid属性目的是在echo test的情况下如果offer和answer的msid相同chrome会忽略远端的track [AUTO-TRANSLATED:f2e3da54]
// Modify the a=msid attribute of mline, the purpose is that in the echo test case, if the offer and answer have the same msid, chrome will ignore the remote track.
void WebRtcEchoTest::onCheckSdp(SdpType type, RtcSession &sdp) {
if (type == SdpType::answer) {
for (auto &m : sdp.media) {

View File

@@ -90,8 +90,10 @@ void WebRtcPlayer::onStartWebRTC() {
}
if (data.is<Buffer>()) {
auto &buffer = data.get<Buffer>();
// PPID 51: 文本string
// PPID 53: 二进制
// PPID 51: 文本string [AUTO-TRANSLATED:69a8cf81]
// PPID 51: Text string
// PPID 53: 二进制 [AUTO-TRANSLATED:faf00c3e]
// PPID 53: Binary
strong_self->sendDatachannel(0, 51, buffer.data(), buffer.size());
} else {
WarnL << "Send unknown message type to webrtc player: " << data.type_name();
@@ -102,7 +104,8 @@ void WebRtcPlayer::onStartWebRTC() {
void WebRtcPlayer::onDestory() {
auto duration = getDuration();
auto bytes_usage = getBytesUsage();
//流量统计事件广播
// 流量统计事件广播 [AUTO-TRANSLATED:6b0b1234]
// Traffic statistics event broadcast
GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
if (_reader && getSession()) {
WarnL << "RTC播放器(" << _media_info.shortUrl() << ")结束播放,耗时(s):" << duration;
@@ -119,7 +122,8 @@ void WebRtcPlayer::onRtcConfigure(RtcConfigure &configure) const {
return ;
}
WebRtcTransportImp::onRtcConfigure(configure);
//这是播放
// 这是播放 [AUTO-TRANSLATED:d93c019e]
// This is playing
configure.audio.direction = configure.video.direction = RtpDirection::sendonly;
configure.setPlayRtspInfo(playSrc->getSdp());
}

View File

@@ -34,15 +34,19 @@ private:
void sendConfigFrames(uint32_t before_seq, uint32_t sample_rate, uint32_t timestamp, uint64_t ntp_timestamp);
private:
//媒体相关元数据
// 媒体相关元数据 [AUTO-TRANSLATED:f4cf8045]
// Media related metadata
MediaInfo _media_info;
//播放的rtsp源
// 播放的rtsp源 [AUTO-TRANSLATED:9963eed1]
// Playing rtsp source
std::weak_ptr<RtspMediaSource> _play_src;
// rtp 直接转发情况下通常会缺少 sps/pps, 在转发 rtp 前, 先发送一次相关帧信息, 部分情况下是可以播放的
// rtp 直接转发情况下通常会缺少 sps/pps, 在转发 rtp 前, 先发送一次相关帧信息, 部分情况下是可以播放的 [AUTO-TRANSLATED:65fdf16a]
// In the case of direct RTP forwarding, sps/pps is usually missing. Before forwarding RTP, send the relevant frame information once. In some cases, it can be played.
bool _send_config_frames_once { false };
//播放rtsp源的reader对象
// 播放rtsp源的reader对象 [AUTO-TRANSLATED:7b305055]
// Reader object for playing rtsp source
RtspMediaSource::RingType::RingReader::Ptr _reader;
};

View File

@@ -42,14 +42,16 @@ WebRtcPusher::WebRtcPusher(const EventPoller::Ptr &poller,
}
bool WebRtcPusher::close(MediaSource &sender) {
//此回调在其他线程触发
// 此回调在其他线程触发 [AUTO-TRANSLATED:c98e7686]
// This callback is triggered in another thread
string err = StrPrinter << "close media: " << sender.getUrl();
weak_ptr<WebRtcPusher> weak_self = static_pointer_cast<WebRtcPusher>(shared_from_this());
getPoller()->async([weak_self, err]() {
auto strong_self = weak_self.lock();
if (strong_self) {
strong_self->onShutdown(SockException(Err_shutdown, err));
//主动关闭推流,那么不延时注销
// 主动关闭推流,那么不延时注销 [AUTO-TRANSLATED:ee7cc580]
// Actively close the stream, then do not delay the logout
strong_self->_push_src = nullptr;
}
});
@@ -91,12 +93,14 @@ void WebRtcPusher::onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Pt
}
if (rtp->type == TrackAudio) {
//音频
// 音频 [AUTO-TRANSLATED:a577d8e1]
// Audio
for (auto &pr : _push_src_sim) {
pr.second->onWrite(rtp, false);
}
} else {
//视频
// 视频 [AUTO-TRANSLATED:904730ac]
// Video
std::lock_guard<std::recursive_mutex> lock(_mtx);
auto &src = _push_src_sim[rid];
if (!src) {
@@ -121,7 +125,8 @@ void WebRtcPusher::onStartWebRTC() {
void WebRtcPusher::onDestory() {
auto duration = getDuration();
auto bytes_usage = getBytesUsage();
//流量统计事件广播
// 流量统计事件广播 [AUTO-TRANSLATED:6b0b1234]
// Traffic statistics event broadcast
GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
if (getSession()) {
@@ -132,9 +137,11 @@ void WebRtcPusher::onDestory() {
}
if (_push_src && _continue_push_ms) {
//取消所有权
// 取消所有权 [AUTO-TRANSLATED:4895d8fa]
// Cancel ownership
_push_src_ownership = nullptr;
//延时10秒注销流
// 延时10秒注销流 [AUTO-TRANSLATED:e1bb11f9]
// Delay 10 seconds to log out the stream
auto push_src = std::move(_push_src);
getPoller()->doDelayTask(_continue_push_ms, [push_src]() { return 0; });
}
@@ -143,7 +150,8 @@ void WebRtcPusher::onDestory() {
void WebRtcPusher::onRtcConfigure(RtcConfigure &configure) const {
WebRtcTransportImp::onRtcConfigure(configure);
//这只是推流
// 这只是推流 [AUTO-TRANSLATED:f877bf98]
// This is just pushing the stream
configure.audio.direction = configure.video.direction = RtpDirection::recvonly;
}
@@ -152,7 +160,8 @@ float WebRtcPusher::getLossRate(MediaSource &sender,TrackType type) {
}
void WebRtcPusher::OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) {
//主动关闭推流,那么不等待重推
// 主动关闭推流,那么不等待重推 [AUTO-TRANSLATED:1ff514d7]
// Actively close the stream, then do not wait for re-pushing
_push_src = nullptr;
WebRtcTransportImp::OnDtlsTransportClosed(dtlsTransport);
}

View File

@@ -30,24 +30,32 @@ protected:
void onRecvRtp(MediaTrack &track, const std::string &rid, RtpPacket::Ptr rtp) override;
void onShutdown(const SockException &ex) override;
void onRtcpBye() override;
//// dtls相关的回调 ////
// // dtls相关的回调 //// [AUTO-TRANSLATED:31a1f32c]
// // dtls related callbacks ////
void OnDtlsTransportClosed(const RTC::DtlsTransport *dtlsTransport) override;
protected:
///////MediaSourceEvent override///////
// 关闭
// 关闭 [AUTO-TRANSLATED:92392f02]
// Close
bool close(MediaSource &sender) override;
// 播放总人数
// 播放总人数 [AUTO-TRANSLATED:c42a3161]
// Total number of players
int totalReaderCount(MediaSource &sender) override;
// 获取媒体源类型
// 获取媒体源类型 [AUTO-TRANSLATED:34290a69]
// Get media source type
MediaOriginType getOriginType(MediaSource &sender) const override;
// 获取媒体源url或者文件路径
// 获取媒体源url或者文件路径 [AUTO-TRANSLATED:fa34d795]
// Get media source url or file path
std::string getOriginUrl(MediaSource &sender) const override;
// 获取媒体源客户端相关信息
// 获取媒体源客户端相关信息 [AUTO-TRANSLATED:037ef910]
// Get media source client related information
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
// 由于支持断连续推存在OwnerPoller变更的可能
// 由于支持断连续推存在OwnerPoller变更的可能 [AUTO-TRANSLATED:1c863b40]
// Due to support for discontinuous pushing, there is a possibility of OwnerPoller changes
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
// 获取丢包率
// 获取丢包率 [AUTO-TRANSLATED:ec61b378]
// Get packet loss rate
float getLossRate(MediaSource &sender,TrackType type) override;
private:
@@ -56,15 +64,20 @@ private:
private:
bool _simulcast = false;
//断连续推延时
// 断连续推延时 [AUTO-TRANSLATED:13ad578a]
// Discontinuous pushing delay
uint32_t _continue_push_ms = 0;
//媒体相关元数据
// 媒体相关元数据 [AUTO-TRANSLATED:f4cf8045]
// Media related metadata
MediaInfo _media_info;
//推流的rtsp源
// 推流的rtsp源 [AUTO-TRANSLATED:4f976bca]
// Rtsp source of the stream
RtspMediaSource::Ptr _push_src;
//推流所有权
// 推流所有权 [AUTO-TRANSLATED:d0ddf5c7]
// Stream ownership
std::shared_ptr<void> _push_src_ownership;
//推流的rtsp源,支持simulcast
// 推流的rtsp源,支持simulcast [AUTO-TRANSLATED:44be9120]
// Rtsp source of the stream, supports simulcast
std::recursive_mutex _mtx;
std::unordered_map<std::string/*rid*/, RtspMediaSource::Ptr> _push_src_sim;
std::unordered_map<std::string/*rid*/, std::shared_ptr<void> > _push_src_sim_ownership;

View File

@@ -31,7 +31,8 @@ static string getUserName(const char *buf, size_t len) {
packet->GetMethod() != RTC::StunPacket::Method::BINDING) {
return "";
}
//收到binding request请求
// 收到binding request请求 [AUTO-TRANSLATED:eff4d773]
// Received binding request
auto vec = split(packet->GetUsername(), ":");
return vec[0];
}
@@ -57,16 +58,19 @@ void WebRtcSession::attachServer(const Server &server) {
void WebRtcSession::onRecv_l(const char *data, size_t len) {
if (_find_transport) {
// 只允许寻找一次transport
// 只允许寻找一次transport [AUTO-TRANSLATED:446fae53]
// Only allow searching for transport once
_find_transport = false;
auto user_name = getUserName(data, len);
auto transport = WebRtcTransportManager::Instance().getItem(user_name);
CHECK(transport);
//WebRtcTransport在其他poller线程上需要切换poller线程并重新创建WebRtcSession对象
// WebRtcTransport在其他poller线程上需要切换poller线程并重新创建WebRtcSession对象 [AUTO-TRANSLATED:7e5534cf]
// WebRtcTransport is on another poller thread, need to switch poller thread and recreate WebRtcSession object
if (!transport->getPoller()->isCurrentThread()) {
auto sock = Socket::createSocket(transport->getPoller(), false);
//1、克隆socket(fd不变)切换poller线程到WebRtcTransport所在线程
// 1、克隆socket(fd不变)切换poller线程到WebRtcTransport所在线程 [AUTO-TRANSLATED:f930bfab]
// 1. Clone socket (fd remains unchanged), switch poller thread to the thread where WebRtcTransport is located
sock->cloneSocket(*(getSock()));
auto server = _server;
std::string str(data, len);
@@ -74,11 +78,13 @@ void WebRtcSession::onRecv_l(const char *data, size_t len) {
auto strong_server = server.lock();
if (strong_server) {
auto session = static_pointer_cast<WebRtcSession>(strong_server->createSession(sock));
//2、创建新的WebRtcSession对象(绑定到WebRtcTransport所在线程)重新处理一遍ice binding request命令
// 2、创建新的WebRtcSession对象(绑定到WebRtcTransport所在线程)重新处理一遍ice binding request命令 [AUTO-TRANSLATED:c75203bb]
// 2. Create a new WebRtcSession object (bound to the thread where WebRtcTransport is located), reprocess the ice binding request command
session->onRecv_l(str.data(), str.size());
}
});
//3、销毁原先的socket和WebRtcSession(原先的对象跟WebRtcTransport不在同一条线程)
// 3、销毁原先的socket和WebRtcSession(原先的对象跟WebRtcTransport不在同一条线程) [AUTO-TRANSLATED:a6d6d63f]
// 3. Destroy the original socket and WebRtcSession (the original object is not on the same thread as WebRtcTransport)
throw std::runtime_error("webrtc over tcp change poller: " + getPoller()->getThreadName() + " -> " + sock->getPoller()->getThreadName());
}
_transport = std::move(transport);
@@ -98,9 +104,12 @@ void WebRtcSession::onRecv(const Buffer::Ptr &buffer) {
}
void WebRtcSession::onError(const SockException &err) {
//udp链接超时但是rtc链接不一定超时因为可能存在链接迁移的情况
//在udp链接迁移时新的WebRtcSession对象将接管WebRtcTransport对象的生命周期
//WebRtcSession对象将在超时后自动销毁
// udp链接超时但是rtc链接不一定超时因为可能存在链接迁移的情况 [AUTO-TRANSLATED:aaa9672f]
// UDP connection timeout, but RTC connection may not timeout, because there may be connection migration
// 在udp链接迁移时新的WebRtcSession对象将接管WebRtcTransport对象的生命周期 [AUTO-TRANSLATED:7e7d19df]
// When UDP connection migrates, the new WebRtcSession object will take over the life cycle of the WebRtcTransport object
// 本WebRtcSession对象将在超时后自动销毁 [AUTO-TRANSLATED:bc903a06]
// This WebRtcSession object will be automatically destroyed after timeout
WarnP(this) << err;
if (!_transport) {
@@ -109,9 +118,11 @@ void WebRtcSession::onError(const SockException &err) {
auto self = static_pointer_cast<WebRtcSession>(shared_from_this());
auto transport = std::move(_transport);
getPoller()->async([transport, self]() mutable {
//延时减引用防止使用transport对象时销毁对象
// 延时减引用防止使用transport对象时销毁对象 [AUTO-TRANSLATED:09dd6609]
// Delay decrementing the reference count to prevent the object from being destroyed when using the transport object
transport->removeTuple(self.get());
//确保transport在Session对象前销毁防止WebRtcTransport::onDestory()时获取不到Session对象
// 确保transport在Session对象前销毁防止WebRtcTransport::onDestory()时获取不到Session对象 [AUTO-TRANSLATED:acd8bd77]
// Ensure that the transport is destroyed before the Session object to prevent WebRtcTransport::onDestory() from not being able to get the Session object
transport = nullptr;
}, false);
}
@@ -135,15 +146,18 @@ ssize_t WebRtcSession::onRecvHeader(const char *data, size_t len) {
const char *WebRtcSession::onSearchPacketTail(const char *data, size_t len) {
if (len < 2) {
// 数据不够
// 数据不够 [AUTO-TRANSLATED:830a2785]
// Not enough data
return nullptr;
}
uint16_t length = (((uint8_t *)data)[0] << 8) | ((uint8_t *)data)[1];
if (len < (size_t)(length + 2)) {
// 数据不够
// 数据不够 [AUTO-TRANSLATED:830a2785]
// Not enough data
return nullptr;
}
// 返回rtp包末尾
// 返回rtp包末尾 [AUTO-TRANSLATED:5134cf6f]
// Return the end of the RTP packet
return data + 2 + length;
}

View File

@@ -37,25 +37,32 @@ using namespace std;
namespace mediakit {
// RTC配置项目
// RTC配置项目 [AUTO-TRANSLATED:19940011]
// RTC configuration project
namespace Rtc {
#define RTC_FIELD "rtc."
// rtp和rtcp接受超时时间
// rtp和rtcp接受超时时间 [AUTO-TRANSLATED:0f318cc0]
// rtp and rtcp receive timeout
const string kTimeOutSec = RTC_FIELD "timeoutSec";
// 服务器外网ip
// 服务器外网ip [AUTO-TRANSLATED:23283ba6]
// Server external network ip
const string kExternIP = RTC_FIELD "externIP";
// 设置remb比特率非0时关闭twcc并开启remb。该设置在rtc推流时有效可以控制推流画质
// 设置remb比特率非0时关闭twcc并开启remb。该设置在rtc推流时有效可以控制推流画质 [AUTO-TRANSLATED:412801db]
// Set remb bitrate, when it is not 0, turn off twcc and turn on remb. This setting is valid when rtc pushes the stream, and can control the pushing stream quality
const string kRembBitRate = RTC_FIELD "rembBitRate";
// webrtc单端口udp服务器
// webrtc单端口udp服务器 [AUTO-TRANSLATED:d17271ea]
// webrtc single-port udp server
const string kPort = RTC_FIELD "port";
const string kTcpPort = RTC_FIELD "tcpPort";
// 比特率设置
// 比特率设置 [AUTO-TRANSLATED:2c75f5bc]
// Bitrate setting
const string kStartBitrate = RTC_FIELD "start_bitrate";
const string kMaxBitrate = RTC_FIELD "max_bitrate";
const string kMinBitrate = RTC_FIELD "min_bitrate";
// 数据通道设置
// 数据通道设置 [AUTO-TRANSLATED:2dc48bc3]
// Data channel setting
const string kDataChannelEcho = RTC_FIELD "datachannel_echo";
static onceToken token([]() {
@@ -92,21 +99,28 @@ static void translateIPFromEnv(std::vector<std::string> &v) {
}
static std::string getServerPrefix() {
//stun_user_name格式: base64(ip+udp_port+tcp_port) + _ + number
//其中ip为二进制char[4], udp_port/tcp_port为大端 uint16.
//number为自增长数确保短时间内唯一
// stun_user_name格式: base64(ip+udp_port+tcp_port) + _ + number [AUTO-TRANSLATED:cc3c5902]
// stun_user_name format: base64(ip+udp_port+tcp_port) + _ + number
// 其中ip为二进制char[4], udp_port/tcp_port为大端 uint16. [AUTO-TRANSLATED:92ea5521]
// Where ip is binary char[4], udp_port/tcp_port is big-endian uint16.
// number为自增长数确保短时间内唯一 [AUTO-TRANSLATED:d31aada9]
// number is an auto-incrementing number, ensuring uniqueness in a short period of time
GET_CONFIG(uint16_t, udp_port, Rtc::kPort);
GET_CONFIG(uint16_t, tcp_port, Rtc::kTcpPort);
char buf[8];
auto host = SockUtil::get_local_ip();
auto addr = SockUtil::make_sockaddr(host.data(), udp_port);
//拷贝ipv4地址
// 拷贝ipv4地址 [AUTO-TRANSLATED:49c16eed]
// Copy ipv4 address
memcpy(buf, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_addr), 4);
//拷贝udp端口
// 拷贝udp端口 [AUTO-TRANSLATED:ebb750d3]
// Copy udp port
memcpy(buf + 4, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_port), 2);
//tcp端口转大端模式
// tcp端口转大端模式 [AUTO-TRANSLATED:4f2293de]
// Convert tcp port to big-endian mode
addr = SockUtil::make_sockaddr(host.data(), tcp_port);
//拷贝tcp端口
// 拷贝tcp端口 [AUTO-TRANSLATED:23191878]
// Copy tcp port
memcpy(buf + 6, &(reinterpret_cast<sockaddr_in *>(&addr)->sin_port), 2);
auto ret = encodeBase64(string(buf, 8)) + '_';
InfoL << "MediaServer(" << host << ":" << udp_port << ":" << tcp_port << ") prefix: " << ret;
@@ -307,7 +321,8 @@ void WebRtcTransport::OnSctpAssociationMessageReceived(
GET_CONFIG(bool, datachannel_echo, Rtc::kDataChannelEcho);
if (datachannel_echo) {
// 回显数据
// 回显数据 [AUTO-TRANSLATED:7868d3a4]
// Echo data
_sctp->SendSctpMessage(params, ppid, msg, len);
}
@@ -370,7 +385,8 @@ string getFingerprint(const string &algorithm_str, const std::shared_ptr<RTC::Dt
}
void WebRtcTransport::setRemoteDtlsFingerprint(const RtcSession &remote) {
// 设置远端dtls签名
// 设置远端dtls签名 [AUTO-TRANSLATED:746d5f9c]
// Set remote dtls signature
RTC::DtlsTransport::Fingerprint remote_fingerprint;
remote_fingerprint.algorithm
= RTC::DtlsTransport::GetFingerprintAlgorithm(_offer_sdp->media[0].fingerprint.algorithm);
@@ -385,7 +401,8 @@ void WebRtcTransport::onRtcConfigure(RtcConfigure &configure) const {
configure.setDefaultSetting(
_ice_server->GetUsernameFragment(), _ice_server->GetPassword(), RtpDirection::sendrecv, fingerprint);
// 开启remb后关闭twcc因为开启twcc后remb无效
// 开启remb后关闭twcc因为开启twcc后remb无效 [AUTO-TRANSLATED:8a8feca2]
// Turn off twcc after turning on remb, because remb is invalid after turning on twcc
GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate);
configure.enableTWCC(!remb_bit_rate);
}
@@ -406,18 +423,21 @@ static void setSdpBitrate(RtcSession &sdp) {
std::string WebRtcTransport::getAnswerSdp(const string &offer) {
try {
//// 解析offer sdp ////
// // 解析offer sdp //// [AUTO-TRANSLATED:87c1f337]
// // Parse offer sdp ////
_offer_sdp = std::make_shared<RtcSession>();
_offer_sdp->loadFrom(offer);
onCheckSdp(SdpType::offer, *_offer_sdp);
_offer_sdp->checkValid();
setRemoteDtlsFingerprint(*_offer_sdp);
//// sdp 配置 ////
// // sdp 配置 //// [AUTO-TRANSLATED:718a72e2]
// // sdp configuration ////
RtcConfigure configure;
onRtcConfigure(configure);
//// 生成answer sdp ////
// // 生成answer sdp //// [AUTO-TRANSLATED:a139475e]
// // Generate answer sdp ////
_answer_sdp = configure.createAnswer(*_offer_sdp);
onCheckSdp(SdpType::answer, *_answer_sdp);
setSdpBitrate(*_answer_sdp);
@@ -472,7 +492,8 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup
void WebRtcTransport::sendRtpPacket(const char *buf, int len, bool flush, void *ctx) {
if (_srtp_session_send) {
auto pkt = _packet_pool.obtain2();
// 预留rtx加入的两个字节
// 预留rtx加入的两个字节 [AUTO-TRANSLATED:d1eb5cd7]
// Reserve two bytes for rtx joining
pkt->setCapacity((size_t)len + SRTP_MAX_TRAILER_LEN + 2);
memcpy(pkt->data(), buf, len);
onBeforeEncryptRtp(pkt->data(), len, ctx);
@@ -486,7 +507,8 @@ void WebRtcTransport::sendRtpPacket(const char *buf, int len, bool flush, void *
void WebRtcTransport::sendRtcpPacket(const char *buf, int len, bool flush, void *ctx) {
if (_srtp_session_send) {
auto pkt = _packet_pool.obtain2();
// 预留rtx加入的两个字节
// 预留rtx加入的两个字节 [AUTO-TRANSLATED:d1eb5cd7]
// Reserve two bytes for rtx joining
pkt->setCapacity((size_t)len + SRTP_MAX_TRAILER_LEN + 2);
memcpy(pkt->data(), buf, len);
onBeforeEncryptRtcp(pkt->data(), len, ctx);
@@ -553,9 +575,11 @@ void WebRtcTransportImp::onSendSockData(Buffer::Ptr buf, bool flush, RTC::Transp
}
}
// 一次性发送一帧的rtp数据提高网络io性能
// 一次性发送一帧的rtp数据提高网络io性能 [AUTO-TRANSLATED:fbab421e]
// Send one frame of rtp data at a time to improve network io performance
if (tuple->getSock()->sockType() == SockNum::Sock_TCP) {
// 增加tcp两字节头
// 增加tcp两字节头 [AUTO-TRANSLATED:62159f79]
// Add two-byte header to tcp
auto len = buf->size();
char tcp_len[2] = { 0 };
tcp_len[0] = (len >> 8) & 0xff;
@@ -590,7 +614,8 @@ bool WebRtcTransportImp::canRecvRtp() const {
}
void WebRtcTransportImp::onStartWebRTC() {
// 获取ssrc和pt相关信息,届时收到rtp和rtcp时分别可以根据pt和ssrc找到相关的信息
// 获取ssrc和pt相关信息,届时收到rtp和rtcp时分别可以根据pt和ssrc找到相关的信息 [AUTO-TRANSLATED:39828247]
// Get ssrc and pt related information, so that when receiving rtp and rtcp, you can find the relevant information according to pt and ssrc respectively
for (auto &m_answer : _answer_sdp->media) {
if (m_answer.type == TrackApplication) {
continue;
@@ -609,7 +634,8 @@ void WebRtcTransportImp::onStartWebRTC() {
// rtp track type --> MediaTrack
if (m_answer.direction == RtpDirection::sendonly || m_answer.direction == RtpDirection::sendrecv) {
// 该类型的track 才支持发送
// 该类型的track 才支持发送 [AUTO-TRANSLATED:b7c1e631]
// This type of track supports sending
_type_to_track[m_answer.type] = track;
}
// send ssrc --> MediaTrack
@@ -627,7 +653,8 @@ void WebRtcTransportImp::onStartWebRTC() {
// rtx pt --> MediaTrack
_pt_to_track.emplace(track->plan_rtx->pt, std::unique_ptr<WrappedMediaTrack>(new WrappedRtxTrack(track)));
}
// 记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id
// 记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id [AUTO-TRANSLATED:5736bd34]
// Record the relationship between rtp ext type and id, which is convenient for modifying rtp ext id when receiving or sending rtp
track->rtp_ext_ctx = std::make_shared<RtpExtContext>(m_answer);
weak_ptr<MediaTrack> weak_track = track;
track->rtp_ext_ctx->setOnGetRtp([this, weak_track](uint8_t pt, uint32_t ssrc, const string &rid) {
@@ -640,14 +667,18 @@ void WebRtcTransportImp::onStartWebRTC() {
size_t index = 0;
for (auto &ssrc : m_offer->rtp_ssrc_sim) {
// 记录ssrc对应的MediaTrack
// 记录ssrc对应的MediaTrack [AUTO-TRANSLATED:8e344bc1]
// Record the MediaTrack corresponding to ssrc
_ssrc_to_track[ssrc.ssrc] = track;
if (m_offer->rtp_rids.size() > index) {
// 支持firefox的simulcast, 提前映射好ssrc和rid的关系
// 支持firefox的simulcast, 提前映射好ssrc和rid的关系 [AUTO-TRANSLATED:86f3e5bf]
// Support firefox's simulcast, map the relationship between ssrc and rid in advance
track->rtp_ext_ctx->setRid(ssrc.ssrc, m_offer->rtp_rids[index]);
} else {
// SDP munging没有rid, 它通过group-ssrc:SIM给出ssrc列表;
// 系统又要有rid这里手工生成rid并为其绑定ssrc
// SDP munging没有rid, 它通过group-ssrc:SIM给出ssrc列表; [AUTO-TRANSLATED:d6cd0b5b]
// SDP munging does not have rid, it gives the ssrc list through group-ssrc:SIM;
// 系统又要有rid这里手工生成rid并为其绑定ssrc [AUTO-TRANSLATED:f4988139]
// The system also needs a rid, so we manually generate a rid and bind it to the ssrc
std::string rid = "r" + std::to_string(index);
track->rtp_ext_ctx->setRid(ssrc.ssrc, rid);
if (ssrc.rtx_ssrc) {
@@ -660,7 +691,8 @@ void WebRtcTransportImp::onStartWebRTC() {
}
void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
// 修改answer sdp的ip、端口信息
// 修改answer sdp的ip、端口信息 [AUTO-TRANSLATED:ff72ec1e]
// Modify the ip and port information of the answer sdp
GET_CONFIG_FUNC(std::vector<std::string>, extern_ips, Rtc::kExternIP, [](string str) {
std::vector<std::string> ret;
if (str.length()) {
@@ -685,7 +717,8 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
}
if (!canSendRtp()) {
// 设置我们发送的rtp的ssrc
// 设置我们发送的rtp的ssrc [AUTO-TRANSLATED:3704484a]
// Set the ssrc of the rtp we send
return;
}
@@ -694,13 +727,16 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
continue;
}
if (!m.rtp_rtx_ssrc.empty()) {
// 已经生成了ssrc
// 已经生成了ssrc [AUTO-TRANSLATED:5883cab8]
// The ssrc has been generated
continue;
}
// 添加answer sdp的ssrc信息
// 添加answer sdp的ssrc信息 [AUTO-TRANSLATED:ab4c3fad]
// Add the ssrc information to the answer sdp
m.rtp_rtx_ssrc.emplace_back();
auto &ssrc = m.rtp_rtx_ssrc.back();
// 发送的ssrc我们随便定义因为在发送rtp时会修改为此值
// 发送的ssrc我们随便定义因为在发送rtp时会修改为此值 [AUTO-TRANSLATED:ee8d77f0]
// We can define the ssrc we send at will, because it will be modified to this value when sending rtp
ssrc.ssrc = m.type + RTP_SSRC_OFFSET;
ssrc.cname = RTP_CNAME;
ssrc.label = std::string(RTP_LABEL) + '-' + m.mid;
@@ -730,11 +766,13 @@ void WebRtcTransportImp::onCheckSdp(SdpType type, RtcSession &sdp) {
SdpAttrCandidate::Ptr
makeIceCandidate(std::string ip, uint16_t port, uint32_t priority = 100, std::string proto = "udp") {
auto candidate = std::make_shared<SdpAttrCandidate>();
// rtp端口
// rtp端口 [AUTO-TRANSLATED:b0addb27]
// rtp port
candidate->component = 1;
candidate->transport = proto;
candidate->foundation = proto + "candidate";
// 优先级单candidate时随便
// 优先级单candidate时随便 [AUTO-TRANSLATED:7c85d820]
// Priority, random when there is only one candidate
candidate->priority = priority;
candidate->address = std::move(ip);
candidate->port = port;
@@ -756,7 +794,8 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
GET_CONFIG(uint16_t, local_udp_port, Rtc::kPort);
GET_CONFIG(uint16_t, local_tcp_port, Rtc::kTcpPort);
// 添加接收端口candidate信息
// 添加接收端口candidate信息 [AUTO-TRANSLATED:cc9a6a90]
// Add the receiving port candidate information
GET_CONFIG_FUNC(std::vector<std::string>, extern_ips, Rtc::kExternIP, [](string str) {
std::vector<std::string> ret;
if (str.length()) {
@@ -800,7 +839,8 @@ public:
_poller = std::move(poller);
_on_nack = std::move(on_nack);
setOnSorted(std::move(cb));
//设置jitter buffer参数
// 设置jitter buffer参数 [AUTO-TRANSLATED:eede98b6]
// Set jitter buffer parameters
GET_CONFIG(uint32_t, nack_maxms, Rtc::kNackMaxMS);
GET_CONFIG(uint32_t, nack_max_rtp, Rtc::kNackMaxSize);
RtpTrackImp::setParams(nack_max_rtp, nack_maxms, nack_max_rtp / 2);
@@ -815,7 +855,8 @@ public:
auto seq = rtp->getSeq();
_nack_ctx.received(seq, is_rtx);
if (!is_rtx) {
// 统计rtp接受情况便于生成nack rtcp包
// 统计rtp接受情况便于生成nack rtcp包 [AUTO-TRANSLATED:57e0f80d]
// Statistics of rtp reception, which is convenient for generating nack rtcp packets
_rtcp_context.onRtp(seq, rtp->getStamp(), rtp->ntp_stamp, sample_rate, len);
}
return rtp;
@@ -895,7 +936,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
switch ((RtcpType)rtcp->pt) {
case RtcpType::RTCP_SR: {
_alive_ticker.resetTime();
// 对方汇报rtp发送情况
// 对方汇报rtp发送情况 [AUTO-TRANSLATED:1389b0c8]
// The other party reports the rtp sending situation
RtcpSR *sr = (RtcpSR *)rtcp;
auto it = _ssrc_to_track.find(sr->ssrc);
if (it != _ssrc_to_track.end()) {
@@ -904,7 +946,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
if (!rtp_chn) {
WarnL << "未识别的sr rtcp包:" << rtcp->dumpString();
} else {
// 设置rtp时间戳与ntp时间戳的对应关系
// 设置rtp时间戳与ntp时间戳的对应关系 [AUTO-TRANSLATED:e92f4749]
// Set the correspondence between rtp timestamp and ntp timestamp
rtp_chn->setNtpStamp(sr->rtpts, sr->getNtpUnixStampMS());
auto rr = rtp_chn->createRtcpRR(sr, track->answer_ssrc_rtp);
sendRtcpPacket(rr->data(), rr->size(), true);
@@ -916,7 +959,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
}
case RtcpType::RTCP_RR: {
_alive_ticker.resetTime();
// 对方汇报rtp接收情况
// 对方汇报rtp接收情况 [AUTO-TRANSLATED:77f50a28]
// The other party reports the rtp receiving situation
RtcpRR *rr = (RtcpRR *)rtcp;
for (auto item : rr->getItemList()) {
auto it = _ssrc_to_track.find(item->ssrc);
@@ -932,7 +976,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
break;
}
case RtcpType::RTCP_BYE: {
// 对方汇报停止发送rtp
// 对方汇报停止发送rtp [AUTO-TRANSLATED:96ad0cf3]
// The other party reports the stop sending rtp
RtcpBye *bye = (RtcpBye *)rtcp;
for (auto ssrc : bye->getSSRC()) {
auto it = _ssrc_to_track.find(*ssrc);
@@ -943,7 +988,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
_ssrc_to_track.erase(it);
}
onRtcpBye();
// bye 会在 sender audio track mute 时出现, 因此不能作为 shutdown 的依据
// bye 会在 sender audio track mute 时出现, 因此不能作为 shutdown 的依据 [AUTO-TRANSLATED:d9fdfaac]
// Bye will appear when the sender audio track is muted, so it cannot be used as the basis for shutdown
break;
}
case RtcpType::RTCP_PSFB:
@@ -963,7 +1009,8 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
auto &track = it->second;
auto &fci = fb->getFci<FCI_NACK>();
track->nack_list.forEach(fci, [&](const RtpPacket::Ptr &rtp) {
// rtp重传
// rtp重传 [AUTO-TRANSLATED:62a37e46]
// rtp retransmission
onSendRtp(rtp, true, true);
});
break;
@@ -1005,7 +1052,8 @@ void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, Medi
ref = std::make_shared<RtpChannel>(
getPoller(), [&track, this, rid](RtpPacket::Ptr rtp) mutable { onSortedRtp(track, rid, std::move(rtp)); },
[&track, weak_self, ssrc](const FCI_NACK &nack) mutable {
// nack发送可能由定时器异步触发
// nack发送可能由定时器异步触发 [AUTO-TRANSLATED:186d6723]
// Nack sending may be triggered asynchronously by a timer
auto strong_self = weak_self.lock();
if (strong_self) {
strong_self->onSendNack(track, nack, ssrc);
@@ -1023,7 +1071,8 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len, uint64_t stamp_ms) {
_alive_ticker.resetTime();
RtpHeader *rtp = (RtpHeader *)buf;
// 根据接收到的rtp的pt信息找到该流的信息
// 根据接收到的rtp的pt信息找到该流的信息 [AUTO-TRANSLATED:9a97682c]
// Find the information of the stream according to the pt information of the received rtp
auto it = _pt_to_track.find(rtp->pt);
if (it == _pt_to_track.end()) {
WarnL << "unknown rtp pt:" << (int)rtp->pt;
@@ -1036,14 +1085,16 @@ void WrappedRtpTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
#if 0
auto seq = ntohs(rtp->seq);
if (track->media->type == TrackVideo && seq % 100 == 0) {
//此处模拟接受丢包
// 此处模拟接受丢包 [AUTO-TRANSLATED:7e8c2e5c]
// Simulate packet loss here
return;
}
#endif
auto ssrc = ntohl(rtp->ssrc);
// 修改ext id至统一
// 修改ext id至统一 [AUTO-TRANSLATED:0769b0ec]
// Modify the ext id to be unified
string rid;
auto twcc_ext = track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
@@ -1056,24 +1107,28 @@ void WrappedRtpTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
_transport.createRtpChannel(rid, ssrc, *track);
}
// 解析并排序rtp
// 解析并排序rtp [AUTO-TRANSLATED:d382b65d]
// Parse and sort rtp
ref->inputRtp(track->media->type, track->plan_rtp->sample_rate, (uint8_t *)buf, len, false);
}
void WrappedRtxTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, RtpHeader *rtp) {
// 修改ext id至统一
// 修改ext id至统一 [AUTO-TRANSLATED:0769b0ec]
// Modify the ext id to be unified
string rid;
track->rtp_ext_ctx->changeRtpExtId(rtp, true, &rid, RtpExtType::transport_cc);
auto &ref = track->rtp_channel[rid];
if (!ref) {
// 再接收到对应的rtp前丢弃rtx包
// 再接收到对应的rtp前丢弃rtx包 [AUTO-TRANSLATED:d4ca6d69]
// Discard rtx packets before receiving the corresponding rtp
WarnL << "unknown rtx rtp, rid:" << rid << ", ssrc:" << ntohl(rtp->ssrc) << ", codec:" << track->plan_rtp->codec
<< ", seq:" << ntohs(rtp->seq);
return;
}
// 这里是rtx重传包
// 这里是rtx重传包 [AUTO-TRANSLATED:6efd3766]
// This is the rtx retransmission packet
// https://datatracker.ietf.org/doc/html/rfc4588#section-4
auto payload = rtp->getPayloadData();
auto size = rtp->getPayloadSize(len);
@@ -1081,9 +1136,11 @@ void WrappedRtxTrack::inputRtp(const char *buf, size_t len, uint64_t stamp_ms, R
return;
}
// 前两个字节是原始的rtp的seq
// 前两个字节是原始的rtp的seq [AUTO-TRANSLATED:c57ff92d]
// The first two bytes are the original rtp seq
auto origin_seq = payload[0] << 8 | payload[1];
// rtx 转换为 rtp
// rtx 转换为 rtp [AUTO-TRANSLATED:be27f61b]
// rtx converted to rtp
rtp->pt = track->plan_rtp->pt;
rtp->seq = htons(origin_seq);
rtp->ssrc = htonl(ref->getSSRC());
@@ -1112,11 +1169,13 @@ void WebRtcTransportImp::onSendTwcc(uint32_t ssrc, const string &twcc_fci) {
void WebRtcTransportImp::onSortedRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) {
if (track.media->type == TrackVideo && _pli_ticker.elapsedTime() > 2000) {
// 定期发送pli请求关键帧方便非rtc等协议
// 定期发送pli请求关键帧方便非rtc等协议 [AUTO-TRANSLATED:b992f020]
// Regularly send pli requests for key frames, which is convenient for non-rtc protocols
_pli_ticker.resetTime();
sendRtcpPli(rtp->getSSRC());
// 开启remb则发送remb包调节比特率
// 开启remb则发送remb包调节比特率 [AUTO-TRANSLATED:20e98cea]
// If remb is enabled, send remb packets to adjust the bitrate
GET_CONFIG(size_t, remb_bit_rate, Rtc::kRembBitRate);
if (remb_bit_rate && _answer_sdp->supportRtcpFb(SdpConst::kRembRtcpFb)) {
sendRtcpRemb(rtp->getSSRC(), remb_bit_rate);
@@ -1131,23 +1190,27 @@ void WebRtcTransportImp::onSortedRtp(MediaTrack &track, const string &rid, RtpPa
void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool rtx) {
auto &track = _type_to_track[rtp->type];
if (!track) {
// 忽略,对方不支持该编码类型
// 忽略,对方不支持该编码类型 [AUTO-TRANSLATED:498ee936]
// Ignore, the other party does not support this encoding type
return;
}
if (!rtx) {
// 统计rtp发送情况好做sr汇报
// 统计rtp发送情况好做sr汇报 [AUTO-TRANSLATED:142028b2]
// Statistics of RTP sending, for SR reporting
track->rtcp_context_send->onRtp(
rtp->getSeq(), rtp->getStamp(), rtp->ntp_stamp, rtp->sample_rate,
rtp->size() - RtpPacket::kRtpTcpHeaderSize);
track->nack_list.pushBack(rtp);
#if 0
//此处模拟发送丢包
// 此处模拟发送丢包 [AUTO-TRANSLATED:9612f08e]
// Simulate packet loss here
if (rtp->type == TrackVideo && rtp->getSeq() % 100 == 0) {
return;
}
#endif
} else {
// 发送rtx重传包
// 发送rtx重传包 [AUTO-TRANSLATED:ae60e1fd]
// Send RTX retransmission packets
// TraceL << "send rtx rtp:" << rtp->getSeq();
}
pair<bool /*rtx*/, MediaTrack *> ctx { rtx, track.get() };
@@ -1160,31 +1223,37 @@ void WebRtcTransportImp::onBeforeEncryptRtp(const char *buf, int &len, void *ctx
auto header = (RtpHeader *)buf;
if (!pr->first || !pr->second->plan_rtx) {
// 普通的rtp,或者不支持rtx, 修改目标pt和ssrc
// 普通的rtp,或者不支持rtx, 修改目标pt和ssrc [AUTO-TRANSLATED:e1264971]
// Ordinary RTP, or does not support RTX, modify the target PT and SSRC
pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
header->pt = pr->second->plan_rtp->pt;
header->ssrc = htonl(pr->second->answer_ssrc_rtp);
} else {
// 重传的rtp, rtx
// 重传的rtp, rtx [AUTO-TRANSLATED:e863a518]
// Retransmitted RTP, RTX
pr->second->rtp_ext_ctx->changeRtpExtId(header, false);
header->pt = pr->second->plan_rtx->pt;
if (pr->second->answer_ssrc_rtx) {
// 有rtx单独的ssrc,有些情况下浏览器支持rtx但是未指定rtx单独的ssrc
// 有rtx单独的ssrc,有些情况下浏览器支持rtx但是未指定rtx单独的ssrc [AUTO-TRANSLATED:181cee9a]
// RTX has a separate SSRC, in some cases, the browser supports RTX, but does not specify a separate SSRC for RTX
header->ssrc = htonl(pr->second->answer_ssrc_rtx);
} else {
// 未单独指定rtx的ssrc那么使用rtp的ssrc
// 未单独指定rtx的ssrc那么使用rtp的ssrc [AUTO-TRANSLATED:dcafdd75]
// If RTX SSRC is not specified separately, use the RTP SSRC
header->ssrc = htonl(pr->second->answer_ssrc_rtp);
}
auto origin_seq = ntohs(header->seq);
// seq跟原来的不一样
// seq跟原来的不一样 [AUTO-TRANSLATED:803f9a5e]
// The sequence is different from the original
header->seq = htons(_rtx_seq[pr->second->media->type]);
++_rtx_seq[pr->second->media->type];
auto payload = header->getPayloadData();
auto payload_size = header->getPayloadSize(len);
if (payload_size) {
// rtp负载后移两个字节这两个字节用于存放osn
// rtp负载后移两个字节这两个字节用于存放osn [AUTO-TRANSLATED:b7eed70e]
// The RTP payload is shifted two bytes, these two bytes are used to store OSN
// https://datatracker.ietf.org/doc/html/rfc4588#section-4
memmove(payload + 2, payload, payload_size);
}
@@ -1328,15 +1397,18 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &
auto push_failed = (bool)src;
while (src) {
// 尝试断连后继续推流
// 尝试断连后继续推流 [AUTO-TRANSLATED:9eaaa6dd]
// Try to continue streaming after disconnecting
auto rtsp_src = dynamic_pointer_cast<RtspMediaSourceImp>(src);
if (!rtsp_src) {
// 源不是rtsp推流产生的
// 源不是rtsp推流产生的 [AUTO-TRANSLATED:47b87993]
// The source is not generated by RTSP streaming
break;
}
auto ownership = rtsp_src->getOwnership();
if (!ownership) {
// 获取推流源所有权失败
// 获取推流源所有权失败 [AUTO-TRANSLATED:256190b2]
// Failed to get the ownership of the streaming source
break;
}
push_src = std::move(rtsp_src);
@@ -1360,10 +1432,12 @@ void push_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &
cb(*rtc);
};
// rtsp推流需要鉴权
// rtsp推流需要鉴权 [AUTO-TRANSLATED:c2cbb7ed]
// RTSP streaming requires authentication
auto flag = NOTICE_EMIT(BroadcastMediaPublishArgs, Broadcast::kBroadcastMediaPublish, MediaOriginType::rtc_push, info, invoker, sender);
if (!flag) {
// 该事件无人监听,默认不鉴权
// 该事件无人监听,默认不鉴权 [AUTO-TRANSLATED:e1fbc6ae]
// No one is listening to this event, authentication is not enabled by default
invoker("", ProtocolOption());
}
}
@@ -1377,7 +1451,8 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &
return;
}
// webrtc播放的是rtsp的源
// webrtc播放的是rtsp的源 [AUTO-TRANSLATED:649ae489]
// WebRTC plays the RTSP source
info.schema = RTSP_SCHEMA;
MediaSource::findAsync(info, session_ptr, [=](const MediaSource::Ptr &src_in) mutable {
auto src = dynamic_pointer_cast<RtspMediaSource>(src_in);
@@ -1385,17 +1460,20 @@ void play_plugin(Session &sender, const WebRtcArgs &args, const onCreateWebRtc &
cb(WebRtcException(SockException(Err_other, "stream not found")));
return;
}
// 还原成rtc目的是为了hook时识别哪种播放协议
// 还原成rtc目的是为了hook时识别哪种播放协议 [AUTO-TRANSLATED:fe8dd2dc]
// Restore to RTC, the purpose is to identify which playback protocol during hooking
info.schema = "rtc";
auto rtc = WebRtcPlayer::create(EventPollerPool::Instance().getPoller(), src, info);
cb(*rtc);
});
};
// 广播通用播放url鉴权事件
// 广播通用播放url鉴权事件 [AUTO-TRANSLATED:81e24be4]
// Broadcast a universal playback URL authentication event
auto flag = NOTICE_EMIT(BroadcastMediaPlayedArgs, Broadcast::kBroadcastMediaPlayed, info, invoker, sender);
if (!flag) {
// 该事件无人监听,默认不鉴权
// 该事件无人监听,默认不鉴权 [AUTO-TRANSLATED:e1fbc6ae]
// No one is listening to this event, authentication is not enabled by default
invoker("");
}
}
@@ -1427,7 +1505,8 @@ static void setWebRtcArgs(const WebRtcArgs &args, WebRtcInterface &rtc) {
auto cand_str = trim(args["cand_udp"]);
auto ip_port = toolkit::split(cand_str, ":");
if (ip_port.size() == 2) {
// udp优先
// udp优先 [AUTO-TRANSLATED:b428f63d]
// UDP priority
auto ice_cand = makeIceCandidate(ip_port[0], atoi(ip_port[1].data()), preferred_tcp ? 100 : 120, "udp");
cands.emplace_back(std::move(*ice_cand));
}
@@ -1436,13 +1515,15 @@ static void setWebRtcArgs(const WebRtcArgs &args, WebRtcInterface &rtc) {
auto cand_str = trim(args["cand_tcp"]);
auto ip_port = toolkit::split(cand_str, ":");
if (ip_port.size() == 2) {
// tcp模式
// tcp模式 [AUTO-TRANSLATED:62fddf91]
// TCP mode
auto ice_cand = makeIceCandidate(ip_port[0], atoi(ip_port[1].data()), preferred_tcp ? 120 : 100, "tcp");
cands.emplace_back(std::move(*ice_cand));
}
}
if (!cands.empty()) {
// udp优先
// udp优先 [AUTO-TRANSLATED:b428f63d]
// UDP priority
rtc.setIceCandidate(std::move(cands));
}
}
@@ -1450,7 +1531,8 @@ static void setWebRtcArgs(const WebRtcArgs &args, WebRtcInterface &rtc) {
static onceToken s_rtc_auto_register([]() {
#if !defined (NDEBUG)
// debug模式才开启echo插件
// debug模式才开启echo插件 [AUTO-TRANSLATED:48fcb116]
// Enable echo plugin only in debug mode
WebRtcPluginManager::Instance().registerPlugin("echo", echo_plugin);
#endif
WebRtcPluginManager::Instance().registerPlugin("push", push_plugin);

View File

@@ -28,7 +28,8 @@
namespace mediakit {
//RTC配置项目
// RTC配置项目 [AUTO-TRANSLATED:65784416]
// RTC configuration project
namespace Rtc {
extern const std::string kPort;
extern const std::string kTcpPort;
@@ -72,11 +73,17 @@ public:
/**
* 创建对象
* Create object
* [AUTO-TRANSLATED:830344e4]
*/
virtual void onCreate();
/**
* 销毁对象
* Destroy object
* [AUTO-TRANSLATED:1016b97b]
*/
virtual void onDestory();
@@ -84,11 +91,19 @@ public:
* 创建webrtc answer sdp
* @param offer offer sdp
* @return answer sdp
* Create webrtc answer sdp
* @param offer offer sdp
* @return answer sdp
* [AUTO-TRANSLATED:d9b027d7]
*/
std::string getAnswerSdp(const std::string &offer) override final;
/**
* 获取对象唯一id
* Get object unique id
* [AUTO-TRANSLATED:9ad519c6]
*/
const std::string& getIdentifier() const override;
const std::string& deleteRandStr() const override;
@@ -98,6 +113,12 @@ public:
* @param buf 数据指针
* @param len 数据长度
* @param tuple 数据来源
* Socket receives udp data
* @param buf data pointer
* @param len data length
* @param tuple data source
* [AUTO-TRANSLATED:1ee86069]
*/
void inputSockData(char *buf, int len, RTC::TransportTuple *tuple);
@@ -107,6 +128,13 @@ public:
* @param len rtcp长度
* @param flush 是否flush socket
* @param ctx 用户指针
* Send rtp
* @param buf rtcp content
* @param len rtcp length
* @param flush whether to flush socket
* @param ctx user pointer
* [AUTO-TRANSLATED:aa833695]
*/
void sendRtpPacket(const char *buf, int len, bool flush, void *ctx = nullptr);
void sendRtcpPacket(const char *buf, int len, bool flush, void *ctx = nullptr);
@@ -116,7 +144,8 @@ public:
Session::Ptr getSession() const;
protected:
//// dtls相关的回调 ////
// // dtls相关的回调 //// [AUTO-TRANSLATED:31a1f32c]
// // dtls related callbacks ////
void OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) override;
void OnDtlsTransportConnected(const RTC::DtlsTransport *dtlsTransport,
RTC::SrtpSession::CryptoSuite srtpCryptoSuite,
@@ -132,7 +161,8 @@ protected:
void OnDtlsTransportApplicationDataReceived(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) override;
protected:
//// ice相关的回调 ///
// // ice相关的回调 /// [AUTO-TRANSLATED:30abf693]
// // ice related callbacks ///
void OnIceServerSendStunPacket(const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) override;
void OnIceServerConnected(const RTC::IceServer *iceServer) override;
void OnIceServerCompleted(const RTC::IceServer *iceServer) override;
@@ -182,7 +212,8 @@ private:
std::shared_ptr<RTC::SrtpSession> _srtp_session_send;
std::shared_ptr<RTC::SrtpSession> _srtp_session_recv;
Ticker _ticker;
// 循环池
// 循环池 [AUTO-TRANSLATED:b7059f37]
// Cycle pool
ResourcePool<BufferRaw> _packet_pool;
#ifdef ENABLE_SCTP
@@ -290,26 +321,36 @@ private:
private:
bool _preferred_tcp = false;
uint16_t _rtx_seq[2] = {0, 0};
//用掉的总流量
// 用掉的总流量 [AUTO-TRANSLATED:713b61c9]
// Total traffic used
uint64_t _bytes_usage = 0;
//保持自我强引用
// 保持自我强引用 [AUTO-TRANSLATED:c2dc228f]
// Keep self strong reference
Ptr _self;
//检测超时的定时器
// 检测超时的定时器 [AUTO-TRANSLATED:a58e1388]
// Timeout detection timer
Timer::Ptr _timer;
//刷新计时器
// 刷新计时器 [AUTO-TRANSLATED:61eb11e5]
// Refresh timer
Ticker _alive_ticker;
//pli rtcp计时器
// pli rtcp计时器 [AUTO-TRANSLATED:a1a5fd18]
// pli rtcp timer
Ticker _pli_ticker;
//twcc rtcp发送上下文对象
// twcc rtcp发送上下文对象 [AUTO-TRANSLATED:aef6476a]
// twcc rtcp send context object
TwccContext _twcc_ctx;
//根据发送rtp的track类型获取相关信息
// 根据发送rtp的track类型获取相关信息 [AUTO-TRANSLATED:ff31c272]
// Get relevant information based on the track type of the sent rtp
MediaTrack::Ptr _type_to_track[2];
//根据rtcp的ssrc获取相关信息收发rtp和rtx的ssrc都会记录
// 根据rtcp的ssrc获取相关信息收发rtp和rtx的ssrc都会记录 [AUTO-TRANSLATED:6c57cd48]
// Get relevant information based on the ssrc of the rtcp, the ssrc of sending and receiving rtp and rtx will be recorded
std::unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track;
//根据接收rtp的pt获取相关信息
// 根据接收rtp的pt获取相关信息 [AUTO-TRANSLATED:39e56d7d]
// Get relevant information based on the pt of the received rtp
std::unordered_map<uint8_t/*pt*/, std::unique_ptr<WrappedMediaTrack>> _pt_to_track;
std::vector<SdpAttrCandidate> _cands;
//http访问时的host ip
// http访问时的host ip [AUTO-TRANSLATED:e8fe6957]
// Host ip for http access
std::string _local_ip;
};