| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 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 | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #import <Foundation/Foundation.h> | |
| 12 | |
| 13 #import "WebRTC/RTCCameraVideoCapturer.h" | |
| 14 #import "WebRTC/RTCLogging.h" | |
| 15 #import "WebRTC/RTCVideoFrameBuffer.h" | |
| 16 | |
| 17 #if TARGET_OS_IPHONE | |
| 18 #import "WebRTC/UIDevice+RTCDevice.h" | |
| 19 #endif | |
| 20 | |
| 21 #import "AVCaptureSession+DevicePosition.h" | |
| 22 #import "RTCDispatcher+Private.h" | |
| 23 | |
| 24 const int64_t kNanosecondsPerSecond = 1000000000; | |
| 25 | |
| 26 static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) { | |
| 27 return (mediaSubType == kCVPixelFormatType_420YpCbCr8PlanarFullRange || | |
| 28 mediaSubType == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange); | |
| 29 } | |
| 30 | |
| 31 @interface RTCCameraVideoCapturer ()<AVCaptureVideoDataOutputSampleBufferDelegat
e> | |
| 32 @property(nonatomic, readonly) dispatch_queue_t frameQueue; | |
| 33 @end | |
| 34 | |
| 35 @implementation RTCCameraVideoCapturer { | |
| 36 AVCaptureVideoDataOutput *_videoDataOutput; | |
| 37 AVCaptureSession *_captureSession; | |
| 38 AVCaptureDevice *_currentDevice; | |
| 39 BOOL _hasRetriedOnFatalError; | |
| 40 BOOL _isRunning; | |
| 41 // Will the session be running once all asynchronous operations have been comp
leted? | |
| 42 BOOL _willBeRunning; | |
| 43 #if TARGET_OS_IPHONE | |
| 44 UIDeviceOrientation _orientation; | |
| 45 #endif | |
| 46 } | |
| 47 | |
| 48 @synthesize frameQueue = _frameQueue; | |
| 49 @synthesize captureSession = _captureSession; | |
| 50 | |
| 51 - (instancetype)initWithDelegate:(__weak id<RTCVideoCapturerDelegate>)delegate { | |
| 52 if (self = [super initWithDelegate:delegate]) { | |
| 53 // Create the capture session and all relevant inputs and outputs. We need | |
| 54 // to do this in init because the application may want the capture session | |
| 55 // before we start the capturer for e.g. AVCapturePreviewLayer. All objects | |
| 56 // created here are retained until dealloc and never recreated. | |
| 57 if (![self setupCaptureSession]) { | |
| 58 return nil; | |
| 59 } | |
| 60 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
| 61 #if TARGET_OS_IPHONE | |
| 62 _orientation = UIDeviceOrientationPortrait; | |
| 63 [center addObserver:self | |
| 64 selector:@selector(deviceOrientationDidChange:) | |
| 65 name:UIDeviceOrientationDidChangeNotification | |
| 66 object:nil]; | |
| 67 [center addObserver:self | |
| 68 selector:@selector(handleCaptureSessionInterruption:) | |
| 69 name:AVCaptureSessionWasInterruptedNotification | |
| 70 object:_captureSession]; | |
| 71 [center addObserver:self | |
| 72 selector:@selector(handleCaptureSessionInterruptionEnded:) | |
| 73 name:AVCaptureSessionInterruptionEndedNotification | |
| 74 object:_captureSession]; | |
| 75 [center addObserver:self | |
| 76 selector:@selector(handleApplicationDidBecomeActive:) | |
| 77 name:UIApplicationDidBecomeActiveNotification | |
| 78 object:[UIApplication sharedApplication]]; | |
| 79 #endif | |
| 80 [center addObserver:self | |
| 81 selector:@selector(handleCaptureSessionRuntimeError:) | |
| 82 name:AVCaptureSessionRuntimeErrorNotification | |
| 83 object:_captureSession]; | |
| 84 [center addObserver:self | |
| 85 selector:@selector(handleCaptureSessionDidStartRunning:) | |
| 86 name:AVCaptureSessionDidStartRunningNotification | |
| 87 object:_captureSession]; | |
| 88 [center addObserver:self | |
| 89 selector:@selector(handleCaptureSessionDidStopRunning:) | |
| 90 name:AVCaptureSessionDidStopRunningNotification | |
| 91 object:_captureSession]; | |
| 92 } | |
| 93 return self; | |
| 94 } | |
| 95 | |
| 96 - (void)dealloc { | |
| 97 NSAssert( | |
| 98 !_willBeRunning, | |
| 99 @"Session was still running in RTCCameraVideoCapturer dealloc. Forgot to c
all stopCapture?"); | |
| 100 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 101 } | |
| 102 | |
| 103 + (NSArray<AVCaptureDevice *> *)captureDevices { | |
| 104 return [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; | |
| 105 } | |
| 106 | |
| 107 + (NSArray<AVCaptureDeviceFormat *> *)supportedFormatsForDevice:(AVCaptureDevice
*)device { | |
| 108 NSMutableArray<AVCaptureDeviceFormat *> *eligibleDeviceFormats = [NSMutableArr
ay array]; | |
| 109 | |
| 110 for (AVCaptureDeviceFormat *format in device.formats) { | |
| 111 // Filter out subTypes that we currently don't support in the stack | |
| 112 FourCharCode mediaSubType = CMFormatDescriptionGetMediaSubType(format.format
Description); | |
| 113 if (IsMediaSubTypeSupported(mediaSubType)) { | |
| 114 [eligibleDeviceFormats addObject:format]; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 return eligibleDeviceFormats; | |
| 119 } | |
| 120 | |
| 121 - (void)startCaptureWithDevice:(AVCaptureDevice *)device | |
| 122 format:(AVCaptureDeviceFormat *)format | |
| 123 fps:(NSInteger)fps { | |
| 124 _willBeRunning = YES; | |
| 125 [RTCDispatcher | |
| 126 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 127 block:^{ | |
| 128 RTCLogInfo("startCaptureWithDevice %@ @ %ld fps", format,
(long)fps); | |
| 129 | |
| 130 #if TARGET_OS_IPHONE | |
| 131 [[UIDevice currentDevice] beginGeneratingDeviceOrientation
Notifications]; | |
| 132 #endif | |
| 133 | |
| 134 _currentDevice = device; | |
| 135 | |
| 136 NSError *error = nil; | |
| 137 if (![_currentDevice lockForConfiguration:&error]) { | |
| 138 RTCLogError( | |
| 139 @"Failed to lock device %@. Error: %@", _currentDevi
ce, error.userInfo); | |
| 140 return; | |
| 141 } | |
| 142 [self reconfigureCaptureSessionInput]; | |
| 143 [self updateOrientation]; | |
| 144 [_captureSession startRunning]; | |
| 145 [self updateDeviceCaptureFormat:format fps:fps]; | |
| 146 [_currentDevice unlockForConfiguration]; | |
| 147 _isRunning = YES; | |
| 148 }]; | |
| 149 } | |
| 150 | |
| 151 - (void)stopCapture { | |
| 152 _willBeRunning = NO; | |
| 153 [RTCDispatcher | |
| 154 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 155 block:^{ | |
| 156 RTCLogInfo("Stop"); | |
| 157 _currentDevice = nil; | |
| 158 for (AVCaptureDeviceInput *oldInput in [_captureSession.in
puts copy]) { | |
| 159 [_captureSession removeInput:oldInput]; | |
| 160 } | |
| 161 [_captureSession stopRunning]; | |
| 162 | |
| 163 #if TARGET_OS_IPHONE | |
| 164 [[UIDevice currentDevice] endGeneratingDeviceOrientationNo
tifications]; | |
| 165 #endif | |
| 166 _isRunning = NO; | |
| 167 }]; | |
| 168 } | |
| 169 | |
| 170 #pragma mark iOS notifications | |
| 171 | |
| 172 #if TARGET_OS_IPHONE | |
| 173 - (void)deviceOrientationDidChange:(NSNotification *)notification { | |
| 174 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 175 block:^{ | |
| 176 [self updateOrientation]; | |
| 177 }]; | |
| 178 } | |
| 179 #endif | |
| 180 | |
| 181 #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate | |
| 182 | |
| 183 - (void)captureOutput:(AVCaptureOutput *)captureOutput | |
| 184 didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer | |
| 185 fromConnection:(AVCaptureConnection *)connection { | |
| 186 NSParameterAssert(captureOutput == _videoDataOutput); | |
| 187 | |
| 188 if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(s
ampleBuffer) || | |
| 189 !CMSampleBufferDataIsReady(sampleBuffer)) { | |
| 190 return; | |
| 191 } | |
| 192 | |
| 193 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); | |
| 194 if (pixelBuffer == nil) { | |
| 195 return; | |
| 196 } | |
| 197 | |
| 198 #if TARGET_OS_IPHONE | |
| 199 // Default to portrait orientation on iPhone. | |
| 200 RTCVideoRotation rotation = RTCVideoRotation_90; | |
| 201 BOOL usingFrontCamera = NO; | |
| 202 // Check the image's EXIF for the camera the image came from as the image coul
d have been | |
| 203 // delayed as we set alwaysDiscardsLateVideoFrames to NO. | |
| 204 AVCaptureDevicePosition cameraPosition = | |
| 205 [AVCaptureSession devicePositionForSampleBuffer:sampleBuffer]; | |
| 206 if (cameraPosition != AVCaptureDevicePositionUnspecified) { | |
| 207 usingFrontCamera = AVCaptureDevicePositionFront == cameraPosition; | |
| 208 } else { | |
| 209 AVCaptureDeviceInput *deviceInput = | |
| 210 (AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.fir
stObject).input; | |
| 211 usingFrontCamera = AVCaptureDevicePositionFront == deviceInput.device.positi
on; | |
| 212 } | |
| 213 switch (_orientation) { | |
| 214 case UIDeviceOrientationPortrait: | |
| 215 rotation = RTCVideoRotation_90; | |
| 216 break; | |
| 217 case UIDeviceOrientationPortraitUpsideDown: | |
| 218 rotation = RTCVideoRotation_270; | |
| 219 break; | |
| 220 case UIDeviceOrientationLandscapeLeft: | |
| 221 rotation = usingFrontCamera ? RTCVideoRotation_180 : RTCVideoRotation_0; | |
| 222 break; | |
| 223 case UIDeviceOrientationLandscapeRight: | |
| 224 rotation = usingFrontCamera ? RTCVideoRotation_0 : RTCVideoRotation_180; | |
| 225 break; | |
| 226 case UIDeviceOrientationFaceUp: | |
| 227 case UIDeviceOrientationFaceDown: | |
| 228 case UIDeviceOrientationUnknown: | |
| 229 // Ignore. | |
| 230 break; | |
| 231 } | |
| 232 #else | |
| 233 // No rotation on Mac. | |
| 234 RTCVideoRotation rotation = RTCVideoRotation_0; | |
| 235 #endif | |
| 236 | |
| 237 RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuff
er:pixelBuffer]; | |
| 238 int64_t timeStampNs = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(
sampleBuffer)) * | |
| 239 kNanosecondsPerSecond; | |
| 240 RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuff
er | |
| 241 rotation:rotation | |
| 242 timeStampNs:timeStampNs]
; | |
| 243 [self.delegate capturer:self didCaptureVideoFrame:videoFrame]; | |
| 244 } | |
| 245 | |
| 246 - (void)captureOutput:(AVCaptureOutput *)captureOutput | |
| 247 didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer | |
| 248 fromConnection:(AVCaptureConnection *)connection { | |
| 249 RTCLogError(@"Dropped sample buffer."); | |
| 250 } | |
| 251 | |
| 252 #pragma mark - AVCaptureSession notifications | |
| 253 | |
| 254 - (void)handleCaptureSessionInterruption:(NSNotification *)notification { | |
| 255 NSString *reasonString = nil; | |
| 256 #if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \ | |
| 257 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 | |
| 258 if ([UIDevice isIOS9OrLater]) { | |
| 259 NSNumber *reason = notification.userInfo[AVCaptureSessionInterruptionReasonK
ey]; | |
| 260 if (reason) { | |
| 261 switch (reason.intValue) { | |
| 262 case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableInBackgrou
nd: | |
| 263 reasonString = @"VideoDeviceNotAvailableInBackground"; | |
| 264 break; | |
| 265 case AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient: | |
| 266 reasonString = @"AudioDeviceInUseByAnotherClient"; | |
| 267 break; | |
| 268 case AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient: | |
| 269 reasonString = @"VideoDeviceInUseByAnotherClient"; | |
| 270 break; | |
| 271 case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultip
leForegroundApps: | |
| 272 reasonString = @"VideoDeviceNotAvailableWithMultipleForegroundApps"; | |
| 273 break; | |
| 274 } | |
| 275 } | |
| 276 } | |
| 277 #endif | |
| 278 RTCLog(@"Capture session interrupted: %@", reasonString); | |
| 279 } | |
| 280 | |
| 281 - (void)handleCaptureSessionInterruptionEnded:(NSNotification *)notification { | |
| 282 RTCLog(@"Capture session interruption ended."); | |
| 283 } | |
| 284 | |
| 285 - (void)handleCaptureSessionRuntimeError:(NSNotification *)notification { | |
| 286 NSError *error = [notification.userInfo objectForKey:AVCaptureSessionErrorKey]
; | |
| 287 RTCLogError(@"Capture session runtime error: %@", error); | |
| 288 | |
| 289 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 290 block:^{ | |
| 291 #if TARGET_OS_IPHONE | |
| 292 if (error.code == AVErrorMediaServicesWereReset
) { | |
| 293 [self handleNonFatalError]; | |
| 294 } else { | |
| 295 [self handleFatalError]; | |
| 296 } | |
| 297 #else | |
| 298 [self handleFatalError]; | |
| 299 #endif | |
| 300 }]; | |
| 301 } | |
| 302 | |
| 303 - (void)handleCaptureSessionDidStartRunning:(NSNotification *)notification { | |
| 304 RTCLog(@"Capture session started."); | |
| 305 | |
| 306 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 307 block:^{ | |
| 308 // If we successfully restarted after an unknow
n error, | |
| 309 // allow future retries on fatal errors. | |
| 310 _hasRetriedOnFatalError = NO; | |
| 311 }]; | |
| 312 } | |
| 313 | |
| 314 - (void)handleCaptureSessionDidStopRunning:(NSNotification *)notification { | |
| 315 RTCLog(@"Capture session stopped."); | |
| 316 } | |
| 317 | |
| 318 - (void)handleFatalError { | |
| 319 [RTCDispatcher | |
| 320 dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 321 block:^{ | |
| 322 if (!_hasRetriedOnFatalError) { | |
| 323 RTCLogWarning(@"Attempting to recover from fatal capture
error."); | |
| 324 [self handleNonFatalError]; | |
| 325 _hasRetriedOnFatalError = YES; | |
| 326 } else { | |
| 327 RTCLogError(@"Previous fatal error recovery failed."); | |
| 328 } | |
| 329 }]; | |
| 330 } | |
| 331 | |
| 332 - (void)handleNonFatalError { | |
| 333 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 334 block:^{ | |
| 335 RTCLog(@"Restarting capture session after error
."); | |
| 336 if (_isRunning) { | |
| 337 [_captureSession startRunning]; | |
| 338 } | |
| 339 }]; | |
| 340 } | |
| 341 | |
| 342 #if TARGET_OS_IPHONE | |
| 343 | |
| 344 #pragma mark - UIApplication notifications | |
| 345 | |
| 346 - (void)handleApplicationDidBecomeActive:(NSNotification *)notification { | |
| 347 [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession | |
| 348 block:^{ | |
| 349 if (_isRunning && !_captureSession.isRunning) { | |
| 350 RTCLog(@"Restarting capture session on active
."); | |
| 351 [_captureSession startRunning]; | |
| 352 } | |
| 353 }]; | |
| 354 } | |
| 355 | |
| 356 #endif // TARGET_OS_IPHONE | |
| 357 | |
| 358 #pragma mark - Private | |
| 359 | |
| 360 - (dispatch_queue_t)frameQueue { | |
| 361 if (!_frameQueue) { | |
| 362 _frameQueue = | |
| 363 dispatch_queue_create("org.webrtc.avfoundationvideocapturer.video", DISP
ATCH_QUEUE_SERIAL); | |
| 364 dispatch_set_target_queue(_frameQueue, | |
| 365 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_
HIGH, 0)); | |
| 366 } | |
| 367 return _frameQueue; | |
| 368 } | |
| 369 | |
| 370 - (BOOL)setupCaptureSession { | |
| 371 NSAssert(_captureSession == nil, @"Setup capture session called twice."); | |
| 372 _captureSession = [[AVCaptureSession alloc] init]; | |
| 373 #if defined(WEBRTC_IOS) | |
| 374 _captureSession.sessionPreset = AVCaptureSessionPresetInputPriority; | |
| 375 _captureSession.usesApplicationAudioSession = NO; | |
| 376 #endif | |
| 377 [self setupVideoDataOutput]; | |
| 378 // Add the output. | |
| 379 if (![_captureSession canAddOutput:_videoDataOutput]) { | |
| 380 RTCLogError(@"Video data output unsupported."); | |
| 381 return NO; | |
| 382 } | |
| 383 [_captureSession addOutput:_videoDataOutput]; | |
| 384 | |
| 385 return YES; | |
| 386 } | |
| 387 | |
| 388 - (void)setupVideoDataOutput { | |
| 389 NSAssert(_videoDataOutput == nil, @"Setup video data output called twice."); | |
| 390 // Make the capturer output NV12. Ideally we want I420 but that's not | |
| 391 // currently supported on iPhone / iPad. | |
| 392 AVCaptureVideoDataOutput *videoDataOutput = [[AVCaptureVideoDataOutput alloc]
init]; | |
| 393 videoDataOutput.videoSettings = @{ | |
| 394 (NSString *) | |
| 395 // TODO(denicija): Remove this color conversion and use the original capture
format directly. | |
| 396 kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFu
llRange) | |
| 397 }; | |
| 398 videoDataOutput.alwaysDiscardsLateVideoFrames = NO; | |
| 399 [videoDataOutput setSampleBufferDelegate:self queue:self.frameQueue]; | |
| 400 _videoDataOutput = videoDataOutput; | |
| 401 } | |
| 402 | |
| 403 #pragma mark - Private, called inside capture queue | |
| 404 | |
| 405 - (void)updateDeviceCaptureFormat:(AVCaptureDeviceFormat *)format fps:(NSInteger
)fps { | |
| 406 NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession], | |
| 407 @"updateDeviceCaptureFormat must be called on the capture queue."); | |
| 408 @try { | |
| 409 _currentDevice.activeFormat = format; | |
| 410 _currentDevice.activeVideoMinFrameDuration = CMTimeMake(1, fps); | |
| 411 _currentDevice.activeVideoMaxFrameDuration = CMTimeMake(1, fps); | |
| 412 } @catch (NSException *exception) { | |
| 413 RTCLogError(@"Failed to set active format!\n User info:%@", exception.userIn
fo); | |
| 414 return; | |
| 415 } | |
| 416 } | |
| 417 | |
| 418 - (void)reconfigureCaptureSessionInput { | |
| 419 NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession], | |
| 420 @"reconfigureCaptureSessionInput must be called on the capture queue.
"); | |
| 421 NSError *error = nil; | |
| 422 AVCaptureDeviceInput *input = | |
| 423 [AVCaptureDeviceInput deviceInputWithDevice:_currentDevice error:&error]; | |
| 424 if (!input) { | |
| 425 RTCLogError(@"Failed to create front camera input: %@", error.localizedDescr
iption); | |
| 426 return; | |
| 427 } | |
| 428 [_captureSession beginConfiguration]; | |
| 429 for (AVCaptureDeviceInput *oldInput in [_captureSession.inputs copy]) { | |
| 430 [_captureSession removeInput:oldInput]; | |
| 431 } | |
| 432 if ([_captureSession canAddInput:input]) { | |
| 433 [_captureSession addInput:input]; | |
| 434 } else { | |
| 435 RTCLogError(@"Cannot add camera as an input to the session."); | |
| 436 } | |
| 437 [_captureSession commitConfiguration]; | |
| 438 } | |
| 439 | |
| 440 - (void)updateOrientation { | |
| 441 NSAssert([RTCDispatcher isOnQueueForType:RTCDispatcherTypeCaptureSession], | |
| 442 @"updateOrientation must be called on the capture queue."); | |
| 443 #if TARGET_OS_IPHONE | |
| 444 _orientation = [UIDevice currentDevice].orientation; | |
| 445 #endif | |
| 446 } | |
| 447 | |
| 448 @end | |
| OLD | NEW |