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

transform.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var transform = require('sdp-transform');
  2. exports.write = function(session, opts) {
  3. if (typeof session !== 'undefined' &&
  4. typeof session.media !== 'undefined' &&
  5. Array.isArray(session.media)) {
  6. session.media.forEach(function (mLine) {
  7. // expand sources to ssrcs
  8. if (typeof mLine.sources !== 'undefined' &&
  9. Object.keys(mLine.sources).length !== 0) {
  10. mLine.ssrcs = [];
  11. Object.keys(mLine.sources).forEach(function (ssrc) {
  12. var source = mLine.sources[ssrc];
  13. Object.keys(source).forEach(function (attribute) {
  14. mLine.ssrcs.push({
  15. id: ssrc,
  16. attribute: attribute,
  17. value: source[attribute]
  18. });
  19. });
  20. });
  21. delete mLine.sources;
  22. }
  23. // join ssrcs in ssrc groups
  24. if (typeof mLine.ssrcGroups !== 'undefined' &&
  25. Array.isArray(mLine.ssrcGroups)) {
  26. mLine.ssrcGroups.forEach(function (ssrcGroup) {
  27. if (typeof ssrcGroup.ssrcs !== 'undefined' &&
  28. Array.isArray(ssrcGroup.ssrcs)) {
  29. ssrcGroup.ssrcs = ssrcGroup.ssrcs.join(' ');
  30. }
  31. });
  32. }
  33. });
  34. }
  35. // join group mids
  36. if (typeof session !== 'undefined' &&
  37. typeof session.groups !== 'undefined' && Array.isArray(session.groups)) {
  38. session.groups.forEach(function (g) {
  39. if (typeof g.mids !== 'undefined' && Array.isArray(g.mids)) {
  40. g.mids = g.mids.join(' ');
  41. }
  42. });
  43. }
  44. return transform.write(session, opts);
  45. };
  46. exports.parse = function(sdp) {
  47. var session = transform.parse(sdp);
  48. if (typeof session !== 'undefined' && typeof session.media !== 'undefined' &&
  49. Array.isArray(session.media)) {
  50. session.media.forEach(function (mLine) {
  51. // group sources attributes by ssrc
  52. if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
  53. mLine.sources = {};
  54. mLine.ssrcs.forEach(function (ssrc) {
  55. if (!mLine.sources[ssrc.id])
  56. mLine.sources[ssrc.id] = {};
  57. mLine.sources[ssrc.id][ssrc.attribute] = ssrc.value;
  58. });
  59. delete mLine.ssrcs;
  60. }
  61. // split ssrcs in ssrc groups
  62. if (typeof mLine.ssrcGroups !== 'undefined' &&
  63. Array.isArray(mLine.ssrcGroups)) {
  64. mLine.ssrcGroups.forEach(function (ssrcGroup) {
  65. if (typeof ssrcGroup.ssrcs === 'string') {
  66. ssrcGroup.ssrcs = ssrcGroup.ssrcs.split(' ');
  67. }
  68. });
  69. }
  70. });
  71. }
  72. // split group mids
  73. if (typeof session !== 'undefined' &&
  74. typeof session.groups !== 'undefined' && Array.isArray(session.groups)) {
  75. session.groups.forEach(function (g) {
  76. if (typeof g.mids === 'string') {
  77. g.mids = g.mids.split(' ');
  78. }
  79. });
  80. }
  81. return session;
  82. };