description.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Staz M
  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. string iceUfrag() const;
  44. string icePwd() const;
  45. std::optional<string> fingerprint() const;
  46. bool ended() const;
  47. void hintType(Type type);
  48. void setFingerprint(string fingerprint);
  49. void addCandidate(Candidate candidate);
  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) 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. Application(const Application &other) = default;
  80. Application(Application &&other) = default;
  81. string description() const override;
  82. Application reciprocate() const;
  83. void setSctpPort(uint16_t port) { mSctpPort = port; }
  84. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  85. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  86. std::optional<uint16_t> sctpPort() const { return mSctpPort; }
  87. std::optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  88. virtual void parseSdpLine(string_view line) override;
  89. private:
  90. virtual string generateSdpLines(string_view eol) const override;
  91. std::optional<uint16_t> mSctpPort;
  92. std::optional<size_t> mMaxMessageSize;
  93. };
  94. // Media (non-data)
  95. class Media : public Entry {
  96. public:
  97. Media(const string &sdp);
  98. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  99. Media(const Media &other) = default;
  100. Media(Media &&other) = default;
  101. virtual ~Media() = default;
  102. string description() const override;
  103. Media reciprocate() const;
  104. void removeFormat(const string &fmt);
  105. void addVideoCodec(int payloadType, const string &codec);
  106. void addH264Codec(int payloadType);
  107. void addVP8Codec(int payloadType);
  108. void addVP9Codec(int payloadType);
  109. void setBitrate(int bitrate);
  110. int getBitrate() const;
  111. bool hasPayloadType(int payloadType) const;
  112. virtual void parseSdpLine(string_view line) override;
  113. private:
  114. virtual string generateSdpLines(string_view eol) const override;
  115. int mBas = -1;
  116. struct RTPMap {
  117. RTPMap(string_view mline);
  118. void removeFB(const string &string);
  119. void addFB(const string &string);
  120. int pt;
  121. string format;
  122. int clockRate;
  123. string encParams;
  124. std::vector<string> rtcpFbs;
  125. std::vector<string> fmtps;
  126. };
  127. Media::RTPMap &getFormat(int fmt);
  128. Media::RTPMap &getFormat(const string &fmt);
  129. std::map<int, RTPMap> mRtpMap;
  130. };
  131. class Audio : public Media {
  132. public:
  133. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  134. };
  135. class Video : public Media {
  136. public:
  137. Video(string mid = "video", Direction dir = Direction::SendOnly);
  138. };
  139. bool hasApplication() const;
  140. bool hasAudioOrVideo() const;
  141. int addMedia(Media media);
  142. int addMedia(Application application);
  143. int addApplication(string mid = "data");
  144. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  145. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  146. std::variant<Media *, Application *> media(int index);
  147. std::variant<const Media *, const Application *> media(int index) const;
  148. int mediaCount() const;
  149. Application *application();
  150. private:
  151. std::shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  152. void removeApplication();
  153. Type mType;
  154. // Session-level attributes
  155. Role mRole;
  156. string mSessionId;
  157. string mIceUfrag, mIcePwd;
  158. std::optional<string> mFingerprint;
  159. // Entries
  160. std::vector<std::shared_ptr<Entry>> mEntries;
  161. std::shared_ptr<Application> mApplication;
  162. // Candidates
  163. std::vector<Candidate> mCandidates;
  164. bool mEnded = false;
  165. static Type stringToType(const string &typeString);
  166. static string typeToString(Type type);
  167. static string roleToString(Role role);
  168. };
  169. } // namespace rtc
  170. std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  171. #endif