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