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

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