Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var grammar = require('./grammar');
  2. // customized util.format - discards excess arguments and can void middle ones
  3. var formatRegExp = /%[sdv%]/g;
  4. var format = function (formatStr) {
  5. var i = 1;
  6. var args = arguments;
  7. var len = args.length;
  8. return formatStr.replace(formatRegExp, function (x) {
  9. if (i >= len) {
  10. return x; // missing argument
  11. }
  12. var arg = args[i];
  13. i += 1;
  14. switch (x) {
  15. case '%%':
  16. return '%';
  17. case '%s':
  18. return String(arg);
  19. case '%d':
  20. return Number(arg);
  21. case '%v':
  22. return '';
  23. }
  24. });
  25. // NB: we discard excess arguments - they are typically undefined from makeLine
  26. };
  27. var makeLine = function (type, obj, location) {
  28. var str = obj.format instanceof Function ?
  29. (obj.format(obj.push ? location : location[obj.name])) :
  30. obj.format;
  31. var args = [type + '=' + str];
  32. if (obj.names) {
  33. for (var i = 0; i < obj.names.length; i += 1) {
  34. var n = obj.names[i];
  35. if (obj.name) {
  36. args.push(location[obj.name][n]);
  37. }
  38. else { // for mLine and push attributes
  39. args.push(location[obj.names[i]]);
  40. }
  41. }
  42. }
  43. else {
  44. args.push(location[obj.name]);
  45. }
  46. return format.apply(null, args);
  47. };
  48. // RFC specified order
  49. // TODO: extend this with all the rest
  50. var defaultOuterOrder = [
  51. 'v', 'o', 's', 'i',
  52. 'u', 'e', 'p', 'c',
  53. 'b', 't', 'r', 'z', 'a', 'y'
  54. ];
  55. var defaultInnerOrder = ['i', 'c', 'b', 'a', 'y'];
  56. module.exports = function (session, opts) {
  57. opts = opts || {};
  58. // ensure certain properties exist
  59. if (session.version == null) {
  60. session.version = 0; // 'v=0' must be there (only defined version atm)
  61. }
  62. if (session.name == null) {
  63. session.name = ' '; // 's= ' must be there if no meaningful name set
  64. }
  65. session.media.forEach(function (mLine) {
  66. if (mLine.payloads == null) {
  67. mLine.payloads = '';
  68. }
  69. });
  70. var outerOrder = opts.outerOrder || defaultOuterOrder;
  71. var innerOrder = opts.innerOrder || defaultInnerOrder;
  72. var sdp = [];
  73. // loop through outerOrder for matching properties on session
  74. outerOrder.forEach(function (type) {
  75. grammar[type].forEach(function (obj) {
  76. if (obj.name in session && session[obj.name] != null) {
  77. sdp.push(makeLine(type, obj, session));
  78. }
  79. else if (obj.push in session && session[obj.push] != null) {
  80. session[obj.push].forEach(function (el) {
  81. sdp.push(makeLine(type, obj, el));
  82. });
  83. }
  84. });
  85. });
  86. // then for each media line, follow the innerOrder
  87. session.media.forEach(function (mLine) {
  88. sdp.push(makeLine('m', grammar.m[0], mLine));
  89. innerOrder.forEach(function (type) {
  90. grammar[type].forEach(function (obj) {
  91. if (obj.name in mLine && mLine[obj.name] != null) {
  92. sdp.push(makeLine(type, obj, mLine));
  93. }
  94. else if (obj.push in mLine && mLine[obj.push] != null) {
  95. mLine[obj.push].forEach(function (el) {
  96. sdp.push(makeLine(type, obj, el));
  97. });
  98. }
  99. });
  100. });
  101. });
  102. return sdp.join('\r\n') + '\r\n';
  103. };