description.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2020 Staz Modrzynski
  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 "common.hpp"
  23. #include <iostream>
  24. #include <map>
  25. #include <vector>
  26. namespace rtc {
  27. const string DEFAULT_OPUS_AUDIO_PROFILE =
  28. "minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
  29. // Use Constrained Baseline profile Level 4.2 (necessary for Firefox)
  30. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  31. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  32. const string DEFAULT_H264_VIDEO_PROFILE =
  33. "profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
  34. class RTC_CPP_EXPORT Description {
  35. public:
  36. enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
  37. enum class Role { ActPass, Passive, Active };
  38. enum class Direction { SendOnly, RecvOnly, SendRecv, Inactive, Unknown };
  39. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  40. Description(const string &sdp, string typeString);
  41. Type type() const;
  42. string typeString() const;
  43. Role role() const;
  44. string bundleMid() const;
  45. optional<string> iceUfrag() const;
  46. optional<string> icePwd() const;
  47. optional<string> fingerprint() const;
  48. bool ended() const;
  49. void hintType(Type type);
  50. void setFingerprint(string fingerprint);
  51. bool hasCandidate(const Candidate &candidate) const;
  52. void addCandidate(Candidate candidate);
  53. void addCandidates(std::vector<Candidate> candidates);
  54. void endCandidates();
  55. std::vector<Candidate> extractCandidates();
  56. operator string() const;
  57. string generateSdp(string_view eol) const;
  58. string generateApplicationSdp(string_view eol) const;
  59. class RTC_CPP_EXPORT Entry {
  60. public:
  61. virtual ~Entry() = default;
  62. virtual string type() const { return mType; }
  63. virtual string description() const { return mDescription; }
  64. virtual string mid() const { return mMid; }
  65. Direction direction() const { return mDirection; }
  66. void setDirection(Direction dir);
  67. operator string() const;
  68. string generateSdp(string_view eol, string_view addr, string_view port) const;
  69. virtual void parseSdpLine(string_view line);
  70. std::vector<string>::iterator beginAttributes();
  71. std::vector<string>::iterator endAttributes();
  72. std::vector<string>::iterator removeAttribute(std::vector<string>::iterator iterator);
  73. protected:
  74. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  75. virtual string generateSdpLines(string_view eol) const;
  76. std::vector<string> mAttributes;
  77. private:
  78. string mType;
  79. string mDescription;
  80. string mMid;
  81. Direction mDirection;
  82. };
  83. struct RTC_CPP_EXPORT Application : public Entry {
  84. public:
  85. Application(string mid = "data");
  86. virtual ~Application() = default;
  87. string description() const override;
  88. Application reciprocate() const;
  89. void setSctpPort(uint16_t port) { mSctpPort = port; }
  90. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  91. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  92. optional<uint16_t> sctpPort() const { return mSctpPort; }
  93. optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  94. virtual void parseSdpLine(string_view line) override;
  95. private:
  96. virtual string generateSdpLines(string_view eol) const override;
  97. optional<uint16_t> mSctpPort;
  98. optional<size_t> mMaxMessageSize;
  99. };
  100. // Media (non-data)
  101. class RTC_CPP_EXPORT Media : public Entry {
  102. public:
  103. Media(const string &sdp);
  104. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  105. virtual ~Media() = default;
  106. string description() const override;
  107. Media reciprocate() const;
  108. void removeFormat(const string &fmt);
  109. void addSSRC(uint32_t ssrc, optional<string> name,
  110. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  111. void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, optional<string> name,
  112. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  113. bool hasSSRC(uint32_t ssrc);
  114. std::vector<uint32_t> getSSRCs();
  115. void setBitrate(int bitrate);
  116. int getBitrate() const;
  117. bool hasPayloadType(int payloadType) const;
  118. void addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
  119. unsigned int clockRate);
  120. virtual void parseSdpLine(string_view line) override;
  121. struct RTPMap {
  122. RTPMap(string_view mline);
  123. RTPMap() {}
  124. void removeFB(const string &string);
  125. void addFB(const string &string);
  126. void addAttribute(string attr) { fmtps.emplace_back(std::move(attr)); }
  127. int pt;
  128. string format;
  129. int clockRate;
  130. string encParams;
  131. std::vector<string> rtcpFbs;
  132. std::vector<string> fmtps;
  133. static int parsePT(string_view view);
  134. void setMLine(string_view view);
  135. };
  136. std::map<int, RTPMap>::iterator beginMaps();
  137. std::map<int, RTPMap>::iterator endMaps();
  138. std::map<int, RTPMap>::iterator removeMap(std::map<int, RTPMap>::iterator iterator);
  139. private:
  140. virtual string generateSdpLines(string_view eol) const override;
  141. int mBas = -1;
  142. Media::RTPMap &getFormat(int fmt);
  143. Media::RTPMap &getFormat(const string &fmt);
  144. std::map<int, RTPMap> mRtpMap;
  145. std::vector<uint32_t> mSsrcs;
  146. public:
  147. void addRTPMap(const RTPMap &map);
  148. void removeSSRC(uint32_t oldSSRC);
  149. };
  150. class RTC_CPP_EXPORT Audio : public Media {
  151. public:
  152. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  153. void addAudioCodec(int payloadType, string codec,
  154. optional<string> profile = std::nullopt);
  155. void addOpusCodec(int payloadType,
  156. optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  157. };
  158. class RTC_CPP_EXPORT Video : public Media {
  159. public:
  160. Video(string mid = "video", Direction dir = Direction::SendOnly);
  161. void addVideoCodec(int payloadType, string codec,
  162. optional<string> profile = std::nullopt);
  163. void addH264Codec(int payloadType,
  164. optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  165. void addVP8Codec(int payloadType);
  166. void addVP9Codec(int payloadType);
  167. };
  168. bool hasApplication() const;
  169. bool hasAudioOrVideo() const;
  170. bool hasMid(string_view mid) const;
  171. int addMedia(Media media);
  172. int addMedia(Application application);
  173. int addApplication(string mid = "data");
  174. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  175. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  176. variant<Media *, Application *> media(unsigned int index);
  177. variant<const Media *, const Application *> media(unsigned int index) const;
  178. unsigned int mediaCount() const;
  179. Application *application();
  180. static Type stringToType(const string &typeString);
  181. static string typeToString(Type type);
  182. private:
  183. optional<Candidate> defaultCandidate() const;
  184. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  185. void removeApplication();
  186. Type mType;
  187. // Session-level attributes
  188. Role mRole;
  189. string mUsername;
  190. string mSessionId;
  191. optional<string> mIceUfrag, mIcePwd;
  192. optional<string> mFingerprint;
  193. // Entries
  194. std::vector<shared_ptr<Entry>> mEntries;
  195. shared_ptr<Application> mApplication;
  196. // Candidates
  197. std::vector<Candidate> mCandidates;
  198. bool mEnded = false;
  199. };
  200. } // namespace rtc
  201. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  202. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  203. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  204. #endif