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

SdpConsistency.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* global __filename */
  2. import { getLogger } from "jitsi-meet-logger";
  3. import { parsePrimarySSRC,
  4. parseSecondarySSRC,
  5. SdpTransformWrap } from './SdpTransformUtil';
  6. const logger = getLogger(__filename);
  7. /**
  8. * Handles the work of keeping video ssrcs consistent across multiple
  9. * o/a cycles, making it such that all stream operations can be
  10. * kept local and do not need to be signaled.
  11. * NOTE: This only keeps the 'primary' video ssrc consistent: meaning
  12. * the primary video stream
  13. */
  14. export default class SdpConsistency {
  15. /**
  16. * Constructor
  17. */
  18. constructor () {
  19. this.clearSsrcCache();
  20. }
  21. /**
  22. * Clear the cached primary and primary rtx ssrcs so that
  23. * they will not be used for the next call to
  24. * makeVideoPrimarySsrcsConsistent
  25. */
  26. clearSsrcCache () {
  27. this.cachedPrimarySsrc = null;
  28. }
  29. /**
  30. * Explicitly set the primary ssrc to be used in
  31. * makeVideoPrimarySsrcsConsistent
  32. * @param {number} primarySsrc the primarySsrc to be used
  33. * in future calls to makeVideoPrimarySsrcsConsistent
  34. * @throws Error if <tt>primarySsrc</tt> is not a number
  35. */
  36. setPrimarySsrc (primarySsrc) {
  37. if (typeof primarySsrc !== 'number') {
  38. throw new Error("Primary SSRC must be a number!");
  39. }
  40. this.cachedPrimarySsrc = primarySsrc;
  41. }
  42. /**
  43. * Given an sdp string, either:
  44. * 1) record the primary video and primary rtx ssrcs to be
  45. * used in future calls to makeVideoPrimarySsrcsConsistent or
  46. * 2) change the primary and primary rtx ssrcs in the given sdp
  47. * to match the ones previously cached
  48. * @param {string} sdpStr the sdp string to (potentially)
  49. * change to make the video ssrcs consistent
  50. * @returns {string} a (potentially) modified sdp string
  51. * with ssrcs consistent with this class' cache
  52. */
  53. makeVideoPrimarySsrcsConsistent (sdpStr) {
  54. const sdpTransformer = new SdpTransformWrap(sdpStr);
  55. const videoMLine = sdpTransformer.selectMedia("video");
  56. if (!videoMLine) {
  57. logger.error(`No 'video' media found in the sdp: ${sdpStr}`);
  58. return sdpStr;
  59. }
  60. if (videoMLine.direction === "inactive") {
  61. logger.info(
  62. "Sdp-consistency doing nothing, video mline is inactive");
  63. return sdpStr;
  64. }
  65. if (videoMLine.direction === "recvonly") {
  66. // If the mline is recvonly, we'll add the primary
  67. // ssrc as a recvonly ssrc
  68. if (this.cachedPrimarySsrc) {
  69. videoMLine.addSSRCAttribute({
  70. id: this.cachedPrimarySsrc,
  71. attribute: "cname",
  72. value: `recvonly-${this.cachedPrimarySsrc}`
  73. });
  74. } else {
  75. logger.error("No SSRC found for the recvonly video stream!");
  76. }
  77. } else {
  78. let newPrimarySsrc = videoMLine.getPrimaryVideoSsrc();
  79. if (!newPrimarySsrc) {
  80. logger.info("Sdp-consistency couldn't parse new primary ssrc");
  81. return sdpStr;
  82. }
  83. if (!this.cachedPrimarySsrc) {
  84. this.cachedPrimarySsrc = newPrimarySsrc;
  85. logger.info(
  86. "Sdp-consistency caching primary ssrc "
  87. + this.cachedPrimarySsrc);
  88. } else {
  89. logger.info(
  90. `Sdp-consistency replacing new ssrc ` +
  91. `${newPrimarySsrc} with cached ${this.cachedPrimarySsrc}`);
  92. videoMLine.replaceSSRC(
  93. newPrimarySsrc, this.cachedPrimarySsrc);
  94. for (const group of videoMLine.ssrcGroups) {
  95. if (group.semantics === "FID") {
  96. let primarySsrc = parsePrimarySSRC(group);
  97. let rtxSsrc = parseSecondarySSRC(group);
  98. if (primarySsrc === newPrimarySsrc) {
  99. group.ssrcs =
  100. this.cachedPrimarySsrc + " " +
  101. rtxSsrc;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return sdpTransformer.toRawSDP();
  108. }
  109. }