支持修改ext id

This commit is contained in:
xia-chu 2021-05-07 18:33:45 +08:00
parent cdbd210a10
commit e9f11a89fd
2 changed files with 52 additions and 7 deletions

View File

@ -34,6 +34,7 @@ public:
static constexpr uint16_t kMinSize = 1;
size_t getSize() const;
uint8_t getId() const;
void setId(uint8_t id);
uint8_t* getData();
private:
@ -64,6 +65,7 @@ public:
size_t getSize() const;
uint8_t getId() const;
void setId(uint8_t id);
uint8_t* getData();
private:
@ -86,6 +88,10 @@ uint8_t RtpExtOneByte::getId() const {
return id;
}
void RtpExtOneByte::setId(uint8_t in) {
id = in & 0x0F;
}
uint8_t *RtpExtOneByte::getData() {
return data;
}
@ -100,6 +106,10 @@ uint8_t RtpExtTwoByte::getId() const {
return id;
}
void RtpExtTwoByte::setId(uint8_t in) {
id = in;
}
uint8_t *RtpExtTwoByte::getData() {
return data;
}
@ -118,7 +128,17 @@ static RtpExtType getExtTypeById(uint8_t id, const RtcMedia &media){
}
template<typename Type>
static void appendExt( map<RtpExtType, RtpExt> &ret,const RtcMedia &media, uint8_t *ptr, const uint8_t *end){
static bool isOneByteExt(){
return false;
}
template<>
static bool isOneByteExt<RtpExtOneByte>(){
return true;
}
template<typename Type>
static void appendExt(map<RtpExtType, RtpExt> &ret, const RtcMedia &media, uint8_t *ptr, const uint8_t *end) {
while (ptr < end) {
auto ext = reinterpret_cast<Type *>(ptr);
if (ext->getId() == (uint8_t) RtpExtType::padding) {
@ -131,11 +151,17 @@ static void appendExt( map<RtpExtType, RtpExt> &ret,const RtcMedia &media, uint8
CHECK(reinterpret_cast<uint8_t *>(ext) + Type::kMinSize <= end);
CHECK(ext->getData() + ext->getSize() <= end);
auto type = getExtTypeById(ext->getId(), media);
ret.emplace(type, RtpExt(type, ext->getId(), reinterpret_cast<char *>(ext->getData()), ext->getSize()));
ret.emplace(type, RtpExt(ext, isOneByteExt<Type>(), type, reinterpret_cast<char *>(ext->getData()), ext->getSize()));
ptr += Type::kMinSize + ext->getSize();
}
}
RtpExt::RtpExt(void *ptr, bool one_byte_ext, RtpExtType type, const char *str, size_t size) : std::string(str, size) {
_ptr = ptr;
_one_byte_ext = one_byte_ext;
_type = type;
}
map<RtpExtType/*type*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header, const RtcMedia &media) {
map<RtpExtType, RtpExt> ret;
assert(header);
@ -274,8 +300,7 @@ string RtpExt::dumpString() const {
break;
}
default: {
printer << getExtName(_type) << ", id:" << (int) _id << ", ";
printer << "hex:" << hexdump(data(), size());
printer << getExtName(_type) << ", hex:" << hexdump(data(), size());
break;
}
}
@ -498,3 +523,14 @@ uint8_t RtpExt::getFramemarkingTID() const {
CHECK(_type == RtpExtType::framemarking && size() >= 3);
return (*this)[0] & 0x07;
}
void RtpExt::setExtId(uint8_t ext_id) {
assert(ext_id > (int) RtpExtType::padding && ext_id <= (int) RtpExtType::reserved && _ptr);
if (_one_byte_ext) {
auto ptr = reinterpret_cast<RtpExtOneByte *>(_ptr);
ptr->setId(ext_id);
} else {
auto ptr = reinterpret_cast<RtpExtTwoByte *>(_ptr);
ptr->setId(ext_id);
}
}

View File

@ -40,14 +40,16 @@ enum class RtpExtType : uint8_t {
toffset = 14,
reserved = 15,
// e2e ?
encrypt
encrypt = reserved
};
class RtcMedia;
class RtpExt : public std::string {
public:
RtpExt(RtpExtType type, uint8_t id, const char *str, size_t size) : std::string(str, size), _type(type), _id(id) {}
template<typename Type>
friend void appendExt(map<RtpExtType, RtpExt> &ret, const RtcMedia &media, uint8_t *ptr, const uint8_t *end);
~RtpExt() = default;
static map<RtpExtType/*type*/, RtpExt/*data*/> getExtValue(const RtpHeader *header, const RtcMedia &media);
@ -85,9 +87,16 @@ public:
uint8_t getFramemarkingTID() const;
//危险函数必须保证关联的RtpHeader对象有效
void setExtId(uint8_t ext_id);
private:
RtpExt(void *ptr, bool one_byte_ext, RtpExtType type, const char *str, size_t size);
private:
void *_ptr = nullptr;
bool _one_byte_ext = true;
RtpExtType _type;
uint8_t _id;
};