description.hpp 11 KB

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