description.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 setFingerprint(CertificateFingerprint f);
  56. void addIceOption(string option);
  57. void removeIceOption(const string &option);
  58. std::vector<string> attributes() const;
  59. void addAttribute(string attr);
  60. void removeAttribute(const string &attr);
  61. std::vector<Candidate> candidates() const;
  62. std::vector<Candidate> extractCandidates();
  63. bool hasCandidate(const Candidate &candidate) const;
  64. void addCandidate(Candidate candidate);
  65. void addCandidates(std::vector<Candidate> candidates);
  66. void endCandidates();
  67. operator string() const;
  68. string generateSdp(string_view eol = "\r\n") const;
  69. string generateApplicationSdp(string_view eol = "\r\n") const;
  70. class RTC_CPP_EXPORT Entry {
  71. public:
  72. virtual ~Entry() = default;
  73. virtual string type() const;
  74. virtual string protocol() const;
  75. virtual string description() const;
  76. virtual string mid() const;
  77. Direction direction() const;
  78. void setDirection(Direction dir);
  79. bool isRemoved() const;
  80. void markRemoved();
  81. std::vector<string> attributes() const;
  82. void addAttribute(string attr);
  83. void removeAttribute(const string &attr);
  84. void addRid(string rid);
  85. struct RTC_CPP_EXPORT ExtMap {
  86. static int parseId(string_view description);
  87. ExtMap(int id, string uri, Direction direction = Direction::Unknown);
  88. ExtMap(string_view description);
  89. void setDescription(string_view description);
  90. int id;
  91. string uri;
  92. string attributes;
  93. Direction direction = Direction::Unknown;
  94. };
  95. std::vector<int> extIds();
  96. ExtMap *extMap(int id);
  97. const ExtMap *extMap(int id) const;
  98. void addExtMap(ExtMap map);
  99. void removeExtMap(int id);
  100. operator string() const;
  101. string generateSdp(string_view eol = "\r\n", string_view addr = "0.0.0.0",
  102. uint16_t port = 9) const;
  103. virtual void parseSdpLine(string_view line);
  104. protected:
  105. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  106. virtual string generateSdpLines(string_view eol) const;
  107. std::vector<string> mAttributes;
  108. std::map<int, ExtMap> mExtMaps;
  109. private:
  110. string mType;
  111. string mProtocol;
  112. string mDescription;
  113. string mMid;
  114. std::vector<string> mRids;
  115. Direction mDirection;
  116. bool mIsRemoved;
  117. };
  118. struct RTC_CPP_EXPORT Application : public Entry {
  119. public:
  120. Application(string mid = "data");
  121. Application(const string &mline, string mid);
  122. virtual ~Application() = default;
  123. Application reciprocate() const;
  124. void setSctpPort(uint16_t port);
  125. void hintSctpPort(uint16_t port);
  126. void setMaxMessageSize(size_t size);
  127. optional<uint16_t> sctpPort() const;
  128. optional<size_t> maxMessageSize() const;
  129. virtual void parseSdpLine(string_view line) override;
  130. private:
  131. virtual string generateSdpLines(string_view eol) const override;
  132. optional<uint16_t> mSctpPort;
  133. optional<size_t> mMaxMessageSize;
  134. };
  135. // Media (non-data)
  136. class RTC_CPP_EXPORT Media : public Entry {
  137. public:
  138. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  139. Media(const string &sdp);
  140. virtual ~Media() = default;
  141. string description() const override;
  142. Media reciprocate() const;
  143. void addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  144. optional<string> trackId = nullopt);
  145. void removeSSRC(uint32_t ssrc);
  146. void replaceSSRC(uint32_t old, uint32_t ssrc, optional<string> name,
  147. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  148. bool hasSSRC(uint32_t ssrc) const;
  149. void clearSSRCs();
  150. std::vector<uint32_t> getSSRCs() const;
  151. optional<std::string> getCNameForSsrc(uint32_t ssrc) const;
  152. int bitrate() const;
  153. void setBitrate(int bitrate);
  154. struct RTC_CPP_EXPORT RtpMap {
  155. static int parsePayloadType(string_view description);
  156. explicit RtpMap(int payloadType);
  157. RtpMap(string_view description);
  158. void setDescription(string_view description);
  159. void addFeedback(string fb);
  160. void removeFeedback(const string &str);
  161. void addParameter(string p);
  162. void removeParameter(const string &str);
  163. int payloadType;
  164. string format;
  165. int clockRate;
  166. string encParams;
  167. std::vector<string> rtcpFbs;
  168. std::vector<string> fmtps;
  169. };
  170. bool hasPayloadType(int payloadType) const;
  171. std::vector<int> payloadTypes() const;
  172. RtpMap *rtpMap(int payloadType);
  173. const RtpMap *rtpMap(int payloadType) const;
  174. void addRtpMap(RtpMap map);
  175. void removeRtpMap(int payloadType);
  176. void removeFormat(const string &format);
  177. void addRtxCodec(int payloadType, int origPayloadType, unsigned int clockRate);
  178. virtual void parseSdpLine(string_view line) override;
  179. private:
  180. virtual string generateSdpLines(string_view eol) const override;
  181. int mBas = -1;
  182. std::vector<int> mOrderedPayloadTypes;
  183. std::map<int, RtpMap> mRtpMaps;
  184. std::vector<uint32_t> mSsrcs;
  185. std::map<uint32_t, string> mCNameMap;
  186. };
  187. class RTC_CPP_EXPORT Audio : public Media {
  188. public:
  189. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  190. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  191. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  192. void addPCMACodec(int payloadType, optional<string> profile = std::nullopt);
  193. void addPCMUCodec(int payloadType, optional<string> profile = std::nullopt);
  194. void addAACCodec(int payloadType, optional<string> profile = std::nullopt);
  195. [[deprecated("Use addAACCodec")]] inline void
  196. addAacCodec(int payloadType, optional<string> profile = std::nullopt) {
  197. addAACCodec(payloadType, std::move(profile));
  198. };
  199. };
  200. class RTC_CPP_EXPORT Video : public Media {
  201. public:
  202. Video(string mid = "video", Direction dir = Direction::SendOnly);
  203. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  204. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  205. void addH265Codec(int payloadType, optional<string> profile = std::nullopt);
  206. void addVP8Codec(int payloadType, optional<string> profile = std::nullopt);
  207. void addVP9Codec(int payloadType, optional<string> profile = std::nullopt);
  208. void addAV1Codec(int payloadType, optional<string> profile = std::nullopt);
  209. };
  210. bool hasApplication() const;
  211. bool hasAudioOrVideo() const;
  212. bool hasMid(string_view mid) const;
  213. int addMedia(Media media);
  214. int addMedia(Application application);
  215. int addApplication(string mid = "data");
  216. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  217. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  218. void clearMedia();
  219. variant<Media *, Application *> media(unsigned int index);
  220. variant<const Media *, const Application *> media(unsigned int index) const;
  221. unsigned int mediaCount() const;
  222. const Application *application() const;
  223. Application *application();
  224. static Type stringToType(const string &typeString);
  225. static string typeToString(Type type);
  226. private:
  227. optional<Candidate> defaultCandidate() const;
  228. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  229. void removeApplication();
  230. Type mType;
  231. // Session-level attributes
  232. Role mRole;
  233. string mUsername;
  234. string mSessionId;
  235. std::vector<string> mIceOptions;
  236. optional<string> mIceUfrag, mIcePwd;
  237. optional<CertificateFingerprint> mFingerprint;
  238. std::vector<string> mAttributes; // other attributes
  239. // Entries
  240. std::vector<shared_ptr<Entry>> mEntries;
  241. shared_ptr<Application> mApplication;
  242. // Candidates
  243. std::vector<Candidate> mCandidates;
  244. bool mEnded = false;
  245. };
  246. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const Description &description);
  247. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, Description::Type type);
  248. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, Description::Role role);
  249. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const Description::Direction &direction);
  250. } // namespace rtc
  251. #endif