description.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_DESCRIPTION_H
  19. #define RTC_DESCRIPTION_H
  20. #include "candidate.hpp"
  21. #include "include.hpp"
  22. #include <iostream>
  23. #include <map>
  24. #include <optional>
  25. #include <vector>
  26. namespace rtc {
  27. class Description {
  28. public:
  29. enum class Type { Unspec = 0, Offer = 1, Answer = 2 };
  30. enum class Role { ActPass = 0, Passive = 1, Active = 2 };
  31. enum Direction {
  32. SEND_ONLY,
  33. RECV_ONLY,
  34. BOTH
  35. };
  36. Description(const string &sdp, const string &typeString = "");
  37. Description(const string &sdp, Type type);
  38. Description(const string &sdp, Type type, Role role);
  39. Type type() const;
  40. string typeString() const;
  41. Role role() const;
  42. string roleString() const;
  43. string dataMid() const;
  44. string bundleMid() const;
  45. std::optional<string> fingerprint() const;
  46. std::optional<uint16_t> sctpPort() const;
  47. std::optional<size_t> maxMessageSize() const;
  48. bool ended() const;
  49. void hintType(Type type);
  50. void setDataMid(string mid);
  51. void setFingerprint(string fingerprint);
  52. void setSctpPort(uint16_t port);
  53. void setMaxMessageSize(size_t size);
  54. void addCandidate(Candidate candidate);
  55. void endCandidates();
  56. std::vector<Candidate> extractCandidates();
  57. bool hasMedia() const;
  58. void addMedia(const Description &source);
  59. operator string() const;
  60. string generateSdp(const string &eol) const;
  61. string generateDataSdp(const string &eol) const;
  62. // Data
  63. struct Data {
  64. string mid;
  65. std::optional<uint16_t> sctpPort;
  66. std::optional<size_t> maxMessageSize;
  67. };
  68. // Media (non-data)
  69. struct Media {
  70. Media(const string &lines);
  71. string type;
  72. string description;
  73. string mid;
  74. std::vector<string> attributes;
  75. std::vector<string> attributesl;
  76. int bAS = -1;
  77. struct RTPMap {
  78. RTPMap(const string &mLine);
  79. int pt;
  80. string format;
  81. int clockRate;
  82. string encParams;
  83. std::vector<string> rtcpFbs;
  84. std::vector<string> fmtps;
  85. void removeFB(const char *string);
  86. };
  87. std::unordered_map<int, RTPMap> rtpMap;
  88. Media::RTPMap& getFormat(int fmt);
  89. Media::RTPMap& getFormat(const string& fmt);
  90. Direction getDirection() {
  91. for (auto attr : attributes) {
  92. if (attr == "sendrecv")
  93. return Direction::BOTH;
  94. if (attr == "recvonly")
  95. return Direction::RECV_ONLY;
  96. if (attr == "sendonly")
  97. return Direction::SEND_ONLY;
  98. }
  99. }
  100. void setDirection(Direction dir) {
  101. auto it = attributes.begin();
  102. while (it != attributes.end()) {
  103. if (*it == "sendrecv" || *it == "sendonly" || *it == "recvonly")
  104. it = attributes.erase(it);
  105. else
  106. it++;
  107. }
  108. if (dir == Direction::BOTH)
  109. attributes.emplace(attributes.begin(), "sendrecv");
  110. else if (dir == Direction::RECV_ONLY)
  111. attributes.emplace(attributes.begin(), "recvonly");
  112. if (dir == Direction::SEND_ONLY)
  113. attributes.emplace(attributes.begin(), "sendonly");
  114. }
  115. void removeFormat(const string &fmt);
  116. void addH264Codec(int pt);
  117. };
  118. std::_Rb_tree_iterator<std::pair<const int, Media>> getMedia(int mLine);
  119. Media & addAudioMedia();
  120. Media &addVideoMedia(bool direction);
  121. private:
  122. Type mType;
  123. Role mRole;
  124. string mSessionId;
  125. string mIceUfrag, mIcePwd;
  126. std::optional<string> mFingerprint;
  127. Data mData;
  128. std::map<int, Media> mMedia; // by m-line index
  129. // Candidates
  130. std::vector<Candidate> mCandidates;
  131. bool mEnded = false;
  132. static Type stringToType(const string &typeString);
  133. static string typeToString(Type type);
  134. static string roleToString(Role role);
  135. };
  136. } // namespace rtc
  137. std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  138. #endif