您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TPCUtils.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 {boolean} Returns true if the operation is successful,
  186. * false otherwise.
  187. */
  188. addTrack(localTrack, isInitiator = true) {
  189. const track = localTrack.getTrack();
  190. if (isInitiator) {
  191. // Use pc.addTransceiver() for the initiator case when local tracks are getting added
  192. // to the peerconnection before a session-initiate is sent over to the peer.
  193. const transceiverInit = {
  194. direction: 'sendrecv',
  195. streams: [ localTrack.getOriginalStream() ],
  196. sendEncodings: []
  197. };
  198. if (!browser.isFirefox()) {
  199. transceiverInit.sendEncodings = this._getStreamEncodings(localTrack);
  200. }
  201. this.pc.peerconnection.addTransceiver(track, transceiverInit);
  202. } else {
  203. // Use pc.addTrack() for responder case so that we can re-use the m-lines that were created
  204. // when setRemoteDescription was called. pc.addTrack() automatically attaches to any existing
  205. // unused "recv-only" transceiver.
  206. this.pc.peerconnection.addTrack(track);
  207. }
  208. // Construct the simulcast stream constraints for the newly added track.
  209. if (localTrack.isVideoTrack() && localTrack.videoType === VideoType.CAMERA && this.pc.isSimulcastOn()) {
  210. this.setSimulcastStreamConstraints(localTrack.getTrack());
  211. }
  212. }
  213. /**
  214. * Adds a track on the RTCRtpSender as part of the unmute operation.
  215. * @param {JitsiLocalTrack} localTrack - track to be unmuted.
  216. * @returns {Promise<boolean>} - Promise that resolves to false if unmute
  217. * operation is successful, a reject otherwise.
  218. */
  219. addTrackUnmute(localTrack) {
  220. const mediaType = localTrack.getType();
  221. const track = localTrack.getTrack();
  222. // The assumption here is that the first transceiver of the specified
  223. // media type is that of the local track.
  224. const transceiver = this.pc.peerconnection.getTransceivers()
  225. .find(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  226. if (!transceiver) {
  227. return Promise.reject(new Error(`RTCRtpTransceiver for ${mediaType} not found`));
  228. }
  229. logger.debug(`Adding ${localTrack} on ${this.pc}`);
  230. // If the client starts with audio/video muted setting, the transceiver direction
  231. // will be set to 'recvonly'. Use addStream here so that a MSID is generated for the stream.
  232. if (transceiver.direction === 'recvonly') {
  233. const stream = localTrack.getOriginalStream();
  234. if (stream) {
  235. this.pc.peerconnection.addStream(localTrack.getOriginalStream());
  236. this.setEncodings(localTrack);
  237. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  238. transceiver.direction = 'sendrecv';
  239. }
  240. return Promise.resolve(false);
  241. }
  242. return transceiver.sender.replaceTrack(track)
  243. .then(() => {
  244. this.pc.localTracks.set(localTrack.rtcId, localTrack);
  245. return Promise.resolve(false);
  246. });
  247. }
  248. /**
  249. * Removes the track from the RTCRtpSender as part of the mute operation.
  250. * @param {JitsiLocalTrack} localTrack - track to be removed.
  251. * @returns {Promise<boolean>} - Promise that resolves to false if unmute
  252. * operation is successful, a reject otherwise.
  253. */
  254. removeTrackMute(localTrack) {
  255. const mediaType = localTrack.getType();
  256. const transceiver = this.pc.peerconnection.getTransceivers()
  257. .find(t => t.sender && t.sender.track && t.sender.track.id === localTrack.getTrackId());
  258. if (!transceiver) {
  259. return Promise.reject(new Error(`RTCRtpTransceiver for ${mediaType} not found`));
  260. }
  261. logger.debug(`Removing ${localTrack} on ${this.pc}`);
  262. return transceiver.sender.replaceTrack(null)
  263. .then(() => {
  264. this.pc.localTracks.delete(localTrack.rtcId);
  265. return Promise.resolve(false);
  266. });
  267. }
  268. /**
  269. * Replaces the existing track on a RTCRtpSender with the given track.
  270. * @param {JitsiLocalTrack} oldTrack - existing track on the sender that needs to be removed.
  271. * @param {JitsiLocalTrack} newTrack - new track that needs to be added to the sender.
  272. * @returns {Promise<false>} Promise that resolves with false as we don't want
  273. * renegotiation to be triggered automatically after this operation. Renegotiation is
  274. * done when the browser fires the negotiationeeded event.
  275. */
  276. replaceTrack(oldTrack, newTrack) {
  277. if (oldTrack && newTrack) {
  278. const mediaType = newTrack.getType();
  279. const stream = newTrack.getOriginalStream();
  280. const track = mediaType === MediaType.AUDIO
  281. ? stream.getAudioTracks()[0]
  282. : stream.getVideoTracks()[0];
  283. const transceiver = this.pc.peerconnection.getTransceivers()
  284. .find(t => t.receiver.track.kind === mediaType && !t.stopped);
  285. if (!transceiver) {
  286. return Promise.reject(new Error('replace track failed'));
  287. }
  288. logger.debug(`Replacing ${oldTrack} with ${newTrack} on ${this.pc}`);
  289. return transceiver.sender.replaceTrack(track)
  290. .then(() => {
  291. const ssrc = this.pc.localSSRCs.get(oldTrack.rtcId);
  292. this.pc.localTracks.delete(oldTrack.rtcId);
  293. this.pc.localSSRCs.delete(oldTrack.rtcId);
  294. this.pc._addedStreams = this.pc._addedStreams.filter(s => s !== stream);
  295. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  296. this.pc._addedStreams.push(stream);
  297. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  298. this.pc.eventEmitter.emit(RTCEvents.LOCAL_TRACK_SSRC_UPDATED,
  299. newTrack,
  300. this.pc._extractPrimarySSRC(ssrc));
  301. });
  302. } else if (oldTrack && !newTrack) {
  303. if (!this.removeTrackMute(oldTrack)) {
  304. return Promise.reject(new Error('replace track failed'));
  305. }
  306. this.pc.localTracks.delete(oldTrack.rtcId);
  307. this.pc.localSSRCs.delete(oldTrack.rtcId);
  308. } else if (newTrack && !oldTrack) {
  309. const ssrc = this.pc.localSSRCs.get(newTrack.rtcId);
  310. if (!this.addTrackUnmute(newTrack)) {
  311. return Promise.reject(new Error('replace track failed'));
  312. }
  313. newTrack.emit(JitsiTrackEvents.TRACK_MUTE_CHANGED, newTrack);
  314. this.pc.localTracks.set(newTrack.rtcId, newTrack);
  315. this.pc.localSSRCs.set(newTrack.rtcId, ssrc);
  316. }
  317. return Promise.resolve(false);
  318. }
  319. /**
  320. * Enables/disables audio transmission on the peer connection. When
  321. * disabled the audio transceiver direction will be set to 'inactive'
  322. * which means that no data will be sent nor accepted, but
  323. * the connection should be kept alive.
  324. * @param {boolean} active - true to enable audio media transmission or
  325. * false to disable.
  326. * @returns {false} - returns false always so that renegotiation is not automatically
  327. * triggered after this operation.
  328. */
  329. setAudioTransferActive(active) {
  330. return this.setMediaTransferActive('audio', active);
  331. }
  332. /**
  333. * Set the simulcast stream encoding properties on the RTCRtpSender.
  334. * @param {JitsiLocalTrack} track - the current track in use for which
  335. * the encodings are to be set.
  336. */
  337. setEncodings(track) {
  338. const transceiver = this.pc.peerconnection.getTransceivers()
  339. .find(t => t.sender && t.sender.track && t.sender.track.kind === track.getType());
  340. const parameters = transceiver.sender.getParameters();
  341. parameters.encodings = this._getStreamEncodings(track);
  342. transceiver.sender.setParameters(parameters);
  343. }
  344. /**
  345. * Enables/disables media transmission on the peerconnection by changing the direction
  346. * on the transceiver for the specified media type.
  347. * @param {String} mediaType - 'audio' or 'video'
  348. * @param {boolean} active - true to enable media transmission or false
  349. * to disable.
  350. * @returns {false} - returns false always so that renegotiation is not automatically
  351. * triggered after this operation
  352. */
  353. setMediaTransferActive(mediaType, active) {
  354. const transceivers = this.pc.peerconnection.getTransceivers()
  355. .filter(t => t.receiver && t.receiver.track && t.receiver.track.kind === mediaType);
  356. const localTracks = Array.from(this.pc.localTracks.values())
  357. .filter(track => track.getType() === mediaType);
  358. if (active) {
  359. transceivers.forEach(transceiver => {
  360. if (localTracks.length) {
  361. transceiver.direction = 'sendrecv';
  362. const parameters = transceiver.sender.getParameters();
  363. if (parameters && parameters.encodings && parameters.encodings.length) {
  364. parameters.encodings.forEach(encoding => {
  365. encoding.active = true;
  366. });
  367. transceiver.sender.setParameters(parameters);
  368. }
  369. } else {
  370. transceiver.direction = 'recvonly';
  371. }
  372. });
  373. } else {
  374. transceivers.forEach(transceiver => {
  375. transceiver.direction = 'inactive';
  376. });
  377. }
  378. return false;
  379. }
  380. /**
  381. * Enables/disables video media transmission on the peer connection. When
  382. * disabled the SDP video media direction in the local SDP will be adjusted to
  383. * 'inactive' which means that no data will be sent nor accepted, but
  384. * the connection should be kept alive.
  385. * @param {boolean} active - true to enable video media transmission or
  386. * false to disable.
  387. * @returns {false} - returns false always so that renegotiation is not automatically
  388. * triggered after this operation.
  389. */
  390. setVideoTransferActive(active) {
  391. return this.setMediaTransferActive('video', active);
  392. }
  393. }