From e9f11a89fd2aacc40f3f05893389fde223208506 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Fri, 7 May 2021 18:33:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BF=AE=E6=94=B9ext=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/RtpExt.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- webrtc/RtpExt.h | 15 ++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/webrtc/RtpExt.cpp b/webrtc/RtpExt.cpp index e1e181a6..10542843 100644 --- a/webrtc/RtpExt.cpp +++ b/webrtc/RtpExt.cpp @@ -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 -static void appendExt( map &ret,const RtcMedia &media, uint8_t *ptr, const uint8_t *end){ +static bool isOneByteExt(){ + return false; +} + +template<> +static bool isOneByteExt(){ + return true; +} + +template +static void appendExt(map &ret, const RtcMedia &media, uint8_t *ptr, const uint8_t *end) { while (ptr < end) { auto ext = reinterpret_cast(ptr); if (ext->getId() == (uint8_t) RtpExtType::padding) { @@ -131,11 +151,17 @@ static void appendExt( map &ret,const RtcMedia &media, uint8 CHECK(reinterpret_cast(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(ext->getData()), ext->getSize())); + ret.emplace(type, RtpExt(ext, isOneByteExt(), type, reinterpret_cast(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 RtpExt::getExtValue(const RtpHeader *header, const RtcMedia &media) { map 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(_ptr); + ptr->setId(ext_id); + } else { + auto ptr = reinterpret_cast(_ptr); + ptr->setId(ext_id); + } +} diff --git a/webrtc/RtpExt.h b/webrtc/RtpExt.h index 0decaeac..2557dc6d 100644 --- a/webrtc/RtpExt.h +++ b/webrtc/RtpExt.h @@ -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 + friend void appendExt(map &ret, const RtcMedia &media, uint8_t *ptr, const uint8_t *end); + ~RtpExt() = default; static map 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; };