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

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/Video/RTCAVFoundationVideoCapturerInternal.mm

Issue 2964703002: [iOS] Fix incorrectly oriented frames when rapidly switching between cameras. (Closed)
Patch Set: Moved common code to new class. Created 3 years, 5 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 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2016 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 "RTCAVFoundationVideoCapturerInternal.h" 11 #import "RTCAVFoundationVideoCapturerInternal.h"
12 12
13 #import <Foundation/Foundation.h> 13 #import <Foundation/Foundation.h>
14 #if TARGET_OS_IPHONE 14 #if TARGET_OS_IPHONE
15 #import <UIKit/UIKit.h> 15 #import <UIKit/UIKit.h>
16 #import "WebRTC/UIDevice+RTCDevice.h" 16 #import "WebRTC/UIDevice+RTCDevice.h"
17 #endif 17 #endif
18 18
19 #import "RTCDispatcher+Private.h" 19 #import "RTCDispatcher+Private.h"
20 #import "RTCImageHelper.h"
20 #import "WebRTC/RTCLogging.h" 21 #import "WebRTC/RTCLogging.h"
21 22
22 #include "avfoundationformatmapper.h" 23 #include "avfoundationformatmapper.h"
23 24
24 @implementation RTCAVFoundationVideoCapturerInternal { 25 @implementation RTCAVFoundationVideoCapturerInternal {
25 // Keep pointers to inputs for convenience. 26 // Keep pointers to inputs for convenience.
26 AVCaptureDeviceInput *_frontCameraInput; 27 AVCaptureDeviceInput *_frontCameraInput;
27 AVCaptureDeviceInput *_backCameraInput; 28 AVCaptureDeviceInput *_backCameraInput;
28 AVCaptureVideoDataOutput *_videoDataOutput; 29 AVCaptureVideoDataOutput *_videoDataOutput;
29 // The cricket::VideoCapturer that owns this class. Should never be NULL. 30 // The cricket::VideoCapturer that owns this class. Should never be NULL.
30 webrtc::AVFoundationVideoCapturer *_capturer; 31 webrtc::AVFoundationVideoCapturer *_capturer;
31 webrtc::VideoRotation _rotation;
32 BOOL _hasRetriedOnFatalError; 32 BOOL _hasRetriedOnFatalError;
33 BOOL _isRunning; 33 BOOL _isRunning;
34 BOOL _hasStarted; 34 BOOL _hasStarted;
35 rtc::CriticalSection _crit; 35 rtc::CriticalSection _crit;
36 BOOL _switchingCameras;
37 #if TARGET_OS_IPHONE
38 UIDeviceOrientation _orientation;
39 #endif
36 } 40 }
37 41
38 @synthesize captureSession = _captureSession; 42 @synthesize captureSession = _captureSession;
39 @synthesize frameQueue = _frameQueue; 43 @synthesize frameQueue = _frameQueue;
40 @synthesize useBackCamera = _useBackCamera; 44 @synthesize useBackCamera = _useBackCamera;
41 45
42 @synthesize isRunning = _isRunning; 46 @synthesize isRunning = _isRunning;
43 @synthesize hasStarted = _hasStarted; 47 @synthesize hasStarted = _hasStarted;
44 48
45 // This is called from the thread that creates the video source, which is likely 49 // This is called from the thread that creates the video source, which is likely
46 // the main thread. 50 // the main thread.
47 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { 51 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer {
48 RTC_DCHECK(capturer); 52 RTC_DCHECK(capturer);
49 if (self = [super init]) { 53 if (self = [super init]) {
50 _capturer = capturer; 54 _capturer = capturer;
51 // Create the capture session and all relevant inputs and outputs. We need 55 // Create the capture session and all relevant inputs and outputs. We need
52 // to do this in init because the application may want the capture session 56 // to do this in init because the application may want the capture session
53 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects 57 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects
54 // created here are retained until dealloc and never recreated. 58 // created here are retained until dealloc and never recreated.
55 if (![self setupCaptureSession]) { 59 if (![self setupCaptureSession]) {
56 return nil; 60 return nil;
57 } 61 }
58 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 62 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
63 _switchingCameras = NO;
59 #if TARGET_OS_IPHONE 64 #if TARGET_OS_IPHONE
65 _orientation = UIDeviceOrientationPortrait;
60 [center addObserver:self 66 [center addObserver:self
61 selector:@selector(deviceOrientationDidChange:) 67 selector:@selector(deviceOrientationDidChange:)
62 name:UIDeviceOrientationDidChangeNotification 68 name:UIDeviceOrientationDidChangeNotification
63 object:nil]; 69 object:nil];
64 [center addObserver:self 70 [center addObserver:self
65 selector:@selector(handleCaptureSessionInterruption:) 71 selector:@selector(handleCaptureSessionInterruption:)
66 name:AVCaptureSessionWasInterruptedNotification 72 name:AVCaptureSessionWasInterruptedNotification
67 object:_captureSession]; 73 object:_captureSession];
68 [center addObserver:self 74 [center addObserver:self
69 selector:@selector(handleCaptureSessionInterruptionEnded:) 75 selector:@selector(handleCaptureSessionInterruptionEnded:)
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 _useBackCamera = useBackCamera; 156 _useBackCamera = useBackCamera;
151 [self updateSessionInputForUseBackCamera:useBackCamera]; 157 [self updateSessionInputForUseBackCamera:useBackCamera];
152 } 158 }
153 } 159 }
154 160
155 // Called from WebRTC thread. 161 // Called from WebRTC thread.
156 - (void)start { 162 - (void)start {
157 if (self.hasStarted) { 163 if (self.hasStarted) {
158 return; 164 return;
159 } 165 }
160 self.hasStarted = YES;
161 [RTCDispatcher 166 [RTCDispatcher
162 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 167 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
163 block:^{ 168 block:^{
164 #if TARGET_OS_IPHONE
165 // Default to portrait orientation on iPhone. This will be reset in
166 // updateOrientation unless orientation is unknown/faceu p/facedown.
167 _rotation = webrtc::kVideoRotation_90;
168 #else
169 // No rotation on Mac.
170 _rotation = webrtc::kVideoRotation_0;
171 #endif
172 [self updateOrientation]; 169 [self updateOrientation];
173 #if TARGET_OS_IPHONE 170 #if TARGET_OS_IPHONE
174 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications]; 171 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications];
175 #endif 172 #endif
176 AVCaptureSession *captureSession = self.captureSession; 173 AVCaptureSession *captureSession = self.captureSession;
177 [captureSession startRunning]; 174 [captureSession startRunning];
175 self.hasStarted = YES;
178 }]; 176 }];
179 } 177 }
180 178
181 // Called from same thread as start. 179 // Called from same thread as start.
182 - (void)stop { 180 - (void)stop {
183 if (!self.hasStarted) { 181 if (!self.hasStarted) {
184 return; 182 return;
185 } 183 }
186 self.hasStarted = NO; 184 self.hasStarted = NO;
187 // Due to this async block, it's possible that the ObjC object outlives the 185 // Due to this async block, it's possible that the ObjC object outlives the
(...skipping 23 matching lines...) Expand all
211 209
212 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate 210 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
213 211
214 - (void)captureOutput:(AVCaptureOutput *)captureOutput 212 - (void)captureOutput:(AVCaptureOutput *)captureOutput
215 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 213 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
216 fromConnection:(AVCaptureConnection *)connection { 214 fromConnection:(AVCaptureConnection *)connection {
217 NSParameterAssert(captureOutput == _videoDataOutput); 215 NSParameterAssert(captureOutput == _videoDataOutput);
218 if (!self.hasStarted) { 216 if (!self.hasStarted) {
219 return; 217 return;
220 } 218 }
221 _capturer->CaptureSampleBuffer(sampleBuffer, _rotation); 219
220 #if TARGET_OS_IPHONE
221 // Default to portrait orientation on iPhone.
222 webrtc::VideoRotation rotation = webrtc::kVideoRotation_90;
223 AVCaptureDeviceInput *deviceInput =
224 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.first Object).input;
225 BOOL usingFrontCamera = deviceInput.device.position == AVCaptureDevicePosition Front;
226 // We gate checking the image's EXIF only if we're switching cameras as we don 't need to parse
227 // the image's attachments and dictionaries for every video image.
228 if (_switchingCameras) {
229 // Check the image's EXIF for the actual camera the image came as the image could have been
230 // delayed as we set alwaysDiscardsLateVideoFrames to NO.
231 usingFrontCamera = [RTCImageHelper isFrontCameraFromSampleBuffer:sampleBuffe r];
232 }
233 switch (_orientation) {
234 case UIDeviceOrientationPortrait:
235 rotation = webrtc::kVideoRotation_90;
236 break;
237 case UIDeviceOrientationPortraitUpsideDown:
238 rotation = webrtc::kVideoRotation_270;
239 break;
240 case UIDeviceOrientationLandscapeLeft:
241 rotation = usingFrontCamera ? webrtc::kVideoRotation_180 : webrtc::kVideoR otation_0;
242 break;
243 case UIDeviceOrientationLandscapeRight:
244 rotation = usingFrontCamera ? webrtc::kVideoRotation_0 : webrtc::kVideoRot ation_180;
245 break;
246 case UIDeviceOrientationFaceUp:
247 case UIDeviceOrientationFaceDown:
248 case UIDeviceOrientationUnknown:
249 // Ignore.
250 break;
251 }
252 #else
253 // No rotation on Mac.
254 webrtc::VideoRotation rotation = webrtc::kVideoRotation_0;
255 #endif
256
257 _capturer->CaptureSampleBuffer(sampleBuffer, rotation);
222 } 258 }
223 259
224 - (void)captureOutput:(AVCaptureOutput *)captureOutput 260 - (void)captureOutput:(AVCaptureOutput *)captureOutput
225 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer 261 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
226 fromConnection:(AVCaptureConnection *)connection { 262 fromConnection:(AVCaptureConnection *)connection {
227 RTCLogError(@"Dropped sample buffer."); 263 RTCLogError(@"Dropped sample buffer.");
228 } 264 }
229 265
230 #pragma mark - AVCaptureSession notifications 266 #pragma mark - AVCaptureSession notifications
231 267
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 return nil; 477 return nil;
442 } 478 }
443 _backCameraInput = backCameraInput; 479 _backCameraInput = backCameraInput;
444 } 480 }
445 return _backCameraInput; 481 return _backCameraInput;
446 } 482 }
447 483
448 // Called from capture session queue. 484 // Called from capture session queue.
449 - (void)updateOrientation { 485 - (void)updateOrientation {
450 #if TARGET_OS_IPHONE 486 #if TARGET_OS_IPHONE
451 switch ([UIDevice currentDevice].orientation) { 487 _orientation = [UIDevice currentDevice].orientation;
452 case UIDeviceOrientationPortrait:
453 _rotation = webrtc::kVideoRotation_90;
454 break;
455 case UIDeviceOrientationPortraitUpsideDown:
456 _rotation = webrtc::kVideoRotation_270;
457 break;
458 case UIDeviceOrientationLandscapeLeft:
459 _rotation =
460 _capturer->GetUseBackCamera() ? webrtc::kVideoRotation_0 : webrtc::kVi deoRotation_180;
461 break;
462 case UIDeviceOrientationLandscapeRight:
463 _rotation =
464 _capturer->GetUseBackCamera() ? webrtc::kVideoRotation_180 : webrtc::k VideoRotation_0;
465 break;
466 case UIDeviceOrientationFaceUp:
467 case UIDeviceOrientationFaceDown:
468 case UIDeviceOrientationUnknown:
469 // Ignore.
470 break;
471 }
472 #endif 488 #endif
473 } 489 }
474 490
475 // Update the current session input to match what's stored in _useBackCamera. 491 // Update the current session input to match what's stored in _useBackCamera.
476 - (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera { 492 - (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera {
477 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 493 [RTCDispatcher
478 block:^{ 494 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
479 [_captureSession beginConfiguration]; 495 block:^{
480 AVCaptureDeviceInput *oldInput = _backCameraI nput; 496 _switchingCameras = YES;
481 AVCaptureDeviceInput *newInput = _frontCamera Input; 497 [_captureSession beginConfiguration];
482 if (useBackCamera) { 498 AVCaptureDeviceInput *oldInput = _backCameraInput;
483 oldInput = _frontCameraInput; 499 AVCaptureDeviceInput *newInput = _frontCameraInput;
484 newInput = _backCameraInput; 500 if (useBackCamera) {
485 } 501 oldInput = _frontCameraInput;
486 if (oldInput) { 502 newInput = _backCameraInput;
487 // Ok to remove this even if it's not attac hed. Will be no-op. 503 }
488 [_captureSession removeInput:oldInput]; 504 if (oldInput) {
489 } 505 // Ok to remove this even if it's not attached. Will be no-op.
490 if (newInput) { 506 [_captureSession removeInput:oldInput];
491 [_captureSession addInput:newInput]; 507 }
492 } 508 if (newInput) {
493 [self updateOrientation]; 509 [_captureSession addInput:newInput];
494 AVCaptureDevice *newDevice = newInput.device; 510 }
495 const cricket::VideoFormat *format = 511 [self updateOrientation];
496 _capturer->GetCaptureFormat(); 512 AVCaptureDevice *newDevice = newInput.device;
497 webrtc::SetFormatForCaptureDevice( 513 const cricket::VideoFormat *format = _capturer->GetCapture Format();
498 newDevice, _captureSession, *format); 514 webrtc::SetFormatForCaptureDevice(newDevice, _captureSessi on, *format);
499 [_captureSession commitConfiguration]; 515 [_captureSession commitConfiguration];
500 }]; 516 _switchingCameras = NO;
517 }];
501 } 518 }
502 519
503 @end 520 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698