description.hpp 9.2 KB

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