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.

LocalSdpMunger.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { isEqual } from 'lodash-es';
  2. import { MediaDirection } from '../../service/RTC/MediaDirection';
  3. import { MediaType } from '../../service/RTC/MediaType';
  4. import browser from '../browser';
  5. import { SdpTransformWrap } from './SdpTransformUtil';
  6. /**
  7. * Fakes local SDP exposed to {@link JingleSessionPC} through the local description getter. Modifies the SDP, so that
  8. * the stream identifiers are unique across all of the local PeerConnections and that the source names and video types
  9. * are injected so that Jicofo can use them to identify the sources.
  10. */
  11. export default class LocalSdpMunger {
  12. /**
  13. * Creates new <tt>LocalSdpMunger</tt> instance.
  14. *
  15. * @param {TraceablePeerConnection} tpc
  16. * @param {string} localEndpointId - The endpoint id of the local user.
  17. */
  18. constructor(tpc, localEndpointId) {
  19. this.tpc = tpc;
  20. this.localEndpointId = localEndpointId;
  21. }
  22. /**
  23. * Updates or adds a 'msid' attribute for the local sources in the SDP. Also adds 'sourceName' and 'videoType'
  24. * (if applicable) attributes. All other source attributes like 'cname', 'label' and 'mslabel' are removed since
  25. * these are not processed by Jicofo.
  26. *
  27. * @param {MLineWrap} mediaSection - The media part (audio or video) of the session description which will be
  28. * modified in place.
  29. * @returns {void}
  30. * @private
  31. */
  32. _transformMediaIdentifiers(mediaSection, ssrcMap) {
  33. const mediaType = mediaSection.mLine.type;
  34. const mediaDirection = mediaSection.mLine.direction;
  35. const sources = [ ...new Set(mediaSection.mLine.ssrcs?.map(s => s.id)) ];
  36. let sourceName;
  37. if (ssrcMap.size) {
  38. const sortedSources = sources.slice().sort();
  39. for (const [ id, trackSsrcs ] of ssrcMap.entries()) {
  40. if (isEqual(sortedSources, [ ...trackSsrcs.ssrcs ].sort())) {
  41. sourceName = id;
  42. }
  43. }
  44. for (const source of sources) {
  45. if ((mediaDirection === MediaDirection.SENDONLY || mediaDirection === MediaDirection.SENDRECV)
  46. && sourceName) {
  47. const msid = ssrcMap.get(sourceName).msid;
  48. const generatedMsid = `${msid}-${this.tpc.id}`;
  49. const existingMsid = mediaSection.ssrcs
  50. .find(ssrc => ssrc.id === source && ssrc.attribute === 'msid');
  51. // Always overwrite msid since we want the msid to be in this format even if the browser generates
  52. // one. '<endpoint_id>-<mediaType>-<trackIndex>-<tpcId>' example - d8ff91-video-0-1
  53. if (existingMsid) {
  54. existingMsid.value = generatedMsid;
  55. } else {
  56. mediaSection.ssrcs.push({
  57. id: source,
  58. attribute: 'msid',
  59. value: generatedMsid
  60. });
  61. }
  62. // Inject source names as a=ssrc:3124985624 name:endpointA-v0
  63. mediaSection.ssrcs.push({
  64. id: source,
  65. attribute: 'name',
  66. value: sourceName
  67. });
  68. const videoType = this.tpc.getLocalVideoTracks()
  69. .find(track => track.getSourceName() === sourceName)
  70. ?.getVideoType();
  71. if (mediaType === MediaType.VIDEO && videoType) {
  72. // Inject videoType as a=ssrc:1234 videoType:desktop.
  73. mediaSection.ssrcs.push({
  74. id: source,
  75. attribute: 'videoType',
  76. value: videoType
  77. });
  78. }
  79. }
  80. }
  81. }
  82. // Ignore the 'cname', 'label' and 'mslabel' attributes.
  83. mediaSection.ssrcs = mediaSection.ssrcs
  84. .filter(ssrc => ssrc.attribute === 'msid' || ssrc.attribute === 'name' || ssrc.attribute === 'videoType');
  85. // On FF when the user has started muted create answer will generate a recv only SSRC. We don't want to signal
  86. // this SSRC in order to reduce the load of the xmpp server for large calls. Therefore the SSRC needs to be
  87. // removed from the SDP.
  88. //
  89. // For all other use cases (when the user has had media but then the user has stopped it) we want to keep the
  90. // receive only SSRCs in the SDP. Otherwise source-remove will be triggered and the next time the user add a
  91. // track we will reuse the SSRCs and send source-add with the same SSRCs. This is problematic because of issues
  92. // on Chrome and FF (https://bugzilla.mozilla.org/show_bug.cgi?id=1768729) when removing and then adding the
  93. // same SSRC in the remote sdp the remote track is not rendered.
  94. if (browser.isFirefox()
  95. && (mediaDirection === MediaDirection.RECVONLY || mediaDirection === MediaDirection.INACTIVE)
  96. && (
  97. (mediaType === MediaType.VIDEO && !this.tpc._hasHadVideoTrack)
  98. || (mediaType === MediaType.AUDIO && !this.tpc._hasHadAudioTrack)
  99. )
  100. ) {
  101. mediaSection.ssrcs = undefined;
  102. mediaSection.ssrcGroups = undefined;
  103. }
  104. }
  105. /**
  106. * This transformation will make sure that stream identifiers are unique across all of the local PeerConnections
  107. * even if the same stream is used by multiple instances at the same time. It also injects 'sourceName' and
  108. * 'videoType' attribute.
  109. *
  110. * @param {RTCSessionDescription} sessionDesc - The local session description (this instance remains unchanged).
  111. * @param {Map<string, TPCSSRCInfo>} ssrcMap - The SSRC and source map for the local tracks.
  112. * @return {RTCSessionDescription} - Transformed local session description
  113. * (a modified copy of the one given as the input).
  114. */
  115. transformStreamIdentifiers(sessionDesc, ssrcMap) {
  116. if (!sessionDesc || !sessionDesc.sdp || !sessionDesc.type) {
  117. return sessionDesc;
  118. }
  119. const transformer = new SdpTransformWrap(sessionDesc.sdp);
  120. const audioMLine = transformer.selectMedia(MediaType.AUDIO)?.[0];
  121. if (audioMLine) {
  122. this._transformMediaIdentifiers(audioMLine, ssrcMap);
  123. }
  124. const videoMlines = transformer.selectMedia(MediaType.VIDEO);
  125. for (const videoMLine of videoMlines) {
  126. this._transformMediaIdentifiers(videoMLine, ssrcMap);
  127. }
  128. return new RTCSessionDescription({
  129. type: sessionDesc.type,
  130. sdp: transformer.toRawSDP()
  131. });
  132. }
  133. }