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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import SDPUtil from './SDPUtil';
  4. import { parseSecondarySSRC, SdpTransformWrap } from './SdpTransformUtil';
  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.debug(`No 'video' media found in the sdp: ${sdpStr}`);
  105. return sdpStr;
  106. }
  107. return this.modifyRtxSsrcs2(videoMLine)
  108. ? sdpTransformer.toRawSDP() : sdpStr;
  109. }
  110. /**
  111. * Does the same thing as {@link modifyRtxSsrcs}, but takes the
  112. * {@link MLineWrap} instance wrapping video media as an argument.
  113. * @param {MLineWrap} videoMLine
  114. * @return {boolean} <tt>true</tt> if the SDP wrapped by
  115. * {@link SdpTransformWrap} has been modified or <tt>false</tt> otherwise.
  116. */
  117. modifyRtxSsrcs2(videoMLine) {
  118. if (videoMLine.direction === 'recvonly') {
  119. logger.debug('RtxModifier doing nothing, video m line is recvonly');
  120. return false;
  121. }
  122. if (videoMLine.getSSRCCount() < 1) {
  123. logger.debug('RtxModifier doing nothing, no video ssrcs present');
  124. return false;
  125. }
  126. logger.debug('Current ssrc mapping: ', this.correspondingRtxSsrcs);
  127. const primaryVideoSsrcs = videoMLine.getPrimaryVideoSSRCs();
  128. logger.debug('Parsed primary video ssrcs ', primaryVideoSsrcs,
  129. ' making sure all have rtx streams');
  130. for (const ssrc of primaryVideoSsrcs) {
  131. const msid = videoMLine.getSSRCAttrValue(ssrc, 'msid');
  132. const cname = videoMLine.getSSRCAttrValue(ssrc, 'cname');
  133. let correspondingRtxSsrc = this.correspondingRtxSsrcs.get(ssrc);
  134. if (correspondingRtxSsrc) {
  135. logger.debug(
  136. 'Already have an associated rtx ssrc for'
  137. + `video ssrc ${ssrc}: ${correspondingRtxSsrc}`);
  138. } else {
  139. logger.debug(
  140. `No previously associated rtx ssrc for video ssrc ${ssrc}`);
  141. // If there's one in the sdp already for it, we'll just set
  142. // that as the corresponding one
  143. const previousAssociatedRtxStream = videoMLine.getRtxSSRC(ssrc);
  144. if (previousAssociatedRtxStream) {
  145. logger.debug(
  146. `Rtx stream ${previousAssociatedRtxStream} `
  147. + 'already existed in the sdp as an rtx stream for '
  148. + `${ssrc}`);
  149. correspondingRtxSsrc = previousAssociatedRtxStream;
  150. } else {
  151. correspondingRtxSsrc = SDPUtil.generateSsrc();
  152. logger.debug(`Generated rtx ssrc ${correspondingRtxSsrc} `
  153. + `for ssrc ${ssrc}`);
  154. }
  155. logger.debug(`Caching rtx ssrc ${correspondingRtxSsrc} `
  156. + `for video ssrc ${ssrc}`);
  157. this.correspondingRtxSsrcs.set(ssrc, correspondingRtxSsrc);
  158. }
  159. updateAssociatedRtxStream(
  160. videoMLine,
  161. {
  162. id: ssrc,
  163. cname,
  164. msid
  165. },
  166. correspondingRtxSsrc);
  167. }
  168. // FIXME we're not looking into much details whether the SDP has been
  169. // modified or not once the precondition requirements are met.
  170. return true;
  171. }
  172. /**
  173. * Strip all rtx streams from the given sdp
  174. * @param {string} sdpStr sdp in raw string format
  175. * @returns {string} sdp string with all rtx streams stripped
  176. */
  177. stripRtx(sdpStr) {
  178. const sdpTransformer = new SdpTransformWrap(sdpStr);
  179. const videoMLine = sdpTransformer.selectMedia('video');
  180. if (!videoMLine) {
  181. logger.debug(`No 'video' media found in the sdp: ${sdpStr}`);
  182. return sdpStr;
  183. }
  184. if (videoMLine.direction === 'recvonly') {
  185. logger.debug('RtxModifier doing nothing, video m line is recvonly');
  186. return sdpStr;
  187. }
  188. if (videoMLine.getSSRCCount() < 1) {
  189. logger.debug('RtxModifier doing nothing, no video ssrcs present');
  190. return sdpStr;
  191. }
  192. if (!videoMLine.containsAnySSRCGroups()) {
  193. logger.debug('RtxModifier doing nothing, '
  194. + 'no video ssrcGroups present');
  195. return sdpStr;
  196. }
  197. const fidGroups = videoMLine.findGroups('FID');
  198. // Remove the fid groups from the mline
  199. videoMLine.removeGroupsBySemantics('FID');
  200. // Get the rtx ssrcs and remove them from the mline
  201. for (const fidGroup of fidGroups) {
  202. const rtxSsrc = parseSecondarySSRC(fidGroup);
  203. videoMLine.removeSSRC(rtxSsrc);
  204. }
  205. return sdpTransformer.toRawSDP();
  206. }
  207. }