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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * Obtains local tracks for given {@link MediaType}.
  48. * @param {MediaType} mediaType - audio or video.
  49. * @return {Array<JitsiLocalTrack>} - array containing the local tracks
  50. * attached to the peerconnection of the given media type.
  51. */
  52. _getLocalTracks(mediaType) {
  53. const tracks = Array.from(this.pc.localTracks.values());
  54. return tracks.filter(track => track.getType() === mediaType);
  55. }
  56. /**
  57. * Obtains stream encodings that need to be configured on the given track.
  58. * @param {JitsiLocalTrack} localTrack
  59. */
  60. _getStreamEncodings(localTrack) {
  61. if (this.pc.isSimulcastOn() && localTrack.isVideoTrack()) {
  62. return this.simulcastEncodings;
  63. }
  64. return [ { active: true } ];
  65. }
  66. /**
  67. * Takes in a *unified plan* offer and inserts the appropriate
  68. * parameters for adding simulcast receive support.
  69. * @param {Object} desc - A session description object
  70. * @param {String} desc.type - the type (offer/answer)
  71. * @param {String} desc.sdp - the sdp content
  72. *
  73. * @return {Object} A session description (same format as above) object
  74. * with its sdp field modified to advertise simulcast receive support
  75. */
  76. _insertUnifiedPlanSimulcastReceive(desc) {
  77. // a=simulcast line is not needed on browsers where
  78. // we munge SDP for turning on simulcast. Remove this check
  79. // when we move to RID/MID based simulcast on all browsers.
  80. if (browser.usesSdpMungingForSimulcast()) {
  81. return desc;
  82. }
  83. const sdp = transform.parse(desc.sdp);
  84. const idx = sdp.media.findIndex(mline => mline.type === 'video');
  85. if (sdp.media[idx].rids && (sdp.media[idx].simulcast_03 || sdp.media[idx].simulcast)) {
  86. // Make sure we don't have the simulcast recv line on video descriptions other than the
  87. // the first video description.
  88. sdp.media.forEach((mline, i) => {
  89. if (mline.type === 'video' && i !== idx) {
  90. sdp.media[i].rids = undefined;
  91. sdp.media[i].simulcast = undefined;
  92. }
  93. });
  94. }
  95. // In order of highest to lowest spatial quality
  96. sdp.media[idx].rids = [
  97. {
  98. id: SIM_LAYER_1_RID,
  99. direction: 'recv'
  100. },
  101. {
  102. id: SIM_LAYER_2_RID,
  103. direction: 'recv'
  104. },
  105. {
  106. id: SIM_LAYER_3_RID,
  107. direction: 'recv'
  108. }
  109. ];
  110. // Firefox 72 has stopped parsing the legacy rid= parameters in simulcast attributes.
  111. // eslint-disable-next-line max-len
  112. // https://www.fxsitecompat.dev/en-CA/docs/2019/pt-and-rid-in-webrtc-simulcast-attributes-are-no-longer-supported/
  113. const simulcastLine = browser.isFirefox() && browser.isVersionGreaterThan(71)
  114. ? `recv ${SIM_LAYER_RIDS.join(';')}`
  115. : `recv rid=${SIM_LAYER_RIDS.join(';')}`;
  116. // eslint-disable-next-line camelcase
  117. sdp.media[idx].simulcast_03 = {
  118. value: simulcastLine
  119. };
  120. return new RTCSessionDescription({
  121. type: desc.type,
  122. sdp: transform.write(sdp)
  123. });
  124. }
  125. /**
  126. * Adds {@link JitsiLocalTrack} to the WebRTC peerconnection for the first time.
  127. * @param {JitsiLocalTrack} track - track to be added to the peerconnection.
  128. * @returns {boolean} Returns true if the operation is successful,
  129. * false otherwise.
  130. */
  131. addTrack(localTrack, isInitiator = true) {
  132. const track = localTrack.getTrack();
  133. if (isInitiator) {
  134. // Use pc.addTransceiver() for the initiator case when local tracks are getting added
  135. // to the peerconnection before a session-initiate is sent over to the peer.
  136. const transceiverInit = {
  137. direction: 'sendrecv',
  138. streams: [ localTrack.getOriginalStream() ],
  139. sendEncodings: []
  140. };
  141. if (!browser.isFirefox()) {
  142. transceiverInit.sendEncodings = this._getStreamEncodings(localTrack);
  143. }
  144. this.pc.peerconnection.addTransceiver(track, transceiverInit);
  145. } else {
  146. // Use pc.addTrack() for responder case so that we can re-use the m-lines that were created
  147. // when setRemoteDescription was called. pc.addTrack() automatically attaches to any existing
  148. // unused "recv-only" transceiver.
  149. this.pc.peerconnection.addTrack(track);
  150. }
  151. }
  152. /**
  153. * Adds a track on the RTCRtpSender as part of the unmute operation.
  154. * @param {JitsiLocalTrack} localTrack - track to be unmuted.
  155. * @returns {boolean} Returns true if the operation is successful,
  156. * false otherwise.
  157. */
  158. addTrackUnmute(localTrack) {
  159. const mediaType = localTrack.getType();
  160. const track = localTrack.getTrack();
  161. // The assumption here is that the first transceiver of the specified
  162. // media type is that of the local track.
  163. const transceiver = this.pc.peerconnection.getTransceivers()
  164. .find(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  165. if (!transceiver) {
  166. logger.error(`RTCRtpTransceiver for ${mediaType} on ${this.pc} not found`);
  167. return false;
  168. }
  169. logger.info(`Adding ${localTrack} on ${this.pc}`);
  170. // If the client starts with audio/video muted setting, the transceiver direction
  171. // will be set to 'recvonly'. Use addStream here so that a MSID is generated for the stream.
  172. if (transceiver.direction === 'recvonly') {
  173. this.pc.peerconnection.addStream(localTrack.getOriginalStream());
  174. this.setEncodings(localTrack);
  175. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  176. transceiver.direction = 'sendrecv';
  177. return true;
  178. }
  179. transceiver.sender.replaceTrack(track)
  180. .then(() => {
  181. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  182. return true;
  183. })
  184. .catch(err => {
  185. logger.error(`Unmute track failed for ${mediaType} track on ${this.pc}, ${err}`);
  186. return false;
  187. });
  188. }
  189. /**
  190. * Removes the track from the RTCRtpSender as part of the mute operation.
  191. * @param {JitsiLocalTrack} localTrack - track to be removed.
  192. * @returns {boolean} Returns true if the operation is successful,
  193. * false otherwise.
  194. */
  195. removeTrackMute(localTrack) {
  196. const mediaType = localTrack.getType();
  197. const transceiver = this.pc.peerconnection.getTransceivers()
  198. .find(t => t.sender && t.sender.track && t.sender.track.id === localTrack.getTrackId());
  199. if (!transceiver) {
  200. logger.error(`RTCRtpTransceiver for ${mediaType} on ${this.pc} not found`);
  201. return false;
  202. }
  203. logger.info(`Removing ${localTrack} on ${this.pc}`);
  204. transceiver.sender.replaceTrack(null)
  205. .then(() => {
  206. this.pc.localTracks.delete(localTrack.rtcId);
  207. this.pc.localSSRCs.delete(localTrack.rtcId);
  208. return true;
  209. })
  210. .catch(err => {
  211. logger.error(`Mute track failed for ${mediaType} track on ${this.pc}, ${err}`);
  212. return false;
  213. });
  214. }
  215. /**
  216. * Replaces the existing track on a RTCRtpSender with the given track.
  217. * @param {JitsiLocalTrack} oldTrack - existing track on the sender that needs to be removed.
  218. * @param {JitsiLocalTrack} newTrack - new track that needs to be added to the sender.
  219. * @returns {Promise<false>} Promise that resolves with false as we don't want
  220. * renegotiation to be triggered automatically after this operation. Renegotiation is
  221. * done when the browser fires the negotiationeeded event.
  222. */
  223. replaceTrack(oldTrack, newTrack) {
  224. if (oldTrack && newTrack) {
  225. const mediaType = newTrack.getType();
  226. const stream = newTrack.getOriginalStream();
  227. const track = stream.getVideoTracks()[0];
  228. const transceiver = this.pc.peerconnection.getTransceivers()
  229. .find(t => t.receiver.track.kind === mediaType && !t.stopped);
  230. if (!transceiver) {
  231. return Promise.reject(new Error('replace track failed'));
  232. }
  233. return transceiver.sender.replaceTrack(track)
  234. .then(() => {
  235. const ssrc = this.pc.localSSRCs.get(oldTrack.rtcId);
  236. this.pc.localTracks.delete(oldTrack.rtcId);
  237. this.pc.localSSRCs.delete(oldTrack.rtcId);
  238. this.pc._addedStreams = this.pc._addedStreams.filter(s => s !== stream);
  239. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  240. this.pc._addedStreams.push(stream);
  241. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  242. this.pc.eventEmitter.emit(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  243. newTrack,
  244. this.pc._extractPrimarySSRC(ssrc));
  245. });
  246. } else if (oldTrack && !newTrack) {
  247. if (!this.removeTrackMute(oldTrack)) {
  248. return Promise.reject(new Error('replace track failed'));
  249. }
  250. this.pc.localTracks.delete(oldTrack.rtcId);
  251. this.pc.localSSRCs.delete(oldTrack.rtcId);
  252. } else if (newTrack && !oldTrack) {
  253. const ssrc = this.pc.localSSRCs.get(newTrack.rtcId);
  254. if (!this.addTrackUnmute(newTrack)) {
  255. return Promise.reject(new Error('replace track failed'));
  256. }
  257. newTrack.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, newTrack);
  258. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  259. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  260. }
  261. return Promise.resolve(false);
  262. }
  263. /**
  264. *
  265. * @param {boolean} active
  266. */
  267. setAudioTransferActive(active) {
  268. return this.setMediaTransferActive('audio', active);
  269. }
  270. /**
  271. * Set the simulcast stream encoding properties on the RTCRtpSender.
  272. * @param {*} track - the current track in use for which the encodings are to be set.
  273. */
  274. setEncodings(track) {
  275. const transceiver = this.pc.peerconnection.getTransceivers()
  276. .find(t => t.sender && t.sender.track && t.sender.track.kind === track.getType());
  277. const parameters = transceiver.sender.getParameters();
  278. parameters.encodings = this._getStreamEncodings(track);
  279. transceiver.sender.setParameters(parameters);
  280. }
  281. /**
  282. *
  283. * @param {*} mediaType
  284. * @param {boolean} active
  285. */
  286. setMediaTransferActive(mediaType, active) {
  287. const transceivers = this.pc.peerconnection.getTransceivers()
  288. .filter(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  289. if (active) {
  290. transceivers.forEach(transceiver => {
  291. if (this._getLocalTracks(mediaType).length > 0) {
  292. transceiver.direction = 'sendrecv';
  293. const parameters = transceiver.sender.getParameters();
  294. if (parameters && parameters.encodings && parameters.encodings.length) {
  295. parameters.encodings.forEach(encoding => {
  296. encoding.active = true;
  297. });
  298. transceiver.sender.setParameters(parameters);
  299. }
  300. } else {
  301. transceiver.direction = 'recvonly';
  302. }
  303. });
  304. } else {
  305. transceivers.forEach(transceiver => {
  306. transceiver.direction = 'inactive';
  307. });
  308. }
  309. return true;
  310. }
  311. /**
  312. *
  313. * @param {boolean} active
  314. */
  315. setVideoTransferActive(active) {
  316. return this.setMediaTransferActive('video', active);
  317. }
  318. }