Skip to content

Commit

Permalink
Limit scaled frame to source frame dimensions (#513)
Browse files Browse the repository at this point in the history
Add logic to prevent scaling up the output frames.
Scaling up the video result frames beyond the resolution of the
device-provided source frames doesn’t make sense.

For example, if the device supports a maximum resolution of 1552x1552,
specifying 3840x2160 will result in an output of 1552x873 (while
maintaining the aspect ratio).
  • Loading branch information
hiroshihorie authored Nov 25, 2024
1 parent 05fa78c commit 1804bad
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Sources/LiveKit/Track/Capturers/CameraCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,27 @@ extension LKRTCVideoFrame {
targetWidth: Int32,
targetHeight: Int32
) -> LKRTCVideoFrame? {
// Ensure target dimensions don't exceed source dimensions
let scaleWidth: Int32
let scaleHeight: Int32

if targetWidth > width || targetHeight > height {
// Calculate scale factor to fit within source dimensions
let widthScale = Double(targetWidth) / Double(width) // Scale down factor
let heightScale = Double(targetHeight) / Double(height)
let scale = max(widthScale, heightScale)

// Apply scale to target dimensions
scaleWidth = Int32(Double(targetWidth) / scale)
scaleHeight = Int32(Double(targetHeight) / scale)
} else {
scaleWidth = targetWidth
scaleHeight = targetHeight
}

// Calculate aspect ratios
let sourceRatio = Double(width) / Double(height)
let targetRatio = Double(targetWidth) / Double(targetHeight)
let targetRatio = Double(scaleWidth) / Double(scaleHeight)

// Calculate crop dimensions
let (cropWidth, cropHeight): (Int32, Int32)
Expand All @@ -379,8 +397,8 @@ extension LKRTCVideoFrame {
offsetY: offsetY,
cropWidth: cropWidth,
cropHeight: cropHeight,
scaleWidth: targetWidth,
scaleHeight: targetHeight
scaleWidth: scaleWidth,
scaleHeight: scaleHeight
) else { return nil }

return LKRTCVideoFrame(buffer: newBuffer, rotation: rotation, timeStampNs: timeStampNs)
Expand Down

0 comments on commit 1804bad

Please sign in to comment.