diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index dae5d776..d4a1d8f3 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -161,9 +161,99 @@ const char* getRtpDirectionString(RtpDirection val){ ////////////////////////////////////////////////////////////////////////////////////////// +string RtcSdpBase::toString() const { + _StrPrinter printer; + for (auto &item : items) { + printer << item->getKey() << "=" << item->toString() << "\r\n"; + } + return std::move(printer); +} + +RtpDirection RtcSdpBase::getDirection() const{ + for (auto &item : items) { + auto attr = dynamic_pointer_cast(item); + if (attr) { + auto dir = dynamic_pointer_cast(attr->detail); + if (dir) { + return dir->getDirection(); + } + } + } + return RtpDirection::invalid; +} + +SdpItem::Ptr RtcSdpBase::getItem(char key, const char *attr_key) const { + for (auto item : items) { + if (item->getKey()[0] == key) { + if (!attr_key) { + return item; + } + auto attr = dynamic_pointer_cast(item); + if (attr && attr->detail->getKey() == attr_key) { + return item; + } + } + } + return SdpItem::Ptr(); +} + +int RtcSdpBase::getVersion() const { + return atoi(getStringItem('v').data()); +} + +SdpOrigin RtcSdpBase::getOrigin() const { + return getItemClass('o'); +} + +string RtcSdpBase::getSessionName() const { + return getStringItem('s'); +} + +string RtcSdpBase::getSessionInfo() const { + return getStringItem('i'); +} + +SdpTime RtcSdpBase::getSessionTime() const{ + return getItemClass('t'); +} + +SdpConnection RtcSdpBase::getConnection() const { + return getItemClass('c'); +} + +SdpBandwidth RtcSdpBase::getBandwidth() const { + return getItemClass('b'); +} + +string RtcSdpBase::getUri() const { + return getStringItem('u'); +} + +string RtcSdpBase::getEmail() const { + return getStringItem('e'); +} + +string RtcSdpBase::getPhone() const { + return getStringItem('p'); +} + +string RtcSdpBase::getTimeZone() const { + return getStringItem('z'); +} + +string RtcSdpBase::getEncryptKey() const { + return getStringItem('k'); +} + +string RtcSdpBase::getRepeatTimes() const { + return getStringItem('r'); +} + +////////////////////////////////////////////////////////////////////// + void RtcSessionSdp::parse(const string &str) { static auto flag = registerAllItem(); - RtcMediaSdp *media = nullptr; + RtcSdpBase *media = nullptr; auto lines = split(str, "\n"); for(auto &line : lines){ trim(line); @@ -173,7 +263,7 @@ void RtcSessionSdp::parse(const string &str) { auto key = line.substr(0, 1); auto value = line.substr(2); if (key == "m") { - medias.emplace_back(RtcMediaSdp()); + medias.emplace_back(RtcSdpBase()); media = &medias.back(); } @@ -195,9 +285,7 @@ void RtcSessionSdp::parse(const string &str) { string RtcSessionSdp::toString() const { _StrPrinter printer; - for (auto &item : items) { - printer << item->getKey() << "=" << item->toString() << "\r\n"; - } + printer << RtcSdpBase::toString(); for (auto &media : medias) { printer << media.toString(); } @@ -205,30 +293,6 @@ string RtcSessionSdp::toString() const { return std::move(printer); } -////////////////////////////////////////////////////////////////////// - -string RtcMediaSdp::toString() const { - _StrPrinter printer; - for (auto &item : items) { - printer << item->getKey() << "=" << item->toString() << "\r\n"; - } - return std::move(printer); -} - -RtpDirection RtcMediaSdp::getDirection() const{ - for (auto &item : items) { - auto attr = dynamic_pointer_cast(item); - if (attr) { - auto dir = dynamic_pointer_cast(attr->detail); - if (dir) { - return dir->getDirection(); - } - } - } - return RtpDirection::invalid; -} - - ////////////////////////////////////////////////////////////////////////////////////////// #define SDP_THROW() throw std::invalid_argument(StrPrinter << "解析sdp " << getKey() << " 字段失败:" << str) diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index 325c11c0..381f51c3 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -421,22 +421,55 @@ public: const char* getKey() const override { return "candidate";} }; -class RtcMediaSdp { +class RtcSdpBase { public: vector items; - string toString() const; - bool haveAttr(const char *attr) const; - string getAttrValue(const char *attr) const; + +public: + virtual string toString() const; + + int getVersion() const; + SdpOrigin getOrigin() const; + string getSessionName() const; + string getSessionInfo() const; + SdpTime getSessionTime() const; + SdpConnection getConnection() const; + SdpBandwidth getBandwidth() const; + + string getUri() const; + string getEmail() const; + string getPhone() const; + string getTimeZone() const; + string getEncryptKey() const; + string getRepeatTimes() const; RtpDirection getDirection() const; + +private: + SdpItem::Ptr getItem(char key, const char *attr_key = nullptr) const; + + template + cls getItemClass(char key, const char *attr_key = nullptr) const{ + auto item = dynamic_pointer_cast(getItem(key, attr_key)); + if (!item) { + return cls(); + } + return *item; + } + + string getStringItem(char key, const char *attr_key = nullptr) const{ + auto item = getItem(key, attr_key); + if (!item) { + return ""; + } + return item->toString(); + } }; -class RtcSessionSdp { +class RtcSessionSdp : public RtcSdpBase{ public: - vector items; - vector medias; - + vector medias; void parse(const string &str); - string toString() const; + string toString() const override; }; ////////////////////////////////////////////////////////////////// @@ -468,12 +501,12 @@ public: uint32_t sample_rate; //音频时有效 uint32_t channel = 0; - vector > fmtp; vector rtcp_fb; + vector > fmtp; }; //rtc 媒体描述 -class RtcCodec{ +class RtcMedia{ public: TrackType type; string mid; @@ -508,6 +541,17 @@ public: vector extmap; }; +class RtcSession{ +public: + int version; + SdpOrigin origin; + string session_name; + string session_info; + SdpConnection connection; + SdpBandwidth bandwidth; + set group_bundle; + vector media; +}; #endif //ZLMEDIAKIT_SDP_H