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.

CodecSelection.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { getLogger } from '@jitsi/logger';
  2. import { CodecMimeType } from '../../service/RTC/CodecMimeType';
  3. import { MediaType } from '../../service/RTC/MediaType';
  4. import { VIDEO_CODECS_BY_COMPLEXITY } from '../../service/RTC/StandardVideoQualitySettings';
  5. import { VideoType } from '../../service/RTC/VideoType';
  6. import browser from '../browser';
  7. const logger = getLogger(__filename);
  8. // Default video codec preferences on mobile and desktop endpoints.
  9. const DESKTOP_VIDEO_CODEC_ORDER = [ CodecMimeType.VP9, CodecMimeType.VP8, CodecMimeType.H264, CodecMimeType.AV1 ];
  10. const MOBILE_P2P_VIDEO_CODEC_ORDER = [ CodecMimeType.H264, CodecMimeType.VP8, CodecMimeType.VP9, CodecMimeType.AV1 ];
  11. const MOBILE_VIDEO_CODEC_ORDER = [ CodecMimeType.VP8, CodecMimeType.VP9, CodecMimeType.H264, CodecMimeType.AV1 ];
  12. /**
  13. * This class handles the codec selection mechanism for the conference based on the config.js settings.
  14. * The preferred codec is selected based on the settings and the list of codecs supported by the browser.
  15. * The preferred codec is published in presence which is then used by the other endpoints in the
  16. * conference to pick a supported codec at join time and when the call transitions between p2p and jvb
  17. * connections.
  18. */
  19. export class CodecSelection {
  20. /**
  21. * Creates a new instance for a given conference.
  22. *
  23. * @param {JitsiConference} conference the conference instance
  24. * @param {*} options
  25. * @param {string} options.jvb settings (codec list, preferred and disabled) for the jvb connection.
  26. * @param {string} options.p2p settings (codec list, preferred and disabled) for the p2p connection.
  27. */
  28. constructor(conference, options) {
  29. this.codecPreferenceOrder = {};
  30. this.conference = conference;
  31. this.encodeTimeStats = new Map();
  32. this.options = options;
  33. this.screenshareCodec = {};
  34. this.visitorCodecs = [];
  35. for (const connectionType of Object.keys(options)) {
  36. // eslint-disable-next-line prefer-const
  37. let { disabledCodec, preferredCodec, preferenceOrder, screenshareCodec } = options[connectionType];
  38. const supportedCodecs = new Set(this._getSupportedVideoCodecs(connectionType));
  39. // Default preference codec order when no codec preferences are set in config.js
  40. let selectedOrder = Array.from(supportedCodecs);
  41. if (preferenceOrder) {
  42. preferenceOrder = preferenceOrder.map(codec => codec.toLowerCase());
  43. // Select all codecs that are supported by the browser.
  44. selectedOrder = preferenceOrder.filter(codec => supportedCodecs.has(codec));
  45. // Generate the codec list based on the supported codecs and the preferred/disabled (deprecated) settings
  46. } else if (preferredCodec || disabledCodec) {
  47. disabledCodec = disabledCodec?.toLowerCase();
  48. preferredCodec = preferredCodec?.toLowerCase();
  49. // VP8 cannot be disabled since it the default codec.
  50. if (disabledCodec && disabledCodec !== CodecMimeType.VP8) {
  51. selectedOrder = selectedOrder.filter(codec => codec !== disabledCodec);
  52. }
  53. const index = selectedOrder.findIndex(codec => codec === preferredCodec);
  54. // Move the preferred codec to the top of the list.
  55. if (preferredCodec && index !== -1) {
  56. selectedOrder.splice(index, 1);
  57. selectedOrder.unshift(preferredCodec);
  58. }
  59. }
  60. // Push VP9 to the end of the list so that the client continues to decode VP9 even if its not
  61. // preferable to encode VP9 (because of browser bugs on the encoding side or other reasons).
  62. const isVp9EncodeSupported = browser.supportsVP9() || (browser.isWebKitBased() && connectionType === 'p2p');
  63. if (!isVp9EncodeSupported) {
  64. const index = selectedOrder.findIndex(codec => codec === CodecMimeType.VP9);
  65. if (index !== -1) {
  66. selectedOrder.splice(index, 1);
  67. // Remove VP9 from the list when E2EE is enabled since it is not supported.
  68. // TODO - remove this check when support for VP9-E2EE is introduced.
  69. if (!this.conference.isE2EEEnabled()) {
  70. selectedOrder.push(CodecMimeType.VP9);
  71. }
  72. }
  73. }
  74. logger.info(`Codec preference order for ${connectionType} connection is ${selectedOrder}`);
  75. this.codecPreferenceOrder[connectionType] = selectedOrder;
  76. // Set the preferred screenshare codec.
  77. if (screenshareCodec && supportedCodecs.has(screenshareCodec.toLowerCase())) {
  78. this.screenshareCodec[connectionType] = screenshareCodec.toLowerCase();
  79. }
  80. }
  81. }
  82. /**
  83. * Returns a list of video codecs that are supported by the browser.
  84. *
  85. * @param {string} connectionType - media connection type, p2p or jvb.
  86. * @returns {Array}
  87. */
  88. _getSupportedVideoCodecs(connectionType) {
  89. const videoCodecMimeTypes = browser.isMobileDevice() && connectionType === 'p2p'
  90. ? MOBILE_P2P_VIDEO_CODEC_ORDER
  91. : browser.isMobileDevice() ? MOBILE_VIDEO_CODEC_ORDER : DESKTOP_VIDEO_CODEC_ORDER;
  92. const supportedCodecs = videoCodecMimeTypes.filter(codec =>
  93. (window.RTCRtpReceiver?.getCapabilities?.(MediaType.VIDEO)?.codecs ?? [])
  94. .some(supportedCodec => supportedCodec.mimeType.toLowerCase() === `${MediaType.VIDEO}/${codec}`));
  95. // Select VP8 as the default codec if RTCRtpReceiver.getCapabilities() is not supported by the browser or if it
  96. // returns an empty set.
  97. !supportedCodecs.length && supportedCodecs.push(CodecMimeType.VP8);
  98. return supportedCodecs;
  99. }
  100. /**
  101. * Returns the current codec preference order for the given connection type.
  102. *
  103. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  104. * @returns {Array<string>}
  105. */
  106. getCodecPreferenceList(connectionType) {
  107. return this.codecPreferenceOrder[connectionType];
  108. }
  109. /**
  110. * Returns the preferred screenshare codec for the given connection type.
  111. *
  112. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  113. * @returns CodecMimeType
  114. */
  115. getScreenshareCodec(connectionType) {
  116. return this.screenshareCodec[connectionType];
  117. }
  118. /**
  119. * Sets the codec on the media session based on the codec preference order configured in config.js and the supported
  120. * codecs published by the remote participants in their presence.
  121. *
  122. * @param {JingleSessionPC} mediaSession session for which the codec selection has to be made.
  123. */
  124. selectPreferredCodec(mediaSession) {
  125. const session = mediaSession ? mediaSession : this.conference.jvbJingleSession;
  126. if (!session) {
  127. return;
  128. }
  129. let localPreferredCodecOrder = this.codecPreferenceOrder.jvb;
  130. // E2EE is curently supported only for VP8 codec.
  131. if (this.conference.isE2EEEnabled()) {
  132. localPreferredCodecOrder = [ CodecMimeType.VP8 ];
  133. }
  134. const remoteParticipants = this.conference.getParticipants().map(participant => participant.getId());
  135. const remoteCodecsPerParticipant = remoteParticipants?.map(remote => {
  136. const peerMediaInfo = session._signalingLayer.getPeerMediaInfo(remote, MediaType.VIDEO);
  137. if (peerMediaInfo?.codecList) {
  138. return peerMediaInfo.codecList;
  139. } else if (peerMediaInfo?.codecType) {
  140. return [ peerMediaInfo.codecType ];
  141. }
  142. return [];
  143. });
  144. // Include the visitor codecs.
  145. this.visitorCodecs.length && remoteCodecsPerParticipant.push(this.visitorCodecs);
  146. const selectedCodecOrder = localPreferredCodecOrder.reduce((acc, localCodec) => {
  147. let codecNotSupportedByRemote = false;
  148. // Remove any codecs that are not supported by any of the remote endpoints. The order of the supported
  149. // codecs locally however will remain the same since we want to support asymmetric codecs.
  150. for (const remoteCodecs of remoteCodecsPerParticipant) {
  151. // Ignore remote participants that do not publish codec preference in presence (transcriber).
  152. if (remoteCodecs.length) {
  153. codecNotSupportedByRemote = codecNotSupportedByRemote
  154. || !remoteCodecs.find(participantCodec => participantCodec === localCodec);
  155. }
  156. }
  157. if (!codecNotSupportedByRemote) {
  158. acc.push(localCodec);
  159. }
  160. return acc;
  161. }, []);
  162. if (!selectedCodecOrder.length) {
  163. logger.warn('Invalid codec list generated because of a user joining/leaving the call');
  164. return;
  165. }
  166. session.setVideoCodecs(selectedCodecOrder, this.screenshareCodec?.jvb);
  167. }
  168. /**
  169. * Changes the codec preference order.
  170. *
  171. * @param {JitsiLocalTrack} localTrack - The local video track.
  172. * @param {CodecMimeType} codec - The codec used for encoding the given local video track.
  173. * @returns boolean - Returns true if the codec order has been updated, false otherwise.
  174. */
  175. changeCodecPreferenceOrder(localTrack, codec) {
  176. const session = this.conference.getActiveMediaSession();
  177. const connectionType = session.isP2P ? 'p2p' : 'jvb';
  178. const codecOrder = this.codecPreferenceOrder[connectionType];
  179. const videoType = localTrack.getVideoType();
  180. const codecsByVideoType = VIDEO_CODECS_BY_COMPLEXITY[videoType]
  181. .filter(val => Boolean(codecOrder.find(supportedCodec => supportedCodec === val)));
  182. const codecIndex = codecsByVideoType.findIndex(val => val === codec.toLowerCase());
  183. // Do nothing if we are using the lowest complexity codec already.
  184. if (codecIndex === codecsByVideoType.length - 1) {
  185. return false;
  186. }
  187. const newCodec = codecsByVideoType[codecIndex + 1];
  188. if (videoType === VideoType.CAMERA) {
  189. const idx = codecOrder.findIndex(val => val === newCodec);
  190. codecOrder.splice(idx, 1);
  191. codecOrder.unshift(newCodec);
  192. logger.info(`QualityController - switching camera codec to ${newCodec} because of cpu restriction`);
  193. } else {
  194. this.screenshareCodec[connectionType] = newCodec;
  195. logger.info(`QualityController - switching screenshare codec to ${newCodec} because of cpu restriction`);
  196. }
  197. this.selectPreferredCodec(session);
  198. return true;
  199. }
  200. /**
  201. * Updates the aggregate list of the codecs supported by all the visitors in the call and calculates the
  202. * selected codec if needed.
  203. * @param {Array} codecList - visitor codecs.
  204. * @returns {void}
  205. */
  206. updateVisitorCodecs(codecList) {
  207. if (this.visitorCodecs === codecList) {
  208. return;
  209. }
  210. this.visitorCodecs = codecList;
  211. this.selectPreferredCodec();
  212. }
  213. }