2
0

description.hpp 10 KB

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