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 |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 #include "avfoundationformatmapper.h" | 22 #include "avfoundationformatmapper.h" |
23 | 23 |
24 @implementation RTCAVFoundationVideoCapturerInternal { | 24 @implementation RTCAVFoundationVideoCapturerInternal { |
25 // Keep pointers to inputs for convenience. | 25 // Keep pointers to inputs for convenience. |
26 AVCaptureDeviceInput *_frontCameraInput; | 26 AVCaptureDeviceInput *_frontCameraInput; |
27 AVCaptureDeviceInput *_backCameraInput; | 27 AVCaptureDeviceInput *_backCameraInput; |
28 AVCaptureVideoDataOutput *_videoDataOutput; | 28 AVCaptureVideoDataOutput *_videoDataOutput; |
29 // The cricket::VideoCapturer that owns this class. Should never be NULL. | 29 // The cricket::VideoCapturer that owns this class. Should never be NULL. |
30 webrtc::AVFoundationVideoCapturer *_capturer; | 30 webrtc::AVFoundationVideoCapturer *_capturer; |
31 webrtc::VideoRotation _rotation; | |
32 BOOL _hasRetriedOnFatalError; | 31 BOOL _hasRetriedOnFatalError; |
33 BOOL _isRunning; | 32 BOOL _isRunning; |
34 BOOL _hasStarted; | 33 BOOL _hasStarted; |
35 rtc::CriticalSection _crit; | 34 rtc::CriticalSection _crit; |
| 35 #if TARGET_OS_IPHONE |
| 36 UIDeviceOrientation _orientation; |
| 37 #endif |
36 } | 38 } |
37 | 39 |
38 @synthesize captureSession = _captureSession; | 40 @synthesize captureSession = _captureSession; |
39 @synthesize frameQueue = _frameQueue; | 41 @synthesize frameQueue = _frameQueue; |
40 @synthesize useBackCamera = _useBackCamera; | 42 @synthesize useBackCamera = _useBackCamera; |
41 | 43 |
42 @synthesize isRunning = _isRunning; | 44 @synthesize isRunning = _isRunning; |
43 @synthesize hasStarted = _hasStarted; | 45 @synthesize hasStarted = _hasStarted; |
44 | 46 |
45 // This is called from the thread that creates the video source, which is likely | 47 // This is called from the thread that creates the video source, which is likely |
46 // the main thread. | 48 // the main thread. |
47 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { | 49 - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { |
48 RTC_DCHECK(capturer); | 50 RTC_DCHECK(capturer); |
49 if (self = [super init]) { | 51 if (self = [super init]) { |
50 _capturer = capturer; | 52 _capturer = capturer; |
51 // 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 |
52 // 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 |
53 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects | 55 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects |
54 // created here are retained until dealloc and never recreated. | 56 // created here are retained until dealloc and never recreated. |
55 if (![self setupCaptureSession]) { | 57 if (![self setupCaptureSession]) { |
56 return nil; | 58 return nil; |
57 } | 59 } |
58 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | 60 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
59 #if TARGET_OS_IPHONE | 61 #if TARGET_OS_IPHONE |
| 62 _orientation = UIDeviceOrientationPortrait; |
60 [center addObserver:self | 63 [center addObserver:self |
61 selector:@selector(deviceOrientationDidChange:) | 64 selector:@selector(deviceOrientationDidChange:) |
62 name:UIDeviceOrientationDidChangeNotification | 65 name:UIDeviceOrientationDidChangeNotification |
63 object:nil]; | 66 object:nil]; |
64 [center addObserver:self | 67 [center addObserver:self |
65 selector:@selector(handleCaptureSessionInterruption:) | 68 selector:@selector(handleCaptureSessionInterruption:) |
66 name:AVCaptureSessionWasInterruptedNotification | 69 name:AVCaptureSessionWasInterruptedNotification |
67 object:_captureSession]; | 70 object:_captureSession]; |
68 [center addObserver:self | 71 [center addObserver:self |
69 selector:@selector(handleCaptureSessionInterruptionEnded:) | 72 selector:@selector(handleCaptureSessionInterruptionEnded:) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 _useBackCamera = useBackCamera; | 153 _useBackCamera = useBackCamera; |
151 [self updateSessionInputForUseBackCamera:useBackCamera]; | 154 [self updateSessionInputForUseBackCamera:useBackCamera]; |
152 } | 155 } |
153 } | 156 } |
154 | 157 |
155 // Called from WebRTC thread. | 158 // Called from WebRTC thread. |
156 - (void)start { | 159 - (void)start { |
157 if (self.hasStarted) { | 160 if (self.hasStarted) { |
158 return; | 161 return; |
159 } | 162 } |
160 self.hasStarted = YES; | 163 self.hasStarted = NO; |
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; |
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 20 matching lines...) Expand all Loading... |
208 }]; | 204 }]; |
209 } | 205 } |
210 #endif | 206 #endif |
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 || !self.isRunning || !connection.enabled || |
| 215 !connection.active) { |
219 return; | 216 return; |
220 } | 217 } |
221 _capturer->CaptureSampleBuffer(sampleBuffer, _rotation); | 218 |
| 219 #if TARGET_OS_IPHONE |
| 220 // Default to portrait orientation on iPhone. |
| 221 webrtc::VideoRotation rotation = webrtc::kVideoRotation_90; |
| 222 AVCaptureDeviceInput *deviceInput = |
| 223 (AVCaptureDeviceInput *)((AVCaptureInputPort *) |
| 224 connection.inputPorts.firstObject) |
| 225 .input; |
| 226 BOOL usingFrontCamera = |
| 227 deviceInput.device.position == AVCaptureDevicePositionFront; |
| 228 switch (_orientation) { |
| 229 case UIDeviceOrientationPortrait: |
| 230 rotation = webrtc::kVideoRotation_90; |
| 231 break; |
| 232 case UIDeviceOrientationPortraitUpsideDown: |
| 233 rotation = webrtc::kVideoRotation_270; |
| 234 break; |
| 235 case UIDeviceOrientationLandscapeLeft: |
| 236 rotation = usingFrontCamera ? webrtc::kVideoRotation_180 |
| 237 : webrtc::kVideoRotation_0; |
| 238 break; |
| 239 case UIDeviceOrientationLandscapeRight: |
| 240 rotation = usingFrontCamera ? webrtc::kVideoRotation_0 |
| 241 : webrtc::kVideoRotation_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 { |
| 490 self.isRunning = NO; |
477 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | 491 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
478 block:^{ | 492 block:^{ |
479 [_captureSession beginConfiguration]; | 493 [_captureSession beginConfiguration]; |
480 AVCaptureDeviceInput *oldInput = _backCameraI
nput; | 494 AVCaptureDeviceInput *oldInput = _backCameraI
nput; |
481 AVCaptureDeviceInput *newInput = _frontCamera
Input; | 495 AVCaptureDeviceInput *newInput = _frontCamera
Input; |
482 if (useBackCamera) { | 496 if (useBackCamera) { |
483 oldInput = _frontCameraInput; | 497 oldInput = _frontCameraInput; |
484 newInput = _backCameraInput; | 498 newInput = _backCameraInput; |
485 } | 499 } |
486 if (oldInput) { | 500 if (oldInput) { |
487 // Ok to remove this even if it's not attac
hed. Will be no-op. | 501 // Ok to remove this even if it's not attac
hed. Will be no-op. |
488 [_captureSession removeInput:oldInput]; | 502 [_captureSession removeInput:oldInput]; |
489 } | 503 } |
490 if (newInput) { | 504 if (newInput) { |
491 [_captureSession addInput:newInput]; | 505 [_captureSession addInput:newInput]; |
492 } | 506 } |
493 [self updateOrientation]; | 507 [self updateOrientation]; |
494 AVCaptureDevice *newDevice = newInput.device; | 508 AVCaptureDevice *newDevice = newInput.device; |
495 const cricket::VideoFormat *format = | 509 const cricket::VideoFormat *format = |
496 _capturer->GetCaptureFormat(); | 510 _capturer->GetCaptureFormat(); |
497 webrtc::SetFormatForCaptureDevice( | 511 webrtc::SetFormatForCaptureDevice( |
498 newDevice, _captureSession, *format); | 512 newDevice, _captureSession, *format); |
499 [_captureSession commitConfiguration]; | 513 [_captureSession commitConfiguration]; |
| 514 self.isRunning = YES; |
500 }]; | 515 }]; |
501 } | 516 } |
502 | 517 |
503 @end | 518 @end |
OLD | NEW |