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.

CodecSelection.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. this.visitorCodecs = [];
  32. for (const connectionType of Object.keys(options)) {
  33. // eslint-disable-next-line prefer-const
  34. let { disabledCodec, preferredCodec, preferenceOrder } = options[connectionType];
  35. const supportedCodecs = new Set(this._getSupportedVideoCodecs(connectionType));
  36. // Default preference codec order when no codec preferences are set in config.js
  37. let selectedOrder = Array.from(supportedCodecs);
  38. if (preferenceOrder) {
  39. preferenceOrder = preferenceOrder.map(codec => codec.toLowerCase());
  40. // Select all codecs that are supported by the browser.
  41. selectedOrder = preferenceOrder.filter(codec => supportedCodecs.has(codec));
  42. // Generate the codec list based on the supported codecs and the preferred/disabled (deprecated) settings
  43. } else if (preferredCodec || disabledCodec) {
  44. disabledCodec = disabledCodec?.toLowerCase();
  45. preferredCodec = preferredCodec?.toLowerCase();
  46. // VP8 cannot be disabled since it the default codec.
  47. if (disabledCodec && disabledCodec !== CodecMimeType.VP8) {
  48. selectedOrder = selectedOrder.filter(codec => codec !== disabledCodec);
  49. }
  50. const index = selectedOrder.findIndex(codec => codec === preferredCodec);
  51. // Move the preferred codec to the top of the list.
  52. if (preferredCodec && index !== -1) {
  53. selectedOrder.splice(index, 1);
  54. selectedOrder.unshift(preferredCodec);
  55. }
  56. }
  57. // Push VP9 to the end of the list so that the client continues to decode VP9 even if its not
  58. // preferable to encode VP9 (because of browser bugs on the encoding side or added complexity on mobile
  59. // devices). Currently, VP9 encode is supported on Chrome and on Safari (only for p2p).
  60. const isVp9EncodeSupported = browser.supportsVP9() || (browser.isWebKitBased() && connectionType === 'p2p');
  61. if (!isVp9EncodeSupported || this.conference.isE2EEEnabled()) {
  62. const index = selectedOrder.findIndex(codec => codec === CodecMimeType.VP9);
  63. if (index !== -1) {
  64. selectedOrder.splice(index, 1);
  65. // Remove VP9 from the list when E2EE is enabled since it is not supported.
  66. // TODO - remove this check when support for VP9-E2EE is introduced.
  67. if (!this.conference.isE2EEEnabled()) {
  68. selectedOrder.push(CodecMimeType.VP9);
  69. }
  70. }
  71. }
  72. logger.info(`Codec preference order for ${connectionType} connection is ${selectedOrder}`);
  73. this.codecPreferenceOrder[connectionType] = selectedOrder;
  74. }
  75. this.conference.on(
  76. JitsiConferenceEvents._MEDIA_SESSION_STARTED,
  77. session => this._selectPreferredCodec(session));
  78. this.conference.on(
  79. JitsiConferenceEvents.CONFERENCE_VISITOR_CODECS_CHANGED,
  80. codecList => this._updateVisitorCodecs(codecList));
  81. this.conference.on(
  82. JitsiConferenceEvents.USER_JOINED,
  83. () => this._selectPreferredCodec());
  84. this.conference.on(
  85. JitsiConferenceEvents.USER_LEFT,
  86. () => this._selectPreferredCodec());
  87. }
  88. /**
  89. * Returns a list of video codecs that are supported by the browser.
  90. *
  91. * @param {string} connectionType - media connection type, p2p or jvb.
  92. * @returns {Array}
  93. */
  94. _getSupportedVideoCodecs(connectionType) {
  95. const videoCodecMimeTypes = browser.isMobileDevice() && connectionType === 'p2p'
  96. ? MOBILE_P2P_VIDEO_CODEC_ORDER
  97. : browser.isMobileDevice() ? MOBILE_VIDEO_CODEC_ORDER : DESKTOP_VIDEO_CODEC_ORDER;
  98. if (connectionType === 'p2p' || this.options.jvb.supportsAv1) {
  99. videoCodecMimeTypes.push(CodecMimeType.AV1);
  100. }
  101. const supportedCodecs = videoCodecMimeTypes.filter(codec =>
  102. (window.RTCRtpReceiver?.getCapabilities?.(MediaType.VIDEO)?.codecs ?? [])
  103. .some(supportedCodec => supportedCodec.mimeType.toLowerCase() === `${MediaType.VIDEO}/${codec}`));
  104. // Select VP8 as the default codec if RTCRtpReceiver.getCapabilities() is not supported by the browser or if it
  105. // returns an empty set.
  106. !supportedCodecs.length && supportedCodecs.push(CodecMimeType.VP8);
  107. return supportedCodecs;
  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 isJvbSession = session === this.conference.jvbJingleSession;
  122. let localPreferredCodecOrder = isJvbSession ? this.codecPreferenceOrder.jvb : this.codecPreferenceOrder.p2p;
  123. // E2EE is curently supported only for VP8 codec.
  124. if (this.conference.isE2EEEnabled() && isJvbSession) {
  125. localPreferredCodecOrder = [ CodecMimeType.VP8 ];
  126. }
  127. const remoteParticipants = this.conference.getParticipants().map(participant => participant.getId());
  128. const remoteCodecsPerParticipant = remoteParticipants?.map(remote => {
  129. const peerMediaInfo = session._signalingLayer.getPeerMediaInfo(remote, MediaType.VIDEO);
  130. if (peerMediaInfo?.codecList) {
  131. return peerMediaInfo.codecList;
  132. } else if (peerMediaInfo?.codecType) {
  133. return [ peerMediaInfo.codecType ];
  134. }
  135. return [];
  136. });
  137. // Include the visitor codecs.
  138. this.visitorCodecs.length && remoteCodecsPerParticipant.push(this.visitorCodecs);
  139. const selectedCodecOrder = localPreferredCodecOrder.reduce((acc, localCodec) => {
  140. let codecNotSupportedByRemote = false;
  141. // Ignore remote codecs for p2p since only the JVB codec preferences are published in presence.
  142. // For p2p, we rely on the codec order present in the remote offer/answer.
  143. if (!session.isP2P) {
  144. // Remove any codecs that are not supported by any of the remote endpoints. The order of the supported
  145. // codecs locally however will remain the same since we want to support asymmetric codecs.
  146. for (const remoteCodecs of remoteCodecsPerParticipant) {
  147. // Ignore remote participants that do not publish codec preference in presence (transcriber).
  148. if (remoteCodecs.length) {
  149. codecNotSupportedByRemote = codecNotSupportedByRemote
  150. || !remoteCodecs.find(participantCodec => participantCodec === localCodec);
  151. }
  152. }
  153. }
  154. if (!codecNotSupportedByRemote) {
  155. acc.push(localCodec);
  156. }
  157. return acc;
  158. }, []);
  159. if (!selectedCodecOrder.length) {
  160. logger.warn('Invalid codec list generated because of a user joining/leaving the call');
  161. return;
  162. }
  163. // Reconfigure the codecs on the media session.
  164. if (!selectedCodecOrder.every((val, index) => val === currentCodecOrder[index])) {
  165. session.setVideoCodecs(selectedCodecOrder);
  166. }
  167. }
  168. /**
  169. * Updates the aggregate list of the codecs supported by all the visitors in the call and calculates the
  170. * selected codec if needed.
  171. * @param {Array} codecList - visitor codecs.
  172. * @returns {void}
  173. */
  174. _updateVisitorCodecs(codecList) {
  175. if (this.visitorCodecs === codecList) {
  176. return;
  177. }
  178. this.visitorCodecs = codecList;
  179. this._selectPreferredCodec();
  180. }
  181. /**
  182. * Returns the current codec preference order for the given connection type.
  183. *
  184. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  185. * @returns {Array<string>}
  186. */
  187. getCodecPreferenceList(connectionType) {
  188. return this.codecPreferenceOrder[connectionType];
  189. }
  190. }