description.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. int mlineIndex = 0;
  53. bool finished;
  54. do {
  55. string line;
  56. finished = !std::getline(ss, line) && line.empty();
  57. trim_end(line);
  58. // Media description line (aka m-line)
  59. if (finished || match_prefix(line, "m=")) {
  60. if (currentMedia) {
  61. if (!currentMedia->mid.empty()) {
  62. if (currentMedia->type == "application")
  63. mData.mid = currentMedia->mid;
  64. else
  65. mMedia.emplace(mlineIndex, std::move(*currentMedia));
  66. ++mlineIndex;
  67. } else if (line.find(" ICE/SDP") != string::npos) {
  68. PLOG_WARNING << "SDP \"m=\" line has no corresponding mid, ignoring";
  69. }
  70. }
  71. if (!finished)
  72. currentMedia.emplace(Media(line.substr(2)));
  73. // Attribute line
  74. } else if (match_prefix(line, "a=")) {
  75. string attr = line.substr(2);
  76. string key, value;
  77. if (size_t separator = attr.find(':'); separator != string::npos) {
  78. key = attr.substr(0, separator);
  79. value = attr.substr(separator + 1);
  80. } else {
  81. key = attr;
  82. }
  83. if (key == "mid") {
  84. if (currentMedia)
  85. currentMedia->mid = value;
  86. } else if (key == "setup") {
  87. if (value == "active")
  88. mRole = Role::Active;
  89. else if (value == "passive")
  90. mRole = Role::Passive;
  91. else
  92. mRole = Role::ActPass;
  93. } else if (key == "fingerprint") {
  94. if (match_prefix(value, "sha-256 ")) {
  95. mFingerprint = value.substr(8);
  96. std::transform(mFingerprint->begin(), mFingerprint->end(),
  97. mFingerprint->begin(),
  98. [](char c) { return char(std::toupper(c)); });
  99. } else {
  100. PLOG_WARNING << "Unknown SDP fingerprint type: " << value;
  101. }
  102. } else if (key == "ice-ufrag") {
  103. mIceUfrag = value;
  104. } else if (key == "ice-pwd") {
  105. mIcePwd = value;
  106. } else if (key == "sctp-port") {
  107. mData.sctpPort = uint16_t(std::stoul(value));
  108. } else if (key == "max-message-size") {
  109. mData.maxMessageSize = size_t(std::stoul(value));
  110. } else if (key == "candidate") {
  111. addCandidate(Candidate(attr, currentMedia ? currentMedia->mid : mData.mid));
  112. } else if (key == "end-of-candidates") {
  113. mEnded = true;
  114. } else if (currentMedia) {
  115. currentMedia->attributes.emplace_back(line.substr(2));
  116. }
  117. }
  118. } while (!finished);
  119. }
  120. Description::Type Description::type() const { return mType; }
  121. string Description::typeString() const { return typeToString(mType); }
  122. Description::Role Description::role() const { return mRole; }
  123. string Description::roleString() const { return roleToString(mRole); }
  124. string Description::dataMid() const { return mData.mid; }
  125. std::optional<string> Description::fingerprint() const { return mFingerprint; }
  126. std::optional<uint16_t> Description::sctpPort() const { return mData.sctpPort; }
  127. std::optional<size_t> Description::maxMessageSize() const { return mData.maxMessageSize; }
  128. bool Description::ended() const { return mEnded; }
  129. void Description::hintType(Type type) {
  130. if (mType == Type::Unspec) {
  131. mType = type;
  132. if (mType == Type::Answer && mRole == Role::ActPass)
  133. mRole = Role::Passive; // ActPass is illegal for an answer, so default to passive
  134. }
  135. }
  136. void Description::setDataMid(string mid) { mData.mid = mid; }
  137. void Description::setFingerprint(string fingerprint) {
  138. mFingerprint.emplace(std::move(fingerprint));
  139. }
  140. void Description::setSctpPort(uint16_t port) { mData.sctpPort.emplace(port); }
  141. void Description::setMaxMessageSize(size_t size) { mData.maxMessageSize.emplace(size); }
  142. void Description::addCandidate(Candidate candidate) {
  143. mCandidates.emplace_back(std::move(candidate));
  144. }
  145. void Description::endCandidates() { mEnded = true; }
  146. std::vector<Candidate> Description::extractCandidates() {
  147. std::vector<Candidate> result;
  148. std::swap(mCandidates, result);
  149. mEnded = false;
  150. return result;
  151. }
  152. bool Description::hasMedia() const { return !mMedia.empty(); }
  153. void Description::addMedia(const Description &source) {
  154. for (auto p : source.mMedia)
  155. mMedia.emplace(p);
  156. }
  157. Description::operator string() const { return generateSdp("\r\n"); }
  158. string Description::generateSdp(const string &eol) const {
  159. std::ostringstream sdp;
  160. // Header
  161. sdp << "v=0" << eol;
  162. sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
  163. sdp << "s=-" << eol;
  164. sdp << "t=0 0" << eol;
  165. // Bundle
  166. // see Negotiating Media Multiplexing Using the Session Description Protocol
  167. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54
  168. sdp << "a=group:BUNDLE";
  169. for (int i = 0; i < int(mMedia.size() + 1); ++i)
  170. if (auto it = mMedia.find(i); it != mMedia.end())
  171. sdp << ' ' << it->second.mid;
  172. else
  173. sdp << ' ' << mData.mid;
  174. sdp << eol;
  175. // Non-data media
  176. if (!mMedia.empty()) {
  177. // Lip-sync
  178. sdp << "a=group:LS";
  179. for (const auto &p : mMedia)
  180. sdp << " " << p.second.mid;
  181. sdp << eol;
  182. }
  183. // Session-level attributes
  184. sdp << "a=msid-semantic: WMS" << eol;
  185. sdp << "a=setup:" << roleToString(mRole) << eol;
  186. sdp << "a=ice-ufrag:" << mIceUfrag << eol;
  187. sdp << "a=ice-pwd:" << mIcePwd << eol;
  188. if (!mEnded)
  189. sdp << "a=ice-options:trickle" << eol;
  190. if (mFingerprint)
  191. sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
  192. // Media descriptions and attributes
  193. for (int i = 0; i < int(mMedia.size() + 1); ++i) {
  194. if (auto it = mMedia.find(i); it != mMedia.end()) {
  195. // Non-data media
  196. const auto &media = it->second;
  197. sdp << "m=" << media.type << ' ' << 0 << ' ' << media.description << eol;
  198. sdp << "c=IN IP4 0.0.0.0" << eol;
  199. sdp << "a=bundle-only" << eol;
  200. sdp << "a=mid:" << media.mid << eol;
  201. for (const auto &attr : media.attributes)
  202. sdp << "a=" << attr << eol;
  203. } else {
  204. // Data
  205. const string description = "UDP/DTLS/SCTP webrtc-datachannel";
  206. sdp << "m=application" << ' ' << (!mMedia.empty() ? 0 : 9) << ' ' << description << eol;
  207. sdp << "c=IN IP4 0.0.0.0" << eol;
  208. if (!mMedia.empty())
  209. sdp << "a=bundle-only" << eol;
  210. sdp << "a=mid:" << mData.mid << eol;
  211. sdp << "a=tls-id:1" << eol;
  212. if (mData.sctpPort)
  213. sdp << "a=sctp-port:" << *mData.sctpPort << eol;
  214. if (mData.maxMessageSize)
  215. sdp << "a=max-message-size:" << *mData.maxMessageSize << eol;
  216. }
  217. }
  218. // Candidates
  219. for (const auto &candidate : mCandidates)
  220. sdp << string(candidate) << eol;
  221. if (mEnded)
  222. sdp << "a=end-of-candidates" << eol;
  223. return sdp.str();
  224. }
  225. string Description::generateDataSdp(const string &eol) const {
  226. std::ostringstream sdp;
  227. // Header
  228. sdp << "v=0" << eol;
  229. sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
  230. sdp << "s=-" << eol;
  231. sdp << "t=0 0" << eol;
  232. // Data
  233. sdp << "m=application 9 UDP/DTLS/SCTP webrtc-datachannel";
  234. sdp << "c=IN IP4 0.0.0.0" << eol;
  235. sdp << "a=mid:" << mData.mid << eol;
  236. sdp << "a=tls-id:1" << eol;
  237. if (mData.sctpPort)
  238. sdp << "a=sctp-port:" << *mData.sctpPort << eol;
  239. if (mData.maxMessageSize)
  240. sdp << "a=max-message-size:" << *mData.maxMessageSize << eol;
  241. sdp << "a=setup:" << roleToString(mRole) << eol;
  242. sdp << "a=ice-ufrag:" << mIceUfrag << eol;
  243. sdp << "a=ice-pwd:" << mIcePwd << eol;
  244. if (!mEnded)
  245. sdp << "a=ice-options:trickle" << eol;
  246. if (mFingerprint)
  247. sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
  248. // Candidates
  249. for (const auto &candidate : mCandidates)
  250. sdp << string(candidate) << eol;
  251. if (mEnded)
  252. sdp << "a=end-of-candidates" << eol;
  253. return sdp.str();
  254. }
  255. Description::Media::Media(const string &mline) {
  256. size_t p = mline.find(' ');
  257. this->type = mline.substr(0, p);
  258. if (p != string::npos)
  259. if (size_t q = mline.find(' ', p + 1); q != string::npos)
  260. this->description = mline.substr(q + 1);
  261. }
  262. Description::Type Description::stringToType(const string &typeString) {
  263. if (typeString == "offer")
  264. return Type::Offer;
  265. else if (typeString == "answer")
  266. return Type::Answer;
  267. else
  268. return Type::Unspec;
  269. }
  270. string Description::typeToString(Type type) {
  271. switch (type) {
  272. case Type::Offer:
  273. return "offer";
  274. case Type::Answer:
  275. return "answer";
  276. default:
  277. return "";
  278. }
  279. }
  280. string Description::roleToString(Role role) {
  281. switch (role) {
  282. case Role::Active:
  283. return "active";
  284. case Role::Passive:
  285. return "passive";
  286. default:
  287. return "actpass";
  288. }
  289. }
  290. } // namespace rtc
  291. std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {
  292. return out << std::string(description);
  293. }