Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RtxModifier.js 8.3KB

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