description.hpp 9.7 KB

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