description.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. SendOnly,
  33. RecvOnly,
  34. SendRecv,
  35. Unknown
  36. };
  37. Description(const string &sdp, const string &typeString = "");
  38. Description(const string &sdp, Type type);
  39. Description(const string &sdp, Type type, Role role);
  40. Description(): Description(""){};
  41. Type type() const;
  42. string typeString() const;
  43. Role role() const;
  44. string roleString() const;
  45. string dataMid() const;
  46. string bundleMid() const;
  47. std::optional<string> fingerprint() const;
  48. std::optional<uint16_t> sctpPort() const;
  49. std::optional<size_t> maxMessageSize() const;
  50. bool ended() const;
  51. void hintType(Type type);
  52. void setDataMid(string mid);
  53. void setFingerprint(string fingerprint);
  54. void setSctpPort(uint16_t port);
  55. void setMaxMessageSize(size_t size);
  56. void addCandidate(Candidate candidate);
  57. void endCandidates();
  58. std::vector<Candidate> extractCandidates();
  59. bool hasMedia() const;
  60. void addMedia(const Description &source);
  61. operator string() const;
  62. string generateSdp(const string &eol) const;
  63. string generateDataSdp(const string &eol) const;
  64. // Data
  65. struct Data {
  66. string mid;
  67. std::optional<uint16_t> sctpPort;
  68. std::optional<size_t> maxMessageSize;
  69. };
  70. // Media (non-data)
  71. struct Media {
  72. Media(const string &lines);
  73. string type;
  74. string description;
  75. string mid;
  76. std::vector<string> attributes;
  77. std::vector<string> attributesl;
  78. int bAS = -1;
  79. struct RTPMap {
  80. RTPMap(const string &mLine);
  81. int pt;
  82. string format;
  83. int clockRate;
  84. string encParams;
  85. std::vector<string> rtcpFbs;
  86. std::vector<string> fmtps;
  87. void removeFB(const string& string);
  88. void addFB(const string& string);
  89. };
  90. std::unordered_map<int, RTPMap> rtpMap;
  91. Media::RTPMap& getFormat(int fmt);
  92. Media::RTPMap& getFormat(const string& fmt);
  93. void removeFormat(const string &fmt);
  94. Direction getDirection();
  95. void setDirection(Direction dir);
  96. void addVideoCodec(int payloadType, const string& codec);
  97. void addH264Codec(int payloadType);
  98. void addVP8Codec(int payloadType);
  99. void addVP9Codec(int payloadType);
  100. void setBitrate(int bitrate);
  101. int getBitrate() const;
  102. };
  103. std::_Rb_tree_iterator<std::pair<const int, Media>> getMedia(int mLine);
  104. Media & addAudioMedia();
  105. Media &addVideoMedia(Direction direction=Direction::RecvOnly);
  106. private:
  107. Type mType;
  108. Role mRole;
  109. string mSessionId;
  110. string mIceUfrag, mIcePwd;
  111. std::optional<string> mFingerprint;
  112. Data mData;
  113. std::map<int, Media> mMedia; // by m-line index
  114. // Candidates
  115. std::vector<Candidate> mCandidates;
  116. bool mEnded = false;
  117. static Type stringToType(const string &typeString);
  118. static string typeToString(Type type);
  119. static string roleToString(Role role);
  120. };
  121. } // namespace rtc
  122. std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  123. #endif