description.hpp 4.1 KB

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