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

TPCUtils.js 17KB

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