description.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 4.2 (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. bool hasCandidate(const Candidate &candidate) const;
  61. void addCandidate(Candidate candidate);
  62. void addCandidates(std::vector<Candidate> candidates);
  63. void endCandidates();
  64. std::vector<Candidate> extractCandidates();
  65. operator string() const;
  66. string generateSdp(string_view eol) const;
  67. string generateApplicationSdp(string_view eol) const;
  68. class RTC_CPP_EXPORT Entry {
  69. public:
  70. virtual ~Entry() = default;
  71. virtual string type() const { return mType; }
  72. virtual string description() const { return mDescription; }
  73. virtual string mid() const { return mMid; }
  74. Direction direction() const { return mDirection; }
  75. void setDirection(Direction dir);
  76. std::vector<string> attributes() const;
  77. void addAttribute(string attr);
  78. void removeAttribute(const string &attr);
  79. struct RTC_CPP_EXPORT ExtMap {
  80. static int parseId(string_view description);
  81. ExtMap(string_view description);
  82. void setDescription(string_view description);
  83. int id;
  84. string uri;
  85. string attributes;
  86. Direction direction = Direction::Unknown;
  87. };
  88. std::vector<int> extIds();
  89. ExtMap *extMap(int id);
  90. void addExtMap(ExtMap map);
  91. void removeExtMap(int id);
  92. operator string() const;
  93. string generateSdp(string_view eol, string_view addr, string_view port) const;
  94. virtual void parseSdpLine(string_view line);
  95. // For backward compatibility, do not use
  96. [[deprecated]] std::vector<string>::iterator beginAttributes();
  97. [[deprecated]] std::vector<string>::iterator endAttributes();
  98. [[deprecated]] std::vector<string>::iterator
  99. removeAttribute(std::vector<string>::iterator iterator);
  100. [[deprecated]] std::map<int, ExtMap>::iterator beginExtMaps();
  101. [[deprecated]] std::map<int, ExtMap>::iterator endExtMaps();
  102. [[deprecated]] std::map<int, ExtMap>::iterator
  103. removeExtMap(std::map<int, ExtMap>::iterator iterator);
  104. protected:
  105. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  106. virtual string generateSdpLines(string_view eol) const;
  107. std::vector<string> mAttributes;
  108. std::map<int, ExtMap> mExtMaps;
  109. private:
  110. string mType;
  111. string mDescription;
  112. string mMid;
  113. Direction mDirection;
  114. };
  115. struct RTC_CPP_EXPORT Application : public Entry {
  116. public:
  117. Application(string mid = "data");
  118. virtual ~Application() = default;
  119. string description() const override;
  120. Application reciprocate() const;
  121. void setSctpPort(uint16_t port) { mSctpPort = port; }
  122. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  123. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  124. optional<uint16_t> sctpPort() const { return mSctpPort; }
  125. optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  126. virtual void parseSdpLine(string_view line) override;
  127. private:
  128. virtual string generateSdpLines(string_view eol) const override;
  129. optional<uint16_t> mSctpPort;
  130. optional<size_t> mMaxMessageSize;
  131. };
  132. // Media (non-data)
  133. class RTC_CPP_EXPORT Media : public Entry {
  134. public:
  135. Media(const string &sdp);
  136. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  137. virtual ~Media() = default;
  138. string description() const override;
  139. Media reciprocate() const;
  140. void addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  141. optional<string> trackID = nullopt);
  142. void removeSSRC(uint32_t oldSSRC);
  143. void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, optional<string> name,
  144. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  145. bool hasSSRC(uint32_t ssrc);
  146. std::vector<uint32_t> getSSRCs();
  147. std::optional<std::string> getCNameForSsrc(uint32_t ssrc);
  148. int bitrate() const;
  149. void setBitrate(int bitrate);
  150. struct RTC_CPP_EXPORT RtpMap {
  151. static int parsePayloadType(string_view description);
  152. explicit RtpMap(int payloadType);
  153. RtpMap(string_view description);
  154. void setDescription(string_view description);
  155. void addFeedback(string fb);
  156. void removeFeedback(const string &str);
  157. void addParameter(string p);
  158. void removeParameter(const string &str);
  159. int payloadType;
  160. string format;
  161. int clockRate;
  162. string encParams;
  163. std::vector<string> rtcpFbs;
  164. std::vector<string> fmtps;
  165. // For backward compatibility, do not use
  166. [[deprecated]] void addFB(string fb) { addFeedback(std::move(fb)); }
  167. [[deprecated]] void removeFB(const string &str) { removeFeedback(str); }
  168. [[deprecated]] void addAttribute(string attr) { addParameter(std::move(attr)); }
  169. };
  170. bool hasPayloadType(int payloadType) const;
  171. std::vector<int> payloadTypes() const;
  172. RtpMap *rtpMap(int payloadType);
  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. // For backward compatibility, do not use
  179. using RTPMap = RtpMap;
  180. [[deprecated]] int getBitrate() const { return bitrate(); }
  181. [[deprecated]] inline void addRTPMap(RtpMap map) { addRtpMap(std::move(map)); }
  182. [[deprecated]] inline void addRTXCodec(int pt, int origpt, unsigned int clk) {
  183. addRtxCodec(pt, origpt, clk);
  184. }
  185. [[deprecated]] std::map<int, RtpMap>::iterator beginMaps();
  186. [[deprecated]] std::map<int, RtpMap>::iterator endMaps();
  187. [[deprecated]] std::map<int, RtpMap>::iterator
  188. removeMap(std::map<int, RtpMap>::iterator iterator);
  189. private:
  190. virtual string generateSdpLines(string_view eol) const override;
  191. int mBas = -1;
  192. std::map<int, RtpMap> mRtpMaps;
  193. std::vector<uint32_t> mSsrcs;
  194. std::map<uint32_t, string> mCNameMap;
  195. };
  196. class RTC_CPP_EXPORT Audio : public Media {
  197. public:
  198. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  199. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  200. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  201. };
  202. class RTC_CPP_EXPORT Video : public Media {
  203. public:
  204. Video(string mid = "video", Direction dir = Direction::SendOnly);
  205. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  206. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  207. void addVP8Codec(int payloadType);
  208. void addVP9Codec(int payloadType);
  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<string> mFingerprint;
  238. // Entries
  239. std::vector<shared_ptr<Entry>> mEntries;
  240. shared_ptr<Application> mApplication;
  241. // Candidates
  242. std::vector<Candidate> mCandidates;
  243. bool mEnded = false;
  244. };
  245. } // namespace rtc
  246. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  247. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  248. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  249. #endif