Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CodecSelection.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { getLogger } from '@jitsi/logger';
  2. import * as JitsiConferenceEvents from '../../JitsiConferenceEvents';
  3. import CodecMimeType from '../../service/RTC/CodecMimeType';
  4. import { MediaType } from '../../service/RTC/MediaType';
  5. import browser from '../browser';
  6. const logger = getLogger(__filename);
  7. // Default video codec preferences on mobile and desktop endpoints.
  8. const DESKTOP_VIDEO_CODEC_ORDER = [ CodecMimeType.VP9, CodecMimeType.VP8, CodecMimeType.H264 ];
  9. const MOBILE_P2P_VIDEO_CODEC_ORDER = [ CodecMimeType.H264, CodecMimeType.VP8, CodecMimeType.VP9 ];
  10. const MOBILE_VIDEO_CODEC_ORDER = [ CodecMimeType.VP8, CodecMimeType.VP9, CodecMimeType.H264 ];
  11. /**
  12. * This class handles the codec selection mechanism for the conference based on the config.js settings.
  13. * The preferred codec is selected based on the settings and the list of codecs supported by the browser.
  14. * The preferred codec is published in presence which is then used by the other endpoints in the
  15. * conference to pick a supported codec at join time and when the call transitions between p2p and jvb
  16. * connections.
  17. */
  18. export class CodecSelection {
  19. /**
  20. * Creates a new instance for a given conference.
  21. *
  22. * @param {JitsiConference} conference the conference instance
  23. * @param {*} options
  24. * @param {string} options.jvb settings (codec list, preferred and disabled) for the jvb connection.
  25. * @param {string} options.p2p settings (codec list, preferred and disabled) for the p2p connection.
  26. */
  27. constructor(conference, options) {
  28. this.conference = conference;
  29. this.options = options;
  30. this.codecPreferenceOrder = {};
  31. for (const connectionType of Object.keys(options)) {
  32. // eslint-disable-next-line prefer-const
  33. let { disabledCodec, preferredCodec, preferenceOrder } = options[connectionType];
  34. const supportedCodecs = new Set(this._getSupportedVideoCodecs(connectionType));
  35. // Default preference codec order when no codec preferences are set in config.js
  36. let selectedOrder = Array.from(supportedCodecs);
  37. if (preferenceOrder) {
  38. preferenceOrder = preferenceOrder.map(codec => codec.toLowerCase());
  39. // Select all codecs that are supported by the browser.
  40. selectedOrder = preferenceOrder.filter(codec => supportedCodecs.has(codec));
  41. // Generate the codec list based on the supported codecs and the preferred/disabled (deprecated) settings
  42. } else if (preferredCodec || disabledCodec) {
  43. disabledCodec = disabledCodec?.toLowerCase();
  44. preferredCodec = preferredCodec?.toLowerCase();
  45. // VP8 cannot be disabled since it the default codec.
  46. if (disabledCodec && disabledCodec !== CodecMimeType.VP8) {
  47. selectedOrder = selectedOrder.filter(codec => codec !== disabledCodec);
  48. }
  49. const index = selectedOrder.findIndex(codec => codec === preferredCodec);
  50. // Move the preferred codec to the top of the list.
  51. if (preferredCodec && index !== -1) {
  52. selectedOrder.splice(index, 1);
  53. selectedOrder.unshift(preferredCodec);
  54. }
  55. }
  56. // Push VP9 to the end of the list so that the client continues to decode VP9 even if its not
  57. // preferable to encode VP9 (because of browser bugs on the encoding side or added complexity on mobile
  58. // devices).
  59. if (!browser.supportsVP9() || this.conference.isE2EEEnabled()) {
  60. const index = selectedOrder.findIndex(codec => codec === CodecMimeType.VP9);
  61. if (index !== -1) {
  62. selectedOrder.splice(index, 1);
  63. // Remove VP9 from the list when E2EE is enabled since it is not supported.
  64. // TODO - remove this check when support for VP9-E2EE is introduced.
  65. if (!this.conference.isE2EEEnabled()) {
  66. selectedOrder.push(CodecMimeType.VP9);
  67. }
  68. }
  69. }
  70. logger.info(`Codec preference order for ${connectionType} connection is ${selectedOrder}`);
  71. this.codecPreferenceOrder[connectionType] = selectedOrder;
  72. }
  73. this.conference.on(
  74. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  75. session => this._selectPreferredCodec(session));
  76. this.conference.on(
  77. JitsiConferenceEvents.USER_JOINED,
  78. () => this._selectPreferredCodec());
  79. this.conference.on(
  80. JitsiConferenceEvents.USER_LEFT,
  81. () => this._selectPreferredCodec());
  82. }
  83. /**
  84. * Returns a list of video codecs that are supported by the browser.
  85. *
  86. * @param {string} connectionType - media connection type, p2p or jvb.
  87. * @returns {Array}
  88. */
  89. _getSupportedVideoCodecs(connectionType) {
  90. const videoCodecMimeTypes = browser.isMobileDevice() && connectionType === 'p2p'
  91. ? MOBILE_P2P_VIDEO_CODEC_ORDER
  92. : browser.isMobileDevice() ? MOBILE_VIDEO_CODEC_ORDER : DESKTOP_VIDEO_CODEC_ORDER;
  93. return videoCodecMimeTypes.filter(codec =>
  94. (window.RTCRtpReceiver?.getCapabilities?.(MediaType.VIDEO)?.codecs ?? [])
  95. .some(supportedCodec => supportedCodec.mimeType.toLowerCase() === `${MediaType.VIDEO}/${codec}`));
  96. }
  97. /**
  98. * Filters VP9 from the list of the preferred video codecs for JVB if E2EE is enabled.
  99. *
  100. * @returns {Array}
  101. */
  102. _maybeFilterJvbCodecs() {
  103. // TODO - remove this check when support for VP9-E2EE is introduced.
  104. if (this.conference.isE2EEEnabled()) {
  105. return this.codecPreferenceOrder.jvb.filter(codec => codec !== CodecMimeType.VP9);
  106. }
  107. return this.codecPreferenceOrder.jvb;
  108. }
  109. /**
  110. * Sets the codec on the media session based on the codec preference order configured in config.js and the supported
  111. * codecs published by the remote participants in their presence.
  112. *
  113. * @param {JingleSessionPC} mediaSession session for which the codec selection has to be made.
  114. */
  115. _selectPreferredCodec(mediaSession) {
  116. const session = mediaSession ? mediaSession : this.conference.jvbJingleSession;
  117. if (!session) {
  118. return;
  119. }
  120. const currentCodecOrder = session.peerconnection.getConfiguredVideoCodecs();
  121. const localPreferredCodecOrder = session === this.conference.jvbJingleSession
  122. ? this._maybeFilterJvbCodecs()
  123. : this.codecPreferenceOrder.p2p;
  124. const remoteParticipants = this.conference.getParticipants().map(participant => participant.getId());
  125. const remoteCodecsPerParticipant = remoteParticipants?.map(remote => {
  126. const peerMediaInfo = session._signalingLayer.getPeerMediaInfo(remote, MediaType.VIDEO);
  127. return peerMediaInfo
  128. ? peerMediaInfo.codecList ?? [ peerMediaInfo.codecType ]
  129. : [];
  130. });
  131. const selectedCodecOrder = localPreferredCodecOrder.reduce((acc, localCodec) => {
  132. let codecNotSupportedByRemote = false;
  133. // Ignore remote codecs for p2p since only the JVB codec preferences are published in presence.
  134. // For p2p, we rely on the codec order present in the remote offer/answer.
  135. if (!session.isP2P) {
  136. // Remove any codecs that are not supported by any of the remote endpoints. The order of the supported
  137. // codecs locally however will remain the same since we want to support asymmetric codecs.
  138. for (const remoteCodecs of remoteCodecsPerParticipant) {
  139. codecNotSupportedByRemote = codecNotSupportedByRemote
  140. || !remoteCodecs.find(participantCodec => participantCodec === localCodec);
  141. }
  142. }
  143. if (!codecNotSupportedByRemote) {
  144. acc.push(localCodec);
  145. }
  146. return acc;
  147. }, []);
  148. if (!selectedCodecOrder.length) {
  149. logger.warn('Invalid codec list generated because of a user joining/leaving the call');
  150. return;
  151. }
  152. // Reconfigure the codecs on the media session.
  153. if (!selectedCodecOrder.every((val, index) => val === currentCodecOrder[index])) {
  154. session.setVideoCodecs(selectedCodecOrder);
  155. }
  156. }
  157. /**
  158. * Returns the current codec preference order for the given connection type.
  159. *
  160. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  161. * @returns {Array<string>}
  162. */
  163. getCodecPreferenceList(connectionType) {
  164. return this.codecPreferenceOrder[connectionType];
  165. }
  166. }