添加辅助方法

This commit is contained in:
ziyue 2021-03-29 11:39:58 +08:00
parent 2643269581
commit b714dfddda
2 changed files with 148 additions and 40 deletions

View File

@ -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<SdpAttr>(item);
if (attr) {
auto dir = dynamic_pointer_cast<DirectionInterface>(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<SdpAttr>(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<SdpOrigin>('o');
}
string RtcSdpBase::getSessionName() const {
return getStringItem('s');
}
string RtcSdpBase::getSessionInfo() const {
return getStringItem('i');
}
SdpTime RtcSdpBase::getSessionTime() const{
return getItemClass<SdpTime>('t');
}
SdpConnection RtcSdpBase::getConnection() const {
return getItemClass<SdpConnection>('c');
}
SdpBandwidth RtcSdpBase::getBandwidth() const {
return getItemClass<SdpBandwidth>('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<SdpAttr>(item);
if (attr) {
auto dir = dynamic_pointer_cast<DirectionInterface>(attr->detail);
if (dir) {
return dir->getDirection();
}
}
}
return RtpDirection::invalid;
}
//////////////////////////////////////////////////////////////////////////////////////////
#define SDP_THROW() throw std::invalid_argument(StrPrinter << "解析sdp " << getKey() << " 字段失败:" << str)

View File

@ -421,22 +421,55 @@ public:
const char* getKey() const override { return "candidate";}
};
class RtcMediaSdp {
class RtcSdpBase {
public:
vector<SdpItem::Ptr> 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<typename cls>
cls getItemClass(char key, const char *attr_key = nullptr) const{
auto item = dynamic_pointer_cast<cls>(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<SdpItem::Ptr> items;
vector<RtcMediaSdp> medias;
vector<RtcSdpBase> 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<std::pair<string/*key*/, string/*value*/> > fmtp;
vector<string> rtcp_fb;
vector<std::pair<string/*key*/, string/*value*/> > fmtp;
};
//rtc 媒体描述
class RtcCodec{
class RtcMedia{
public:
TrackType type;
string mid;
@ -508,6 +541,17 @@ public:
vector<SdpAttrExtmap> extmap;
};
class RtcSession{
public:
int version;
SdpOrigin origin;
string session_name;
string session_info;
SdpConnection connection;
SdpBandwidth bandwidth;
set<TrackType> group_bundle;
vector<RtcMedia> media;
};
#endif //ZLMEDIAKIT_SDP_H