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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  47. /**
  48. * Figures out the send video constraint as specified by {@link selectSendMaxFrameHeight} and sets it on all media
  49. * sessions for the reasons mentioned in this class description.
  50. *
  51. * @returns {Promise<void[]>}
  52. * @private
  53. */
  54. _propagateSendMaxFrameHeight() {
  55. const sendMaxFrameHeight = this.selectSendMaxFrameHeight();
  56. const promises = [];
  57. if (!sendMaxFrameHeight) {
  58. return Promise.resolve();
  59. }
  60. for (const session of this.conference._getMediaSessions()) {
  61. promises.push(session.setSenderVideoConstraint(sendMaxFrameHeight));
  62. }
  63. return Promise.all(promises);
  64. }
  65. /**
  66. * Selects the lowest common value for the local video send constraint by looking at local user's preference and
  67. * the active media session's receive preference set by the remote party.
  68. *
  69. * @returns {number|undefined}
  70. */
  71. selectSendMaxFrameHeight() {
  72. const activeMediaSession = this.conference._getActiveMediaSession();
  73. const remoteRecvMaxFrameHeight = activeMediaSession && activeMediaSession.getRemoteRecvMaxFrameHeight();
  74. if (this.preferredSendMaxFrameHeight && remoteRecvMaxFrameHeight) {
  75. return Math.min(this.preferredSendMaxFrameHeight, remoteRecvMaxFrameHeight);
  76. } else if (remoteRecvMaxFrameHeight) {
  77. return remoteRecvMaxFrameHeight;
  78. }
  79. return this.preferredSendMaxFrameHeight;
  80. }
  81. /**
  82. * Sets local preference for max receive video frame height.
  83. * @param {number|undefined} maxFrameHeight - the new value.
  84. */
  85. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  86. this.preferredReceiveMaxFrameHeight = maxFrameHeight;
  87. for (const session of this.conference._getMediaSessions()) {
  88. maxFrameHeight && session.setReceiverVideoConstraint(maxFrameHeight);
  89. }
  90. }
  91. /**
  92. * Sets local preference for max send video frame height.
  93. *
  94. * @param {number} maxFrameHeight - the new value to set.
  95. * @returns {Promise<void[]>} - resolved when the operation is complete.
  96. */
  97. setPreferredSendMaxFrameHeight(maxFrameHeight) {
  98. this.preferredSendMaxFrameHeight = maxFrameHeight;
  99. return this._propagateSendMaxFrameHeight();
  100. }
  101. }