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 description() const;
  75. virtual string mid() const;
  76. Direction direction() const;
  77. void setDirection(Direction dir);
  78. bool isRemoved() const;
  79. void markRemoved();
  80. std::vector<string> attributes() const;
  81. void addAttribute(string attr);
  82. void removeAttribute(const string &attr);
  83. void addRid(string rid);
  84. struct RTC_CPP_EXPORT ExtMap {
  85. static int parseId(string_view description);
  86. ExtMap(int id, string uri, Direction direction = Direction::Unknown);
  87. ExtMap(string_view description);
  88. void setDescription(string_view description);
  89. int id;
  90. string uri;
  91. string attributes;
  92. Direction direction = Direction::Unknown;
  93. };
  94. std::vector<int> extIds() const;
  95. optional<int> findExtId(string_view uri) const;
  96. int nextExtId() const;
  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 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. string description() const override;
  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 &sdp);
  140. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  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::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