ZLMediaKit/webrtc/Sdp.h

424 lines
12 KiB
C++
Raw Normal View History

2021-03-27 18:33:20 +08:00
//
// Created by xzl on 2021/3/27.
//
#ifndef ZLMEDIAKIT_SDP_H
#define ZLMEDIAKIT_SDP_H
#include <string>
2021-03-27 22:23:38 +08:00
#include <vector>
#include "Extension/Frame.h"
2021-03-27 18:33:20 +08:00
using namespace std;
2021-03-27 22:23:38 +08:00
using namespace mediakit;
2021-03-27 18:33:20 +08:00
2021-03-27 22:23:38 +08:00
//https://datatracker.ietf.org/doc/rfc4566/?include_text=1
2021-03-27 18:33:20 +08:00
//Session description
// v= (protocol version)
// o= (originator and session identifier)
// s= (session name)
// i=* (session information)
// u=* (URI of description)
// e=* (email address)
// p=* (phone number)
// c=* (connection information -- not required if included in
// all media)
// b=* (zero or more bandwidth information lines)
// One or more time descriptions ("t=" and "r=" lines; see below)
// z=* (time zone adjustments)
// k=* (encryption key)
// a=* (zero or more session attribute lines)
// Zero or more media descriptions
//
// Time description
// t= (time the session is active)
// r=* (zero or more repeat times)
//
// Media description, if present
// m= (media name and transport address)
// i=* (media title)
// c=* (connection information -- optional if included at
// session level)
// b=* (zero or more bandwidth information lines)
// k=* (encryption key)
// a=* (zero or more media attribute lines)
2021-03-27 22:23:38 +08:00
enum class RtpDirection {
invalid = -1,
//只发送
sendonly,
//只接收
2021-03-28 17:32:53 +08:00
recvonly,
2021-03-27 22:23:38 +08:00
//同时发送接收
sendrecv,
//禁止发送数据
inactive
};
enum class DtlsRole {
invalid = -1,
//客户端
active,
//服务端
passive,
//既可作做客户端也可以做服务端
actpass,
};
enum class SdpType {
invalid = -1,
offer,
answer
};
2021-03-28 17:32:53 +08:00
TrackType getTrackType(const string &str);
const char* getTrackString(TrackType type);
DtlsRole getDtlsRole(const string &str);
const char* getDtlsRoleString(DtlsRole role);
RtpDirection getRtpDirection(const string &str);
const char* getRtpDirectionString(RtpDirection val);
2021-03-27 18:33:20 +08:00
class SdpItem {
public:
2021-03-28 09:49:34 +08:00
using Ptr = std::shared_ptr<SdpItem>;
2021-03-27 18:33:20 +08:00
virtual ~SdpItem() = default;
2021-03-28 09:49:34 +08:00
virtual void parse(const string &str) {
value = str;
}
virtual string toString() const {
return value;
}
2021-03-28 23:31:21 +08:00
virtual const char* getKey() const = 0;
2021-03-28 17:32:53 +08:00
protected:
mutable string value;
2021-03-27 18:33:20 +08:00
};
2021-03-28 09:49:34 +08:00
template <char KEY>
class SdpString : public SdpItem{
public:
// *=*
2021-03-28 23:31:21 +08:00
const char* getKey() const override { static string key(1, KEY); return key.data();}
2021-03-28 09:49:34 +08:00
};
class SdpCommon : public SdpItem {
public:
string key;
SdpCommon(string key) { this->key = std::move(key); }
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return key.data();}
2021-03-28 09:49:34 +08:00
};
2021-03-27 18:33:20 +08:00
class SdpTime : public SdpItem{
public:
2021-03-27 22:23:38 +08:00
//5.9. Timing ("t=")
// t=<start-time> <stop-time>
2021-03-28 17:32:53 +08:00
uint64_t start {0};
uint64_t stop {0};
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "t";}
2021-03-27 18:33:20 +08:00
};
class SdpOrigin : public SdpItem{
public:
2021-03-27 22:23:38 +08:00
// 5.2. Origin ("o=")
2021-03-27 18:33:20 +08:00
// o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
// o=<username> <sess-id> <sess-version> <nettype> <addrtype> <unicast-address>
2021-03-28 17:32:53 +08:00
string username {"-"};
2021-03-27 18:33:20 +08:00
string session_id;
string session_version;
2021-03-28 17:32:53 +08:00
string nettype {"IN"};
string addrtype {"IP4"};
string address {"0.0.0.0"};
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "o";}
2021-03-27 18:33:20 +08:00
};
class SdpConnection : public SdpItem {
public:
2021-03-27 22:23:38 +08:00
// 5.7. Connection Data ("c=")
2021-03-27 18:33:20 +08:00
// c=IN IP4 224.2.17.12/127
// c=<nettype> <addrtype> <connection-address>
2021-03-28 17:32:53 +08:00
string nettype {"IN"};
string addrtype {"IP4"};
string address {"0.0.0.0"};
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "c";}
2021-03-27 22:23:38 +08:00
};
class SdpBandwidth : public SdpItem {
public:
//5.8. Bandwidth ("b=")
//b=<bwtype>:<bandwidth>
//AS、CT
2021-03-28 17:32:53 +08:00
string bwtype {"AS"};
uint32_t bandwidth {0};
2021-03-27 22:23:38 +08:00
2021-03-28 17:32:53 +08:00
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "b";}
2021-03-27 22:23:38 +08:00
};
class SdpMedia : public SdpItem {
public:
// 5.14. Media Descriptions ("m=")
// m=<media> <port> <proto> <fmt> ...
TrackType type;
uint16_t port;
2021-03-28 17:32:53 +08:00
string proto;
vector<uint32_t> fmts;
2021-03-27 22:23:38 +08:00
2021-03-28 17:32:53 +08:00
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "m";}
2021-03-27 22:23:38 +08:00
};
class SdpAttr : public SdpItem{
public:
2021-03-28 09:49:34 +08:00
using Ptr = std::shared_ptr<SdpAttr>;
2021-03-27 22:23:38 +08:00
//5.13. Attributes ("a=")
//a=<attribute>
//a=<attribute>:<value>
2021-03-28 09:49:34 +08:00
SdpItem::Ptr detail;
2021-03-27 22:23:38 +08:00
void parse(const string &str) override;
2021-03-28 17:32:53 +08:00
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "a";}
2021-03-27 22:23:38 +08:00
};
class SdpAttrGroup : public SdpItem{
public:
//a=group:BUNDLE line with all the 'mid' identifiers part of the
// BUNDLE group is included at the session-level.
//a=group:LS session level attribute MUST be included wth the 'mid'
// identifiers that are part of the same lip sync group.
2021-03-28 17:32:53 +08:00
string type {"BUNDLE"};
vector<string> mids;
void parse(const string &str) override ;
string toString() const override ;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "group";}
2021-03-27 18:33:20 +08:00
};
2021-03-28 09:49:34 +08:00
class SdpAttrMsidSemantic : public SdpItem {
2021-03-27 18:33:20 +08:00
public:
2021-03-28 17:32:53 +08:00
//https://tools.ietf.org/html/draft-alvestrand-rtcweb-msid-02#section-3
//3. The Msid-Semantic Attribute
//
// In order to fully reproduce the semantics of the SDP and SSRC
// grouping frameworks, a session-level attribute is defined for
// signalling the semantics associated with an msid grouping.
//
// This OPTIONAL attribute gives the message ID and its group semantic.
// a=msid-semantic: examplefoo LS
//
//
// The ABNF of msid-semantic is:
//
// msid-semantic-attr = "msid-semantic:" " " msid token
// token = <as defined in RFC 4566>
//
// The semantic field may hold values from the IANA registries
// "Semantics for the "ssrc-group" SDP Attribute" and "Semantics for the
// "group" SDP Attribute".
//a=msid-semantic: WMS 616cfbb1-33a3-4d8c-8275-a199d6005549
string msid{"WMS"};
string token;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "msid-semantic";}
2021-03-28 09:49:34 +08:00
};
2021-03-27 18:33:20 +08:00
2021-03-28 09:49:34 +08:00
class SdpAttrRtcp : public SdpItem {
public:
2021-03-28 17:32:53 +08:00
// a=rtcp:9 IN IP4 0.0.0.0
2021-03-28 09:49:34 +08:00
uint16_t port;
2021-03-28 17:32:53 +08:00
string nettype {"IN"};
string addrtype {"IP4"};
string address {"0.0.0.0"};
void parse(const string &str) override;;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "rtcp";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrIceUfrag : public SdpItem {
public:
//a=ice-ufrag:sXJ3
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "ice-ufrag";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrIcePwd : public SdpItem {
public:
//a=ice-pwd:yEclOTrLg1gEubBFefOqtmyV
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "ice-pwd";}
};
class SdpAttrIceOption : public SdpItem {
public:
//a=ice-options:trickle
const char* getKey() const override { return "ice-options";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrFingerprint : public SdpItem {
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
string algorithm;
2021-03-28 17:32:53 +08:00
string hash;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "fingerprint";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrSetup : public SdpItem {
public:
//a=setup:actpass
2021-03-28 17:32:53 +08:00
DtlsRole role{DtlsRole::actpass};
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "setup";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrMid : public SdpItem {
public:
//a=mid:audio
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "mid";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrExtmap : public SdpItem {
public:
//a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
int index;
2021-03-28 17:32:53 +08:00
string ext;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "extmap";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrRtpMap : public SdpItem {
public:
//a=rtpmap:111 opus/48000/2
uint8_t pt;
string codec;
int sample_rate;
2021-03-28 17:32:53 +08:00
int channel {0};
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "rtpmap";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrRtcpFb : public SdpItem {
public:
2021-03-28 17:32:53 +08:00
//a=rtcp-fb:98 nack pli
2021-03-28 09:49:34 +08:00
uint8_t pt;
2021-03-28 17:32:53 +08:00
vector<string> arr;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "rtcp-fb";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrFmtp : public SdpItem {
public:
2021-03-28 17:32:53 +08:00
//fmtp:96 level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42e01f
2021-03-28 09:49:34 +08:00
uint8_t pt;
2021-03-28 17:32:53 +08:00
vector<std::pair<string, string> > arr;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "fmtp";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrSSRC : public SdpItem {
public:
//a=ssrc:120276603 cname:iSkJ2vn5cYYubTve
2021-03-28 17:32:53 +08:00
//a=ssrc:<ssrc-id> <attribute>
//a=ssrc:<ssrc-id> <attribute>:<value>
2021-03-28 09:49:34 +08:00
uint32_t ssrc;
2021-03-28 17:32:53 +08:00
string attribute;
string attribute_value;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "ssrc";}
};
class SdpAttrSSRCGroup : public SdpItem {
public:
//a=ssrc-group 定义参考 RFC 5576 ,用于描述多个 ssrc 之间的关联,常见的有两种:
//a=ssrc-group:FID 2430709021 3715850271
// FID (Flow Identification) 最初用在 FEC 的关联中WebRTC 中通常用于关联一组常规 RTP stream 和 重传 RTP stream 。
//a=ssrc-group:SIM 360918977 360918978 360918980
// 在 Chrome 独有的 SDP munging 风格的 simulcast 中使用,将三组编码质量由低到高的 MediaStreamTrack 关联在一起。
string type{"FID"};
union {
struct {
uint32_t rtp_ssrc;
uint32_t rtx_ssrc;
} fid;
struct {
uint32_t rtp_ssrc_low;
uint32_t rtp_ssrc_mid;
uint32_t rtp_ssrc_high;
} sim;
} u;
void parse(const string &str) override;
string toString() const override;
const char* getKey() const override { return "ssrc-group";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrSctpMap : public SdpItem {
public:
2021-03-28 17:32:53 +08:00
//https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-05
//a=sctpmap:5000 webrtc-datachannel 1024
//a=sctpmap: sctpmap-number media-subtypes [streams]
2021-03-28 09:49:34 +08:00
uint16_t port;
2021-03-28 17:32:53 +08:00
string subtypes;
int streams;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "sctpmap";}
2021-03-28 09:49:34 +08:00
};
class SdpAttrCandidate : public SdpItem {
public:
2021-03-28 17:32:53 +08:00
//https://tools.ietf.org/html/rfc5245
//15.1. "candidate" Attribute
//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>
uint32_t foundation;
uint32_t component;
string transport {"udp"};
uint32_t priority;
string address;
uint16_t port;
string type;
vector<std::pair<string, string> > arr;
void parse(const string &str) override;
string toString() const override;
2021-03-28 23:31:21 +08:00
const char* getKey() const override { return "candidate";}
2021-03-28 09:49:34 +08:00
};
class RtcMedia {
public:
vector<SdpItem::Ptr> items;
2021-03-28 17:32:53 +08:00
string toString() const;
2021-03-27 22:23:38 +08:00
bool haveAttr(const char *attr) const;
string getAttrValue(const char *attr) const;
2021-03-28 17:32:53 +08:00
RtpDirection getDirection() const;
2021-03-27 18:33:20 +08:00
};
2021-03-27 22:23:38 +08:00
2021-03-27 18:33:20 +08:00
class RtcSdp {
public:
2021-03-28 09:49:34 +08:00
vector<SdpItem::Ptr> items;
vector<RtcMedia> medias;
void parse(const string &str);
string toString() const;
2021-03-27 18:33:20 +08:00
};
#endif //ZLMEDIAKIT_SDP_H