description.hpp 7.2 KB

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