1const MAX_SURFACE_DIMENSION = 8192;23function positiveFinite(value, name) {4 if (!Number.isFinite(value) || value <= 0) throw new RangeError(`${name} must be positive`);5 return value;6}78function roundedSurface(width, height) {9 return Object.freeze({10 width: Math.round(Math.min(MAX_SURFACE_DIMENSION, width)),11 height: Math.round(Math.min(MAX_SURFACE_DIMENSION, height)),12 });13}1415export function fitStreamSurface({ frameWidth, frameHeight, availableWidth, availableHeight }) {16 const width = positiveFinite(frameWidth, 'frame width');17 const height = positiveFinite(frameHeight, 'frame height');18 const widthLimit = positiveFinite(availableWidth, 'available surface width');19 const heightLimit = positiveFinite(availableHeight, 'available surface height');20 const fittedWidth = Math.min(widthLimit, heightLimit * (width / height));21 return roundedSurface(fittedWidth, fittedWidth / (width / height));22}