OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "WebRTC/RTCCameraPreviewView.h" | 11 #import "WebRTC/RTCCameraPreviewView.h" |
12 | 12 |
13 #import <AVFoundation/AVFoundation.h> | 13 #import <AVFoundation/AVFoundation.h> |
| 14 #import <UIKit/UIKit.h> |
14 | 15 |
15 #import "RTCDispatcher+Private.h" | 16 #import "RTCDispatcher+Private.h" |
16 | 17 |
17 @implementation RTCCameraPreviewView | 18 @implementation RTCCameraPreviewView |
18 | 19 |
19 @synthesize captureSession = _captureSession; | 20 @synthesize captureSession = _captureSession; |
20 | 21 |
21 + (Class)layerClass { | 22 + (Class)layerClass { |
22 return [AVCaptureVideoPreviewLayer class]; | 23 return [AVCaptureVideoPreviewLayer class]; |
23 } | 24 } |
24 | 25 |
25 - (void)setCaptureSession:(AVCaptureSession *)captureSession { | 26 - (void)setCaptureSession:(AVCaptureSession *)captureSession { |
26 if (_captureSession == captureSession) { | 27 if (_captureSession == captureSession) { |
27 return; | 28 return; |
28 } | 29 } |
29 _captureSession = captureSession; | 30 _captureSession = captureSession; |
30 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer]; | 31 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer]; |
31 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | 32 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
32 block:^{ | 33 block:^{ |
33 previewLayer.session = captureSession; | 34 previewLayer.session = captureSession; |
34 }]; | 35 }]; |
35 } | 36 } |
36 | 37 |
| 38 - (void)layoutSubviews { |
| 39 [super layoutSubviews]; |
| 40 |
| 41 // Update the video orientation based on the device orientation. |
| 42 [self setCorrectVideoOrientation]; |
| 43 } |
| 44 |
| 45 - (void)setCorrectVideoOrientation { |
| 46 // Get current device orientation. |
| 47 UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation; |
| 48 AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer]; |
| 49 |
| 50 // First check if we are allowed to set the video orientation. |
| 51 if (previewLayer.connection.isVideoOrientationSupported) { |
| 52 // Set the video orientation based on device orientation. |
| 53 if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) { |
| 54 previewLayer.connection.videoOrientation = |
| 55 AVCaptureVideoOrientationPortraitUpsideDown; |
| 56 } else if (deviceOrientation == UIInterfaceOrientationLandscapeRight) { |
| 57 previewLayer.connection.videoOrientation = |
| 58 AVCaptureVideoOrientationLandscapeRight; |
| 59 } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) { |
| 60 previewLayer.connection.videoOrientation = |
| 61 AVCaptureVideoOrientationLandscapeLeft; |
| 62 } else { |
| 63 previewLayer.connection.videoOrientation = |
| 64 AVCaptureVideoOrientationPortrait; |
| 65 } |
| 66 } |
| 67 } |
| 68 |
37 #pragma mark - Private | 69 #pragma mark - Private |
38 | 70 |
39 - (AVCaptureVideoPreviewLayer *)previewLayer { | 71 - (AVCaptureVideoPreviewLayer *)previewLayer { |
40 return (AVCaptureVideoPreviewLayer *)self.layer; | 72 return (AVCaptureVideoPreviewLayer *)self.layer; |
41 } | 73 } |
42 | 74 |
43 @end | 75 @end |
OLD | NEW |