haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "WebRTC/RTCCameraPreviewView.h" |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 12 | |
| 13 | #import <AVFoundation/AVFoundation.h> |
meetAkshay99 | 0d335c7 | 2017-04-18 07:12:05 -0700 | [diff] [blame] | 14 | #import <UIKit/UIKit.h> |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 15 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 16 | #import "RTCDispatcher+Private.h" |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 17 | |
| 18 | @implementation RTCCameraPreviewView |
| 19 | |
| 20 | @synthesize captureSession = _captureSession; |
| 21 | |
| 22 | + (Class)layerClass { |
| 23 | return [AVCaptureVideoPreviewLayer class]; |
| 24 | } |
| 25 | |
Gustavo Garcia gustavo@lifeonair.com | 19d77c1 | 2017-11-15 16:03:18 +0200 | [diff] [blame^] | 26 | - (instancetype)initWithFrame:(CGRect)aRect { |
| 27 | self = [super initWithFrame:aRect]; |
| 28 | if (self) { |
| 29 | [self addOrientationObserver]; |
| 30 | } |
| 31 | return self; |
| 32 | } |
| 33 | |
| 34 | - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 35 | self = [super initWithCoder:aDecoder]; |
| 36 | if (self) { |
| 37 | [self addOrientationObserver]; |
| 38 | } |
| 39 | return self; |
| 40 | } |
| 41 | |
| 42 | - (void)dealloc { |
| 43 | [self removeOrientationObserver]; |
| 44 | } |
| 45 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 46 | - (void)setCaptureSession:(AVCaptureSession *)captureSession { |
| 47 | if (_captureSession == captureSession) { |
| 48 | return; |
| 49 | } |
andersc | c288dab | 2017-08-15 00:36:00 -0700 | [diff] [blame] | 50 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 51 | block:^{ |
andersc | c288dab | 2017-08-15 00:36:00 -0700 | [diff] [blame] | 52 | _captureSession = captureSession; |
| 53 | AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer]; |
| 54 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 55 | block:^{ |
| 56 | previewLayer.session = captureSession; |
Gustavo Garcia gustavo@lifeonair.com | 19d77c1 | 2017-11-15 16:03:18 +0200 | [diff] [blame^] | 57 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain |
| 58 | block:^{ |
| 59 | [self setCorrectVideoOrientation]; |
| 60 | }]; |
andersc | c288dab | 2017-08-15 00:36:00 -0700 | [diff] [blame] | 61 | }]; |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 62 | }]; |
| 63 | } |
| 64 | |
meetAkshay99 | 0d335c7 | 2017-04-18 07:12:05 -0700 | [diff] [blame] | 65 | - (void)layoutSubviews { |
| 66 | [super layoutSubviews]; |
| 67 | |
| 68 | // Update the video orientation based on the device orientation. |
| 69 | [self setCorrectVideoOrientation]; |
| 70 | } |
| 71 | |
Gustavo Garcia gustavo@lifeonair.com | 19d77c1 | 2017-11-15 16:03:18 +0200 | [diff] [blame^] | 72 | -(void)orientationChanged:(NSNotification *)notification { |
| 73 | [self setCorrectVideoOrientation]; |
| 74 | } |
| 75 | |
meetAkshay99 | 0d335c7 | 2017-04-18 07:12:05 -0700 | [diff] [blame] | 76 | - (void)setCorrectVideoOrientation { |
| 77 | // Get current device orientation. |
| 78 | UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; |
| 79 | AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer]; |
| 80 | |
| 81 | // First check if we are allowed to set the video orientation. |
| 82 | if (previewLayer.connection.isVideoOrientationSupported) { |
| 83 | // Set the video orientation based on device orientation. |
| 84 | if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) { |
| 85 | previewLayer.connection.videoOrientation = |
| 86 | AVCaptureVideoOrientationPortraitUpsideDown; |
| 87 | } else if (deviceOrientation == UIInterfaceOrientationLandscapeRight) { |
| 88 | previewLayer.connection.videoOrientation = |
| 89 | AVCaptureVideoOrientationLandscapeRight; |
| 90 | } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) { |
| 91 | previewLayer.connection.videoOrientation = |
| 92 | AVCaptureVideoOrientationLandscapeLeft; |
| 93 | } else { |
| 94 | previewLayer.connection.videoOrientation = |
| 95 | AVCaptureVideoOrientationPortrait; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 100 | #pragma mark - Private |
| 101 | |
Gustavo Garcia gustavo@lifeonair.com | 19d77c1 | 2017-11-15 16:03:18 +0200 | [diff] [blame^] | 102 | - (void)addOrientationObserver { |
| 103 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 104 | selector:@selector(orientationChanged:) |
| 105 | name:UIDeviceOrientationDidChangeNotification |
| 106 | object:nil]; |
| 107 | } |
| 108 | |
| 109 | - (void)removeOrientationObserver { |
| 110 | [[NSNotificationCenter defaultCenter] removeObserver:self |
| 111 | name:UIDeviceOrientationDidChangeNotification |
| 112 | object:nil]; |
| 113 | } |
| 114 | |
haysc | edd8fef | 2015-12-08 11:08:39 -0800 | [diff] [blame] | 115 | - (AVCaptureVideoPreviewLayer *)previewLayer { |
| 116 | return (AVCaptureVideoPreviewLayer *)self.layer; |
| 117 | } |
| 118 | |
| 119 | @end |