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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { getLogger } from '@jitsi/logger';
  2. import { MediaDirection } from '../../service/RTC/MediaDirection';
  3. import { MediaType } from '../../service/RTC/MediaType';
  4. import { getSourceNameForJitsiTrack } from '../../service/RTC/SignalingLayer';
  5. import { VideoType } from '../../service/RTC/VideoType';
  6. import browser from '../browser';
  7. import FeatureFlags from '../flags/FeatureFlags';
  8. import { SdpTransformWrap } from './SdpTransformUtil';
  9. const logger = getLogger(__filename);
  10. /**
  11. * Fakes local SDP exposed to {@link JingleSessionPC} through the local
  12. * description getter. Modifies the SDP, so that it will contain muted local
  13. * video tracks description, even though their underlying {MediaStreamTrack}s
  14. * are no longer in the WebRTC peerconnection. That prevents from SSRC updates
  15. * being sent to Jicofo/remote peer and prevents sRD/sLD cycle on the remote
  16. * side.
  17. */
  18. export default class LocalSdpMunger {
  19. /**
  20. * Creates new <tt>LocalSdpMunger</tt> instance.
  21. *
  22. * @param {TraceablePeerConnection} tpc
  23. * @param {string} localEndpointId - The endpoint id of the local user.
  24. */
  25. constructor(tpc, localEndpointId) {
  26. this.tpc = tpc;
  27. this.localEndpointId = localEndpointId;
  28. this.audioSourcesToMsidMap = new Map();
  29. this.videoSourcesToMsidMap = new Map();
  30. }
  31. /**
  32. * Makes sure that muted local video tracks associated with the parent
  33. * {@link TraceablePeerConnection} are described in the local SDP. It's done
  34. * in order to prevent from sending 'source-remove'/'source-add' Jingle
  35. * notifications when local video track is muted (<tt>MediaStream</tt> is
  36. * removed from the peerconnection).
  37. *
  38. * NOTE 1 video track is assumed
  39. *
  40. * @param {SdpTransformWrap} transformer the transformer instance which will
  41. * be used to process the SDP.
  42. * @return {boolean} <tt>true</tt> if there were any modifications to
  43. * the SDP wrapped by <tt>transformer</tt>.
  44. * @private
  45. */
  46. _addMutedLocalVideoTracksToSDP(transformer) {
  47. // Go over each video tracks and check if the SDP has to be changed
  48. const localVideos = this.tpc.getLocalTracks(MediaType.VIDEO);
  49. if (!localVideos.length) {
  50. return false;
  51. } else if (localVideos.length !== 1) {
  52. logger.error(
  53. `${this.tpc} there is more than 1 video track ! `
  54. + 'Strange things may happen !', localVideos);
  55. }
  56. const videoMLine = transformer.selectMedia(MediaType.VIDEO)?.[0];
  57. if (!videoMLine) {
  58. logger.debug(
  59. `${this.tpc} unable to hack local video track SDP`
  60. + '- no "video" media');
  61. return false;
  62. }
  63. let modified = false;
  64. for (const videoTrack of localVideos) {
  65. const muted = videoTrack.isMuted();
  66. const mediaStream = videoTrack.getOriginalStream();
  67. const isCamera = videoTrack.videoType === VideoType.CAMERA;
  68. // During the mute/unmute operation there are periods of time when
  69. // the track's underlying MediaStream is not added yet to
  70. // the PeerConnection. The SDP needs to be munged in such case.
  71. const isInPeerConnection
  72. = mediaStream && this.tpc.isMediaStreamInPc(mediaStream);
  73. const shouldFakeSdp = isCamera && (muted || !isInPeerConnection);
  74. if (!shouldFakeSdp) {
  75. continue; // eslint-disable-line no-continue
  76. }
  77. // Inject removed SSRCs
  78. const requiredSSRCs
  79. = this.tpc.isSimulcastOn()
  80. ? this.tpc.simulcast.ssrcCache
  81. : [ this.tpc.sdpConsistency.cachedPrimarySsrc ];
  82. if (!requiredSSRCs.length) {
  83. logger.error(`No SSRCs stored for: ${videoTrack} in ${this.tpc}`);
  84. continue; // eslint-disable-line no-continue
  85. }
  86. modified = true;
  87. // We need to fake sendrecv.
  88. // NOTE the SDP produced here goes only to Jicofo and is never set
  89. // as localDescription. That's why
  90. // TraceablePeerConnection.mediaTransferActive is ignored here.
  91. videoMLine.direction = MediaDirection.SENDRECV;
  92. // Check if the recvonly has MSID
  93. const primarySSRC = requiredSSRCs[0];
  94. // FIXME The cname could come from the stream, but may turn out to
  95. // be too complex. It is fine to come up with any value, as long as
  96. // we only care about the actual SSRC values when deciding whether
  97. // or not an update should be sent.
  98. const primaryCname = `injected-${primarySSRC}`;
  99. for (const ssrcNum of requiredSSRCs) {
  100. // Remove old attributes
  101. videoMLine.removeSSRC(ssrcNum);
  102. // Inject
  103. videoMLine.addSSRCAttribute({
  104. id: ssrcNum,
  105. attribute: 'cname',
  106. value: primaryCname
  107. });
  108. videoMLine.addSSRCAttribute({
  109. id: ssrcNum,
  110. attribute: 'msid',
  111. value: videoTrack.storedMSID
  112. });
  113. }
  114. if (requiredSSRCs.length > 1) {
  115. const group = {
  116. ssrcs: requiredSSRCs.join(' '),
  117. semantics: 'SIM'
  118. };
  119. if (!videoMLine.findGroup(group.semantics, group.ssrcs)) {
  120. // Inject the group
  121. videoMLine.addSSRCGroup(group);
  122. }
  123. }
  124. // Insert RTX
  125. // FIXME in P2P RTX is used by Chrome regardless of config option
  126. // status. Because of that 'source-remove'/'source-add'
  127. // notifications are still sent to remove/add RTX SSRC and FID group
  128. if (!this.tpc.options.disableRtx) {
  129. this.tpc.rtxModifier.modifyRtxSsrcs2(videoMLine);
  130. }
  131. }
  132. return modified;
  133. }
  134. /**
  135. * Returns a string that can be set as the MSID attribute for a source.
  136. *
  137. * @param {string} mediaType - Media type of the source.
  138. * @param {string} trackId - Id of the MediaStreamTrack associated with the source.
  139. * @param {string} streamId - Id of the MediaStream associated with the source.
  140. * @returns {string|null}
  141. */
  142. _generateMsidAttribute(mediaType, trackId, streamId = null) {
  143. if (!(mediaType && trackId)) {
  144. logger.error(`Unable to munge local MSID - track id=${trackId} or media type=${mediaType} is missing`);
  145. return null;
  146. }
  147. const pcId = this.tpc.id;
  148. // Handle a case on Firefox when the browser doesn't produce a 'a:ssrc' line with the 'msid' attribute or has
  149. // '-' for the stream id part of the msid line. Jicofo needs an unique identifier to be associated with a ssrc
  150. // and uses the msid for that.
  151. if (streamId === '-' || !streamId) {
  152. return `${this.localEndpointId}-${mediaType}-${pcId} ${trackId}-${pcId}`;
  153. }
  154. return `${streamId}-${pcId} ${trackId}-${pcId}`;
  155. }
  156. /**
  157. * Modifies 'cname', 'msid', 'label' and 'mslabel' by appending the id of {@link LocalSdpMunger#tpc} at the end,
  158. * preceding by a dash sign.
  159. *
  160. * @param {MLineWrap} mediaSection - The media part (audio or video) of the session description which will be
  161. * modified in place.
  162. * @returns {void}
  163. * @private
  164. */
  165. _transformMediaIdentifiers(mediaSection) {
  166. const mediaType = mediaSection.mLine?.type;
  167. const pcId = this.tpc.id;
  168. for (const ssrcLine of mediaSection.ssrcs) {
  169. switch (ssrcLine.attribute) {
  170. case 'cname':
  171. case 'label':
  172. case 'mslabel':
  173. ssrcLine.value = ssrcLine.value && `${ssrcLine.value}-${pcId}`;
  174. break;
  175. case 'msid': {
  176. if (ssrcLine.value) {
  177. const streamAndTrackIDs = ssrcLine.value.split(' ');
  178. let streamId = streamAndTrackIDs[0];
  179. const trackId = streamAndTrackIDs[1];
  180. // Always overwrite streamId since we want the msid to be in this format even if the browser
  181. // generates one (in p2p mode).
  182. streamId = `${this.localEndpointId}-${mediaType}`;
  183. // eslint-disable-next-line max-depth
  184. if (mediaType === MediaType.VIDEO) {
  185. // eslint-disable-next-line max-depth
  186. if (!this.videoSourcesToMsidMap.has(trackId)) {
  187. streamId = `${streamId}-${this.videoSourcesToMsidMap.size}`;
  188. this.videoSourcesToMsidMap.set(trackId, streamId);
  189. }
  190. } else if (!this.audioSourcesToMsidMap.has(trackId)) {
  191. streamId = `${streamId}-${this.audioSourcesToMsidMap.size}`;
  192. this.audioSourcesToMsidMap.set(trackId, streamId);
  193. }
  194. streamId = mediaType === MediaType.VIDEO
  195. ? this.videoSourcesToMsidMap.get(trackId)
  196. : this.audioSourcesToMsidMap.get(trackId);
  197. ssrcLine.value = this._generateMsidAttribute(mediaType, trackId, streamId);
  198. } else {
  199. logger.warn(`Unable to munge local MSID - weird format detected: ${ssrcLine.value}`);
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. // Additional transformations related to MSID are applicable to Unified-plan implementation only.
  206. if (!this.tpc.usesUnifiedPlan()) {
  207. return;
  208. }
  209. const mediaDirection = mediaSection.mLine?.direction;
  210. // On FF when the user has started muted create answer will generate a recv only SSRC. We don't want to signal
  211. // this SSRC in order to reduce the load of the xmpp server for large calls. Therefore the SSRC needs to be
  212. // removed from the SDP.
  213. //
  214. // For all other use cases (when the user has had media but then the user has stopped it) we want to keep the
  215. // receive only SSRCs in the SDP. Otherwise source-remove will be triggered and the next time the user add a
  216. // track we will reuse the SSRCs and send source-add with the same SSRCs. This is problematic because of issues
  217. // on Chrome and FF (https://bugzilla.mozilla.org/show_bug.cgi?id=1768729) when removing and then adding the
  218. // same SSRC in the remote sdp the remote track is not rendered.
  219. if (browser.isFirefox()
  220. && (mediaDirection === MediaDirection.RECVONLY || mediaDirection === MediaDirection.INACTIVE)
  221. && (
  222. (mediaType === MediaType.VIDEO && !this.tpc._hasHadVideoTrack)
  223. || (mediaType === MediaType.AUDIO && !this.tpc._hasHadAudioTrack)
  224. )
  225. ) {
  226. mediaSection.ssrcs = undefined;
  227. mediaSection.ssrcGroups = undefined;
  228. }
  229. const msidLine = mediaSection.mLine?.msid;
  230. const trackId = msidLine && msidLine.split(' ')[1];
  231. const sources = [ ...new Set(mediaSection.mLine?.ssrcs?.map(s => s.id)) ];
  232. for (const source of sources) {
  233. const msidExists = mediaSection.ssrcs
  234. .find(ssrc => ssrc.id === source && ssrc.attribute === 'msid');
  235. if (!msidExists && trackId) {
  236. const generatedMsid = this._generateMsidAttribute(mediaType, trackId);
  237. mediaSection.ssrcs.push({
  238. id: source,
  239. attribute: 'msid',
  240. value: generatedMsid
  241. });
  242. }
  243. }
  244. }
  245. /**
  246. * Maybe modifies local description to fake local video tracks SDP when
  247. * those are muted.
  248. *
  249. * @param {object} desc the WebRTC SDP object instance for the local
  250. * description.
  251. * @returns {RTCSessionDescription}
  252. */
  253. maybeAddMutedLocalVideoTracksToSDP(desc) {
  254. if (!desc) {
  255. throw new Error('No local description passed in.');
  256. }
  257. const transformer = new SdpTransformWrap(desc.sdp);
  258. if (this._addMutedLocalVideoTracksToSDP(transformer)) {
  259. return new RTCSessionDescription({
  260. type: desc.type,
  261. sdp: transformer.toRawSDP()
  262. });
  263. }
  264. return desc;
  265. }
  266. /**
  267. * This transformation will make sure that stream identifiers are unique
  268. * across all of the local PeerConnections even if the same stream is used
  269. * by multiple instances at the same time.
  270. * Each PeerConnection assigns different SSRCs to the same local
  271. * MediaStream, but the MSID remains the same as it's used to identify
  272. * the stream by the WebRTC backend. The transformation will append
  273. * {@link TraceablePeerConnection#id} at the end of each stream's identifier
  274. * ("cname", "msid", "label" and "mslabel").
  275. *
  276. * @param {RTCSessionDescription} sessionDesc - The local session
  277. * description (this instance remains unchanged).
  278. * @return {RTCSessionDescription} - Transformed local session description
  279. * (a modified copy of the one given as the input).
  280. */
  281. transformStreamIdentifiers(sessionDesc) {
  282. // FIXME similar check is probably duplicated in all other transformers
  283. if (!sessionDesc || !sessionDesc.sdp || !sessionDesc.type) {
  284. return sessionDesc;
  285. }
  286. const transformer = new SdpTransformWrap(sessionDesc.sdp);
  287. const audioMLine = transformer.selectMedia(MediaType.AUDIO)?.[0];
  288. if (audioMLine) {
  289. this._transformMediaIdentifiers(audioMLine);
  290. this._injectSourceNames(audioMLine);
  291. }
  292. const videoMlines = transformer.selectMedia(MediaType.VIDEO);
  293. if (!FeatureFlags.isMultiStreamSendSupportEnabled()) {
  294. videoMlines.splice(1);
  295. }
  296. for (const videoMLine of videoMlines) {
  297. this._transformMediaIdentifiers(videoMLine);
  298. this._injectSourceNames(videoMLine);
  299. }
  300. // Plan-b clients generate new SSRCs and trackIds whenever tracks are removed and added back to the
  301. // peerconnection, therefore local track based map for msids needs to be reset after every transformation.
  302. if (!this.tpc._usesUnifiedPlan) {
  303. this.audioSourcesToMsidMap.clear();
  304. this.videoSourcesToMsidMap.clear();
  305. }
  306. return new RTCSessionDescription({
  307. type: sessionDesc.type,
  308. sdp: transformer.toRawSDP()
  309. });
  310. }
  311. /**
  312. * Injects source names. Source names are need to for multiple streams per endpoint support. The final plan is to
  313. * use the "mid" attribute for source names, but because the SDP to Jingle conversion still operates in the Plan-B
  314. * semantics (one source name per media), a custom "name" attribute is injected into SSRC lines..
  315. *
  316. * @param {MLineWrap} mediaSection - The media part (audio or video) of the session description which will be
  317. * modified in place.
  318. * @returns {void}
  319. * @private
  320. */
  321. _injectSourceNames(mediaSection) {
  322. const sources = [ ...new Set(mediaSection.mLine?.ssrcs?.map(s => s.id)) ];
  323. const mediaType = mediaSection.mLine?.type;
  324. if (!mediaType) {
  325. throw new Error('_transformMediaIdentifiers - no media type in mediaSection');
  326. }
  327. for (const source of sources) {
  328. const nameExists = mediaSection.ssrcs.find(ssrc => ssrc.id === source && ssrc.attribute === 'name');
  329. const msid = mediaSection.ssrcs.find(ssrc => ssrc.id === source && ssrc.attribute === 'msid')?.value;
  330. let trackIndex;
  331. if (msid) {
  332. const streamId = msid.split(' ')[0];
  333. // Example stream id: d8ff91-video-8-1
  334. // In the example above 8 is the track index
  335. const trackIndexParts = streamId.split('-');
  336. trackIndex = trackIndexParts[trackIndexParts.length - 2];
  337. }
  338. const sourceName = getSourceNameForJitsiTrack(this.localEndpointId, mediaType, trackIndex);
  339. if (!nameExists) {
  340. // Inject source names as a=ssrc:3124985624 name:endpointA-v0
  341. mediaSection.ssrcs.push({
  342. id: source,
  343. attribute: 'name',
  344. value: sourceName
  345. });
  346. }
  347. if (mediaType === MediaType.VIDEO) {
  348. const videoType = this.tpc.getLocalVideoTracks().find(track => track.getSourceName() === sourceName)
  349. ?.getVideoType();
  350. if (videoType) {
  351. // Inject videoType as a=ssrc:1234 videoType:desktop.
  352. mediaSection.ssrcs.push({
  353. id: source,
  354. attribute: 'videoType',
  355. value: videoType
  356. });
  357. }
  358. }
  359. }
  360. }
  361. }