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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 added complexity on mobile
  62. // devices). Currently, VP9 encode is supported on Chrome and on Safari (only for p2p).
  63. const isVp9EncodeSupported = browser.supportsVP9() || (browser.isWebKitBased() && connectionType === 'p2p');
  64. if (!isVp9EncodeSupported || this.conference.isE2EEEnabled()) {
  65. const index = selectedOrder.findIndex(codec => codec === CodecMimeType.VP9);
  66. if (index !== -1) {
  67. selectedOrder.splice(index, 1);
  68. // Remove VP9 from the list when E2EE is enabled since it is not supported.
  69. // TODO - remove this check when support for VP9-E2EE is introduced.
  70. if (!this.conference.isE2EEEnabled()) {
  71. selectedOrder.push(CodecMimeType.VP9);
  72. }
  73. }
  74. }
  75. logger.info(`Codec preference order for ${connectionType} connection is ${selectedOrder}`);
  76. this.codecPreferenceOrder[connectionType] = selectedOrder;
  77. // Set the preferred screenshare codec.
  78. if (screenshareCodec && supportedCodecs.has(screenshareCodec.toLowerCase())) {
  79. this.screenshareCodec[connectionType] = screenshareCodec.toLowerCase();
  80. }
  81. }
  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. const supportedCodecs = videoCodecMimeTypes.filter(codec =>
  94. (window.RTCRtpReceiver?.getCapabilities?.(MediaType.VIDEO)?.codecs ?? [])
  95. .some(supportedCodec => supportedCodec.mimeType.toLowerCase() === `${MediaType.VIDEO}/${codec}`));
  96. // Select VP8 as the default codec if RTCRtpReceiver.getCapabilities() is not supported by the browser or if it
  97. // returns an empty set.
  98. !supportedCodecs.length && supportedCodecs.push(CodecMimeType.VP8);
  99. return supportedCodecs;
  100. }
  101. /**
  102. * Returns the current codec preference order for the given connection type.
  103. *
  104. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  105. * @returns {Array<string>}
  106. */
  107. getCodecPreferenceList(connectionType) {
  108. return this.codecPreferenceOrder[connectionType];
  109. }
  110. /**
  111. * Returns the preferred screenshare codec for the given connection type.
  112. *
  113. * @param {String} connectionType The media connection type, 'p2p' or 'jvb'.
  114. * @returns CodecMimeType
  115. */
  116. getScreenshareCodec(connectionType) {
  117. return this.screenshareCodec[connectionType];
  118. }
  119. /**
  120. * Sets the codec on the media session based on the codec preference order configured in config.js and the supported
  121. * codecs published by the remote participants in their presence.
  122. *
  123. * @param {JingleSessionPC} mediaSession session for which the codec selection has to be made.
  124. */
  125. selectPreferredCodec(mediaSession) {
  126. const session = mediaSession ? mediaSession : this.conference.jvbJingleSession;
  127. if (!session) {
  128. return;
  129. }
  130. let localPreferredCodecOrder = this.codecPreferenceOrder.jvb;
  131. // E2EE is curently supported only for VP8 codec.
  132. if (this.conference.isE2EEEnabled()) {
  133. localPreferredCodecOrder = [ CodecMimeType.VP8 ];
  134. }
  135. const remoteParticipants = this.conference.getParticipants().map(participant => participant.getId());
  136. const remoteCodecsPerParticipant = remoteParticipants?.map(remote => {
  137. const peerMediaInfo = session._signalingLayer.getPeerMediaInfo(remote, MediaType.VIDEO);
  138. if (peerMediaInfo?.codecList) {
  139. return peerMediaInfo.codecList;
  140. } else if (peerMediaInfo?.codecType) {
  141. return [ peerMediaInfo.codecType ];
  142. }
  143. return [];
  144. });
  145. // Include the visitor codecs.
  146. this.visitorCodecs.length && remoteCodecsPerParticipant.push(this.visitorCodecs);
  147. const selectedCodecOrder = localPreferredCodecOrder.reduce((acc, localCodec) => {
  148. let codecNotSupportedByRemote = false;
  149. // Remove any codecs that are not supported by any of the remote endpoints. The order of the supported
  150. // codecs locally however will remain the same since we want to support asymmetric codecs.
  151. for (const remoteCodecs of remoteCodecsPerParticipant) {
  152. // Ignore remote participants that do not publish codec preference in presence (transcriber).
  153. if (remoteCodecs.length) {
  154. codecNotSupportedByRemote = codecNotSupportedByRemote
  155. || !remoteCodecs.find(participantCodec => participantCodec === localCodec);
  156. }
  157. }
  158. if (!codecNotSupportedByRemote) {
  159. acc.push(localCodec);
  160. }
  161. return acc;
  162. }, []);
  163. if (!selectedCodecOrder.length) {
  164. logger.warn('Invalid codec list generated because of a user joining/leaving the call');
  165. return;
  166. }
  167. session.setVideoCodecs(selectedCodecOrder, this.screenshareCodec?.jvb);
  168. }
  169. /**
  170. * Changes the codec preference order.
  171. *
  172. * @param {JitsiLocalTrack} localTrack - The local video track.
  173. * @param {CodecMimeType} codec - The codec used for encoding the given local video track.
  174. * @returns boolean - Returns true if the codec order has been updated, false otherwise.
  175. */
  176. changeCodecPreferenceOrder(localTrack, codec) {
  177. const session = this.conference.getActiveMediaSession();
  178. const connectionType = session.isP2P ? 'p2p' : 'jvb';
  179. const codecOrder = this.codecPreferenceOrder[connectionType];
  180. const videoType = localTrack.getVideoType();
  181. const codecsByVideoType = VIDEO_CODECS_BY_COMPLEXITY[videoType]
  182. .filter(val => Boolean(codecOrder.find(supportedCodec => supportedCodec === val)));
  183. const codecIndex = codecsByVideoType.findIndex(val => val === codec.toLowerCase());
  184. // Do nothing if we are using the lowest complexity codec already.
  185. if (codecIndex === codecsByVideoType.length - 1) {
  186. return false;
  187. }
  188. const newCodec = codecsByVideoType[codecIndex + 1];
  189. if (videoType === VideoType.CAMERA) {
  190. const idx = codecOrder.findIndex(val => val === newCodec);
  191. codecOrder.splice(idx, 1);
  192. codecOrder.unshift(newCodec);
  193. logger.info(`QualityController - switching camera codec to ${newCodec} because of cpu restriction`);
  194. } else {
  195. this.screenshareCodec[connectionType] = newCodec;
  196. logger.info(`QualityController - switching screenshare codec to ${newCodec} because of cpu restriction`);
  197. }
  198. this.selectPreferredCodec(session);
  199. return true;
  200. }
  201. /**
  202. * Updates the aggregate list of the codecs supported by all the visitors in the call and calculates the
  203. * selected codec if needed.
  204. * @param {Array} codecList - visitor codecs.
  205. * @returns {void}
  206. */
  207. updateVisitorCodecs(codecList) {
  208. if (this.visitorCodecs === codecList) {
  209. return;
  210. }
  211. this.visitorCodecs = codecList;
  212. this.selectPreferredCodec();
  213. }
  214. }