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.

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