Skip to content

Commit

Permalink
[Fix]Orientation issue on older devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Sep 20, 2024
1 parent 1c98771 commit 8a4cdbf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,33 @@ open class StreamDeviceOrientationAdapter: ObservableObject {
/// The default provider for device orientation based on platform.
public static let defaultProvider: Provider = {
#if canImport(UIKit)
switch UIDevice.current.orientation {
case .unknown, .portrait:
return .portrait(isUpsideDown: false)
case .portraitUpsideDown:
return .portrait(isUpsideDown: true)
case .landscapeLeft:
return .landscape(isLeft: true)
case .landscapeRight:
return .landscape(isLeft: false)
case .faceUp, .faceDown:
return .portrait(isUpsideDown: false)
@unknown default:
if let window = UIApplication.shared.connectedScenes.first as? UIWindowScene {
switch window.interfaceOrientation {
case .unknown, .portrait:
return .portrait(isUpsideDown: false)
case .portraitUpsideDown:
return .portrait(isUpsideDown: true)
case .landscapeLeft:
return .landscape(isLeft: true)
case .landscapeRight:
return .landscape(isLeft: false)
@unknown default:
return .portrait(isUpsideDown: false)
}
} else {
return .portrait(isUpsideDown: false)
}
#else
return .portrait
return .portrait(isUpsideDown: false)
#endif
}

private var provider: Provider
private var notificationCancellable: AnyCancellable?
private var __cancelable: AnyCancellable?

/// The current orientation observed by the adapter.
@Published public private(set) var orientation: StreamDeviceOrientation
@Published public private(set) var orientation: StreamDeviceOrientation = .portrait(isUpsideDown: false)

/// Initializes an adapter for observing device orientation changes.
/// - Parameters:
Expand All @@ -87,9 +90,9 @@ open class StreamDeviceOrientationAdapter: ObservableObject {
_ provider: @escaping Provider = StreamDeviceOrientationAdapter.defaultProvider
) {
self.provider = provider
orientation = provider()

#if canImport(UIKit)
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
// Subscribe to orientation change notifications on UIKit platforms.
notificationCancellable = notificationCenter
.publisher(for: UIDevice.orientationDidChangeNotification)
Expand All @@ -99,11 +102,18 @@ open class StreamDeviceOrientationAdapter: ObservableObject {
self.orientation = provider() // Update orientation based on the provider.
}
#endif

Task { @MainActor in
orientation = provider()
}
}

/// Cleans up resources when the adapter is deallocated.
deinit {
notificationCancellable?.cancel() // Cancel notification subscription.
#if canImport(UIKit)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ final class StreamVideoCaptureHandler: NSObject, RTCVideoCapturerDelegate {
case let .landscape(isLeft):
switch (isLeft, currentCameraPosition == .front) {
case (true, true):
rotation = ._180
case (true, false):
rotation = ._0
case (true, false):
rotation = ._180
case (false, true):
rotation = ._0
case (false, false):
rotation = ._180
case (false, false):
rotation = ._0
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ final class StreamVideoCaptureHandler_Tests: XCTestCase {
try await assertFrameOrientation(
deviceOrientation: .landscape(isLeft: true),
cameraPosition: .front,
expected: ._180
expected: ._0
)
}

func test_didCapture_orientationLandscapeRightCameraFront_frameHasExpectedOrientation() async throws {
try await assertFrameOrientation(
deviceOrientation: .landscape(isLeft: false),
cameraPosition: .front,
expected: ._0
expected: ._180
)
}

Expand All @@ -81,15 +81,15 @@ final class StreamVideoCaptureHandler_Tests: XCTestCase {
try await assertFrameOrientation(
deviceOrientation: .landscape(isLeft: true),
cameraPosition: .back,
expected: ._0
expected: ._180
)
}

func test_didCapture_orientationLandscapeRightCameraBack_frameHasExpectedOrientation() async throws {
try await assertFrameOrientation(
deviceOrientation: .landscape(isLeft: false),
cameraPosition: .back,
expected: ._180
expected: ._0
)
}

Expand Down

0 comments on commit 8a4cdbf

Please sign in to comment.