2
0

description.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Staz Modrzynski
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #ifndef RTC_DESCRIPTION_H
  10. #define RTC_DESCRIPTION_H
  11. #include "candidate.hpp"
  12. #include "common.hpp"
  13. #include <iostream>
  14. #include <map>
  15. #include <vector>
  16. namespace rtc {
  17. const string DEFAULT_OPUS_AUDIO_PROFILE =
  18. "minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
  19. // Use Constrained Baseline profile Level 3.1 (necessary for Firefox)
  20. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  21. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  22. const string DEFAULT_H264_VIDEO_PROFILE =
  23. "profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
  24. struct CertificateFingerprint {
  25. enum class Algorithm { Sha1, Sha224, Sha256, Sha384, Sha512 };
  26. static string AlgorithmIdentifier(Algorithm algorithm);
  27. static size_t AlgorithmSize(Algorithm algorithm);
  28. bool isValid() const;
  29. Algorithm algorithm;
  30. string value;
  31. };
  32. class RTC_CPP_EXPORT Description {
  33. public:
  34. enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
  35. enum class Role { ActPass, Passive, Active };
  36. enum class Direction {
  37. SendOnly = RTC_DIRECTION_SENDONLY,
  38. RecvOnly = RTC_DIRECTION_RECVONLY,
  39. SendRecv = RTC_DIRECTION_SENDRECV,
  40. Inactive = RTC_DIRECTION_INACTIVE,
  41. Unknown = RTC_DIRECTION_UNKNOWN
  42. };
  43. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  44. Description(const string &sdp, string typeString);
  45. Type type() const;
  46. string typeString() const;
  47. Role role() const;
  48. string bundleMid() const;
  49. std::vector<string> iceOptions() const;
  50. optional<string> iceUfrag() const;
  51. optional<string> icePwd() const;
  52. optional<CertificateFingerprint> fingerprint() const;
  53. bool ended() const;
  54. void hintType(Type type);
  55. void addIceOption(string option);
  56. void removeIceOption(const string &option);
  57. void setIceAttribute(string ufrag, string pwd);
  58. void setFingerprint(CertificateFingerprint f);
  59. std::vector<string> attributes() const;
  60. void addAttribute(string attr);
  61. void removeAttribute(const string &attr);
  62. std::vector<Candidate> candidates() const;
  63. std::vector<Candidate> extractCandidates();
  64. bool hasCandidate(const Candidate &candidate) const;
  65. void addCandidate(Candidate candidate);
  66. void addCandidates(std::vector<Candidate> candidates);
  67. void endCandidates();
  68. operator string() const;
  69. string generateSdp(string_view eol = "\r\n") const;
  70. string generateApplicationSdp(string_view eol = "\r\n") const;
  71. class RTC_CPP_EXPORT Entry {
  72. public:
  73. virtual ~Entry() = default;
  74. virtual string type() const;
  75. virtual string protocol() const;
  76. virtual string description() const;
  77. virtual string mid() const;
  78. Direction direction() const;
  79. void setDirection(Direction dir);
  80. bool isRemoved() const;
  81. void markRemoved();
  82. std::vector<string> attributes() const;
  83. void addAttribute(string attr);
  84. void removeAttribute(const string &attr);
  85. void addRid(string rid);
  86. struct RTC_CPP_EXPORT ExtMap {
  87. static int parseId(string_view description);
  88. ExtMap(int id, string uri, Direction direction = Direction::Unknown);
  89. ExtMap(string_view description);
  90. void setDescription(string_view description);
  91. int id;
  92. string uri;
  93. string attributes;
  94. Direction direction = Direction::Unknown;
  95. };
  96. std::vector<int> extIds();
  97. ExtMap *extMap(int id);
  98. const ExtMap *extMap(int id) const;
  99. void addExtMap(ExtMap map);
  100. void removeExtMap(int id);
  101. operator string() const;
  102. string generateSdp(string_view eol = "\r\n", string_view addr = "0.0.0.0",
  103. uint16_t port = 9) const;
  104. virtual void parseSdpLine(string_view line);
  105. protected:
  106. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  107. virtual string generateSdpLines(string_view eol) const;
  108. std::vector<string> mAttributes;
  109. std::map<int, ExtMap> mExtMaps;
  110. private:
  111. string mType;
  112. string mProtocol;
  113. string mDescription;
  114. string mMid;
  115. std::vector<string> mRids;
  116. Direction mDirection;
  117. bool mIsRemoved;
  118. };
  119. struct RTC_CPP_EXPORT Application : public Entry {
  120. public:
  121. Application(string mid = "data");
  122. Application(const string &mline, string mid);
  123. virtual ~Application() = default;
  124. Application reciprocate() const;
  125. void setSctpPort(uint16_t port);
  126. void hintSctpPort(uint16_t port);
  127. void setMaxMessageSize(size_t size);
  128. optional<uint16_t> sctpPort() const;
  129. optional<size_t> maxMessageSize() const;
  130. virtual void parseSdpLine(string_view line) override;
  131. private:
  132. virtual string generateSdpLines(string_view eol) const override;
  133. optional<uint16_t> mSctpPort;
  134. optional<size_t> mMaxMessageSize;
  135. };
  136. // Media (non-data)
  137. class RTC_CPP_EXPORT Media : public Entry {
  138. public:
  139. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  140. Media(const string &sdp);
  141. virtual ~Media() = default;
  142. string description() const override;
  143. Media reciprocate() const;
  144. void addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  145. optional<string> trackId = nullopt);
  146. void removeSSRC(uint32_t ssrc);
  147. void replaceSSRC(uint32_t old, uint32_t ssrc, optional<string> name,
  148. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  149. bool hasSSRC(uint32_t ssrc) const;
  150. void clearSSRCs();
  151. std::vector<uint32_t> getSSRCs() const;
  152. optional<std::string> getCNameForSsrc(uint32_t ssrc) const;
  153. int bitrate() const;
  154. void setBitrate(int bitrate);
  155. struct RTC_CPP_EXPORT RtpMap {
  156. static int parsePayloadType(string_view description);
  157. explicit RtpMap(int payloadType);
  158. RtpMap(string_view description);
  159. void setDescription(string_view description);
  160. void addFeedback(string fb);
  161. void removeFeedback(const string &str);
  162. void addParameter(string p);
  163. void removeParameter(const string &str);
  164. int payloadType;
  165. string format;
  166. int clockRate;
  167. string encParams;
  168. std::vector<string> rtcpFbs;
  169. std::vector<string> fmtps;
  170. };
  171. bool hasPayloadType(int payloadType) const;
  172. std::vector<int> payloadTypes() const;
  173. RtpMap *rtpMap(int payloadType);
  174. const RtpMap *rtpMap(int payloadType) const;
  175. void addRtpMap(RtpMap map);
  176. void removeRtpMap(int payloadType);
  177. void removeFormat(const string &format);
  178. void addRtxCodec(int payloadType, int origPayloadType, unsigned int clockRate);
  179. virtual void parseSdpLine(string_view line) override;
  180. private:
  181. virtual string generateSdpLines(string_view eol) const override;
  182. int mBas = -1;
  183. std::vector<int> mOrderedPayloadTypes;
  184. std::map<int, RtpMap> mRtpMaps;
  185. std::vector<uint32_t> mSsrcs;
  186. std::map<uint32_t, string> mCNameMap;
  187. };
  188. class RTC_CPP_EXPORT Audio : public Media {
  189. public:
  190. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  191. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  192. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  193. void addPCMACodec(int payloadType, optional<string> profile = std::nullopt);
  194. void addPCMUCodec(int payloadType, optional<string> profile = std::nullopt);
  195. void addAACCodec(int payloadType, optional<string> profile = std::nullopt);
  196. void addG722Codec(int payloadType, optional<string> profile = std::nullopt);
  197. [[deprecated("Use addAACCodec")]] inline void
  198. addAacCodec(int payloadType, optional<string> profile = std::nullopt) {
  199. addAACCodec(payloadType, std::move(profile));
  200. };
  201. };
  202. class RTC_CPP_EXPORT Video : public Media {
  203. public:
  204. Video(string mid = "video", Direction dir = Direction::SendOnly);
  205. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  206. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  207. void addH265Codec(int payloadType, optional<string> profile = std::nullopt);
  208. void addVP8Codec(int payloadType, optional<string> profile = std::nullopt);
  209. void addVP9Codec(int payloadType, optional<string> profile = std::nullopt);
  210. void addAV1Codec(int payloadType, optional<string> profile = std::nullopt);
  211. };
  212. bool hasApplication() const;
  213. bool hasAudioOrVideo() const;
  214. bool hasMid(string_view mid) const;
  215. int addMedia(Media media);
  216. int addMedia(Application application);
  217. int addApplication(string mid = "data");
  218. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  219. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  220. void clearMedia();
  221. variant<Media *, Application *> media(int index);
  222. variant<const Media *, const Application *> media(int index) const;
  223. int mediaCount() const;
  224. const Application *application() const;
  225. Application *application();
  226. static Type stringToType(const string &typeString);
  227. static string typeToString(Type type);
  228. private:
  229. optional<Candidate> defaultCandidate() const;
  230. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  231. void removeApplication();
  232. Type mType;
  233. // Session-level attributes
  234. Role mRole;
  235. string mUsername;
  236. string mSessionId;
  237. std::vector<string> mIceOptions;
  238. optional<string> mIceUfrag, mIcePwd;
  239. optional<CertificateFingerprint> mFingerprint;
  240. std::vector<string> mAttributes; // other attributes
  241. // Entries
  242. std::vector<shared_ptr<Entry>> mEntries;
  243. shared_ptr<Application> mApplication;
  244. // Candidates
  245. std::vector<Candidate> mCandidates;
  246. bool mEnded = false;
  247. };
  248. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const Description &description);
  249. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, Description::Type type);
  250. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, Description::Role role);
  251. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const Description::Direction &direction);
  252. } // namespace rtc
  253. #endif