Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CodecSelection.js 12KB

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