您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SDPUtil.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. SDPUtil = {
  2. filter_special_chars: function (text) {
  3. return text.replace(/[\\\/\{,\}\+]/g, "");
  4. },
  5. iceparams: function (mediadesc, sessiondesc) {
  6. var data = null;
  7. if (SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc) &&
  8. SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc)) {
  9. data = {
  10. ufrag: SDPUtil.parse_iceufrag(SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc)),
  11. pwd: SDPUtil.parse_icepwd(SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))
  12. };
  13. }
  14. return data;
  15. },
  16. parse_iceufrag: function (line) {
  17. return line.substring(12);
  18. },
  19. build_iceufrag: function (frag) {
  20. return 'a=ice-ufrag:' + frag;
  21. },
  22. parse_icepwd: function (line) {
  23. return line.substring(10);
  24. },
  25. build_icepwd: function (pwd) {
  26. return 'a=ice-pwd:' + pwd;
  27. },
  28. parse_mid: function (line) {
  29. return line.substring(6);
  30. },
  31. parse_mline: function (line) {
  32. var parts = line.substring(2).split(' '),
  33. data = {};
  34. data.media = parts.shift();
  35. data.port = parts.shift();
  36. data.proto = parts.shift();
  37. if (parts[parts.length - 1] === '') { // trailing whitespace
  38. parts.pop();
  39. }
  40. data.fmt = parts;
  41. return data;
  42. },
  43. build_mline: function (mline) {
  44. return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
  45. },
  46. parse_rtpmap: function (line) {
  47. var parts = line.substring(9).split(' '),
  48. data = {};
  49. data.id = parts.shift();
  50. parts = parts[0].split('/');
  51. data.name = parts.shift();
  52. data.clockrate = parts.shift();
  53. data.channels = parts.length ? parts.shift() : '1';
  54. return data;
  55. },
  56. /**
  57. * Parses SDP line "a=sctpmap:..." and extracts SCTP port from it.
  58. * @param line eg. "a=sctpmap:5000 webrtc-datachannel"
  59. * @returns [SCTP port number, protocol, streams]
  60. */
  61. parse_sctpmap: function (line)
  62. {
  63. var parts = line.substring(10).split(' ');
  64. var sctpPort = parts[0];
  65. var protocol = parts[1];
  66. // Stream count is optional
  67. var streamCount = parts.length > 2 ? parts[2] : null;
  68. return [sctpPort, protocol, streamCount];// SCTP port
  69. },
  70. build_rtpmap: function (el) {
  71. var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
  72. if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
  73. line += '/' + el.getAttribute('channels');
  74. }
  75. return line;
  76. },
  77. parse_crypto: function (line) {
  78. var parts = line.substring(9).split(' '),
  79. data = {};
  80. data.tag = parts.shift();
  81. data['crypto-suite'] = parts.shift();
  82. data['key-params'] = parts.shift();
  83. if (parts.length) {
  84. data['session-params'] = parts.join(' ');
  85. }
  86. return data;
  87. },
  88. parse_fingerprint: function (line) { // RFC 4572
  89. var parts = line.substring(14).split(' '),
  90. data = {};
  91. data.hash = parts.shift();
  92. data.fingerprint = parts.shift();
  93. // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
  94. return data;
  95. },
  96. parse_fmtp: function (line) {
  97. var parts = line.split(' '),
  98. i, key, value,
  99. data = [];
  100. parts.shift();
  101. parts = parts.join(' ').split(';');
  102. for (i = 0; i < parts.length; i++) {
  103. key = parts[i].split('=')[0];
  104. while (key.length && key[0] == ' ') {
  105. key = key.substring(1);
  106. }
  107. value = parts[i].split('=')[1];
  108. if (key && value) {
  109. data.push({name: key, value: value});
  110. } else if (key) {
  111. // rfc 4733 (DTMF) style stuff
  112. data.push({name: '', value: key});
  113. }
  114. }
  115. return data;
  116. },
  117. parse_icecandidate: function (line) {
  118. var candidate = {},
  119. elems = line.split(' ');
  120. candidate.foundation = elems[0].substring(12);
  121. candidate.component = elems[1];
  122. candidate.protocol = elems[2].toLowerCase();
  123. candidate.priority = elems[3];
  124. candidate.ip = elems[4];
  125. candidate.port = elems[5];
  126. // elems[6] => "typ"
  127. candidate.type = elems[7];
  128. candidate.generation = 0; // default value, may be overwritten below
  129. for (var i = 8; i < elems.length; i += 2) {
  130. switch (elems[i]) {
  131. case 'raddr':
  132. candidate['rel-addr'] = elems[i + 1];
  133. break;
  134. case 'rport':
  135. candidate['rel-port'] = elems[i + 1];
  136. break;
  137. case 'generation':
  138. candidate.generation = elems[i + 1];
  139. break;
  140. case 'tcptype':
  141. candidate.tcptype = elems[i + 1];
  142. break;
  143. default: // TODO
  144. console.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  145. }
  146. }
  147. candidate.network = '1';
  148. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  149. return candidate;
  150. },
  151. build_icecandidate: function (cand) {
  152. var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
  153. line += ' ';
  154. switch (cand.type) {
  155. case 'srflx':
  156. case 'prflx':
  157. case 'relay':
  158. if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
  159. line += 'raddr';
  160. line += ' ';
  161. line += cand['rel-addr'];
  162. line += ' ';
  163. line += 'rport';
  164. line += ' ';
  165. line += cand['rel-port'];
  166. line += ' ';
  167. }
  168. break;
  169. }
  170. if (cand.hasOwnAttribute('tcptype')) {
  171. line += 'tcptype';
  172. line += ' ';
  173. line += cand.tcptype;
  174. line += ' ';
  175. }
  176. line += 'generation';
  177. line += ' ';
  178. line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
  179. return line;
  180. },
  181. parse_ssrc: function (desc) {
  182. // proprietary mapping of a=ssrc lines
  183. // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
  184. // and parse according to that
  185. var lines = desc.split('\r\n'),
  186. data = {};
  187. for (var i = 0; i < lines.length; i++) {
  188. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  189. var idx = lines[i].indexOf(' ');
  190. data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
  191. }
  192. }
  193. return data;
  194. },
  195. parse_rtcpfb: function (line) {
  196. var parts = line.substr(10).split(' ');
  197. var data = {};
  198. data.pt = parts.shift();
  199. data.type = parts.shift();
  200. data.params = parts;
  201. return data;
  202. },
  203. parse_extmap: function (line) {
  204. var parts = line.substr(9).split(' ');
  205. var data = {};
  206. data.value = parts.shift();
  207. if (data.value.indexOf('/') != -1) {
  208. data.direction = data.value.substr(data.value.indexOf('/') + 1);
  209. data.value = data.value.substr(0, data.value.indexOf('/'));
  210. } else {
  211. data.direction = 'both';
  212. }
  213. data.uri = parts.shift();
  214. data.params = parts;
  215. return data;
  216. },
  217. find_line: function (haystack, needle, sessionpart) {
  218. var lines = haystack.split('\r\n');
  219. for (var i = 0; i < lines.length; i++) {
  220. if (lines[i].substring(0, needle.length) == needle) {
  221. return lines[i];
  222. }
  223. }
  224. if (!sessionpart) {
  225. return false;
  226. }
  227. // search session part
  228. lines = sessionpart.split('\r\n');
  229. for (var j = 0; j < lines.length; j++) {
  230. if (lines[j].substring(0, needle.length) == needle) {
  231. return lines[j];
  232. }
  233. }
  234. return false;
  235. },
  236. find_lines: function (haystack, needle, sessionpart) {
  237. var lines = haystack.split('\r\n'),
  238. needles = [];
  239. for (var i = 0; i < lines.length; i++) {
  240. if (lines[i].substring(0, needle.length) == needle)
  241. needles.push(lines[i]);
  242. }
  243. if (needles.length || !sessionpart) {
  244. return needles;
  245. }
  246. // search session part
  247. lines = sessionpart.split('\r\n');
  248. for (var j = 0; j < lines.length; j++) {
  249. if (lines[j].substring(0, needle.length) == needle) {
  250. needles.push(lines[j]);
  251. }
  252. }
  253. return needles;
  254. },
  255. candidateToJingle: function (line) {
  256. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  257. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  258. if (line.indexOf('candidate:') === 0) {
  259. line = 'a=' + line;
  260. } else if (line.substring(0, 12) != 'a=candidate:') {
  261. console.log('parseCandidate called with a line that is not a candidate line');
  262. console.log(line);
  263. return null;
  264. }
  265. if (line.substring(line.length - 2) == '\r\n') // chomp it
  266. line = line.substring(0, line.length - 2);
  267. var candidate = {},
  268. elems = line.split(' '),
  269. i;
  270. if (elems[6] != 'typ') {
  271. console.log('did not find typ in the right place');
  272. console.log(line);
  273. return null;
  274. }
  275. candidate.foundation = elems[0].substring(12);
  276. candidate.component = elems[1];
  277. candidate.protocol = elems[2].toLowerCase();
  278. candidate.priority = elems[3];
  279. candidate.ip = elems[4];
  280. candidate.port = elems[5];
  281. // elems[6] => "typ"
  282. candidate.type = elems[7];
  283. candidate.generation = '0'; // default, may be overwritten below
  284. for (i = 8; i < elems.length; i += 2) {
  285. switch (elems[i]) {
  286. case 'raddr':
  287. candidate['rel-addr'] = elems[i + 1];
  288. break;
  289. case 'rport':
  290. candidate['rel-port'] = elems[i + 1];
  291. break;
  292. case 'generation':
  293. candidate.generation = elems[i + 1];
  294. break;
  295. case 'tcptype':
  296. candidate.tcptype = elems[i + 1];
  297. break;
  298. default: // TODO
  299. console.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  300. }
  301. }
  302. candidate.network = '1';
  303. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  304. return candidate;
  305. },
  306. candidateFromJingle: function (cand) {
  307. var line = 'a=candidate:';
  308. line += cand.getAttribute('foundation');
  309. line += ' ';
  310. line += cand.getAttribute('component');
  311. line += ' ';
  312. line += cand.getAttribute('protocol'); //.toUpperCase(); // chrome M23 doesn't like this
  313. line += ' ';
  314. line += cand.getAttribute('priority');
  315. line += ' ';
  316. line += cand.getAttribute('ip');
  317. line += ' ';
  318. line += cand.getAttribute('port');
  319. line += ' ';
  320. line += 'typ';
  321. line += ' ' + cand.getAttribute('type');
  322. line += ' ';
  323. switch (cand.getAttribute('type')) {
  324. case 'srflx':
  325. case 'prflx':
  326. case 'relay':
  327. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  328. line += 'raddr';
  329. line += ' ';
  330. line += cand.getAttribute('rel-addr');
  331. line += ' ';
  332. line += 'rport';
  333. line += ' ';
  334. line += cand.getAttribute('rel-port');
  335. line += ' ';
  336. }
  337. break;
  338. }
  339. if (cand.getAttribute('protocol').toLowerCase() == 'tcp') {
  340. line += 'tcptype';
  341. line += ' ';
  342. line += cand.getAttribute('tcptype');
  343. line += ' ';
  344. }
  345. line += 'generation';
  346. line += ' ';
  347. line += cand.getAttribute('generation') || '0';
  348. return line + '\r\n';
  349. }
  350. };
  351. module.exports = SDPUtil;