description.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, 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 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. protected:
  66. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  67. virtual string generateSdpLines(string_view eol) const;
  68. std::vector<string> mAttributes;
  69. private:
  70. string mType;
  71. string mDescription;
  72. string mMid;
  73. Direction mDirection;
  74. };
  75. struct Application : public Entry {
  76. public:
  77. Application(string mid = "data");
  78. Application(const Application &other) = default;
  79. Application(Application &&other) = 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. Media(const Media &other) = default;
  99. Media(Media &&other) = default;
  100. virtual ~Media() = default;
  101. string description() const override;
  102. Media reciprocate() const;
  103. void removeFormat(const string &fmt);
  104. void addVideoCodec(int payloadType, const string &codec);
  105. void addH264Codec(int payloadType);
  106. void addVP8Codec(int payloadType);
  107. void addVP9Codec(int payloadType);
  108. void setBitrate(int bitrate);
  109. int getBitrate() const;
  110. bool hasPayloadType(int payloadType) const;
  111. virtual void parseSdpLine(string_view line) override;
  112. private:
  113. virtual string generateSdpLines(string_view eol) const override;
  114. int mBas = -1;
  115. struct RTPMap {
  116. RTPMap(string_view mline);
  117. void removeFB(const string &string);
  118. void addFB(const string &string);
  119. int pt;
  120. string format;
  121. int clockRate;
  122. string encParams;
  123. std::vector<string> rtcpFbs;
  124. std::vector<string> fmtps;
  125. };
  126. Media::RTPMap &getFormat(int fmt);
  127. Media::RTPMap &getFormat(const string &fmt);
  128. std::map<int, RTPMap> mRtpMap;
  129. };
  130. class Audio : public Media {
  131. public:
  132. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  133. };
  134. class Video : public Media {
  135. public:
  136. Video(string mid = "video", Direction dir = Direction::SendOnly);
  137. };
  138. bool hasApplication() const;
  139. bool hasAudioOrVideo() const;
  140. int addMedia(Media media);
  141. int addMedia(Application application);
  142. int addApplication(string mid = "data");
  143. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  144. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  145. std::variant<Media *, Application *> media(int index);
  146. std::variant<const Media *, const Application *> media(int index) const;
  147. int mediaCount() const;
  148. Application *application();
  149. static Type stringToType(const string &typeString);
  150. static string typeToString(Type type);
  151. private:
  152. std::optional<Candidate> defaultCandidate() const;
  153. std::shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  154. void removeApplication();
  155. Type mType;
  156. // Session-level attributes
  157. Role mRole;
  158. string mUsername;
  159. string mSessionId;
  160. std::optional<string> mIceUfrag, mIcePwd;
  161. std::optional<string> mFingerprint;
  162. // Entries
  163. std::vector<std::shared_ptr<Entry>> mEntries;
  164. std::shared_ptr<Application> mApplication;
  165. // Candidates
  166. std::vector<Candidate> mCandidates;
  167. bool mEnded = false;
  168. };
  169. } // namespace rtc
  170. std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  171. std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  172. std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  173. #endif