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.

TPCUtils.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import transform from 'sdp-transform';
  3. import * as JitsiTrackEvents from '../../JitsiTrackEvents';
  4. import browser from '../browser';
  5. import RTCEvents from '../../service/RTC/RTCEvents';
  6. const logger = getLogger(__filename);
  7. const SIM_LAYER_1_RID = '1';
  8. const SIM_LAYER_2_RID = '2';
  9. const SIM_LAYER_3_RID = '3';
  10. export const SIM_LAYER_RIDS = [ SIM_LAYER_1_RID, SIM_LAYER_2_RID, SIM_LAYER_3_RID ];
  11. /**
  12. * Handles track related operations on TraceablePeerConnection when browser is
  13. * running in unified plan mode.
  14. */
  15. export class TPCUtils {
  16. /**
  17. * @constructor
  18. */
  19. constructor(peerconnection) {
  20. this.pc = peerconnection;
  21. /**
  22. * The simulcast encodings that will be configured on the RTCRtpSender
  23. * for the video tracks in the unified plan mode.
  24. */
  25. this.simulcastEncodings = [
  26. {
  27. active: true,
  28. maxBitrate: browser.isFirefox() ? 2500000 : 200000,
  29. rid: SIM_LAYER_1_RID,
  30. scaleResolutionDownBy: browser.isFirefox() ? 1.0 : 4.0
  31. },
  32. {
  33. active: true,
  34. maxBitrate: 700000,
  35. rid: SIM_LAYER_2_RID,
  36. scaleResolutionDownBy: 2.0
  37. },
  38. {
  39. active: true,
  40. maxBitrate: browser.isFirefox() ? 200000 : 2500000,
  41. rid: SIM_LAYER_3_RID,
  42. scaleResolutionDownBy: browser.isFirefox() ? 4.0 : 1.0
  43. }
  44. ];
  45. }
  46. /**
  47. * Ensures that the ssrcs associated with a FID ssrc-group appear in the correct order, i.e.,
  48. * the primary ssrc first and the secondary rtx ssrc later. This is important for unified
  49. * plan since we have only one FID group per media description.
  50. * @param {Object} description the webRTC session description instance for the remote
  51. * description.
  52. * @private
  53. */
  54. _ensureCorrectOrderOfSsrcs(description) {
  55. const parsedSdp = transform.parse(description.sdp);
  56. parsedSdp.media.forEach(mLine => {
  57. if (mLine.type === 'audio') {
  58. return;
  59. }
  60. if (!mLine.ssrcGroups || !mLine.ssrcGroups.length) {
  61. return;
  62. }
  63. let reorderedSsrcs = [];
  64. mLine.ssrcGroups[0].ssrcs.split(' ').forEach(ssrc => {
  65. const sources = mLine.ssrcs.filter(source => source.id.toString() === ssrc);
  66. reorderedSsrcs = reorderedSsrcs.concat(sources);
  67. });
  68. mLine.ssrcs = reorderedSsrcs;
  69. });
  70. return new RTCSessionDescription({
  71. type: description.type,
  72. sdp: transform.write(parsedSdp)
  73. });
  74. }
  75. /**
  76. * Obtains stream encodings that need to be configured on the given track.
  77. * @param {JitsiLocalTrack} localTrack
  78. */
  79. _getStreamEncodings(localTrack) {
  80. if (this.pc.isSimulcastOn() && localTrack.isVideoTrack()) {
  81. return this.simulcastEncodings;
  82. }
  83. return [ { active: true } ];
  84. }
  85. /**
  86. * Takes in a *unified plan* offer and inserts the appropriate
  87. * parameters for adding simulcast receive support.
  88. * @param {Object} desc - A session description object
  89. * @param {String} desc.type - the type (offer/answer)
  90. * @param {String} desc.sdp - the sdp content
  91. *
  92. * @return {Object} A session description (same format as above) object
  93. * with its sdp field modified to advertise simulcast receive support
  94. */
  95. _insertUnifiedPlanSimulcastReceive(desc) {
  96. // a=simulcast line is not needed on browsers where
  97. // we munge SDP for turning on simulcast. Remove this check
  98. // when we move to RID/MID based simulcast on all browsers.
  99. if (browser.usesSdpMungingForSimulcast()) {
  100. return desc;
  101. }
  102. const sdp = transform.parse(desc.sdp);
  103. const idx = sdp.media.findIndex(mline => mline.type === 'video');
  104. if (sdp.media[idx].rids && (sdp.media[idx].simulcast_03 || sdp.media[idx].simulcast)) {
  105. // Make sure we don't have the simulcast recv line on video descriptions other than the
  106. // the first video description.
  107. sdp.media.forEach((mline, i) => {
  108. if (mline.type === 'video' && i !== idx) {
  109. sdp.media[i].rids = undefined;
  110. sdp.media[i].simulcast = undefined;
  111. // eslint-disable-next-line camelcase
  112. sdp.media[i].simulcast_03 = undefined;
  113. }
  114. });
  115. return new RTCSessionDescription({
  116. type: desc.type,
  117. sdp: transform.write(sdp)
  118. });
  119. }
  120. // In order of highest to lowest spatial quality
  121. sdp.media[idx].rids = [
  122. {
  123. id: SIM_LAYER_1_RID,
  124. direction: 'recv'
  125. },
  126. {
  127. id: SIM_LAYER_2_RID,
  128. direction: 'recv'
  129. },
  130. {
  131. id: SIM_LAYER_3_RID,
  132. direction: 'recv'
  133. }
  134. ];
  135. // Firefox 72 has stopped parsing the legacy rid= parameters in simulcast attributes.
  136. // eslint-disable-next-line max-len
  137. // https://www.fxsitecompat.dev/en-CA/docs/2019/pt-and-rid-in-webrtc-simulcast-attributes-are-no-longer-supported/
  138. const simulcastLine = browser.isFirefox() && browser.isVersionGreaterThan(71)
  139. ? `recv ${SIM_LAYER_RIDS.join(';')}`
  140. : `recv rid=${SIM_LAYER_RIDS.join(';')}`;
  141. // eslint-disable-next-line camelcase
  142. sdp.media[idx].simulcast_03 = {
  143. value: simulcastLine
  144. };
  145. return new RTCSessionDescription({
  146. type: desc.type,
  147. sdp: transform.write(sdp)
  148. });
  149. }
  150. /**
  151. * Adds {@link JitsiLocalTrack} to the WebRTC peerconnection for the first time.
  152. * @param {JitsiLocalTrack} track - track to be added to the peerconnection.
  153. * @returns {boolean} Returns true if the operation is successful,
  154. * false otherwise.
  155. */
  156. addTrack(localTrack, isInitiator = true) {
  157. const track = localTrack.getTrack();
  158. if (isInitiator) {
  159. // Use pc.addTransceiver() for the initiator case when local tracks are getting added
  160. // to the peerconnection before a session-initiate is sent over to the peer.
  161. const transceiverInit = {
  162. direction: 'sendrecv',
  163. streams: [ localTrack.getOriginalStream() ],
  164. sendEncodings: []
  165. };
  166. if (!browser.isFirefox()) {
  167. transceiverInit.sendEncodings = this._getStreamEncodings(localTrack);
  168. }
  169. this.pc.peerconnection.addTransceiver(track, transceiverInit);
  170. } else {
  171. // Use pc.addTrack() for responder case so that we can re-use the m-lines that were created
  172. // when setRemoteDescription was called. pc.addTrack() automatically attaches to any existing
  173. // unused "recv-only" transceiver.
  174. this.pc.peerconnection.addTrack(track);
  175. }
  176. }
  177. /**
  178. * Adds a track on the RTCRtpSender as part of the unmute operation.
  179. * @param {JitsiLocalTrack} localTrack - track to be unmuted.
  180. * @returns {Promise<boolean>} - Promise that resolves to false if unmute
  181. * operation is successful, a reject otherwise.
  182. */
  183. addTrackUnmute(localTrack) {
  184. const mediaType = localTrack.getType();
  185. const track = localTrack.getTrack();
  186. // The assumption here is that the first transceiver of the specified
  187. // media type is that of the local track.
  188. const transceiver = this.pc.peerconnection.getTransceivers()
  189. .find(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  190. if (!transceiver) {
  191. return Promise.reject(new Error(`RTCRtpTransceiver for ${mediaType} not found`));
  192. }
  193. logger.debug(`Adding ${localTrack} on ${this.pc}`);
  194. // If the client starts with audio/video muted setting, the transceiver direction
  195. // will be set to 'recvonly'. Use addStream here so that a MSID is generated for the stream.
  196. if (transceiver.direction === 'recvonly') {
  197. this.pc.peerconnection.addStream(localTrack.getOriginalStream());
  198. this.setEncodings(localTrack);
  199. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  200. transceiver.direction = 'sendrecv';
  201. return Promise.resolve(false);
  202. }
  203. return transceiver.sender.replaceTrack(track)
  204. .then(() => {
  205. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  206. return Promise.resolve(false);
  207. });
  208. }
  209. /**
  210. * Removes the track from the RTCRtpSender as part of the mute operation.
  211. * @param {JitsiLocalTrack} localTrack - track to be removed.
  212. * @returns {Promise<boolean>} - Promise that resolves to false if unmute
  213. * operation is successful, a reject otherwise.
  214. */
  215. removeTrackMute(localTrack) {
  216. const mediaType = localTrack.getType();
  217. const transceiver = this.pc.peerconnection.getTransceivers()
  218. .find(t => t.sender && t.sender.track && t.sender.track.id === localTrack.getTrackId());
  219. if (!transceiver) {
  220. return Promise.reject(new Error(`RTCRtpTransceiver for ${mediaType} not found`));
  221. }
  222. logger.debug(`Removing ${localTrack} on ${this.pc}`);
  223. return transceiver.sender.replaceTrack(null)
  224. .then(() => {
  225. this.pc.localTracks.delete(localTrack.rtcId);
  226. return Promise.resolve(false);
  227. });
  228. }
  229. /**
  230. * Replaces the existing track on a RTCRtpSender with the given track.
  231. * @param {JitsiLocalTrack} oldTrack - existing track on the sender that needs to be removed.
  232. * @param {JitsiLocalTrack} newTrack - new track that needs to be added to the sender.
  233. * @returns {Promise<false>} Promise that resolves with false as we don't want
  234. * renegotiation to be triggered automatically after this operation. Renegotiation is
  235. * done when the browser fires the negotiationeeded event.
  236. */
  237. replaceTrack(oldTrack, newTrack) {
  238. if (oldTrack && newTrack) {
  239. const mediaType = newTrack.getType();
  240. const stream = newTrack.getOriginalStream();
  241. const track = stream.getVideoTracks()[0];
  242. const transceiver = this.pc.peerconnection.getTransceivers()
  243. .find(t => t.receiver.track.kind === mediaType && !t.stopped);
  244. if (!transceiver) {
  245. return Promise.reject(new Error('replace track failed'));
  246. }
  247. logger.debug(`Replacing ${oldTrack} with ${newTrack} on ${this.pc}`);
  248. return transceiver.sender.replaceTrack(track)
  249. .then(() => {
  250. const ssrc = this.pc.localSSRCs.get(oldTrack.rtcId);
  251. this.pc.localTracks.delete(oldTrack.rtcId);
  252. this.pc.localSSRCs.delete(oldTrack.rtcId);
  253. this.pc._addedStreams = this.pc._addedStreams.filter(s => s !== stream);
  254. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  255. this.pc._addedStreams.push(stream);
  256. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  257. this.pc.eventEmitter.emit(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  258. newTrack,
  259. this.pc._extractPrimarySSRC(ssrc));
  260. });
  261. } else if (oldTrack && !newTrack) {
  262. if (!this.removeTrackMute(oldTrack)) {
  263. return Promise.reject(new Error('replace track failed'));
  264. }
  265. this.pc.localTracks.delete(oldTrack.rtcId);
  266. this.pc.localSSRCs.delete(oldTrack.rtcId);
  267. } else if (newTrack && !oldTrack) {
  268. const ssrc = this.pc.localSSRCs.get(newTrack.rtcId);
  269. if (!this.addTrackUnmute(newTrack)) {
  270. return Promise.reject(new Error('replace track failed'));
  271. }
  272. newTrack.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, newTrack);
  273. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  274. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  275. }
  276. return Promise.resolve(false);
  277. }
  278. /**
  279. * Enables/disables audio transmission on the peer connection. When
  280. * disabled the audio transceiver direction will be set to 'inactive'
  281. * which means that no data will be sent nor accepted, but
  282. * the connection should be kept alive.
  283. * @param {boolean} active - true to enable audio media transmission or
  284. * false to disable.
  285. * @returns {false} - returns false always so that renegotiation is not automatically
  286. * triggered after this operation.
  287. */
  288. setAudioTransferActive(active) {
  289. return this.setMediaTransferActive('audio', active);
  290. }
  291. /**
  292. * Set the simulcast stream encoding properties on the RTCRtpSender.
  293. * @param {JitsiLocalTrack} track - the current track in use for which
  294. * the encodings are to be set.
  295. */
  296. setEncodings(track) {
  297. const transceiver = this.pc.peerconnection.getTransceivers()
  298. .find(t => t.sender && t.sender.track && t.sender.track.kind === track.getType());
  299. const parameters = transceiver.sender.getParameters();
  300. parameters.encodings = this._getStreamEncodings(track);
  301. transceiver.sender.setParameters(parameters);
  302. }
  303. /**
  304. * Enables/disables media transmission on the peerconnection by changing the direction
  305. * on the transceiver for the specified media type.
  306. * @param {String} mediaType - 'audio' or 'video'
  307. * @param {boolean} active - true to enable media transmission or false
  308. * to disable.
  309. * @returns {false} - returns false always so that renegotiation is not automatically
  310. * triggered after this operation
  311. */
  312. setMediaTransferActive(mediaType, active) {
  313. const transceivers = this.pc.peerconnection.getTransceivers()
  314. .filter(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  315. const localTracks = Array.from(this.pc.localTracks.values())
  316. .filter(track => track.getType() === mediaType);
  317. if (active) {
  318. transceivers.forEach(transceiver => {
  319. if (localTracks.length) {
  320. transceiver.direction = 'sendrecv';
  321. const parameters = transceiver.sender.getParameters();
  322. if (parameters && parameters.encodings && parameters.encodings.length) {
  323. parameters.encodings.forEach(encoding => {
  324. encoding.active = true;
  325. });
  326. transceiver.sender.setParameters(parameters);
  327. }
  328. } else {
  329. transceiver.direction = 'recvonly';
  330. }
  331. });
  332. } else {
  333. transceivers.forEach(transceiver => {
  334. transceiver.direction = 'inactive';
  335. });
  336. }
  337. return false;
  338. }
  339. /**
  340. * Enables/disables video media transmission on the peer connection. When
  341. * disabled the SDP video media direction in the local SDP will be adjusted to
  342. * 'inactive' which means that no data will be sent nor accepted, but
  343. * the connection should be kept alive.
  344. * @param {boolean} active - true to enable video media transmission or
  345. * false to disable.
  346. * @returns {false} - returns false always so that renegotiation is not automatically
  347. * triggered after this operation.
  348. */
  349. setVideoTransferActive(active) {
  350. return this.setMediaTransferActive('video', active);
  351. }
  352. }