blob: adc62cc30af497bdc16644a3181e59a45c8dd34f [file] [log] [blame]
hayscedd8fef2015-12-08 11:08:39 -08001/*
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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020011#import "RTCCameraPreviewView.h"
hayscedd8fef2015-12-08 11:08:39 -080012
13#import <AVFoundation/AVFoundation.h>
meetAkshay990d335c72017-04-18 07:12:05 -070014#import <UIKit/UIKit.h>
hayscedd8fef2015-12-08 11:08:39 -080015
tkchin9eeb6242016-04-27 01:54:20 -070016#import "RTCDispatcher+Private.h"
hayscedd8fef2015-12-08 11:08:39 -080017
18@implementation RTCCameraPreviewView
19
20@synthesize captureSession = _captureSession;
21
22+ (Class)layerClass {
23 return [AVCaptureVideoPreviewLayer class];
24}
25
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +020026- (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
hayscedd8fef2015-12-08 11:08:39 -080046- (void)setCaptureSession:(AVCaptureSession *)captureSession {
47 if (_captureSession == captureSession) {
48 return;
49 }
Jiawei Ou4aeb35b2018-11-09 13:55:45 -080050 [RTCDispatcher
51 dispatchAsyncOnType:RTCDispatcherTypeMain
52 block:^{
53 self.captureSession = captureSession;
54 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
55 [RTCDispatcher
56 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
57 block:^{
58 previewLayer.session = captureSession;
59 [RTCDispatcher
60 dispatchAsyncOnType:RTCDispatcherTypeMain
61 block:^{
62 [self setCorrectVideoOrientation];
63 }];
64 }];
65 }];
hayscedd8fef2015-12-08 11:08:39 -080066}
67
meetAkshay990d335c72017-04-18 07:12:05 -070068- (void)layoutSubviews {
69 [super layoutSubviews];
70
71 // Update the video orientation based on the device orientation.
72 [self setCorrectVideoOrientation];
73}
74
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +020075-(void)orientationChanged:(NSNotification *)notification {
76 [self setCorrectVideoOrientation];
77}
78
meetAkshay990d335c72017-04-18 07:12:05 -070079- (void)setCorrectVideoOrientation {
80 // Get current device orientation.
81 UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
82 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
83
84 // First check if we are allowed to set the video orientation.
85 if (previewLayer.connection.isVideoOrientationSupported) {
86 // Set the video orientation based on device orientation.
87 if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
88 previewLayer.connection.videoOrientation =
89 AVCaptureVideoOrientationPortraitUpsideDown;
90 } else if (deviceOrientation == UIInterfaceOrientationLandscapeRight) {
91 previewLayer.connection.videoOrientation =
92 AVCaptureVideoOrientationLandscapeRight;
93 } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
94 previewLayer.connection.videoOrientation =
95 AVCaptureVideoOrientationLandscapeLeft;
Anders Carlsson22d43f32017-11-17 15:57:05 +010096 } else if (deviceOrientation == UIInterfaceOrientationPortrait) {
meetAkshay990d335c72017-04-18 07:12:05 -070097 previewLayer.connection.videoOrientation =
98 AVCaptureVideoOrientationPortrait;
99 }
Anders Carlsson22d43f32017-11-17 15:57:05 +0100100 // If device orientation switches to FaceUp or FaceDown, don't change video orientation.
meetAkshay990d335c72017-04-18 07:12:05 -0700101 }
102}
103
hayscedd8fef2015-12-08 11:08:39 -0800104#pragma mark - Private
105
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +0200106- (void)addOrientationObserver {
107 [[NSNotificationCenter defaultCenter] addObserver:self
108 selector:@selector(orientationChanged:)
109 name:UIDeviceOrientationDidChangeNotification
110 object:nil];
111}
112
113- (void)removeOrientationObserver {
114 [[NSNotificationCenter defaultCenter] removeObserver:self
115 name:UIDeviceOrientationDidChangeNotification
116 object:nil];
117}
118
hayscedd8fef2015-12-08 11:08:39 -0800119- (AVCaptureVideoPreviewLayer *)previewLayer {
120 return (AVCaptureVideoPreviewLayer *)self.layer;
121}
122
123@end