Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sdp.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var util = require('util');
  2. var parsers = {
  3. o: function(o) {
  4. var t = o.split(/\s+/);
  5. return {
  6. username: t[0],
  7. id : t[1],
  8. version : t[2],
  9. nettype : t[3],
  10. addrtype : t[4],
  11. address : t[5]
  12. };
  13. },
  14. c: function(c) {
  15. var t = c.split(/\s+/);
  16. return { nettype: t[0], addrtype: t[1], address: t[2] };
  17. },
  18. m: function(m) {
  19. var t = /^(\w+) +(\d+)(?:\/(\d))? +(\S+) (\d+( +\d+)*)/.exec(m);
  20. return {
  21. media: t[1],
  22. port: +t[2],
  23. portnum: +(t[3] || 1),
  24. proto: t[4],
  25. fmt: t[5].split(/\s+/).map(function(x) { return +x; })
  26. };
  27. },
  28. a: function(a) {
  29. return a;
  30. }
  31. };
  32. exports.parse = function(sdp) {
  33. var sdp = sdp.split(/\r\n/);
  34. var root = {};
  35. var m;
  36. root.m = [];
  37. for(var i = 0; i < sdp.length; ++i) {
  38. var tmp = /^(\w)=(.*)/.exec(sdp[i]);
  39. if(tmp) {
  40. var c = (parsers[tmp[1]] || function(x) { return x;})(tmp[2]);
  41. switch(tmp[1]) {
  42. case 'm':
  43. if(m) root.m.push(m);
  44. m = c;
  45. break;
  46. case 'a':
  47. var o = (m || root);
  48. if(o.a === undefined) o.a = [];
  49. o.a.push(c);
  50. break;
  51. default:
  52. (m || root)[tmp[1]] = c;
  53. break;
  54. }
  55. }
  56. }
  57. if(m) root.m.push(m);
  58. return root;
  59. };
  60. var stringifiers = {
  61. o: function(o) {
  62. return [o.username || '-', o.id, o.version, o.nettype || 'IN', o.addrtype || 'IP4', o.address].join(' ');
  63. },
  64. c: function(c) {
  65. return [c.nettype || 'IN', c.addrtype || 'IP4', c.address].join(' ');
  66. },
  67. m: function(m) {
  68. return [m.media || 'audio', m.port, m.proto || 'RTP/AVP', m.fmt.join(' ')].join(' ');
  69. }
  70. };
  71. function stringifyParam(sdp, type, def) {
  72. if(sdp[type] !== undefined) {
  73. var stringifier = function(x) { return type + '=' + ((stringifiers[type] && stringifiers[type](x)) || x) + '\r\n'; };
  74. if(Array.isArray(sdp[type]))
  75. return sdp[type].map(stringifier).join('');
  76. return stringifier(sdp[type]);
  77. }
  78. if(def !== undefined)
  79. return type + '=' + def + '\r\n';
  80. return '';
  81. }
  82. exports.stringify = function(sdp) {
  83. var s = '';
  84. s += stringifyParam(sdp, 'v', 0);
  85. s += stringifyParam(sdp, 'o');
  86. s += stringifyParam(sdp, 's', '-');
  87. s += stringifyParam(sdp, 'i');
  88. s += stringifyParam(sdp, 'u');
  89. s += stringifyParam(sdp, 'e');
  90. s += stringifyParam(sdp, 'p');
  91. s += stringifyParam(sdp, 'c');
  92. s += stringifyParam(sdp, 'b');
  93. s += stringifyParam(sdp, 't', '0 0');
  94. s += stringifyParam(sdp, 'r');
  95. s += stringifyParam(sdp, 'z');
  96. s += stringifyParam(sdp, 'k');
  97. s += stringifyParam(sdp, 'a');
  98. sdp.m.forEach(function(m) {
  99. s += stringifyParam({m:m}, 'm');
  100. s += stringifyParam(m, 'i');
  101. s += stringifyParam(m, 'c');
  102. s += stringifyParam(m, 'b');
  103. s += stringifyParam(m, 'k');
  104. s += stringifyParam(m, 'a');
  105. });
  106. return s;
  107. }