Format code

This commit is contained in:
xiongziliang 2024-03-23 23:08:10 +08:00
parent d8893877b2
commit 7aaafa18e7
2 changed files with 436 additions and 460 deletions

View File

@ -20,19 +20,19 @@ namespace mediakit {
namespace Rtc { namespace Rtc {
#define RTC_FIELD "rtc." #define RTC_FIELD "rtc."
const string kPreferredCodecA = RTC_FIELD"preferredCodecA"; const string kPreferredCodecA = RTC_FIELD "preferredCodecA";
const string kPreferredCodecV = RTC_FIELD"preferredCodecV"; const string kPreferredCodecV = RTC_FIELD "preferredCodecV";
static onceToken token([]() { static onceToken token([]() {
mINI::Instance()[kPreferredCodecA] = "PCMA,PCMU,opus,mpeg4-generic"; mINI::Instance()[kPreferredCodecA] = "PCMA,PCMU,opus,mpeg4-generic";
mINI::Instance()[kPreferredCodecV] = "H264,H265,AV1,VP9,VP8"; mINI::Instance()[kPreferredCodecV] = "H264,H265,AV1,VP9,VP8";
}); });
} } // namespace Rtc
using onCreateSdpItem = function<SdpItem::Ptr(const string &key, const string &value)>; using onCreateSdpItem = function<SdpItem::Ptr(const string &key, const string &value)>;
static map<string, onCreateSdpItem, StrCaseCompare> sdpItemCreator; static map<string, onCreateSdpItem, StrCaseCompare> sdpItemCreator;
template <typename Item> template <typename Item>
void registerSdpItem(){ void registerSdpItem() {
onCreateSdpItem func = [](const string &key, const string &value) { onCreateSdpItem func = [](const string &key, const string &value) {
auto ret = std::make_shared<Item>(); auto ret = std::make_shared<Item>();
ret->parse(value); ret->parse(value);
@ -47,52 +47,50 @@ public:
virtual RtpDirection getDirection() const = 0; virtual RtpDirection getDirection() const = 0;
}; };
class SdpDirectionSendonly : public SdpItem, public DirectionInterface{ class SdpDirectionSendonly : public SdpItem, public DirectionInterface {
public: public:
const char* getKey() const override { return getRtpDirectionString(getDirection());} const char *getKey() const override { return getRtpDirectionString(getDirection()); }
RtpDirection getDirection() const override {return RtpDirection::sendonly;} RtpDirection getDirection() const override { return RtpDirection::sendonly; }
}; };
class SdpDirectionRecvonly : public SdpItem, public DirectionInterface{ class SdpDirectionRecvonly : public SdpItem, public DirectionInterface {
public: public:
const char* getKey() const override { return getRtpDirectionString(getDirection());} const char *getKey() const override { return getRtpDirectionString(getDirection()); }
RtpDirection getDirection() const override {return RtpDirection::recvonly;} RtpDirection getDirection() const override { return RtpDirection::recvonly; }
}; };
class SdpDirectionSendrecv : public SdpItem, public DirectionInterface{ class SdpDirectionSendrecv : public SdpItem, public DirectionInterface {
public: public:
const char* getKey() const override { return getRtpDirectionString(getDirection());} const char *getKey() const override { return getRtpDirectionString(getDirection()); }
RtpDirection getDirection() const override {return RtpDirection::sendrecv;} RtpDirection getDirection() const override { return RtpDirection::sendrecv; }
}; };
class SdpDirectionInactive : public SdpItem, public DirectionInterface{ class SdpDirectionInactive : public SdpItem, public DirectionInterface {
public: public:
const char* getKey() const override { return getRtpDirectionString(getDirection());} const char *getKey() const override { return getRtpDirectionString(getDirection()); }
RtpDirection getDirection() const override {return RtpDirection::inactive;} RtpDirection getDirection() const override { return RtpDirection::inactive; }
}; };
class DirectionInterfaceImp : public SdpItem, public DirectionInterface{ class DirectionInterfaceImp : public SdpItem, public DirectionInterface {
public: public:
DirectionInterfaceImp(RtpDirection direct){ DirectionInterfaceImp(RtpDirection direct) { direction = direct; }
direction = direct; const char *getKey() const override { return getRtpDirectionString(getDirection()); }
} RtpDirection getDirection() const override { return direction; }
const char* getKey() const override { return getRtpDirectionString(getDirection());}
RtpDirection getDirection() const override {return direction;}
private: private:
RtpDirection direction; RtpDirection direction;
}; };
static bool registerAllItem(){ static bool registerAllItem() {
registerSdpItem<SdpString<'v'> >(); registerSdpItem<SdpString<'v'>>();
registerSdpItem<SdpString<'s'> >(); registerSdpItem<SdpString<'s'>>();
registerSdpItem<SdpString<'i'> >(); registerSdpItem<SdpString<'i'>>();
registerSdpItem<SdpString<'u'> >(); registerSdpItem<SdpString<'u'>>();
registerSdpItem<SdpString<'e'> >(); registerSdpItem<SdpString<'e'>>();
registerSdpItem<SdpString<'p'> >(); registerSdpItem<SdpString<'p'>>();
registerSdpItem<SdpString<'z'> >(); registerSdpItem<SdpString<'z'>>();
registerSdpItem<SdpString<'k'> >(); registerSdpItem<SdpString<'k'>>();
registerSdpItem<SdpString<'r'> >(); registerSdpItem<SdpString<'r'>>();
registerSdpItem<SdpTime>(); registerSdpItem<SdpTime>();
registerSdpItem<SdpOrigin>(); registerSdpItem<SdpOrigin>();
registerSdpItem<SdpConnection>(); registerSdpItem<SdpConnection>();
@ -138,11 +136,11 @@ DtlsRole getDtlsRole(const string &str) {
return it == dtls_role_map.end() ? DtlsRole::invalid : it->second; return it == dtls_role_map.end() ? DtlsRole::invalid : it->second;
} }
const char* getDtlsRoleString(DtlsRole role){ const char *getDtlsRoleString(DtlsRole role) {
switch (role) { switch (role) {
case DtlsRole::active : return "active"; case DtlsRole::active: return "active";
case DtlsRole::passive : return "passive"; case DtlsRole::passive: return "passive";
case DtlsRole::actpass : return "actpass"; case DtlsRole::actpass: return "actpass";
default: return "invalid"; default: return "invalid";
} }
} }
@ -159,12 +157,12 @@ RtpDirection getRtpDirection(const string &str) {
return it == direction_map.end() ? RtpDirection::invalid : it->second; return it == direction_map.end() ? RtpDirection::invalid : it->second;
} }
const char* getRtpDirectionString(RtpDirection val){ const char *getRtpDirectionString(RtpDirection val) {
switch (val) { switch (val) {
case RtpDirection::sendonly : return "sendonly"; case RtpDirection::sendonly: return "sendonly";
case RtpDirection::recvonly : return "recvonly"; case RtpDirection::recvonly: return "recvonly";
case RtpDirection::sendrecv : return "sendrecv"; case RtpDirection::sendrecv: return "sendrecv";
case RtpDirection::inactive : return "inactive"; case RtpDirection::inactive: return "inactive";
default: return "invalid"; default: return "invalid";
} }
} }
@ -179,7 +177,7 @@ string RtcSdpBase::toString() const {
return std::move(printer); return std::move(printer);
} }
RtpDirection RtcSdpBase::getDirection() const{ RtpDirection RtcSdpBase::getDirection() const {
for (auto &item : items) { for (auto &item : items) {
auto attr = dynamic_pointer_cast<SdpAttr>(item); auto attr = dynamic_pointer_cast<SdpAttr>(item);
if (attr) { if (attr) {
@ -200,7 +198,7 @@ SdpItem::Ptr RtcSdpBase::getItem(char key_c, const char *attr_key) const {
return item; return item;
} }
auto attr = dynamic_pointer_cast<SdpAttr>(item); auto attr = dynamic_pointer_cast<SdpAttr>(item);
if (attr && !strcasecmp(attr->detail->getKey() , attr_key)) { if (attr && !strcasecmp(attr->detail->getKey(), attr_key)) {
return attr->detail; return attr->detail;
} }
} }
@ -225,7 +223,7 @@ string RtcSessionSdp::getSessionInfo() const {
return getStringItem('i'); return getStringItem('i');
} }
SdpTime RtcSessionSdp::getSessionTime() const{ SdpTime RtcSessionSdp::getSessionTime() const {
return getItemClass<SdpTime>('t'); return getItemClass<SdpTime>('t');
} }
@ -475,7 +473,7 @@ string SdpAttrRtcp::toString() const {
return SdpItem::toString(); return SdpItem::toString();
} }
void SdpAttrIceOption::parse(const string &str){ void SdpAttrIceOption::parse(const string &str) {
auto vec = split(str, " "); auto vec = split(str, " ");
for (auto &v : vec) { for (auto &v : vec) {
if (!strcasecmp(v.data(), "trickle")) { if (!strcasecmp(v.data(), "trickle")) {
@ -489,7 +487,7 @@ void SdpAttrIceOption::parse(const string &str){
} }
} }
string SdpAttrIceOption::toString() const{ string SdpAttrIceOption::toString() const {
if (value.empty()) { if (value.empty()) {
if (trickle && renomination) { if (trickle && renomination) {
value = "trickle renomination"; value = "trickle renomination";
@ -529,8 +527,8 @@ string SdpAttrSetup::toString() const {
} }
void SdpAttrExtmap::parse(const string &str) { void SdpAttrExtmap::parse(const string &str) {
char buf[128] = {0}; char buf[128] = { 0 };
char direction_buf[32] = {0}; char direction_buf[32] = { 0 };
if (sscanf(str.data(), "%" SCNd8 "/%31[^ ] %127s", &id, direction_buf, buf) != 3) { if (sscanf(str.data(), "%" SCNd8 "/%31[^ ] %127s", &id, direction_buf, buf) != 3) {
CHECK_SDP(sscanf(str.data(), "%" SCNd8 " %127s", &id, buf) == 2); CHECK_SDP(sscanf(str.data(), "%" SCNd8 " %127s", &id, buf) == 2);
direction = RtpDirection::sendrecv; direction = RtpDirection::sendrecv;
@ -542,7 +540,7 @@ void SdpAttrExtmap::parse(const string &str) {
string SdpAttrExtmap::toString() const { string SdpAttrExtmap::toString() const {
if (value.empty()) { if (value.empty()) {
if(direction == RtpDirection::invalid || direction == RtpDirection::sendrecv){ if (direction == RtpDirection::invalid || direction == RtpDirection::sendrecv) {
value = to_string((int)id) + " " + ext; value = to_string((int)id) + " " + ext;
} else { } else {
value = to_string((int)id) + "/" + getRtpDirectionString(direction) + " " + ext; value = to_string((int)id) + "/" + getRtpDirectionString(direction) + " " + ext;
@ -552,11 +550,11 @@ string SdpAttrExtmap::toString() const {
} }
void SdpAttrRtpMap::parse(const string &str) { void SdpAttrRtpMap::parse(const string &str) {
char buf[32] = {0}; char buf[32] = { 0 };
if (sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32 "/%" SCNd32, &pt, buf, &sample_rate, &channel) != 4) { if (sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32 "/%" SCNd32, &pt, buf, &sample_rate, &channel) != 4) {
CHECK_SDP(sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32, &pt, buf, &sample_rate) == 3); CHECK_SDP(sscanf(str.data(), "%" SCNu8 " %31[^/]/%" SCNd32, &pt, buf, &sample_rate) == 3);
if (getTrackType(getCodecId(buf)) == TrackAudio) { if (getTrackType(getCodecId(buf)) == TrackAudio) {
//未指定通道数时且为音频时那么通道数默认为1 // 未指定通道数时且为音频时那么通道数默认为1
channel = 1; channel = 1;
} }
} }
@ -576,7 +574,7 @@ string SdpAttrRtpMap::toString() const {
void SdpAttrRtcpFb::parse(const string &str_in) { void SdpAttrRtcpFb::parse(const string &str_in) {
auto str = str_in + "\n"; auto str = str_in + "\n";
char rtcp_type_buf[32] = {0}; char rtcp_type_buf[32] = { 0 };
CHECK_SDP(sscanf(str.data(), "%" SCNu8 " %31[^\n]", &pt, rtcp_type_buf) == 2); CHECK_SDP(sscanf(str.data(), "%" SCNu8 " %31[^\n]", &pt, rtcp_type_buf) == 2);
rtcp_type = rtcp_type_buf; rtcp_type = rtcp_type_buf;
} }
@ -596,7 +594,7 @@ void SdpAttrFmtp::parse(const string &str) {
for (auto &item : vec) { for (auto &item : vec) {
trim(item); trim(item);
auto pos = item.find('='); auto pos = item.find('=');
if(pos == string::npos){ if (pos == string::npos) {
fmtp.emplace(std::make_pair(item, "")); fmtp.emplace(std::make_pair(item, ""));
} else { } else {
fmtp.emplace(std::make_pair(item.substr(0, pos), item.substr(pos + 1))); fmtp.emplace(std::make_pair(item.substr(0, pos), item.substr(pos + 1)));
@ -619,8 +617,8 @@ string SdpAttrFmtp::toString() const {
void SdpAttrSSRC::parse(const string &str_in) { void SdpAttrSSRC::parse(const string &str_in) {
auto str = str_in + '\n'; auto str = str_in + '\n';
char attr_buf[32] = {0}; char attr_buf[32] = { 0 };
char attr_val_buf[128] = {0}; char attr_val_buf[128] = { 0 };
if (3 == sscanf(str.data(), "%" SCNu32 " %31[^:]:%127[^\n]", &ssrc, attr_buf, attr_val_buf)) { if (3 == sscanf(str.data(), "%" SCNu32 " %31[^:]:%127[^\n]", &ssrc, attr_buf, attr_val_buf)) {
attribute = attr_buf; attribute = attr_buf;
attribute_value = attr_val_buf; attribute_value = attr_val_buf;
@ -650,14 +648,14 @@ void SdpAttrSSRCGroup::parse(const string &str) {
CHECK(isFID() || isSIM()); CHECK(isFID() || isSIM());
vec.erase(vec.begin()); vec.erase(vec.begin());
for (auto ssrc : vec) { for (auto ssrc : vec) {
ssrcs.emplace_back((uint32_t) atoll(ssrc.data())); ssrcs.emplace_back((uint32_t)atoll(ssrc.data()));
} }
} }
string SdpAttrSSRCGroup::toString() const { string SdpAttrSSRCGroup::toString() const {
if (value.empty()) { if (value.empty()) {
value = type; value = type;
//最少要求2个ssrc // 最少要求2个ssrc
CHECK(ssrcs.size() >= 2); CHECK(ssrcs.size() >= 2);
for (auto &ssrc : ssrcs) { for (auto &ssrc : ssrcs) {
value += ' '; value += ' ';
@ -668,7 +666,7 @@ string SdpAttrSSRCGroup::toString() const {
} }
void SdpAttrSctpMap::parse(const string &str) { void SdpAttrSctpMap::parse(const string &str) {
char subtypes_buf[64] = {0}; char subtypes_buf[64] = { 0 };
CHECK_SDP(3 == sscanf(str.data(), "%" SCNu16 " %63[^ ] %" SCNd32, &port, subtypes_buf, &streams)); CHECK_SDP(3 == sscanf(str.data(), "%" SCNu16 " %63[^ ] %" SCNd32, &port, subtypes_buf, &streams));
subtypes = subtypes_buf; subtypes = subtypes_buf;
} }
@ -685,10 +683,10 @@ string SdpAttrSctpMap::toString() const {
} }
void SdpAttrCandidate::parse(const string &str) { void SdpAttrCandidate::parse(const string &str) {
char foundation_buf[40] = {0}; char foundation_buf[40] = { 0 };
char transport_buf[16] = {0}; char transport_buf[16] = { 0 };
char address_buf[64] = {0}; char address_buf[64] = { 0 };
char type_buf[16] = {0}; char type_buf[16] = { 0 };
// https://datatracker.ietf.org/doc/html/rfc5245#section-15.1 // https://datatracker.ietf.org/doc/html/rfc5245#section-15.1
CHECK_SDP(sscanf(str.data(), "%32[^ ] %" SCNu32 " %15[^ ] %" SCNu32 " %63[^ ] %" SCNu16 " typ %15[^ ]", CHECK_SDP(sscanf(str.data(), "%32[^ ] %" SCNu32 " %15[^ ] %" SCNu32 " %63[^ ] %" SCNu16 " typ %15[^ ]",
@ -717,8 +715,7 @@ void SdpAttrCandidate::parse(const string &str) {
string SdpAttrCandidate::toString() const { string SdpAttrCandidate::toString() const {
if (value.empty()) { if (value.empty()) {
value = foundation + " " + to_string(component) + " " + transport + " " + to_string(priority) + value = foundation + " " + to_string(component) + " " + transport + " " + to_string(priority) + " " + address + " " + to_string(port) + " typ " + type;
" " + address + " " + to_string(port) + " typ " + type;
for (auto &pr : arr) { for (auto &pr : arr) {
value += ' '; value += ' ';
value += pr.first; value += pr.first;
@ -730,10 +727,10 @@ string SdpAttrCandidate::toString() const {
} }
void SdpAttrSimulcast::parse(const string &str) { void SdpAttrSimulcast::parse(const string &str) {
//https://www.meetecho.com/blog/simulcast-janus-ssrc/ // https://www.meetecho.com/blog/simulcast-janus-ssrc/
//a=simulcast:send/recv q;h;f // a=simulcast:send/recv q;h;f
//a=simulcast:send/recv [rid=]q;h;f // a=simulcast:send/recv [rid=]q;h;f
//a=simulcast: recv h;m;l // a=simulcast: recv h;m;l
// //
auto vec = split(str, " "); auto vec = split(str, " ");
CHECK_SDP(vec.size() == 2); CHECK_SDP(vec.size() == 2);
@ -845,12 +842,12 @@ void RtcSession::loadFrom(const string &str) {
for (auto &group : ssrc_groups) { for (auto &group : ssrc_groups) {
if (group.isFID()) { if (group.isFID()) {
have_rtx_ssrc = true; have_rtx_ssrc = true;
//ssrc-group:FID字段必须包含rtp/rtx的ssrc // ssrc-group:FID字段必须包含rtp/rtx的ssrc
CHECK(group.ssrcs.size() == 2); CHECK(group.ssrcs.size() == 2);
//根据rtp ssrc找到对象 // 根据rtp ssrc找到对象
auto it = rtc_ssrc_map.find(group.ssrcs[0]); auto it = rtc_ssrc_map.find(group.ssrcs[0]);
CHECK(it != rtc_ssrc_map.end()); CHECK(it != rtc_ssrc_map.end());
//设置rtx ssrc // 设置rtx ssrc
it->second.rtx_ssrc = group.ssrcs[1]; it->second.rtx_ssrc = group.ssrcs[1];
rtc_media.rtp_rtx_ssrc.emplace_back(it->second); rtc_media.rtp_rtx_ssrc.emplace_back(it->second);
} else if (group.isSIM()) { } else if (group.isSIM()) {
@ -860,7 +857,7 @@ void RtcSession::loadFrom(const string &str) {
} }
if (!have_rtx_ssrc) { if (!have_rtx_ssrc) {
//按照sdp顺序依次添加ssrc // 按照sdp顺序依次添加ssrc
for (auto &attr : ssrc_attr) { for (auto &attr : ssrc_attr) {
if (attr.attribute == "cname") { if (attr.attribute == "cname") {
rtc_media.rtp_rtx_ssrc.emplace_back(rtc_ssrc_map[attr.ssrc]); rtc_media.rtp_rtx_ssrc.emplace_back(rtc_ssrc_map[attr.ssrc]);
@ -883,56 +880,56 @@ void RtcSession::loadFrom(const string &str) {
CHECK(rid.direction == simulcast.direction); CHECK(rid.direction == simulcast.direction);
CHECK(rid_map.find(rid.rid) != rid_map.end()); CHECK(rid_map.find(rid.rid) != rid_map.end());
} }
//simulcast最少要求2种方案 // simulcast最少要求2种方案
CHECK(simulcast.rids.size() >= 2); CHECK(simulcast.rids.size() >= 2);
rtc_media.rtp_rids = simulcast.rids; rtc_media.rtp_rids = simulcast.rids;
} }
if (ssrc_group_sim) { if (ssrc_group_sim) {
//指定了a=ssrc-group:SIM // 指定了a=ssrc-group:SIM
for (auto ssrc : ssrc_group_sim->ssrcs) { for (auto ssrc : ssrc_group_sim->ssrcs) {
auto it = rtc_ssrc_map.find(ssrc); auto it = rtc_ssrc_map.find(ssrc);
CHECK(it != rtc_ssrc_map.end()); CHECK(it != rtc_ssrc_map.end());
rtc_media.rtp_ssrc_sim.emplace_back(it->second); rtc_media.rtp_ssrc_sim.emplace_back(it->second);
} }
} else if (!rtc_media.rtp_rids.empty()) { } else if (!rtc_media.rtp_rids.empty()) {
//未指定a=ssrc-group:SIM, 但是指定了a=simulcast, 那么只能根据ssrc顺序来对应rid顺序 // 未指定a=ssrc-group:SIM, 但是指定了a=simulcast, 那么只能根据ssrc顺序来对应rid顺序
rtc_media.rtp_ssrc_sim = rtc_media.rtp_rtx_ssrc; rtc_media.rtp_ssrc_sim = rtc_media.rtp_rtx_ssrc;
} }
if (!rtc_media.supportSimulcast()) { if (!rtc_media.supportSimulcast()) {
//不支持simulcast的情况下最多一组ssrc // 不支持simulcast的情况下最多一组ssrc
CHECK(rtc_media.rtp_rtx_ssrc.size() <= 1); CHECK(rtc_media.rtp_rtx_ssrc.size() <= 1);
} else { } else {
//simulcast的情况下要么没有指定ssrc要么指定的ssrc个数与rid个数一致 // simulcast的情况下要么没有指定ssrc要么指定的ssrc个数与rid个数一致
//CHECK(rtc_media.rtp_ssrc_sim.empty() || rtc_media.rtp_ssrc_sim.size() == rtc_media.rtp_rids.size()); // CHECK(rtc_media.rtp_ssrc_sim.empty() || rtc_media.rtp_ssrc_sim.size() == rtc_media.rtp_rids.size());
} }
auto rtpmap_arr = media.getAllItem<SdpAttrRtpMap>('a', "rtpmap"); auto rtpmap_arr = media.getAllItem<SdpAttrRtpMap>('a', "rtpmap");
auto rtcpfb_arr = media.getAllItem<SdpAttrRtcpFb>('a', "rtcp-fb"); auto rtcpfb_arr = media.getAllItem<SdpAttrRtcpFb>('a', "rtcp-fb");
auto fmtp_aar = media.getAllItem<SdpAttrFmtp>('a', "fmtp"); auto fmtp_aar = media.getAllItem<SdpAttrFmtp>('a', "fmtp");
//方便根据pt查找rtpmap,一个pt必有一条 // 方便根据pt查找rtpmap,一个pt必有一条
map<uint8_t, SdpAttrRtpMap &> rtpmap_map; map<uint8_t, SdpAttrRtpMap &> rtpmap_map;
//方便根据pt查找rtcp-fb,一个pt可能有多条或0条 // 方便根据pt查找rtcp-fb,一个pt可能有多条或0条
multimap<uint8_t, SdpAttrRtcpFb &> rtcpfb_map; multimap<uint8_t, SdpAttrRtcpFb &> rtcpfb_map;
//方便根据pt查找fmtp一个pt最多一条 // 方便根据pt查找fmtp一个pt最多一条
map<uint8_t, SdpAttrFmtp &> fmtp_map; map<uint8_t, SdpAttrFmtp &> fmtp_map;
for (auto &rtpmap : rtpmap_arr) { for (auto &rtpmap : rtpmap_arr) {
//添加失败,有多条 // 添加失败,有多条
CHECK(rtpmap_map.emplace(rtpmap.pt, rtpmap).second, "该pt存在多条a=rtpmap:", (int)rtpmap.pt); CHECK(rtpmap_map.emplace(rtpmap.pt, rtpmap).second, "该pt存在多条a=rtpmap:", (int)rtpmap.pt);
} }
for (auto &rtpfb : rtcpfb_arr) { for (auto &rtpfb : rtcpfb_arr) {
rtcpfb_map.emplace(rtpfb.pt, rtpfb); rtcpfb_map.emplace(rtpfb.pt, rtpfb);
} }
for (auto &fmtp : fmtp_aar) { for (auto &fmtp : fmtp_aar) {
//添加失败,有多条 // 添加失败,有多条
CHECK(fmtp_map.emplace(fmtp.pt, fmtp).second, "该pt存在多条a=fmtp:", (int)fmtp.pt); CHECK(fmtp_map.emplace(fmtp.pt, fmtp).second, "该pt存在多条a=fmtp:", (int)fmtp.pt);
} }
for (auto &item : mline.fmts) { for (auto &item : mline.fmts) {
auto pt = atoi(item.c_str()); auto pt = atoi(item.c_str());
CHECK(pt < 0xFF, "invalid payload type: ", item); CHECK(pt < 0xFF, "invalid payload type: ", item);
//遍历所有编码方案的pt // 遍历所有编码方案的pt
rtc_media.plan.emplace_back(); rtc_media.plan.emplace_back();
auto &plan = rtc_media.plan.back(); auto &plan = rtc_media.plan.back();
auto rtpmap_it = rtpmap_map.find(pt); auto rtpmap_it = rtpmap_map.find(pt);
@ -952,8 +949,7 @@ void RtcSession::loadFrom(const string &str) {
if (fmtp_it != fmtp_map.end()) { if (fmtp_it != fmtp_map.end()) {
plan.fmtp = fmtp_it->second.fmtp; plan.fmtp = fmtp_it->second.fmtp;
} }
for (auto rtpfb_it = rtcpfb_map.find(pt); for (auto rtpfb_it = rtcpfb_map.find(pt); rtpfb_it != rtcpfb_map.end() && rtpfb_it->second.pt == pt; ++rtpfb_it) {
rtpfb_it != rtcpfb_map.end() && rtpfb_it->second.pt == pt; ++rtpfb_it) {
plan.rtcp_fb.emplace(rtpfb_it->second.rtcp_type); plan.rtcp_fb.emplace(rtpfb_it->second.rtcp_type);
} }
} }
@ -971,7 +967,7 @@ void RtcSdpBase::toRtsp() {
case 'i': case 'i':
case 't': case 't':
case 'c': case 'c':
case 'b':{ case 'b': {
++it; ++it;
break; break;
} }
@ -986,8 +982,7 @@ void RtcSdpBase::toRtsp() {
case 'a': { case 'a': {
auto attr = dynamic_pointer_cast<SdpAttr>(*it); auto attr = dynamic_pointer_cast<SdpAttr>(*it);
CHECK(attr); CHECK(attr);
if (!strcasecmp(attr->detail->getKey(), "rtpmap") if (!strcasecmp(attr->detail->getKey(), "rtpmap") || !strcasecmp(attr->detail->getKey(), "fmtp")) {
|| !strcasecmp(attr->detail->getKey(), "fmtp")) {
++it; ++it;
break; break;
} }
@ -1000,7 +995,7 @@ void RtcSdpBase::toRtsp() {
} }
} }
string RtcSession::toRtspSdp() const{ string RtcSession::toRtspSdp() const {
RtcSession copy = *this; RtcSession copy = *this;
copy.media.clear(); copy.media.clear();
for (auto &m : media) { for (auto &m : media) {
@ -1056,17 +1051,17 @@ void addSdpAttrSSRC(const RtcSSRC &rtp_ssrc, RtcSdpBase &media, uint32_t ssrc_nu
} }
} }
RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const {
RtcSessionSdp::Ptr ret = std::make_shared<RtcSessionSdp>(); RtcSessionSdp::Ptr ret = std::make_shared<RtcSessionSdp>();
auto &sdp = *ret; auto &sdp = *ret;
sdp.addItem(std::make_shared<SdpString<'v'> >(to_string(version))); sdp.addItem(std::make_shared<SdpString<'v'>>(to_string(version)));
sdp.addItem(std::make_shared<SdpOrigin>(origin)); sdp.addItem(std::make_shared<SdpOrigin>(origin));
sdp.addItem(std::make_shared<SdpString<'s'> >(session_name)); sdp.addItem(std::make_shared<SdpString<'s'>>(session_name));
if (!session_info.empty()) { if (!session_info.empty()) {
sdp.addItem(std::make_shared<SdpString<'i'> >(session_info)); sdp.addItem(std::make_shared<SdpString<'i'>>(session_info));
} }
sdp.addItem(std::make_shared<SdpTime>(time)); sdp.addItem(std::make_shared<SdpTime>(time));
if(connection.empty()){ if (connection.empty()) {
sdp.addItem(std::make_shared<SdpConnection>(connection)); sdp.addItem(std::make_shared<SdpConnection>(connection));
} }
sdp.addAttr(std::make_shared<SdpAttrGroup>(group)); sdp.addAttr(std::make_shared<SdpAttrGroup>(group));
@ -1124,21 +1119,21 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
sdp_media.addAttr(std::make_shared<SdpCommon>("rtcp-rsize")); sdp_media.addAttr(std::make_shared<SdpCommon>("rtcp-rsize"));
} }
if(m.type != TrackApplication) { if (m.type != TrackApplication) {
for (auto &p : m.plan) { for (auto &p : m.plan) {
auto rtp_map = std::make_shared<SdpAttrRtpMap>(); auto rtp_map = std::make_shared<SdpAttrRtpMap>();
rtp_map->pt = p.pt; rtp_map->pt = p.pt;
rtp_map->codec = p.codec; rtp_map->codec = p.codec;
rtp_map->sample_rate = p.sample_rate; rtp_map->sample_rate = p.sample_rate;
rtp_map->channel = p.channel; rtp_map->channel = p.channel;
//添加a=rtpmap // 添加a=rtpmap
sdp_media.addAttr(std::move(rtp_map)); sdp_media.addAttr(std::move(rtp_map));
for (auto &fb : p.rtcp_fb) { for (auto &fb : p.rtcp_fb) {
auto rtcp_fb = std::make_shared<SdpAttrRtcpFb>(); auto rtcp_fb = std::make_shared<SdpAttrRtcpFb>();
rtcp_fb->pt = p.pt; rtcp_fb->pt = p.pt;
rtcp_fb->rtcp_type = fb; rtcp_fb->rtcp_type = fb;
//添加a=rtcp-fb // 添加a=rtcp-fb
sdp_media.addAttr(std::move(rtcp_fb)); sdp_media.addAttr(std::move(rtcp_fb));
} }
@ -1146,13 +1141,13 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
auto fmtp = std::make_shared<SdpAttrFmtp>(); auto fmtp = std::make_shared<SdpAttrFmtp>();
fmtp->pt = p.pt; fmtp->pt = p.pt;
fmtp->fmtp = p.fmtp; fmtp->fmtp = p.fmtp;
//添加a=fmtp // 添加a=fmtp
sdp_media.addAttr(std::move(fmtp)); sdp_media.addAttr(std::move(fmtp));
} }
} }
{ {
//添加a=msid字段 // 添加a=msid字段
if (!m.rtp_rtx_ssrc.empty()) { if (!m.rtp_rtx_ssrc.empty()) {
if (!m.rtp_rtx_ssrc[0].msid.empty()) { if (!m.rtp_rtx_ssrc[0].msid.empty()) {
auto msid = std::make_shared<SdpAttrMsid>(); auto msid = std::make_shared<SdpAttrMsid>();
@ -1164,14 +1159,14 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
{ {
for (auto &ssrc : m.rtp_rtx_ssrc) { for (auto &ssrc : m.rtp_rtx_ssrc) {
//添加a=ssrc字段 // 添加a=ssrc字段
CHECK(!ssrc.empty()); CHECK(!ssrc.empty());
addSdpAttrSSRC(ssrc, sdp_media, ssrc.ssrc); addSdpAttrSSRC(ssrc, sdp_media, ssrc.ssrc);
if (ssrc.rtx_ssrc) { if (ssrc.rtx_ssrc) {
addSdpAttrSSRC(ssrc, sdp_media, ssrc.rtx_ssrc); addSdpAttrSSRC(ssrc, sdp_media, ssrc.rtx_ssrc);
//生成a=ssrc-group:FID字段 // 生成a=ssrc-group:FID字段
//有rtx ssrc // 有rtx ssrc
auto group = std::make_shared<SdpAttrSSRCGroup>(); auto group = std::make_shared<SdpAttrSSRCGroup>();
group->type = "FID"; group->type = "FID";
group->ssrcs.emplace_back(ssrc.ssrc); group->ssrcs.emplace_back(ssrc.ssrc);
@ -1183,12 +1178,12 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
{ {
if (m.rtp_ssrc_sim.size() >= 2) { if (m.rtp_ssrc_sim.size() >= 2) {
//simulcast 要求 2~3路 // simulcast 要求 2~3路
auto group = std::make_shared<SdpAttrSSRCGroup>(); auto group = std::make_shared<SdpAttrSSRCGroup>();
for (auto &ssrc : m.rtp_ssrc_sim) { for (auto &ssrc : m.rtp_ssrc_sim) {
group->ssrcs.emplace_back(ssrc.ssrc); group->ssrcs.emplace_back(ssrc.ssrc);
} }
//添加a=ssrc-group:SIM字段 // 添加a=ssrc-group:SIM字段
group->type = "SIM"; group->type = "SIM";
sdp_media.addAttr(std::move(group)); sdp_media.addAttr(std::move(group));
} }
@ -1221,17 +1216,17 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
} }
} }
} }
if(ice_lite) if (ice_lite) {
sdp.addAttr(std::make_shared<SdpCommon>("ice-lite")); sdp.addAttr(std::make_shared<SdpCommon>("ice-lite"));
}
return ret; return ret;
} }
string RtcSession::toString() const{ string RtcSession::toString() const {
return toRtcSessionSdp()->toString(); return toRtcSessionSdp()->toString();
} }
string RtcCodecPlan::getFmtp(const char *key) const{ string RtcCodecPlan::getFmtp(const char *key) const {
for (auto &item : fmtp) { for (auto &item : fmtp) {
if (strcasecmp(item.first.data(), key) == 0) { if (strcasecmp(item.first.data(), key) == 0) {
return item.second; return item.second;
@ -1240,7 +1235,7 @@ string RtcCodecPlan::getFmtp(const char *key) const{
return ""; return "";
} }
const RtcCodecPlan *RtcMedia::getPlan(uint8_t pt) const{ const RtcCodecPlan *RtcMedia::getPlan(uint8_t pt) const {
for (auto &item : plan) { for (auto &item : plan) {
if (item.pt == pt) { if (item.pt == pt) {
return &item; return &item;
@ -1249,7 +1244,7 @@ const RtcCodecPlan *RtcMedia::getPlan(uint8_t pt) const{
return nullptr; return nullptr;
} }
const RtcCodecPlan *RtcMedia::getPlan(const char *codec) const{ const RtcCodecPlan *RtcMedia::getPlan(const char *codec) const {
for (auto &item : plan) { for (auto &item : plan) {
if (strcasecmp(item.codec.data(), codec) == 0) { if (strcasecmp(item.codec.data(), codec) == 0) {
return &item; return &item;
@ -1258,7 +1253,7 @@ const RtcCodecPlan *RtcMedia::getPlan(const char *codec) const{
return nullptr; return nullptr;
} }
const RtcCodecPlan *RtcMedia::getRelatedRtxPlan(uint8_t pt) const{ const RtcCodecPlan *RtcMedia::getRelatedRtxPlan(uint8_t pt) const {
for (auto &item : plan) { for (auto &item : plan) {
if (strcasecmp(item.codec.data(), "rtx") == 0) { if (strcasecmp(item.codec.data(), "rtx") == 0) {
auto apt = atoi(item.getFmtp("apt").data()); auto apt = atoi(item.getFmtp("apt").data());
@ -1294,7 +1289,7 @@ bool RtcMedia::supportSimulcast() const {
return false; return false;
} }
void RtcMedia::checkValid() const{ void RtcMedia::checkValid() const {
CHECK(type != TrackInvalid); CHECK(type != TrackInvalid);
CHECK(!mid.empty()); CHECK(!mid.empty());
CHECK(!proto.empty()); CHECK(!proto.empty());
@ -1304,7 +1299,7 @@ void RtcMedia::checkValid() const{
bool send_rtp = (direction == RtpDirection::sendonly || direction == RtpDirection::sendrecv); bool send_rtp = (direction == RtpDirection::sendonly || direction == RtpDirection::sendrecv);
if (!supportSimulcast()) { if (!supportSimulcast()) {
//非simulcast时检查有没有指定rtp ssrc // 非simulcast时检查有没有指定rtp ssrc
CHECK(!rtp_rtx_ssrc.empty() || !send_rtp); CHECK(!rtp_rtx_ssrc.empty() || !send_rtp);
} }
@ -1318,7 +1313,7 @@ void RtcMedia::checkValid() const{
#endif #endif
} }
void RtcSession::checkValid() const{ void RtcSession::checkValid() const {
CHECK(version == 0); CHECK(version == 0);
CHECK(!origin.empty()); CHECK(!origin.empty());
CHECK(!session_name.empty()); CHECK(!session_name.empty());
@ -1337,15 +1332,15 @@ void RtcSession::checkValid() const{
case RtpDirection::sendrecv: case RtpDirection::sendrecv:
case RtpDirection::sendonly: case RtpDirection::sendonly:
case RtpDirection::recvonly: have_active_media = true; break; case RtpDirection::recvonly: have_active_media = true; break;
default : break; default: break;
} }
} }
CHECK(have_active_media, "必须确保最少有一个活跃的track"); CHECK(have_active_media, "必须确保最少有一个活跃的track");
} }
const RtcMedia *RtcSession::getMedia(TrackType type) const{ const RtcMedia *RtcSession::getMedia(TrackType type) const {
for(auto &m : media){ for (auto &m : media) {
if(m.type == type){ if (m.type == type) {
return &m; return &m;
} }
} }
@ -1377,7 +1372,7 @@ bool RtcSession::isOnlyDatachannel() const {
string const SdpConst::kTWCCRtcpFb = "transport-cc"; string const SdpConst::kTWCCRtcpFb = "transport-cc";
string const SdpConst::kRembRtcpFb = "goog-remb"; string const SdpConst::kRembRtcpFb = "goog-remb";
void RtcConfigure::RtcTrackConfigure::enableTWCC(bool enable){ void RtcConfigure::RtcTrackConfigure::enableTWCC(bool enable) {
if (!enable) { if (!enable) {
rtcp_fb.erase(SdpConst::kTWCCRtcpFb); rtcp_fb.erase(SdpConst::kTWCCRtcpFb);
extmap.erase(RtpExtType::transport_cc); extmap.erase(RtpExtType::transport_cc);
@ -1387,7 +1382,7 @@ void RtcConfigure::RtcTrackConfigure::enableTWCC(bool enable){
} }
} }
void RtcConfigure::RtcTrackConfigure::enableREMB(bool enable){ void RtcConfigure::RtcTrackConfigure::enableREMB(bool enable) {
if (!enable) { if (!enable) {
rtcp_fb.erase(SdpConst::kRembRtcpFb); rtcp_fb.erase(SdpConst::kRembRtcpFb);
extmap.erase(RtpExtType::abs_send_time); extmap.erase(RtpExtType::abs_send_time);
@ -1397,7 +1392,7 @@ void RtcConfigure::RtcTrackConfigure::enableREMB(bool enable){
} }
} }
static vector<CodecId> toCodecArray(const string &str){ static vector<CodecId> toCodecArray(const string &str) {
vector<CodecId> ret; vector<CodecId> ret;
auto vec = split(str, ","); auto vec = split(str, ",");
for (auto &s : vec) { for (auto &s : vec) {
@ -1409,7 +1404,7 @@ static vector<CodecId> toCodecArray(const string &str){
return ret; return ret;
} }
void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type) {
rtcp_mux = true; rtcp_mux = true;
rtcp_rsize = false; rtcp_rsize = false;
group_bundle = true; group_bundle = true;
@ -1421,47 +1416,43 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){
ice_renomination = false; ice_renomination = false;
switch (type) { switch (type) {
case TrackAudio: { case TrackAudio: {
//此处调整偏好的编码格式优先级 // 此处调整偏好的编码格式优先级
GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecA, toCodecArray); GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecA, toCodecArray);
CHECK(!s_preferred_codec.empty(), "rtc音频偏好codec不能为空"); CHECK(!s_preferred_codec.empty(), "rtc音频偏好codec不能为空");
preferred_codec = s_preferred_codec; preferred_codec = s_preferred_codec;
rtcp_fb = {SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb}; rtcp_fb = { SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb };
extmap = { extmap = { { RtpExtType::ssrc_audio_level, RtpDirection::sendrecv },
{RtpExtType::ssrc_audio_level, RtpDirection::sendrecv}, { RtpExtType::csrc_audio_level, RtpDirection::sendrecv },
{RtpExtType::csrc_audio_level, RtpDirection::sendrecv}, { RtpExtType::abs_send_time, RtpDirection::sendrecv },
{RtpExtType::abs_send_time, RtpDirection::sendrecv}, { RtpExtType::transport_cc, RtpDirection::sendrecv },
{RtpExtType::transport_cc, RtpDirection::sendrecv}, // rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
//rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
//{RtpExtType::sdes_mid,RtpDirection::sendrecv}, //{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv}, { RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv },
{RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv} { RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv } };
};
break; break;
} }
case TrackVideo: { case TrackVideo: {
//此处调整偏好的编码格式优先级 // 此处调整偏好的编码格式优先级
GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecV, toCodecArray); GET_CONFIG_FUNC(vector<CodecId>, s_preferred_codec, Rtc::kPreferredCodecV, toCodecArray);
CHECK(!s_preferred_codec.empty(), "rtc视频偏好codec不能为空"); CHECK(!s_preferred_codec.empty(), "rtc视频偏好codec不能为空");
preferred_codec = s_preferred_codec; preferred_codec = s_preferred_codec;
rtcp_fb = {SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb, "nack", "ccm fir", "nack pli"}; rtcp_fb = { SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb, "nack", "ccm fir", "nack pli" };
extmap = { extmap = { { RtpExtType::abs_send_time, RtpDirection::sendrecv },
{RtpExtType::abs_send_time, RtpDirection::sendrecv}, { RtpExtType::transport_cc, RtpDirection::sendrecv },
{RtpExtType::transport_cc, RtpDirection::sendrecv}, // rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
//rtx重传rtp时忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时如果存在sdes_mid的ext,将导致无法播放
//{RtpExtType::sdes_mid,RtpDirection::sendrecv}, //{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv}, { RtpExtType::sdes_rtp_stream_id, RtpDirection::sendrecv },
{RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv}, { RtpExtType::sdes_repaired_rtp_stream_id, RtpDirection::sendrecv },
{RtpExtType::video_timing, RtpDirection::sendrecv}, { RtpExtType::video_timing, RtpDirection::sendrecv },
{RtpExtType::color_space, RtpDirection::sendrecv}, { RtpExtType::color_space, RtpDirection::sendrecv },
{RtpExtType::video_content_type, RtpDirection::sendrecv}, { RtpExtType::video_content_type, RtpDirection::sendrecv },
{RtpExtType::playout_delay, RtpDirection::sendrecv}, { RtpExtType::playout_delay, RtpDirection::sendrecv },
//手机端推webrtc 会带有旋转角度rtc协议能正常播放 其他协议拉流画面旋转 // 手机端推webrtc 会带有旋转角度rtc协议能正常播放 其他协议拉流画面旋转
//{RtpExtType::video_orientation, RtpDirection::sendrecv}, //{RtpExtType::video_orientation, RtpDirection::sendrecv},
{RtpExtType::toffset, RtpDirection::sendrecv}, { RtpExtType::toffset, RtpDirection::sendrecv },
{RtpExtType::framemarking, RtpDirection::sendrecv} { RtpExtType::framemarking, RtpDirection::sendrecv } };
};
break; break;
} }
case TrackApplication: { case TrackApplication: {
@ -1471,8 +1462,7 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){
} }
} }
void RtcConfigure::setDefaultSetting(string ice_ufrag, string ice_pwd, RtpDirection direction, void RtcConfigure::setDefaultSetting(string ice_ufrag, string ice_pwd, RtpDirection direction, const SdpAttrFingerprint &fingerprint) {
const SdpAttrFingerprint &fingerprint) {
video.setDefaultSetting(TrackVideo); video.setDefaultSetting(TrackVideo);
audio.setDefaultSetting(TrackAudio); audio.setDefaultSetting(TrackAudio);
application.setDefaultSetting(TrackApplication); application.setDefaultSetting(TrackApplication);
@ -1512,7 +1502,7 @@ void RtcConfigure::addCandidate(const SdpAttrCandidate &candidate, TrackType typ
} }
} }
void RtcConfigure::enableTWCC(bool enable, TrackType type){ void RtcConfigure::enableTWCC(bool enable, TrackType type) {
switch (type) { switch (type) {
case TrackAudio: { case TrackAudio: {
audio.enableTWCC(enable); audio.enableTWCC(enable);
@ -1530,7 +1520,7 @@ void RtcConfigure::enableTWCC(bool enable, TrackType type){
} }
} }
void RtcConfigure::enableREMB(bool enable, TrackType type){ void RtcConfigure::enableREMB(bool enable, TrackType type) {
switch (type) { switch (type) {
case TrackAudio: { case TrackAudio: {
audio.enableREMB(enable); audio.enableREMB(enable);
@ -1559,10 +1549,10 @@ shared_ptr<RtcSession> RtcConfigure::createAnswer(const RtcSession &offer) const
matchMedia(ret, m); matchMedia(ret, m);
} }
//设置音视频端口复用 // 设置音视频端口复用
if (!offer.group.mids.empty()) { if (!offer.group.mids.empty()) {
for (auto &m : ret->media) { for (auto &m : ret->media) {
//The remote end has rejected (port 0) the m-section, so it should not be putting its mid in the group attribute. // The remote end has rejected (port 0) the m-section, so it should not be putting its mid in the group attribute.
if (m.port) { if (m.port) {
ret->group.mids.emplace_back(m.mid); ret->group.mids.emplace_back(m.mid);
} }
@ -1571,32 +1561,32 @@ shared_ptr<RtcSession> RtcConfigure::createAnswer(const RtcSession &offer) const
return ret; return ret;
} }
static RtpDirection matchDirection(RtpDirection offer_direction, RtpDirection supported){ static RtpDirection matchDirection(RtpDirection offer_direction, RtpDirection supported) {
switch (offer_direction) { switch (offer_direction) {
case RtpDirection::sendonly : { case RtpDirection::sendonly: {
if (supported != RtpDirection::recvonly && supported != RtpDirection::sendrecv) { if (supported != RtpDirection::recvonly && supported != RtpDirection::sendrecv) {
//我们不支持接收 // 我们不支持接收
return RtpDirection::inactive; return RtpDirection::inactive;
} }
return RtpDirection::recvonly; return RtpDirection::recvonly;
} }
case RtpDirection::recvonly : { case RtpDirection::recvonly: {
if (supported != RtpDirection::sendonly && supported != RtpDirection::sendrecv) { if (supported != RtpDirection::sendonly && supported != RtpDirection::sendrecv) {
//我们不支持发送 // 我们不支持发送
return RtpDirection::inactive; return RtpDirection::inactive;
} }
return RtpDirection::sendonly; return RtpDirection::sendonly;
} }
//对方支持发送接收,那么最终能力根据配置来决定 // 对方支持发送接收,那么最终能力根据配置来决定
case RtpDirection::sendrecv : return (supported == RtpDirection::invalid ? RtpDirection::inactive : supported); case RtpDirection::sendrecv: return (supported == RtpDirection::invalid ? RtpDirection::inactive : supported);
case RtpDirection::inactive : return RtpDirection::inactive; case RtpDirection::inactive: return RtpDirection::inactive;
default: return RtpDirection::invalid; default: return RtpDirection::invalid;
} }
} }
static DtlsRole mathDtlsRole(DtlsRole role){ static DtlsRole mathDtlsRole(DtlsRole role) {
switch (role) { switch (role) {
case DtlsRole::actpass: case DtlsRole::actpass:
case DtlsRole::active: return DtlsRole::passive; case DtlsRole::active: return DtlsRole::passive;
@ -1605,7 +1595,7 @@ static DtlsRole mathDtlsRole(DtlsRole role){
} }
} }
void RtcConfigure::matchMedia(const std::shared_ptr<RtcSession> &ret,const RtcMedia &offer_media) const { void RtcConfigure::matchMedia(const std::shared_ptr<RtcSession> &ret, const RtcMedia &offer_media) const {
bool check_profile = true; bool check_profile = true;
bool check_codec = true; bool check_codec = true;
const RtcTrackConfigure *cfg_ptr = nullptr; const RtcTrackConfigure *cfg_ptr = nullptr;
@ -1643,20 +1633,20 @@ RETRY:
} }
const RtcCodecPlan *selected_plan = nullptr; const RtcCodecPlan *selected_plan = nullptr;
for (auto &plan : offer_media.plan) { for (auto &plan : offer_media.plan) {
//先检查编码格式是否为偏好 // 先检查编码格式是否为偏好
if (check_codec && getCodecId(plan.codec) != codec) { if (check_codec && getCodecId(plan.codec) != codec) {
continue; continue;
} }
//命中偏好的编码格式,然后检查规格 // 命中偏好的编码格式,然后检查规格
if (check_profile && !onCheckCodecProfile(plan, codec)) { if (check_profile && !onCheckCodecProfile(plan, codec)) {
continue; continue;
} }
//找到中意的codec // 找到中意的codec
selected_plan = &plan; selected_plan = &plan;
break; break;
} }
if (!selected_plan) { if (!selected_plan) {
//offer中该媒体的所有的codec都不支持 // offer中该媒体的所有的codec都不支持
continue; continue;
} }
RtcMedia answer_media; RtcMedia answer_media;
@ -1682,24 +1672,23 @@ RETRY:
answer_media.role = mathDtlsRole(offer_media.role); answer_media.role = mathDtlsRole(offer_media.role);
//如果codec匹配失败那么禁用该track // 如果codec匹配失败那么禁用该track
answer_media.direction = check_codec ? matchDirection(offer_media.direction, configure.direction) answer_media.direction = check_codec ? matchDirection(offer_media.direction, configure.direction) : RtpDirection::inactive;
: RtpDirection::inactive;
if (answer_media.direction == RtpDirection::invalid) { if (answer_media.direction == RtpDirection::invalid) {
continue; continue;
} }
if (answer_media.direction == RtpDirection::sendrecv) { if (answer_media.direction == RtpDirection::sendrecv) {
//如果是收发双向那么我们拷贝offer sdp的ssrc确保ssrc一致 // 如果是收发双向那么我们拷贝offer sdp的ssrc确保ssrc一致
answer_media.rtp_rtx_ssrc = offer_media.rtp_rtx_ssrc; answer_media.rtp_rtx_ssrc = offer_media.rtp_rtx_ssrc;
} }
//添加媒体plan // 添加媒体plan
answer_media.plan.emplace_back(*selected_plan); answer_media.plan.emplace_back(*selected_plan);
onSelectPlan(answer_media.plan.back(), codec); onSelectPlan(answer_media.plan.back(), codec);
set<uint8_t> pt_selected = {selected_plan->pt}; set<uint8_t> pt_selected = { selected_plan->pt };
//添加rtx,red,ulpfec plan // 添加rtx,red,ulpfec plan
if (configure.support_red || configure.support_rtx || configure.support_ulpfec) { if (configure.support_red || configure.support_rtx || configure.support_ulpfec) {
for (auto &plan : offer_media.plan) { for (auto &plan : offer_media.plan) {
if (!strcasecmp(plan.codec.data(), "rtx")) { if (!strcasecmp(plan.codec.data(), "rtx")) {
@ -1726,7 +1715,7 @@ RETRY:
} }
} }
//对方和我方都支持的扩展,那么我们才支持 // 对方和我方都支持的扩展,那么我们才支持
for (auto &ext : offer_media.extmap) { for (auto &ext : offer_media.extmap) {
auto it = configure.extmap.find(RtpExt::getExtType(ext.ext)); auto it = configure.extmap.find(RtpExt::getExtType(ext.ext));
if (it != configure.extmap.end()) { if (it != configure.extmap.end()) {
@ -1743,10 +1732,10 @@ RETRY:
auto &rtcp_fb_ref = answer_media.plan[0].rtcp_fb; auto &rtcp_fb_ref = answer_media.plan[0].rtcp_fb;
rtcp_fb_ref.clear(); rtcp_fb_ref.clear();
//对方和我方都支持的rtcpfb那么我们才支持 // 对方和我方都支持的rtcpfb那么我们才支持
for (auto &fp : selected_plan->rtcp_fb) { for (auto &fp : selected_plan->rtcp_fb) {
if (configure.rtcp_fb.find(fp) != configure.rtcp_fb.end()) { if (configure.rtcp_fb.find(fp) != configure.rtcp_fb.end()) {
//对方该rtcp被我们支持 // 对方该rtcp被我们支持
rtcp_fb_ref.emplace(fp); rtcp_fb_ref.emplace(fp);
} }
} }
@ -1764,19 +1753,19 @@ RETRY:
} }
if (check_profile) { if (check_profile) {
//如果是由于检查profile导致匹配失败那么重试一次且不检查profile // 如果是由于检查profile导致匹配失败那么重试一次且不检查profile
check_profile = false; check_profile = false;
goto RETRY; goto RETRY;
} }
if (check_codec) { if (check_codec) {
//如果是由于检查codec导致匹配失败那么重试一次且不检查codec // 如果是由于检查codec导致匹配失败那么重试一次且不检查codec
check_codec = false; check_codec = false;
goto RETRY; goto RETRY;
} }
} }
void RtcConfigure::setPlayRtspInfo(const string &sdp){ void RtcConfigure::setPlayRtspInfo(const string &sdp) {
RtcSession session; RtcSession session;
video.direction = RtpDirection::inactive; video.direction = RtpDirection::inactive;
audio.direction = RtpDirection::inactive; audio.direction = RtpDirection::inactive;
@ -1784,14 +1773,14 @@ void RtcConfigure::setPlayRtspInfo(const string &sdp){
session.loadFrom(sdp); session.loadFrom(sdp);
for (auto &m : session.media) { for (auto &m : session.media) {
switch (m.type) { switch (m.type) {
case TrackVideo : { case TrackVideo: {
video.direction = RtpDirection::sendonly; video.direction = RtpDirection::sendonly;
_rtsp_video_plan = std::make_shared<RtcCodecPlan>(m.plan[0]); _rtsp_video_plan = std::make_shared<RtcCodecPlan>(m.plan[0]);
video.preferred_codec.clear(); video.preferred_codec.clear();
video.preferred_codec.emplace_back(getCodecId(_rtsp_video_plan->codec)); video.preferred_codec.emplace_back(getCodecId(_rtsp_video_plan->codec));
break; break;
} }
case TrackAudio : { case TrackAudio: {
audio.direction = RtpDirection::sendonly; audio.direction = RtpDirection::sendonly;
_rtsp_audio_plan = std::make_shared<RtcCodecPlan>(m.plan[0]); _rtsp_audio_plan = std::make_shared<RtcCodecPlan>(m.plan[0]);
audio.preferred_codec.clear(); audio.preferred_codec.clear();
@ -1803,21 +1792,21 @@ void RtcConfigure::setPlayRtspInfo(const string &sdp){
} }
} }
static const string kProfile{"profile-level-id"}; static const string kProfile { "profile-level-id" };
static const string kMode{"packetization-mode"}; static const string kMode { "packetization-mode" };
bool RtcConfigure::onCheckCodecProfile(const RtcCodecPlan &plan, CodecId codec) const { bool RtcConfigure::onCheckCodecProfile(const RtcCodecPlan &plan, CodecId codec) const {
if (_rtsp_audio_plan && codec == getCodecId(_rtsp_audio_plan->codec)) { if (_rtsp_audio_plan && codec == getCodecId(_rtsp_audio_plan->codec)) {
if (plan.sample_rate != _rtsp_audio_plan->sample_rate || plan.channel != _rtsp_audio_plan->channel) { if (plan.sample_rate != _rtsp_audio_plan->sample_rate || plan.channel != _rtsp_audio_plan->channel) {
//音频采样率和通道数必须相同 // 音频采样率和通道数必须相同
return false; return false;
} }
return true; return true;
} }
if (_rtsp_video_plan && codec == CodecH264 && getCodecId(_rtsp_video_plan->codec) == CodecH264) { if (_rtsp_video_plan && codec == CodecH264 && getCodecId(_rtsp_video_plan->codec) == CodecH264) {
//h264时profile-level-id // h264时profile-level-id
if (strcasecmp(_rtsp_video_plan->fmtp[kProfile].data(), const_cast<RtcCodecPlan &>(plan).fmtp[kProfile].data())) { if (strcasecmp(_rtsp_video_plan->fmtp[kProfile].data(), const_cast<RtcCodecPlan &>(plan).fmtp[kProfile].data())) {
//profile-level-id 不匹配 // profile-level-id 不匹配
return false; return false;
} }
return true; return true;

View File

@ -22,10 +22,10 @@
namespace mediakit { namespace mediakit {
//https://datatracker.ietf.org/doc/rfc4566/?include_text=1 // https://datatracker.ietf.org/doc/rfc4566/?include_text=1
//https://blog.csdn.net/aggresss/article/details/109850434 // https://blog.csdn.net/aggresss/article/details/109850434
//https://aggresss.blog.csdn.net/article/details/106436703 // https://aggresss.blog.csdn.net/article/details/106436703
//Session description // Session description
// v= (protocol version) // v= (protocol version)
// o= (originator and session identifier) // o= (originator and session identifier)
// s= (session name) // s= (session name)
@ -57,62 +57,52 @@ namespace mediakit {
enum class RtpDirection { enum class RtpDirection {
invalid = -1, invalid = -1,
//只发送 // 只发送
sendonly, sendonly,
//只接收 // 只接收
recvonly, recvonly,
//同时发送接收 // 同时发送接收
sendrecv, sendrecv,
//禁止发送数据 // 禁止发送数据
inactive inactive
}; };
enum class DtlsRole { enum class DtlsRole {
invalid = -1, invalid = -1,
//客户端 // 客户端
active, active,
//服务端 // 服务端
passive, passive,
//既可作做客户端也可以做服务端 // 既可作做客户端也可以做服务端
actpass, actpass,
}; };
enum class SdpType { enum class SdpType { invalid = -1, offer, answer };
invalid = -1,
offer,
answer
};
DtlsRole getDtlsRole(const std::string &str); DtlsRole getDtlsRole(const std::string &str);
const char* getDtlsRoleString(DtlsRole role); const char *getDtlsRoleString(DtlsRole role);
RtpDirection getRtpDirection(const std::string &str); RtpDirection getRtpDirection(const std::string &str);
const char* getRtpDirectionString(RtpDirection val); const char *getRtpDirectionString(RtpDirection val);
class SdpItem { class SdpItem {
public: public:
using Ptr = std::shared_ptr<SdpItem>; using Ptr = std::shared_ptr<SdpItem>;
virtual ~SdpItem() = default; virtual ~SdpItem() = default;
virtual void parse(const std::string &str) { virtual void parse(const std::string &str) { value = str; }
value = str; virtual std::string toString() const { return value; }
} virtual const char *getKey() const = 0;
virtual std::string toString() const {
return value;
}
virtual const char* getKey() const = 0;
void reset() { void reset() { value.clear(); }
value.clear();
}
protected: protected:
mutable std::string value; mutable std::string value;
}; };
template <char KEY> template <char KEY>
class SdpString : public SdpItem{ class SdpString : public SdpItem {
public: public:
SdpString() = default; SdpString() = default;
SdpString(std::string val) {value = std::move(val);} SdpString(std::string val) { value = std::move(val); }
// *=* // *=*
const char* getKey() const override { static std::string key(1, KEY); return key.data();} const char* getKey() const override { static std::string key(1, KEY); return key.data();}
}; };
@ -126,34 +116,34 @@ public:
this->value = std::move(val); this->value = std::move(val);
} }
const char* getKey() const override { return key.data();} const char *getKey() const override { return key.data(); }
}; };
class SdpTime : public SdpItem{ class SdpTime : public SdpItem {
public: public:
//5.9. Timing ("t=") // 5.9. Timing ("t=")
// t=<start-time> <stop-time> // t=<start-time> <stop-time>
uint64_t start {0}; uint64_t start { 0 };
uint64_t stop {0}; uint64_t stop { 0 };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "t";} const char *getKey() const override { return "t"; }
}; };
class SdpOrigin : public SdpItem{ class SdpOrigin : public SdpItem {
public: public:
// 5.2. Origin ("o=") // 5.2. Origin ("o=")
// o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5 // o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
// o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address> // o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address>
std::string username {"-"}; std::string username { "-" };
std::string session_id; std::string session_id;
std::string session_version; std::string session_version;
std::string nettype {"IN"}; std::string nettype { "IN" };
std::string addrtype {"IP4"}; std::string addrtype { "IP4" };
std::string address {"0.0.0.0"}; std::string address { "0.0.0.0" };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "o";} const char *getKey() const override { return "o"; }
bool empty() const { bool empty() const {
return username.empty() || session_id.empty() || session_version.empty() return username.empty() || session_id.empty() || session_version.empty()
|| nettype.empty() || addrtype.empty() || address.empty(); || nettype.empty() || addrtype.empty() || address.empty();
@ -165,28 +155,28 @@ public:
// 5.7. Connection Data ("c=") // 5.7. Connection Data ("c=")
// c=IN IP4 224.2.17.12/127 // c=IN IP4 224.2.17.12/127
// c=<nettype> <addrtype> <connection-address> // c=<nettype> <addrtype> <connection-address>
std::string nettype {"IN"}; std::string nettype { "IN" };
std::string addrtype {"IP4"}; std::string addrtype { "IP4" };
std::string address {"0.0.0.0"}; std::string address { "0.0.0.0" };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "c";} const char *getKey() const override { return "c"; }
bool empty() const {return address.empty();} bool empty() const { return address.empty(); }
}; };
class SdpBandwidth : public SdpItem { class SdpBandwidth : public SdpItem {
public: public:
//5.8. Bandwidth ("b=") // 5.8. Bandwidth ("b=")
//b=<bwtype>:<bandwidth> // b=<bwtype>:<bandwidth>
//AS、CT // AS、CT
std::string bwtype {"AS"}; std::string bwtype { "AS" };
uint32_t bandwidth {0}; uint32_t bandwidth { 0 };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "b";} const char *getKey() const override { return "b"; }
bool empty() const {return bandwidth == 0;} bool empty() const { return bandwidth == 0; }
}; };
class SdpMedia : public SdpItem { class SdpMedia : public SdpItem {
@ -195,47 +185,47 @@ public:
// m=<media> <port> <proto> <fmt> ... // m=<media> <port> <proto> <fmt> ...
TrackType type; TrackType type;
uint16_t port; uint16_t port;
//RTP/AVP应用场景为视频/音频的 RTP 协议。参考 RFC 3551 // RTP/AVP应用场景为视频/音频的 RTP 协议。参考 RFC 3551
//RTP/SAVP应用场景为视频/音频的 SRTP 协议。参考 RFC 3711 // RTP/SAVP应用场景为视频/音频的 SRTP 协议。参考 RFC 3711
//RTP/AVPF: 应用场景为视频/音频的 RTP 协议,支持 RTCP-based Feedback。参考 RFC 4585 // RTP/AVPF: 应用场景为视频/音频的 RTP 协议,支持 RTCP-based Feedback。参考 RFC 4585
//RTP/SAVPF: 应用场景为视频/音频的 SRTP 协议,支持 RTCP-based Feedback。参考 RFC 5124 // RTP/SAVPF: 应用场景为视频/音频的 SRTP 协议,支持 RTCP-based Feedback。参考 RFC 5124
std::string proto; std::string proto;
std::vector<std::string> fmts; std::vector<std::string> fmts;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "m";} const char *getKey() const override { return "m"; }
}; };
class SdpAttr : public SdpItem{ class SdpAttr : public SdpItem {
public: public:
using Ptr = std::shared_ptr<SdpAttr>; using Ptr = std::shared_ptr<SdpAttr>;
//5.13. Attributes ("a=") // 5.13. Attributes ("a=")
//a=<attribute> // a=<attribute>
//a=<attribute>:<value> // a=<attribute>:<value>
SdpItem::Ptr detail; SdpItem::Ptr detail;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "a";} const char *getKey() const override { return "a"; }
}; };
class SdpAttrGroup : public SdpItem{ class SdpAttrGroup : public SdpItem {
public: public:
//a=group:BUNDLE line with all the 'mid' identifiers part of the // a=group:BUNDLE line with all the 'mid' identifiers part of the
// BUNDLE group is included at the session-level. // BUNDLE group is included at the session-level.
//a=group:LS session level attribute MUST be included wth the 'mid' // a=group:LS session level attribute MUST be included wth the 'mid'
// identifiers that are part of the same lip sync group. // identifiers that are part of the same lip sync group.
std::string type {"BUNDLE"}; std::string type { "BUNDLE" };
std::vector<std::string> mids; std::vector<std::string> mids;
void parse(const std::string &str) override ; void parse(const std::string &str) override;
std::string toString() const override ; std::string toString() const override;
const char* getKey() const override { return "group";} const char *getKey() const override { return "group"; }
}; };
class SdpAttrMsidSemantic : public SdpItem { class SdpAttrMsidSemantic : public SdpItem {
public: public:
//https://tools.ietf.org/html/draft-alvestrand-rtcweb-msid-02#section-3 // https://tools.ietf.org/html/draft-alvestrand-rtcweb-msid-02#section-3
//3. The Msid-Semantic Attribute // 3. The Msid-Semantic Attribute
// //
// In order to fully reproduce the semantics of the SDP and SSRC // In order to fully reproduce the semantics of the SDP and SSRC
// grouping frameworks, a session-level attribute is defined for // grouping frameworks, a session-level attribute is defined for
@ -253,229 +243,226 @@ public:
// The semantic field may hold values from the IANA registries // The semantic field may hold values from the IANA registries
// "Semantics for the "ssrc-group" SDP Attribute" and "Semantics for the // "Semantics for the "ssrc-group" SDP Attribute" and "Semantics for the
// "group" SDP Attribute". // "group" SDP Attribute".
//a=msid-semantic: WMS 616cfbb1-33a3-4d8c-8275-a199d6005549 // a=msid-semantic: WMS 616cfbb1-33a3-4d8c-8275-a199d6005549
std::string msid{"WMS"}; std::string msid { "WMS" };
std::string token; std::string token;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "msid-semantic";} const char *getKey() const override { return "msid-semantic"; }
bool empty() const { bool empty() const { return msid.empty(); }
return msid.empty();
}
}; };
class SdpAttrRtcp : public SdpItem { class SdpAttrRtcp : public SdpItem {
public: public:
// a=rtcp:9 IN IP4 0.0.0.0 // a=rtcp:9 IN IP4 0.0.0.0
uint16_t port{0}; uint16_t port { 0 };
std::string nettype {"IN"}; std::string nettype { "IN" };
std::string addrtype {"IP4"}; std::string addrtype { "IP4" };
std::string address {"0.0.0.0"}; std::string address { "0.0.0.0" };
void parse(const std::string &str) override;; void parse(const std::string &str) override;
;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "rtcp";} const char *getKey() const override { return "rtcp"; }
bool empty() const { bool empty() const { return address.empty() || !port; }
return address.empty() || !port;
}
}; };
class SdpAttrIceUfrag : public SdpItem { class SdpAttrIceUfrag : public SdpItem {
public: public:
SdpAttrIceUfrag() = default; SdpAttrIceUfrag() = default;
SdpAttrIceUfrag(std::string str) {value = std::move(str);} SdpAttrIceUfrag(std::string str) { value = std::move(str); }
//a=ice-ufrag:sXJ3 // a=ice-ufrag:sXJ3
const char* getKey() const override { return "ice-ufrag";} const char *getKey() const override { return "ice-ufrag"; }
}; };
class SdpAttrIcePwd : public SdpItem { class SdpAttrIcePwd : public SdpItem {
public: public:
SdpAttrIcePwd() = default; SdpAttrIcePwd() = default;
SdpAttrIcePwd(std::string str) {value = std::move(str);} SdpAttrIcePwd(std::string str) { value = std::move(str); }
//a=ice-pwd:yEclOTrLg1gEubBFefOqtmyV // a=ice-pwd:yEclOTrLg1gEubBFefOqtmyV
const char* getKey() const override { return "ice-pwd";} const char *getKey() const override { return "ice-pwd"; }
}; };
class SdpAttrIceOption : public SdpItem { class SdpAttrIceOption : public SdpItem {
public: public:
//a=ice-options:trickle // a=ice-options:trickle
bool trickle{false}; bool trickle { false };
bool renomination{false}; bool renomination { false };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "ice-options";} const char *getKey() const override { return "ice-options"; }
}; };
class SdpAttrFingerprint : public SdpItem { class SdpAttrFingerprint : public SdpItem {
public: public:
//a=fingerprint:sha-256 22:14:B5:AF:66:12:C7:C7:8D:EF:4B:DE:40:25:ED:5D:8F:17:54:DD:88:33:C0:13:2E:FD:1A:FA:7E:7A:1B:79 // a=fingerprint:sha-256 22:14:B5:AF:66:12:C7:C7:8D:EF:4B:DE:40:25:ED:5D:8F:17:54:DD:88:33:C0:13:2E:FD:1A:FA:7E:7A:1B:79
std::string algorithm; std::string algorithm;
std::string hash; std::string hash;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "fingerprint";} const char *getKey() const override { return "fingerprint"; }
bool empty() const { return algorithm.empty() || hash.empty(); } bool empty() const { return algorithm.empty() || hash.empty(); }
}; };
class SdpAttrSetup : public SdpItem { class SdpAttrSetup : public SdpItem {
public: public:
//a=setup:actpass // a=setup:actpass
SdpAttrSetup() = default; SdpAttrSetup() = default;
SdpAttrSetup(DtlsRole r) { role = r; } SdpAttrSetup(DtlsRole r) { role = r; }
DtlsRole role{DtlsRole::actpass}; DtlsRole role { DtlsRole::actpass };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "setup";} const char *getKey() const override { return "setup"; }
}; };
class SdpAttrMid : public SdpItem { class SdpAttrMid : public SdpItem {
public: public:
SdpAttrMid() = default; SdpAttrMid() = default;
SdpAttrMid(std::string val) { value = std::move(val); } SdpAttrMid(std::string val) { value = std::move(val); }
//a=mid:audio // a=mid:audio
const char* getKey() const override { return "mid";} const char *getKey() const override { return "mid"; }
}; };
class SdpAttrExtmap : public SdpItem { class SdpAttrExtmap : public SdpItem {
public: public:
//https://aggresss.blog.csdn.net/article/details/106436703 // https://aggresss.blog.csdn.net/article/details/106436703
//a=extmap:1[/sendonly] urn:ietf:params:rtp-hdrext:ssrc-audio-level // a=extmap:1[/sendonly] urn:ietf:params:rtp-hdrext:ssrc-audio-level
uint8_t id; uint8_t id;
RtpDirection direction{RtpDirection::invalid}; RtpDirection direction { RtpDirection::invalid };
std::string ext; std::string ext;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "extmap";} const char *getKey() const override { return "extmap"; }
}; };
class SdpAttrRtpMap : public SdpItem { class SdpAttrRtpMap : public SdpItem {
public: public:
//a=rtpmap:111 opus/48000/2 // a=rtpmap:111 opus/48000/2
uint8_t pt; uint8_t pt;
std::string codec; std::string codec;
uint32_t sample_rate; uint32_t sample_rate;
uint32_t channel {0}; uint32_t channel { 0 };
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "rtpmap";} const char *getKey() const override { return "rtpmap"; }
}; };
class SdpAttrRtcpFb : public SdpItem { class SdpAttrRtcpFb : public SdpItem {
public: public:
//a=rtcp-fb:98 nack pli // a=rtcp-fb:98 nack pli
//a=rtcp-fb:120 nack 支持 nack 重传nack (Negative-Acknowledgment) 。 // a=rtcp-fb:120 nack 支持 nack 重传nack (Negative-Acknowledgment) 。
//a=rtcp-fb:120 nack pli 支持 nack 关键帧重传PLI (Picture Loss Indication) 。 // a=rtcp-fb:120 nack pli 支持 nack 关键帧重传PLI (Picture Loss Indication) 。
//a=rtcp-fb:120 ccm fir 支持编码层关键帧请求CCM (Codec Control Message)FIR (Full Intra Request ),通常与 nack pli 有同样的效果,但是 nack pli 是用于重传时的关键帧请求。 // a=rtcp-fb:120 ccm fir 支持编码层关键帧请求CCM (Codec Control Message)FIR (Full Intra Request ),通常与 nack pli 有同样的效果,但是 nack pli
//a=rtcp-fb:120 goog-remb 支持 REMB (Receiver Estimated Maximum Bitrate) 。 // 是用于重传时的关键帧请求。 a=rtcp-fb:120 goog-remb 支持 REMB (Receiver Estimated Maximum Bitrate) 。 a=rtcp-fb:120 transport-cc 支持 TCC (Transport
//a=rtcp-fb:120 transport-cc 支持 TCC (Transport Congest Control) 。 // Congest Control) 。
uint8_t pt; uint8_t pt;
std::string rtcp_type; std::string rtcp_type;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "rtcp-fb";} const char *getKey() const override { return "rtcp-fb"; }
}; };
class SdpAttrFmtp : public SdpItem { class SdpAttrFmtp : public SdpItem {
public: public:
//fmtp:96 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f // fmtp:96 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f
uint8_t pt; uint8_t pt;
std::map<std::string/*key*/, std::string/*value*/, StrCaseCompare> fmtp; std::map<std::string /*key*/, std::string /*value*/, StrCaseCompare> fmtp;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "fmtp";} const char *getKey() const override { return "fmtp"; }
}; };
class SdpAttrSSRC : public SdpItem { class SdpAttrSSRC : public SdpItem {
public: public:
//a=ssrc:3245185839 cname:Cx4i/VTR51etgjT7 // a=ssrc:3245185839 cname:Cx4i/VTR51etgjT7
//a=ssrc:3245185839 msid:cb373bff-0fea-4edb-bc39-e49bb8e8e3b9 0cf7e597-36a2-4480-9796-69bf0955eef5 // a=ssrc:3245185839 msid:cb373bff-0fea-4edb-bc39-e49bb8e8e3b9 0cf7e597-36a2-4480-9796-69bf0955eef5
//a=ssrc:3245185839 mslabel:cb373bff-0fea-4edb-bc39-e49bb8e8e3b9 // a=ssrc:3245185839 mslabel:cb373bff-0fea-4edb-bc39-e49bb8e8e3b9
//a=ssrc:3245185839 label:0cf7e597-36a2-4480-9796-69bf0955eef5 // a=ssrc:3245185839 label:0cf7e597-36a2-4480-9796-69bf0955eef5
//a=ssrc:<ssrc-id> <attribute> // a=ssrc:<ssrc-id> <attribute>
//a=ssrc:<ssrc-id> <attribute>:<value> // a=ssrc:<ssrc-id> <attribute>:<value>
//cname 是必须的msid/mslabel/label 这三个属性都是 WebRTC 自创的,或者说 Google 自创的,可以参考 https://tools.ietf.org/html/draft-ietf-mmusic-msid-17 // cname 是必须的msid/mslabel/label 这三个属性都是 WebRTC 自创的,或者说 Google 自创的,可以参考 https://tools.ietf.org/html/draft-ietf-mmusic-msid-17
// 理解它们三者的关系需要先了解三个概念RTP stream / MediaStreamTrack / MediaStream // 理解它们三者的关系需要先了解三个概念RTP stream / MediaStreamTrack / MediaStream
//一个 a=ssrc 代表一个 RTP stream // 一个 a=ssrc 代表一个 RTP stream
//一个 MediaStreamTrack 通常包含一个或多个 RTP stream例如一个视频 MediaStreamTrack 中通常包含两个 RTP stream一个用于常规传输一个用于 nack 重传; // 一个 MediaStreamTrack 通常包含一个或多个 RTP stream例如一个视频 MediaStreamTrack 中通常包含两个 RTP stream一个用于常规传输一个用于 nack 重传;
//一个 MediaStream 通常包含一个或多个 MediaStreamTrack ,例如 simulcast 场景下,一个 MediaStream 通常会包含三个不同编码质量的 MediaStreamTrack // 一个 MediaStream 通常包含一个或多个 MediaStreamTrack ,例如 simulcast 场景下,一个 MediaStream 通常会包含三个不同编码质量的 MediaStreamTrack
//这种标记方式并不被 Firefox 认可,在 Firefox 生成的 SDP 中一个 a=ssrc 通常只有一行,例如: // 这种标记方式并不被 Firefox 认可,在 Firefox 生成的 SDP 中一个 a=ssrc 通常只有一行,例如:
//a=ssrc:3245185839 cname:Cx4i/VTR51etgjT7 // a=ssrc:3245185839 cname:Cx4i/VTR51etgjT7
uint32_t ssrc; uint32_t ssrc;
std::string attribute; std::string attribute;
std::string attribute_value; std::string attribute_value;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "ssrc";} const char *getKey() const override { return "ssrc"; }
}; };
class SdpAttrSSRCGroup : public SdpItem { class SdpAttrSSRCGroup : public SdpItem {
public: public:
//a=ssrc-group 定义参考 RFC 5576(https://tools.ietf.org/html/rfc5576) ,用于描述多个 ssrc 之间的关联,常见的有两种: // a=ssrc-group 定义参考 RFC 5576(https://tools.ietf.org/html/rfc5576) ,用于描述多个 ssrc 之间的关联,常见的有两种:
//a=ssrc-group:FID 2430709021 3715850271 // a=ssrc-group:FID 2430709021 3715850271
// FID (Flow Identification) 最初用在 FEC 的关联中WebRTC 中通常用于关联一组常规 RTP stream 和 重传 RTP stream 。 // FID (Flow Identification) 最初用在 FEC 的关联中WebRTC 中通常用于关联一组常规 RTP stream 和 重传 RTP stream 。
//a=ssrc-group:SIM 360918977 360918978 360918980 // a=ssrc-group:SIM 360918977 360918978 360918980
// 在 Chrome 独有的 SDP munging 风格的 simulcast 中使用,将三组编码质量由低到高的 MediaStreamTrack 关联在一起。 // 在 Chrome 独有的 SDP munging 风格的 simulcast 中使用,将三组编码质量由低到高的 MediaStreamTrack 关联在一起。
std::string type{"FID"}; std::string type { "FID" };
std::vector<uint32_t> ssrcs; std::vector<uint32_t> ssrcs;
bool isFID() const { return type == "FID"; } bool isFID() const { return type == "FID"; }
bool isSIM() const { return type == "SIM"; } bool isSIM() const { return type == "SIM"; }
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "ssrc-group";} const char *getKey() const override { return "ssrc-group"; }
}; };
class SdpAttrSctpMap : public SdpItem { class SdpAttrSctpMap : public SdpItem {
public: public:
//https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-05 // https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-05
//a=sctpmap:5000 webrtc-datachannel 1024 // a=sctpmap:5000 webrtc-datachannel 1024
//a=sctpmap: sctpmap-number media-subtypes [streams] // a=sctpmap: sctpmap-number media-subtypes [streams]
uint16_t port = 0; uint16_t port = 0;
std::string subtypes; std::string subtypes;
uint32_t streams = 0; uint32_t streams = 0;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "sctpmap";} const char *getKey() const override { return "sctpmap"; }
bool empty() const { return port == 0 && subtypes.empty() && streams == 0; } bool empty() const { return port == 0 && subtypes.empty() && streams == 0; }
}; };
class SdpAttrCandidate : public SdpItem { class SdpAttrCandidate : public SdpItem {
public: public:
using Ptr = std::shared_ptr<SdpAttrCandidate>; using Ptr = std::shared_ptr<SdpAttrCandidate>;
//https://tools.ietf.org/html/rfc5245 // https://tools.ietf.org/html/rfc5245
//15.1. "candidate" Attribute // 15.1. "candidate" Attribute
//a=candidate:4 1 udp 2 192.168.1.7 58107 typ host // a=candidate:4 1 udp 2 192.168.1.7 58107 typ host
//a=candidate:<foundation> <component-id> <transport> <priority> <address> <port> typ <cand-type> // a=candidate:<foundation> <component-id> <transport> <priority> <address> <port> typ <cand-type>
std::string foundation; std::string foundation;
//传输媒体的类型,1代表RTP;2代表 RTCP。 // 传输媒体的类型,1代表RTP;2代表 RTCP。
uint32_t component; uint32_t component;
std::string transport {"udp"}; std::string transport { "udp" };
uint32_t priority; uint32_t priority;
std::string address; std::string address;
uint16_t port; uint16_t port;
std::string type; std::string type;
std::vector<std::pair<std::string, std::string> > arr; std::vector<std::pair<std::string, std::string>> arr;
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "candidate";} const char *getKey() const override { return "candidate"; }
}; };
class SdpAttrMsid : public SdpItem{ class SdpAttrMsid : public SdpItem {
public: public:
const char* getKey() const override { return "msid";} const char *getKey() const override { return "msid"; }
}; };
class SdpAttrExtmapAllowMixed : public SdpItem{ class SdpAttrExtmapAllowMixed : public SdpItem {
public: public:
const char* getKey() const override { return "extmap-allow-mixed";} const char *getKey() const override { return "extmap-allow-mixed"; }
}; };
class SdpAttrSimulcast : public SdpItem{ class SdpAttrSimulcast : public SdpItem {
public: public:
//https://www.meetecho.com/blog/simulcast-janus-ssrc/ // https://www.meetecho.com/blog/simulcast-janus-ssrc/
//https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-14 // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-14
const char* getKey() const override { return "simulcast";} const char *getKey() const override { return "simulcast"; }
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
bool empty() const { return rids.empty(); } bool empty() const { return rids.empty(); }
@ -483,11 +470,11 @@ public:
std::vector<std::string> rids; std::vector<std::string> rids;
}; };
class SdpAttrRid : public SdpItem{ class SdpAttrRid : public SdpItem {
public: public:
void parse(const std::string &str) override; void parse(const std::string &str) override;
std::string toString() const override; std::string toString() const override;
const char* getKey() const override { return "rid";} const char *getKey() const override { return "rid"; }
std::string direction; std::string direction;
std::string rid; std::string rid;
}; };
@ -507,8 +494,8 @@ public:
RtpDirection getDirection() const; RtpDirection getDirection() const;
template<typename cls> template <typename cls>
cls getItemClass(char key, const char *attr_key = nullptr) const{ cls getItemClass(char key, const char *attr_key = nullptr) const {
auto item = std::dynamic_pointer_cast<cls>(getItem(key, attr_key)); auto item = std::dynamic_pointer_cast<cls>(getItem(key, attr_key));
if (!item) { if (!item) {
return cls(); return cls();
@ -516,7 +503,7 @@ public:
return *item; return *item;
} }
std::string getStringItem(char key, const char *attr_key = nullptr) const{ std::string getStringItem(char key, const char *attr_key = nullptr) const {
auto item = getItem(key, attr_key); auto item = getItem(key, attr_key);
if (!item) { if (!item) {
return ""; return "";
@ -526,7 +513,7 @@ public:
SdpItem::Ptr getItem(char key, const char *attr_key = nullptr) const; SdpItem::Ptr getItem(char key, const char *attr_key = nullptr) const;
template<typename cls> template <typename cls>
std::vector<cls> getAllItem(char key_c, const char *attr_key = nullptr) const { std::vector<cls> getAllItem(char key_c, const char *attr_key = nullptr) const {
std::vector<cls> ret; std::vector<cls> ret;
std::string key(1, key_c); std::string key(1, key_c);
@ -555,7 +542,7 @@ private:
std::vector<SdpItem::Ptr> items; std::vector<SdpItem::Ptr> items;
}; };
class RtcSessionSdp : public RtcSdpBase{ class RtcSessionSdp : public RtcSdpBase {
public: public:
using Ptr = std::shared_ptr<RtcSessionSdp>; using Ptr = std::shared_ptr<RtcSessionSdp>;
int getVersion() const; int getVersion() const;
@ -580,45 +567,45 @@ public:
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
//ssrc相关信息 // ssrc相关信息
class RtcSSRC{ class RtcSSRC {
public: public:
uint32_t ssrc {0}; uint32_t ssrc { 0 };
uint32_t rtx_ssrc {0}; uint32_t rtx_ssrc { 0 };
std::string cname; std::string cname;
std::string msid; std::string msid;
std::string mslabel; std::string mslabel;
std::string label; std::string label;
bool empty() const {return ssrc == 0 && cname.empty();} bool empty() const { return ssrc == 0 && cname.empty(); }
}; };
//rtc传输编码方案 // rtc传输编码方案
class RtcCodecPlan{ class RtcCodecPlan {
public: public:
using Ptr = std::shared_ptr<RtcCodecPlan>; using Ptr = std::shared_ptr<RtcCodecPlan>;
uint8_t pt; uint8_t pt;
std::string codec; std::string codec;
uint32_t sample_rate; uint32_t sample_rate;
//音频时有效 // 音频时有效
uint32_t channel = 0; uint32_t channel = 0;
//rtcp反馈 // rtcp反馈
std::set<std::string> rtcp_fb; std::set<std::string> rtcp_fb;
std::map<std::string/*key*/, std::string/*value*/, StrCaseCompare> fmtp; std::map<std::string /*key*/, std::string /*value*/, StrCaseCompare> fmtp;
std::string getFmtp(const char *key) const; std::string getFmtp(const char *key) const;
}; };
//rtc 媒体描述 // rtc 媒体描述
class RtcMedia{ class RtcMedia {
public: public:
TrackType type{TrackType::TrackInvalid}; TrackType type { TrackType::TrackInvalid };
std::string mid; std::string mid;
uint16_t port{0}; uint16_t port { 0 };
SdpConnection addr; SdpConnection addr;
SdpBandwidth bandwidth; SdpBandwidth bandwidth;
std::string proto; std::string proto;
RtpDirection direction{RtpDirection::invalid}; RtpDirection direction { RtpDirection::invalid };
std::vector<RtcCodecPlan> plan; std::vector<RtcCodecPlan> plan;
//////// rtp //////// //////// rtp ////////
@ -629,20 +616,20 @@ public:
std::vector<std::string> rtp_rids; std::vector<std::string> rtp_rids;
//////// rtcp //////// //////// rtcp ////////
bool rtcp_mux{false}; bool rtcp_mux { false };
bool rtcp_rsize{false}; bool rtcp_rsize { false };
SdpAttrRtcp rtcp_addr; SdpAttrRtcp rtcp_addr;
//////// ice //////// //////// ice ////////
bool ice_trickle{false}; bool ice_trickle { false };
bool ice_lite{false}; bool ice_lite { false };
bool ice_renomination{false}; bool ice_renomination { false };
std::string ice_ufrag; std::string ice_ufrag;
std::string ice_pwd; std::string ice_pwd;
std::vector<SdpAttrCandidate> candidate; std::vector<SdpAttrCandidate> candidate;
//////// dtls //////// //////// dtls ////////
DtlsRole role{DtlsRole::invalid}; DtlsRole role { DtlsRole::invalid };
SdpAttrFingerprint fingerprint; SdpAttrFingerprint fingerprint;
//////// extmap //////// //////// extmap ////////
@ -650,7 +637,7 @@ public:
//////// sctp //////////// //////// sctp ////////////
SdpAttrSctpMap sctpmap; SdpAttrSctpMap sctpmap;
uint32_t sctp_port{0}; uint32_t sctp_port { 0 };
void checkValid() const; void checkValid() const;
const RtcCodecPlan *getPlan(uint8_t pt) const; const RtcCodecPlan *getPlan(uint8_t pt) const;
@ -705,7 +692,7 @@ public:
std::string ice_ufrag; std::string ice_ufrag;
std::string ice_pwd; std::string ice_pwd;
RtpDirection direction{RtpDirection::invalid}; RtpDirection direction { RtpDirection::invalid };
SdpAttrFingerprint fingerprint; SdpAttrFingerprint fingerprint;
std::set<std::string> rtcp_fb; std::set<std::string> rtcp_fb;
@ -752,6 +739,6 @@ private:
~SdpConst() = delete; ~SdpConst() = delete;
}; };
}// namespace mediakit } // namespace mediakit
#endif //ZLMEDIAKIT_SDP_H #endif // ZLMEDIAKIT_SDP_H