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

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: Renamed RTCImageHelper method, removed gate check for switching cameras. 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 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 #if TARGET_OS_IPHONE
37 UIDeviceOrientation _orientation;
38 #endif
36 } 39 }
37 40
38 @synthesize captureSession = _captureSession; 41 @synthesize captureSession = _captureSession;
39 @synthesize frameQueue = _frameQueue; 42 @synthesize frameQueue = _frameQueue;
40 @synthesize useBackCamera = _useBackCamera; 43 @synthesize useBackCamera = _useBackCamera;
41 44
42 @synthesize isRunning = _isRunning; 45 @synthesize isRunning = _isRunning;
43 @synthesize hasStarted = _hasStarted; 46 @synthesize hasStarted = _hasStarted;
44 47
45 // This is called from the thread that creates the video source, which is likely 48 // This is called from the thread that creates the video source, which is likely
46 // the main thread. 49 // the main thread.
47 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { 50 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer {
48 RTC_DCHECK(capturer); 51 RTC_DCHECK(capturer);
49 if (self = [super init]) { 52 if (self = [super init]) {
50 _capturer = capturer; 53 _capturer = capturer;
51 // Create the capture session and all relevant inputs and outputs. We need 54 // 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 55 // 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 56 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects
54 // created here are retained until dealloc and never recreated. 57 // created here are retained until dealloc and never recreated.
55 if (![self setupCaptureSession]) { 58 if (![self setupCaptureSession]) {
56 return nil; 59 return nil;
57 } 60 }
58 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 61 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
59 #if TARGET_OS_IPHONE 62 #if TARGET_OS_IPHONE
63 _orientation = UIDeviceOrientationPortrait;
60 [center addObserver:self 64 [center addObserver:self
61 selector:@selector(deviceOrientationDidChange:) 65 selector:@selector(deviceOrientationDidChange:)
62 name:UIDeviceOrientationDidChangeNotification 66 name:UIDeviceOrientationDidChangeNotification
63 object:nil]; 67 object:nil];
64 [center addObserver:self 68 [center addObserver:self
65 selector:@selector(handleCaptureSessionInterruption:) 69 selector:@selector(handleCaptureSessionInterruption:)
66 name:AVCaptureSessionWasInterruptedNotification 70 name:AVCaptureSessionWasInterruptedNotification
67 object:_captureSession]; 71 object:_captureSession];
68 [center addObserver:self 72 [center addObserver:self
69 selector:@selector(handleCaptureSessionInterruptionEnded:) 73 selector:@selector(handleCaptureSessionInterruptionEnded:)
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 _useBackCamera = useBackCamera; 154 _useBackCamera = useBackCamera;
151 [self updateSessionInputForUseBackCamera:useBackCamera]; 155 [self updateSessionInputForUseBackCamera:useBackCamera];
152 } 156 }
153 } 157 }
154 158
155 // Called from WebRTC thread. 159 // Called from WebRTC thread.
156 - (void)start { 160 - (void)start {
157 if (self.hasStarted) { 161 if (self.hasStarted) {
158 return; 162 return;
159 } 163 }
160 self.hasStarted = YES;
161 [RTCDispatcher 164 [RTCDispatcher
162 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 165 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
163 block:^{ 166 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]; 167 [self updateOrientation];
173 #if TARGET_OS_IPHONE 168 #if TARGET_OS_IPHONE
174 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications]; 169 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications];
175 #endif 170 #endif
176 AVCaptureSession *captureSession = self.captureSession; 171 AVCaptureSession *captureSession = self.captureSession;
177 [captureSession startRunning]; 172 [captureSession startRunning];
173 self.hasStarted = YES;
tkchin_webrtc 2017/07/24 21:30:37 This should be reverted.
jtt_webrtc 2017/07/24 22:29:51 Done.
178 }]; 174 }];
179 } 175 }
180 176
181 // Called from same thread as start. 177 // Called from same thread as start.
182 - (void)stop { 178 - (void)stop {
183 if (!self.hasStarted) { 179 if (!self.hasStarted) {
184 return; 180 return;
185 } 181 }
186 self.hasStarted = NO; 182 self.hasStarted = NO;
187 // Due to this async block, it's possible that the ObjC object outlives the 183 // Due to this async block, it's possible that the ObjC object outlives the
(...skipping 23 matching lines...) Expand all
211 207
212 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate 208 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
213 209
214 - (void)captureOutput:(AVCaptureOutput *)captureOutput 210 - (void)captureOutput:(AVCaptureOutput *)captureOutput
215 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 211 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
216 fromConnection:(AVCaptureConnection *)connection { 212 fromConnection:(AVCaptureConnection *)connection {
217 NSParameterAssert(captureOutput == _videoDataOutput); 213 NSParameterAssert(captureOutput == _videoDataOutput);
218 if (!self.hasStarted) { 214 if (!self.hasStarted) {
219 return; 215 return;
220 } 216 }
221 _capturer->CaptureSampleBuffer(sampleBuffer, _rotation); 217
218 #if TARGET_OS_IPHONE
219 // Default to portrait orientation on iPhone.
220 webrtc::VideoRotation rotation = webrtc::kVideoRotation_90;
221 AVCaptureDeviceInput *deviceInput =
222 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.first Object).input;
223 BOOL usingFrontCamera = deviceInput.device.position == AVCaptureDevicePosition Front;
224 // Check the image's EXIF for the actual camera the image came as the image co uld have been
225 // delayed as we set alwaysDiscardsLateVideoFrames to NO.
226 AVCaptureDevicePosition cameraPosition = [RTCImageHelper cameraFromSampleBuffe r:sampleBuffer];
227 if (cameraPosition != AVCaptureDevicePositionUnspecified) {
228 usingFrontCamera = cameraPosition == AVCaptureDevicePositionFront;
229 }
230 switch (_orientation) {
231 case UIDeviceOrientationPortrait:
232 rotation = webrtc::kVideoRotation_90;
233 break;
234 case UIDeviceOrientationPortraitUpsideDown:
235 rotation = webrtc::kVideoRotation_270;
236 break;
237 case UIDeviceOrientationLandscapeLeft:
238 rotation = usingFrontCamera ? webrtc::kVideoRotation_180 : webrtc::kVideoR otation_0;
239 break;
240 case UIDeviceOrientationLandscapeRight:
241 rotation = usingFrontCamera ? webrtc::kVideoRotation_0 : webrtc::kVideoRot ation_180;
242 break;
243 case UIDeviceOrientationFaceUp:
244 case UIDeviceOrientationFaceDown:
245 case UIDeviceOrientationUnknown:
246 // Ignore.
247 break;
248 }
249 #else
250 // No rotation on Mac.
251 webrtc::VideoRotation rotation = webrtc::kVideoRotation_0;
252 #endif
253
254 _capturer->CaptureSampleBuffer(sampleBuffer, rotation);
222 } 255 }
223 256
224 - (void)captureOutput:(AVCaptureOutput *)captureOutput 257 - (void)captureOutput:(AVCaptureOutput *)captureOutput
225 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer 258 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
226 fromConnection:(AVCaptureConnection *)connection { 259 fromConnection:(AVCaptureConnection *)connection {
227 RTCLogError(@"Dropped sample buffer."); 260 RTCLogError(@"Dropped sample buffer.");
228 } 261 }
229 262
230 #pragma mark - AVCaptureSession notifications 263 #pragma mark - AVCaptureSession notifications
231 264
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 return nil; 474 return nil;
442 } 475 }
443 _backCameraInput = backCameraInput; 476 _backCameraInput = backCameraInput;
444 } 477 }
445 return _backCameraInput; 478 return _backCameraInput;
446 } 479 }
447 480
448 // Called from capture session queue. 481 // Called from capture session queue.
449 - (void)updateOrientation { 482 - (void)updateOrientation {
450 #if TARGET_OS_IPHONE 483 #if TARGET_OS_IPHONE
451 switch ([UIDevice currentDevice].orientation) { 484 _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 485 #endif
473 } 486 }
474 487
475 // Update the current session input to match what's stored in _useBackCamera. 488 // Update the current session input to match what's stored in _useBackCamera.
476 - (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera { 489 - (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera {
477 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 490 [RTCDispatcher
478 block:^{ 491 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
479 [_captureSession beginConfiguration]; 492 block:^{
480 AVCaptureDeviceInput *oldInput = _backCameraI nput; 493 [_captureSession beginConfiguration];
481 AVCaptureDeviceInput *newInput = _frontCamera Input; 494 AVCaptureDeviceInput *oldInput = _backCameraInput;
482 if (useBackCamera) { 495 AVCaptureDeviceInput *newInput = _frontCameraInput;
483 oldInput = _frontCameraInput; 496 if (useBackCamera) {
484 newInput = _backCameraInput; 497 oldInput = _frontCameraInput;
485 } 498 newInput = _backCameraInput;
486 if (oldInput) { 499 }
487 // Ok to remove this even if it's not attac hed. Will be no-op. 500 if (oldInput) {
488 [_captureSession removeInput:oldInput]; 501 // Ok to remove this even if it's not attached. Will be no-op.
489 } 502 [_captureSession removeInput:oldInput];
490 if (newInput) { 503 }
491 [_captureSession addInput:newInput]; 504 if (newInput) {
492 } 505 [_captureSession addInput:newInput];
493 [self updateOrientation]; 506 }
494 AVCaptureDevice *newDevice = newInput.device; 507 [self updateOrientation];
495 const cricket::VideoFormat *format = 508 AVCaptureDevice *newDevice = newInput.device;
496 _capturer->GetCaptureFormat(); 509 const cricket::VideoFormat *format = _capturer->GetCapture Format();
497 webrtc::SetFormatForCaptureDevice( 510 webrtc::SetFormatForCaptureDevice(newDevice, _captureSessi on, *format);
498 newDevice, _captureSession, *format); 511 [_captureSession commitConfiguration];
499 [_captureSession commitConfiguration]; 512 }];
500 }];
501 } 513 }
502 514
503 @end 515 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698