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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import * as MediaType from '../../service/RTC/MediaType';
  4. import { SdpTransformWrap } from './SdpTransformUtil';
  5. const logger = getLogger(__filename);
  6. /**
  7. * Fakes local SDP exposed to {@link JingleSessionPC} through the local
  8. * description getter. Modifies the SDP, so that it will contain muted local
  9. * video tracks description, even though their underlying {MediaStreamTrack}s
  10. * are no longer in the WebRTC peerconnection. That prevents from SSRC updates
  11. * being sent to Jicofo/remote peer and prevents sRD/sLD cycle on the remote
  12. * side.
  13. */
  14. export default class LocalSdpMunger {
  15. /**
  16. * Creates new <tt>LocalSdpMunger</tt> instance.
  17. *
  18. * @param {TraceablePeerConnection} tpc
  19. */
  20. constructor(tpc) {
  21. this.tpc = tpc;
  22. }
  23. /**
  24. * Makes sure that muted local video tracks associated with the parent
  25. * {@link TraceablePeerConnection} are described in the local SDP. It's done
  26. * in order to prevent from sending 'source-remove'/'source-add' Jingle
  27. * notifications when local video track is muted (<tt>MediaStream</tt> is
  28. * removed from the peerconnection).
  29. *
  30. * NOTE 1 video track is assumed
  31. *
  32. * @param {SdpTransformWrap} transformer the transformer instance which will
  33. * be used to process the SDP.
  34. * @return {boolean} <tt>true</tt> if there were any modifications to
  35. * the SDP wrapped by <tt>transformer</tt>.
  36. * @private
  37. */
  38. _addMutedLocalVideoTracksToSDP(transformer) {
  39. // Go over each video tracks and check if the SDP has to be changed
  40. const localVideos = this.tpc.getLocalTracks(MediaType.VIDEO);
  41. if (!localVideos.length) {
  42. return false;
  43. } else if (localVideos.length !== 1) {
  44. logger.error(
  45. `${this.tpc} there is more than 1 video track ! `
  46. + 'Strange things may happen !', localVideos);
  47. }
  48. const videoMLine = transformer.selectMedia('video');
  49. if (!videoMLine) {
  50. logger.debug(
  51. `${this.tpc} unable to hack local video track SDP`
  52. + '- no "video" media');
  53. return false;
  54. }
  55. let modified = false;
  56. for (const videoTrack of localVideos) {
  57. const muted = videoTrack.isMuted();
  58. const mediaStream = videoTrack.getOriginalStream();
  59. // During the mute/unmute operation there are periods of time when
  60. // the track's underlying MediaStream is not added yet to
  61. // the PeerConnection. The SDP needs to be munged in such case.
  62. const isInPeerConnection
  63. = mediaStream && this.tpc.isMediaStreamInPc(mediaStream);
  64. const shouldFakeSdp = muted || !isInPeerConnection;
  65. logger.debug(
  66. `${this.tpc} ${videoTrack} muted: ${
  67. muted}, is in PeerConnection: ${
  68. isInPeerConnection} => should fake sdp ? : ${
  69. shouldFakeSdp}`);
  70. if (!shouldFakeSdp) {
  71. continue; // eslint-disable-line no-continue
  72. }
  73. // Inject removed SSRCs
  74. const requiredSSRCs
  75. = this.tpc.isSimulcastOn()
  76. ? this.tpc.simulcast.ssrcCache
  77. : [ this.tpc.sdpConsistency.cachedPrimarySsrc ];
  78. if (!requiredSSRCs.length) {
  79. logger.error(
  80. `No SSRCs stored for: ${videoTrack} in ${this.tpc}`);
  81. continue; // eslint-disable-line no-continue
  82. }
  83. modified = true;
  84. // We need to fake sendrecv.
  85. // NOTE the SDP produced here goes only to Jicofo and is never set
  86. // as localDescription. That's why
  87. // TraceablePeerConnection.mediaTransferActive is ignored here.
  88. videoMLine.direction = 'sendrecv';
  89. // Check if the recvonly has MSID
  90. const primarySSRC = requiredSSRCs[0];
  91. // FIXME The cname could come from the stream, but may turn out to
  92. // be too complex. It is fine to come up with any value, as long as
  93. // we only care about the actual SSRC values when deciding whether
  94. // or not an update should be sent.
  95. const primaryCname = `injected-${primarySSRC}`;
  96. for (const ssrcNum of requiredSSRCs) {
  97. // Remove old attributes
  98. videoMLine.removeSSRC(ssrcNum);
  99. // Inject
  100. logger.debug(
  101. `${this.tpc} injecting video SSRC: ${ssrcNum} for ${
  102. videoTrack}`);
  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. logger.debug(
  122. `${this.tpc} injecting SIM group for ${videoTrack}`,
  123. group);
  124. videoMLine.addSSRCGroup(group);
  125. }
  126. }
  127. // Insert RTX
  128. // FIXME in P2P RTX is used by Chrome regardless of config option
  129. // status. Because of that 'source-remove'/'source-add'
  130. // notifications are still sent to remove/add RTX SSRC and FID group
  131. if (!this.tpc.options.disableRtx) {
  132. this.tpc.rtxModifier.modifyRtxSsrcs2(videoMLine);
  133. }
  134. }
  135. return modified;
  136. }
  137. /**
  138. * Modifies 'cname', 'msid', 'label' and 'mslabel' by appending
  139. * the id of {@link LocalSdpMunger#tpc} at the end, preceding by a dash
  140. * sign.
  141. *
  142. * @param {MLineWrap} mediaSection - The media part (audio or video) of the
  143. * session description which will be modified in place.
  144. * @returns {void}
  145. * @private
  146. */
  147. _transformMediaIdentifiers(mediaSection) {
  148. const pcId = this.tpc.id;
  149. for (const ssrcLine of mediaSection.ssrcs) {
  150. switch (ssrcLine.attribute) {
  151. case 'cname':
  152. case 'label':
  153. case 'mslabel':
  154. ssrcLine.value = ssrcLine.value && `${ssrcLine.value}-${pcId}`;
  155. break;
  156. case 'msid': {
  157. if (ssrcLine.value) {
  158. const streamAndTrackIDs = ssrcLine.value.split(' ');
  159. if (streamAndTrackIDs.length === 2) {
  160. const streamId = streamAndTrackIDs[0];
  161. const trackId = streamAndTrackIDs[1];
  162. ssrcLine.value
  163. = `${streamId}-${pcId} ${trackId}-${pcId}`;
  164. } else {
  165. logger.warn(
  166. 'Unable to munge local MSID'
  167. + `- weird format detected: ${ssrcLine.value}`);
  168. }
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Maybe modifies local description to fake local video tracks SDP when
  177. * those are muted.
  178. *
  179. * @param {object} desc the WebRTC SDP object instance for the local
  180. * description.
  181. * @returns {RTCSessionDescription}
  182. */
  183. maybeAddMutedLocalVideoTracksToSDP(desc) {
  184. if (!desc) {
  185. throw new Error('No local description passed in.');
  186. }
  187. const transformer = new SdpTransformWrap(desc.sdp);
  188. if (this._addMutedLocalVideoTracksToSDP(transformer)) {
  189. return new RTCSessionDescription({
  190. type: desc.type,
  191. sdp: transformer.toRawSDP()
  192. });
  193. }
  194. return desc;
  195. }
  196. /**
  197. * This transformation will make sure that stream identifiers are unique
  198. * across all of the local PeerConnections even if the same stream is used
  199. * by multiple instances at the same time.
  200. * Each PeerConnection assigns different SSRCs to the same local
  201. * MediaStream, but the MSID remains the same as it's used to identify
  202. * the stream by the WebRTC backend. The transformation will append
  203. * {@link TraceablePeerConnection#id} at the end of each stream's identifier
  204. * ("cname", "msid", "label" and "mslabel").
  205. *
  206. * @param {RTCSessionDescription} sessionDesc - The local session
  207. * description (this instance remains unchanged).
  208. * @return {RTCSessionDescription} - Transformed local session description
  209. * (a modified copy of the one given as the input).
  210. */
  211. transformStreamIdentifiers(sessionDesc) {
  212. // FIXME similar check is probably duplicated in all other transformers
  213. if (!sessionDesc || !sessionDesc.sdp || !sessionDesc.type) {
  214. return sessionDesc;
  215. }
  216. const transformer = new SdpTransformWrap(sessionDesc.sdp);
  217. const audioMLine = transformer.selectMedia('audio');
  218. if (audioMLine) {
  219. this._transformMediaIdentifiers(audioMLine);
  220. }
  221. const videoMLine = transformer.selectMedia('video');
  222. if (videoMLine) {
  223. this._transformMediaIdentifiers(videoMLine);
  224. }
  225. return new RTCSessionDescription({
  226. type: sessionDesc.type,
  227. sdp: transformer.toRawSDP()
  228. });
  229. }
  230. }