Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit scaled frame to source frame dimensions #513

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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