description.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "include.hpp"
  23. #include <iostream>
  24. #include <map>
  25. #include <memory>
  26. #include <optional>
  27. #include <variant>
  28. #include <vector>
  29. namespace rtc {
  30. const string DEFAULT_OPUS_AUDIO_PROFILE =
  31. "minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
  32. // Use Constrained Baseline profile Level 4.2 (necessary for Firefox)
  33. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  34. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  35. const string DEFAULT_H264_VIDEO_PROFILE =
  36. "profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
  37. class RTC_CPP_EXPORT Description {
  38. public:
  39. enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
  40. enum class Role { ActPass, Passive, Active };
  41. enum class Direction { SendOnly, RecvOnly, SendRecv, Inactive, Unknown };
  42. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  43. Description(const string &sdp, string typeString);
  44. Type type() const;
  45. string typeString() const;
  46. Role role() const;
  47. string bundleMid() const;
  48. std::optional<string> iceUfrag() const;
  49. std::optional<string> icePwd() const;
  50. std::optional<string> fingerprint() const;
  51. bool ended() const;
  52. void hintType(Type type);
  53. void setFingerprint(string fingerprint);
  54. bool hasCandidate(const Candidate &candidate) const;
  55. void addCandidate(Candidate candidate);
  56. void addCandidates(std::vector<Candidate> candidates);
  57. void endCandidates();
  58. std::vector<Candidate> extractCandidates();
  59. operator string() const;
  60. string generateSdp(string_view eol) const;
  61. string generateApplicationSdp(string_view eol) 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. operator string() const;
  71. string generateSdp(string_view eol, string_view addr, string_view port) const;
  72. virtual void parseSdpLine(string_view line);
  73. std::vector<string>::iterator beginAttributes();
  74. std::vector<string>::iterator endAttributes();
  75. std::vector<string>::iterator removeAttribute(std::vector<string>::iterator iterator);
  76. protected:
  77. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  78. virtual string generateSdpLines(string_view eol) const;
  79. std::vector<string> mAttributes;
  80. private:
  81. string mType;
  82. string mDescription;
  83. string mMid;
  84. Direction mDirection;
  85. };
  86. struct RTC_CPP_EXPORT Application : public Entry {
  87. public:
  88. Application(string mid = "data");
  89. virtual ~Application() = default;
  90. string description() const override;
  91. Application reciprocate() const;
  92. void setSctpPort(uint16_t port) { mSctpPort = port; }
  93. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  94. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  95. std::optional<uint16_t> sctpPort() const { return mSctpPort; }
  96. std::optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  97. virtual void parseSdpLine(string_view line) override;
  98. private:
  99. virtual string generateSdpLines(string_view eol) const override;
  100. std::optional<uint16_t> mSctpPort;
  101. std::optional<size_t> mMaxMessageSize;
  102. };
  103. // Media (non-data)
  104. class RTC_CPP_EXPORT Media : public Entry {
  105. public:
  106. Media(const string &sdp);
  107. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  108. virtual ~Media() = default;
  109. string description() const override;
  110. Media reciprocate() const;
  111. void removeFormat(const string &fmt);
  112. void addSSRC(uint32_t ssrc, std::optional<string> name,
  113. std::optional<string> msid = nullopt, std::optional<string> trackID = nullopt);
  114. void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, std::optional<string> name,
  115. std::optional<string> msid = nullopt, std::optional<string> trackID = nullopt);
  116. bool hasSSRC(uint32_t ssrc);
  117. std::vector<uint32_t> getSSRCs();
  118. void setBitrate(int bitrate);
  119. int getBitrate() const;
  120. bool hasPayloadType(int payloadType) const;
  121. void addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
  122. unsigned int clockRate);
  123. virtual void parseSdpLine(string_view line) override;
  124. struct RTPMap {
  125. RTPMap(string_view mline);
  126. RTPMap() {}
  127. void removeFB(const string &string);
  128. void addFB(const string &string);
  129. void addAttribute(string attr) { fmtps.emplace_back(std::move(attr)); }
  130. int pt;
  131. string format;
  132. int clockRate;
  133. string encParams;
  134. std::vector<string> rtcpFbs;
  135. std::vector<string> fmtps;
  136. static int parsePT(string_view view);
  137. void setMLine(string_view view);
  138. };
  139. std::map<int, RTPMap>::iterator beginMaps();
  140. std::map<int, RTPMap>::iterator endMaps();
  141. std::map<int, RTPMap>::iterator removeMap(std::map<int, RTPMap>::iterator iterator);
  142. private:
  143. virtual string generateSdpLines(string_view eol) const override;
  144. int mBas = -1;
  145. Media::RTPMap &getFormat(int fmt);
  146. Media::RTPMap &getFormat(const string &fmt);
  147. std::map<int, RTPMap> mRtpMap;
  148. std::vector<uint32_t> mSsrcs;
  149. public:
  150. void addRTPMap(const RTPMap &map);
  151. void removeSSRC(uint32_t oldSSRC);
  152. };
  153. class RTC_CPP_EXPORT Audio : public Media {
  154. public:
  155. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  156. void addAudioCodec(int payloadType, string codec,
  157. std::optional<string> profile = std::nullopt);
  158. void addOpusCodec(int payloadType,
  159. std::optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  160. };
  161. class RTC_CPP_EXPORT Video : public Media {
  162. public:
  163. Video(string mid = "video", Direction dir = Direction::SendOnly);
  164. void addVideoCodec(int payloadType, string codec,
  165. std::optional<string> profile = std::nullopt);
  166. void addH264Codec(int payloadType,
  167. std::optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  168. void addVP8Codec(int payloadType);
  169. void addVP9Codec(int payloadType);
  170. };
  171. bool hasApplication() const;
  172. bool hasAudioOrVideo() const;
  173. bool hasMid(string_view mid) const;
  174. int addMedia(Media media);
  175. int addMedia(Application application);
  176. int addApplication(string mid = "data");
  177. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  178. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  179. std::variant<Media *, Application *> media(unsigned int index);
  180. std::variant<const Media *, const Application *> media(unsigned int index) const;
  181. unsigned int mediaCount() const;
  182. Application *application();
  183. static Type stringToType(const string &typeString);
  184. static string typeToString(Type type);
  185. private:
  186. std::optional<Candidate> defaultCandidate() const;
  187. std::shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  188. void removeApplication();
  189. Type mType;
  190. // Session-level attributes
  191. Role mRole;
  192. string mUsername;
  193. string mSessionId;
  194. std::optional<string> mIceUfrag, mIcePwd;
  195. std::optional<string> mFingerprint;
  196. // Entries
  197. std::vector<std::shared_ptr<Entry>> mEntries;
  198. std::shared_ptr<Application> mApplication;
  199. // Candidates
  200. std::vector<Candidate> mCandidates;
  201. bool mEnded = false;
  202. };
  203. } // namespace rtc
  204. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  205. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  206. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  207. #endif