description.hpp 8.9 KB

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