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.

SendVideoController.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { getLogger } from '@jitsi/logger';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. import RTCEvents from '../../service/RTC/RTCEvents';
  4. import FeatureFlags from '../flags/FeatureFlags';
  5. import MediaSessionEvents from '../xmpp/MediaSessionEvents';
  6. const logger = getLogger(__filename);
  7. const MAX_LOCAL_RESOLUTION = 2160;
  8. /**
  9. * The class manages send video constraints across media sessions({@link JingleSessionPC}) which belong to
  10. * {@link JitsiConference}. It finds the lowest common value, between the local user's send preference and
  11. * the remote party's receive preference. Also this module will consider only the active session's receive value,
  12. * because local tracks are shared and while JVB may have no preference, the remote p2p may have and they may be totally
  13. * different.
  14. */
  15. export default class SendVideoController {
  16. /**
  17. * Creates new instance for a given conference.
  18. *
  19. * @param {JitsiConference} conference - the conference instance for which the new instance will be managing
  20. * the send video quality constraints.
  21. * @param {RTC} rtc - the rtc instance that is responsible for sending the messages on the bridge channel.
  22. */
  23. constructor(conference, rtc) {
  24. this._conference = conference;
  25. this._preferredSendMaxFrameHeight = MAX_LOCAL_RESOLUTION;
  26. this._rtc = rtc;
  27. /**
  28. * Source name based sender constraints.
  29. * @type {Map<string, number>};
  30. */
  31. this._sourceSenderConstraints = new Map();
  32. this._conference.on(
  33. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  34. session => this._onMediaSessionStarted(session));
  35. this._conference.on(
  36. JitsiConferenceEvents._MEDIA_SESSION_ACTIVE_CHANGED,
  37. () => this._configureConstraintsForLocalSources());
  38. this._rtc.on(
  39. RTCEvents.SENDER_VIDEO_CONSTRAINTS_CHANGED,
  40. videoConstraints => this._onSenderConstraintsReceived(videoConstraints));
  41. }
  42. /**
  43. * Configures the video encodings on the local sources when a media connection is established or becomes active.
  44. *
  45. * @returns {Promise<void[]>}
  46. * @private
  47. */
  48. _configureConstraintsForLocalSources() {
  49. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  50. for (const track of this._rtc.getLocalVideoTracks()) {
  51. const sourceName = track.getSourceName();
  52. sourceName && this._propagateSendMaxFrameHeight(sourceName);
  53. }
  54. } else {
  55. this._propagateSendMaxFrameHeight();
  56. }
  57. }
  58. /**
  59. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  60. * session. It doesn't mean it's already active though. For example the JVB connection may be created after
  61. * the conference has entered the p2p mode already.
  62. *
  63. * @param {JingleSessionPC} mediaSession - the started media session.
  64. * @private
  65. */
  66. _onMediaSessionStarted(mediaSession) {
  67. mediaSession.addListener(
  68. MediaSessionEvents.REMOTE_VIDEO_CONSTRAINTS_CHANGED,
  69. session => {
  70. if (session === this._conference.getActiveMediaSession()) {
  71. this._configureConstraintsForLocalSources();
  72. }
  73. });
  74. }
  75. /**
  76. * Propagates the video constraints if they have changed.
  77. *
  78. * @param {Object} videoConstraints - The sender video constraints received from the bridge.
  79. * @returns {Promise<void[]>}
  80. * @private
  81. */
  82. _onSenderConstraintsReceived(videoConstraints) {
  83. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  84. const { maxHeight, sourceName } = videoConstraints;
  85. const localVideoTracks = this._conference.getLocalVideoTracks() ?? [];
  86. for (const track of localVideoTracks) {
  87. // Propagate the sender constraint only if it has changed.
  88. if (track.getSourceName() === sourceName
  89. && (!this._sourceSenderConstraints.has(sourceName)
  90. || this._sourceSenderConstraints.get(sourceName) !== maxHeight)) {
  91. this._sourceSenderConstraints.set(sourceName, maxHeight);
  92. logger.debug(`Sender constraints for source:${sourceName} changed to maxHeight:${maxHeight}`);
  93. this._propagateSendMaxFrameHeight(sourceName);
  94. }
  95. }
  96. } else if (this._senderVideoConstraints?.idealHeight !== videoConstraints.idealHeight) {
  97. this._senderVideoConstraints = videoConstraints;
  98. this._propagateSendMaxFrameHeight();
  99. }
  100. }
  101. /**
  102. * Figures out the send video constraint as specified by {@link _selectSendMaxFrameHeight} and sets it on all media
  103. * sessions for the reasons mentioned in this class description.
  104. *
  105. * @param {string} sourceName - The source for which sender constraints have changed.
  106. * @returns {Promise<void[]>}
  107. * @private
  108. */
  109. _propagateSendMaxFrameHeight(sourceName = null) {
  110. if (FeatureFlags.isSourceNameSignalingEnabled() && !sourceName) {
  111. throw new Error('sourceName missing for calculating the sendMaxHeight for video tracks');
  112. }
  113. const sendMaxFrameHeight = this._selectSendMaxFrameHeight(sourceName);
  114. const promises = [];
  115. if (sendMaxFrameHeight >= 0) {
  116. for (const session of this._conference.getMediaSessions()) {
  117. promises.push(session.setSenderVideoConstraint(sendMaxFrameHeight, sourceName));
  118. }
  119. }
  120. return Promise.all(promises);
  121. }
  122. /**
  123. * Selects the lowest common value for the local video send constraint by looking at local user's preference and
  124. * the active media session's receive preference set by the remote party.
  125. *
  126. * @param {string} sourceName - The source for which sender constraints have changed.
  127. * @returns {number|undefined}
  128. * @private
  129. */
  130. _selectSendMaxFrameHeight(sourceName = null) {
  131. if (FeatureFlags.isSourceNameSignalingEnabled() && !sourceName) {
  132. throw new Error('sourceName missing for calculating the sendMaxHeight for video tracks');
  133. }
  134. const activeMediaSession = this._conference.getActiveMediaSession();
  135. const remoteRecvMaxFrameHeight = activeMediaSession
  136. ? activeMediaSession.isP2P
  137. ? activeMediaSession.getRemoteRecvMaxFrameHeight()
  138. : sourceName ? this._sourceSenderConstraints.get(sourceName) : this._senderVideoConstraints?.idealHeight
  139. : undefined;
  140. if (this._preferredSendMaxFrameHeight >= 0 && remoteRecvMaxFrameHeight >= 0) {
  141. return Math.min(this._preferredSendMaxFrameHeight, remoteRecvMaxFrameHeight);
  142. } else if (remoteRecvMaxFrameHeight >= 0) {
  143. return remoteRecvMaxFrameHeight;
  144. }
  145. return this._preferredSendMaxFrameHeight;
  146. }
  147. /**
  148. * Sets local preference for max send video frame height.
  149. *
  150. * @param {number} maxFrameHeight - the new value to set.
  151. * @returns {Promise<void[]>} - resolved when the operation is complete.
  152. */
  153. setPreferredSendMaxFrameHeight(maxFrameHeight) {
  154. this._preferredSendMaxFrameHeight = maxFrameHeight;
  155. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  156. const promises = [];
  157. for (const sourceName of this._sourceSenderConstraints.keys()) {
  158. promises.push(this._propagateSendMaxFrameHeight(sourceName));
  159. }
  160. return Promise.allSettled(promises);
  161. }
  162. return this._propagateSendMaxFrameHeight();
  163. }
  164. }