Commit f263dd5d by xia-chu

优化rtp ext相关代码

parent c5fff230
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
*/ */
#include "RtpExt.h" #include "RtpExt.h"
#include "Sdp.h"
#if defined(_WIN32) #if defined(_WIN32)
#pragma pack(push, 1) #pragma pack(push, 1)
...@@ -108,20 +109,18 @@ uint8_t *RtpExtTwoByte::getData() { ...@@ -108,20 +109,18 @@ uint8_t *RtpExtTwoByte::getData() {
static constexpr uint16_t kOneByteHeader = 0xBEDE; static constexpr uint16_t kOneByteHeader = 0xBEDE;
static constexpr uint16_t kTwoByteHeader = 0x1000; static constexpr uint16_t kTwoByteHeader = 0x1000;
map<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header, const RtcMedia &media) { static RtpExtType getExtTypeById(uint8_t id, const RtcMedia &media){
map<uint8_t, RtpExt> ret; auto it = media.extmap.find(id);
assert(header); if (it == media.extmap.end()) {
auto ext_size = header->getExtSize(); return RtpExtType::padding;
if (!ext_size) {
return ret;
} }
auto reserved = header->getExtReserved(); return RtpExt::getExtType(it->second.ext);
auto ptr = const_cast<RtpHeader *>(header)->getExtData(); }
auto end = ptr + ext_size;
RtpExtType type; template<typename Type>
if (reserved == kOneByteHeader) { static void appendExt( map<RtpExtType, RtpExt> &ret,const RtcMedia &media, uint8_t *ptr, const uint8_t *end){
while (ptr < end) { while (ptr < end) {
RtpExtOneByte *ext = reinterpret_cast<RtpExtOneByte *>(ptr); auto ext = reinterpret_cast<Type *>(ptr);
if (ext->getId() == (uint8_t) RtpExtType::padding) { if (ext->getId() == (uint8_t) RtpExtType::padding) {
//padding,忽略 //padding,忽略
++ptr; ++ptr;
...@@ -129,55 +128,55 @@ map<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header, ...@@ -129,55 +128,55 @@ map<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header,
} }
//15类型的rtp ext为保留 //15类型的rtp ext为保留
CHECK(ext->getId() < (uint8_t) RtpExtType::reserved); CHECK(ext->getId() < (uint8_t) RtpExtType::reserved);
CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtOneByte::kMinSize <= end); CHECK(reinterpret_cast<uint8_t *>(ext) + Type::kMinSize <= end);
CHECK(ext->getData() + ext->getSize() <= end); CHECK(ext->getData() + ext->getSize() <= end);
ret.emplace(ext->getId(), RtpExt(type, reinterpret_cast<char *>(ext->getData()), ext->getSize())); auto type = getExtTypeById(ext->getId(), media);
ptr += RtpExtOneByte::kMinSize + ext->getSize(); ret.emplace(type, RtpExt(type, ext->getId(), reinterpret_cast<char *>(ext->getData()), ext->getSize()));
} ptr += Type::kMinSize + ext->getSize();
return ret;
} }
}
if ((reserved & 0xFFF0) >> 4 == kTwoByteHeader) { map<RtpExtType/*type*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header, const RtcMedia &media) {
while (ptr < end) { map<RtpExtType, RtpExt> ret;
RtpExtTwoByte *ext = reinterpret_cast<RtpExtTwoByte *>(ptr); assert(header);
if (ext->getId() == (uint8_t) RtpExtType::padding) { auto ext_size = header->getExtSize();
//padding,忽略 if (!ext_size) {
++ptr; return ret;
continue;
} }
//15类型的rtp ext为保留 auto reserved = header->getExtReserved();
CHECK(ext->getId() < (uint8_t) RtpExtType::reserved); auto ptr = const_cast<RtpHeader *>(header)->getExtData();
CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtTwoByte::kMinSize <= end); auto end = ptr + ext_size;
CHECK(ext->getData() + ext->getSize() <= end); if (reserved == kOneByteHeader) {
ret.emplace(ext->getId(), RtpExt(type, reinterpret_cast<char *>(ext->getData()), ext->getSize())); appendExt<RtpExtOneByte>(ret, media, ptr, end);
ptr += RtpExtTwoByte::kMinSize + ext->getSize(); return ret;
} }
if ((reserved & 0xFFF0) >> 4 == kTwoByteHeader) {
appendExt<RtpExtTwoByte>(ret, media, ptr, end);
return ret; return ret;
} }
return ret; return ret;
} }
#define RTP_EXT_MAP(XX) \ #define RTP_EXT_MAP(XX) \
XX(RtpExtType::ssrc_audio_level, "urn:ietf:params:rtp-hdrext:ssrc-audio-level") \ XX(ssrc_audio_level, "urn:ietf:params:rtp-hdrext:ssrc-audio-level") \
XX(RtpExtType::abs_send_time, "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time") \ XX(abs_send_time, "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time") \
XX(RtpExtType::transport_cc, "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01") \ XX(transport_cc, "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01") \
XX(RtpExtType::sdes_mid, "urn:ietf:params:rtp-hdrext:sdes:mid") \ XX(sdes_mid, "urn:ietf:params:rtp-hdrext:sdes:mid") \
XX(RtpExtType::sdes_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id") \ XX(sdes_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id") \
XX(RtpExtType::sdes_repaired_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id") \ XX(sdes_repaired_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id") \
XX(RtpExtType::video_timing, "http://www.webrtc.org/experiments/rtp-hdrext/video-timing") \ XX(video_timing, "http://www.webrtc.org/experiments/rtp-hdrext/video-timing") \
XX(RtpExtType::color_space, "http://www.webrtc.org/experiments/rtp-hdrext/color-space") \ XX(color_space, "http://www.webrtc.org/experiments/rtp-hdrext/color-space") \
XX(RtpExtType::video_content_type, "http://www.webrtc.org/experiments/rtp-hdrext/video-content-type") \ XX(video_content_type, "http://www.webrtc.org/experiments/rtp-hdrext/video-content-type") \
XX(RtpExtType::playout_delay, "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay") \ XX(playout_delay, "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay") \
XX(RtpExtType::video_orientation, "urn:3gpp:video-orientation") \ XX(video_orientation, "urn:3gpp:video-orientation") \
XX(RtpExtType::toffset, "urn:ietf:params:rtp-hdrext:toffset") XX(toffset, "urn:ietf:params:rtp-hdrext:toffset")
#define XX(type, url) {type , url}, #define XX(type, url) {RtpExtType::type , url},
static unordered_map<RtpExtType/*id*/, string/*ext*/> s_type_to_url = {RTP_EXT_MAP(XX)}; static unordered_map<RtpExtType/*id*/, string/*ext*/> s_type_to_url = {RTP_EXT_MAP(XX)};
#undef XX #undef XX
#define XX(type, url) {url, type}, #define XX(type, url) {url, RtpExtType::type},
static unordered_map<string/*ext*/, RtpExtType/*id*/> s_url_to_type = {RTP_EXT_MAP(XX)}; static unordered_map<string/*ext*/, RtpExtType/*id*/> s_url_to_type = {RTP_EXT_MAP(XX)};
#undef XX #undef XX
...@@ -197,3 +196,17 @@ const string &RtpExt::getExtUrl(RtpExtType type) { ...@@ -197,3 +196,17 @@ const string &RtpExt::getExtUrl(RtpExtType type) {
return it->second; return it->second;
} }
const char *RtpExt::getExtName(RtpExtType type) {
#define XX(type, url) case RtpExtType::type: return #type;
switch (type) {
RTP_EXT_MAP(XX)
default: return "unknown ext type";
}
#undef XX
}
string RtpExt::dumpString() const {
_StrPrinter printer;
printer << getExtName(_type) << ", id:" << (int)_id << " " << hexdump(data(), size());
return std::move(printer);
}
...@@ -41,16 +41,19 @@ class RtcMedia; ...@@ -41,16 +41,19 @@ class RtcMedia;
class RtpExt : public std::string { class RtpExt : public std::string {
public: public:
RtpExt(RtpExtType type, uint8_t id, const char *str, size_t size) : std::string(str, size), _type(type), _id(id) {}
~RtpExt() = default; ~RtpExt() = default;
static map<uint8_t/*id*/, RtpExt/*data*/> getExtValue(const RtpHeader *header, const RtcMedia &media);
static map<RtpExtType/*type*/, RtpExt/*data*/> getExtValue(const RtpHeader *header, const RtcMedia &media);
static RtpExtType getExtType(const string &url); static RtpExtType getExtType(const string &url);
static const string& getExtUrl(RtpExtType type); static const string& getExtUrl(RtpExtType type);
static const char *getExtName(RtpExtType type);
private: string dumpString() const;
RtpExt(RtpExtType type, const char *str, size_t size) : std::string(str, size), _type(type) {}
private: private:
RtpExtType _type; RtpExtType _type;
uint8_t _id;
}; };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论