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.

ReceiveVideoController.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { getLogger } from '@jitsi/logger';
  2. import { isEqual } from 'lodash-es';
  3. import { MediaType } from '../../service/RTC/MediaType';
  4. import { ASSUMED_BANDWIDTH_BPS, LAST_N_UNLIMITED } from '../../service/RTC/StandardVideoQualitySettings';
  5. const logger = getLogger(__filename);
  6. const MAX_HEIGHT = 2160;
  7. /**
  8. * This class manages the receive video contraints for a given {@link JitsiConference}. These constraints are
  9. * determined by the application based on how the remote video streams need to be displayed. This class is responsible
  10. * for communicating these constraints to the bridge over the bridge channel.
  11. */
  12. export default class ReceiveVideoController {
  13. /**
  14. * Creates a new instance for a given conference.
  15. *
  16. * @param {JitsiConference} conference the conference instance for which the new instance will be managing
  17. * the receive video quality constraints.
  18. */
  19. constructor(conference) {
  20. this._conference = conference;
  21. this._rtc = conference.rtc;
  22. const { config } = conference.options;
  23. // The number of videos requested from the bridge, -1 represents unlimited or all available videos.
  24. this._lastN = config?.startLastN ?? (config?.channelLastN || LAST_N_UNLIMITED);
  25. // The number representing the maximum video height the local client should receive from the bridge.
  26. this._maxFrameHeight = MAX_HEIGHT;
  27. /**
  28. * The map that holds the max frame height requested per remote source for p2p connection.
  29. *
  30. * @type Map<string, number>
  31. */
  32. this._sourceReceiverConstraints = new Map();
  33. /**
  34. * The number of bps requested from the bridge.
  35. */
  36. this._assumedBandwidthBps = ASSUMED_BANDWIDTH_BPS;
  37. this._lastNLimitedByCpu = false;
  38. this._receiveResolutionLimitedByCpu = false;
  39. // The default receiver video constraints.
  40. this._receiverVideoConstraints = {
  41. assumedBandwidthBps: this._assumedBandwidthBps,
  42. lastN: this._lastN
  43. };
  44. }
  45. /**
  46. * Returns a map of all the remote source names and the corresponding max frame heights.
  47. *
  48. * @param {JingleSessionPC} mediaSession - the media session.
  49. * @param {number} maxFrameHeight - the height to be requested for remote sources.
  50. * @returns
  51. */
  52. _getDefaultSourceReceiverConstraints(mediaSession, maxFrameHeight) {
  53. const height = maxFrameHeight ?? MAX_HEIGHT;
  54. const remoteVideoTracks = mediaSession.peerconnection?.getRemoteTracks(null, MediaType.VIDEO) || [];
  55. const receiverConstraints = new Map();
  56. for (const track of remoteVideoTracks) {
  57. receiverConstraints.set(track.getSourceName(), height);
  58. }
  59. return receiverConstraints;
  60. }
  61. /**
  62. * Updates the source based constraints based on the maxHeight set.
  63. *
  64. * @param {number} maxFrameHeight - the height to be requested for remote sources.
  65. * @returns {void}
  66. */
  67. _updateIndividualConstraints(maxFrameHeight) {
  68. const individualConstraints = this._receiverVideoConstraints.constraints;
  69. if (individualConstraints && Object.keys(individualConstraints).length) {
  70. for (const value of Object.values(individualConstraints)) {
  71. value.maxHeight = maxFrameHeight ?? Math.min(value.maxHeight, this._maxFrameHeight);
  72. }
  73. } else {
  74. this._receiverVideoConstraints.defaultConstraints = { 'maxHeight': this._maxFrameHeight };
  75. }
  76. }
  77. /**
  78. * Returns the last set of receiver constraints that were set on the bridge channel.
  79. *
  80. * @returns {Object}
  81. */
  82. getCurrentReceiverConstraints() {
  83. return this._receiverVideoConstraints;
  84. }
  85. /**
  86. * Returns the lastN value for the conference.
  87. *
  88. * @returns {number}
  89. */
  90. getLastN() {
  91. return this._lastN;
  92. }
  93. /**
  94. * Checks whether last-n was lowered because of a cpu limitation.
  95. *
  96. * @returns {boolean}
  97. */
  98. isLastNLimitedByCpu() {
  99. return this._lastNLimitedByCpu;
  100. }
  101. /**
  102. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  103. * session. The preferred receive frameHeight is applied on the media session.
  104. *
  105. * @param {JingleSessionPC} mediaSession - the started media session.
  106. * @returns {void}
  107. */
  108. onMediaSessionStarted(mediaSession) {
  109. if (mediaSession.isP2P) {
  110. mediaSession.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(mediaSession));
  111. } else {
  112. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  113. }
  114. }
  115. /**
  116. * Sets the assumed bandwidth bps the local participant should receive from remote participants.
  117. *
  118. * @param {number|undefined} assumedBandwidthBps - the new value.
  119. * @returns {void}
  120. */
  121. setAssumedBandwidthBps(assumedBandwidthBps) {
  122. if (this._receiverVideoConstraints.assumedBandwidthBps !== assumedBandwidthBps) {
  123. this._receiverVideoConstraints.assumedBandwidthBps = assumedBandwidthBps;
  124. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  125. }
  126. }
  127. /**
  128. * Selects a new value for "lastN". The requested amount of videos are going to be delivered after the value is
  129. * in effect. Set to -1 for unlimited or all available videos.
  130. *
  131. * @param {number} value the new value for lastN.
  132. * @returns {void}
  133. */
  134. setLastN(value) {
  135. if (this._lastN !== value) {
  136. this._lastN = value;
  137. this._receiverVideoConstraints.lastN = value;
  138. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  139. }
  140. }
  141. /**
  142. * Updates the lastNLimitedByCpu field.
  143. *
  144. * @param {boolean} enabled
  145. * @returns {void}
  146. */
  147. setLastNLimitedByCpu(enabled) {
  148. if (this._lastNLimitedByCpu !== enabled) {
  149. this._lastNLimitedByCpu = enabled;
  150. logger.info(`ReceiveVideoController - Setting the lastNLimitedByCpu flag to ${enabled}`);
  151. }
  152. }
  153. /**
  154. * Sets the maximum video resolution the local participant should receive from remote participants.
  155. *
  156. * @param {number|undefined} maxFrameHeight - the new value.
  157. * @returns {void}
  158. */
  159. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  160. this._maxFrameHeight = maxFrameHeight;
  161. for (const session of this._conference.getMediaSessions()) {
  162. if (session.isP2P) {
  163. session.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(session, maxFrameHeight));
  164. } else {
  165. this._updateIndividualConstraints(maxFrameHeight);
  166. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  167. }
  168. }
  169. }
  170. /**
  171. * Sets the receiver constraints for the conference.
  172. *
  173. * @param {Object} constraints The video constraints.
  174. */
  175. setReceiverConstraints(constraints) {
  176. if (!constraints) {
  177. return;
  178. }
  179. const isEndpointsFormat = Object.keys(constraints).includes('onStageEndpoints', 'selectedEndpoints');
  180. if (isEndpointsFormat) {
  181. throw new Error(
  182. '"onStageEndpoints" and "selectedEndpoints" are not supported when sourceNameSignaling is enabled.'
  183. );
  184. }
  185. const constraintsChanged = !isEqual(this._receiverVideoConstraints, constraints);
  186. if (constraintsChanged || this._lastNLimitedByCpu || this._receiveResolutionLimitedByCpu) {
  187. this._receiverVideoConstraints = constraints;
  188. this._assumedBandwidthBps = constraints.assumedBandwidthBps ?? this._assumedBandwidthBps;
  189. this._lastN = typeof constraints.lastN !== 'undefined' && !this._lastNLimitedByCpu
  190. ? constraints.lastN : this._lastN;
  191. this._receiverVideoConstraints.lastN = this._lastN;
  192. this._receiveResolutionLimitedByCpu && this._updateIndividualConstraints();
  193. // Send the contraints on the bridge channel.
  194. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  195. const p2pSession = this._conference.getMediaSessions().find(session => session.isP2P);
  196. if (!p2pSession || !this._receiverVideoConstraints.constraints) {
  197. return;
  198. }
  199. const mappedConstraints = Array.from(Object.entries(this._receiverVideoConstraints.constraints))
  200. .map(constraint => {
  201. constraint[1] = constraint[1].maxHeight;
  202. return constraint;
  203. });
  204. this._sourceReceiverConstraints = new Map(mappedConstraints);
  205. // Send the receiver constraints to the peer through a "content-modify" message.
  206. p2pSession.setReceiverVideoConstraint(this._sourceReceiverConstraints);
  207. }
  208. }
  209. /**
  210. * Updates the receivedResolutioLimitedByCpu field.
  211. *
  212. * @param {booem} enabled
  213. * @return {void}
  214. */
  215. setReceiveResolutionLimitedByCpu(enabled) {
  216. if (this._receiveResolutionLimitedByCpu !== enabled) {
  217. this._receiveResolutionLimitedByCpu = enabled;
  218. logger.info(`ReceiveVideoController - Setting the receiveResolutionLimitedByCpu flag to ${enabled}`);
  219. }
  220. }
  221. }