Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

QualityController.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 >= 0) {
  60. for (const session of this.conference._getMediaSessions()) {
  61. promises.push(session.setSenderVideoConstraint(sendMaxFrameHeight));
  62. }
  63. }
  64. return Promise.all(promises);
  65. }
  66. /**
  67. * Selects the lowest common value for the local video send constraint by looking at local user's preference and
  68. * the active media session's receive preference set by the remote party.
  69. *
  70. * @returns {number|undefined}
  71. */
  72. selectSendMaxFrameHeight() {
  73. const activeMediaSession = this.conference._getActiveMediaSession();
  74. const remoteRecvMaxFrameHeight = activeMediaSession && activeMediaSession.getRemoteRecvMaxFrameHeight();
  75. if (this.preferredSendMaxFrameHeight >= 0 && remoteRecvMaxFrameHeight >= 0) {
  76. return Math.min(this.preferredSendMaxFrameHeight, remoteRecvMaxFrameHeight);
  77. } else if (remoteRecvMaxFrameHeight >= 0) {
  78. return remoteRecvMaxFrameHeight;
  79. }
  80. return this.preferredSendMaxFrameHeight;
  81. }
  82. /**
  83. * Sets local preference for max receive video frame height.
  84. * @param {number|undefined} maxFrameHeight - the new value.
  85. */
  86. setPreferredReceiveMaxFrameHeight(maxFrameHeight) {
  87. this.preferredReceiveMaxFrameHeight = maxFrameHeight;
  88. for (const session of this.conference._getMediaSessions()) {
  89. maxFrameHeight && session.setReceiverVideoConstraint(maxFrameHeight);
  90. }
  91. }
  92. /**
  93. * Sets local preference for max send video frame height.
  94. *
  95. * @param {number} maxFrameHeight - the new value to set.
  96. * @returns {Promise<void[]>} - resolved when the operation is complete.
  97. */
  98. setPreferredSendMaxFrameHeight(maxFrameHeight) {
  99. this.preferredSendMaxFrameHeight = maxFrameHeight;
  100. return this._propagateSendMaxFrameHeight();
  101. }
  102. }