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.

QualityController.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  2. import MediaSessionEvents from '../xmpp/MediaSessionEvents';
  3. /**
  4. * The class manages send and receive video constraints across media sessions({@link JingleSessionPC}) which belong to
  5. * {@link JitsiConference}. It finds the lowest common value, between the local user's send preference and
  6. * the remote party's receive preference. Also this module will consider only the active session's receive value,
  7. * because local tracks are shared and while JVB may have no preference, the remote p2p may have and they may be totally
  8. * different.
  9. */
  10. export class QualityController {
  11. /**
  12. * Creates new instance for a given conference.
  13. *
  14. * @param {JitsiConference} conference - the conference instance for which the new instance will be managing
  15. * the quality constraints.
  16. */
  17. constructor(conference) {
  18. this.conference = conference;
  19. this.conference.on(
  20. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  21. session => this._onMediaSessionStarted(session));
  22. this.conference.on(
  23. JitsiConferenceEvents._MEDIA_SESSION_ACTIVE_CHANGED,
  24. () => this._propagateSendMaxFrameHeight());
  25. }
  26. /**
  27. * Handles the {@link JitsiConferenceEvents.MEDIA_SESSION_STARTED}, that is when the conference creates new media
  28. * session. It doesn't mean it's already active though. For example the JVB connection may be created after
  29. * the conference has entered the p2p mode already.
  30. *
  31. * @param {JingleSessionPC} mediaSession - the started media session.
  32. * @private
  33. */
  34. _onMediaSessionStarted(mediaSession) {
  35. mediaSession.addListener(
  36. MediaSessionEvents.REMOTE_VIDEO_CONSTRAINTS_CHANGED,
  37. session => {
  38. if (session === this.conference._getActiveMediaSession()) {
  39. this._propagateSendMaxFrameHeight();
  40. }
  41. });
  42. this.preferredReceiveMaxFrameHeight
  43. && mediaSession.setReceiverVideoConstraint(this.preferredReceiveMaxFrameHeight);
  44. // Set the degradation preference on the local video track.
  45. mediaSession.setSenderVideoDegradationPreference();
  46. // Set the max bitrates on video sender if they are specified in config.js videoQuality settings.
  47. mediaSession.setSenderMaxBitrates();
  48. }
  49. /**
  50. * Figures out the send video constraint as specified by {@link selectSendMaxFrameHeight} and sets it on all media
  51. * sessions for the reasons mentioned in this class description.
  52. *
  53. * @returns {Promise<void[]>}
  54. * @private
  55. */
  56. _propagateSendMaxFrameHeight() {
  57. const sendMaxFrameHeight = this.selectSendMaxFrameHeight();
  58. const promises = [];
  59. if (!sendMaxFrameHeight) {
  60. return Promise.resolve();
  61. }
  62. for (const session of this.conference._getMediaSessions()) {
  63. promises.push(session.setSenderVideoConstraint(sendMaxFrameHeight));
  64. }
  65. return Promise.all(promises);
  66. }
  67. /**
  68. * Selects the lowest common value for the local video send constraint by looking at local user's preference and
  69. * the active media session's receive preference set by the remote party.
  70. *
  71. * @returns {number|undefined}
  72. */
  73. selectSendMaxFrameHeight() {
  74. const activeMediaSession = this.conference._getActiveMediaSession();
  75. const remoteRecvMaxFrameHeight = activeMediaSession && activeMediaSession.getRemoteRecvMaxFrameHeight();
  76. if (this.preferredSendMaxFrameHeight && remoteRecvMaxFrameHeight) {
  77. return Math.min(this.preferredSendMaxFrameHeight, remoteRecvMaxFrameHeight);
  78. } else if (remoteRecvMaxFrameHeight) {
  79. return remoteRecvMaxFrameHeight;
  80. }
  81. return this.preferredSendMaxFrameHeight;
  82. }
  83. /**
  84. * Sets local preference for max receive video frame height.
  85. * @param {number|undefined} maxFrameHeight - the new value.
  86. */
  87. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  88. this.preferredReceiveMaxFrameHeight = maxFrameHeight;
  89. for (const session of this.conference._getMediaSessions()) {
  90. maxFrameHeight && session.setReceiverVideoConstraint(maxFrameHeight);
  91. }
  92. }
  93. /**
  94. * Sets local preference for max send video frame height.
  95. *
  96. * @param {number} maxFrameHeight - the new value to set.
  97. * @returns {Promise<void[]>} - resolved when the operation is complete.
  98. */
  99. setPreferredSendMaxFrameHeight(maxFrameHeight) {
  100. this.preferredSendMaxFrameHeight = maxFrameHeight;
  101. return this._propagateSendMaxFrameHeight();
  102. }
  103. }