webrtc iPhone横屏锁定屏幕本地摄像头显示不正常

基于M70版本,修改的文件有以下3个文件

rtc_video_capture_objc.mRTCCameraVideoCapturer.mRTCCameraPreviewView.m,其中主要思路就是把Device监听改为监听StatusBar的变化。

image.png

统一修改地方

// 注释掉设备监听通知
//    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

rtc_video_capture_objc.mm修改

// 修改监听通知
[notify addObserver:self
               selector:@selector(deviceOrientationDidChange:)
                   name:UIApplicationDidChangeStatusBarFrameNotification
                 object:nil];

- (void)setRelativeVideoOrientation {
  if (!_connection.supportsVideoOrientation) {
    return;
  }
#ifndef WEBRTC_IOS
  _connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
  return;
#else
  UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
  switch (orient) {
    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationUnknown:
      _connection.videoOrientation = AVCaptureVideoOrientationPortrait;
      break;
    case UIInterfaceOrientationPortraitUpsideDown:
      _connection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
      break;
    case UIInterfaceOrientationLandscapeRight:
      _connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
      break;
    case UIInterfaceOrientationLandscapeLeft:
      _connection.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
      break;
    default:
      if (!_orientationHasChanged) {
        _connection.videoOrientation = AVCaptureVideoOrientationPortrait;
      }
      break;
  }
#endif
}

RTCCameraVideoCapturer.m修改

// 修改声明
UIInterfaceOrientation _orientation;
// 修改监听
    _orientation = UIInterfaceOrientationPortrait;
     _rotation = RTCVideoRotation_90;
    [center addObserver:self
               selector:@selector(deviceOrientationDidChange:)
                   name:UIApplicationDidChangeStatusBarFrameNotification
                 object:nil];

// 修改实现
- (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection
switch (_orientation) {
    case UIInterfaceOrientationPortrait:
    case UIInterfaceOrientationUnknown:
      _rotation = RTCVideoRotation_90;
      break;
    case UIInterfaceOrientationPortraitUpsideDown:
      _rotation = RTCVideoRotation_270;
      break;
    case UIInterfaceOrientationLandscapeRight:
      _rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
      break;
    case UIInterfaceOrientationLandscapeLeft:
      _rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
      break;
    default:
      // Ignore.
      break;
  }

RTCCameraPreviewView.m

- (void)setCorrectVideoOrientation {
  // Get current device orientation.
  UIInterfaceOrientation deviceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
........
}

- (void)addOrientationObserver {
  [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(orientationChanged:)
                                                name:UIApplicationDidChangeStatusBarFrameNotification
                                              object:nil];
}

- (void)removeOrientationObserver {
  [[NSNotificationCenter defaultCenter] removeObserver:self
                                                  name:UIApplicationDidChangeStatusBarFrameNotification
                                                object:nil];
}

你可能感兴趣的:(webrtc iPhone横屏锁定屏幕本地摄像头显示不正常)