完善FCI相关代码

This commit is contained in:
xia-chu
2021-04-23 15:06:55 +08:00
parent 743862d786
commit 48338af700
6 changed files with 255 additions and 202 deletions

View File

@@ -8,13 +8,16 @@
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "assert.h"
#include "RtcpFCI.h"
#define CHECK(exp) Assert_Throw(!(exp), #exp, __FUNCTION__, __FILE__, __LINE__);
#include "Util/logger.h"
using namespace toolkit;
namespace mediakit {
void FCI_SLI::check(size_t size){
CHECK(size == kSize);
}
FCI_SLI::FCI_SLI(uint16_t first, uint16_t number, uint8_t pic_id) {
//13 bits
first &= 0x1FFF;
@@ -27,19 +30,15 @@ FCI_SLI::FCI_SLI(uint16_t first, uint16_t number, uint8_t pic_id) {
}
uint16_t FCI_SLI::getFirst() const {
return data >> 19;
return ntohl(data) >> 19;
}
uint16_t FCI_SLI::getNumber() const {
return (data >> 6) & 0x1FFF;
return (ntohl(data) >> 6) & 0x1FFF;
}
uint8_t FCI_SLI::getPicID() const {
return data & 0x3F;
}
void FCI_SLI::net2Host() {
data = ntohl(data);
return ntohl(data) & 0x3F;
}
string FCI_SLI::dumpString() const {
@@ -48,29 +47,52 @@ string FCI_SLI::dumpString() const {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FCI_FIR::net2Host() {
ssrc = ntohl(ssrc);
reserved = ntohl(reserved) >> 8;
void FCI_FIR::check(size_t size){
CHECK(size == kSize);
}
uint32_t FCI_FIR::getSSRC() const{
return ntohl(ssrc);
}
uint8_t FCI_FIR::getSeq() const{
return seq_number;
}
uint32_t FCI_FIR::getReserved() const{
return (reserved[0] << 16) | (reserved[1] << 8) | reserved[2];
}
string FCI_FIR::dumpString() const {
return StrPrinter << "ssrc:" << ssrc << ", seq_number:" << seq_number << ", reserved:" << reserved;
return StrPrinter << "ssrc:" << getSSRC() << ", seq_number:" << (int)getSeq() << ", reserved:" << getReserved();
}
FCI_FIR::FCI_FIR(uint32_t ssrc, uint8_t seq_number, uint32_t reserved) {
this->ssrc = htonl(ssrc);
this->seq_number = seq_number;
this->reserved = htonl(reserved) >> 8;
this->reserved[0] = (reserved >> 16) & 0xFF;
this->reserved[1] = (reserved >> 8) & 0xFF;
this->reserved[2] = reserved & 0xFF;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string FCI_REMB::create(const initializer_list<uint32_t> &ssrcs, uint32_t bitrate) {
static const char kRembMagic[] = "REMB";
void FCI_REMB::check(size_t size){
CHECK(size >= kSize);
CHECK(memcmp(magic, kRembMagic, sizeof(magic)) == 0);
auto num_ssrc = bitrate[0];
auto expect_size = kSize + 4 * num_ssrc;
CHECK(size == expect_size);
}
string FCI_REMB::create(const vector<uint32_t> &ssrcs, uint32_t bitrate) {
CHECK(ssrcs.size() > 0 && ssrcs.size() <= 0xFF);
string ret;
ret.resize(kSize + ssrcs.size() * 4);
FCI_REMB *thiz = (FCI_REMB *) ret.data();
memcpy(thiz->magic, "REMB", 4);
memcpy(thiz->magic, kRembMagic, sizeof(magic));
/* bitrate --> BR Exp/BR Mantissa */
uint8_t b = 0;
@@ -97,24 +119,12 @@ string FCI_REMB::create(const initializer_list<uint32_t> &ssrcs, uint32_t bitrat
//设置ssrc列表
auto ptr = thiz->ssrc_feedback;
for (auto &ssrc : ssrcs) {
for (auto ssrc : ssrcs) {
*(ptr++) = htonl(ssrc);
}
return ret;
}
void FCI_REMB::net2Host(size_t total_size) {
CHECK(total_size >= kSize);
CHECK(memcmp(magic, "REMB", 4) == 0);
auto num_ssrc = bitrate[0];
auto expect_size = kSize + 4 * num_ssrc;
CHECK(total_size == expect_size);
auto ptr = ssrc_feedback;
while (num_ssrc--) {
*(ptr++) = ntohl(*ptr);
}
}
uint32_t FCI_REMB::getBitRate() const {
uint8_t exp = (bitrate[1] >> 2) & 0x3F;
uint32_t mantissa = (bitrate[1] & 0x03) << 16;
@@ -123,12 +133,12 @@ uint32_t FCI_REMB::getBitRate() const {
return mantissa << exp;
}
vector<uint32_t *> FCI_REMB::getSSRC() {
vector<uint32_t *> ret;
vector<uint32_t> FCI_REMB::getSSRC() {
vector<uint32_t> ret;
auto num_ssrc = bitrate[0];
auto ptr = ssrc_feedback;
while (num_ssrc--) {
ret.emplace_back(ptr++);
ret.emplace_back(ntohl(*ptr++));
}
return ret;
}
@@ -137,30 +147,51 @@ string FCI_REMB::dumpString() const {
_StrPrinter printer;
printer << "bitrate:" << getBitRate() << ", ssrc:";
for (auto &ssrc : ((FCI_REMB *) this)->getSSRC()) {
printer << *ssrc << " ";
printer << ssrc << " ";
}
return printer;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FCI_NACK::net2Host() {
pid = ntohs(pid);
blp = ntohs(blp);
FCI_NACK::FCI_NACK(uint16_t pid_h, const vector<bool> &type) {
uint16_t blp_h = 0;
int i = kBitSize;
for (auto item : type) {
--i;
if (item) {
blp_h |= (1 << i);
}
}
blp = htons(blp_h);
pid = htons(pid_h);
}
void FCI_NACK::check(size_t size){
CHECK(size == kSize);
}
uint16_t FCI_NACK::getPid() const {
return ntohs(pid);
}
uint16_t FCI_NACK::getBlp() const {
return ntohs(blp);
}
vector<bool> FCI_NACK::getBitArray() const {
vector<bool> ret;
ret.resize(kBitSize);
auto blp_h = getBlp();
for (size_t i = 0; i < kBitSize; ++i) {
ret[i] = blp & (1 << (kBitSize - i - 1));
ret[i] = blp_h & (1 << (kBitSize - i - 1));
}
return ret;
}
string FCI_NACK::dumpString() const {
_StrPrinter printer;
printer << "pid:" << pid << ",blp:";
printer << "pid:" << getPid() << ",blp:";
for (auto &flag : getBitArray()) {
printer << flag << " ";
}
@@ -169,6 +200,37 @@ string FCI_NACK::dumpString() const {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
uint16_t type: 1;
uint16_t symbol: 2;
uint16_t run_length_high: 5;
#else
// Run Length 高5位
uint16_t run_length_high: 5;
//参考SymbolStatus定义
uint16_t symbol: 2;
//固定为0
uint16_t type: 1;
#endif
// Run Length 低8位
uint16_t run_length_low: 8;
//获取Run Length
uint16_t getRunLength() const;
//构造函数
RunLengthChunk(SymbolStatus status, uint16_t run_length);
//打印本对象
string dumpString() const;
} PACKED;
RunLengthChunk::RunLengthChunk(SymbolStatus status, uint16_t run_length) {
type = 0;
symbol = (uint8_t)status & 0x03;
@@ -189,6 +251,37 @@ string RunLengthChunk::dumpString() const{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
uint16_t type: 1;
uint16_t symbol: 1;
uint16_t symbol_list_high: 6;
#else
// symbol_list 高6位
uint16_t symbol_list_high: 6;
//symbol_list中元素是1个还是2个bit
uint16_t symbol: 1;
//固定为1
uint16_t type: 1;
#endif
// symbol_list 低8位
uint16_t symbol_list_low: 8;
//获取symbollist
vector<SymbolStatus> getSymbolList() const;
//构造函数
StatusVecChunk(const vector<SymbolStatus> &status);
//打印本对象
string dumpString() const;
} PACKED;
StatusVecChunk::StatusVecChunk(const vector<SymbolStatus> &status) {
uint16_t value = 0;
type = 1;
@@ -248,10 +341,16 @@ string StatusVecChunk::dumpString() const {
///////////////////////////////////////////////////////
void FCI_TWCC::net2Host(size_t total_size) {
CHECK(total_size >= kSize);
base_seq = ntohs(base_seq);
pkt_status_count = ntohs(pkt_status_count);
void FCI_TWCC::check(size_t size){
CHECK(size >= kSize);
}
uint16_t FCI_TWCC::getBaseSeq() const {
return ntohs(base_seq);
}
uint16_t FCI_TWCC::getPacketCount() const {
return ntohs(pkt_status_count);
}
uint32_t FCI_TWCC::getReferenceTime() const {
@@ -323,10 +422,10 @@ map<uint16_t, std::pair<SymbolStatus, uint32_t/*stamp*/> > FCI_TWCC::getPacketCh
auto ptr = (uint8_t *) this + kSize;
auto end = (uint8_t *) this + total_size;
CHECK(ptr < end);
auto seq = base_seq;
auto seq = getBaseSeq();
for (uint8_t i = 0; i < pkt_status_count;) {
CHECK(ptr + RunLengthChunk::kSize <= end)
for (uint8_t i = 0; i < getPacketCount();) {
CHECK(ptr + RunLengthChunk::kSize <= end);
RunLengthChunk *chunk = (RunLengthChunk *) ptr;
if (!chunk->type) {
//RunLengthChunk
@@ -345,7 +444,7 @@ map<uint16_t, std::pair<SymbolStatus, uint32_t/*stamp*/> > FCI_TWCC::getPacketCh
ptr += 2;
}
for (auto &pr : ret) {
CHECK(ptr <= end)
CHECK(ptr <= end);
pr.second.second = 250 * getRecvDelta(pr.second.first, ptr, end);
}
return ret;
@@ -354,11 +453,55 @@ map<uint16_t, std::pair<SymbolStatus, uint32_t/*stamp*/> > FCI_TWCC::getPacketCh
string FCI_TWCC::dumpString(size_t total_size) const {
_StrPrinter printer;
auto map = getPacketChunkList(total_size);
printer << "twcc fci, base_seq:" << base_seq << ",pkt_status_count:" << pkt_status_count << ", ref time:" << getReferenceTime() << ", fb count:" << (int)fb_pkt_count << "\n";
printer << "twcc fci, base_seq:" << getBaseSeq() << ",pkt_status_count:" << getPacketCount() << ", ref time:" << getReferenceTime() << ", fb count:" << (int)fb_pkt_count << "\n";
for (auto &pr : map) {
printer << "rtp seq:" << pr.first <<", packet status:" << (int)(pr.second.first) << ", delta:" << pr.second.second << "\n";
}
return std::move(printer);
}
}//namespace mediakit
}//namespace mediakit
#if 1
using namespace mediakit;
void testFCI() {
{
FCI_SLI fci(8191, 0, 63);
InfoL << hexdump(&fci, FCI_SLI::kSize) << fci.dumpString();
}
{
FCI_FIR fci(123456, 139, 456789);
InfoL << hexdump(&fci, FCI_FIR::kSize) << fci.dumpString();
}
{
auto str = FCI_REMB::create({1234, 2345, 5678}, 4 * 1024 * 1024);
FCI_REMB *ptr = (FCI_REMB *) str.data();
InfoL << hexdump(str.data(), str.size()) << ptr->dumpString();
}
{
FCI_NACK nack(1234, vector<bool>({1, 0, 0, 0, 1, 0, 1, 0, 1, 0}));
InfoL << hexdump(&nack, FCI_NACK::kSize) << nack.dumpString();
}
{
RunLengthChunk chunk(SymbolStatus::large_delta, 8024);
InfoL << hexdump(&chunk, RunLengthChunk::kSize) << chunk.dumpString();
}
auto lam = [](const initializer_list<int> &lst) {
vector<SymbolStatus> ret;
for (auto &num : lst) {
ret.emplace_back((SymbolStatus) num);
}
return ret;
};
{
StatusVecChunk chunk(lam({0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1}));
InfoL << hexdump(&chunk, StatusVecChunk::kSize) << chunk.dumpString();
}
{
StatusVecChunk chunk(lam({0, 1, 2, 2, 0, 1, 2}));
InfoL << hexdump(&chunk, StatusVecChunk::kSize) << chunk.dumpString();
}
}
#endif