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

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