diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index 505f4dcc..9e80d54a 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -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('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('t'); } -SdpConnection RtcSdpBase::getConnection() const { +SdpConnection RtcSessionSdp::getConnection() const { return getItemClass('c'); } -SdpBandwidth RtcSdpBase::getBandwidth() const { +SdpBandwidth RtcSessionSdp::getBandwidth() const { return getItemClass('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('a', "group"); } -std::shared_ptr wrapSdpAttr(SdpItem::Ptr item){ - auto ret = std::make_shared(); - ret->detail = std::move(item); - return ret; -} - static void toRtsp(vector &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("control", string("trackID=") + to_string(i++)))); + m.addAttr(std::make_shared("control", string("trackID=") + to_string(i++))); } return sdp->toString(); } -void addSdpAttrSSRC(const RtcSSRC &rtp_ssrc, vector &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(ssrc))); + media.addAttr(std::make_shared(ssrc)); if (!rtp_ssrc.msid.empty()) { ssrc.attribute = "msid"; ssrc.attribute_value = rtp_ssrc.msid; - items.emplace_back(wrapSdpAttr(std::make_shared(ssrc))); + media.addAttr(std::make_shared(ssrc)); } if (!rtp_ssrc.mslabel.empty()) { ssrc.attribute = "mslabel"; ssrc.attribute_value = rtp_ssrc.mslabel; - items.emplace_back(wrapSdpAttr(std::make_shared(ssrc))); + media.addAttr(std::make_shared(ssrc)); } if (!rtp_ssrc.label.empty()) { ssrc.attribute = "label"; ssrc.attribute_value = rtp_ssrc.label; - items.emplace_back(wrapSdpAttr(std::make_shared(ssrc))); + media.addAttr(std::make_shared(ssrc)); } } RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{ RtcSessionSdp::Ptr ret = std::make_shared(); auto &sdp = *ret; - sdp.items.emplace_back(std::make_shared >(to_string(version))); - sdp.items.emplace_back(std::make_shared(origin)); - sdp.items.emplace_back(std::make_shared >(session_name)); + sdp.addItem(std::make_shared >(to_string(version))); + sdp.addItem(std::make_shared(origin)); + sdp.addItem(std::make_shared >(session_name)); if (!session_info.empty()) { - sdp.items.emplace_back(std::make_shared >(session_info)); + sdp.addItem(std::make_shared >(session_info)); } - sdp.items.emplace_back(std::make_shared(time)); + sdp.addItem(std::make_shared(time)); if(connection.empty()){ - sdp.items.emplace_back(std::make_shared(connection)); + sdp.addItem(std::make_shared(connection)); } if (!bandwidth.empty()) { - sdp.items.emplace_back(std::make_shared(bandwidth)); + sdp.addItem(std::make_shared(bandwidth)); } - sdp.items.emplace_back(wrapSdpAttr(std::make_shared(group))); - sdp.items.emplace_back(wrapSdpAttr(std::make_shared(msid_semantic))); + sdp.addAttr(std::make_shared(group)); + sdp.addAttr(std::make_shared(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(m.addr)); + sdp_media.addItem(std::move(mline)); + sdp_media.addItem(std::make_shared(m.addr)); if (!m.rtcp_addr.empty()) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.rtcp_addr))); + sdp_media.addAttr(std::make_shared(m.rtcp_addr)); } - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.ice_ufrag))); - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.ice_pwd))); + sdp_media.addAttr(std::make_shared(m.ice_ufrag)); + sdp_media.addAttr(std::make_shared(m.ice_pwd)); if (m.ice_trickle || m.ice_renomination) { auto attr = std::make_shared(); 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(m.fingerprint))); - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.role))); - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.mid))); + sdp_media.addAttr(std::make_shared(m.fingerprint)); + sdp_media.addAttr(std::make_shared(m.role)); + sdp_media.addAttr(std::make_shared(m.mid)); if (m.ice_lite) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared("ice-lite"))); + sdp_media.addAttr(std::make_shared("ice-lite")); } for (auto &ext : m.extmap) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(ext))); + sdp_media.addAttr(std::make_shared(ext)); } if (m.direction != RtpDirection::invalid) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(m.direction))); + sdp_media.addAttr(std::make_shared(m.direction)); } if (m.rtcp_mux) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared("rtcp-mux"))); + sdp_media.addAttr(std::make_shared("rtcp-mux")); } if (m.rtcp_rsize) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared("rtcp-rsize"))); + sdp_media.addAttr(std::make_shared("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(); 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(); 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(); 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(); 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(m.sctpmap))); + sdp_media.addAttr(std::make_shared(m.sctpmap)); } - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared("sctp-port", to_string(m.sctp_port)))); + sdp_media.addAttr(std::make_shared("sctp-port", to_string(m.sctp_port))); } for (auto &cand : m.candidate) { - sdp_media.items.emplace_back(wrapSdpAttr(std::make_shared(cand))); + sdp_media.addAttr(std::make_shared(cand)); } } return ret; diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index 942c5800..d49a72aa 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -491,25 +491,18 @@ public: class RtcSdpBase { public: std::vector items; - + void addItem(SdpItem::Ptr item) { items.push_back(item); } + void addAttr(SdpItem::Ptr attr) { + auto item = std::make_shared(); + 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 @@ -534,8 +527,8 @@ public: template std::vector getAllItem(char key_c, const char *attr_key = nullptr) const { std::vector ret; + std::string key(1, key_c); for (auto item : items) { - std::string key(1, key_c); if (strcasecmp(item->getKey(), key.data()) == 0) { if (!attr_key) { auto c = std::dynamic_pointer_cast(item); @@ -560,7 +553,21 @@ public: class RtcSessionSdp : public RtcSdpBase{ public: using Ptr = std::shared_ptr; + 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 medias; void parse(const std::string &str); std::string toString() const override;