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.

SDPDiffer.ts 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { isEqual } from 'lodash-es';
  2. import Strophe from 'strophe';
  3. import { XEP } from '../../service/xmpp/XMPPExtensioProtocols';
  4. import SDPUtil from './SDPUtil';
  5. export interface IMediaSsrc {
  6. lines: string[];
  7. ssrc: number;
  8. }
  9. export interface IMediaSsrcs {
  10. [ssrcNum: string]: IMediaSsrc;
  11. }
  12. export interface ISsrcGroup {
  13. semantics: string;
  14. ssrcs: number[];
  15. }
  16. export interface IMediaSource {
  17. mediaType: string;
  18. mid?: string;
  19. ssrcGroups: ISsrcGroup[];
  20. ssrcs: IMediaSsrcs;
  21. }
  22. export interface IDiffSourceInfo {
  23. [index: string]: IMediaSource;
  24. }
  25. export interface ISDP {
  26. getMediaSsrcMap: () => Map<string, IMediaSource>;
  27. }
  28. /**
  29. * A class that provides methods for comparing the source information present in two different SDPs so that the delta
  30. * can be signaled to Jicofo via 'source-remove' or 'source-add'.
  31. */
  32. export class SDPDiffer {
  33. private isP2P: boolean;
  34. private mySdp: ISDP;
  35. private othersSdp: ISDP;
  36. /**
  37. * Constructor.
  38. *
  39. * @param {SDP} mySdp - the new SDP.
  40. * @param {SDP} othersSdp - the old SDP.
  41. * @param {boolean} isP2P - Whether the SDPs belong to a p2p peerconnection.
  42. */
  43. constructor(mySdp: ISDP, othersSdp: ISDP, isP2P: boolean) {
  44. this.isP2P = isP2P;
  45. this.mySdp = mySdp;
  46. this.othersSdp = othersSdp;
  47. }
  48. /**
  49. * Returns a map of the sources that are present in 'othersSdp' but not in 'mySdp'.
  50. *
  51. * @returns {*}
  52. */
  53. getNewMedia(): IDiffSourceInfo {
  54. const mySources = this.mySdp.getMediaSsrcMap();
  55. const othersSources = this.othersSdp.getMediaSsrcMap();
  56. const diff: IDiffSourceInfo = {};
  57. for (const [ index, othersSource ] of othersSources.entries()) {
  58. const mySource = mySources.get(index);
  59. if (!mySource) {
  60. diff[index] = othersSource;
  61. continue; // eslint-disable-line no-continue
  62. }
  63. const othersSsrcs = Object.keys(othersSource.ssrcs);
  64. if (othersSsrcs.length && !isEqual(Object.keys(mySource.ssrcs).sort(), [ ...othersSsrcs ].sort())) {
  65. diff[index] = othersSource;
  66. }
  67. }
  68. return diff;
  69. }
  70. /**
  71. * Adds the diff source info to the provided IQ stanza.
  72. *
  73. * @param {Strophe.Builder} modify - Stanza IQ.
  74. * @returns {boolean}
  75. */
  76. toJingle(modify: Strophe.Builder): boolean {
  77. let modified = false;
  78. const diffSourceInfo = this.getNewMedia();
  79. for (const media of Object.values(diffSourceInfo)) {
  80. modified = true;
  81. modify.c('content', { name: this.isP2P ? media.mid : media.mediaType });
  82. modify.c('description', {
  83. xmlns: XEP.RTP_MEDIA,
  84. media: media.mediaType
  85. });
  86. Object.keys(media.ssrcs).forEach(ssrcNum => {
  87. const mediaSsrc = media.ssrcs[ssrcNum];
  88. const ssrcLines = mediaSsrc.lines;
  89. const sourceName = SDPUtil.parseSourceNameLine(ssrcLines);
  90. const videoType = SDPUtil.parseVideoTypeLine(ssrcLines);
  91. modify.c('source', { xmlns: XEP.SOURCE_ATTRIBUTES });
  92. modify.attrs({
  93. name: sourceName,
  94. videoType,
  95. ssrc: mediaSsrc.ssrc
  96. });
  97. // Only MSID attribute is sent
  98. const msid = SDPUtil.parseMSIDAttribute(ssrcLines);
  99. if (msid) {
  100. modify.c('parameter', { name: 'msid', value: msid }).up();
  101. }
  102. modify.up(); // end of source
  103. });
  104. // generate source groups from lines
  105. media.ssrcGroups.forEach(ssrcGroup => {
  106. if (ssrcGroup.ssrcs.length) {
  107. modify.c('ssrc-group', {
  108. semantics: ssrcGroup.semantics,
  109. xmlns: XEP.SOURCE_ATTRIBUTES
  110. });
  111. ssrcGroup.ssrcs.forEach(ssrc => {
  112. modify.c('source', { ssrc }).up(); // end of source
  113. });
  114. modify.up(); // end of ssrc-group
  115. }
  116. });
  117. modify.up(); // end of description
  118. modify.up(); // end of content
  119. }
  120. return modified;
  121. }
  122. }