blob: 2b31e107a83f1db27561e68bd604161bcae01938 [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
tkchin9eeb6242016-04-27 01:54:20 -070011#import "WebRTC/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 }
anderscc288dab2017-08-15 00:36:00 -070050 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain
hayscedd8fef2015-12-08 11:08:39 -080051 block:^{
anderscc288dab2017-08-15 00:36:00 -070052 _captureSession = captureSession;
53 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
54 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
55 block:^{
56 previewLayer.session = captureSession;
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +020057 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain
58 block:^{
59 [self setCorrectVideoOrientation];
60 }];
anderscc288dab2017-08-15 00:36:00 -070061 }];
hayscedd8fef2015-12-08 11:08:39 -080062 }];
63}
64
meetAkshay990d335c72017-04-18 07:12:05 -070065- (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.com19d77c12017-11-15 16:03:18 +020072-(void)orientationChanged:(NSNotification *)notification {
73 [self setCorrectVideoOrientation];
74}
75
meetAkshay990d335c72017-04-18 07:12:05 -070076- (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
hayscedd8fef2015-12-08 11:08:39 -0800100#pragma mark - Private
101
Gustavo Garcia gustavo@lifeonair.com19d77c12017-11-15 16:03:18 +0200102- (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
hayscedd8fef2015-12-08 11:08:39 -0800115- (AVCaptureVideoPreviewLayer *)previewLayer {
116 return (AVCaptureVideoPreviewLayer *)self.layer;
117}
118
119@end