Updated iOS video capturer to take device orientation into consideration.

BUG=4122
R=tkchin@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/48769004

Patch from Jonas Martinsson <jonas.d.martinsson@gmail.com>.

Cr-Commit-Position: refs/heads/master@{#8953}
diff --git a/webrtc/modules/video_capture/ios/rtc_video_capture_ios_objc.mm b/webrtc/modules/video_capture/ios/rtc_video_capture_ios_objc.mm
index f6302f1..71ccc98 100644
--- a/webrtc/modules/video_capture/ios/rtc_video_capture_ios_objc.mm
+++ b/webrtc/modules/video_capture/ios/rtc_video_capture_ios_objc.mm
@@ -31,6 +31,7 @@
   webrtc::VideoCaptureCapability _capability;
   AVCaptureSession* _captureSession;
   int _captureId;
+  BOOL _orientationHasChanged;
   AVCaptureConnection* _connection;
   BOOL _captureChanging;  // Guarded by _captureChangingCondition.
   NSCondition* _captureChangingCondition;
@@ -80,14 +81,16 @@
                    __LINE__);
     }
 
+    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
+
     NSNotificationCenter* notify = [NSNotificationCenter defaultCenter];
     [notify addObserver:self
                selector:@selector(onVideoError:)
                    name:AVCaptureSessionRuntimeErrorNotification
                  object:_captureSession];
     [notify addObserver:self
-               selector:@selector(statusBarOrientationDidChange:)
-                   name:@"StatusBarOrientationDidChange"
+               selector:@selector(deviceOrientationDidChange:)
+                   name:UIDeviceOrientationDidChangeNotification
                  object:nil];
   }
 
@@ -105,7 +108,8 @@
   [[self currentOutput] setSampleBufferDelegate:nil queue:NULL];
 }
 
-- (void)statusBarOrientationDidChange:(NSNotification*)notification {
+- (void)deviceOrientationDidChange:(NSNotification*)notification {
+  _orientationHasChanged = YES;
   [self setRelativeVideoOrientation];
 }
 
@@ -171,6 +175,7 @@
 
   [self directOutputToSelf];
 
+  _orientationHasChanged = NO;
   _captureChanging = YES;
   dispatch_async(
       dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
@@ -238,24 +243,33 @@
 }
 
 - (void)setRelativeVideoOrientation {
-  if (!_connection.supportsVideoOrientation)
+  if (!_connection.supportsVideoOrientation) {
     return;
-  switch ([UIApplication sharedApplication].statusBarOrientation) {
-    case UIInterfaceOrientationPortrait:
-#if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
-    case UIInterfaceOrientationUnknown:
-#endif
-      _connection.videoOrientation = AVCaptureVideoOrientationPortrait;
-      break;
-    case UIInterfaceOrientationPortraitUpsideDown:
+  }
+
+  switch ([UIDevice currentDevice].orientation) {
+    case UIDeviceOrientationPortrait:
       _connection.videoOrientation =
           AVCaptureVideoOrientationPortraitUpsideDown;
+    case UIDeviceOrientationPortraitUpsideDown:
+      _connection.videoOrientation =
+          AVCaptureVideoOrientationPortrait;
       break;
-    case UIInterfaceOrientationLandscapeLeft:
-      _connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
+    case UIDeviceOrientationLandscapeLeft:
+      _connection.videoOrientation =
+          AVCaptureVideoOrientationLandscapeRight;
       break;
-    case UIInterfaceOrientationLandscapeRight:
-      _connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
+    case UIDeviceOrientationLandscapeRight:
+      _connection.videoOrientation =
+          AVCaptureVideoOrientationLandscapeLeft;
+      break;
+    case UIDeviceOrientationFaceUp:
+    case UIDeviceOrientationFaceDown:
+    case UIDeviceOrientationUnknown:
+      if (!_orientationHasChanged) {
+        _connection.videoOrientation =
+            AVCaptureVideoOrientationPortrait;
+      }
       break;
   }
 }
@@ -273,6 +287,8 @@
 }
 
 - (BOOL)stopCapture {
+  [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
+  _orientationHasChanged = NO;
   [self waitForCaptureChangeToFinish];
   [self directOutputToNil];