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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { getLogger } from 'jitsi-meet-logger';
  2. import isEqual from 'lodash.isequal';
  3. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  4. const logger = getLogger(__filename);
  5. const MAX_HEIGHT_ONSTAGE = 2160;
  6. const MAX_HEIGHT_THUMBNAIL = 180;
  7. const LASTN_UNLIMITED = -1;
  8. /**
  9. * This class translates the legacy signaling format between the client and the bridge (that affects bandwidth
  10. * allocation) to the new format described here https://github.com/jitsi/jitsi-videobridge/blob/master/doc/allocation.md
  11. */
  12. export class ReceiverVideoConstraints {
  13. /**
  14. * Creates a new instance.
  15. */
  16. constructor() {
  17. // Default constraints used for endpoints that are not explicitly included in constraints.
  18. // These constraints are used for endpoints that are thumbnails in the stage view.
  19. this._defaultConstraints = { 'maxHeight': MAX_HEIGHT_THUMBNAIL };
  20. // The number of videos requested from the bridge.
  21. this._lastN = LASTN_UNLIMITED;
  22. // The number representing the maximum video height the local client should receive from the bridge.
  23. this._maxFrameHeight = MAX_HEIGHT_ONSTAGE;
  24. // The endpoint IDs of the participants that are currently selected.
  25. this._selectedEndpoints = [];
  26. this._receiverVideoConstraints = {
  27. constraints: {},
  28. defaultConstraints: this.defaultConstraints,
  29. lastN: this._lastN,
  30. onStageEndpoints: [],
  31. selectedEndpoints: this._selectedEndpoints
  32. };
  33. }
  34. /**
  35. * Returns the receiver video constraints that need to be sent on the bridge channel.
  36. */
  37. get constraints() {
  38. this._receiverVideoConstraints.lastN = this._lastN;
  39. if (!this._selectedEndpoints.length) {
  40. return this._receiverVideoConstraints;
  41. }
  42. // The client is assumed to be in TileView if it has selected more than one endpoint, otherwise it is
  43. // assumed to be in StageView.
  44. this._receiverVideoConstraints.constraints = {};
  45. if (this._selectedEndpoints.length > 1) {
  46. /**
  47. * Tile view.
  48. * Only the default constraints are specified here along with lastN (if it is set).
  49. * {
  50. * 'colibriClass': 'ReceiverVideoConstraints',
  51. * 'defaultConstraints': { 'maxHeight': 360 }
  52. * }
  53. */
  54. this._receiverVideoConstraints.defaultConstraints = { 'maxHeight': this._maxFrameHeight };
  55. this._receiverVideoConstraints.onStageEndpoints = [];
  56. this._receiverVideoConstraints.selectedEndpoints = [];
  57. } else {
  58. /**
  59. * Stage view.
  60. * The participant on stage is specified in onStageEndpoints and a higher maxHeight is specified
  61. * for that endpoint while a default maxHeight of 180 is applied to all the other endpoints.
  62. * {
  63. * 'colibriClass': 'ReceiverVideoConstraints',
  64. * 'onStageEndpoints': ['A'],
  65. * 'defaultConstraints': { 'maxHeight': 180 },
  66. * 'constraints': {
  67. * 'A': { 'maxHeight': 720 }
  68. * }
  69. * }
  70. */
  71. this._receiverVideoConstraints.constraints[this._selectedEndpoints[0]] = {
  72. 'maxHeight': this._maxFrameHeight
  73. };
  74. this._receiverVideoConstraints.defaultConstraints = this._defaultConstraints;
  75. this._receiverVideoConstraints.onStageEndpoints = this._selectedEndpoints;
  76. this._receiverVideoConstraints.selectedEndpoints = [];
  77. }
  78. return this._receiverVideoConstraints;
  79. }
  80. /**
  81. * Updates the lastN field of the ReceiverVideoConstraints sent to the bridge.
  82. *
  83. * @param {number} value
  84. * @returns {boolean} Returns true if the the value has been updated, false otherwise.
  85. */
  86. updateLastN(value) {
  87. const changed = this._lastN !== value;
  88. if (changed) {
  89. this._lastN = value;
  90. logger.debug(`Updating ReceiverVideoConstraints lastN(${value})`);
  91. }
  92. return changed;
  93. }
  94. /**
  95. * Updates the resolution (height requested) in the contraints field of the ReceiverVideoConstraints
  96. * sent to the bridge.
  97. *
  98. * @param {number} maxFrameHeight
  99. * @requires {boolean} Returns true if the the value has been updated, false otherwise.
  100. */
  101. updateReceiveResolution(maxFrameHeight) {
  102. const changed = this._maxFrameHeight !== maxFrameHeight;
  103. if (changed) {
  104. this._maxFrameHeight = maxFrameHeight;
  105. logger.debug(`Updating receive maxFrameHeight: ${maxFrameHeight}`);
  106. }
  107. return changed;
  108. }
  109. /**
  110. * Updates the receiver constraints sent to the bridge.
  111. *
  112. * @param {Object} videoConstraints
  113. * @returns {boolean} Returns true if the the value has been updated, false otherwise.
  114. */
  115. updateReceiverVideoConstraints(videoConstraints) {
  116. const changed = !isEqual(this._receiverVideoConstraints, videoConstraints);
  117. if (changed) {
  118. this._receiverVideoConstraints = videoConstraints;
  119. logger.debug(`Updating ReceiverVideoConstraints ${JSON.stringify(videoConstraints)}`);
  120. }
  121. return changed;
  122. }
  123. /**
  124. * Updates the list of selected endpoints.
  125. *
  126. * @param {Array<string>} ids
  127. * @returns {void}
  128. */
  129. updateSelectedEndpoints(ids) {
  130. logger.debug(`Updating selected endpoints: ${JSON.stringify(ids)}`);
  131. this._selectedEndpoints = ids;
  132. }
  133. }
  134. /**
  135. * This class manages the receive video contraints for a given {@link JitsiConference}. These constraints are
  136. * determined by the application based on how the remote video streams need to be displayed. This class is responsible
  137. * for communicating these constraints to the bridge over the bridge channel.
  138. */
  139. export class ReceiveVideoController {
  140. /**
  141. * Creates a new instance for a given conference.
  142. *
  143. * @param {JitsiConference} conference the conference instance for which the new instance will be managing
  144. * the receive video quality constraints.
  145. * @param {RTC} rtc the rtc instance which is responsible for initializing the bridge channel.
  146. */
  147. constructor(conference, rtc) {
  148. this._conference = conference;
  149. this._rtc = rtc;
  150. // Translate the legacy bridge channel signaling format to the new format.
  151. this._receiverVideoConstraints = conference.options?.config?.useNewBandwidthAllocationStrategy
  152. ? new ReceiverVideoConstraints()
  153. : undefined;
  154. // The number of videos requested from the bridge, -1 represents unlimited or all available videos.
  155. this._lastN = LASTN_UNLIMITED;
  156. // The number representing the maximum video height the local client should receive from the bridge.
  157. this._maxFrameHeight = MAX_HEIGHT_ONSTAGE;
  158. // The endpoint IDs of the participants that are currently selected.
  159. this._selectedEndpoints = [];
  160. this._conference.on(
  161. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  162. session => this._onMediaSessionStarted(session));
  163. }
  164. /**
  165. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  166. * session. The preferred receive frameHeight is applied on the media session.
  167. *
  168. * @param {JingleSessionPC} mediaSession - the started media session.
  169. * @returns {void}
  170. * @private
  171. */
  172. _onMediaSessionStarted(mediaSession) {
  173. this._maxFrameHeight && mediaSession.setReceiverVideoConstraint(this._maxFrameHeight);
  174. }
  175. /**
  176. * Returns the lastN value for the conference.
  177. *
  178. * @returns {number}
  179. */
  180. getLastN() {
  181. return this._lastN;
  182. }
  183. /**
  184. * Elects the participants with the given ids to be the selected participants in order to always receive video
  185. * for this participant (even when last n is enabled).
  186. *
  187. * @param {Array<string>} ids - The user ids.
  188. * @returns {void}
  189. */
  190. selectEndpoints(ids) {
  191. this._selectedEndpoints = ids;
  192. if (this._receiverVideoConstraints) {
  193. // Filter out the local endpointId from the list of selected endpoints.
  194. const remoteEndpointIds = ids.filter(id => id !== this._conference.myUserId());
  195. const oldConstraints = JSON.parse(JSON.stringify(this._receiverVideoConstraints.constraints));
  196. remoteEndpointIds.length && this._receiverVideoConstraints.updateSelectedEndpoints(remoteEndpointIds);
  197. const newConstraints = this._receiverVideoConstraints.constraints;
  198. // Send bridge message only when the constraints change.
  199. if (!isEqual(newConstraints, oldConstraints)) {
  200. this._rtc.setNewReceiverVideoConstraints(newConstraints);
  201. }
  202. return;
  203. }
  204. this._rtc.selectEndpoints(ids);
  205. }
  206. /**
  207. * Selects a new value for "lastN". The requested amount of videos are going to be delivered after the value is
  208. * in effect. Set to -1 for unlimited or all available videos.
  209. *
  210. * @param {number} value the new value for lastN.
  211. * @returns {void}
  212. */
  213. setLastN(value) {
  214. if (this._lastN !== value) {
  215. this._lastN = value;
  216. if (this._receiverVideoConstraints) {
  217. const lastNUpdated = this._receiverVideoConstraints.updateLastN(value);
  218. // Send out the message on the bridge channel if lastN was updated.
  219. lastNUpdated && this._rtc.setNewReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  220. return;
  221. }
  222. this._rtc.setLastN(value);
  223. }
  224. }
  225. /**
  226. * Sets the maximum video resolution the local participant should receive from remote participants.
  227. *
  228. * @param {number|undefined} maxFrameHeight - the new value.
  229. * @returns {void}
  230. */
  231. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  232. this._maxFrameHeight = maxFrameHeight;
  233. for (const session of this._conference._getMediaSessions()) {
  234. if (session.isP2P || !this._receiverVideoConstraints) {
  235. maxFrameHeight && session.setReceiverVideoConstraint(maxFrameHeight);
  236. } else {
  237. const resolutionUpdated = this._receiverVideoConstraints.updateReceiveResolution(maxFrameHeight);
  238. resolutionUpdated
  239. && this._rtc.setNewReceiverVideoConstraints(this._receiverVideoConstraints.constraints);
  240. }
  241. }
  242. }
  243. /**
  244. * Sets the receiver constraints for the conference.
  245. *
  246. * @param {Object} constraints The video constraints.
  247. */
  248. setReceiverConstraints(constraints) {
  249. if (!this._receiverVideoConstraints) {
  250. this._receiverVideoConstraints = new ReceiverVideoConstraints();
  251. }
  252. const constraintsChanged = this._receiverVideoConstraints.updateReceiverVideoConstraints(constraints);
  253. constraintsChanged && this._rtc.setNewReceiverVideoConstraints(constraints);
  254. }
  255. }