Skip to content

Commit 28e2021

Browse files
hiroshihoriecloudwebrtc
authored andcommitted
Fix camera rotation (#92)
Use UIInterfaceOrientation instead of UIDeviceOrientation for RTCVideoFrame's rotation
1 parent f12f8d9 commit 28e2021

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

sdk/objc/components/capturer/RTCCameraVideoCapturer.m

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ @implementation RTC_OBJC_TYPE (RTCCameraVideoCapturer) {
4242
FourCharCode _outputPixelFormat;
4343
RTCVideoRotation _rotation;
4444
#if TARGET_OS_IPHONE
45-
UIDeviceOrientation _orientation;
45+
UIInterfaceOrientation _orientation;
4646
BOOL _generatingOrientationNotifications;
4747
#endif
4848
}
@@ -75,7 +75,7 @@ - (instancetype)initWithDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelega
7575
}
7676
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
7777
#if TARGET_OS_IPHONE
78-
_orientation = UIDeviceOrientationPortrait;
78+
_orientation = UIInterfaceOrientationPortrait;
7979
_rotation = RTCVideoRotation_90;
8080
[center addObserver:self
8181
selector:@selector(deviceOrientationDidChange:)
@@ -166,6 +166,8 @@ - (void)startCaptureWithDevice:(AVCaptureDevice *)device
166166
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
167167
self->_generatingOrientationNotifications = YES;
168168
}
169+
// Must be called on main
170+
[self updateOrientation];
169171
});
170172
#endif
171173

@@ -183,7 +185,6 @@ - (void)startCaptureWithDevice:(AVCaptureDevice *)device
183185
return;
184186
}
185187
[self reconfigureCaptureSessionInput];
186-
[self updateOrientation];
187188
[self updateDeviceCaptureFormat:format fps:fps];
188189
[self updateVideoDataOutputPixelFormat:format];
189190
[self.captureSession startRunning];
@@ -226,10 +227,7 @@ - (void)stopCaptureWithCompletionHandler:(nullable void (^)(void))completionHand
226227

227228
#if TARGET_OS_IPHONE
228229
- (void)deviceOrientationDidChange:(NSNotification *)notification {
229-
[RTC_OBJC_TYPE(RTCDispatcher) dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
230-
block:^{
231-
[self updateOrientation];
232-
}];
230+
[self updateOrientation];
233231
}
234232
#endif
235233

@@ -265,22 +263,20 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput
265263
usingFrontCamera = AVCaptureDevicePositionFront == deviceInput.device.position;
266264
}
267265
switch (_orientation) {
268-
case UIDeviceOrientationPortrait:
266+
case UIInterfaceOrientationPortrait:
269267
_rotation = RTCVideoRotation_90;
270268
break;
271-
case UIDeviceOrientationPortraitUpsideDown:
269+
case UIInterfaceOrientationPortraitUpsideDown:
272270
_rotation = RTCVideoRotation_270;
273271
break;
274-
case UIDeviceOrientationLandscapeLeft:
275-
_rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
276-
break;
277-
case UIDeviceOrientationLandscapeRight:
272+
case UIInterfaceOrientationLandscapeLeft:
278273
_rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
279274
break;
280-
case UIDeviceOrientationFaceUp:
281-
case UIDeviceOrientationFaceDown:
282-
case UIDeviceOrientationUnknown:
283-
// Ignore.
275+
case UIInterfaceOrientationLandscapeRight:
276+
_rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
277+
break;
278+
case UIInterfaceOrientationUnknown:
279+
_rotation = RTCVideoRotation_0;
284280
break;
285281
}
286282
#else
@@ -495,8 +491,8 @@ - (void)updateDeviceCaptureFormat:(AVCaptureDeviceFormat *)format fps:(NSInteger
495491
@"updateDeviceCaptureFormat must be called on the capture queue.");
496492
@try {
497493
_currentDevice.activeFormat = format;
498-
if(![NSStringFromClass([_currentDevice class]) isEqualToString:@"AVCaptureDALDevice"]) {
499-
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
494+
if (![NSStringFromClass([_currentDevice class]) isEqualToString:@"AVCaptureDALDevice"]) {
495+
_currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps);
500496
}
501497
} @catch (NSException *exception) {
502498
RTCLogError(@"Failed to set active format!\n User info:%@", exception.userInfo);
@@ -508,8 +504,8 @@ - (void)reconfigureCaptureSessionInput {
508504
NSAssert([RTC_OBJC_TYPE(RTCDispatcher) isOnQueueForType:RTCDispatcherTypeCaptureSession],
509505
@"reconfigureCaptureSessionInput must be called on the capture queue.");
510506
NSError *error = nil;
511-
AVCaptureDeviceInput *input =
512-
[AVCaptureDeviceInput deviceInputWithDevice:_currentDevice error:&error];
507+
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:_currentDevice
508+
error:&error];
513509
if (!input) {
514510
RTCLogError(@"Failed to create front camera input: %@", error.localizedDescription);
515511
return;
@@ -526,12 +522,19 @@ - (void)reconfigureCaptureSessionInput {
526522
[_captureSession commitConfiguration];
527523
}
528524

529-
- (void)updateOrientation {
530-
NSAssert([RTC_OBJC_TYPE(RTCDispatcher) isOnQueueForType:RTCDispatcherTypeCaptureSession],
531-
@"updateOrientation must be called on the capture queue.");
532525
#if TARGET_OS_IPHONE
533-
_orientation = [UIDevice currentDevice].orientation;
534-
#endif
526+
- (void)updateOrientation {
527+
NSAssert([RTC_OBJC_TYPE(RTCDispatcher) isOnQueueForType:RTCDispatcherTypeMain],
528+
@"statusBarOrientation must be called on the main queue.");
529+
// statusBarOrientation must be called on the main queue
530+
UIInterfaceOrientation newOrientation = [UIApplication sharedApplication].statusBarOrientation;
531+
532+
[RTC_OBJC_TYPE(RTCDispatcher) dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
533+
block:^{
534+
// Must be called on the capture queue
535+
self->_orientation = newOrientation;
536+
}];
535537
}
538+
#endif
536539

537540
@end

0 commit comments

Comments
 (0)