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

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: Refactored checking of image's source device. 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 "AVCaptureSession+Device.h"
19 #import "RTCDispatcher+Private.h" 20 #import "RTCDispatcher+Private.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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 164 self.hasStarted = YES;
161 [RTCDispatcher 165 [RTCDispatcher
162 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession 166 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
163 block:^{ 167 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]; 168 [self updateOrientation];
173 #if TARGET_OS_IPHONE 169 #if TARGET_OS_IPHONE
174 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications]; 170 [[UIDevice currentDevice] beginGeneratingDeviceOrientati onNotifications];
175 #endif 171 #endif
176 AVCaptureSession *captureSession = self.captureSession; 172 AVCaptureSession *captureSession = self.captureSession;
177 [captureSession startRunning]; 173 [captureSession startRunning];
178 }]; 174 }];
179 } 175 }
180 176
181 // Called from same thread as start. 177 // Called from same thread as start.
(...skipping 29 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 BOOL usingFrontCamera;
tkchin_webrtc 2017/07/25 19:33:30 init to NO
222 // Check the image's EXIF for the camera the image came from as the image coul d have been
223 // delayed as we set alwaysDiscardsLateVideoFrames to NO.
224 AVCaptureDevicePosition cameraPosition =
225 [AVCaptureSession devicePositionForSampleBuffer:sampleBuffer];
226 if (cameraPosition != AVCaptureDevicePositionUnspecified) {
227 usingFrontCamera = AVCaptureDevicePositionFront == cameraPosition;
228 } else {
229 AVCaptureDeviceInput *deviceInput =
230 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.fir stObject).input;
231 usingFrontCamera = AVCaptureDevicePositionFront == deviceInput.device.positi on;
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 [_captureSession beginConfiguration];
481 AVCaptureDeviceInput *newInput = _frontCamera Input; 497 AVCaptureDeviceInput *oldInput = _backCameraInput;
482 if (useBackCamera) { 498 AVCaptureDeviceInput *newInput = _frontCameraInput;
483 oldInput = _frontCameraInput; 499 if (useBackCamera) {
484 newInput = _backCameraInput; 500 oldInput = _frontCameraInput;
485 } 501 newInput = _backCameraInput;
486 if (oldInput) { 502 }
487 // Ok to remove this even if it's not attac hed. Will be no-op. 503 if (oldInput) {
488 [_captureSession removeInput:oldInput]; 504 // Ok to remove this even if it's not attached. Will be no-op.
489 } 505 [_captureSession removeInput:oldInput];
490 if (newInput) { 506 }
491 [_captureSession addInput:newInput]; 507 if (newInput) {
492 } 508 [_captureSession addInput:newInput];
493 [self updateOrientation]; 509 }
494 AVCaptureDevice *newDevice = newInput.device; 510 [self updateOrientation];
495 const cricket::VideoFormat *format = 511 AVCaptureDevice *newDevice = newInput.device;
496 _capturer->GetCaptureFormat(); 512 const cricket::VideoFormat *format = _capturer->GetCapture Format();
497 webrtc::SetFormatForCaptureDevice( 513 webrtc::SetFormatForCaptureDevice(newDevice, _captureSessi on, *format);
498 newDevice, _captureSession, *format); 514 [_captureSession commitConfiguration];
499 [_captureSession commitConfiguration]; 515 }];
500 }];
501 } 516 }
502 517
503 @end 518 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698