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

File diff suppressed because it is too large Load Diff

View File

@ -22,97 +22,87 @@
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)
// i=* (session information) // i=* (session information)
// u=* (URI of description) // u=* (URI of description)
// e=* (email address) // e=* (email address)
// p=* (phone number) // p=* (phone number)
// c=* (connection information -- not required if included in // c=* (connection information -- not required if included in
// all media) // all media)
// b=* (zero or more bandwidth information lines) // b=* (zero or more bandwidth information lines)
// One or more time descriptions ("t=" and "r=" lines; see below) // One or more time descriptions ("t=" and "r=" lines; see below)
// z=* (time zone adjustments) // z=* (time zone adjustments)
// k=* (encryption key) // k=* (encryption key)
// a=* (zero or more session attribute lines) // a=* (zero or more session attribute lines)
// Zero or more media descriptions // Zero or more media descriptions
// //
// Time description // Time description
// t= (time the session is active) // t= (time the session is active)
// r=* (zero or more repeat times) // r=* (zero or more repeat times)
// //
// Media description, if present // Media description, if present
// m= (media name and transport address) // m= (media name and transport address)
// i=* (media title) // i=* (media title)
// c=* (connection information -- optional if included at // c=* (connection information -- optional if included at
// session level) // session level)
// b=* (zero or more bandwidth information lines) // b=* (zero or more bandwidth information lines)
// k=* (encryption key) // k=* (encryption key)
// a=* (zero or more media attribute lines) // a=* (zero or more media attribute lines)
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,287 +185,284 @@ 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
// signalling the semantics associated with an msid grouping. // signalling the semantics associated with an msid grouping.
// //
// This OPTIONAL attribute gives the message ID and its group semantic. // This OPTIONAL attribute gives the message ID and its group semantic.
// a=msid-semantic: examplefoo LS // a=msid-semantic: examplefoo LS
// //
// //
// The ABNF of msid-semantic is: // The ABNF of msid-semantic is:
// //
// msid-semantic-attr = "msid-semantic:" " " msid token // msid-semantic-attr = "msid-semantic:" " " msid token
// token = <as defined in RFC 4566> // token = <as defined in RFC 4566>
// //
// 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;
@ -679,7 +666,7 @@ public:
void checkValid() const; void checkValid() const;
std::string toString() const; std::string toString() const;
std::string toRtspSdp() const; std::string toRtspSdp() const;
const RtcMedia *getMedia(TrackType type) const; const RtcMedia *getMedia(TrackType type) const;
bool supportRtcpFb(const std::string &name, TrackType type = TrackType::TrackVideo) const; bool supportRtcpFb(const std::string &name, TrackType type = TrackType::TrackVideo) const;
bool supportSimulcast() const; bool supportSimulcast() const;
bool isOnlyDatachannel() const; bool isOnlyDatachannel() 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