Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

parser.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var toIntIfInt = function (v) {
  2. return String(Number(v)) === v ? Number(v) : v;
  3. };
  4. var attachProperties = function (match, location, names, rawName) {
  5. if (rawName && !names) {
  6. location[rawName] = toIntIfInt(match[1]);
  7. }
  8. else {
  9. for (var i = 0; i < names.length; i += 1) {
  10. if (match[i+1] != null) {
  11. location[names[i]] = toIntIfInt(match[i+1]);
  12. }
  13. }
  14. }
  15. };
  16. var parseReg = function (obj, location, content) {
  17. var needsBlank = obj.name && obj.names;
  18. if (obj.push && !location[obj.push]) {
  19. location[obj.push] = [];
  20. }
  21. else if (needsBlank && !location[obj.name]) {
  22. location[obj.name] = {};
  23. }
  24. var keyLocation = obj.push ?
  25. {} : // blank object that will be pushed
  26. needsBlank ? location[obj.name] : location; // otherwise, named location or root
  27. attachProperties(content.match(obj.reg), keyLocation, obj.names, obj.name);
  28. if (obj.push) {
  29. location[obj.push].push(keyLocation);
  30. }
  31. };
  32. var grammar = require('./grammar');
  33. var validLine = RegExp.prototype.test.bind(/^([a-z])=(.*)/);
  34. exports.parse = function (sdp) {
  35. var session = {}
  36. , media = []
  37. , location = session; // points at where properties go under (one of the above)
  38. // parse lines we understand
  39. sdp.split(/(\r\n|\r|\n)/).filter(validLine).forEach(function (l) {
  40. var type = l[0];
  41. var content = l.slice(2);
  42. if (type === 'm') {
  43. media.push({rtp: [], fmtp: []});
  44. location = media[media.length-1]; // point at latest media line
  45. }
  46. for (var j = 0; j < (grammar[type] || []).length; j += 1) {
  47. var obj = grammar[type][j];
  48. if (obj.reg.test(content)) {
  49. return parseReg(obj, location, content);
  50. }
  51. }
  52. });
  53. session.media = media; // link it up
  54. return session;
  55. };
  56. var paramReducer = function (acc, expr) {
  57. var s = expr.split(/=(.+)/, 2);
  58. if (s.length === 2) {
  59. acc[s[0]] = toIntIfInt(s[1]);
  60. } else if (s.length === 1 && expr.length > 1) {
  61. acc[s[0]] = undefined;
  62. }
  63. return acc;
  64. };
  65. exports.parseParams = function (str) {
  66. return str.split(/;\s?/).reduce(paramReducer, {});
  67. };
  68. // For backward compatibility - alias will be removed in 3.0.0
  69. exports.parseFmtpConfig = exports.parseParams;
  70. exports.parsePayloads = function (str) {
  71. return str.toString().split(' ').map(Number);
  72. };
  73. exports.parseRemoteCandidates = function (str) {
  74. var candidates = [];
  75. var parts = str.split(' ').map(toIntIfInt);
  76. for (var i = 0; i < parts.length; i += 3) {
  77. candidates.push({
  78. component: parts[i],
  79. ip: parts[i + 1],
  80. port: parts[i + 2]
  81. });
  82. }
  83. return candidates;
  84. };
  85. exports.parseImageAttributes = function (str) {
  86. return str.split(' ').map(function (item) {
  87. return item.substring(1, item.length-1).split(',').reduce(paramReducer, {});
  88. });
  89. };
  90. exports.parseSimulcastStreamList = function (str) {
  91. return str.split(';').map(function (stream) {
  92. return stream.split(',').map(function (format) {
  93. var scid, paused = false;
  94. if (format[0] !== '~') {
  95. scid = toIntIfInt(format);
  96. } else {
  97. scid = toIntIfInt(format.substring(1, format.length));
  98. paused = true;
  99. }
  100. return {
  101. scid: scid,
  102. paused: paused
  103. };
  104. });
  105. });
  106. };