rtp ext不做内存拷贝

This commit is contained in:
ziyue 2021-05-13 01:29:55 +08:00
parent 0e968a2d5e
commit a7cd4d0719
2 changed files with 38 additions and 13 deletions

View File

@ -147,9 +147,28 @@ void appendExt(map<uint8_t, RtpExt> &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) { RtpExt::RtpExt(void *ext, bool one_byte_ext, const char *str, size_t size) {
_ptr = ptr; _ext = ext;
_one_byte_ext = one_byte_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<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header) { map<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header) {
@ -364,7 +383,7 @@ uint16_t RtpExt::getTransportCCSeq() const {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | ID | len | SDES Item text value ... | // | ID | len | SDES Item text value ... |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
const string &RtpExt::getSdesMid() const { string RtpExt::getSdesMid() const {
CHECK(_type == RtpExtType::sdes_mid && size() >= 1); CHECK(_type == RtpExtType::sdes_mid && size() >= 1);
return *this; return *this;
} }
@ -515,23 +534,23 @@ uint8_t RtpExt::getFramemarkingTID() const {
} }
void RtpExt::setExtId(uint8_t ext_id) { 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) { if (_one_byte_ext) {
auto ptr = reinterpret_cast<RtpExtOneByte *>(_ptr); auto ptr = reinterpret_cast<RtpExtOneByte *>(_ext);
ptr->setId(ext_id); ptr->setId(ext_id);
} else { } else {
auto ptr = reinterpret_cast<RtpExtTwoByte *>(_ptr); auto ptr = reinterpret_cast<RtpExtTwoByte *>(_ext);
ptr->setId(ext_id); ptr->setId(ext_id);
} }
} }
void RtpExt::clearExt(){ void RtpExt::clearExt(){
assert(_ptr); assert(_ext);
if (_one_byte_ext) { if (_one_byte_ext) {
auto ptr = reinterpret_cast<RtpExtOneByte *>(_ptr); auto ptr = reinterpret_cast<RtpExtOneByte *>(_ext);
memset(ptr, (int) RtpExtType::padding, RtpExtOneByte::kMinSize + ptr->getSize()); memset(ptr, (int) RtpExtType::padding, RtpExtOneByte::kMinSize + ptr->getSize());
} else { } else {
auto ptr = reinterpret_cast<RtpExtTwoByte *>(_ptr); auto ptr = reinterpret_cast<RtpExtTwoByte *>(_ext);
memset(ptr, (int) RtpExtType::padding, RtpExtTwoByte::kMinSize + ptr->getSize()); memset(ptr, (int) RtpExtType::padding, RtpExtTwoByte::kMinSize + ptr->getSize());
} }
} }

View File

@ -45,7 +45,8 @@ enum class RtpExtType : uint8_t {
class RtcMedia; class RtcMedia;
class RtpExt : public std::string { //使用次对象的方法前需保证RtpHeader内存未释放
class RtpExt {
public: public:
template<typename Type> template<typename Type>
friend void appendExt(map<uint8_t, RtpExt> &ret, uint8_t *ptr, const uint8_t *end); friend void appendExt(map<uint8_t, RtpExt> &ret, uint8_t *ptr, const uint8_t *end);
@ -63,7 +64,7 @@ public:
uint8_t getAudioLevel(bool *vad) const; uint8_t getAudioLevel(bool *vad) const;
uint32_t getAbsSendTime() const; uint32_t getAbsSendTime() const;
uint16_t getTransportCCSeq() const; uint16_t getTransportCCSeq() const;
const string& getSdesMid() const; string getSdesMid() const;
string getRtpStreamId() const; string getRtpStreamId() const;
string getRepairedRtpStreamId() const; string getRepairedRtpStreamId() const;
@ -88,15 +89,20 @@ public:
uint8_t getFramemarkingTID() const; uint8_t getFramemarkingTID() const;
//危险函数必须保证关联的RtpHeader对象有效
void setExtId(uint8_t ext_id); void setExtId(uint8_t ext_id);
void clearExt(); void clearExt();
private: private:
RtpExt(void *ptr, bool one_byte_ext, const char *str, size_t size); 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: private:
void *_ptr = nullptr; void *_ext = nullptr;
const char *_data;
size_t _size;
bool _one_byte_ext = true; bool _one_byte_ext = true;
RtpExtType _type = RtpExtType::padding; RtpExtType _type = RtpExtType::padding;
}; };