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

RtxModifier.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { getLogger } from '@jitsi/logger';
  2. import { MediaDirection } from '../../service/RTC/MediaDirection';
  3. import { MediaType } from '../../service/RTC/MediaType';
  4. import SDPUtil from './SDPUtil';
  5. import { SdpTransformWrap, parseSecondarySSRC } from './SdpTransformUtil';
  6. const logger = getLogger(__filename);
  7. /**
  8. * Begin helper functions
  9. */
  10. /**
  11. * Updates or inserts the appropriate rtx information for primarySsrc with
  12. * the given rtxSsrc. If no rtx ssrc for primarySsrc currently exists, it will
  13. * add the appropriate ssrc and ssrc group lines. If primarySsrc already has
  14. * an rtx ssrc, the appropriate ssrc and group lines will be updated
  15. * @param {MLineWrap} mLine
  16. * @param {object} primarySsrcInfo the info (ssrc, msid & cname) for the
  17. * primary ssrc
  18. * @param {number} rtxSsrc the rtx ssrc to associate with the primary ssrc
  19. */
  20. function updateAssociatedRtxStream(mLine, primarySsrcInfo, rtxSsrc) {
  21. const primarySsrc = primarySsrcInfo.id;
  22. const primarySsrcMsid = primarySsrcInfo.msid;
  23. const primarySsrcCname = primarySsrcInfo.cname;
  24. const previousRtxSSRC = mLine.getRtxSSRC(primarySsrc);
  25. if (previousRtxSSRC === rtxSsrc) {
  26. return;
  27. }
  28. if (previousRtxSSRC) {
  29. // Stream already had an rtx ssrc that is different than the one given,
  30. // remove all trace of the old one
  31. mLine.removeSSRC(previousRtxSSRC);
  32. mLine.removeGroupsWithSSRC(previousRtxSSRC);
  33. }
  34. mLine.addSSRCAttribute({
  35. id: rtxSsrc,
  36. attribute: 'cname',
  37. value: primarySsrcCname
  38. });
  39. mLine.addSSRCAttribute({
  40. id: rtxSsrc,
  41. attribute: 'msid',
  42. value: primarySsrcMsid
  43. });
  44. mLine.addSSRCGroup({
  45. semantics: 'FID',
  46. ssrcs: `${primarySsrc} ${rtxSsrc}`
  47. });
  48. }
  49. /**
  50. * End helper functions
  51. */
  52. /**
  53. * Adds any missing RTX streams for video streams
  54. * and makes sure that they remain consistent
  55. */
  56. export default class RtxModifier {
  57. /**
  58. * Constructor
  59. */
  60. constructor() {
  61. /**
  62. * Map of video ssrc to corresponding RTX
  63. * ssrc
  64. */
  65. this.correspondingRtxSsrcs = new Map();
  66. }
  67. /**
  68. * Clear the cached map of primary video ssrcs to
  69. * their corresponding rtx ssrcs so that they will
  70. * not be used for the next call to modifyRtxSsrcs
  71. */
  72. clearSsrcCache() {
  73. this.correspondingRtxSsrcs.clear();
  74. }
  75. /**
  76. * Explicitly set the primary video ssrc -> rtx ssrc
  77. * mapping to be used in modifyRtxSsrcs
  78. * @param {Map} ssrcMapping a mapping of primary video
  79. * ssrcs to their corresponding rtx ssrcs
  80. */
  81. setSsrcCache(ssrcMapping) {
  82. logger.debug('Setting ssrc cache to ', ssrcMapping);
  83. this.correspondingRtxSsrcs = ssrcMapping;
  84. }
  85. /**
  86. * Adds RTX ssrcs for any video ssrcs that don't already have them. If the video ssrc has been seen before, and
  87. * already had an RTX ssrc generated, the same RTX ssrc will be used again.
  88. *
  89. * @param {string} sdpStr sdp in raw string format
  90. * @returns {string} The modified sdp in raw string format.
  91. */
  92. modifyRtxSsrcs(sdpStr) {
  93. let modified = false;
  94. const sdpTransformer = new SdpTransformWrap(sdpStr);
  95. const videoMLines = sdpTransformer.selectMedia(MediaType.VIDEO);
  96. if (!videoMLines?.length) {
  97. logger.debug(`No 'video' media found in the sdp: ${sdpStr}`);
  98. return sdpStr;
  99. }
  100. for (const videoMLine of videoMLines) {
  101. if (this.modifyRtxSsrcs2(videoMLine)) {
  102. modified = true;
  103. }
  104. }
  105. return modified ? sdpTransformer.toRawSDP() : sdpStr;
  106. }
  107. /**
  108. * Does the same thing as {@link modifyRtxSsrcs}, but takes the {@link MLineWrap} instance wrapping video media as
  109. * an argument.
  110. * @param {MLineWrap} videoMLine
  111. * @return {boolean} <tt>true</tt> if the SDP wrapped by {@link SdpTransformWrap} has been modified or
  112. * <tt>false</tt> otherwise.
  113. */
  114. modifyRtxSsrcs2(videoMLine) {
  115. if (videoMLine.direction === MediaDirection.RECVONLY) {
  116. return false;
  117. }
  118. if (videoMLine.getSSRCCount() < 1) {
  119. return false;
  120. }
  121. const primaryVideoSsrcs = videoMLine.getPrimaryVideoSSRCs();
  122. for (const ssrc of primaryVideoSsrcs) {
  123. const msid = videoMLine.getSSRCAttrValue(ssrc, 'msid');
  124. const cname = videoMLine.getSSRCAttrValue(ssrc, 'cname');
  125. let correspondingRtxSsrc = this.correspondingRtxSsrcs.get(ssrc);
  126. if (!correspondingRtxSsrc) {
  127. // If there's one in the sdp already for it, we'll just set
  128. // that as the corresponding one
  129. const previousAssociatedRtxStream = videoMLine.getRtxSSRC(ssrc);
  130. if (previousAssociatedRtxStream) {
  131. correspondingRtxSsrc = previousAssociatedRtxStream;
  132. } else {
  133. correspondingRtxSsrc = SDPUtil.generateSsrc();
  134. }
  135. this.correspondingRtxSsrcs.set(ssrc, correspondingRtxSsrc);
  136. }
  137. updateAssociatedRtxStream(
  138. videoMLine,
  139. {
  140. id: ssrc,
  141. cname,
  142. msid
  143. },
  144. correspondingRtxSsrc);
  145. }
  146. // FIXME we're not looking into much details whether the SDP has been
  147. // modified or not once the precondition requirements are met.
  148. return true;
  149. }
  150. /**
  151. * Strip all rtx streams from the given sdp.
  152. *
  153. * @param {string} sdpStr sdp in raw string format
  154. * @returns {string} sdp string with all rtx streams stripped
  155. */
  156. stripRtx(sdpStr) {
  157. const sdpTransformer = new SdpTransformWrap(sdpStr);
  158. const videoMLines = sdpTransformer.selectMedia(MediaType.VIDEO);
  159. if (!videoMLines?.length) {
  160. logger.debug(`No 'video' media found in the sdp: ${sdpStr}`);
  161. return sdpStr;
  162. }
  163. for (const videoMLine of videoMLines) {
  164. if (videoMLine.direction !== MediaDirection.RECVONLY
  165. && videoMLine.getSSRCCount()
  166. && videoMLine.containsAnySSRCGroups()) {
  167. const fidGroups = videoMLine.findGroups('FID');
  168. // Remove the fid groups from the mline
  169. videoMLine.removeGroupsBySemantics('FID');
  170. // Get the rtx ssrcs and remove them from the mline
  171. for (const fidGroup of fidGroups) {
  172. const rtxSsrc = parseSecondarySSRC(fidGroup);
  173. videoMLine.removeSSRC(rtxSsrc);
  174. }
  175. }
  176. }
  177. return sdpTransformer.toRawSDP();
  178. }
  179. }