ZLMediaKit/src/Rtcp/RtcpFCI.cpp

563 lines
17 KiB
C++
Raw Normal View History

2021-04-13 22:27:57 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2021-04-13 22:27:57 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2021-04-13 22:27:57 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2021-04-13 22:27:57 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
2021-04-13 18:50:20 +08:00
#include "RtcpFCI.h"
2021-04-23 15:06:55 +08:00
#include "Util/logger.h"
using namespace std;
2021-04-23 15:06:55 +08:00
using namespace toolkit;
2021-04-13 22:27:57 +08:00
namespace mediakit {
2022-07-14 22:49:31 +08:00
void FCI_SLI::check(size_t size) {
2021-05-10 10:14:00 +08:00
CHECK(size >= kSize);
2021-04-23 15:06:55 +08:00
}
2021-04-14 16:19:46 +08:00
FCI_SLI::FCI_SLI(uint16_t first, uint16_t number, uint8_t pic_id) {
2022-07-14 22:49:31 +08:00
// 13 bits
2021-04-14 16:19:46 +08:00
first &= 0x1FFF;
2022-07-14 22:49:31 +08:00
// 13 bits
2021-04-14 16:19:46 +08:00
number &= 0x1FFF;
2022-07-14 22:49:31 +08:00
// 6 bits
2021-04-14 16:19:46 +08:00
pic_id &= 0x3F;
data = (first << 19) | (number << 6) | pic_id;
data = htonl(data);
}
uint16_t FCI_SLI::getFirst() const {
2021-04-23 15:06:55 +08:00
return ntohl(data) >> 19;
2021-04-14 16:19:46 +08:00
}
uint16_t FCI_SLI::getNumber() const {
2021-04-23 15:06:55 +08:00
return (ntohl(data) >> 6) & 0x1FFF;
2021-04-14 16:19:46 +08:00
}
uint8_t FCI_SLI::getPicID() const {
2021-04-23 15:06:55 +08:00
return ntohl(data) & 0x3F;
2021-04-14 16:19:46 +08:00
}
string FCI_SLI::dumpString() const {
return StrPrinter << "First:" << getFirst() << ", Number:" << getNumber() << ", PictureID:" << (int)getPicID();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2022-07-14 22:49:31 +08:00
void FCI_FIR::check(size_t size) {
2021-05-10 10:14:00 +08:00
CHECK(size >= kSize);
2021-04-23 15:06:55 +08:00
}
2022-07-14 22:49:31 +08:00
uint32_t FCI_FIR::getSSRC() const {
2021-04-23 15:06:55 +08:00
return ntohl(ssrc);
}
2022-07-14 22:49:31 +08:00
uint8_t FCI_FIR::getSeq() const {
2021-04-23 15:06:55 +08:00
return seq_number;
}
2022-07-14 22:49:31 +08:00
uint32_t FCI_FIR::getReserved() const {
2021-04-23 15:06:55 +08:00
return (reserved[0] << 16) | (reserved[1] << 8) | reserved[2];
2021-04-14 16:19:46 +08:00
}
string FCI_FIR::dumpString() const {
2021-04-23 15:06:55 +08:00
return StrPrinter << "ssrc:" << getSSRC() << ", seq_number:" << (int)getSeq() << ", reserved:" << getReserved();
2021-04-14 16:19:46 +08:00
}
FCI_FIR::FCI_FIR(uint32_t ssrc, uint8_t seq_number, uint32_t reserved) {
this->ssrc = htonl(ssrc);
this->seq_number = seq_number;
2021-04-23 15:06:55 +08:00
this->reserved[0] = (reserved >> 16) & 0xFF;
this->reserved[1] = (reserved >> 8) & 0xFF;
this->reserved[2] = reserved & 0xFF;
2021-04-14 16:19:46 +08:00
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2021-04-23 15:06:55 +08:00
static const char kRembMagic[] = "REMB";
2022-07-14 22:49:31 +08:00
void FCI_REMB::check(size_t size) {
2021-04-23 15:06:55 +08:00
CHECK(size >= kSize);
CHECK(memcmp(magic, kRembMagic, sizeof(magic)) == 0);
auto num_ssrc = bitrate[0];
auto expect_size = kSize + 4 * num_ssrc;
2021-05-10 10:14:00 +08:00
CHECK(size >= expect_size);
2021-04-23 15:06:55 +08:00
}
string FCI_REMB::create(const vector<uint32_t> &ssrcs, uint32_t bitrate) {
2021-04-14 16:19:46 +08:00
CHECK(ssrcs.size() > 0 && ssrcs.size() <= 0xFF);
string ret;
ret.resize(kSize + ssrcs.size() * 4);
2022-07-14 22:49:31 +08:00
FCI_REMB *thiz = (FCI_REMB *)ret.data();
2021-04-23 15:06:55 +08:00
memcpy(thiz->magic, kRembMagic, sizeof(magic));
2021-04-14 16:19:46 +08:00
/* bitrate --> BR Exp/BR Mantissa */
uint8_t b = 0;
uint8_t exp = 0;
uint32_t mantissa = 0;
for (b = 0; b < 32; b++) {
2022-07-14 22:49:31 +08:00
if (bitrate <= ((uint32_t)0x3FFFF << b)) {
2021-04-14 16:19:46 +08:00
exp = b;
break;
}
}
if (b > 31) {
b = 31;
}
mantissa = bitrate >> b;
2022-07-14 22:49:31 +08:00
// Num SSRC (8 bits)
2021-04-14 16:19:46 +08:00
thiz->bitrate[0] = ssrcs.size() & 0xFF;
2022-07-14 22:49:31 +08:00
// BR Exp (6 bits)/BR Mantissa (18 bits)
thiz->bitrate[1] = (uint8_t)((exp << 2) + ((mantissa >> 16) & 0x03));
// BR Mantissa (18 bits)
thiz->bitrate[2] = (uint8_t)(mantissa >> 8);
// BR Mantissa (18 bits)
thiz->bitrate[3] = (uint8_t)(mantissa);
// 设置ssrc列表
2021-12-30 15:28:02 +08:00
int i = 0;
2021-04-23 15:06:55 +08:00
for (auto ssrc : ssrcs) {
2021-12-30 15:28:02 +08:00
thiz->ssrc_feedback[i++] = htonl(ssrc);
2021-04-14 16:19:46 +08:00
}
return ret;
}
uint32_t FCI_REMB::getBitRate() const {
uint8_t exp = (bitrate[1] >> 2) & 0x3F;
uint32_t mantissa = (bitrate[1] & 0x03) << 16;
mantissa += (bitrate[2] << 8);
mantissa += (bitrate[3]);
return mantissa << exp;
}
2021-04-23 15:06:55 +08:00
vector<uint32_t> FCI_REMB::getSSRC() {
vector<uint32_t> ret;
2021-04-14 16:19:46 +08:00
auto num_ssrc = bitrate[0];
2021-12-30 15:28:02 +08:00
int i = 0;
2021-04-14 16:19:46 +08:00
while (num_ssrc--) {
2021-12-30 15:28:02 +08:00
ret.emplace_back(ntohl(ssrc_feedback[i]));
++i;
2021-04-14 16:19:46 +08:00
}
return ret;
}
string FCI_REMB::dumpString() const {
_StrPrinter printer;
printer << "bitrate:" << getBitRate() << ", ssrc:";
2022-07-14 22:49:31 +08:00
for (auto &ssrc : ((FCI_REMB *)this)->getSSRC()) {
2021-04-23 15:06:55 +08:00
printer << ssrc << " ";
2021-04-14 16:19:46 +08:00
}
2021-05-08 19:33:51 +08:00
return std::move(printer);
2021-04-14 16:19:46 +08:00
}
2021-04-13 22:27:57 +08:00
2021-04-20 17:15:01 +08:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2021-04-23 15:06:55 +08:00
FCI_NACK::FCI_NACK(uint16_t pid_h, const vector<bool> &type) {
2021-07-28 12:20:16 +08:00
assert(type.size() <= kBitSize);
2021-04-23 15:06:55 +08:00
uint16_t blp_h = 0;
2021-07-28 12:20:16 +08:00
int i = 0;
2021-04-23 15:06:55 +08:00
for (auto item : type) {
if (item) {
blp_h |= (1 << i);
}
2021-07-28 12:20:16 +08:00
++i;
2021-04-23 15:06:55 +08:00
}
blp = htons(blp_h);
pid = htons(pid_h);
}
2022-07-14 22:49:31 +08:00
void FCI_NACK::check(size_t size) {
2021-05-10 10:14:00 +08:00
CHECK(size >= kSize);
2021-04-23 15:06:55 +08:00
}
uint16_t FCI_NACK::getPid() const {
return ntohs(pid);
}
uint16_t FCI_NACK::getBlp() const {
return ntohs(blp);
2021-04-20 17:15:01 +08:00
}
vector<bool> FCI_NACK::getBitArray() const {
vector<bool> ret;
ret.resize(kBitSize + 1);
2022-07-14 22:49:31 +08:00
// nack第一个包丢包
2021-05-11 11:18:55 +08:00
ret[0] = true;
2021-04-23 15:06:55 +08:00
auto blp_h = getBlp();
2021-04-20 17:15:01 +08:00
for (size_t i = 0; i < kBitSize; ++i) {
2021-07-28 12:20:16 +08:00
ret[i + 1] = blp_h & (1 << i);
2021-04-20 17:15:01 +08:00
}
return ret;
}
string FCI_NACK::dumpString() const {
_StrPrinter printer;
auto pid = getPid();
printer << "pid:" << pid << ",blp:" << getBlp() << ",dropped rtp seq:";
2021-04-26 20:29:06 +08:00
for (auto flag : getBitArray()) {
if (flag) {
printer << pid << " ";
}
++pid;
2021-04-20 17:15:01 +08:00
}
return std::move(printer);
}
2021-04-22 11:43:33 +08:00
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2023-12-31 22:15:51 +08:00
#pragma pack(push, 1)
2021-04-23 15:06:55 +08:00
class RunLengthChunk {
public:
static size_t constexpr kSize = 2;
// 0 1
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |T| S | Run Length |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#if __BYTE_ORDER == __BIG_ENDIAN
2022-07-14 22:49:31 +08:00
uint16_t type : 1;
uint16_t symbol : 2;
uint16_t run_length_high : 5;
2021-04-23 15:06:55 +08:00
#else
// Run Length 高5位
2022-07-14 22:49:31 +08:00
uint16_t run_length_high : 5;
// 参考SymbolStatus定义
uint16_t symbol : 2;
// 固定为0
uint16_t type : 1;
2021-04-23 15:06:55 +08:00
#endif
// Run Length 低8位
2022-07-14 22:49:31 +08:00
uint16_t run_length_low : 8;
2021-04-23 15:06:55 +08:00
2022-07-14 22:49:31 +08:00
// 获取Run Length
2021-04-23 15:06:55 +08:00
uint16_t getRunLength() const;
2022-07-14 22:49:31 +08:00
// 构造函数
2021-04-23 15:06:55 +08:00
RunLengthChunk(SymbolStatus status, uint16_t run_length);
2022-07-14 22:49:31 +08:00
// 打印本对象
2021-04-23 15:06:55 +08:00
string dumpString() const;
};
2023-12-31 22:15:51 +08:00
#pragma pack(pop)
2021-04-23 15:06:55 +08:00
2021-04-22 11:43:33 +08:00
RunLengthChunk::RunLengthChunk(SymbolStatus status, uint16_t run_length) {
type = 0;
symbol = (uint8_t)status & 0x03;
run_length_high = (run_length >> 8) & 0x1F;
run_length_low = run_length & 0xFF;
}
uint16_t RunLengthChunk::getRunLength() const {
CHECK(type == 0);
return run_length_high << 8 | run_length_low;
}
2022-07-14 22:49:31 +08:00
string RunLengthChunk::dumpString() const {
2021-04-22 11:43:33 +08:00
_StrPrinter printer;
printer << "run length chunk, symbol:" << (int)symbol << ", run length:" << getRunLength();
return std::move(printer);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2023-12-31 22:15:51 +08:00
#pragma pack(push, 1)
2021-04-23 15:06:55 +08:00
class StatusVecChunk {
public:
static size_t constexpr kSize = 2;
// 0 1
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |T|S| symbol list |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#if __BYTE_ORDER == __BIG_ENDIAN
2022-07-14 22:49:31 +08:00
uint16_t type : 1;
uint16_t symbol : 1;
uint16_t symbol_list_high : 6;
2021-04-23 15:06:55 +08:00
#else
// symbol_list 高6位
2022-07-14 22:49:31 +08:00
uint16_t symbol_list_high : 6;
// symbol_list中元素是1个还是2个bit
uint16_t symbol : 1;
// 固定为1
uint16_t type : 1;
2021-04-23 15:06:55 +08:00
#endif
// symbol_list 低8位
2022-07-14 22:49:31 +08:00
uint16_t symbol_list_low : 8;
2021-04-23 15:06:55 +08:00
2022-07-14 22:49:31 +08:00
// 获取symbollist
2021-04-23 15:06:55 +08:00
vector<SymbolStatus> getSymbolList() const;
2022-07-14 22:49:31 +08:00
// 构造函数
2021-10-07 12:28:59 +08:00
StatusVecChunk(bool symbol_bit, const vector<SymbolStatus> &status);
2022-07-14 22:49:31 +08:00
// 打印本对象
2021-04-23 15:06:55 +08:00
string dumpString() const;
};
2023-12-31 22:15:51 +08:00
#pragma pack(pop)
2021-04-23 15:06:55 +08:00
2021-10-07 12:28:59 +08:00
StatusVecChunk::StatusVecChunk(bool symbol_bit, const vector<SymbolStatus> &status) {
2022-07-14 22:49:31 +08:00
CHECK(status.size() << symbol_bit <= 14);
2021-04-22 11:43:33 +08:00
uint16_t value = 0;
type = 1;
2021-10-07 12:28:59 +08:00
symbol = symbol_bit;
2021-04-22 11:43:33 +08:00
int i = 13;
for (auto &item : status) {
CHECK(item <= SymbolStatus::reserved);
if (!symbol) {
CHECK(item <= SymbolStatus::small_delta);
2022-07-14 22:49:31 +08:00
value |= (int)item << i;
2021-04-22 11:43:33 +08:00
--i;
} else {
2022-07-14 22:49:31 +08:00
value |= (int)item << (i - 1);
2021-04-22 11:43:33 +08:00
i -= 2;
}
}
symbol_list_low = value & 0xFF;
2022-07-14 22:49:31 +08:00
symbol_list_high = (value >> 8) & 0x3F;
2021-04-22 11:43:33 +08:00
}
vector<SymbolStatus> StatusVecChunk::getSymbolList() const {
CHECK(type == 1);
vector<SymbolStatus> ret;
2022-07-14 22:49:31 +08:00
auto thiz = ntohs(*((uint16_t *)this));
2021-04-22 11:43:33 +08:00
if (symbol == 0) {
2022-07-14 22:49:31 +08:00
// s = 0 时表示symbollist的每一个bit能表示一个数据包的到达状态
2021-04-22 11:43:33 +08:00
for (int i = 13; i >= 0; --i) {
2022-07-14 22:49:31 +08:00
SymbolStatus status = (SymbolStatus)((bool)(thiz & (1 << i)));
2021-04-22 11:43:33 +08:00
ret.emplace_back(status);
}
} else {
2022-07-14 22:49:31 +08:00
// s = 1 时表示symbollist每两个bit表示一个数据包的状态
2021-04-22 11:43:33 +08:00
for (int i = 12; i >= 0; i -= 2) {
2022-07-14 22:49:31 +08:00
SymbolStatus status = (SymbolStatus)((thiz & (3 << i)) >> i);
2021-04-22 11:43:33 +08:00
ret.emplace_back(status);
}
}
return ret;
}
string StatusVecChunk::dumpString() const {
_StrPrinter printer;
2022-07-14 22:49:31 +08:00
printer << "status vector chunk, symbol:" << (int)symbol << ", symbol list:";
2021-04-22 11:43:33 +08:00
auto vec = getSymbolList();
for (auto &item : vec) {
2022-07-14 22:49:31 +08:00
printer << (int)item << " ";
2021-04-22 11:43:33 +08:00
}
return std::move(printer);
}
2021-04-20 17:15:01 +08:00
2021-04-22 17:34:26 +08:00
///////////////////////////////////////////////////////
2022-07-14 22:49:31 +08:00
void FCI_TWCC::check(size_t size) {
2021-04-23 15:06:55 +08:00
CHECK(size >= kSize);
}
uint16_t FCI_TWCC::getBaseSeq() const {
return ntohs(base_seq);
}
uint16_t FCI_TWCC::getPacketCount() const {
return ntohs(pkt_status_count);
2021-04-22 17:34:26 +08:00
}
uint32_t FCI_TWCC::getReferenceTime() const {
uint32_t ret = 0;
ret |= ref_time[0] << 16;
ret |= ref_time[1] << 8;
ret |= ref_time[2];
return ret;
}
2022-07-14 22:49:31 +08:00
// 3.1.5. Receive Delta
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// Deltas are represented as multiples of 250us:
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// o If the "Packet received, small delta" symbol has been appended to
// the status list, an 8-bit unsigned receive delta will be appended
// to recv delta list, representing a delta in the range [0, 63.75]
// ms.
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// o If the "Packet received, large or negative delta" symbol has been
// appended to the status list, a 16-bit signed receive delta will be
// appended to recv delta list, representing a delta in the range
// [-8192.0, 8191.75] ms.
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// o If the delta exceeds even the larger limits, a new feedback
// message must be used, where the 24-bit base receive delta can
// cover very large gaps.
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// The smaller receive delta upper bound of 63.75 ms means that this is
// only viable at about 1000/25.5 ~= 16 packets per second and above.
// With a packet size of 1200 bytes/packet that amounts to a bitrate of
// about 150 kbit/s.
2021-04-22 18:56:12 +08:00
//
2022-07-14 22:49:31 +08:00
// The 0.25 ms resolution means that up to 4000 packets per second can
// be represented. With a 1200 bytes/packet payload, that amounts to
// 38.4 Mbit/s payload bandwidth.
2021-04-22 18:56:12 +08:00
2022-07-14 22:49:31 +08:00
static int16_t getRecvDelta(SymbolStatus status, uint8_t *&ptr, const uint8_t *end) {
2021-04-22 18:56:12 +08:00
int16_t delta = 0;
2021-04-22 17:34:26 +08:00
switch (status) {
2022-07-14 22:49:31 +08:00
case SymbolStatus::not_received: {
// 丢包, recv delta为0个字节
break;
}
case SymbolStatus::small_delta: {
CHECK(ptr + 1 <= end);
// 时间戳增量小于256 recv delta为1个字节
delta = *ptr;
ptr += 1;
break;
}
case SymbolStatus::large_delta: {
CHECK(ptr + 2 <= end);
// 时间戳增量256~65535间recv delta为2个字节
delta = *ptr << 8 | *(ptr + 1);
ptr += 2;
break;
}
case SymbolStatus::reserved: {
// 没有时间戳
break;
}
default:
// 这个逻辑分支不可达到
CHECK(0);
break;
2021-04-22 17:34:26 +08:00
}
return delta;
}
2021-10-07 12:28:59 +08:00
FCI_TWCC::TwccPacketStatus FCI_TWCC::getPacketChunkList(size_t total_size) const {
TwccPacketStatus ret;
2022-07-14 22:49:31 +08:00
auto ptr = (uint8_t *)this + kSize;
auto end = (uint8_t *)this + total_size;
2021-04-22 17:34:26 +08:00
CHECK(ptr < end);
2021-04-23 15:06:55 +08:00
auto seq = getBaseSeq();
2021-04-26 21:50:31 +08:00
auto rtp_count = getPacketCount();
for (uint16_t i = 0; i < rtp_count;) {
2021-04-23 15:06:55 +08:00
CHECK(ptr + RunLengthChunk::kSize <= end);
2022-07-14 22:49:31 +08:00
RunLengthChunk *chunk = (RunLengthChunk *)ptr;
2021-04-22 17:34:26 +08:00
if (!chunk->type) {
2022-07-14 22:49:31 +08:00
// RunLengthChunk
2021-04-22 17:34:26 +08:00
for (auto j = 0; j < chunk->getRunLength(); ++j) {
2022-07-14 22:49:31 +08:00
ret.emplace(seq++, std::make_pair((SymbolStatus)chunk->symbol, 0));
2021-04-26 21:50:31 +08:00
if (++i >= rtp_count) {
break;
}
2021-04-22 17:34:26 +08:00
}
} else {
2022-07-14 22:49:31 +08:00
// StatusVecChunk
StatusVecChunk *chunk = (StatusVecChunk *)ptr;
2021-04-22 17:34:26 +08:00
for (auto &symbol : chunk->getSymbolList()) {
2021-04-22 18:56:12 +08:00
ret.emplace(seq++, std::make_pair(symbol, 0));
2021-04-26 21:50:31 +08:00
if (++i >= rtp_count) {
break;
}
2021-04-22 17:34:26 +08:00
}
}
2021-04-22 18:56:12 +08:00
ptr += 2;
}
for (auto &pr : ret) {
2021-04-23 15:06:55 +08:00
CHECK(ptr <= end);
2021-09-25 17:34:12 +08:00
pr.second.second = getRecvDelta(pr.second.first, ptr, end);
2021-04-22 17:34:26 +08:00
}
return ret;
}
string FCI_TWCC::dumpString(size_t total_size) const {
_StrPrinter printer;
auto map = getPacketChunkList(total_size);
2022-07-14 22:49:31 +08:00
printer << "twcc fci, base_seq:" << getBaseSeq() << ", pkt_status_count:" << getPacketCount()
<< ", ref time:" << getReferenceTime() << ", fb count:" << (int)fb_pkt_count << "\n";
2021-04-22 17:34:26 +08:00
for (auto &pr : map) {
2022-07-14 22:49:31 +08:00
printer << "rtp seq:" << pr.first << ", packet status:" << (int)(pr.second.first)
<< ", delta:" << pr.second.second << "\n";
2021-04-22 17:34:26 +08:00
}
return std::move(printer);
}
2022-07-14 22:49:31 +08:00
static void appendDeltaString(string &delta_str, FCI_TWCC::TwccPacketStatus &status, int count) {
2021-10-07 12:28:59 +08:00
for (auto it = status.begin(); it != status.end() && count--;) {
switch (it->second.first) {
2022-07-14 22:49:31 +08:00
// large delta模式先写高字节再写低字节
case SymbolStatus::large_delta:
delta_str.push_back((it->second.second >> 8) & 0xFF);
// small delta模式只写低字节
case SymbolStatus::small_delta:
delta_str.push_back(it->second.second & 0xFF);
break;
default:
break;
2021-10-07 12:28:59 +08:00
}
2022-07-14 22:49:31 +08:00
// 移除已经处理过的数据
2021-10-07 12:28:59 +08:00
it = status.erase(it);
2021-04-23 15:06:55 +08:00
}
2021-10-07 12:28:59 +08:00
}
2021-04-23 15:06:55 +08:00
2021-10-07 12:28:59 +08:00
string FCI_TWCC::create(uint32_t ref_time, uint8_t fb_pkt_count, TwccPacketStatus &status) {
string fci;
fci.resize(FCI_TWCC::kSize);
2022-07-14 22:49:31 +08:00
FCI_TWCC *ptr = (FCI_TWCC *)(fci.data());
2021-10-07 12:28:59 +08:00
ptr->base_seq = htons(status.begin()->first);
ptr->pkt_status_count = htons(status.size());
ptr->fb_pkt_count = fb_pkt_count;
ptr->ref_time[0] = (ref_time >> 16) & 0xFF;
ptr->ref_time[1] = (ref_time >> 8) & 0xFF;
ptr->ref_time[2] = (ref_time >> 0) & 0xFF;
string delta_str;
while (!status.empty()) {
{
2022-07-14 22:49:31 +08:00
// 第一个rtp的状态
2021-10-07 12:28:59 +08:00
auto symbol = status.begin()->second.first;
int16_t count = 0;
for (auto &pr : status) {
if (pr.second.first != symbol) {
2022-07-14 22:49:31 +08:00
// 状态发送变更了本chunk结束
2021-10-07 12:28:59 +08:00
break;
}
2021-10-07 13:07:25 +08:00
if (++count >= (0xFFFF >> 3)) {
2022-07-14 22:49:31 +08:00
// RunLengthChunk 13个bit表明rtp个数最多可以表述0xFFFF >> 3个rtp状态
2021-10-07 13:07:25 +08:00
break;
}
2021-10-07 12:28:59 +08:00
}
if (count >= 7) {
2022-07-14 22:49:31 +08:00
// 连续状态相同个数大于6个时使用RunLengthChunk模式比较节省带宽
2021-10-07 12:28:59 +08:00
RunLengthChunk chunk(symbol, count);
fci.append((char *)&chunk, RunLengthChunk::kSize);
appendDeltaString(delta_str, status, count);
continue;
}
}
2021-04-23 15:06:55 +08:00
2021-10-07 12:28:59 +08:00
{
2022-07-14 22:49:31 +08:00
// StatusVecChunk模式
// symbol_list中元素是1个bit
2021-10-07 12:28:59 +08:00
auto symbol = 0;
vector<SymbolStatus> vec;
for (auto &pr : status) {
vec.push_back(pr.second.first);
if (pr.second.first >= SymbolStatus::large_delta) {
2022-07-14 22:49:31 +08:00
// symbol_list中元素是2个bit
2021-10-07 12:28:59 +08:00
symbol = 1;
}
2021-10-07 13:07:25 +08:00
if (vec.size() << symbol >= 14) {
2022-07-14 22:49:31 +08:00
// symbol为0时最多存放14个rtp的状态
// symbol为1时最多存放7个rtp的状态
2021-10-07 12:28:59 +08:00
break;
}
}
2021-12-30 15:28:02 +08:00
vec.resize(MIN(vec.size(), (size_t)14 >> symbol));
2021-10-07 12:28:59 +08:00
StatusVecChunk chunk(symbol, vec);
fci.append((char *)&chunk, StatusVecChunk::kSize);
2021-10-07 13:07:25 +08:00
appendDeltaString(delta_str, status, vec.size());
2021-04-23 15:06:55 +08:00
}
}
2021-10-07 12:28:59 +08:00
2022-07-14 22:49:31 +08:00
// recv delta部分
2021-10-07 12:28:59 +08:00
fci.append(delta_str);
return fci;
2021-04-23 15:06:55 +08:00
}
2021-10-07 12:28:59 +08:00
2022-07-14 22:49:31 +08:00
} // namespace mediakit