OLD | NEW |
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 "WebRTC/RTCLogging.h" | 20 #import "WebRTC/RTCLogging.h" |
21 | 21 |
22 #include "avfoundationformatmapper.h" | 22 #include "avfoundationformatmapper.h" |
23 | 23 |
| 24 @interface RTCAVFoundationVideoCapturerInternal () |
| 25 @property(atomic, assign) BOOL changingCamera; // Whether we are switching came
ras. |
| 26 @end |
| 27 |
24 @implementation RTCAVFoundationVideoCapturerInternal { | 28 @implementation RTCAVFoundationVideoCapturerInternal { |
25 // Keep pointers to inputs for convenience. | 29 // Keep pointers to inputs for convenience. |
26 AVCaptureDeviceInput *_frontCameraInput; | 30 AVCaptureDeviceInput *_frontCameraInput; |
27 AVCaptureDeviceInput *_backCameraInput; | 31 AVCaptureDeviceInput *_backCameraInput; |
28 AVCaptureVideoDataOutput *_videoDataOutput; | 32 AVCaptureVideoDataOutput *_videoDataOutput; |
29 // The cricket::VideoCapturer that owns this class. Should never be NULL. | 33 // The cricket::VideoCapturer that owns this class. Should never be NULL. |
30 webrtc::AVFoundationVideoCapturer *_capturer; | 34 webrtc::AVFoundationVideoCapturer *_capturer; |
31 webrtc::VideoRotation _rotation; | |
32 BOOL _hasRetriedOnFatalError; | 35 BOOL _hasRetriedOnFatalError; |
33 BOOL _isRunning; | 36 BOOL _isRunning; |
34 BOOL _hasStarted; | 37 BOOL _hasStarted; |
35 rtc::CriticalSection _crit; | 38 rtc::CriticalSection _crit; |
| 39 BOOL _changingCamera; |
| 40 #if TARGET_OS_IPHONE |
| 41 UIDeviceOrientation _orientation; |
| 42 #endif |
36 } | 43 } |
37 | 44 |
38 @synthesize captureSession = _captureSession; | 45 @synthesize captureSession = _captureSession; |
39 @synthesize frameQueue = _frameQueue; | 46 @synthesize frameQueue = _frameQueue; |
40 @synthesize useBackCamera = _useBackCamera; | 47 @synthesize useBackCamera = _useBackCamera; |
41 | 48 |
42 @synthesize isRunning = _isRunning; | 49 @synthesize isRunning = _isRunning; |
43 @synthesize hasStarted = _hasStarted; | 50 @synthesize hasStarted = _hasStarted; |
| 51 @synthesize changingCamera = _changingCamera; |
44 | 52 |
45 // This is called from the thread that creates the video source, which is likely | 53 // This is called from the thread that creates the video source, which is likely |
46 // the main thread. | 54 // the main thread. |
47 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { | 55 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { |
48 RTC_DCHECK(capturer); | 56 RTC_DCHECK(capturer); |
49 if (self = [super init]) { | 57 if (self = [super init]) { |
50 _capturer = capturer; | 58 _capturer = capturer; |
51 // Create the capture session and all relevant inputs and outputs. We need | 59 // 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 | 60 // 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 | 61 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects |
54 // created here are retained until dealloc and never recreated. | 62 // created here are retained until dealloc and never recreated. |
55 if (![self setupCaptureSession]) { | 63 if (![self setupCaptureSession]) { |
56 return nil; | 64 return nil; |
57 } | 65 } |
| 66 self.changingCamera = NO; |
58 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | 67 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
59 #if TARGET_OS_IPHONE | 68 #if TARGET_OS_IPHONE |
| 69 _orientation = UIDeviceOrientationPortrait; |
60 [center addObserver:self | 70 [center addObserver:self |
61 selector:@selector(deviceOrientationDidChange:) | 71 selector:@selector(deviceOrientationDidChange:) |
62 name:UIDeviceOrientationDidChangeNotification | 72 name:UIDeviceOrientationDidChangeNotification |
63 object:nil]; | 73 object:nil]; |
64 [center addObserver:self | 74 [center addObserver:self |
65 selector:@selector(handleCaptureSessionInterruption:) | 75 selector:@selector(handleCaptureSessionInterruption:) |
66 name:AVCaptureSessionWasInterruptedNotification | 76 name:AVCaptureSessionWasInterruptedNotification |
67 object:_captureSession]; | 77 object:_captureSession]; |
68 [center addObserver:self | 78 [center addObserver:self |
69 selector:@selector(handleCaptureSessionInterruptionEnded:) | 79 selector:@selector(handleCaptureSessionInterruptionEnded:) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 _useBackCamera = useBackCamera; | 160 _useBackCamera = useBackCamera; |
151 [self updateSessionInputForUseBackCamera:useBackCamera]; | 161 [self updateSessionInputForUseBackCamera:useBackCamera]; |
152 } | 162 } |
153 } | 163 } |
154 | 164 |
155 // Called from WebRTC thread. | 165 // Called from WebRTC thread. |
156 - (void)start { | 166 - (void)start { |
157 if (self.hasStarted) { | 167 if (self.hasStarted) { |
158 return; | 168 return; |
159 } | 169 } |
160 self.hasStarted = YES; | |
161 [RTCDispatcher | 170 [RTCDispatcher |
162 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | 171 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
163 block:^{ | 172 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]; | 173 [self updateOrientation]; |
173 #if TARGET_OS_IPHONE | 174 #if TARGET_OS_IPHONE |
174 [[UIDevice currentDevice] beginGeneratingDeviceOrientati
onNotifications]; | 175 [[UIDevice currentDevice] beginGeneratingDeviceOrientati
onNotifications]; |
175 #endif | 176 #endif |
176 AVCaptureSession *captureSession = self.captureSession; | 177 AVCaptureSession *captureSession = self.captureSession; |
177 [captureSession startRunning]; | 178 [captureSession startRunning]; |
| 179 self.hasStarted = YES; |
178 }]; | 180 }]; |
179 } | 181 } |
180 | 182 |
181 // Called from same thread as start. | 183 // Called from same thread as start. |
182 - (void)stop { | 184 - (void)stop { |
183 if (!self.hasStarted) { | 185 if (!self.hasStarted) { |
184 return; | 186 return; |
185 } | 187 } |
186 self.hasStarted = NO; | 188 self.hasStarted = NO; |
187 // Due to this async block, it's possible that the ObjC object outlives the | 189 // Due to this async block, it's possible that the ObjC object outlives the |
(...skipping 20 matching lines...) Expand all Loading... |
208 }]; | 210 }]; |
209 } | 211 } |
210 #endif | 212 #endif |
211 | 213 |
212 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate | 214 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate |
213 | 215 |
214 - (void)captureOutput:(AVCaptureOutput *)captureOutput | 216 - (void)captureOutput:(AVCaptureOutput *)captureOutput |
215 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer | 217 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer |
216 fromConnection:(AVCaptureConnection *)connection { | 218 fromConnection:(AVCaptureConnection *)connection { |
217 NSParameterAssert(captureOutput == _videoDataOutput); | 219 NSParameterAssert(captureOutput == _videoDataOutput); |
218 if (!self.hasStarted) { | 220 if (self.changingCamera || !self.hasStarted) { |
219 return; | 221 return; |
220 } | 222 } |
221 _capturer->CaptureSampleBuffer(sampleBuffer, _rotation); | 223 |
| 224 #if TARGET_OS_IPHONE |
| 225 // Default to portrait orientation on iPhone. |
| 226 webrtc::VideoRotation rotation = webrtc::kVideoRotation_90; |
| 227 AVCaptureDeviceInput *deviceInput = |
| 228 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.first
Object).input; |
| 229 BOOL usingFrontCamera = deviceInput.device.position == AVCaptureDevicePosition
Front; |
| 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 Loading... |
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 self.changingCamera = YES; |
481 AVCaptureDeviceInput *newInput = _frontCamera
Input; | 494 [_captureSession beginConfiguration]; |
482 if (useBackCamera) { | 495 AVCaptureDeviceInput *oldInput = _backCameraInput; |
483 oldInput = _frontCameraInput; | 496 AVCaptureDeviceInput *newInput = _frontCameraInput; |
484 newInput = _backCameraInput; | 497 if (useBackCamera) { |
485 } | 498 oldInput = _frontCameraInput; |
486 if (oldInput) { | 499 newInput = _backCameraInput; |
487 // Ok to remove this even if it's not attac
hed. Will be no-op. | 500 } |
488 [_captureSession removeInput:oldInput]; | 501 if (oldInput) { |
489 } | 502 // Ok to remove this even if it's not attached. Will be
no-op. |
490 if (newInput) { | 503 [_captureSession removeInput:oldInput]; |
491 [_captureSession addInput:newInput]; | 504 } |
492 } | 505 if (newInput) { |
493 [self updateOrientation]; | 506 [_captureSession addInput:newInput]; |
494 AVCaptureDevice *newDevice = newInput.device; | 507 } |
495 const cricket::VideoFormat *format = | 508 [self updateOrientation]; |
496 _capturer->GetCaptureFormat(); | 509 AVCaptureDevice *newDevice = newInput.device; |
497 webrtc::SetFormatForCaptureDevice( | 510 const cricket::VideoFormat *format = _capturer->GetCapture
Format(); |
498 newDevice, _captureSession, *format); | 511 webrtc::SetFormatForCaptureDevice(newDevice, _captureSessi
on, *format); |
499 [_captureSession commitConfiguration]; | 512 [_captureSession commitConfiguration]; |
500 }]; | 513 self.changingCamera = NO; |
| 514 }]; |
501 } | 515 } |
502 | 516 |
503 @end | 517 @end |
OLD | NEW |