Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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