sdp代码重构, 增加addItem和addAttr

This commit is contained in:
cqm 2022-05-18 09:56:53 +08:00
parent 9d9315d8c6
commit 2b29e58f71
2 changed files with 80 additions and 78 deletions

View File

@ -191,8 +191,8 @@ RtpDirection RtcSdpBase::getDirection() const{
}
SdpItem::Ptr RtcSdpBase::getItem(char key_c, const char *attr_key) const {
std::string key(1, key_c);
for (auto item : items) {
string key(1, key_c);
if (strcasecmp(item->getKey(), key.data()) == 0) {
if (!attr_key) {
return item;
@ -206,55 +206,56 @@ SdpItem::Ptr RtcSdpBase::getItem(char key_c, const char *attr_key) const {
return SdpItem::Ptr();
}
int RtcSdpBase::getVersion() const {
//////////////////////////////////////////////////////////////////////////
int RtcSessionSdp::getVersion() const {
return atoi(getStringItem('v').data());
}
SdpOrigin RtcSdpBase::getOrigin() const {
SdpOrigin RtcSessionSdp::getOrigin() const {
return getItemClass<SdpOrigin>('o');
}
string RtcSdpBase::getSessionName() const {
string RtcSessionSdp::getSessionName() const {
return getStringItem('s');
}
string RtcSdpBase::getSessionInfo() const {
string RtcSessionSdp::getSessionInfo() const {
return getStringItem('i');
}
SdpTime RtcSdpBase::getSessionTime() const{
SdpTime RtcSessionSdp::getSessionTime() const{
return getItemClass<SdpTime>('t');
}
SdpConnection RtcSdpBase::getConnection() const {
SdpConnection RtcSessionSdp::getConnection() const {
return getItemClass<SdpConnection>('c');
}
SdpBandwidth RtcSdpBase::getBandwidth() const {
SdpBandwidth RtcSessionSdp::getBandwidth() const {
return getItemClass<SdpBandwidth>('b');
}
string RtcSdpBase::getUri() const {
string RtcSessionSdp::getUri() const {
return getStringItem('u');
}
string RtcSdpBase::getEmail() const {
string RtcSessionSdp::getEmail() const {
return getStringItem('e');
}
string RtcSdpBase::getPhone() const {
string RtcSessionSdp::getPhone() const {
return getStringItem('p');
}
string RtcSdpBase::getTimeZone() const {
string RtcSessionSdp::getTimeZone() const {
return getStringItem('z');
}
string RtcSdpBase::getEncryptKey() const {
string RtcSessionSdp::getEncryptKey() const {
return getStringItem('k');
}
string RtcSdpBase::getRepeatTimes() const {
string RtcSessionSdp::getRepeatTimes() const {
return getStringItem('r');
}
@ -952,12 +953,6 @@ void RtcSession::loadFrom(const string &str) {
group = sdp.getItemClass<SdpAttrGroup>('a', "group");
}
std::shared_ptr<SdpItem> wrapSdpAttr(SdpItem::Ptr item){
auto ret = std::make_shared<SdpAttr>();
ret->detail = std::move(item);
return ret;
}
static void toRtsp(vector <SdpItem::Ptr> &items) {
for (auto it = items.begin(); it != items.end();) {
switch ((*it)->getKey()[0]) {
@ -1019,57 +1014,57 @@ string RtcSession::toRtspSdp() const{
int i = 0;
for (auto &m : sdp->medias) {
toRtsp(m.items);
m.items.push_back(wrapSdpAttr(std::make_shared<SdpCommon>("control", string("trackID=") + to_string(i++))));
m.addAttr(std::make_shared<SdpCommon>("control", string("trackID=") + to_string(i++)));
}
return sdp->toString();
}
void addSdpAttrSSRC(const RtcSSRC &rtp_ssrc, vector<SdpItem::Ptr> &items, uint32_t ssrc_num) {
void addSdpAttrSSRC(const RtcSSRC &rtp_ssrc, RtcSdpBase &media, uint32_t ssrc_num) {
assert(ssrc_num);
SdpAttrSSRC ssrc;
ssrc.ssrc = ssrc_num;
ssrc.attribute = "cname";
ssrc.attribute_value = rtp_ssrc.cname;
items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSSRC>(ssrc)));
media.addAttr(std::make_shared<SdpAttrSSRC>(ssrc));
if (!rtp_ssrc.msid.empty()) {
ssrc.attribute = "msid";
ssrc.attribute_value = rtp_ssrc.msid;
items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSSRC>(ssrc)));
media.addAttr(std::make_shared<SdpAttrSSRC>(ssrc));
}
if (!rtp_ssrc.mslabel.empty()) {
ssrc.attribute = "mslabel";
ssrc.attribute_value = rtp_ssrc.mslabel;
items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSSRC>(ssrc)));
media.addAttr(std::make_shared<SdpAttrSSRC>(ssrc));
}
if (!rtp_ssrc.label.empty()) {
ssrc.attribute = "label";
ssrc.attribute_value = rtp_ssrc.label;
items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSSRC>(ssrc)));
media.addAttr(std::make_shared<SdpAttrSSRC>(ssrc));
}
}
RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
RtcSessionSdp::Ptr ret = std::make_shared<RtcSessionSdp>();
auto &sdp = *ret;
sdp.items.emplace_back(std::make_shared<SdpString<'v'> >(to_string(version)));
sdp.items.emplace_back(std::make_shared<SdpOrigin>(origin));
sdp.items.emplace_back(std::make_shared<SdpString<'s'> >(session_name));
sdp.addItem(std::make_shared<SdpString<'v'> >(to_string(version)));
sdp.addItem(std::make_shared<SdpOrigin>(origin));
sdp.addItem(std::make_shared<SdpString<'s'> >(session_name));
if (!session_info.empty()) {
sdp.items.emplace_back(std::make_shared<SdpString<'i'> >(session_info));
sdp.addItem(std::make_shared<SdpString<'i'> >(session_info));
}
sdp.items.emplace_back(std::make_shared<SdpTime>(time));
sdp.addItem(std::make_shared<SdpTime>(time));
if(connection.empty()){
sdp.items.emplace_back(std::make_shared<SdpConnection>(connection));
sdp.addItem(std::make_shared<SdpConnection>(connection));
}
if (!bandwidth.empty()) {
sdp.items.emplace_back(std::make_shared<SdpBandwidth>(bandwidth));
sdp.addItem(std::make_shared<SdpBandwidth>(bandwidth));
}
sdp.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrGroup>(group)));
sdp.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrMsidSemantic>(msid_semantic)));
sdp.addAttr(std::make_shared<SdpAttrGroup>(group));
sdp.addAttr(std::make_shared<SdpAttrMsidSemantic>(msid_semantic));
for (auto &m : media) {
sdp.medias.emplace_back();
auto &sdp_media = sdp.medias.back();
@ -1083,37 +1078,37 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
if (m.type == TrackApplication) {
mline->fmts.emplace_back("webrtc-datachannel");
}
sdp_media.items.emplace_back(std::move(mline));
sdp_media.items.emplace_back(std::make_shared<SdpConnection>(m.addr));
sdp_media.addItem(std::move(mline));
sdp_media.addItem(std::make_shared<SdpConnection>(m.addr));
if (!m.rtcp_addr.empty()) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrRtcp>(m.rtcp_addr)));
sdp_media.addAttr(std::make_shared<SdpAttrRtcp>(m.rtcp_addr));
}
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrIceUfrag>(m.ice_ufrag)));
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrIcePwd>(m.ice_pwd)));
sdp_media.addAttr(std::make_shared<SdpAttrIceUfrag>(m.ice_ufrag));
sdp_media.addAttr(std::make_shared<SdpAttrIcePwd>(m.ice_pwd));
if (m.ice_trickle || m.ice_renomination) {
auto attr = std::make_shared<SdpAttrIceOption>();
attr->trickle = m.ice_trickle;
attr->renomination = m.ice_renomination;
sdp_media.items.emplace_back(wrapSdpAttr(attr));
sdp_media.addAttr(attr);
}
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrFingerprint>(m.fingerprint)));
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSetup>(m.role)));
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrMid>(m.mid)));
sdp_media.addAttr(std::make_shared<SdpAttrFingerprint>(m.fingerprint));
sdp_media.addAttr(std::make_shared<SdpAttrSetup>(m.role));
sdp_media.addAttr(std::make_shared<SdpAttrMid>(m.mid));
if (m.ice_lite) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpCommon>("ice-lite")));
sdp_media.addAttr(std::make_shared<SdpCommon>("ice-lite"));
}
for (auto &ext : m.extmap) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrExtmap>(ext)));
sdp_media.addAttr(std::make_shared<SdpAttrExtmap>(ext));
}
if (m.direction != RtpDirection::invalid) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<DirectionInterfaceImp>(m.direction)));
sdp_media.addAttr(std::make_shared<DirectionInterfaceImp>(m.direction));
}
if (m.rtcp_mux) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpCommon>("rtcp-mux")));
sdp_media.addAttr(std::make_shared<SdpCommon>("rtcp-mux"));
}
if (m.rtcp_rsize) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpCommon>("rtcp-rsize")));
sdp_media.addAttr(std::make_shared<SdpCommon>("rtcp-rsize"));
}
if(m.type != TrackApplication) {
@ -1124,14 +1119,14 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
rtp_map->sample_rate = p.sample_rate;
rtp_map->channel = p.channel;
//添加a=rtpmap
sdp_media.items.emplace_back(wrapSdpAttr(std::move(rtp_map)));
sdp_media.addAttr(std::move(rtp_map));
for (auto &fb : p.rtcp_fb) {
auto rtcp_fb = std::make_shared<SdpAttrRtcpFb>();
rtcp_fb->pt = p.pt;
rtcp_fb->rtcp_type = fb;
//添加a=rtcp-fb
sdp_media.items.emplace_back(wrapSdpAttr(std::move(rtcp_fb)));
sdp_media.addAttr(std::move(rtcp_fb));
}
if (!p.fmtp.empty()) {
@ -1139,7 +1134,7 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
fmtp->pt = p.pt;
fmtp->fmtp = p.fmtp;
//添加a=fmtp
sdp_media.items.emplace_back(wrapSdpAttr(std::move(fmtp)));
sdp_media.addAttr(std::move(fmtp));
}
}
@ -1149,7 +1144,7 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
if (!m.rtp_rtx_ssrc[0].msid.empty()) {
auto msid = std::make_shared<SdpAttrMsid>();
msid->parse(m.rtp_rtx_ssrc[0].msid);
sdp_media.items.emplace_back(wrapSdpAttr(std::move(msid)));
sdp_media.addAttr(std::move(msid));
}
}
}
@ -1158,9 +1153,9 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
for (auto &ssrc : m.rtp_rtx_ssrc) {
//添加a=ssrc字段
CHECK(!ssrc.empty());
addSdpAttrSSRC(ssrc, sdp_media.items, ssrc.ssrc);
addSdpAttrSSRC(ssrc, sdp_media, ssrc.ssrc);
if (ssrc.rtx_ssrc) {
addSdpAttrSSRC(ssrc, sdp_media.items, ssrc.rtx_ssrc);
addSdpAttrSSRC(ssrc, sdp_media, ssrc.rtx_ssrc);
//生成a=ssrc-group:FID字段
//有rtx ssrc
@ -1168,7 +1163,7 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
group->type = "FID";
group->ssrcs.emplace_back(ssrc.ssrc);
group->ssrcs.emplace_back(ssrc.rtx_ssrc);
sdp_media.items.emplace_back(wrapSdpAttr(std::move(group)));
sdp_media.addAttr(std::move(group));
}
}
}
@ -1182,33 +1177,33 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{
}
//添加a=ssrc-group:SIM字段
group->type = "SIM";
sdp_media.items.emplace_back(wrapSdpAttr(std::move(group)));
sdp_media.addAttr(std::move(group));
}
if (m.rtp_rids.size() >= 2) {
auto simulcast = std::make_shared<SdpAttrSimulcast>();
simulcast->direction = "recv";
simulcast->rids = m.rtp_rids;
sdp_media.items.emplace_back(wrapSdpAttr(std::move(simulcast)));
sdp_media.addAttr(std::move(simulcast));
for (auto &rid : m.rtp_rids) {
auto attr_rid = std::make_shared<SdpAttrRid>();
attr_rid->rid = rid;
attr_rid->direction = "recv";
sdp_media.items.emplace_back(wrapSdpAttr(std::move(attr_rid)));
sdp_media.addAttr(std::move(attr_rid));
}
}
}
} else {
if (!m.sctpmap.empty()) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrSctpMap>(m.sctpmap)));
sdp_media.addAttr(std::make_shared<SdpAttrSctpMap>(m.sctpmap));
}
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpCommon>("sctp-port", to_string(m.sctp_port))));
sdp_media.addAttr(std::make_shared<SdpCommon>("sctp-port", to_string(m.sctp_port)));
}
for (auto &cand : m.candidate) {
sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared<SdpAttrCandidate>(cand)));
sdp_media.addAttr(std::make_shared<SdpAttrCandidate>(cand));
}
}
return ret;

