description.hpp 6.8 KB

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