2
0

description.hpp 9.5 KB

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