Chromium Code Reviews

Unified Diff: webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m

Issue 2964703002: [iOS] Fix incorrectly oriented frames when rapidly switching between cameras. (Closed)
Patch Set: Used a new flag _changingCamera to gate the frame handling instead. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
« no previous file with comments | « no previous file | webrtc/sdk/objc/Framework/Classes/Video/RTCAVFoundationVideoCapturerInternal.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
diff --git a/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m b/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
index a2290c2458405a1d39458f64eae820abfdc14bc9..1ff3d550367d1552450354558ef103cc2a4de3a9 100644
--- a/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
+++ b/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
@@ -35,11 +35,14 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
AVCaptureVideoDataOutput *_videoDataOutput;
AVCaptureSession *_captureSession;
AVCaptureDevice *_currentDevice;
- RTCVideoRotation _rotation;
BOOL _hasRetriedOnFatalError;
BOOL _isRunning;
// Will the session be running once all asynchronous operations have been completed?
BOOL _willBeRunning;
+ BOOL _changingCamera;
+#if TARGET_OS_IPHONE
+ UIDeviceOrientation _orientation;
+#endif
}
@synthesize frameQueue = _frameQueue;
@@ -55,7 +58,9 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
return nil;
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ _changingCamera = false;
#if TARGET_OS_IPHONE
+ _orientation = UIDeviceOrientationPortrait;
[center addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
@@ -136,12 +141,14 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
return;
}
+ _changingCamera = true;
tkchin_webrtc 2017/07/19 21:08:01 YES / NO not true / false
[self reconfigureCaptureSessionInput];
[self updateOrientation];
[_captureSession startRunning];
[self updateDeviceCaptureFormat:format fps:fps];
[_currentDevice unlockForConfiguration];
_isRunning = true;
+ _changingCamera = false;
}];
}
@@ -181,9 +188,10 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
NSParameterAssert(captureOutput == _videoDataOutput);
+ NSParameterAssert(1 == connection.inputPorts.count);
- if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(sampleBuffer) ||
- !CMSampleBufferDataIsReady(sampleBuffer)) {
+ if (_changingCamera || CMSampleBufferGetNumSamples(sampleBuffer) != 1 ||
+ !CMSampleBufferIsValid(sampleBuffer) || !CMSampleBufferDataIsReady(sampleBuffer)) {
return;
}
@@ -192,11 +200,42 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
return;
}
+#if TARGET_OS_IPHONE
+ // Default to portrait orientation on iPhone.
+ RTCVideoRotation rotation = RTCVideoRotation_90;
+ // Check here, which camera this frame is from, to avoid any race conditions.
+ AVCaptureDeviceInput *deviceInput =
+ (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.firstObject).input;
+ BOOL usingFrontCamera = deviceInput.device.position == AVCaptureDevicePositionFront;
+ switch (_orientation) {
+ case UIDeviceOrientationPortrait:
+ rotation = RTCVideoRotation_90;
+ break;
+ case UIDeviceOrientationPortraitUpsideDown:
+ rotation = RTCVideoRotation_270;
+ break;
+ case UIDeviceOrientationLandscapeLeft:
+ rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
+ break;
+ case UIDeviceOrientationLandscapeRight:
+ rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
+ break;
+ case UIDeviceOrientationFaceUp:
+ case UIDeviceOrientationFaceDown:
+ case UIDeviceOrientationUnknown:
+ // Ignore.
+ break;
+ }
+#else
+ // No rotation on Mac.
+ RTCVideoRotation rotation = RTCVideoRotation_0;
+#endif
+
RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer];
int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) *
- kNanosecondsPerSecond;
+ kNanosecondsPerSecond;
RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
- rotation:_rotation
+ rotation:rotation
timeStampNs:timeStampNs];
[self.delegate capturer:self didCaptureVideoFrame:videoFrame];
}
@@ -399,26 +438,7 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession],
@"updateOrientation must be called on the capture queue.");
#if TARGET_OS_IPHONE
- BOOL usingFrontCamera = _currentDevice.position == AVCaptureDevicePositionFront;
- switch ([UIDevice currentDevice].orientation) {
- case UIDeviceOrientationPortrait:
- _rotation = RTCVideoRotation_90;
- break;
- case UIDeviceOrientationPortraitUpsideDown:
- _rotation = RTCVideoRotation_270;
- break;
- case UIDeviceOrientationLandscapeLeft:
- _rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
- break;
- case UIDeviceOrientationLandscapeRight:
- _rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
- break;
- case UIDeviceOrientationFaceUp:
- case UIDeviceOrientationFaceDown:
- case UIDeviceOrientationUnknown:
- // Ignore.
- break;
- }
+ _orientation = [UIDevice currentDevice].orientation;
#endif
}
« no previous file with comments | « no previous file | webrtc/sdk/objc/Framework/Classes/Video/RTCAVFoundationVideoCapturerInternal.mm » ('j') | no next file with comments »

Powered by Google App Engine