description.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. #include "description.hpp"
  20. #include <algorithm>
  21. #include <array>
  22. #include <cctype>
  23. #include <chrono>
  24. #include <iostream>
  25. #include <random>
  26. #include <sstream>
  27. using std::shared_ptr;
  28. using std::size_t;
  29. using std::string;
  30. using std::string_view;
  31. using std::chrono::system_clock;
  32. namespace {
  33. inline bool match_prefix(string_view str, string_view prefix) {
  34. return str.size() >= prefix.size() &&
  35. std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
  36. }
  37. inline void trim_end(string &str) {
  38. str.erase(
  39. std::find_if(str.rbegin(), str.rend(), [](char c) { return !std::isspace(c); }).base(),
  40. str.end());
  41. }
  42. inline std::pair<string_view, string_view> parse_pair(string_view attr) {
  43. string_view key, value;
  44. if (size_t separator = attr.find(':'); separator != string::npos) {
  45. key = attr.substr(0, separator);
  46. value = attr.substr(separator + 1);
  47. } else {
  48. key = attr;
  49. }
  50. return std::make_pair(std::move(key), std::move(value));
  51. }
  52. template <typename T> T to_integer(string_view s) {
  53. return std::is_signed<T>::value ? T(std::stol(string(s))) : T(std::stoul(string(s)));
  54. }
  55. } // namespace
  56. namespace rtc {
  57. Description::Description(const string &sdp, const string &typeString)
  58. : Description(sdp, stringToType(typeString)) {}
  59. Description::Description(const string &sdp, Type type) : Description(sdp, type, Role::ActPass) {}
  60. Description::Description(const string &sdp, Type type, Role role)
  61. : mType(Type::Unspec), mRole(role) {
  62. hintType(type);
  63. auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
  64. std::default_random_engine generator(seed);
  65. std::uniform_int_distribution<uint32_t> uniform;
  66. mSessionId = std::to_string(uniform(generator));
  67. std::istringstream ss(sdp);
  68. std::shared_ptr<Entry> current;
  69. int index = -1;
  70. string line;
  71. while (std::getline(ss, line) || !line.empty()) {
  72. trim_end(line);
  73. // Media description line (aka m-line)
  74. if (match_prefix(line, "m=")) {
  75. ++index;
  76. string mline = line.substr(2);
  77. current = createEntry(std::move(mline), std::to_string(index), Direction::Unknown);
  78. // Attribute line
  79. } else if (match_prefix(line, "a=")) {
  80. string attr = line.substr(2);
  81. auto [key, value] = parse_pair(attr);
  82. if (key == "setup") {
  83. if (value == "active")
  84. mRole = Role::Active;
  85. else if (value == "passive")
  86. mRole = Role::Passive;
  87. else
  88. mRole = Role::ActPass;
  89. } else if (key == "fingerprint") {
  90. if (match_prefix(value, "sha-256 ")) {
  91. mFingerprint = value.substr(8);
  92. std::transform(mFingerprint->begin(), mFingerprint->end(),
  93. mFingerprint->begin(),
  94. [](char c) { return char(std::toupper(c)); });
  95. } else {
  96. PLOG_WARNING << "Unknown SDP fingerprint type: " << value;
  97. }
  98. } else if (key == "ice-ufrag") {
  99. mIceUfrag = value;
  100. } else if (key == "ice-pwd") {
  101. mIcePwd = value;
  102. } else if (key == "candidate") {
  103. addCandidate(Candidate(attr, bundleMid()));
  104. } else if (key == "end-of-candidates") {
  105. mEnded = true;
  106. } else if (current) {
  107. current->parseSdpLine(std::move(line));
  108. }
  109. } else if (current) {
  110. current->parseSdpLine(std::move(line));
  111. }
  112. };
  113. }
  114. Description::Type Description::type() const { return mType; }
  115. string Description::typeString() const { return typeToString(mType); }
  116. Description::Role Description::role() const { return mRole; }
  117. string Description::roleString() const { return roleToString(mRole); }
  118. string Description::bundleMid() const {
  119. // Get the mid of the first media
  120. return !mEntries.empty() ? mEntries[0]->mid() : "0";
  121. }
  122. std::optional<string> Description::fingerprint() const { return mFingerprint; }
  123. bool Description::ended() const { return mEnded; }
  124. void Description::hintType(Type type) {
  125. if (mType == Type::Unspec) {
  126. mType = type;
  127. if (mType == Type::Answer && mRole == Role::ActPass)
  128. mRole = Role::Passive; // ActPass is illegal for an answer, so default to passive
  129. }
  130. }
  131. void Description::setFingerprint(string fingerprint) {
  132. mFingerprint.emplace(std::move(fingerprint));
  133. }
  134. void Description::addCandidate(Candidate candidate) {
  135. mCandidates.emplace_back(std::move(candidate));
  136. }
  137. void Description::endCandidates() { mEnded = true; }
  138. std::vector<Candidate> Description::extractCandidates() {
  139. std::vector<Candidate> result;
  140. std::swap(mCandidates, result);
  141. mEnded = false;
  142. return result;
  143. }
  144. Description::operator string() const { return generateSdp("\r\n"); }
  145. string Description::generateSdp(string_view eol) const {
  146. std::ostringstream sdp;
  147. // Header
  148. sdp << "v=0" << eol;
  149. sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
  150. sdp << "s=-" << eol;
  151. sdp << "t=0 0" << eol;
  152. // Bundle
  153. // see Negotiating Media Multiplexing Using the Session Description Protocol
  154. // https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54
  155. sdp << "a=group:BUNDLE";
  156. for (const auto &entry : mEntries)
  157. sdp << ' ' << entry->mid();
  158. sdp << eol;
  159. // Lip-sync
  160. std::ostringstream lsGroup;
  161. for (const auto &entry : mEntries)
  162. if (entry != mApplication)
  163. lsGroup << ' ' << entry->mid();
  164. if (!lsGroup.str().empty())
  165. sdp << "a=group:LS" << lsGroup.str() << eol;
  166. // Session-level attributes
  167. sdp << "a=msid-semantic:WMS *" << eol;
  168. sdp << "a=setup:" << roleToString(mRole) << eol;
  169. sdp << "a=ice-ufrag:" << mIceUfrag << eol;
  170. sdp << "a=ice-pwd:" << mIcePwd << eol;
  171. if (!mEnded)
  172. sdp << "a=ice-options:trickle" << eol;
  173. if (mFingerprint)
  174. sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
  175. // Entries
  176. bool first = true;
  177. for (const auto &entry : mEntries) {
  178. sdp << entry->generateSdp(eol);
  179. if (std::exchange(first, false)) {
  180. // Candidates
  181. for (const auto &candidate : mCandidates)
  182. sdp << string(candidate) << eol;
  183. if (mEnded)
  184. sdp << "a=end-of-candidates" << eol;
  185. }
  186. }
  187. return sdp.str();
  188. }
  189. string Description::generateApplicationSdp(string_view eol) const {
  190. std::ostringstream sdp;
  191. // Header
  192. sdp << "v=0" << eol;
  193. sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
  194. sdp << "s=-" << eol;
  195. sdp << "t=0 0" << eol;
  196. // Application
  197. auto app = mApplication ? mApplication : std::make_shared<Application>();
  198. sdp << app->generateSdp(eol);
  199. // Session-level attributes
  200. sdp << "a=msid-semantic:WMS *" << eol;
  201. sdp << "a=setup:" << roleToString(mRole) << eol;
  202. sdp << "a=ice-ufrag:" << mIceUfrag << eol;
  203. sdp << "a=ice-pwd:" << mIcePwd << eol;
  204. if (!mEnded)
  205. sdp << "a=ice-options:trickle" << eol;
  206. if (mFingerprint)
  207. sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
  208. // Candidates
  209. for (const auto &candidate : mCandidates)
  210. sdp << string(candidate) << eol;
  211. if (mEnded)
  212. sdp << "a=end-of-candidates" << eol;
  213. return sdp.str();
  214. }
  215. shared_ptr<Description::Entry> Description::createEntry(string mline, string mid, Direction dir) {
  216. string type = mline.substr(0, mline.find(' '));
  217. if (type == "application") {
  218. removeApplication();
  219. mApplication = std::make_shared<Application>(std::move(mid));
  220. mEntries.emplace_back(mApplication);
  221. return mApplication;
  222. } else {
  223. auto media = std::make_shared<Media>(std::move(mline), std::move(mid), dir);
  224. mEntries.emplace_back(media);
  225. return media;
  226. }
  227. }
  228. void Description::removeApplication() {
  229. if (!mApplication)
  230. return;
  231. auto it = std::find(mEntries.begin(), mEntries.end(), mApplication);
  232. if (it != mEntries.end())
  233. mEntries.erase(it);
  234. mApplication.reset();
  235. }
  236. bool Description::hasApplication() const { return mApplication != nullptr; }
  237. bool Description::hasAudioOrVideo() const {
  238. for (auto entry : mEntries)
  239. if (entry != mApplication)
  240. return true;
  241. return false;
  242. }
  243. int Description::addMedia(Media media) {
  244. mEntries.emplace_back(std::make_shared<Media>(std::move(media)));
  245. return int(mEntries.size()) - 1;
  246. }
  247. int Description::addMedia(Application application) {
  248. removeApplication();
  249. mApplication = std::make_shared<Application>(std::move(application));
  250. mEntries.emplace_back(mApplication);
  251. return int(mEntries.size()) - 1;
  252. }
  253. int Description::addApplication(string mid) { return addMedia(Application(std::move(mid))); }
  254. Description::Application *Description::application() { return mApplication.get(); }
  255. int Description::addVideo(string mid, Direction dir) {
  256. return addMedia(Video(std::move(mid), dir));
  257. }
  258. int Description::addAudio(string mid, Direction dir) {
  259. return addMedia(Audio(std::move(mid), dir));
  260. }
  261. std::variant<Description::Media *, Description::Application *> Description::media(int index) {
  262. if (index < 0 || index >= int(mEntries.size()))
  263. throw std::out_of_range("Media index out of range");
  264. const auto &entry = mEntries[index];
  265. if (entry == mApplication) {
  266. auto result = dynamic_cast<Application *>(entry.get());
  267. if (!result)
  268. throw std::logic_error("Bad type of application in description");
  269. return result;
  270. } else {
  271. auto result = dynamic_cast<Media *>(entry.get());
  272. if (!result)
  273. throw std::logic_error("Bad type of media in description");
  274. return result;
  275. }
  276. }
  277. std::variant<const Description::Media *, const Description::Application *>
  278. Description::media(int index) const {
  279. if (index < 0 || index >= int(mEntries.size()))
  280. throw std::out_of_range("Media index out of range");
  281. const auto &entry = mEntries[index];
  282. if (entry == mApplication) {
  283. auto result = dynamic_cast<Application *>(entry.get());
  284. if (!result)
  285. throw std::logic_error("Bad type of application in description");
  286. return result;
  287. } else {
  288. auto result = dynamic_cast<Media *>(entry.get());
  289. if (!result)
  290. throw std::logic_error("Bad type of media in description");
  291. return result;
  292. }
  293. }
  294. int Description::mediaCount() const { return int(mEntries.size()); }
  295. Description::Entry::Entry(const string &mline, string mid, Direction dir)
  296. : mMid(std::move(mid)), mDirection(dir) {
  297. unsigned int port;
  298. std::istringstream ss(mline);
  299. ss >> mType;
  300. ss >> port; // ignored
  301. ss >> mDescription;
  302. }
  303. void Description::Entry::setDirection(Direction dir) { mDirection = dir; }
  304. Description::Entry::operator string() const { return generateSdp("\r\n"); }
  305. string Description::Entry::generateSdp(string_view eol) const {
  306. std::ostringstream sdp;
  307. // Port 9 is the discard protocol
  308. sdp << "m=" << type() << ' ' << 9 << ' ' << description() << eol;
  309. sdp << "c=IN IP4 0.0.0.0" << eol;
  310. sdp << generateSdpLines(eol);
  311. return sdp.str();
  312. }
  313. string Description::Entry::generateSdpLines(string_view eol) const {
  314. std::ostringstream sdp;
  315. sdp << "a=bundle-only" << eol;
  316. sdp << "a=mid:" << mMid << eol;
  317. switch (mDirection) {
  318. case Direction::SendOnly:
  319. sdp << "a=sendonly" << eol;
  320. break;
  321. case Direction::RecvOnly:
  322. sdp << "a=recvonly" << eol;
  323. break;
  324. case Direction::SendRecv:
  325. sdp << "a=sendrecv" << eol;
  326. break;
  327. case Direction::Inactive:
  328. sdp << "a=inactive" << eol;
  329. break;
  330. default:
  331. // Ignore
  332. break;
  333. }
  334. for (const auto &attr : mAttributes)
  335. sdp << "a=" << attr << eol;
  336. return sdp.str();
  337. }
  338. void Description::Entry::parseSdpLine(string_view line) {
  339. if (match_prefix(line, "a=")) {
  340. string_view attr = line.substr(2);
  341. auto [key, value] = parse_pair(attr);
  342. if (key == "mid")
  343. mMid = value;
  344. else if (attr == "sendonly")
  345. mDirection = Direction::SendOnly;
  346. else if (attr == "recvonly")
  347. mDirection = Direction::RecvOnly;
  348. else if (key == "sendrecv")
  349. mDirection = Direction::SendRecv;
  350. else if (key == "inactive")
  351. mDirection = Direction::Inactive;
  352. else if (key == "bundle-only") {
  353. // always added
  354. } else
  355. mAttributes.emplace_back(line.substr(2));
  356. }
  357. }
  358. void Description::Entry::addSSRC(uint32_t ssrc, std::string name) {
  359. mAttributes.emplace_back("ssrc:" + std::to_string(ssrc) + " cname:" + name);
  360. }
  361. Description::Application::Application(string mid)
  362. : Entry("application 9 UDP/DTLS/SCTP", std::move(mid), Direction::SendRecv) {}
  363. string Description::Application::description() const {
  364. return Entry::description() + " webrtc-datachannel";
  365. }
  366. Description::Application Description::Application::reciprocate() const {
  367. Application reciprocated(*this);
  368. reciprocated.mMaxMessageSize.reset();
  369. return reciprocated;
  370. }
  371. string Description::Application::generateSdpLines(string_view eol) const {
  372. std::ostringstream sdp;
  373. sdp << Entry::generateSdpLines(eol);
  374. if (mSctpPort)
  375. sdp << "a=sctp-port:" << *mSctpPort << eol;
  376. if (mMaxMessageSize)
  377. sdp << "a=max-message-size:" << *mMaxMessageSize << eol;
  378. return sdp.str();
  379. }
  380. void Description::Application::parseSdpLine(string_view line) {
  381. if (match_prefix(line, "a=")) {
  382. string_view attr = line.substr(2);
  383. auto [key, value] = parse_pair(attr);
  384. if (key == "sctp-port") {
  385. mSctpPort = to_integer<uint16_t>(value);
  386. } else if (key == "max-message-size") {
  387. mMaxMessageSize = to_integer<size_t>(value);
  388. } else {
  389. Entry::parseSdpLine(line);
  390. }
  391. } else {
  392. Entry::parseSdpLine(line);
  393. }
  394. }
  395. Description::Media::Media(const string &sdp) : Entry(sdp, "", Direction::Unknown) {
  396. std::istringstream ss(sdp);
  397. string line;
  398. while (std::getline(ss, line) || !line.empty()) {
  399. trim_end(line);
  400. parseSdpLine(line);
  401. }
  402. if (mid().empty())
  403. throw std::invalid_argument("Missing mid in media SDP");
  404. }
  405. Description::Media::Media(const string &mline, string mid, Direction dir)
  406. : Entry(mline, std::move(mid), dir) {
  407. }
  408. string Description::Media::description() const {
  409. std::ostringstream desc;
  410. desc << Entry::description();
  411. for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it)
  412. desc << ' ' << it->first;
  413. return desc.str();
  414. }
  415. Description::Media Description::Media::reciprocate() const {
  416. Media reciprocated(*this);
  417. // Invert direction
  418. switch (direction()) {
  419. case Direction::RecvOnly:
  420. reciprocated.setDirection(Direction::SendOnly);
  421. break;
  422. case Direction::SendOnly:
  423. reciprocated.setDirection(Direction::RecvOnly);
  424. break;
  425. default:
  426. // We are good
  427. break;
  428. }
  429. return reciprocated;
  430. }
  431. Description::Media::RTPMap &Description::Media::getFormat(int fmt) {
  432. auto it = mRtpMap.find(fmt);
  433. if (it != mRtpMap.end())
  434. return it->second;
  435. throw std::invalid_argument("m-line index is out of bounds");
  436. }
  437. Description::Media::RTPMap &Description::Media::getFormat(const string &fmt) {
  438. for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it)
  439. if (it->second.format == fmt)
  440. return it->second;
  441. throw std::invalid_argument("format was not found");
  442. }
  443. void Description::Media::removeFormat(const string &fmt) {
  444. auto it = mRtpMap.begin();
  445. std::vector<int> remed;
  446. // Remove the actual formats
  447. while (it != mRtpMap.end()) {
  448. if (it->second.format == fmt) {
  449. remed.emplace_back(it->first);
  450. it = mRtpMap.erase(it);
  451. } else {
  452. it++;
  453. }
  454. }
  455. // Remove any other rtpmaps that depend on the formats we just removed
  456. it = mRtpMap.begin();
  457. while (it != mRtpMap.end()) {
  458. auto it2 = it->second.fmtps.begin();
  459. bool rem = false;
  460. while (it2 != it->second.fmtps.end()) {
  461. if (it2->find("apt=") == 0) {
  462. for (auto remid : remed) {
  463. if (it2->find(std::to_string(remid)) != string::npos) {
  464. std::cout << *it2 << ' ' << remid << std::endl;
  465. it = mRtpMap.erase(it);
  466. rem = true;
  467. break;
  468. }
  469. }
  470. break;
  471. }
  472. it2++;
  473. }
  474. if (!rem)
  475. it++;
  476. }
  477. }
  478. void Description::Media::addVideoCodec(int payloadType, const string &codec) {
  479. RTPMap map(std::to_string(payloadType) + ' ' + codec + "/90000");
  480. map.addFB("nack");
  481. map.addFB("goog-remb");
  482. if (codec == "H264") {
  483. // Use Constrained Baseline profile Level 4.2 (necessary for Firefox)
  484. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
  485. // TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
  486. map.fmtps.emplace_back("profile-level-id=42E02A;level-asymmetry-allowed=1");
  487. }
  488. mRtpMap.emplace(map.pt, map);
  489. }
  490. void Description::Media::addH264Codec(int pt) { addVideoCodec(pt, "H264"); }
  491. void Description::Media::addVP8Codec(int payloadType) { addVideoCodec(payloadType, "VP8"); }
  492. void Description::Media::addVP9Codec(int payloadType) { addVideoCodec(payloadType, "VP9"); }
  493. void Description::Media::setBitrate(int bitrate) { mBas = bitrate; }
  494. int Description::Media::getBitrate() const { return mBas; }
  495. bool Description::Media::hasPayloadType(int payloadType) const {
  496. return mRtpMap.find(payloadType) != mRtpMap.end();
  497. }
  498. string Description::Media::generateSdpLines(string_view eol) const {
  499. std::ostringstream sdp;
  500. if (mBas >= 0)
  501. sdp << "b=AS:" << mBas << eol;
  502. sdp << Entry::generateSdpLines(eol);
  503. sdp << "a=rtcp-mux" << eol;
  504. for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it) {
  505. auto &map = it->second;
  506. // Create the a=rtpmap
  507. sdp << "a=rtpmap:" << map.pt << ' ' << map.format << '/' << map.clockRate;
  508. if (!map.encParams.empty())
  509. sdp << '/' << map.encParams;
  510. sdp << eol;
  511. for (const auto &val : map.rtcpFbs)
  512. sdp << "a=rtcp-fb:" << map.pt << ' ' << val << eol;
  513. for (const auto &val : map.fmtps)
  514. sdp << "a=fmtp:" << map.pt << ' ' << val << eol;
  515. }
  516. return sdp.str();
  517. }
  518. void Description::Media::parseSdpLine(string_view line) {
  519. if (match_prefix(line, "a=")) {
  520. string_view attr = line.substr(2);
  521. auto [key, value] = parse_pair(attr);
  522. if (key == "rtpmap") {
  523. Description::Media::RTPMap map(value);
  524. int pt = map.pt;
  525. mRtpMap.emplace(pt, std::move(map));
  526. } else if (key == "rtcp-fb") {
  527. size_t p = value.find(' ');
  528. int pt = to_integer<int>(value.substr(0, p));
  529. auto it = mRtpMap.find(pt);
  530. if (it == mRtpMap.end()) {
  531. PLOG_WARNING << "rtcp-fb applied before the corresponding rtpmap, ignoring";
  532. } else {
  533. it->second.rtcpFbs.emplace_back(value.substr(p + 1));
  534. }
  535. } else if (key == "fmtp") {
  536. size_t p = value.find(' ');
  537. int pt = to_integer<int>(value.substr(0, p));
  538. auto it = mRtpMap.find(pt);
  539. if (it == mRtpMap.end()) {
  540. PLOG_WARNING << "fmtp applied before the corresponding rtpmap, ignoring";
  541. } else {
  542. it->second.fmtps.emplace_back(value.substr(p + 1));
  543. }
  544. } else if (key == "rtcp-mux") {
  545. // always added
  546. } else {
  547. Entry::parseSdpLine(line);
  548. }
  549. } else if (match_prefix(line, "b=AS")) {
  550. mBas = to_integer<int>(line.substr(line.find(':') + 1));
  551. } else {
  552. Entry::parseSdpLine(line);
  553. }
  554. }
  555. Description::Media::RTPMap::RTPMap(string_view mline) {
  556. size_t p = mline.find(' ');
  557. this->pt = to_integer<int>(mline.substr(0, p));
  558. string_view line = mline.substr(p + 1);
  559. size_t spl = line.find('/');
  560. this->format = line.substr(0, spl);
  561. line = line.substr(spl + 1);
  562. spl = line.find('/');
  563. if (spl == string::npos) {
  564. spl = line.find(' ');
  565. }
  566. if (spl == string::npos)
  567. this->clockRate = to_integer<int>(line);
  568. else {
  569. this->clockRate = to_integer<int>(line.substr(0, spl));
  570. this->encParams = line.substr(spl);
  571. }
  572. }
  573. void Description::Media::RTPMap::removeFB(const string &str) {
  574. auto it = rtcpFbs.begin();
  575. while (it != rtcpFbs.end()) {
  576. if (it->find(str) != std::string::npos) {
  577. it = rtcpFbs.erase(it);
  578. } else
  579. it++;
  580. }
  581. }
  582. void Description::Media::RTPMap::addFB(const string &str) { rtcpFbs.emplace_back(str); }
  583. Description::Audio::Audio(string mid, Direction dir)
  584. : Media("audio 9 UDP/TLS/RTP/SAVPF", std::move(mid), dir) {}
  585. Description::Video::Video(string mid, Direction dir)
  586. : Media("video 9 UDP/TLS/RTP/SAVPF", std::move(mid), dir) {}
  587. Description::Type Description::stringToType(const string &typeString) {
  588. if (typeString == "offer")
  589. return Type::Offer;
  590. else if (typeString == "answer")
  591. return Type::Answer;
  592. else
  593. return Type::Unspec;
  594. }
  595. string Description::typeToString(Type type) {
  596. switch (type) {
  597. case Type::Offer:
  598. return "offer";
  599. case Type::Answer:
  600. return "answer";
  601. default:
  602. return "";
  603. }
  604. }
  605. string Description::roleToString(Role role) {
  606. switch (role) {
  607. case Role::Active:
  608. return "active";
  609. case Role::Passive:
  610. return "passive";
  611. default:
  612. return "actpass";
  613. }
  614. }
  615. } // namespace rtc
  616. std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {
  617. return out << std::string(description);
  618. }