description.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Staz Modrzynski
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef RTC_DESCRIPTION_H
  20. #define RTC_DESCRIPTION_H
  21. #include "candidate.hpp"
  22. #include "common.hpp"
  23. #include <iostream>
  24. #include <map>
  25. #include <vector>
  26. namespace rtc {
  27. const string DEFAULT_OPUS_AUDIO_PROFILE =
  28. "minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
  29. // Use Constrained Baseline profile Level 3.1 (necessary for Firefox)
  30. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  31. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  32. const string DEFAULT_H264_VIDEO_PROFILE =
  33. "profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
  34. class RTC_CPP_EXPORT Description {
  35. public:
  36. enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
  37. enum class Role { ActPass, Passive, Active };
  38. enum class Direction {
  39. SendOnly = RTC_DIRECTION_SENDONLY,
  40. RecvOnly = RTC_DIRECTION_RECVONLY,
  41. SendRecv = RTC_DIRECTION_SENDRECV,
  42. Inactive = RTC_DIRECTION_INACTIVE,
  43. Unknown = RTC_DIRECTION_UNKNOWN
  44. };
  45. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  46. Description(const string &sdp, string typeString);
  47. Type type() const;
  48. string typeString() const;
  49. Role role() const;
  50. string bundleMid() const;
  51. std::vector<string> iceOptions() const;
  52. optional<string> iceUfrag() const;
  53. optional<string> icePwd() const;
  54. optional<string> fingerprint() const;
  55. bool ended() const;
  56. void hintType(Type type);
  57. void setFingerprint(string fingerprint);
  58. void addIceOption(string option);
  59. void removeIceOption(const string &option);
  60. std::vector<string> attributes() const;
  61. void addAttribute(string attr);
  62. void removeAttribute(const string &attr);
  63. std::vector<Candidate> candidates() const;
  64. std::vector<Candidate> extractCandidates();
  65. bool hasCandidate(const Candidate &candidate) const;
  66. void addCandidate(Candidate candidate);
  67. void addCandidates(std::vector<Candidate> candidates);
  68. void endCandidates();
  69. operator string() const;
  70. string generateSdp(string_view eol) const;
  71. string generateApplicationSdp(string_view eol) const;
  72. class RTC_CPP_EXPORT Entry {
  73. public:
  74. virtual ~Entry() = default;
  75. virtual string type() const { return mType; }
  76. virtual string description() const { return mDescription; }
  77. virtual string mid() const { return mMid; }
  78. Direction direction() const { return mDirection; }
  79. void setDirection(Direction dir);
  80. bool isRemoved() const { return mIsRemoved; }
  81. void markRemoved();
  82. std::vector<string> attributes() const;
  83. void addAttribute(string attr);
  84. void removeAttribute(const string &attr);
  85. struct RTC_CPP_EXPORT ExtMap {
  86. static int parseId(string_view description);
  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. void addExtMap(ExtMap map);
  97. void removeExtMap(int id);
  98. operator string() const;
  99. string generateSdp(string_view eol, string_view addr, uint16_t port) const;
  100. virtual void parseSdpLine(string_view line);
  101. // For backward compatibility, do not use
  102. [[deprecated]] std::vector<string>::iterator beginAttributes();
  103. [[deprecated]] std::vector<string>::iterator endAttributes();
  104. [[deprecated]] std::vector<string>::iterator
  105. removeAttribute(std::vector<string>::iterator iterator);
  106. [[deprecated]] std::map<int, ExtMap>::iterator beginExtMaps();
  107. [[deprecated]] std::map<int, ExtMap>::iterator endExtMaps();
  108. [[deprecated]] std::map<int, ExtMap>::iterator
  109. removeExtMap(std::map<int, ExtMap>::iterator iterator);
  110. protected:
  111. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  112. virtual string generateSdpLines(string_view eol) const;
  113. std::vector<string> mAttributes;
  114. std::map<int, ExtMap> mExtMaps;
  115. private:
  116. string mType;
  117. string mDescription;
  118. string mMid;
  119. Direction mDirection;
  120. bool mIsRemoved;
  121. };
  122. struct RTC_CPP_EXPORT Application : public Entry {
  123. public:
  124. Application(string mid = "data");
  125. Application(const string &mline, string mid);
  126. virtual ~Application() = default;
  127. string description() const override;
  128. Application reciprocate() const;
  129. void setSctpPort(uint16_t port) { mSctpPort = port; }
  130. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  131. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  132. optional<uint16_t> sctpPort() const { return mSctpPort; }
  133. optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  134. virtual void parseSdpLine(string_view line) override;
  135. private:
  136. virtual string generateSdpLines(string_view eol) const override;
  137. optional<uint16_t> mSctpPort;
  138. optional<size_t> mMaxMessageSize;
  139. };
  140. // Media (non-data)
  141. class RTC_CPP_EXPORT Media : public Entry {
  142. public:
  143. Media(const string &sdp);
  144. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  145. virtual ~Media() = default;
  146. string description() const override;
  147. Media reciprocate() const;
  148. void addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  149. optional<string> trackId = nullopt);
  150. void removeSSRC(uint32_t ssrc);
  151. void replaceSSRC(uint32_t old, uint32_t ssrc, optional<string> name,
  152. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  153. bool hasSSRC(uint32_t ssrc);
  154. std::vector<uint32_t> getSSRCs();
  155. std::optional<std::string> getCNameForSsrc(uint32_t ssrc);
  156. int bitrate() const;
  157. void setBitrate(int bitrate);
  158. struct RTC_CPP_EXPORT RtpMap {
  159. static int parsePayloadType(string_view description);
  160. explicit RtpMap(int payloadType);
  161. RtpMap(string_view description);
  162. void setDescription(string_view description);
  163. void addFeedback(string fb);
  164. void removeFeedback(const string &str);
  165. void addParameter(string p);
  166. void removeParameter(const string &str);
  167. int payloadType;
  168. string format;
  169. int clockRate;
  170. string encParams;
  171. std::vector<string> rtcpFbs;
  172. std::vector<string> fmtps;
  173. // For backward compatibility, do not use
  174. [[deprecated]] void addFB(string fb) { addFeedback(std::move(fb)); }
  175. [[deprecated]] void removeFB(const string &str) { removeFeedback(str); }
  176. [[deprecated]] void addAttribute(string attr) { addParameter(std::move(attr)); }
  177. };
  178. bool hasPayloadType(int payloadType) const;
  179. std::vector<int> payloadTypes() const;
  180. RtpMap *rtpMap(int payloadType);
  181. void addRtpMap(RtpMap map);
  182. void removeRtpMap(int payloadType);
  183. void removeFormat(const string &format);
  184. void addRtxCodec(int payloadType, int origPayloadType, unsigned int clockRate);
  185. virtual void parseSdpLine(string_view line) override;
  186. // For backward compatibility, do not use
  187. using RTPMap = RtpMap;
  188. [[deprecated]] int getBitrate() const { return bitrate(); }
  189. [[deprecated]] inline void addRTPMap(RtpMap map) { addRtpMap(std::move(map)); }
  190. [[deprecated]] inline void addRTXCodec(int pt, int origpt, unsigned int clk) {
  191. addRtxCodec(pt, origpt, clk);
  192. }
  193. [[deprecated]] std::map<int, RtpMap>::iterator beginMaps();
  194. [[deprecated]] std::map<int, RtpMap>::iterator endMaps();
  195. [[deprecated]] std::map<int, RtpMap>::iterator
  196. removeMap(std::map<int, RtpMap>::iterator iterator);
  197. private:
  198. virtual string generateSdpLines(string_view eol) const override;
  199. int mBas = -1;
  200. std::map<int, RtpMap> mRtpMaps;
  201. std::vector<uint32_t> mSsrcs;
  202. std::map<uint32_t, string> mCNameMap;
  203. };
  204. class RTC_CPP_EXPORT Audio : public Media {
  205. public:
  206. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  207. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  208. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  209. };
  210. class RTC_CPP_EXPORT Video : public Media {
  211. public:
  212. Video(string mid = "video", Direction dir = Direction::SendOnly);
  213. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  214. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  215. void addVP8Codec(int payloadType);
  216. void addVP9Codec(int payloadType);
  217. };
  218. bool hasApplication() const;
  219. bool hasAudioOrVideo() const;
  220. bool hasMid(string_view mid) const;
  221. int addMedia(Media media);
  222. int addMedia(Application application);
  223. int addApplication(string mid = "data");
  224. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  225. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  226. void clearMedia();
  227. variant<Media *, Application *> media(unsigned int index);
  228. variant<const Media *, const Application *> media(unsigned int index) const;
  229. unsigned int mediaCount() const;
  230. const Application *application() const;
  231. Application *application();
  232. static Type stringToType(const string &typeString);
  233. static string typeToString(Type type);
  234. private:
  235. optional<Candidate> defaultCandidate() const;
  236. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  237. void removeApplication();
  238. Type mType;
  239. // Session-level attributes
  240. Role mRole;
  241. string mUsername;
  242. string mSessionId;
  243. std::vector<string> mIceOptions;
  244. optional<string> mIceUfrag, mIcePwd;
  245. optional<string> mFingerprint;
  246. std::vector<string> mAttributes; // other attributes
  247. // Entries
  248. std::vector<shared_ptr<Entry>> mEntries;
  249. shared_ptr<Application> mApplication;
  250. // Candidates
  251. std::vector<Candidate> mCandidates;
  252. bool mEnded = false;
  253. };
  254. } // namespace rtc
  255. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  256. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  257. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  258. #endif