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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import { getLogger } from '@jitsi/logger';
  2. import transform from 'sdp-transform';
  3. import { MediaDirection } from '../../service/RTC/MediaDirection';
  4. import { MediaType } from '../../service/RTC/MediaType';
  5. import { getSourceIndexFromSourceName } from '../../service/RTC/SignalingLayer';
  6. import { VideoType } from '../../service/RTC/VideoType';
  7. import browser from '../browser';
  8. import FeatureFlags from '../flags/FeatureFlags';
  9. const logger = getLogger(__filename);
  10. const DESKTOP_SHARE_RATE = 500000;
  11. const LD_BITRATE = 200000;
  12. const SD_BITRATE = 700000;
  13. const SIM_LAYER_1_RID = '1';
  14. const SIM_LAYER_2_RID = '2';
  15. const SIM_LAYER_3_RID = '3';
  16. export const HD_BITRATE = 2500000;
  17. export const HD_SCALE_FACTOR = 1;
  18. export const LD_SCALE_FACTOR = 4;
  19. export const SD_SCALE_FACTOR = 2;
  20. export const SIM_LAYER_RIDS = [ SIM_LAYER_1_RID, SIM_LAYER_2_RID, SIM_LAYER_3_RID ];
  21. /**
  22. * Handles track related operations on TraceablePeerConnection when browser is
  23. * running in unified plan mode.
  24. */
  25. export class TPCUtils {
  26. /**
  27. * Creates a new instance for a given TraceablePeerConnection
  28. *
  29. * @param peerconnection - the tpc instance for which we have utility functions.
  30. */
  31. constructor(peerconnection) {
  32. this.pc = peerconnection;
  33. const bitrateSettings = this.pc.options?.videoQuality?.maxBitratesVideo;
  34. const standardBitrates = {
  35. low: LD_BITRATE,
  36. standard: SD_BITRATE,
  37. high: HD_BITRATE
  38. };
  39. // Check if the max. bitrates for video are specified through config.js videoQuality settings.
  40. // Right now only VP8 bitrates are configured on the simulcast encodings, VP9 bitrates have to be
  41. // configured on the SDP using b:AS line.
  42. this.videoBitrates = bitrateSettings ?? standardBitrates;
  43. const encodingBitrates = this.videoBitrates.VP8 ?? this.videoBitrates;
  44. /**
  45. * The startup configuration for the stream encodings that are applicable to
  46. * the video stream when a new sender is created on the peerconnection. The initial
  47. * config takes into account the differences in browser's simulcast implementation.
  48. *
  49. * Encoding parameters:
  50. * active - determine the on/off state of a particular encoding.
  51. * maxBitrate - max. bitrate value to be applied to that particular encoding
  52. * based on the encoding's resolution and config.js videoQuality settings if applicable.
  53. * rid - Rtp Stream ID that is configured for a particular simulcast stream.
  54. * scaleResolutionDownBy - the factor by which the encoding is scaled down from the
  55. * original resolution of the captured video.
  56. */
  57. this.localStreamEncodingsConfig = [
  58. {
  59. active: true,
  60. maxBitrate: browser.isFirefox() ? encodingBitrates.high : encodingBitrates.low,
  61. rid: SIM_LAYER_1_RID,
  62. scaleResolutionDownBy: browser.isFirefox() ? HD_SCALE_FACTOR : LD_SCALE_FACTOR
  63. },
  64. {
  65. active: true,
  66. maxBitrate: encodingBitrates.standard,
  67. rid: SIM_LAYER_2_RID,
  68. scaleResolutionDownBy: SD_SCALE_FACTOR
  69. },
  70. {
  71. active: true,
  72. maxBitrate: browser.isFirefox() ? encodingBitrates.low : encodingBitrates.high,
  73. rid: SIM_LAYER_3_RID,
  74. scaleResolutionDownBy: browser.isFirefox() ? LD_SCALE_FACTOR : HD_SCALE_FACTOR
  75. }
  76. ];
  77. }
  78. /**
  79. * Obtains stream encodings that need to be configured on the given track based
  80. * on the track media type and the simulcast setting.
  81. * @param {JitsiLocalTrack} localTrack
  82. */
  83. _getStreamEncodings(localTrack) {
  84. if (this.pc.isSimulcastOn() && localTrack.isVideoTrack()) {
  85. return this.localStreamEncodingsConfig;
  86. }
  87. return localTrack.isVideoTrack()
  88. ? [ {
  89. active: true,
  90. maxBitrate: this.videoBitrates.high
  91. } ]
  92. : [ { active: true } ];
  93. }
  94. /**
  95. * Ensures that the ssrcs associated with a FID ssrc-group appear in the correct order, i.e.,
  96. * the primary ssrc first and the secondary rtx ssrc later. This is important for unified
  97. * plan since we have only one FID group per media description.
  98. * @param {Object} description the webRTC session description instance for the remote
  99. * description.
  100. * @private
  101. */
  102. ensureCorrectOrderOfSsrcs(description) {
  103. const parsedSdp = transform.parse(description.sdp);
  104. parsedSdp.media.forEach(mLine => {
  105. if (mLine.type === MediaType.AUDIO) {
  106. return;
  107. }
  108. if (!mLine.ssrcGroups || !mLine.ssrcGroups.length) {
  109. return;
  110. }
  111. let reorderedSsrcs = [];
  112. const ssrcs = new Set();
  113. mLine.ssrcGroups.map(group =>
  114. group.ssrcs
  115. .split(' ')
  116. .filter(Boolean)
  117. .forEach(ssrc => ssrcs.add(ssrc))
  118. );
  119. ssrcs.forEach(ssrc => {
  120. const sources = mLine.ssrcs.filter(source => source.id.toString() === ssrc);
  121. reorderedSsrcs = reorderedSsrcs.concat(sources);
  122. });
  123. mLine.ssrcs = reorderedSsrcs;
  124. });
  125. return new RTCSessionDescription({
  126. type: description.type,
  127. sdp: transform.write(parsedSdp)
  128. });
  129. }
  130. /**
  131. * Returns the transceiver associated with a given RTCRtpSender/RTCRtpReceiver.
  132. *
  133. * @param {string} mediaType - type of track associated with the transceiver 'audio' or 'video'.
  134. * @param {JitsiLocalTrack} localTrack - local track to be used for lookup.
  135. * @returns {RTCRtpTransceiver}
  136. */
  137. findTransceiver(mediaType, localTrack = null) {
  138. const transceiver = localTrack?.track && localTrack.getOriginalStream()
  139. ? this.pc.peerconnection.getTransceivers().find(t => t.sender?.track?.id === localTrack.getTrackId())
  140. : this.pc.peerconnection.getTransceivers().find(t => t.receiver?.track?.kind === mediaType);
  141. return transceiver;
  142. }
  143. /**
  144. * Takes in a *unified plan* offer and inserts the appropriate
  145. * parameters for adding simulcast receive support.
  146. * @param {Object} desc - A session description object
  147. * @param {String} desc.type - the type (offer/answer)
  148. * @param {String} desc.sdp - the sdp content
  149. *
  150. * @return {Object} A session description (same format as above) object
  151. * with its sdp field modified to advertise simulcast receive support
  152. */
  153. insertUnifiedPlanSimulcastReceive(desc) {
  154. // a=simulcast line is not needed on browsers where we SDP munging is used for enabling on simulcast.
  155. // Remove this check when the client switches to RID/MID based simulcast on all browsers.
  156. if (browser.usesSdpMungingForSimulcast()) {
  157. return desc;
  158. }
  159. const sdp = transform.parse(desc.sdp);
  160. const idx = sdp.media.findIndex(mline => mline.type === MediaType.VIDEO);
  161. if (sdp.media[idx].rids && (sdp.media[idx].simulcast_03 || sdp.media[idx].simulcast)) {
  162. // Make sure we don't have the simulcast recv line on video descriptions other than
  163. // the first video description.
  164. sdp.media.forEach((mline, i) => {
  165. if (mline.type === MediaType.VIDEO && i !== idx) {
  166. sdp.media[i].rids = undefined;
  167. sdp.media[i].simulcast = undefined;
  168. // eslint-disable-next-line camelcase
  169. sdp.media[i].simulcast_03 = undefined;
  170. }
  171. });
  172. return new RTCSessionDescription({
  173. type: desc.type,
  174. sdp: transform.write(sdp)
  175. });
  176. }
  177. // In order of highest to lowest spatial quality
  178. sdp.media[idx].rids = [
  179. {
  180. id: SIM_LAYER_1_RID,
  181. direction: 'recv'
  182. },
  183. {
  184. id: SIM_LAYER_2_RID,
  185. direction: 'recv'
  186. },
  187. {
  188. id: SIM_LAYER_3_RID,
  189. direction: 'recv'
  190. }
  191. ];
  192. // Firefox 72 has stopped parsing the legacy rid= parameters in simulcast attributes.
  193. // eslint-disable-next-line max-len
  194. // https://www.fxsitecompat.dev/en-CA/docs/2019/pt-and-rid-in-webrtc-simulcast-attributes-are-no-longer-supported/
  195. const simulcastLine = browser.isFirefox() && browser.isVersionGreaterThan(71)
  196. ? `recv ${SIM_LAYER_RIDS.join(';')}`
  197. : `recv rid=${SIM_LAYER_RIDS.join(';')}`;
  198. // eslint-disable-next-line camelcase
  199. sdp.media[idx].simulcast_03 = {
  200. value: simulcastLine
  201. };
  202. return new RTCSessionDescription({
  203. type: desc.type,
  204. sdp: transform.write(sdp)
  205. });
  206. }
  207. /**
  208. * Adds {@link JitsiLocalTrack} to the WebRTC peerconnection for the first time.
  209. * @param {JitsiLocalTrack} track - track to be added to the peerconnection.
  210. * @param {boolean} isInitiator - boolean that indicates if the endpoint is offerer in a p2p connection.
  211. * @returns {void}
  212. */
  213. addTrack(localTrack, isInitiator) {
  214. const track = localTrack.getTrack();
  215. if (isInitiator) {
  216. const streams = [];
  217. if (localTrack.getOriginalStream()) {
  218. streams.push(localTrack.getOriginalStream());
  219. }
  220. // Use pc.addTransceiver() for the initiator case when local tracks are getting added
  221. // to the peerconnection before a session-initiate is sent over to the peer.
  222. const transceiverInit = {
  223. direction: MediaDirection.SENDRECV,
  224. streams,
  225. sendEncodings: []
  226. };
  227. if (!browser.isFirefox()) {
  228. transceiverInit.sendEncodings = this._getStreamEncodings(localTrack);
  229. }
  230. this.pc.peerconnection.addTransceiver(track, transceiverInit);
  231. } else {
  232. // Use pc.addTrack() for responder case so that we can re-use the m-lines that were created
  233. // when setRemoteDescription was called. pc.addTrack() automatically attaches to any existing
  234. // unused "recv-only" transceiver.
  235. this.pc.peerconnection.addTrack(track);
  236. }
  237. }
  238. /**
  239. * Returns the calculated active state of the simulcast encodings based on the frame height requested for the send
  240. * stream. All the encodings that have a resolution lower than the frame height requested will be enabled.
  241. *
  242. * @param {JitsiLocalTrack} localVideoTrack The local video track.
  243. * @param {number} newHeight The resolution requested for the video track.
  244. * @returns {Array<boolean>}
  245. */
  246. calculateEncodingsActiveState(localVideoTrack, newHeight) {
  247. const localTrack = localVideoTrack.getTrack();
  248. const { height } = localTrack.getSettings();
  249. const encodingsState = this.localStreamEncodingsConfig
  250. .map(encoding => height / encoding.scaleResolutionDownBy)
  251. .map((frameHeight, idx) => {
  252. let active = localVideoTrack.getVideoType() === VideoType.CAMERA
  253. // Keep the LD stream enabled even when the LD stream's resolution is higher than of the requested
  254. // resolution. This can happen when camera is captured at resolutions higher than 720p but the
  255. // requested resolution is 180. Since getParameters doesn't give us information about the resolutions
  256. // of the simulcast encodings, we have to rely on our initial config for the simulcast streams.
  257. ? newHeight > 0 && this.localStreamEncodingsConfig[idx]?.scaleResolutionDownBy === LD_SCALE_FACTOR
  258. ? true
  259. : frameHeight <= newHeight
  260. // Keep all the encodings for desktop track active.
  261. : true;
  262. // Disable the lower spatial layers for screensharing in Unified plan when low fps screensharing is in
  263. // progress. Sending all three streams often results in the browser suspending the high resolution in low
  264. // b/w and cpu cases, especially on the low end machines. Suspending the low resolution streams ensures
  265. // that the highest resolution stream is available always. Safari is an exception here since it does not
  266. // send the desktop stream at all if only the high resolution stream is enabled.
  267. if (localVideoTrack.getVideoType() === VideoType.DESKTOP
  268. && this.pc._capScreenshareBitrate
  269. && this.pc.usesUnifiedPlan()
  270. && !browser.isWebKitBased()
  271. && this.localStreamEncodingsConfig[idx].scaleResolutionDownBy !== HD_SCALE_FACTOR) {
  272. active = false;
  273. }
  274. return active;
  275. });
  276. return encodingsState;
  277. }
  278. /**
  279. * Returns the calculates max bitrates that need to be configured on the simulcast encodings based on the video
  280. * type and other considerations associated with screenshare.
  281. *
  282. * @param {JitsiLocalTrack} localVideoTrack The local video track.
  283. * @returns {Array<number>}
  284. */
  285. calculateEncodingsBitrates(localVideoTrack) {
  286. const videoType = localVideoTrack.getVideoType();
  287. const desktopShareBitrate = this.pc.options?.videoQuality?.desktopBitrate || DESKTOP_SHARE_RATE;
  288. const lowFpsScreenshare = localVideoTrack.getVideoType() === VideoType.DESKTOP
  289. && this.pc._capScreenshareBitrate
  290. && !browser.isWebKitBased();
  291. const encodingsBitrates = this.localStreamEncodingsConfig
  292. .map(encoding => {
  293. const bitrate = lowFpsScreenshare
  294. ? desktopShareBitrate
  295. // For high fps screenshare, 'maxBitrate' setting must be cleared on Chrome in plan-b, because
  296. // if simulcast is enabled for screen and maxBitrates are set then Chrome will not send the
  297. // desktop stream.
  298. : videoType === VideoType.DESKTOP && browser.isChromiumBased() && !this.pc.usesUnifiedPlan()
  299. ? undefined
  300. : encoding.maxBitrate;
  301. return bitrate;
  302. });
  303. return encodingsBitrates;
  304. }
  305. /**
  306. * Replaces the existing track on a RTCRtpSender with the given track.
  307. *
  308. * @param {JitsiLocalTrack} oldTrack - existing track on the sender that needs to be removed.
  309. * @param {JitsiLocalTrack} newTrack - new track that needs to be added to the sender.
  310. * @returns {Promise<RTCRtpTransceiver>} - resolved with the associated transceiver when done, rejected otherwise.
  311. */
  312. replaceTrack(oldTrack, newTrack) {
  313. const mediaType = newTrack?.getType() ?? oldTrack?.getType();
  314. const localTracks = this.pc.getLocalTracks(mediaType);
  315. const track = newTrack?.getTrack() ?? null;
  316. const isNewLocalSource = FeatureFlags.isMultiStreamSendSupportEnabled()
  317. && localTracks?.length
  318. && !oldTrack
  319. && newTrack
  320. && !localTracks.find(t => t === newTrack);
  321. let transceiver;
  322. // If old track exists, replace the track on the corresponding sender.
  323. if (oldTrack && !oldTrack.isMuted()) {
  324. transceiver = this.pc.peerconnection.getTransceivers().find(t => t.sender.track === oldTrack.getTrack());
  325. // Find the first recvonly transceiver when more than one track of the same media type is being added to the pc.
  326. // As part of the track addition, a new m-line was added to the remote description with direction set to
  327. // recvonly.
  328. } else if (isNewLocalSource) {
  329. transceiver = this.pc.peerconnection.getTransceivers().find(
  330. t => t.receiver.track.kind === mediaType
  331. && t.direction === MediaDirection.RECVONLY
  332. // Re-use any existing recvonly transceiver (if available) for p2p case.
  333. && ((this.pc.isP2P && t.currentDirection === MediaDirection.RECVONLY)
  334. || (t.currentDirection === MediaDirection.INACTIVE && !t.stopped)));
  335. // For mute/unmute operations, find the transceiver based on the track index in the source name if present,
  336. // otherwise it is assumed to be the first local track that was added to the peerconnection.
  337. } else {
  338. transceiver = this.pc.peerconnection.getTransceivers().find(t => t.receiver.track.kind === mediaType);
  339. const sourceName = newTrack?.getSourceName() ?? oldTrack?.getSourceName();
  340. if (sourceName) {
  341. const trackIndex = getSourceIndexFromSourceName(sourceName);
  342. if (this.pc.isP2P) {
  343. transceiver = this.pc.peerconnection.getTransceivers()
  344. .filter(t => t.receiver.track.kind === mediaType)[trackIndex];
  345. } else if (oldTrack) {
  346. const transceiverMid = this.pc._localTrackTransceiverMids.get(oldTrack.rtcId);
  347. transceiver = this.pc.peerconnection.getTransceivers().find(t => t.mid === transceiverMid);
  348. } else if (trackIndex) {
  349. transceiver = this.pc.peerconnection.getTransceivers()
  350. .filter(t => t.receiver.track.kind === mediaType
  351. && t.direction !== MediaDirection.RECVONLY)[trackIndex];
  352. }
  353. }
  354. }
  355. if (!transceiver) {
  356. return Promise.reject(
  357. new Error(`Replace track failed - no transceiver for old: ${oldTrack}, new: ${newTrack}`));
  358. }
  359. logger.debug(`${this.pc} Replacing ${oldTrack} with ${newTrack}`);
  360. return transceiver.sender.replaceTrack(track)
  361. .then(() => Promise.resolve(transceiver));
  362. }
  363. /**
  364. * Enables/disables audio transmission on the peer connection. When
  365. * disabled the audio transceiver direction will be set to 'inactive'
  366. * which means that no data will be sent nor accepted, but
  367. * the connection should be kept alive.
  368. * @param {boolean} active - true to enable audio media transmission or
  369. * false to disable.
  370. * @returns {void}
  371. */
  372. setAudioTransferActive(active) {
  373. this.setMediaTransferActive(MediaType.AUDIO, active);
  374. }
  375. /**
  376. * Set the simulcast stream encoding properties on the RTCRtpSender.
  377. * @param {JitsiLocalTrack} track - the current track in use for which
  378. * the encodings are to be set.
  379. * @returns {Promise<void>} - resolved when done.
  380. */
  381. setEncodings(track) {
  382. const mediaType = track.getType();
  383. const transceiver = this.findTransceiver(mediaType, track);
  384. const parameters = transceiver?.sender?.getParameters();
  385. // Resolve if the encodings are not available yet. This happens immediately after the track is added to the
  386. // peerconnection on chrome in unified-plan. It is ok to ignore and not report the error here since the
  387. // action that triggers 'addTrack' (like unmute) will also configure the encodings and set bitrates after that.
  388. if (!parameters?.encodings?.length) {
  389. return Promise.resolve();
  390. }
  391. parameters.encodings = this._getStreamEncodings(track);
  392. return transceiver.sender.setParameters(parameters);
  393. }
  394. /**
  395. * Enables/disables media transmission on the peerconnection by changing the direction
  396. * on the transceiver for the specified media type.
  397. * @param {String} mediaType - 'audio' or 'video'
  398. * @param {boolean} active - true to enable media transmission or false
  399. * to disable.
  400. * @returns {void}
  401. */
  402. setMediaTransferActive(mediaType, active) {
  403. const transceivers = this.pc.peerconnection.getTransceivers()
  404. .filter(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  405. logger.info(`${this.pc} ${active ? 'Enabling' : 'Suspending'} ${mediaType} media transfer.`);
  406. transceivers.forEach(transceiver => {
  407. if (active) {
  408. const localTrackMids = Array.from(this.pc._localTrackTransceiverMids);
  409. if (localTrackMids.find(mids => mids[1] === transceiver.mid)) {
  410. transceiver.direction = MediaDirection.SENDRECV;
  411. } else {
  412. transceiver.direction = MediaDirection.RECVONLY;
  413. }
  414. } else {
  415. transceiver.direction = MediaDirection.INACTIVE;
  416. }
  417. });
  418. }
  419. /**
  420. * Enables/disables video media transmission on the peer connection. When
  421. * disabled the SDP video media direction in the local SDP will be adjusted to
  422. * 'inactive' which means that no data will be sent nor accepted, but
  423. * the connection should be kept alive.
  424. * @param {boolean} active - true to enable video media transmission or
  425. * false to disable.
  426. * @returns {void}
  427. */
  428. setVideoTransferActive(active) {
  429. this.setMediaTransferActive(MediaType.VIDEO, active);
  430. }
  431. /**
  432. * Ensures that the resolution of the stream encodings are consistent with the values
  433. * that were configured on the RTCRtpSender when the source was added to the peerconnection.
  434. * This should prevent us from overriding the default values if the browser returns
  435. * erroneous values when RTCRtpSender.getParameters is used for getting the encodings info.
  436. * @param {Object} parameters - the RTCRtpEncodingParameters obtained from the browser.
  437. * @returns {void}
  438. */
  439. updateEncodingsResolution(parameters) {
  440. if (!(browser.isWebKitBased() && parameters.encodings && Array.isArray(parameters.encodings))) {
  441. return;
  442. }
  443. const allEqualEncodings
  444. = encodings => encodings.every(encoding => typeof encoding.scaleResolutionDownBy !== 'undefined'
  445. && encoding.scaleResolutionDownBy === encodings[0].scaleResolutionDownBy);
  446. // Implement the workaround only when all the encodings report the same resolution.
  447. if (allEqualEncodings(parameters.encodings)) {
  448. parameters.encodings.forEach((encoding, idx) => {
  449. encoding.scaleResolutionDownBy = this.localStreamEncodingsConfig[idx].scaleResolutionDownBy;
  450. });
  451. }
  452. }
  453. }