Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SDPUtil.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. import {getLogger} from "jitsi-meet-logger";
  2. const logger = getLogger(__filename);
  3. import RandomUtil from "../util/RandomUtil";
  4. var RTCBrowserType = require("../RTC/RTCBrowserType");
  5. var SDPUtil = {
  6. filter_special_chars: function (text) {
  7. // XXX Neither one of the falsy values (e.g. null, undefined, false,
  8. // "", etc.) "contain" special chars.
  9. return text ? text.replace(/[\\\/\{,\}\+]/g, "") : text;
  10. },
  11. iceparams: function (mediadesc, sessiondesc) {
  12. var data = null;
  13. var ufrag, pwd;
  14. if ((ufrag = SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc))
  15. && (pwd = SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))) {
  16. data = {
  17. ufrag: SDPUtil.parse_iceufrag(ufrag),
  18. pwd: SDPUtil.parse_icepwd(pwd)
  19. };
  20. }
  21. return data;
  22. },
  23. parse_iceufrag: function (line) {
  24. return line.substring(12);
  25. },
  26. build_iceufrag: function (frag) {
  27. return 'a=ice-ufrag:' + frag;
  28. },
  29. parse_icepwd: function (line) {
  30. return line.substring(10);
  31. },
  32. build_icepwd: function (pwd) {
  33. return 'a=ice-pwd:' + pwd;
  34. },
  35. parse_mid: function (line) {
  36. return line.substring(6);
  37. },
  38. parse_mline: function (line) {
  39. var parts = line.substring(2).split(' '),
  40. data = {};
  41. data.media = parts.shift();
  42. data.port = parts.shift();
  43. data.proto = parts.shift();
  44. if (parts[parts.length - 1] === '') { // trailing whitespace
  45. parts.pop();
  46. }
  47. data.fmt = parts;
  48. return data;
  49. },
  50. build_mline: function (mline) {
  51. return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
  52. },
  53. parse_rtpmap: function (line) {
  54. var parts = line.substring(9).split(' '),
  55. data = {};
  56. data.id = parts.shift();
  57. parts = parts[0].split('/');
  58. data.name = parts.shift();
  59. data.clockrate = parts.shift();
  60. data.channels = parts.length ? parts.shift() : '1';
  61. return data;
  62. },
  63. /**
  64. * Parses SDP line "a=sctpmap:..." and extracts SCTP port from it.
  65. * @param line eg. "a=sctpmap:5000 webrtc-datachannel"
  66. * @returns [SCTP port number, protocol, streams]
  67. */
  68. parse_sctpmap: function (line) {
  69. var parts = line.substring(10).split(' ');
  70. var sctpPort = parts[0];
  71. var protocol = parts[1];
  72. // Stream count is optional
  73. var streamCount = parts.length > 2 ? parts[2] : null;
  74. return [sctpPort, protocol, streamCount];// SCTP port
  75. },
  76. build_rtpmap: function (el) {
  77. var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
  78. if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
  79. line += '/' + el.getAttribute('channels');
  80. }
  81. return line;
  82. },
  83. parse_crypto: function (line) {
  84. var parts = line.substring(9).split(' '),
  85. data = {};
  86. data.tag = parts.shift();
  87. data['crypto-suite'] = parts.shift();
  88. data['key-params'] = parts.shift();
  89. if (parts.length) {
  90. data['session-params'] = parts.join(' ');
  91. }
  92. return data;
  93. },
  94. parse_fingerprint: function (line) { // RFC 4572
  95. var parts = line.substring(14).split(' '),
  96. data = {};
  97. data.hash = parts.shift();
  98. data.fingerprint = parts.shift();
  99. // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
  100. return data;
  101. },
  102. parse_fmtp: function (line) {
  103. var parts = line.split(' '),
  104. i, key, value,
  105. data = [];
  106. parts.shift();
  107. parts = parts.join(' ').split(';');
  108. for (i = 0; i < parts.length; i++) {
  109. key = parts[i].split('=')[0];
  110. while (key.length && key[0] == ' ') {
  111. key = key.substring(1);
  112. }
  113. value = parts[i].split('=')[1];
  114. if (key && value) {
  115. data.push({name: key, value: value});
  116. } else if (key) {
  117. // rfc 4733 (DTMF) style stuff
  118. data.push({name: '', value: key});
  119. }
  120. }
  121. return data;
  122. },
  123. parse_icecandidate: function (line) {
  124. var candidate = {},
  125. elems = line.split(' ');
  126. candidate.foundation = elems[0].substring(12);
  127. candidate.component = elems[1];
  128. candidate.protocol = elems[2].toLowerCase();
  129. candidate.priority = elems[3];
  130. candidate.ip = elems[4];
  131. candidate.port = elems[5];
  132. // elems[6] => "typ"
  133. candidate.type = elems[7];
  134. candidate.generation = 0; // default value, may be overwritten below
  135. for (var i = 8; i < elems.length; i += 2) {
  136. switch (elems[i]) {
  137. case 'raddr':
  138. candidate['rel-addr'] = elems[i + 1];
  139. break;
  140. case 'rport':
  141. candidate['rel-port'] = elems[i + 1];
  142. break;
  143. case 'generation':
  144. candidate.generation = elems[i + 1];
  145. break;
  146. case 'tcptype':
  147. candidate.tcptype = elems[i + 1];
  148. break;
  149. default: // TODO
  150. logger.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  151. }
  152. }
  153. candidate.network = '1';
  154. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  155. return candidate;
  156. },
  157. build_icecandidate: function (cand) {
  158. var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
  159. line += ' ';
  160. switch (cand.type) {
  161. case 'srflx':
  162. case 'prflx':
  163. case 'relay':
  164. if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
  165. line += 'raddr';
  166. line += ' ';
  167. line += cand['rel-addr'];
  168. line += ' ';
  169. line += 'rport';
  170. line += ' ';
  171. line += cand['rel-port'];
  172. line += ' ';
  173. }
  174. break;
  175. }
  176. if (cand.hasOwnAttribute('tcptype')) {
  177. line += 'tcptype';
  178. line += ' ';
  179. line += cand.tcptype;
  180. line += ' ';
  181. }
  182. line += 'generation';
  183. line += ' ';
  184. line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
  185. return line;
  186. },
  187. parse_ssrc: function (desc) {
  188. // proprietary mapping of a=ssrc lines
  189. // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
  190. // and parse according to that
  191. var lines = desc.split('\r\n'),
  192. data = {};
  193. for (var i = 0; i < lines.length; i++) {
  194. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  195. var idx = lines[i].indexOf(' ');
  196. data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
  197. }
  198. }
  199. return data;
  200. },
  201. parse_rtcpfb: function (line) {
  202. var parts = line.substr(10).split(' ');
  203. var data = {};
  204. data.pt = parts.shift();
  205. data.type = parts.shift();
  206. data.params = parts;
  207. return data;
  208. },
  209. parse_extmap: function (line) {
  210. var parts = line.substr(9).split(' ');
  211. var data = {};
  212. data.value = parts.shift();
  213. if (data.value.indexOf('/') != -1) {
  214. data.direction = data.value.substr(data.value.indexOf('/') + 1);
  215. data.value = data.value.substr(0, data.value.indexOf('/'));
  216. } else {
  217. data.direction = 'both';
  218. }
  219. data.uri = parts.shift();
  220. data.params = parts;
  221. return data;
  222. },
  223. find_line: function (haystack, needle, sessionpart) {
  224. var lines = haystack.split('\r\n');
  225. for (var i = 0; i < lines.length; i++) {
  226. if (lines[i].substring(0, needle.length) == needle) {
  227. return lines[i];
  228. }
  229. }
  230. if (!sessionpart) {
  231. return false;
  232. }
  233. // search session part
  234. lines = sessionpart.split('\r\n');
  235. for (var j = 0; j < lines.length; j++) {
  236. if (lines[j].substring(0, needle.length) == needle) {
  237. return lines[j];
  238. }
  239. }
  240. return false;
  241. },
  242. find_lines: function (haystack, needle, sessionpart) {
  243. var lines = haystack.split('\r\n'),
  244. needles = [];
  245. for (var i = 0; i < lines.length; i++) {
  246. if (lines[i].substring(0, needle.length) == needle) {
  247. needles.push(lines[i]);
  248. }
  249. }
  250. if (needles.length || !sessionpart) {
  251. return needles;
  252. }
  253. // search session part
  254. lines = sessionpart.split('\r\n');
  255. for (var j = 0; j < lines.length; j++) {
  256. if (lines[j].substring(0, needle.length) == needle) {
  257. needles.push(lines[j]);
  258. }
  259. }
  260. return needles;
  261. },
  262. candidateToJingle: function (line) {
  263. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  264. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  265. if (line.indexOf('candidate:') === 0) {
  266. line = 'a=' + line;
  267. } else if (line.substring(0, 12) != 'a=candidate:') {
  268. logger.log('parseCandidate called with a line that is not a candidate line');
  269. logger.log(line);
  270. return null;
  271. }
  272. if (line.substring(line.length - 2) == '\r\n') {// chomp it
  273. line = line.substring(0, line.length - 2);
  274. }
  275. var candidate = {},
  276. elems = line.split(' '),
  277. i;
  278. if (elems[6] != 'typ') {
  279. logger.log('did not find typ in the right place');
  280. logger.log(line);
  281. return null;
  282. }
  283. candidate.foundation = elems[0].substring(12);
  284. candidate.component = elems[1];
  285. candidate.protocol = elems[2].toLowerCase();
  286. candidate.priority = elems[3];
  287. candidate.ip = elems[4];
  288. candidate.port = elems[5];
  289. // elems[6] => "typ"
  290. candidate.type = elems[7];
  291. candidate.generation = '0'; // default, may be overwritten below
  292. for (i = 8; i < elems.length; i += 2) {
  293. switch (elems[i]) {
  294. case 'raddr':
  295. candidate['rel-addr'] = elems[i + 1];
  296. break;
  297. case 'rport':
  298. candidate['rel-port'] = elems[i + 1];
  299. break;
  300. case 'generation':
  301. candidate.generation = elems[i + 1];
  302. break;
  303. case 'tcptype':
  304. candidate.tcptype = elems[i + 1];
  305. break;
  306. default: // TODO
  307. logger.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  308. }
  309. }
  310. candidate.network = '1';
  311. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  312. return candidate;
  313. },
  314. candidateFromJingle: function (cand) {
  315. var line = 'a=candidate:';
  316. line += cand.getAttribute('foundation');
  317. line += ' ';
  318. line += cand.getAttribute('component');
  319. line += ' ';
  320. var protocol = cand.getAttribute('protocol');
  321. // use tcp candidates for FF
  322. if (RTCBrowserType.isFirefox() && protocol.toLowerCase() == 'ssltcp') {
  323. protocol = 'tcp';
  324. }
  325. line += protocol; //.toUpperCase(); // chrome M23 doesn't like this
  326. line += ' ';
  327. line += cand.getAttribute('priority');
  328. line += ' ';
  329. line += cand.getAttribute('ip');
  330. line += ' ';
  331. line += cand.getAttribute('port');
  332. line += ' ';
  333. line += 'typ';
  334. line += ' ' + cand.getAttribute('type');
  335. line += ' ';
  336. switch (cand.getAttribute('type')) {
  337. case 'srflx':
  338. case 'prflx':
  339. case 'relay':
  340. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  341. line += 'raddr';
  342. line += ' ';
  343. line += cand.getAttribute('rel-addr');
  344. line += ' ';
  345. line += 'rport';
  346. line += ' ';
  347. line += cand.getAttribute('rel-port');
  348. line += ' ';
  349. }
  350. break;
  351. }
  352. if (protocol.toLowerCase() == 'tcp') {
  353. line += 'tcptype';
  354. line += ' ';
  355. line += cand.getAttribute('tcptype');
  356. line += ' ';
  357. }
  358. line += 'generation';
  359. line += ' ';
  360. line += cand.getAttribute('generation') || '0';
  361. return line + '\r\n';
  362. },
  363. /**
  364. * Parse the 'most' primary video ssrc from the given m line
  365. * @param {object} mLine object as parsed from transform.parse
  366. * @return {number} the primary video ssrc from the given m line
  367. */
  368. parsePrimaryVideoSsrc: function(videoMLine) {
  369. const numSsrcs = videoMLine.ssrcs
  370. .map(ssrcInfo => ssrcInfo.id)
  371. .filter((ssrc, index, array) => array.indexOf(ssrc) === index)
  372. .length;
  373. const numGroups = (videoMLine.ssrcGroups && videoMLine.ssrcGroups.length) || 0;
  374. if (numSsrcs > 1 && numGroups === 0) {
  375. // Ambiguous, can't figure out the primary
  376. return;
  377. }
  378. let primarySsrc = null;
  379. if (numSsrcs === 1) {
  380. primarySsrc = videoMLine.ssrcs[0].id;
  381. } else {
  382. if (numSsrcs === 2) {
  383. // Can figure it out if there's an FID group
  384. const fidGroup = videoMLine.ssrcGroups
  385. .find(group => group.semantics === "FID");
  386. if (fidGroup) {
  387. primarySsrc = fidGroup.ssrcs.split(" ")[0];
  388. }
  389. } else if (numSsrcs >= 3) {
  390. // Can figure it out if there's a sim group
  391. const simGroup = videoMLine.ssrcGroups
  392. .find(group => group.semantics === "SIM");
  393. if (simGroup) {
  394. primarySsrc = simGroup.ssrcs.split(" ")[0];
  395. }
  396. }
  397. }
  398. return primarySsrc;
  399. },
  400. /**
  401. * Generate an ssrc
  402. * @returns {number} an ssrc
  403. */
  404. generateSsrc: function() {
  405. return RandomUtil.randomInt(1, 0xffffffff);
  406. },
  407. /**
  408. * Get an attribute for the given ssrc with the given attributeName
  409. * from the given mline
  410. * @param {object} mLine an mLine object as parsed from transform.parse
  411. * @param {number} ssrc the ssrc for which an attribtue is desired
  412. * @param {string} attributeName the name of the desired attribute
  413. * @returns {string} the value corresponding to the given ssrc
  414. * and attributeName
  415. */
  416. getSsrcAttribute: function (mLine, ssrc, attributeName) {
  417. for (let i = 0; i < mLine.ssrcs.length; ++i) {
  418. const ssrcLine = mLine.ssrcs[i];
  419. if (ssrcLine.id === ssrc &&
  420. ssrcLine.attribute === attributeName) {
  421. return ssrcLine.value;
  422. }
  423. }
  424. },
  425. /**
  426. * Parses the ssrcs from the group sdp line and
  427. * returns them as a list of numbers
  428. * @param {object} the ssrcGroup object as parsed from
  429. * sdp-transform
  430. * @returns {list<number>} a list of the ssrcs in the group
  431. * parsed as numbers
  432. */
  433. parseGroupSsrcs: function (ssrcGroup) {
  434. return ssrcGroup
  435. .ssrcs
  436. .split(" ")
  437. .map(ssrcStr => parseInt(ssrcStr));
  438. },
  439. /**
  440. * Get the mline of the given type from the given sdp
  441. * @param {object} sdp sdp as parsed from transform.parse
  442. * @param {string} type the type of the desired mline (e.g. "video")
  443. * @returns {object} a media object
  444. */
  445. getMedia: function (sdp, type) {
  446. return sdp.media.find(m => m.type === type);
  447. },
  448. /**
  449. * Sets the given codecName as the preferred codec by
  450. * moving it to the beginning of the payload types
  451. * list (modifies the given mline in place). If there
  452. * are multiple options within the same codec (multiple h264
  453. * profiles, for instance), this will prefer the first one
  454. * that is found.
  455. * @param {object} videoMLine the video mline object from
  456. * an sdp as parsed by transform.parse
  457. * @param {string} the name of the preferred codec
  458. */
  459. preferVideoCodec: function(videoMLine, codecName) {
  460. let payloadType = null;
  461. for (let i = 0; i < videoMLine.rtp.length; ++i) {
  462. const rtp = videoMLine.rtp[i];
  463. if (rtp.codec === codecName) {
  464. payloadType = rtp.payload;
  465. break;
  466. }
  467. }
  468. if (payloadType) {
  469. const payloadTypes = videoMLine.payloads.split(" ").map(p => parseInt(p));
  470. const payloadIndex = payloadTypes.indexOf(payloadType);
  471. payloadTypes.splice(payloadIndex, 1);
  472. payloadTypes.unshift(payloadType);
  473. videoMLine.payloads = payloadTypes.join(" ");
  474. }
  475. },
  476. };
  477. module.exports = SDPUtil;