blob: 8af3384e7f9f3645d837670e63bb54db52842432 [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
26- (void)setCaptureSession:(AVCaptureSession *)captureSession {
27 if (_captureSession == captureSession) {
28 return;
29 }
anderscc288dab2017-08-15 00:36:00 -070030 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeMain
hayscedd8fef2015-12-08 11:08:39 -080031 block:^{
anderscc288dab2017-08-15 00:36:00 -070032 _captureSession = captureSession;
33 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
34 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
35 block:^{
36 previewLayer.session = captureSession;
37 }];
hayscedd8fef2015-12-08 11:08:39 -080038 }];
39}
40
meetAkshay990d335c72017-04-18 07:12:05 -070041- (void)layoutSubviews {
42 [super layoutSubviews];
43
44 // Update the video orientation based on the device orientation.
45 [self setCorrectVideoOrientation];
46}
47
48- (void)setCorrectVideoOrientation {
49 // Get current device orientation.
50 UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
51 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
52
53 // First check if we are allowed to set the video orientation.
54 if (previewLayer.connection.isVideoOrientationSupported) {
55 // Set the video orientation based on device orientation.
56 if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
57 previewLayer.connection.videoOrientation =
58 AVCaptureVideoOrientationPortraitUpsideDown;
59 } else if (deviceOrientation == UIInterfaceOrientationLandscapeRight) {
60 previewLayer.connection.videoOrientation =
61 AVCaptureVideoOrientationLandscapeRight;
62 } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
63 previewLayer.connection.videoOrientation =
64 AVCaptureVideoOrientationLandscapeLeft;
65 } else {
66 previewLayer.connection.videoOrientation =
67 AVCaptureVideoOrientationPortrait;
68 }
69 }
70}
71
hayscedd8fef2015-12-08 11:08:39 -080072#pragma mark - Private
73
74- (AVCaptureVideoPreviewLayer *)previewLayer {
75 return (AVCaptureVideoPreviewLayer *)self.layer;
76}
77
78@end