diff --git a/webrtc/RtpExt.cpp b/webrtc/RtpExt.cpp index 62cdc830..a3e2ec3a 100644 --- a/webrtc/RtpExt.cpp +++ b/webrtc/RtpExt.cpp @@ -147,9 +147,28 @@ void appendExt(map &ret, uint8_t *ptr, const uint8_t *end) { } } -RtpExt::RtpExt(void *ptr, bool one_byte_ext, const char *str, size_t size) : std::string(str, size) { - _ptr = ptr; +RtpExt::RtpExt(void *ext, bool one_byte_ext, const char *str, size_t size) { + _ext = ext; _one_byte_ext = one_byte_ext; + _data = str; + _size = size; +} + +const char *RtpExt::data() const { + return _data; +} + +size_t RtpExt::size() const { + return _size; +} + +const char& RtpExt::operator[](size_t pos) const{ + CHECK(pos < _size); + return _data[pos]; +} + +RtpExt::operator std::string() const{ + return string(_data, _size); } map RtpExt::getExtValue(const RtpHeader *header) { @@ -364,7 +383,7 @@ uint16_t RtpExt::getTransportCCSeq() const { // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | ID | len | SDES Item text value ... | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -const string &RtpExt::getSdesMid() const { +string RtpExt::getSdesMid() const { CHECK(_type == RtpExtType::sdes_mid && size() >= 1); return *this; } @@ -515,23 +534,23 @@ uint8_t RtpExt::getFramemarkingTID() const { } void RtpExt::setExtId(uint8_t ext_id) { - assert(ext_id > (int) RtpExtType::padding && ext_id <= (int) RtpExtType::reserved && _ptr); + assert(ext_id > (int) RtpExtType::padding && ext_id <= (int) RtpExtType::reserved && _ext); if (_one_byte_ext) { - auto ptr = reinterpret_cast(_ptr); + auto ptr = reinterpret_cast(_ext); ptr->setId(ext_id); } else { - auto ptr = reinterpret_cast(_ptr); + auto ptr = reinterpret_cast(_ext); ptr->setId(ext_id); } } void RtpExt::clearExt(){ - assert(_ptr); + assert(_ext); if (_one_byte_ext) { - auto ptr = reinterpret_cast(_ptr); + auto ptr = reinterpret_cast(_ext); memset(ptr, (int) RtpExtType::padding, RtpExtOneByte::kMinSize + ptr->getSize()); } else { - auto ptr = reinterpret_cast(_ptr); + auto ptr = reinterpret_cast(_ext); memset(ptr, (int) RtpExtType::padding, RtpExtTwoByte::kMinSize + ptr->getSize()); } } diff --git a/webrtc/RtpExt.h b/webrtc/RtpExt.h index 3d916def..9c59129c 100644 --- a/webrtc/RtpExt.h +++ b/webrtc/RtpExt.h @@ -45,7 +45,8 @@ enum class RtpExtType : uint8_t { class RtcMedia; -class RtpExt : public std::string { +//使用次对象的方法前需保证RtpHeader内存未释放 +class RtpExt { public: template friend void appendExt(map &ret, uint8_t *ptr, const uint8_t *end); @@ -63,7 +64,7 @@ public: uint8_t getAudioLevel(bool *vad) const; uint32_t getAbsSendTime() const; uint16_t getTransportCCSeq() const; - const string& getSdesMid() const; + string getSdesMid() const; string getRtpStreamId() const; string getRepairedRtpStreamId() const; @@ -88,15 +89,20 @@ public: uint8_t getFramemarkingTID() const; - //危险函数,必须保证关联的RtpHeader对象有效 void setExtId(uint8_t ext_id); void clearExt(); private: RtpExt(void *ptr, bool one_byte_ext, const char *str, size_t size); + const char *data() const; + size_t size() const; + const char& operator[](size_t pos) const; + operator std::string() const; private: - void *_ptr = nullptr; + void *_ext = nullptr; + const char *_data; + size_t _size; bool _one_byte_ext = true; RtpExtType _type = RtpExtType::padding; };