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.

SdpConsistency.js 4.7KB

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