From 19d77c1bbb97cde837288fe2e33a95c4de5edb0a Mon Sep 17 00:00:00 2001 From: "Gustavo Garcia gustavo@lifeonair.com" Date: Wed, 15 Nov 2017 16:03:18 +0200 Subject: [PATCH] Fix support orientation changes in Camera Preview Bug: webrtc:8532 Change-Id: I2a3cc6974af711f1be6a55a6a1e04e7189175bd3 Reviewed-on: https://webrtc-review.googlesource.com/23220 Reviewed-by: Anders Carlsson Reviewed-by: Magnus Jedvert Commit-Queue: Anders Carlsson Cr-Commit-Position: refs/heads/master@{#20702} --- .../ios/ARDVideoCallViewController.m | 4 ++ examples/objc/AppRTCMobile/ios/Info.plist | 3 ++ .../Classes/UI/RTCCameraPreviewView.m | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/examples/objc/AppRTCMobile/ios/ARDVideoCallViewController.m b/examples/objc/AppRTCMobile/ios/ARDVideoCallViewController.m index ad5bc80d4b..3deb067a68 100644 --- a/examples/objc/AppRTCMobile/ios/ARDVideoCallViewController.m +++ b/examples/objc/AppRTCMobile/ios/ARDVideoCallViewController.m @@ -63,6 +63,10 @@ [session addDelegate:self]; } +- (UIInterfaceOrientationMask)supportedInterfaceOrientations { + return UIInterfaceOrientationMaskAll; +} + #pragma mark - ARDAppClientDelegate - (void)appClient:(ARDAppClient *)client diff --git a/examples/objc/AppRTCMobile/ios/Info.plist b/examples/objc/AppRTCMobile/ios/Info.plist index 90d6bb401e..a2f0a683ed 100644 --- a/examples/objc/AppRTCMobile/ios/Info.plist +++ b/examples/objc/AppRTCMobile/ios/Info.plist @@ -53,6 +53,9 @@ UISupportedInterfaceOrientations UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + UIInterfaceOrientationPortraitUpsideDown UIAppFonts diff --git a/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m b/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m index 8af3384e7f..2b31e107a8 100644 --- a/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m +++ b/sdk/objc/Framework/Classes/UI/RTCCameraPreviewView.m @@ -23,6 +23,26 @@ return [AVCaptureVideoPreviewLayer class]; } +- (instancetype)initWithFrame:(CGRect)aRect { + self = [super initWithFrame:aRect]; + if (self) { + [self addOrientationObserver]; + } + return self; +} + +- (instancetype)initWithCoder:(NSCoder*)aDecoder { + self = [super initWithCoder:aDecoder]; + if (self) { + [self addOrientationObserver]; + } + return self; +} + +- (void)dealloc { + [self removeOrientationObserver]; +} + - (void)setCaptureSession:(AVCaptureSession *)captureSession { if (_captureSession == captureSession) { return; @@ -34,6 +54,10 @@ [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession block:^{ previewLayer.session = captureSession; + [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain + block:^{ + [self setCorrectVideoOrientation]; + }]; }]; }]; } @@ -45,6 +69,10 @@ [self setCorrectVideoOrientation]; } +-(void)orientationChanged:(NSNotification *)notification { + [self setCorrectVideoOrientation]; +} + - (void)setCorrectVideoOrientation { // Get current device orientation. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; @@ -71,6 +99,19 @@ #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; }