[iOS] Update RTCCameraPreviewView to conform to iOS 17

In iOS 17, the video orientation API is deprecated. This CL updates the
RTCCameraPreviewView file to use the video rotation angle API instead.

This file is being updated now to support Chrome for iOS effort to raise
its minimum deployment target to iOS 17.

In addition, this CL is a follow-up to the linked reverted CL that
fixes the incorrect build guard that gated the deprecated code.

Change-Id: I82feadbdca227259856948f0b683478ec52535cb
Bug: chromium:348649285
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/391240
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#44618}
diff --git a/sdk/objc/helpers/RTCCameraPreviewView.m b/sdk/objc/helpers/RTCCameraPreviewView.m
index 92c3cd2..0ebdcb5 100644
--- a/sdk/objc/helpers/RTCCameraPreviewView.m
+++ b/sdk/objc/helpers/RTCCameraPreviewView.m
@@ -80,6 +80,54 @@
 }
 
 - (void)setCorrectVideoOrientation {
+  if (@available(iOS 17, *)) {
+    [self modifyVideoAngle];
+    return;
+  }
+
+  [self modifyVideoOrientation];
+}
+
+#pragma mark - Private
+
+- (void)addOrientationObserver {
+  [[NSNotificationCenter defaultCenter]
+      addObserver:self
+         selector:@selector(orientationChanged:)
+             name:UIDeviceOrientationDidChangeNotification
+           object:nil];
+}
+
+- (void)removeOrientationObserver {
+  [[NSNotificationCenter defaultCenter]
+      removeObserver:self
+                name:UIDeviceOrientationDidChangeNotification
+              object:nil];
+}
+
+- (AVCaptureVideoPreviewLayer *)previewLayer {
+  return (AVCaptureVideoPreviewLayer *)self.layer;
+}
+
+- (void)modifyVideoAngle API_AVAILABLE(ios(17.0)) {
+  AVCaptureDeviceInput* captureSessionInput =
+      _captureSession.inputs.firstObject;
+  AVCaptureDevice* camera = captureSessionInput.device;
+  AVCaptureVideoPreviewLayer* previewLayer = [self previewLayer];
+  AVCaptureConnection* videoConnection = previewLayer.connection;
+  AVCaptureDeviceRotationCoordinator* rotationCoordiantor =
+      [[AVCaptureDeviceRotationCoordinator alloc]
+          initWithDevice:camera
+            previewLayer:previewLayer];
+  CGFloat angle =
+      rotationCoordiantor.videoRotationAngleForHorizonLevelCapture;
+  if ([videoConnection isVideoRotationAngleSupported:angle]) {
+    [videoConnection setVideoRotationAngle:angle];
+  }
+}
+
+#if !defined(__IPHONE_17_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_17_0
+- (void)modifyVideoOrientation {
   // Get current device orientation.
   UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
   AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
@@ -104,26 +152,6 @@
     // orientation.
   }
 }
+#endif
 
-#pragma mark - Private
-
-- (void)addOrientationObserver {
-  [[NSNotificationCenter defaultCenter]
-      addObserver:self
-         selector:@selector(orientationChanged:)
-             name:UIDeviceOrientationDidChangeNotification
-           object:nil];
-}
-
-- (void)removeOrientationObserver {
-  [[NSNotificationCenter defaultCenter]
-      removeObserver:self
-                name:UIDeviceOrientationDidChangeNotification
-              object:nil];
-}
-
-- (AVCaptureVideoPreviewLayer *)previewLayer {
-  return (AVCaptureVideoPreviewLayer *)self.layer;
-}
-
-@end
+@end
\ No newline at end of file