Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ReceiveVideoController.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * @returns {void}
  65. */
  66. _updateIndividualConstraints() {
  67. const individualConstraints = this._receiverVideoConstraints.constraints;
  68. if (individualConstraints && Object.keys(individualConstraints).length) {
  69. for (const value of Object.values(individualConstraints)) {
  70. value.maxHeight = Math.min(value.maxHeight, this._maxFrameHeight);
  71. }
  72. } else {
  73. this._receiverVideoConstraints.defaultConstraints = { 'maxHeight': this._maxFrameHeight };
  74. }
  75. }
  76. /**
  77. * Returns the last set of receiver constraints that were set on the bridge channel.
  78. *
  79. * @returns {Object}
  80. */
  81. getCurrentReceiverConstraints() {
  82. return this._receiverVideoConstraints;
  83. }
  84. /**
  85. * Returns the lastN value for the conference.
  86. *
  87. * @returns {number}
  88. */
  89. getLastN() {
  90. return this._lastN;
  91. }
  92. /**
  93. * Checks whether last-n was lowered because of a cpu limitation.
  94. *
  95. * @returns {boolean}
  96. */
  97. isLastNLimitedByCpu() {
  98. return this._lastNLimitedByCpu;
  99. }
  100. /**
  101. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  102. * session. The preferred receive frameHeight is applied on the media session.
  103. *
  104. * @param {JingleSessionPC} mediaSession - the started media session.
  105. * @returns {void}
  106. */
  107. onMediaSessionStarted(mediaSession) {
  108. if (mediaSession.isP2P) {
  109. mediaSession.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(mediaSession));
  110. } else {
  111. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  112. }
  113. }
  114. /**
  115. * Sets the assumed bandwidth bps the local participant should receive from remote participants.
  116. *
  117. * @param {number|undefined} assumedBandwidthBps - the new value.
  118. * @returns {void}
  119. */
  120. setAssumedBandwidthBps(assumedBandwidthBps) {
  121. if (this._receiverVideoConstraints.assumedBandwidthBps !== assumedBandwidthBps) {
  122. this._receiverVideoConstraints.assumedBandwidthBps = assumedBandwidthBps;
  123. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  124. }
  125. }
  126. /**
  127. * Selects a new value for "lastN". The requested amount of videos are going to be delivered after the value is
  128. * in effect. Set to -1 for unlimited or all available videos.
  129. *
  130. * @param {number} value the new value for lastN.
  131. * @returns {void}
  132. */
  133. setLastN(value) {
  134. if (this._lastN !== value) {
  135. this._lastN = value;
  136. this._receiverVideoConstraints.lastN = value;
  137. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  138. }
  139. }
  140. /**
  141. * Updates the lastNLimitedByCpu field.
  142. *
  143. * @param {boolean} enabled
  144. * @returns {void}
  145. */
  146. setLastNLimitedByCpu(enabled) {
  147. if (this._lastNLimitedByCpu !== enabled) {
  148. this._lastNLimitedByCpu = enabled;
  149. logger.info(`ReceiveVideoController - Setting the lastNLimitedByCpu flag to ${enabled}`);
  150. }
  151. }
  152. /**
  153. * Sets the maximum video resolution the local participant should receive from remote participants.
  154. *
  155. * @param {number|undefined} maxFrameHeight - the new value.
  156. * @returns {void}
  157. */
  158. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  159. this._maxFrameHeight = maxFrameHeight;
  160. for (const session of this._conference.getMediaSessions()) {
  161. if (session.isP2P) {
  162. session.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(session, maxFrameHeight));
  163. } else {
  164. this._updateIndividualConstraints();
  165. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  166. }
  167. }
  168. }
  169. /**
  170. * Sets the receiver constraints for the conference.
  171. *
  172. * @param {Object} constraints The video constraints.
  173. */
  174. setReceiverConstraints(constraints) {
  175. if (!constraints) {
  176. return;
  177. }
  178. const isEndpointsFormat = Object.keys(constraints).includes('onStageEndpoints', 'selectedEndpoints');
  179. if (isEndpointsFormat) {
  180. throw new Error(
  181. '"onStageEndpoints" and "selectedEndpoints" are not supported when sourceNameSignaling is enabled.'
  182. );
  183. }
  184. const constraintsChanged = !isEqual(this._receiverVideoConstraints, constraints);
  185. if (constraintsChanged || this._lastNLimitedByCpu || this._receiveResolutionLimitedByCpu) {
  186. this._receiverVideoConstraints = constraints;
  187. this._assumedBandwidthBps = constraints.assumedBandwidthBps ?? this._assumedBandwidthBps;
  188. this._lastN = typeof constraints.lastN !== 'undefined' && !this._lastNLimitedByCpu
  189. ? constraints.lastN : this._lastN;
  190. this._receiverVideoConstraints.lastN = this._lastN;
  191. this._receiveResolutionLimitedByCpu && this._updateIndividualConstraints();
  192. // Send the contraints on the bridge channel.
  193. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints);
  194. const p2pSession = this._conference.getMediaSessions().find(session => session.isP2P);
  195. if (!p2pSession || !this._receiverVideoConstraints.constraints) {
  196. return;
  197. }
  198. const mappedConstraints = Array.from(Object.entries(this._receiverVideoConstraints.constraints))
  199. .map(constraint => {
  200. constraint[1] = constraint[1].maxHeight;
  201. return constraint;
  202. });
  203. this._sourceReceiverConstraints = new Map(mappedConstraints);
  204. // Send the receiver constraints to the peer through a "content-modify" message.
  205. p2pSession.setReceiverVideoConstraint(this._sourceReceiverConstraints);
  206. }
  207. }
  208. /**
  209. * Updates the receivedResolutioLimitedByCpu field.
  210. *
  211. * @param {booem} enabled
  212. * @return {void}
  213. */
  214. setReceiveResolutionLimitedByCpu(enabled) {
  215. if (this._receiveResolutionLimitedByCpu !== enabled) {
  216. this._receiveResolutionLimitedByCpu = enabled;
  217. logger.info(`ReceiveVideoController - Setting the receiveResolutionLimitedByCpu flag to ${enabled}`);
  218. }
  219. }
  220. }