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

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

Issue 2964703002: [iOS] Fix incorrectly oriented frames when rapidly switching between cameras. (Closed)
Patch Set: Fix method rename. Created 3 years, 4 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #import <Foundation/Foundation.h> 11 #import <Foundation/Foundation.h>
12 12
13 #import "WebRTC/RTCCameraVideoCapturer.h" 13 #import "WebRTC/RTCCameraVideoCapturer.h"
14 #import "WebRTC/RTCLogging.h" 14 #import "WebRTC/RTCLogging.h"
15 #import "WebRTC/RTCVideoFrameBuffer.h" 15 #import "WebRTC/RTCVideoFrameBuffer.h"
16 16
17 #if TARGET_OS_IPHONE 17 #if TARGET_OS_IPHONE
18 #import "WebRTC/UIDevice+RTCDevice.h" 18 #import "WebRTC/UIDevice+RTCDevice.h"
19 #endif 19 #endif
20 20
21 #import "RTCDispatcher+Private.h" 21 #import "RTCDispatcher+Private.h"
22 #import "RTCImageHelper.h"
22 23
23 const int64_t kNanosecondsPerSecond = 1000000000; 24 const int64_t kNanosecondsPerSecond = 1000000000;
24 25
25 static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) { 26 static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
26 return (mediaSubType == kCVPixelFormatType_420YpCbCr8PlanarFullRange || 27 return (mediaSubType == kCVPixelFormatType_420YpCbCr8PlanarFullRange ||
27 mediaSubType == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange); 28 mediaSubType == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
28 } 29 }
29 30
30 @interface RTCCameraVideoCapturer ()<AVCaptureVideoDataOutputSampleBufferDelegat e> 31 @interface RTCCameraVideoCapturer ()<AVCaptureVideoDataOutputSampleBufferDelegat e>
31 @property(nonatomic, readonly) dispatch_queue_t frameQueue; 32 @property(nonatomic, readonly) dispatch_queue_t frameQueue;
32 @end 33 @end
33 34
34 @implementation RTCCameraVideoCapturer { 35 @implementation RTCCameraVideoCapturer {
35 AVCaptureVideoDataOutput *_videoDataOutput; 36 AVCaptureVideoDataOutput *_videoDataOutput;
36 AVCaptureSession *_captureSession; 37 AVCaptureSession *_captureSession;
37 AVCaptureDevice *_currentDevice; 38 AVCaptureDevice *_currentDevice;
38 RTCVideoRotation _rotation;
39 BOOL _hasRetriedOnFatalError; 39 BOOL _hasRetriedOnFatalError;
40 BOOL _isRunning; 40 BOOL _isRunning;
41 // Will the session be running once all asynchronous operations have been comp leted? 41 // Will the session be running once all asynchronous operations have been comp leted?
42 BOOL _willBeRunning; 42 BOOL _willBeRunning;
43 #if TARGET_OS_IPHONE
44 UIDeviceOrientation _orientation;
45 #endif
43 } 46 }
44 47
45 @synthesize frameQueue = _frameQueue; 48 @synthesize frameQueue = _frameQueue;
46 @synthesize captureSession = _captureSession; 49 @synthesize captureSession = _captureSession;
47 50
48 - (instancetype)initWithDelegate:(__weak id<RTCVideoCapturerDelegate>)delegate { 51 - (instancetype)initWithDelegate:(__weak id<RTCVideoCapturerDelegate>)delegate {
49 if (self = [super initWithDelegate:delegate]) { 52 if (self = [super initWithDelegate:delegate]) {
50 // Create the capture session and all relevant inputs and outputs. We need 53 // Create the capture session and all relevant inputs and outputs. We need
51 // to do this in init because the application may want the capture session 54 // to do this in init because the application may want the capture session
52 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects 55 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects
53 // created here are retained until dealloc and never recreated. 56 // created here are retained until dealloc and never recreated.
54 if (![self setupCaptureSession]) { 57 if (![self setupCaptureSession]) {
55 return nil; 58 return nil;
56 } 59 }
57 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 60 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
58 #if TARGET_OS_IPHONE 61 #if TARGET_OS_IPHONE
62 _orientation = UIDeviceOrientationPortrait;
59 [center addObserver:self 63 [center addObserver:self
60 selector:@selector(deviceOrientationDidChange:) 64 selector:@selector(deviceOrientationDidChange:)
61 name:UIDeviceOrientationDidChangeNotification 65 name:UIDeviceOrientationDidChangeNotification
62 object:nil]; 66 object:nil];
63 [center addObserver:self 67 [center addObserver:self
64 selector:@selector(handleCaptureSessionInterruption:) 68 selector:@selector(handleCaptureSessionInterruption:)
65 name:AVCaptureSessionWasInterruptedNotification 69 name:AVCaptureSessionWasInterruptedNotification
66 object:_captureSession]; 70 object:_captureSession];
67 [center addObserver:self 71 [center addObserver:self
68 selector:@selector(handleCaptureSessionInterruptionEnded:) 72 selector:@selector(handleCaptureSessionInterruptionEnded:)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 [eligibleDeviceFormats addObject:format]; 114 [eligibleDeviceFormats addObject:format];
111 } 115 }
112 } 116 }
113 117
114 return eligibleDeviceFormats; 118 return eligibleDeviceFormats;
115 } 119 }
116 120
117 - (void)startCaptureWithDevice:(AVCaptureDevice *)device 121 - (void)startCaptureWithDevice:(AVCaptureDevice *)device
118 format:(AVCaptureDeviceFormat *)format 122 format:(AVCaptureDeviceFormat *)format
119 fps:(NSInteger)fps { 123 fps:(NSInteger)fps {
120 _willBeRunning = true; 124 _willBeRunning = YES;
121 [RTCDispatcher 125 [RTCDispatcher
122 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 126 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
123 block:^{ 127 block:^{
124 RTCLogInfo("startCaptureWithDevice %@ @ %zd fps", format, fps); 128 RTCLogInfo("startCaptureWithDevice %@ @ %zd fps", format, fps);
125 129
126 #if TARGET_OS_IPHONE 130 #if TARGET_OS_IPHONE
127 [[UIDevice currentDevice] beginGeneratingDeviceOrientation Notifications]; 131 [[UIDevice currentDevice] beginGeneratingDeviceOrientation Notifications];
128 #endif 132 #endif
129 133
130 _currentDevice = device; 134 _currentDevice = device;
131 135
132 NSError *error = nil; 136 NSError *error = nil;
133 if (![_currentDevice lockForConfiguration:&error]) { 137 if (![_currentDevice lockForConfiguration:&error]) {
134 RTCLogError( 138 RTCLogError(
135 @"Failed to lock device %@. Error: %@", _currentDevi ce, error.userInfo); 139 @"Failed to lock device %@. Error: %@", _currentDevi ce, error.userInfo);
136 return; 140 return;
137 } 141 }
138 142
139 [self reconfigureCaptureSessionInput]; 143 [self reconfigureCaptureSessionInput];
140 [self updateOrientation]; 144 [self updateOrientation];
141 [_captureSession startRunning]; 145 [_captureSession startRunning];
142 [self updateDeviceCaptureFormat:format fps:fps]; 146 [self updateDeviceCaptureFormat:format fps:fps];
143 [_currentDevice unlockForConfiguration]; 147 [_currentDevice unlockForConfiguration];
144 _isRunning = true; 148 _isRunning = YES;
145 }]; 149 }];
146 } 150 }
147 151
148 - (void)stopCapture { 152 - (void)stopCapture {
149 _willBeRunning = false; 153 _willBeRunning = NO;
150 [RTCDispatcher 154 [RTCDispatcher
151 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 155 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
152 block:^{ 156 block:^{
153 RTCLogInfo("Stop"); 157 RTCLogInfo("Stop");
154 _currentDevice = nil; 158 _currentDevice = nil;
155 for (AVCaptureDeviceInput *oldInput in [_captureSession.in puts copy]) { 159 for (AVCaptureDeviceInput *oldInput in [_captureSession.in puts copy]) {
156 [_captureSession removeInput:oldInput]; 160 [_captureSession removeInput:oldInput];
157 } 161 }
158 [_captureSession stopRunning]; 162 [_captureSession stopRunning];
159 163
160 #if TARGET_OS_IPHONE 164 #if TARGET_OS_IPHONE
161 [[UIDevice currentDevice] endGeneratingDeviceOrientationNo tifications]; 165 [[UIDevice currentDevice] endGeneratingDeviceOrientationNo tifications];
162 #endif 166 #endif
163 _isRunning = false; 167 _isRunning = NO;
164 }]; 168 }];
165 } 169 }
166 170
167 #pragma mark iOS notifications 171 #pragma mark iOS notifications
168 172
169 #if TARGET_OS_IPHONE 173 #if TARGET_OS_IPHONE
170 - (void)deviceOrientationDidChange:(NSNotification *)notification { 174 - (void)deviceOrientationDidChange:(NSNotification *)notification {
171 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 175 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
172 block:^{ 176 block:^{
173 [self updateOrientation]; 177 [self updateOrientation];
(...skipping 11 matching lines...) Expand all
185 if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(s ampleBuffer) || 189 if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(s ampleBuffer) ||
186 !CMSampleBufferDataIsReady(sampleBuffer)) { 190 !CMSampleBufferDataIsReady(sampleBuffer)) {
187 return; 191 return;
188 } 192 }
189 193
190 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 194 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
191 if (pixelBuffer == nil) { 195 if (pixelBuffer == nil) {
192 return; 196 return;
193 } 197 }
194 198
199 #if TARGET_OS_IPHONE
200 // Default to portrait orientation on iPhone.
201 RTCVideoRotation rotation = RTCVideoRotation_90;
202 // Check here, which camera this frame is from, to avoid any race conditions.
203 AVCaptureDeviceInput *deviceInput =
204 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.first Object).input;
205 BOOL usingFrontCamera = deviceInput.device.position == AVCaptureDevicePosition Front;
206 // Check the image's EXIF for the actual camera the image came as the image co uld have been
207 // delayed as we set alwaysDiscardsLateVideoFrames to NO.
208 AVCaptureDevicePosition cameraPosition =
209 [RTCImageHelper devicePositionForSampleBuffer:sampleBuffer];
210 if (cameraPosition != AVCaptureDevicePositionUnspecified) {
211 usingFrontCamera = cameraPosition == AVCaptureDevicePositionFront;
212 }
213 switch (_orientation) {
214 case UIDeviceOrientationPortrait:
215 rotation = RTCVideoRotation_90;
216 break;
217 case UIDeviceOrientationPortraitUpsideDown:
218 rotation = RTCVideoRotation_270;
219 break;
220 case UIDeviceOrientationLandscapeLeft:
221 rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
222 break;
223 case UIDeviceOrientationLandscapeRight:
224 rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
225 break;
226 case UIDeviceOrientationFaceUp:
227 case UIDeviceOrientationFaceDown:
228 case UIDeviceOrientationUnknown:
229 // Ignore.
230 break;
231 }
232 #else
233 // No rotation on Mac.
234 RTCVideoRotation rotation = RTCVideoRotation_0;
235 #endif
236
195 RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuff er:pixelBuffer]; 237 RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuff er:pixelBuffer];
196 int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp( sampleBuffer)) * 238 int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp( sampleBuffer)) *
197 kNanosecondsPerSecond; 239 kNanosecondsPerSecond;
198 RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuff er 240 RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuff er
199 rotation:_rotation 241 rotation:rotation
200 timeStampNs:timeStampNs] ; 242 timeStampNs:timeStampNs] ;
201 [self.delegate capturer:self didCaptureVideoFrame:videoFrame]; 243 [self.delegate capturer:self didCaptureVideoFrame:videoFrame];
202 } 244 }
203 245
204 - (void)captureOutput:(AVCaptureOutput *)captureOutput 246 - (void)captureOutput:(AVCaptureOutput *)captureOutput
205 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer 247 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
206 fromConnection:(AVCaptureConnection *)connection { 248 fromConnection:(AVCaptureConnection *)connection {
207 RTCLogError(@"Dropped sample buffer."); 249 RTCLogError(@"Dropped sample buffer.");
208 } 250 }
209 251
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 } else { 434 } else {
393 RTCLogError(@"Cannot add camera as an input to the session."); 435 RTCLogError(@"Cannot add camera as an input to the session.");
394 } 436 }
395 [_captureSession commitConfiguration]; 437 [_captureSession commitConfiguration];
396 } 438 }
397 439
398 - (void)updateOrientation { 440 - (void)updateOrientation {
399 NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession], 441 NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession],
400 @"updateOrientation must be called on the capture queue."); 442 @"updateOrientation must be called on the capture queue.");
401 #if TARGET_OS_IPHONE 443 #if TARGET_OS_IPHONE
402 BOOL usingFrontCamera = _currentDevice.position == AVCaptureDevicePositionFron t; 444 _orientation = [UIDevice currentDevice].orientation;
403 switch ([UIDevice currentDevice].orientation) {
404 case UIDeviceOrientationPortrait:
405 _rotation = RTCVideoRotation_90;
406 break;
407 case UIDeviceOrientationPortraitUpsideDown:
408 _rotation = RTCVideoRotation_270;
409 break;
410 case UIDeviceOrientationLandscapeLeft:
411 _rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0;
412 break;
413 case UIDeviceOrientationLandscapeRight:
414 _rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180;
415 break;
416 case UIDeviceOrientationFaceUp:
417 case UIDeviceOrientationFaceDown:
418 case UIDeviceOrientationUnknown:
419 // Ignore.
420 break;
421 }
422 #endif 445 #endif
423 } 446 }
424 447
425 @end 448 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698