Skip to content

Commit

Permalink
Do a bunch of weird rotations...
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jun 4, 2024
1 parent 4fb112e commit 8851f83
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
9 changes: 7 additions & 2 deletions package/ios/Core/CameraSession+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,13 @@ extension CameraSession {
// 2. Configure
videoOutput.setSampleBufferDelegate(self, queue: CameraQueues.videoQueue)
videoOutput.alwaysDiscardsLateVideoFrames = true
videoOutput.orientation = videoOrientation
videoOutput.isMirrored = isMirrored
if isMirrored {
// rotate by 180 deg and mirror the selfie camera so it behaves like the back camera.
let before = videoOutput.orientation
let after = videoOutput.orientation.reversed()
videoOutput.orientation = videoOutput.orientation.reversed()
videoOutput.isMirrored = isMirrored
}

self.videoOutput = videoOutput
}
Expand Down
18 changes: 4 additions & 14 deletions package/ios/Core/CameraSession+Video.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,11 @@ extension CameraSession {
}

private func getVideoRecordingOrientation() throws -> Orientation {
guard let videoDeviceInput = videoDeviceInput else {
throw CameraError.session(.cameraNotReady)
var orientation = outputOrientation
if isMirrored && !orientation.isLandscape {
// If the video is mirrored and rotated, we need to counter-rotate by 180° because we applied that translation when creating the output.
orientation = orientation.reversed()
}

// Video stream is already rotated against sensor orientation so we can properly mirror.
// counter that rotation again for the AVAssetWriter transforms
let sensorOrientation = videoDeviceInput.device.sensorOrientation
var outputOrientation = self.outputOrientation
if isMirrored {
// mirrored streams are reversed.
outputOrientation = outputOrientation.reversed()
}
let orientation = outputOrientation.rotateBy(orientation: sensorOrientation.reversed())
VisionLogger.log(level: .info, message: "Sensor Orientation is \(sensorOrientation), output orientation is \(outputOrientation)." +
"Video will be \(orientation)...")
return orientation
}

Expand Down
4 changes: 4 additions & 0 deletions package/ios/Core/Types/Orientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ enum Orientation: String, JSUnionValue {
}
}

var isLandscape: Bool {
return self == .landscapeLeft || self == .landscapeRight
}

func rotateBy(orientation: Orientation) -> Orientation {
let added = degrees + orientation.degrees
let degress = added.truncatingRemainder(dividingBy: 360)
Expand Down

0 comments on commit 8851f83

Please sign in to comment.