description.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. bool hasCandidate(const Candidate &candidate) const;
  60. void addCandidate(Candidate candidate);
  61. void addCandidates(std::vector<Candidate> candidates);
  62. void endCandidates();
  63. std::vector<Candidate> extractCandidates();
  64. operator string() const;
  65. string generateSdp(string_view eol) const;
  66. string generateApplicationSdp(string_view eol) const;
  67. class RTC_CPP_EXPORT Entry {
  68. public:
  69. virtual ~Entry() = default;
  70. virtual string type() const { return mType; }
  71. virtual string description() const { return mDescription; }
  72. virtual string mid() const { return mMid; }
  73. Direction direction() const { return mDirection; }
  74. void setDirection(Direction dir);
  75. operator string() const;
  76. string generateSdp(string_view eol, string_view addr, string_view port) const;
  77. virtual void parseSdpLine(string_view line);
  78. std::vector<string>::iterator beginAttributes();
  79. std::vector<string>::iterator endAttributes();
  80. std::vector<string>::iterator removeAttribute(std::vector<string>::iterator iterator);
  81. struct RTC_CPP_EXPORT ExtMap {
  82. ExtMap(string_view description);
  83. ExtMap() {}
  84. int id;
  85. string uri;
  86. string attributes;
  87. Direction direction = Direction::Unknown;
  88. static int parseId(string_view view);
  89. void setDescription(string_view view);
  90. };
  91. void addExtMap(const ExtMap &map);
  92. std::map<int, ExtMap>::iterator beginExtMaps();
  93. std::map<int, ExtMap>::iterator endExtMaps();
  94. std::map<int, ExtMap>::iterator removeExtMap(std::map<int, ExtMap>::iterator iterator);
  95. protected:
  96. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  97. virtual string generateSdpLines(string_view eol) const;
  98. std::vector<string> mAttributes;
  99. std::map<int, ExtMap> mExtMap;
  100. private:
  101. string mType;
  102. string mDescription;
  103. string mMid;
  104. Direction mDirection;
  105. };
  106. struct RTC_CPP_EXPORT Application : public Entry {
  107. public:
  108. Application(string mid = "data");
  109. virtual ~Application() = default;
  110. string description() const override;
  111. Application reciprocate() const;
  112. void setSctpPort(uint16_t port) { mSctpPort = port; }
  113. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  114. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  115. optional<uint16_t> sctpPort() const { return mSctpPort; }
  116. optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  117. virtual void parseSdpLine(string_view line) override;
  118. private:
  119. virtual string generateSdpLines(string_view eol) const override;
  120. optional<uint16_t> mSctpPort;
  121. optional<size_t> mMaxMessageSize;
  122. };
  123. // Media (non-data)
  124. class RTC_CPP_EXPORT Media : public Entry {
  125. public:
  126. Media(const string &sdp);
  127. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  128. virtual ~Media() = default;
  129. string description() const override;
  130. Media reciprocate() const;
  131. void removeFormat(const string &fmt);
  132. void addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  133. optional<string> trackID = nullopt);
  134. void removeSSRC(uint32_t oldSSRC);
  135. void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, optional<string> name,
  136. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  137. bool hasSSRC(uint32_t ssrc);
  138. std::vector<uint32_t> getSSRCs();
  139. std::optional<std::string> getCNameForSsrc(uint32_t ssrc);
  140. void setBitrate(int bitrate);
  141. int getBitrate() const;
  142. bool hasPayloadType(int payloadType) const;
  143. void addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
  144. unsigned int clockRate);
  145. virtual void parseSdpLine(string_view line) override;
  146. struct RTC_CPP_EXPORT RTPMap {
  147. RTPMap(string_view mline);
  148. RTPMap() {}
  149. void removeFB(const string &string);
  150. void addFB(const string &string);
  151. void addAttribute(string attr) { fmtps.emplace_back(std::move(attr)); }
  152. int pt;
  153. string format;
  154. int clockRate;
  155. string encParams;
  156. std::vector<string> rtcpFbs;
  157. std::vector<string> fmtps;
  158. static int parsePT(string_view view);
  159. void setMLine(string_view view);
  160. };
  161. void addRTPMap(const RTPMap &map);
  162. std::map<int, RTPMap>::iterator beginMaps();
  163. std::map<int, RTPMap>::iterator endMaps();
  164. std::map<int, RTPMap>::iterator removeMap(std::map<int, RTPMap>::iterator iterator);
  165. private:
  166. virtual string generateSdpLines(string_view eol) const override;
  167. int mBas = -1;
  168. Media::RTPMap &getFormat(int fmt);
  169. Media::RTPMap &getFormat(const string &fmt);
  170. std::map<int, RTPMap> mRtpMap;
  171. std::vector<uint32_t> mSsrcs;
  172. std::map<uint32_t, string> mCNameMap;
  173. };
  174. class RTC_CPP_EXPORT Audio : public Media {
  175. public:
  176. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  177. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  178. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  179. };
  180. class RTC_CPP_EXPORT Video : public Media {
  181. public:
  182. Video(string mid = "video", Direction dir = Direction::SendOnly);
  183. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  184. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  185. void addVP8Codec(int payloadType);
  186. void addVP9Codec(int payloadType);
  187. };
  188. bool hasApplication() const;
  189. bool hasAudioOrVideo() const;
  190. bool hasMid(string_view mid) const;
  191. int addMedia(Media media);
  192. int addMedia(Application application);
  193. int addApplication(string mid = "data");
  194. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  195. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  196. void clearMedia();
  197. variant<Media *, Application *> media(unsigned int index);
  198. variant<const Media *, const Application *> media(unsigned int index) const;
  199. unsigned int mediaCount() const;
  200. const Application *application() const;
  201. Application *application();
  202. static Type stringToType(const string &typeString);
  203. static string typeToString(Type type);
  204. private:
  205. optional<Candidate> defaultCandidate() const;
  206. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  207. void removeApplication();
  208. Type mType;
  209. // Session-level attributes
  210. Role mRole;
  211. string mUsername;
  212. string mSessionId;
  213. std::vector<string> mIceOptions;
  214. optional<string> mIceUfrag, mIcePwd;
  215. optional<string> mFingerprint;
  216. // Entries
  217. std::vector<shared_ptr<Entry>> mEntries;
  218. shared_ptr<Application> mApplication;
  219. // Candidates
  220. std::vector<Candidate> mCandidates;
  221. bool mEnded = false;
  222. };
  223. } // namespace rtc
  224. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  225. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  226. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  227. #endif