description.hpp 5.7 KB

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