description.hpp 6.4 KB

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