Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TPCUtils.js 17KB

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