You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #import "RTCCameraPreviewView.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <UIKit/UIKit.h>
  13. #import "RTCDispatcher+Private.h"
  14. @implementation RTCCameraPreviewView
  15. @synthesize captureSession = _captureSession;
  16. + (Class)layerClass {
  17. return [AVCaptureVideoPreviewLayer class];
  18. }
  19. - (instancetype)initWithFrame:(CGRect)aRect {
  20. self = [super initWithFrame:aRect];
  21. if (self) {
  22. [self addOrientationObserver];
  23. }
  24. return self;
  25. }
  26. - (instancetype)initWithCoder:(NSCoder*)aDecoder {
  27. self = [super initWithCoder:aDecoder];
  28. if (self) {
  29. [self addOrientationObserver];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc {
  34. [self removeOrientationObserver];
  35. }
  36. - (void)setCaptureSession:(AVCaptureSession *)captureSession {
  37. if (_captureSession == captureSession) {
  38. return;
  39. }
  40. _captureSession = captureSession;
  41. [RTCDispatcher
  42. dispatchAsyncOnType:RTCDispatcherTypeMain
  43. block:^{
  44. AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
  45. [RTCDispatcher
  46. dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
  47. block:^{
  48. previewLayer.session = captureSession;
  49. [RTCDispatcher
  50. dispatchAsyncOnType:RTCDispatcherTypeMain
  51. block:^{
  52. [self setCorrectVideoOrientation];
  53. }];
  54. }];
  55. }];
  56. }
  57. - (void)layoutSubviews {
  58. [super layoutSubviews];
  59. // Update the video orientation based on the device orientation.
  60. [self setCorrectVideoOrientation];
  61. }
  62. -(void)orientationChanged:(NSNotification *)notification {
  63. [self setCorrectVideoOrientation];
  64. }
  65. - (void)setCorrectVideoOrientation {
  66. // Get current device orientation.
  67. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  68. AVCaptureVideoPreviewLayer *previewLayer = [self previewLayer];
  69. // First check if we are allowed to set the video orientation.
  70. if (previewLayer.connection.isVideoOrientationSupported) {
  71. // Set the video orientation based on device orientation.
  72. if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
  73. previewLayer.connection.videoOrientation =
  74. AVCaptureVideoOrientationPortraitUpsideDown;
  75. } else if (deviceOrientation == UIInterfaceOrientationLandscapeRight) {
  76. previewLayer.connection.videoOrientation =
  77. AVCaptureVideoOrientationLandscapeRight;
  78. } else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
  79. previewLayer.connection.videoOrientation =
  80. AVCaptureVideoOrientationLandscapeLeft;
  81. } else if (deviceOrientation == UIInterfaceOrientationPortrait) {
  82. previewLayer.connection.videoOrientation =
  83. AVCaptureVideoOrientationPortrait;
  84. }
  85. // If device orientation switches to FaceUp or FaceDown, don't change video orientation.
  86. }
  87. }
  88. #pragma mark - Private
  89. - (void)addOrientationObserver {
  90. [[NSNotificationCenter defaultCenter] addObserver:self
  91. selector:@selector(orientationChanged:)
  92. name:UIDeviceOrientationDidChangeNotification
  93. object:nil];
  94. }
  95. - (void)removeOrientationObserver {
  96. [[NSNotificationCenter defaultCenter] removeObserver:self
  97. name:UIDeviceOrientationDidChangeNotification
  98. object:nil];
  99. }
  100. - (AVCaptureVideoPreviewLayer *)previewLayer {
  101. return (AVCaptureVideoPreviewLayer *)self.layer;
  102. }
  103. @end