description.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Staz Modrzynski
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #ifndef RTC_DESCRIPTION_H
  10. #define RTC_DESCRIPTION_H
  11. #include "candidate.hpp"
  12. #include "common.hpp"
  13. #include <iostream>
  14. #include <map>
  15. #include <vector>
  16. namespace rtc {
  17. const string DEFAULT_OPUS_AUDIO_PROFILE =
  18. "minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
  19. // Use Constrained Baseline profile Level 3.1 (necessary for Firefox)
  20. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  21. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  22. const string DEFAULT_H264_VIDEO_PROFILE =
  23. "profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
  24. class RTC_CPP_EXPORT Description {
  25. public:
  26. enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
  27. enum class Role { ActPass, Passive, Active };
  28. enum class Direction {
  29. SendOnly = RTC_DIRECTION_SENDONLY,
  30. RecvOnly = RTC_DIRECTION_RECVONLY,
  31. SendRecv = RTC_DIRECTION_SENDRECV,
  32. Inactive = RTC_DIRECTION_INACTIVE,
  33. Unknown = RTC_DIRECTION_UNKNOWN
  34. };
  35. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  36. Description(const string &sdp, string typeString);
  37. Type type() const;
  38. string typeString() const;
  39. Role role() const;
  40. string bundleMid() const;
  41. std::vector<string> iceOptions() const;
  42. optional<string> iceUfrag() const;
  43. optional<string> icePwd() const;
  44. optional<string> fingerprint() const;
  45. bool ended() const;
  46. void hintType(Type type);
  47. void setFingerprint(string fingerprint);
  48. void addIceOption(string option);
  49. void removeIceOption(const string &option);
  50. std::vector<string> attributes() const;
  51. void addAttribute(string attr);
  52. void removeAttribute(const string &attr);
  53. std::vector<Candidate> candidates() const;
  54. std::vector<Candidate> extractCandidates();
  55. bool hasCandidate(const Candidate &candidate) const;
  56. void addCandidate(Candidate candidate);
  57. void addCandidates(std::vector<Candidate> candidates);
  58. void endCandidates();
  59. operator string() const;
  60. string generateSdp(string_view eol = "\r\n") const;
  61. string generateApplicationSdp(string_view eol = "\r\n") const;
  62. class RTC_CPP_EXPORT Entry {
  63. public:
  64. virtual ~Entry() = default;
  65. virtual string type() const { return mType; }
  66. virtual string description() const { return mDescription; }
  67. virtual string mid() const { return mMid; }
  68. Direction direction() const { return mDirection; }
  69. void setDirection(Direction dir);
  70. bool isRemoved() const { return mIsRemoved; }
  71. void markRemoved();
  72. std::vector<string> attributes() const;
  73. void addAttribute(string attr);
  74. void removeAttribute(const string &attr);
  75. struct RTC_CPP_EXPORT ExtMap {
  76. static int parseId(string_view description);
  77. ExtMap(int id, string uri, Direction direction = Direction::Unknown);
  78. ExtMap(string_view description);
  79. void setDescription(string_view description);
  80. int id;
  81. string uri;
  82. string attributes;
  83. Direction direction = Direction::Unknown;
  84. };
  85. std::vector<int> extIds();
  86. ExtMap *extMap(int id);
  87. void addExtMap(ExtMap map);
  88. void removeExtMap(int id);
  89. operator string() const;
  90. string generateSdp(string_view eol = "\r\n", string_view addr = "0.0.0.0",
  91. uint16_t port = 9) const;
  92. virtual void parseSdpLine(string_view line);
  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> mExtMaps;
  98. private:
  99. string mType;
  100. string mDescription;
  101. string mMid;
  102. Direction mDirection;
  103. bool mIsRemoved;
  104. };
  105. struct RTC_CPP_EXPORT Application : public Entry {
  106. public:
  107. Application(string mid = "data");
  108. Application(const string &mline, string mid);
  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 addSSRC(uint32_t ssrc, optional<string> name, optional<string> msid = nullopt,
  132. optional<string> trackId = nullopt);
  133. void removeSSRC(uint32_t ssrc);
  134. void replaceSSRC(uint32_t old, uint32_t ssrc, optional<string> name,
  135. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  136. bool hasSSRC(uint32_t ssrc) const;
  137. void clearSSRCs();
  138. std::vector<uint32_t> getSSRCs() const;
  139. std::optional<std::string> getCNameForSsrc(uint32_t ssrc) const;
  140. int bitrate() const;
  141. void setBitrate(int bitrate);
  142. struct RTC_CPP_EXPORT RtpMap {
  143. static int parsePayloadType(string_view description);
  144. explicit RtpMap(int payloadType);
  145. RtpMap(string_view description);
  146. void setDescription(string_view description);
  147. void addFeedback(string fb);
  148. void removeFeedback(const string &str);
  149. void addParameter(string p);
  150. void removeParameter(const string &str);
  151. int payloadType;
  152. string format;
  153. int clockRate;
  154. string encParams;
  155. std::vector<string> rtcpFbs;
  156. std::vector<string> fmtps;
  157. };
  158. bool hasPayloadType(int payloadType) const;
  159. std::vector<int> payloadTypes() const;
  160. RtpMap *rtpMap(int payloadType);
  161. void addRtpMap(RtpMap map);
  162. void removeRtpMap(int payloadType);
  163. void removeFormat(const string &format);
  164. void addRtxCodec(int payloadType, int origPayloadType, unsigned int clockRate);
  165. virtual void parseSdpLine(string_view line) override;
  166. private:
  167. virtual string generateSdpLines(string_view eol) const override;
  168. int mBas = -1;
  169. std::map<int, RtpMap> mRtpMaps;
  170. std::vector<uint32_t> mSsrcs;
  171. std::map<uint32_t, string> mCNameMap;
  172. };
  173. class RTC_CPP_EXPORT Audio : public Media {
  174. public:
  175. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  176. void addAudioCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  177. void addOpusCodec(int payloadType, optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  178. void addPCMACodec(int payloadType, optional<string> profile = std::nullopt);
  179. void addPCMUCodec(int payloadType, optional<string> profile = std::nullopt);
  180. };
  181. class RTC_CPP_EXPORT Video : public Media {
  182. public:
  183. Video(string mid = "video", Direction dir = Direction::SendOnly);
  184. void addVideoCodec(int payloadType, string codec, optional<string> profile = std::nullopt);
  185. void addH264Codec(int payloadType, optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  186. void addVP8Codec(int payloadType);
  187. void addVP9Codec(int payloadType);
  188. };
  189. bool hasApplication() const;
  190. bool hasAudioOrVideo() const;
  191. bool hasMid(string_view mid) const;
  192. int addMedia(Media media);
  193. int addMedia(Application application);
  194. int addApplication(string mid = "data");
  195. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  196. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  197. void clearMedia();
  198. variant<Media *, Application *> media(unsigned int index);
  199. variant<const Media *, const Application *> media(unsigned int index) const;
  200. unsigned int mediaCount() const;
  201. const Application *application() const;
  202. Application *application();
  203. static Type stringToType(const string &typeString);
  204. static string typeToString(Type type);
  205. private:
  206. optional<Candidate> defaultCandidate() const;
  207. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  208. void removeApplication();
  209. Type mType;
  210. // Session-level attributes
  211. Role mRole;
  212. string mUsername;
  213. string mSessionId;
  214. std::vector<string> mIceOptions;
  215. optional<string> mIceUfrag, mIcePwd;
  216. optional<string> mFingerprint;
  217. std::vector<string> mAttributes; // other attributes
  218. // Entries
  219. std::vector<shared_ptr<Entry>> mEntries;
  220. shared_ptr<Application> mApplication;
  221. // Candidates
  222. std::vector<Candidate> mCandidates;
  223. bool mEnded = false;
  224. };
  225. } // namespace rtc
  226. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  227. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  228. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  229. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description::Direction &direction);
  230. #endif