View File

@ -491,25 +491,18 @@ public:
class RtcSdpBase {
public:
std::vector<SdpItem::Ptr> items;
void addItem(SdpItem::Ptr item) { items.push_back(item); }
void addAttr(SdpItem::Ptr attr) {
auto item = std::make_shared<SdpAttr>();
item->detail = std::move(attr);
items.push_back(item);
}
SdpItem::Ptr findItem(char key) const { return getItem(key);}
SdpItem::Ptr findAttr(const char* key) const { return getItem('a', key);}
public:
virtual ~RtcSdpBase() = default;
virtual std::string toString() const;
int getVersion() const;
SdpOrigin getOrigin() const;
std::string getSessionName() const;
std::string getSessionInfo() const;
SdpTime getSessionTime() const;
SdpConnection getConnection() const;
SdpBandwidth getBandwidth() const;
std::string getUri() const;
std::string getEmail() const;
std::string getPhone() const;
std::string getTimeZone() const;
std::string getEncryptKey() const;
std::string getRepeatTimes() const;
RtpDirection getDirection() const;
template<typename cls>
@ -534,8 +527,8 @@ public:
template<typename cls>
std::vector<cls> getAllItem(char key_c, const char *attr_key = nullptr) const {
std::vector<cls> ret;
for (auto item : items) {
std::string key(1, key_c);
for (auto item : items) {
if (strcasecmp(item->getKey(), key.data()) == 0) {
if (!attr_key) {
auto c = std::dynamic_pointer_cast<cls>(item);
@ -560,6 +553,20 @@ public:
class RtcSessionSdp : public RtcSdpBase{
public:
using Ptr = std::shared_ptr<RtcSessionSdp>;
int getVersion() const;
SdpOrigin getOrigin() const;
std::string getSessionName() const;
std::string getSessionInfo() const;
SdpTime getSessionTime() const;
SdpConnection getConnection() const;
SdpBandwidth getBandwidth() const;
std::string getUri() const;
std::string getEmail() const;
std::string getPhone() const;
std::string getTimeZone() const;
std::string getEncryptKey() const;
std::string getRepeatTimes() const;
std::vector<RtcSdpBase> medias;
void parse(const std::string &str);