Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReceiveVideoController.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { getLogger } from '@jitsi/logger';
  2. import isEqual from 'lodash.isequal';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. import { MediaType } from '../../service/RTC/MediaType';
  5. const logger = getLogger(__filename);
  6. const MAX_HEIGHT = 2160;
  7. const LASTN_UNLIMITED = -1;
  8. const ASSUMED_BANDWIDTH_BPS = -1;
  9. /**
  10. * This class translates the legacy signaling format between the client and the bridge (that affects bandwidth
  11. * allocation) to the new format described here https://github.com/jitsi/jitsi-videobridge/blob/master/doc/allocation.md
  12. */
  13. class ReceiverVideoConstraints {
  14. /**
  15. * Creates a new instance.
  16. * @param {Object} options - The instance options:
  17. * - lastN: Number of videos to be requested from the bridge.
  18. * - assumedBandwidthBps: Number of bps to be requested from the bridge.
  19. */
  20. constructor(options) {
  21. const { lastN, assumedBandwidthBps } = options;
  22. // The number of videos requested from the bridge.
  23. this._lastN = lastN ?? LASTN_UNLIMITED;
  24. // The number representing the maximum video height the local client should receive from the bridge/peer.
  25. this._maxFrameHeight = MAX_HEIGHT;
  26. // The number representing the assumed count of bps the local client should receive from the bridge.
  27. this._assumedBandwidthBps = assumedBandwidthBps ?? ASSUMED_BANDWIDTH_BPS;
  28. this._receiverVideoConstraints = {
  29. assumedBandwidthBps: this._assumedBandwidthBps,
  30. constraints: {},
  31. defaultConstraints: { 'maxHeight': this._maxFrameHeight },
  32. lastN: this._lastN
  33. };
  34. }
  35. /**
  36. * Returns the receiver video constraints that need to be sent on the bridge channel or to the remote peer.
  37. */
  38. get constraints() {
  39. this._receiverVideoConstraints.assumedBandwidthBps = this._assumedBandwidthBps;
  40. this._receiverVideoConstraints.lastN = this._lastN;
  41. if (Object.keys(this._receiverVideoConstraints.constraints)?.length) {
  42. /* eslint-disable no-unused-vars */
  43. for (const [ key, value ] of Object.entries(this._receiverVideoConstraints.constraints)) {
  44. value.maxHeight = this._maxFrameHeight;
  45. }
  46. } else {
  47. this._receiverVideoConstraints.defaultConstraints = { 'maxHeight': this._maxFrameHeight };
  48. }
  49. return this._receiverVideoConstraints;
  50. }
  51. /**
  52. * Updates the assumed bandwidth bps of the ReceiverVideoConstraints sent to the bridge.
  53. *
  54. * @param {number} assumedBandwidthBps
  55. * @requires {boolean} Returns true if the the value has been updated, false otherwise.
  56. */
  57. updateAssumedBandwidthBps(assumedBandwidthBps) {
  58. const changed = this._assumedBandwidthBps !== assumedBandwidthBps;
  59. if (changed) {
  60. this._assumedBandwidthBps = assumedBandwidthBps;
  61. logger.debug(`Updating receive assumedBandwidthBps: ${assumedBandwidthBps}`);
  62. }
  63. return changed;
  64. }
  65. /**
  66. * Updates the lastN field of the ReceiverVideoConstraints sent to the bridge.
  67. *
  68. * @param {number} value
  69. * @returns {boolean} Returns true if the the value has been updated, false otherwise.
  70. */
  71. updateLastN(value) {
  72. const changed = this._lastN !== value;
  73. if (changed) {
  74. this._lastN = value;
  75. logger.debug(`Updating ReceiverVideoConstraints lastN(${value})`);
  76. }
  77. return changed;
  78. }
  79. /**
  80. * Updates the resolution (height requested) in the contraints field of the ReceiverVideoConstraints
  81. * sent to the bridge.
  82. *
  83. * @param {number} maxFrameHeight
  84. * @requires {boolean} Returns true if the the value has been updated, false otherwise.
  85. */
  86. updateReceiveResolution(maxFrameHeight) {
  87. const changed = this._maxFrameHeight !== maxFrameHeight;
  88. if (changed) {
  89. this._maxFrameHeight = maxFrameHeight;
  90. logger.debug(`Updating receive maxFrameHeight: ${maxFrameHeight}`);
  91. }
  92. return changed;
  93. }
  94. /**
  95. * Updates the receiver constraints sent to the bridge.
  96. *
  97. * @param {Object} videoConstraints
  98. * @returns {boolean} Returns true if the the value has been updated, false otherwise.
  99. */
  100. updateReceiverVideoConstraints(videoConstraints) {
  101. const changed = !isEqual(this._receiverVideoConstraints, videoConstraints);
  102. if (changed) {
  103. this._receiverVideoConstraints = videoConstraints;
  104. logger.debug(`Updating ReceiverVideoConstraints ${JSON.stringify(videoConstraints)}`);
  105. }
  106. return changed;
  107. }
  108. }
  109. /**
  110. * This class manages the receive video contraints for a given {@link JitsiConference}. These constraints are
  111. * determined by the application based on how the remote video streams need to be displayed. This class is responsible
  112. * for communicating these constraints to the bridge over the bridge channel.
  113. */
  114. export default class ReceiveVideoController {
  115. /**
  116. * Creates a new instance for a given conference.
  117. *
  118. * @param {JitsiConference} conference the conference instance for which the new instance will be managing
  119. * the receive video quality constraints.
  120. * @param {RTC} rtc the rtc instance which is responsible for initializing the bridge channel.
  121. */
  122. constructor(conference, rtc) {
  123. this._conference = conference;
  124. this._rtc = rtc;
  125. const { config } = conference.options;
  126. // The number of videos requested from the bridge, -1 represents unlimited or all available videos.
  127. this._lastN = config?.startLastN ?? (config?.channelLastN || LASTN_UNLIMITED);
  128. // The number representing the maximum video height the local client should receive from the bridge.
  129. this._maxFrameHeight = MAX_HEIGHT;
  130. /**
  131. * The map that holds the max frame height requested per remote source for p2p connection.
  132. *
  133. * @type Map<string, number>
  134. */
  135. this._sourceReceiverConstraints = new Map();
  136. /**
  137. * The number of bps requested from the bridge.
  138. */
  139. this._assumedBandwidthBps = ASSUMED_BANDWIDTH_BPS;
  140. // The default receiver video constraints.
  141. this._receiverVideoConstraints = new ReceiverVideoConstraints({
  142. lastN: this._lastN,
  143. assumedBandwidthBps: this._assumedBandwidthBps
  144. });
  145. this._conference.on(
  146. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  147. session => this._onMediaSessionStarted(session));
  148. }
  149. /**
  150. * Returns a map of all the remote source names and the corresponding max frame heights.
  151. *
  152. * @param {JingleSessionPC} mediaSession - the media session.
  153. * @param {number} maxFrameHeight - the height to be requested for remote sources.
  154. * @returns
  155. */
  156. _getDefaultSourceReceiverConstraints(mediaSession, maxFrameHeight) {
  157. const height = maxFrameHeight ?? MAX_HEIGHT;
  158. const remoteVideoTracks = mediaSession.peerconnection?.getRemoteTracks(null, MediaType.VIDEO) || [];
  159. const receiverConstraints = new Map();
  160. for (const track of remoteVideoTracks) {
  161. receiverConstraints.set(track.getSourceName(), height);
  162. }
  163. return receiverConstraints;
  164. }
  165. /**
  166. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  167. * session. The preferred receive frameHeight is applied on the media session.
  168. *
  169. * @param {JingleSessionPC} mediaSession - the started media session.
  170. * @returns {void}
  171. * @private
  172. */
  173. _onMediaSessionStarted(mediaSession) {
  174. if (mediaSession.isP2P) {
  175. mediaSession.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(mediaSession));
  176. } else {
  177. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  178. }
  179. }
  180. /**
  181. * Returns the lastN value for the conference.
  182. *
  183. * @returns {number}
  184. */
  185. getLastN() {
  186. return this._lastN;
  187. }
  188. /**
  189. * Sets the assumed bandwidth bps the local participant should receive from remote participants.
  190. *
  191. * @param {number|undefined} assumedBandwidthBps - the new value.
  192. * @returns {void}
  193. */
  194. setAssumedBandwidthBps(assumedBandwidthBps) {
  195. if (this._receiverVideoConstraints.updateAssumedBandwidthBps(assumedBandwidthBps)) {
  196. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  197. }
  198. }
  199. /**
  200. * Selects a new value for "lastN". The requested amount of videos are going to be delivered after the value is
  201. * in effect. Set to -1 for unlimited or all available videos.
  202. *
  203. * @param {number} value the new value for lastN.
  204. * @returns {void}
  205. */
  206. setLastN(value) {
  207. if (this._lastN !== value) {
  208. this._lastN = value;
  209. if (this._receiverVideoConstraints.updateLastN(value)) {
  210. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  211. }
  212. }
  213. }
  214. /**
  215. * Sets the maximum video resolution the local participant should receive from remote participants.
  216. *
  217. * @param {number|undefined} maxFrameHeight - the new value.
  218. * @returns {void}
  219. */
  220. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  221. this._maxFrameHeight = maxFrameHeight;
  222. for (const session of this._conference.getMediaSessions()) {
  223. if (session.isP2P) {
  224. session.setReceiverVideoConstraint(this._getDefaultSourceReceiverConstraints(session, maxFrameHeight));
  225. } else if (this._receiverVideoConstraints.updateReceiveResolution(maxFrameHeight)) {
  226. this._rtc.setReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  227. }
  228. }
  229. }
  230. /**
  231. * Sets the receiver constraints for the conference.
  232. *
  233. * @param {Object} constraints The video constraints.
  234. */
  235. setReceiverConstraints(constraints) {
  236. if (!constraints) {
  237. return;
  238. }
  239. const isEndpointsFormat = Object.keys(constraints).includes('onStageEndpoints', 'selectedEndpoints');
  240. if (isEndpointsFormat) {
  241. throw new Error(
  242. '"onStageEndpoints" and "selectedEndpoints" are not supported when sourceNameSignaling is enabled.'
  243. );
  244. }
  245. const constraintsChanged = this._receiverVideoConstraints.updateReceiverVideoConstraints(constraints);
  246. if (constraintsChanged) {
  247. this._assumedBandwidthBps = constraints.assumedBandwidthBps ?? this._assumedBandwidthBps;
  248. this._lastN = constraints.lastN ?? this._lastN;
  249. // Send the contraints on the bridge channel.
  250. this._rtc.setReceiverVideoConstraints(constraints);
  251. const p2pSession = this._conference.getMediaSessions().find(session => session.isP2P);
  252. if (!p2pSession) {
  253. return;
  254. }
  255. const mappedConstraints = Array.from(Object.entries(constraints.constraints))
  256. .map(constraint => {
  257. constraint[1] = constraint[1].maxHeight;
  258. return constraint;
  259. });
  260. this._sourceReceiverConstraints = new Map(mappedConstraints);
  261. // Send the receiver constraints to the peer through a "content-modify" message.
  262. p2pSession.setReceiverVideoConstraint(this._sourceReceiverConstraints);
  263. }
  264. }
  265. }