description.hpp 5.8 KB

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