description.hpp 7.1 KB

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