description.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 {
  39. SendOnly = RTC_DIRECTION_SENDONLY,
  40. RecvOnly = RTC_DIRECTION_RECVONLY,
  41. SendRecv = RTC_DIRECTION_SENDRECV,
  42. Inactive = RTC_DIRECTION_INACTIVE,
  43. Unknown = RTC_DIRECTION_UNKNOWN
  44. };
  45. Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
  46. Description(const string &sdp, string typeString);
  47. Type type() const;
  48. string typeString() const;
  49. Role role() const;
  50. string bundleMid() const;
  51. optional<string> iceUfrag() const;
  52. optional<string> icePwd() const;
  53. optional<string> fingerprint() const;
  54. bool ended() const;
  55. void hintType(Type type);
  56. void setFingerprint(string fingerprint);
  57. bool hasCandidate(const Candidate &candidate) const;
  58. void addCandidate(Candidate candidate);
  59. void addCandidates(std::vector<Candidate> candidates);
  60. void endCandidates();
  61. std::vector<Candidate> extractCandidates();
  62. operator string() const;
  63. string generateSdp(string_view eol) const;
  64. string generateApplicationSdp(string_view eol) const;
  65. class RTC_CPP_EXPORT Entry {
  66. public:
  67. virtual ~Entry() = default;
  68. virtual string type() const { return mType; }
  69. virtual string description() const { return mDescription; }
  70. virtual string mid() const { return mMid; }
  71. Direction direction() const { return mDirection; }
  72. void setDirection(Direction dir);
  73. operator string() const;
  74. string generateSdp(string_view eol, string_view addr, string_view port) const;
  75. virtual void parseSdpLine(string_view line);
  76. std::vector<string>::iterator beginAttributes();
  77. std::vector<string>::iterator endAttributes();
  78. std::vector<string>::iterator removeAttribute(std::vector<string>::iterator iterator);
  79. protected:
  80. Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
  81. virtual string generateSdpLines(string_view eol) const;
  82. std::vector<string> mAttributes;
  83. private:
  84. string mType;
  85. string mDescription;
  86. string mMid;
  87. Direction mDirection;
  88. };
  89. struct RTC_CPP_EXPORT Application : public Entry {
  90. public:
  91. Application(string mid = "data");
  92. virtual ~Application() = default;
  93. string description() const override;
  94. Application reciprocate() const;
  95. void setSctpPort(uint16_t port) { mSctpPort = port; }
  96. void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
  97. void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
  98. optional<uint16_t> sctpPort() const { return mSctpPort; }
  99. optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
  100. virtual void parseSdpLine(string_view line) override;
  101. private:
  102. virtual string generateSdpLines(string_view eol) const override;
  103. optional<uint16_t> mSctpPort;
  104. optional<size_t> mMaxMessageSize;
  105. };
  106. // Media (non-data)
  107. class RTC_CPP_EXPORT Media : public Entry {
  108. public:
  109. Media(const string &sdp);
  110. Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
  111. virtual ~Media() = default;
  112. string description() const override;
  113. Media reciprocate() const;
  114. void removeFormat(const string &fmt);
  115. void addSSRC(uint32_t ssrc, optional<string> name,
  116. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  117. void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, optional<string> name,
  118. optional<string> msid = nullopt, optional<string> trackID = nullopt);
  119. bool hasSSRC(uint32_t ssrc);
  120. std::vector<uint32_t> getSSRCs();
  121. std::optional<std::string> getCNameForSsrc(uint32_t ssrc);
  122. void setBitrate(int bitrate);
  123. int getBitrate() const;
  124. bool hasPayloadType(int payloadType) const;
  125. void addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
  126. unsigned int clockRate);
  127. virtual void parseSdpLine(string_view line) override;
  128. struct RTPMap {
  129. RTPMap(string_view mline);
  130. RTPMap() {}
  131. void removeFB(const string &string);
  132. void addFB(const string &string);
  133. void addAttribute(string attr) { fmtps.emplace_back(std::move(attr)); }
  134. int pt;
  135. string format;
  136. int clockRate;
  137. string encParams;
  138. std::vector<string> rtcpFbs;
  139. std::vector<string> fmtps;
  140. static int parsePT(string_view view);
  141. void setMLine(string_view view);
  142. };
  143. std::map<int, RTPMap>::iterator beginMaps();
  144. std::map<int, RTPMap>::iterator endMaps();
  145. std::map<int, RTPMap>::iterator removeMap(std::map<int, RTPMap>::iterator iterator);
  146. private:
  147. virtual string generateSdpLines(string_view eol) const override;
  148. int mBas = -1;
  149. Media::RTPMap &getFormat(int fmt);
  150. Media::RTPMap &getFormat(const string &fmt);
  151. std::map<int, RTPMap> mRtpMap;
  152. std::vector<uint32_t> mSsrcs;
  153. std::map<uint32_t, string> mCNameMap;
  154. public:
  155. void addRTPMap(const RTPMap &map);
  156. void removeSSRC(uint32_t oldSSRC);
  157. };
  158. class RTC_CPP_EXPORT Audio : public Media {
  159. public:
  160. Audio(string mid = "audio", Direction dir = Direction::SendOnly);
  161. void addAudioCodec(int payloadType, string codec,
  162. optional<string> profile = std::nullopt);
  163. void addOpusCodec(int payloadType,
  164. optional<string> profile = DEFAULT_OPUS_AUDIO_PROFILE);
  165. };
  166. class RTC_CPP_EXPORT Video : public Media {
  167. public:
  168. Video(string mid = "video", Direction dir = Direction::SendOnly);
  169. void addVideoCodec(int payloadType, string codec,
  170. optional<string> profile = std::nullopt);
  171. void addH264Codec(int payloadType,
  172. optional<string> profile = DEFAULT_H264_VIDEO_PROFILE);
  173. void addVP8Codec(int payloadType);
  174. void addVP9Codec(int payloadType);
  175. };
  176. bool hasApplication() const;
  177. bool hasAudioOrVideo() const;
  178. bool hasMid(string_view mid) const;
  179. int addMedia(Media media);
  180. int addMedia(Application application);
  181. int addApplication(string mid = "data");
  182. int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
  183. int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
  184. void clearMedia();
  185. variant<Media *, Application *> media(unsigned int index);
  186. variant<const Media *, const Application *> media(unsigned int index) const;
  187. unsigned int mediaCount() const;
  188. const Application *application() const;
  189. Application *application();
  190. static Type stringToType(const string &typeString);
  191. static string typeToString(Type type);
  192. private:
  193. optional<Candidate> defaultCandidate() const;
  194. shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
  195. void removeApplication();
  196. Type mType;
  197. // Session-level attributes
  198. Role mRole;
  199. string mUsername;
  200. string mSessionId;
  201. optional<string> mIceUfrag, mIcePwd;
  202. optional<string> mFingerprint;
  203. // Entries
  204. std::vector<shared_ptr<Entry>> mEntries;
  205. shared_ptr<Application> mApplication;
  206. // Candidates
  207. std::vector<Candidate> mCandidates;
  208. bool mEnded = false;
  209. };
  210. } // namespace rtc
  211. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
  212. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
  213. RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
  214. #endif