description.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "description.hpp"
  19. #include <algorithm>
  20. #include <cctype>
  21. #include <chrono>
  22. #include <random>
  23. #include <sstream>
  24. using std::size_t;
  25. using std::string;
  26. using std::chrono::system_clock;
  27. namespace {
  28. inline bool match_prefix(const string &str, const string &prefix) {
  29. return str.size() >= prefix.size() &&
  30. std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
  31. }
  32. inline void trim_end(string &str) {
  33. str.erase(
  34. std::find_if(str.rbegin(), str.rend(), [](char c) { return !std::isspace(c); }).base(),
  35. str.end());
  36. }
  37. } // namespace
  38. namespace rtc {
  39. Description::Description(const string &sdp, const string &typeString)
  40. : Description(sdp, stringToType(typeString)) {}
  41. Description::Description(const string &sdp, Type type) : Description(sdp, type, Role::ActPass) {}
  42. Description::Description(const string &sdp, Type type, Role role)
  43. : mType(Type::Unspec), mRole(role) {
  44. mData.mid = "data";
  45. hintType(type);
  46. auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
  47. std::default_random_engine generator(seed);
  48. std::uniform_int_distribution<uint32_t> uniform;
  49. mSessionId = std::to_string(uniform(generator));
  50. std::istringstream ss(sdp);
  51. std::optional<Media> currentMedia;
  52. bool finished;
  53. do {
  54. string line;
  55. finished = !std::getline(ss, line) && line.empty();
  56. trim_end(line);
  57. // Media description line (aka m-line)
  58. if (finished || match_prefix(line, "m=")) {
  59. if (currentMedia) {
  60. if (!currentMedia->mid.empty()) {
  61. if (currentMedia->type == "application")
  62. mData.mid = currentMedia->mid;
  63. else
  64. mMedia.emplace(currentMedia->mid, std::move(*currentMedia));
  65. } else if (line.find(" ICE/SDP") != string::npos) {
  66. PLOG_WARNING << "SDP \"m=\" line has no corresponding mid, ignoring";
  67. }
  68. }
  69. if (!finished)
  70. currentMedia.emplace(Media(line.substr(2)));
  71. // Attribute line
  72. } else if (match_prefix(line, "a=")) {
  73. string attr = line.substr(2);
  74. string key, value;
  75. if (size_t separator = attr.find(':'); separator != string::npos) {
  76. key = attr.substr(0, separator);
  77. value = attr.substr(separator + 1);
  78. } else {
  79. key = attr;
  80. }
  81. if (key == "mid") {
  82. if (currentMedia)
  83. currentMedia->mid = value;
  84. } else if (key == "setup") {
  85. if (value == "active")
  86. mRole = Role::Active;
  87. else if (value == "passive")
  88. mRole = Role::Passive;
  89. else
  90. mRole = Role::ActPass;
  91. } else if (key == "fingerprint") {
  92. if (match_prefix(value, "sha-256 ")) {
  93. mFingerprint = value.substr(8);
  94. std::transform(mFingerprint->begin(), mFingerprint->end(),
  95. mFingerprint->begin(), [](char c) { return std::toupper(c); });
  96. } else {
  97. PLOG_WARNING << "Unknown SDP fingerprint type: " << value;
  98. }
  99. } else if (key == "ice-ufrag") {
  100. mIceUfrag = value;
  101. } else if (key == "ice-pwd") {
  102. mIcePwd = value;
  103. } else if (key == "sctp-port") {
  104. mData.sctpPort = uint16_t(std::stoul(value));
  105. } else if (key == "max-message-size") {
  106. mData.maxMessageSize = size_t(std::stoul(value));
  107. } else if (key == "candidate") {
  108. addCandidate(Candidate(attr, currentMedia ? currentMedia->mid : mData.mid));
  109. } else if (key == "end-of-candidates") {
  110. mEnded = true;
  111. } else if (currentMedia) {
  112. currentMedia->attributes.emplace_back(line.substr(2));
  113. }
  114. }
  115. } while (!finished);
  116. }
  117. Description::Type Description::type() const { return mType; }
  118. string Description::typeString() const { return typeToString(mType); }
  119. Description::Role Description::role() const { return mRole; }
  120. string Description::roleString() const { return roleToString(mRole); }
  121. string Description::dataMid() const { return mData.mid; }
  122. std::optional<string> Description::fingerprint() const { return mFingerprint; }
  123. std::optional<uint16_t> Description::sctpPort() const { return mData.sctpPort; }
  124. std::optional<size_t> Description::maxMessageSize() const { return mData.maxMessageSize; }
  125. bool Description::ended() const { return mEnded; }
  126. void Description::hintType(Type type) {
  127. if (mType == Type::Unspec) {
  128. mType = type;
  129. if (mType == Type::Answer && mRole == Role::ActPass)
  130. mRole = Role::Passive; // ActPass is illegal for an answer, so default to passive
  131. }
  132. }
  133. void Description::setFingerprint(string fingerprint) {
  134. mFingerprint.emplace(std::move(fingerprint));
  135. }
  136. void Description::setSctpPort(uint16_t port) { mData.sctpPort.emplace(port); }
  137. void Description::setMaxMessageSize(size_t size) { mData.maxMessageSize.emplace(size); }
  138. void Description::addCandidate(Candidate candidate) {
  139. mCandidates.emplace_back(std::move(candidate));
  140. }
  141. void Description::endCandidates() { mEnded = true; }
  142. std::vector<Candidate> Description::extractCandidates() {
  143. std::vector<Candidate> result;
  144. std::swap(mCandidates, result);
  145. mEnded = false;
  146. return result;
  147. }
  148. bool Description::hasMedia() const { return !mMedia.empty(); }
  149. void Description::addMedia(const Description &source) {
  150. for (auto [mid, media] : source.mMedia)
  151. if (mid != mData.mid)
  152. mMedia.emplace(mid, media);
  153. else
  154. PLOG_WARNING << "Media mid \"" << mid << "\" is the same as data mid, ignoring";
  155. }
  156. Description::operator string() const { return generateSdp("\r\n"); }
  157. string Description::generateSdp(const string &eol) const {
  158. if (!mFingerprint)
  159. throw std::logic_error("Fingerprint must be set to generate an SDP string");
  160. std::ostringstream sdp;
  161. // Header
  162. sdp << "v=0" << eol;
  163. sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
  164. sdp << "s=-" << eol;
  165. sdp << "t=0 0" << eol;
  166. // Bundle
  167. // see Negotiating Media Multiplexing Using the Session Description Protocol
  168. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54
  169. sdp << "a=group:BUNDLE";
  170. for (const auto &[mid, _] : mMedia)
  171. sdp << " " << mid;
  172. sdp << " " << mData.mid << eol;
  173. // Data
  174. const string dataDescription = "UDP/DTLS/SCTP webrtc-datachannel";
  175. sdp << "m=application" << ' ' << (!mMedia.empty() ? 0 : 9) << ' ' << dataDescription << eol;
  176. sdp << "c=IN IP4 0.0.0.0" << eol;
  177. if (!mMedia.empty())
  178. sdp << "a=bundle-only" << eol;
  179. sdp << "a=mid:" << mData.mid << eol;
  180. if (mData.sctpPort)
  181. sdp << "a=sctp-port:" << *mData.sctpPort << eol;
  182. if (mData.maxMessageSize)
  183. sdp << "a=max-message-size:" << *mData.maxMessageSize << eol;
  184. // Non-data media
  185. if (!mMedia.empty()) {
  186. // Lip-sync
  187. sdp << "a=group:LS";
  188. for (const auto &[mid, _] : mMedia)
  189. sdp << " " << mid;
  190. sdp << eol;
  191. // Descriptions and attributes
  192. for (const auto &[_, media] : mMedia) {
  193. sdp << "m=" << media.type << ' ' << 0 << ' ' << media.description << eol;
  194. sdp << "c=IN IP4 0.0.0.0" << eol;
  195. sdp << "a=bundle-only" << eol;
  196. sdp << "a=mid:" << media.mid << eol;
  197. for (const auto &attr : media.attributes)
  198. sdp << "a=" << attr << eol;
  199. }
  200. }
  201. // Common
  202. sdp << "a=ice-options:trickle" << eol;
  203. sdp << "a=ice-ufrag:" << mIceUfrag << eol;
  204. sdp << "a=ice-pwd:" << mIcePwd << eol;
  205. sdp << "a=setup:" << roleToString(mRole) << eol;
  206. sdp << "a=dtls-id:1" << eol;
  207. if (mFingerprint)
  208. sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
  209. // Candidates
  210. for (const auto &candidate : mCandidates)
  211. sdp << string(candidate) << eol;
  212. if (mEnded)
  213. sdp << "a=end-of-candidates" << eol;
  214. return sdp.str();
  215. }
  216. Description::Media::Media(const string &mline) {
  217. size_t p = mline.find(' ');
  218. this->type = mline.substr(0, p);
  219. if (p != string::npos)
  220. if (size_t q = mline.find(' ', p + 1); q != string::npos)
  221. this->description = mline.substr(q + 1);
  222. }
  223. Description::Type Description::stringToType(const string &typeString) {
  224. if (typeString == "offer")
  225. return Type::Offer;
  226. else if (typeString == "answer")
  227. return Type::Answer;
  228. else
  229. return Type::Unspec;
  230. }
  231. string Description::typeToString(Type type) {
  232. switch (type) {
  233. case Type::Offer:
  234. return "offer";
  235. case Type::Answer:
  236. return "answer";
  237. default:
  238. return "";
  239. }
  240. }
  241. string Description::roleToString(Role role) {
  242. switch (role) {
  243. case Role::Active:
  244. return "active";
  245. case Role::Passive:
  246. return "passive";
  247. default:
  248. return "actpass";
  249. }
  250. }
  251. } // namespace rtc
  252. std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {
  253. return out << std::string(description);
  254. }