Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

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: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« 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..dfafe0abf9120c7f4b2ff8d418e374dbadba626e 100644
--- a/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
+++ b/webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCCameraVideoCapturer.m
@@ -35,11 +35,13 @@ 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;
+#if TARGET_OS_IPHONE
+ UIDeviceOrientation _orientation;
+#endif
}
@synthesize frameQueue = _frameQueue;
@@ -56,6 +58,7 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if TARGET_OS_IPHONE
+ _orientation = UIDeviceOrientationPortrait;
[center addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
@@ -117,6 +120,7 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
- (void)startCaptureWithDevice:(AVCaptureDevice *)device
format:(AVCaptureDeviceFormat *)format
fps:(NSInteger)fps {
+ _isRunning = false;
magjed_webrtc 2017/06/30 17:50:50 Why is this needed?
jtt_webrtc 2017/06/30 17:54:49 We need to stop frames from being processed until
jtt_webrtc 2017/06/30 18:07:47 I removed this check and the flicker came back. T
magjed_webrtc 2017/07/03 08:23:46 Why does it flicker if we remove this? I thought i
_willBeRunning = true;
[RTCDispatcher
dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
@@ -133,6 +137,7 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
if (![_currentDevice lockForConfiguration:&error]) {
RTCLogError(
@"Failed to lock device %@. Error: %@", _currentDevice, error.userInfo);
+ _isRunning = true;
magjed_webrtc 2017/06/30 17:50:50 Same here, why is this change needed?
jtt_webrtc 2017/06/30 17:54:49 See above. We don't want to process frames until t
return;
}
@@ -181,9 +186,13 @@ 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 (!_isRunning || !_willBeRunning ||
magjed_webrtc 2017/06/30 17:50:50 Why do we need these extra checks? What happens ot
jtt_webrtc 2017/06/30 17:54:49 See above, we still get the flicker but more rare.
+ CMSampleBufferGetNumSamples(sampleBuffer) != 1 ||
+ !CMSampleBufferIsValid(sampleBuffer) ||
+ !CMSampleBufferDataIsReady(sampleBuffer) || !connection.enabled ||
+ !connection.active) {
return;
}
@@ -192,12 +201,46 @@ static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
return;
}
+#if TARGET_OS_IPHONE
+ // Default to portrait orientation on iPhone.
+ RTCVideoRotation rotation = RTCVideoRotation_90;
+ 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;
- RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
- rotation:_rotation
- timeStampNs:timeStampNs];
+ RTCVideoFrame *videoFrame =
+ [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
+ rotation:rotation
+ timeStampNs:timeStampNs];
[self.delegate capturer:self didCaptureVideoFrame:videoFrame];
}
@@ -399,26 +442,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
This is Rietveld 408576698