iOS: Don’t change video rotation in FaceUp or FaceDown.

Previously, if using the device in landscape and then tilting the phone
into FaceUp orientation, the video rotation would reset to portrait.

Bug: webrtc:8492
Change-Id: I3e11e3adecabf99249ba3a8d5532291580a93f2e
Reviewed-on: https://webrtc-review.googlesource.com/24021
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20792}
diff --git a/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m b/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
index 17aa6cc..86eda60 100644
--- a/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
+++ b/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
@@ -37,6 +37,7 @@
   BOOL _isRunning;
   // Will the session be running once all asynchronous operations have been completed?
   BOOL _willBeRunning;
+  RTCVideoRotation _rotation;
 #if TARGET_OS_IPHONE
   UIDeviceOrientation _orientation;
 #endif
@@ -57,6 +58,7 @@
     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 #if TARGET_OS_IPHONE
     _orientation = UIDeviceOrientationPortrait;
+    _rotation = RTCVideoRotation_90;
     [center addObserver:self
                selector:@selector(deviceOrientationDidChange:)
                    name:UIDeviceOrientationDidChangeNotification
@@ -191,7 +193,6 @@
 
 #if TARGET_OS_IPHONE
   // Default to portrait orientation on iPhone.
-  RTCVideoRotation rotation = RTCVideoRotation_90;
   BOOL usingFrontCamera = NO;
   // Check the image's EXIF for the camera the image came from as the image could have been
   // delayed as we set alwaysDiscardsLateVideoFrames to NO.
@@ -206,16 +207,16 @@
   }
   switch (_orientation) {
     case UIDeviceOrientationPortrait:
-      rotation = RTCVideoRotation_90;
+      _rotation = RTCVideoRotation_90;
       break;
     case UIDeviceOrientationPortraitUpsideDown:
-      rotation = RTCVideoRotation_270;
+      _rotation = RTCVideoRotation_270;
       break;
     case UIDeviceOrientationLandscapeLeft:
-      rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
+      _rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
       break;
     case UIDeviceOrientationLandscapeRight:
-      rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
+      _rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
       break;
     case UIDeviceOrientationFaceUp:
     case UIDeviceOrientationFaceDown:
@@ -225,14 +226,14 @@
   }
 #else
   // No rotation on Mac.
-  RTCVideoRotation rotation = RTCVideoRotation_0;
+  _rotation = RTCVideoRotation_0;
 #endif
 
   RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer];
   int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) *
       kNanosecondsPerSecond;
   RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
-                                                           rotation:rotation
+                                                           rotation:_rotation
                                                         timeStampNs:timeStampNs];
   [self.delegate capturer:self didCaptureVideoFrame:videoFrame];
 }
diff --git a/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m b/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m
index 2b31e10..492c210a 100644
--- a/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m
+++ b/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m
@@ -90,10 +90,11 @@
     } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
       previewLayer.connection.videoOrientation =
           AVCaptureVideoOrientationLandscapeLeft;
-    } else {
+    } else if (deviceOrientation == UIInterfaceOrientationPortrait) {
       previewLayer.connection.videoOrientation =
           AVCaptureVideoOrientationPortrait;
     }
+    // If device orientation switches to FaceUp or FaceDown, don't change video orientation.
   }
 }