完善rtp ext相关代码

This commit is contained in:
xia-chu 2021-05-07 12:00:26 +08:00
parent b2be30fd51
commit 2e8a7be5ea
2 changed files with 80 additions and 10 deletions

View File

@ -108,8 +108,8 @@ 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*/, string/*data*/> RtpExt::getExtValue(const RtpHeader *header) { map<uint8_t/*id*/, RtpExt/*data*/> RtpExt::getExtValue(const RtpHeader *header, const RtcMedia &media) {
map<uint8_t, string> ret; map<uint8_t, RtpExt> ret;
assert(header); assert(header);
auto ext_size = header->getExtSize(); auto ext_size = header->getExtSize();
if (!ext_size) { if (!ext_size) {
@ -118,20 +118,20 @@ map<uint8_t/*id*/, string/*data*/> RtpExt::getExtValue(const RtpHeader *header)
auto reserved = header->getExtReserved(); auto reserved = header->getExtReserved();
auto ptr = const_cast<RtpHeader *>(header)->getExtData(); auto ptr = const_cast<RtpHeader *>(header)->getExtData();
auto end = ptr + ext_size; auto end = ptr + ext_size;
RtpExtType type;
if (reserved == kOneByteHeader) { if (reserved == kOneByteHeader) {
while (ptr < end) { while (ptr < end) {
RtpExtOneByte *ext = reinterpret_cast<RtpExtOneByte *>(ptr); RtpExtOneByte *ext = reinterpret_cast<RtpExtOneByte *>(ptr);
if (ext->getId() == 0) { if (ext->getId() == (uint8_t) RtpExtType::padding) {
//padding忽略 //padding忽略
++ptr; ++ptr;
continue; continue;
} }
//15类型的rtp ext为保留 //15类型的rtp ext为保留
CHECK(ext->getId() != 15); CHECK(ext->getId() < (uint8_t) RtpExtType::reserved);
CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtOneByte::kMinSize <= end); CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtOneByte::kMinSize <= end);
CHECK(ext->getData() + ext->getSize() <= end); CHECK(ext->getData() + ext->getSize() <= end);
ret.emplace(ext->getId(), string(reinterpret_cast<char *>(ext->getData()), ext->getSize())); ret.emplace(ext->getId(), RtpExt(type, reinterpret_cast<char *>(ext->getData()), ext->getSize()));
ptr += RtpExtOneByte::kMinSize + ext->getSize(); ptr += RtpExtOneByte::kMinSize + ext->getSize();
} }
return ret; return ret;
@ -140,14 +140,16 @@ map<uint8_t/*id*/, string/*data*/> RtpExt::getExtValue(const RtpHeader *header)
if ((reserved & 0xFFF0) >> 4 == kTwoByteHeader) { if ((reserved & 0xFFF0) >> 4 == kTwoByteHeader) {
while (ptr < end) { while (ptr < end) {
RtpExtTwoByte *ext = reinterpret_cast<RtpExtTwoByte *>(ptr); RtpExtTwoByte *ext = reinterpret_cast<RtpExtTwoByte *>(ptr);
if (ext->getId() == 0) { if (ext->getId() == (uint8_t) RtpExtType::padding) {
//padding忽略 //padding忽略
++ptr; ++ptr;
continue; continue;
} }
//15类型的rtp ext为保留
CHECK(ext->getId() < (uint8_t) RtpExtType::reserved);
CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtTwoByte::kMinSize <= end); CHECK(reinterpret_cast<uint8_t *>(ext) + RtpExtTwoByte::kMinSize <= end);
CHECK(ext->getData() + ext->getSize() <= end); CHECK(ext->getData() + ext->getSize() <= end);
ret.emplace(ext->getId(), string(reinterpret_cast<char *>(ext->getData()), ext->getSize())); ret.emplace(ext->getId(), RtpExt(type, reinterpret_cast<char *>(ext->getData()), ext->getSize()));
ptr += RtpExtTwoByte::kMinSize + ext->getSize(); ptr += RtpExtTwoByte::kMinSize + ext->getSize();
} }
return ret; return ret;
@ -155,3 +157,43 @@ map<uint8_t/*id*/, string/*data*/> RtpExt::getExtValue(const RtpHeader *header)
return ret; return ret;
} }
#define RTP_EXT_MAP(XX) \
XX(RtpExtType::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(RtpExtType::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(RtpExtType::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(RtpExtType::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(RtpExtType::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(RtpExtType::video_orientation, "urn:3gpp:video-orientation") \
XX(RtpExtType::toffset, "urn:ietf:params:rtp-hdrext:toffset")
#define XX(type, url) {type , url},
static unordered_map<RtpExtType/*id*/, string/*ext*/> s_type_to_url = {RTP_EXT_MAP(XX)};
#undef XX
#define XX(type, url) {url, type},
static unordered_map<string/*ext*/, RtpExtType/*id*/> s_url_to_type = {RTP_EXT_MAP(XX)};
#undef XX
RtpExtType RtpExt::getRtpExtType(const string &url) {
auto it = s_url_to_type.find(url);
if (it == s_url_to_type.end()) {
throw std::invalid_argument(string("未识别的rtp ext url类型:") + url);
}
return it->second;
}
const string &RtpExt::getRtpExtUrl(RtpExtType type) {
auto it = s_type_to_url.find(type);
if (it == s_type_to_url.end()) {
throw std::invalid_argument(string("未识别的rtp ext类型:") + to_string((int) type));
}
return it->second;
}

View File

@ -20,9 +20,37 @@
using namespace std; using namespace std;
using namespace mediakit; using namespace mediakit;
class RtpExt { enum class RtpExtType : uint8_t {
padding = 0,
ssrc_audio_level = 1,
abs_send_time = 2,
transport_cc = 3,
sdes_mid = 4,
sdes_rtp_stream_id = 5,
sdes_repaired_rtp_stream_id = 6,
video_timing = 7,
color_space = 8,
video_content_type = 11,
playout_delay = 12,
video_orientation = 13,
toffset = 14,
reserved = 15,
};
class RtcMedia;
class RtpExt : public std::string {
public: public:
static map<uint8_t/*id*/, string/*data*/> getExtValue(const RtpHeader *header); ~RtpExt() = default;
static map<uint8_t/*id*/, RtpExt/*data*/> getExtValue(const RtpHeader *header, const RtcMedia &media);
static RtpExtType getRtpExtType(const string &url);
static const string& getRtpExtUrl(RtpExtType type);
private:
RtpExt(RtpExtType type, const char *str, size_t size) : std::string(str, size), _type(type) {}
private:
RtpExtType _type;
}; };