Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #include "avfoundationvideocapturer.h" |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 12 | |
| 13 | #import <AVFoundation/AVFoundation.h> |
| 14 | #import <Foundation/Foundation.h> |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 15 | #if TARGET_OS_IPHONE |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 16 | #import <UIKit/UIKit.h> |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 17 | #endif |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 18 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 19 | #import "RTCDispatcher+Private.h" |
| 20 | #import "WebRTC/RTCLogging.h" |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 21 | #if TARGET_OS_IPHONE |
| 22 | #import "WebRTC/UIDevice+RTCDevice.h" |
| 23 | #endif |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 24 | |
| 25 | #include "webrtc/base/bind.h" |
| 26 | #include "webrtc/base/checks.h" |
| 27 | #include "webrtc/base/thread.h" |
magjed | 39607c9 | 2016-07-14 08:12:17 -0700 | [diff] [blame] | 28 | #include "webrtc/common_video/include/corevideo_frame_buffer.h" |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 29 | |
| 30 | // TODO(tkchin): support other formats. |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 31 | static NSString *const kDefaultPreset = AVCaptureSessionPreset640x480; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 32 | static cricket::VideoFormat const kDefaultFormat = |
| 33 | cricket::VideoFormat(640, |
| 34 | 480, |
| 35 | cricket::VideoFormat::FpsToInterval(30), |
| 36 | cricket::FOURCC_NV12); |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 37 | // iPhone4S is too slow to handle 30fps. |
| 38 | static cricket::VideoFormat const kIPhone4SFormat = |
| 39 | cricket::VideoFormat(640, |
| 40 | 480, |
| 41 | cricket::VideoFormat::FpsToInterval(15), |
| 42 | cricket::FOURCC_NV12); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 43 | |
| 44 | // This class used to capture frames using AVFoundation APIs on iOS. It is meant |
| 45 | // to be owned by an instance of AVFoundationVideoCapturer. The reason for this |
| 46 | // because other webrtc objects own cricket::VideoCapturer, which is not |
| 47 | // ref counted. To prevent bad behavior we do not expose this class directly. |
| 48 | @interface RTCAVFoundationVideoCapturerInternal : NSObject |
| 49 | <AVCaptureVideoDataOutputSampleBufferDelegate> |
| 50 | |
| 51 | @property(nonatomic, readonly) AVCaptureSession *captureSession; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 52 | @property(nonatomic, readonly) dispatch_queue_t frameQueue; |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 53 | @property(nonatomic, readonly) BOOL canUseBackCamera; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 54 | @property(nonatomic, assign) BOOL useBackCamera; // Defaults to NO. |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 55 | @property(nonatomic, assign) BOOL isRunning; // Whether the capture session is running. |
| 56 | @property(atomic, assign) BOOL hasStarted; // Whether we have an unmatched start. |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 57 | |
| 58 | // We keep a pointer back to AVFoundationVideoCapturer to make callbacks on it |
| 59 | // when we receive frames. This is safe because this object should be owned by |
| 60 | // it. |
| 61 | - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer; |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 62 | |
| 63 | // Starts and stops the capture session asynchronously. We cannot do this |
| 64 | // synchronously without blocking a WebRTC thread. |
| 65 | - (void)start; |
| 66 | - (void)stop; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 67 | |
| 68 | @end |
| 69 | |
| 70 | @implementation RTCAVFoundationVideoCapturerInternal { |
| 71 | // Keep pointers to inputs for convenience. |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 72 | AVCaptureDeviceInput *_frontCameraInput; |
| 73 | AVCaptureDeviceInput *_backCameraInput; |
| 74 | AVCaptureVideoDataOutput *_videoDataOutput; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 75 | // The cricket::VideoCapturer that owns this class. Should never be NULL. |
| 76 | webrtc::AVFoundationVideoCapturer *_capturer; |
| 77 | BOOL _orientationHasChanged; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 78 | BOOL _hasRetriedOnFatalError; |
| 79 | BOOL _isRunning; |
| 80 | BOOL _hasStarted; |
| 81 | rtc::CriticalSection _crit; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @synthesize captureSession = _captureSession; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 85 | @synthesize frameQueue = _frameQueue; |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 86 | @synthesize useBackCamera = _useBackCamera; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 87 | @synthesize hasStarted = _hasStarted; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 88 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 89 | // This is called from the thread that creates the video source, which is likely |
| 90 | // the main thread. |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 91 | - (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 92 | RTC_DCHECK(capturer); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 93 | if (self = [super init]) { |
| 94 | _capturer = capturer; |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 95 | // Create the capture session and all relevant inputs and outputs. We need |
| 96 | // to do this in init because the application may want the capture session |
| 97 | // before we start the capturer for e.g. AVCapturePreviewLayer. All objects |
| 98 | // created here are retained until dealloc and never recreated. |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 99 | if (![self setupCaptureSession]) { |
| 100 | return nil; |
| 101 | } |
| 102 | NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 103 | #if TARGET_OS_IPHONE |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 104 | [center addObserver:self |
| 105 | selector:@selector(deviceOrientationDidChange:) |
| 106 | name:UIDeviceOrientationDidChangeNotification |
| 107 | object:nil]; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 108 | [center addObserver:self |
| 109 | selector:@selector(handleCaptureSessionInterruption:) |
| 110 | name:AVCaptureSessionWasInterruptedNotification |
| 111 | object:_captureSession]; |
| 112 | [center addObserver:self |
| 113 | selector:@selector(handleCaptureSessionInterruptionEnded:) |
| 114 | name:AVCaptureSessionInterruptionEndedNotification |
| 115 | object:_captureSession]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 116 | #endif |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 117 | [center addObserver:self |
| 118 | selector:@selector(handleCaptureSessionRuntimeError:) |
| 119 | name:AVCaptureSessionRuntimeErrorNotification |
| 120 | object:_captureSession]; |
| 121 | [center addObserver:self |
| 122 | selector:@selector(handleCaptureSessionDidStartRunning:) |
| 123 | name:AVCaptureSessionDidStartRunningNotification |
| 124 | object:_captureSession]; |
| 125 | [center addObserver:self |
| 126 | selector:@selector(handleCaptureSessionDidStopRunning:) |
| 127 | name:AVCaptureSessionDidStopRunningNotification |
| 128 | object:_captureSession]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 129 | } |
| 130 | return self; |
| 131 | } |
| 132 | |
| 133 | - (void)dealloc { |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 134 | RTC_DCHECK(!self.hasStarted); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 135 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 136 | _capturer = nullptr; |
| 137 | } |
| 138 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 139 | - (AVCaptureSession *)captureSession { |
| 140 | return _captureSession; |
| 141 | } |
| 142 | |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 143 | - (dispatch_queue_t)frameQueue { |
| 144 | if (!_frameQueue) { |
| 145 | _frameQueue = |
| 146 | dispatch_queue_create("org.webrtc.avfoundationvideocapturer.video", |
| 147 | DISPATCH_QUEUE_SERIAL); |
| 148 | dispatch_set_target_queue( |
| 149 | _frameQueue, |
| 150 | dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)); |
| 151 | } |
| 152 | return _frameQueue; |
| 153 | } |
| 154 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 155 | // Called from any thread (likely main thread). |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 156 | - (BOOL)canUseBackCamera { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 157 | return _backCameraInput != nil; |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 160 | // Called from any thread (likely main thread). |
| 161 | - (BOOL)useBackCamera { |
| 162 | @synchronized(self) { |
| 163 | return _useBackCamera; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Called from any thread (likely main thread). |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 168 | - (void)setUseBackCamera:(BOOL)useBackCamera { |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 169 | if (!self.canUseBackCamera) { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 170 | if (useBackCamera) { |
| 171 | RTCLogWarning(@"No rear-facing camera exists or it cannot be used;" |
| 172 | "not switching."); |
| 173 | } |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 176 | @synchronized(self) { |
| 177 | if (_useBackCamera == useBackCamera) { |
| 178 | return; |
| 179 | } |
| 180 | _useBackCamera = useBackCamera; |
| 181 | [self updateSessionInputForUseBackCamera:useBackCamera]; |
| 182 | } |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 185 | - (BOOL)isRunning { |
| 186 | rtc::CritScope cs(&_crit); |
| 187 | return _isRunning; |
| 188 | } |
| 189 | |
| 190 | - (void)setIsRunning:(BOOL)isRunning { |
| 191 | rtc::CritScope cs(&_crit); |
| 192 | _isRunning = isRunning; |
| 193 | } |
| 194 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 195 | // Called from WebRTC thread. |
| 196 | - (void)start { |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 197 | if (self.hasStarted) { |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 200 | self.hasStarted = YES; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 201 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 202 | block:^{ |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 203 | _orientationHasChanged = NO; |
| 204 | [self updateOrientation]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 205 | #if TARGET_OS_IPHONE |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 206 | [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 207 | #endif |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 208 | AVCaptureSession *captureSession = self.captureSession; |
| 209 | [captureSession startRunning]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 210 | }]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 211 | } |
| 212 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 213 | // Called from same thread as start. |
| 214 | - (void)stop { |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 215 | if (!self.hasStarted) { |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 218 | self.hasStarted = NO; |
| 219 | // Due to this async block, it's possible that the ObjC object outlives the |
| 220 | // C++ one. In order to not invoke functions on the C++ object, we set |
| 221 | // hasStarted immediately instead of dispatching it async. |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 222 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 223 | block:^{ |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 224 | [_videoDataOutput setSampleBufferDelegate:nil queue:nullptr]; |
| 225 | [_captureSession stopRunning]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 226 | #if TARGET_OS_IPHONE |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 227 | [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 228 | #endif |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 229 | }]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 232 | #pragma mark iOS notifications |
| 233 | |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 234 | #if TARGET_OS_IPHONE |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 235 | - (void)deviceOrientationDidChange:(NSNotification *)notification { |
| 236 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 237 | block:^{ |
| 238 | _orientationHasChanged = YES; |
| 239 | [self updateOrientation]; |
| 240 | }]; |
| 241 | } |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 242 | #endif |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 243 | |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 244 | #pragma mark AVCaptureVideoDataOutputSampleBufferDelegate |
| 245 | |
| 246 | - (void)captureOutput:(AVCaptureOutput *)captureOutput |
| 247 | didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer |
| 248 | fromConnection:(AVCaptureConnection *)connection { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 249 | NSParameterAssert(captureOutput == _videoDataOutput); |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 250 | if (!self.hasStarted) { |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | _capturer->CaptureSampleBuffer(sampleBuffer); |
| 254 | } |
| 255 | |
| 256 | - (void)captureOutput:(AVCaptureOutput *)captureOutput |
| 257 | didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 258 | fromConnection:(AVCaptureConnection *)connection { |
| 259 | RTCLogError(@"Dropped sample buffer."); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 262 | #pragma mark - AVCaptureSession notifications |
| 263 | |
| 264 | - (void)handleCaptureSessionInterruption:(NSNotification *)notification { |
| 265 | NSString *reasonString = nil; |
| 266 | #if defined(__IPHONE_9_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 |
| 267 | NSNumber *reason = |
| 268 | notification.userInfo[AVCaptureSessionInterruptionReasonKey]; |
| 269 | if (reason) { |
| 270 | switch (reason.intValue) { |
| 271 | case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableInBackground: |
| 272 | reasonString = @"VideoDeviceNotAvailableInBackground"; |
| 273 | break; |
| 274 | case AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient: |
| 275 | reasonString = @"AudioDeviceInUseByAnotherClient"; |
| 276 | break; |
| 277 | case AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient: |
| 278 | reasonString = @"VideoDeviceInUseByAnotherClient"; |
| 279 | break; |
| 280 | case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps: |
| 281 | reasonString = @"VideoDeviceNotAvailableWithMultipleForegroundApps"; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | #endif |
| 286 | RTCLog(@"Capture session interrupted: %@", reasonString); |
| 287 | // TODO(tkchin): Handle this case. |
| 288 | } |
| 289 | |
| 290 | - (void)handleCaptureSessionInterruptionEnded:(NSNotification *)notification { |
| 291 | RTCLog(@"Capture session interruption ended."); |
| 292 | // TODO(tkchin): Handle this case. |
| 293 | } |
| 294 | |
| 295 | - (void)handleCaptureSessionRuntimeError:(NSNotification *)notification { |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 296 | NSError *error = |
| 297 | [notification.userInfo objectForKey:AVCaptureSessionErrorKey]; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 298 | RTCLogError(@"Capture session runtime error: %@", error.localizedDescription); |
| 299 | |
| 300 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 301 | block:^{ |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 302 | #if TARGET_OS_IPHONE |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 303 | if (error.code == AVErrorMediaServicesWereReset) { |
| 304 | [self handleNonFatalError]; |
| 305 | } else { |
| 306 | [self handleFatalError]; |
| 307 | } |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 308 | #else |
| 309 | [self handleFatalError]; |
| 310 | #endif |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 311 | }]; |
| 312 | } |
| 313 | |
| 314 | - (void)handleCaptureSessionDidStartRunning:(NSNotification *)notification { |
| 315 | RTCLog(@"Capture session started."); |
| 316 | self.isRunning = YES; |
| 317 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 318 | block:^{ |
| 319 | // If we successfully restarted after an unknown error, allow future |
| 320 | // retries on fatal errors. |
| 321 | _hasRetriedOnFatalError = NO; |
| 322 | }]; |
| 323 | } |
| 324 | |
| 325 | - (void)handleCaptureSessionDidStopRunning:(NSNotification *)notification { |
| 326 | RTCLog(@"Capture session stopped."); |
| 327 | self.isRunning = NO; |
| 328 | } |
| 329 | |
| 330 | - (void)handleFatalError { |
| 331 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 332 | block:^{ |
| 333 | if (!_hasRetriedOnFatalError) { |
| 334 | RTCLogWarning(@"Attempting to recover from fatal capture error."); |
| 335 | [self handleNonFatalError]; |
| 336 | _hasRetriedOnFatalError = YES; |
| 337 | } else { |
| 338 | RTCLogError(@"Previous fatal error recovery failed."); |
| 339 | } |
| 340 | }]; |
| 341 | } |
| 342 | |
| 343 | - (void)handleNonFatalError { |
| 344 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 345 | block:^{ |
| 346 | if (self.hasStarted) { |
| 347 | RTCLog(@"Restarting capture session after error."); |
| 348 | [self.captureSession startRunning]; |
| 349 | } |
| 350 | }]; |
| 351 | } |
| 352 | |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 353 | #pragma mark - Private |
| 354 | |
| 355 | - (BOOL)setupCaptureSession { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 356 | AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 357 | #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 |
| 358 | NSString *version = [[UIDevice currentDevice] systemVersion]; |
| 359 | if ([version integerValue] >= 7) { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 360 | captureSession.usesApplicationAudioSession = NO; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 361 | } |
| 362 | #endif |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 363 | if (![captureSession canSetSessionPreset:kDefaultPreset]) { |
| 364 | RTCLogError(@"Session preset unsupported."); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 365 | return NO; |
| 366 | } |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 367 | captureSession.sessionPreset = kDefaultPreset; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 368 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 369 | // Add the output. |
| 370 | AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput]; |
| 371 | if (![captureSession canAddOutput:videoDataOutput]) { |
| 372 | RTCLogError(@"Video data output unsupported."); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 373 | return NO; |
| 374 | } |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 375 | [captureSession addOutput:videoDataOutput]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 376 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 377 | // Get the front and back cameras. If there isn't a front camera |
| 378 | // give up. |
| 379 | AVCaptureDeviceInput *frontCameraInput = [self frontCameraInput]; |
| 380 | AVCaptureDeviceInput *backCameraInput = [self backCameraInput]; |
| 381 | if (!frontCameraInput) { |
| 382 | RTCLogError(@"No front camera for capture session."); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 383 | return NO; |
| 384 | } |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 385 | |
| 386 | // Add the inputs. |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 387 | if (![captureSession canAddInput:frontCameraInput] || |
| 388 | (backCameraInput && ![captureSession canAddInput:backCameraInput])) { |
| 389 | RTCLogError(@"Session does not support capture inputs."); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 390 | return NO; |
| 391 | } |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 392 | AVCaptureDeviceInput *input = self.useBackCamera ? |
| 393 | backCameraInput : frontCameraInput; |
| 394 | [captureSession addInput:input]; |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 395 | #if TARGET_OS_IPHONE |
| 396 | if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { |
| 397 | [self setMinFrameDuration:CMTimeMake(1, 15) forDevice:input.device]; |
| 398 | } |
| 399 | #endif |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 400 | _captureSession = captureSession; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 401 | return YES; |
| 402 | } |
| 403 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 404 | - (AVCaptureVideoDataOutput *)videoDataOutput { |
| 405 | if (!_videoDataOutput) { |
| 406 | // Make the capturer output NV12. Ideally we want I420 but that's not |
| 407 | // currently supported on iPhone / iPad. |
| 408 | AVCaptureVideoDataOutput *videoDataOutput = |
| 409 | [[AVCaptureVideoDataOutput alloc] init]; |
| 410 | videoDataOutput = [[AVCaptureVideoDataOutput alloc] init]; |
| 411 | videoDataOutput.videoSettings = @{ |
| 412 | (NSString *)kCVPixelBufferPixelFormatTypeKey : |
| 413 | @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) |
| 414 | }; |
| 415 | videoDataOutput.alwaysDiscardsLateVideoFrames = NO; |
Zeke Chin | 5251680 | 2016-06-03 11:59:22 -0700 | [diff] [blame] | 416 | [videoDataOutput setSampleBufferDelegate:self queue:self.frameQueue]; |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 417 | _videoDataOutput = videoDataOutput; |
| 418 | } |
| 419 | return _videoDataOutput; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 420 | } |
| 421 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 422 | - (AVCaptureDevice *)videoCaptureDeviceForPosition: |
| 423 | (AVCaptureDevicePosition)position { |
| 424 | for (AVCaptureDevice *captureDevice in |
| 425 | [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) { |
| 426 | if (captureDevice.position == position) { |
| 427 | return captureDevice; |
| 428 | } |
| 429 | } |
| 430 | return nil; |
| 431 | } |
| 432 | |
| 433 | - (AVCaptureDeviceInput *)frontCameraInput { |
| 434 | if (!_frontCameraInput) { |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 435 | #if TARGET_OS_IPHONE |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 436 | AVCaptureDevice *frontCameraDevice = |
| 437 | [self videoCaptureDeviceForPosition:AVCaptureDevicePositionFront]; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 438 | #else |
| 439 | AVCaptureDevice *frontCameraDevice = |
| 440 | [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; |
| 441 | #endif |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 442 | if (!frontCameraDevice) { |
| 443 | RTCLogWarning(@"Failed to find front capture device."); |
| 444 | return nil; |
| 445 | } |
| 446 | NSError *error = nil; |
| 447 | AVCaptureDeviceInput *frontCameraInput = |
| 448 | [AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice |
| 449 | error:&error]; |
| 450 | if (!frontCameraInput) { |
| 451 | RTCLogError(@"Failed to create front camera input: %@", |
| 452 | error.localizedDescription); |
| 453 | return nil; |
| 454 | } |
| 455 | _frontCameraInput = frontCameraInput; |
| 456 | } |
| 457 | return _frontCameraInput; |
| 458 | } |
| 459 | |
| 460 | - (AVCaptureDeviceInput *)backCameraInput { |
| 461 | if (!_backCameraInput) { |
| 462 | AVCaptureDevice *backCameraDevice = |
| 463 | [self videoCaptureDeviceForPosition:AVCaptureDevicePositionBack]; |
| 464 | if (!backCameraDevice) { |
| 465 | RTCLogWarning(@"Failed to find front capture device."); |
| 466 | return nil; |
| 467 | } |
| 468 | NSError *error = nil; |
| 469 | AVCaptureDeviceInput *backCameraInput = |
| 470 | [AVCaptureDeviceInput deviceInputWithDevice:backCameraDevice |
| 471 | error:&error]; |
| 472 | if (!backCameraInput) { |
| 473 | RTCLogError(@"Failed to create front camera input: %@", |
| 474 | error.localizedDescription); |
| 475 | return nil; |
| 476 | } |
| 477 | _backCameraInput = backCameraInput; |
| 478 | } |
| 479 | return _backCameraInput; |
| 480 | } |
| 481 | |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 482 | - (void)setMinFrameDuration:(CMTime)minFrameDuration |
| 483 | forDevice:(AVCaptureDevice *)device { |
| 484 | NSError *error = nil; |
| 485 | if (![device lockForConfiguration:&error]) { |
| 486 | RTCLogError(@"Failed to lock device for configuration. Error: %@", error.localizedDescription); |
| 487 | return; |
| 488 | } |
| 489 | device.activeVideoMinFrameDuration = minFrameDuration; |
| 490 | [device unlockForConfiguration]; |
| 491 | } |
| 492 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 493 | // Called from capture session queue. |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 494 | - (void)updateOrientation { |
| 495 | AVCaptureConnection *connection = |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 496 | [_videoDataOutput connectionWithMediaType:AVMediaTypeVideo]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 497 | if (!connection.supportsVideoOrientation) { |
| 498 | // TODO(tkchin): set rotation bit on frames. |
| 499 | return; |
| 500 | } |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 501 | #if TARGET_OS_IPHONE |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 502 | AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait; |
| 503 | switch ([UIDevice currentDevice].orientation) { |
| 504 | case UIDeviceOrientationPortrait: |
| 505 | orientation = AVCaptureVideoOrientationPortrait; |
| 506 | break; |
| 507 | case UIDeviceOrientationPortraitUpsideDown: |
| 508 | orientation = AVCaptureVideoOrientationPortraitUpsideDown; |
| 509 | break; |
| 510 | case UIDeviceOrientationLandscapeLeft: |
| 511 | orientation = AVCaptureVideoOrientationLandscapeRight; |
| 512 | break; |
| 513 | case UIDeviceOrientationLandscapeRight: |
| 514 | orientation = AVCaptureVideoOrientationLandscapeLeft; |
| 515 | break; |
| 516 | case UIDeviceOrientationFaceUp: |
| 517 | case UIDeviceOrientationFaceDown: |
| 518 | case UIDeviceOrientationUnknown: |
| 519 | if (!_orientationHasChanged) { |
| 520 | connection.videoOrientation = orientation; |
| 521 | } |
| 522 | return; |
| 523 | } |
| 524 | connection.videoOrientation = orientation; |
adam.fedor | fc22e03 | 2016-06-08 17:24:37 -0700 | [diff] [blame] | 525 | #endif |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 526 | } |
| 527 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 528 | // Update the current session input to match what's stored in _useBackCamera. |
| 529 | - (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera { |
| 530 | [RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession |
| 531 | block:^{ |
| 532 | [_captureSession beginConfiguration]; |
| 533 | AVCaptureDeviceInput *oldInput = _backCameraInput; |
| 534 | AVCaptureDeviceInput *newInput = _frontCameraInput; |
| 535 | if (useBackCamera) { |
| 536 | oldInput = _frontCameraInput; |
| 537 | newInput = _backCameraInput; |
| 538 | } |
| 539 | if (oldInput) { |
| 540 | // Ok to remove this even if it's not attached. Will be no-op. |
| 541 | [_captureSession removeInput:oldInput]; |
| 542 | } |
| 543 | if (newInput) { |
| 544 | [_captureSession addInput:newInput]; |
| 545 | } |
| 546 | [self updateOrientation]; |
| 547 | [_captureSession commitConfiguration]; |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 548 | #if TARGET_OS_IPHONE |
| 549 | if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { |
| 550 | [self setMinFrameDuration:CMTimeMake(1, 15) forDevice:newInput.device]; |
| 551 | } |
| 552 | #endif |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 553 | }]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | @end |
| 557 | |
| 558 | namespace webrtc { |
| 559 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 560 | enum AVFoundationVideoCapturerMessageType : uint32_t { |
| 561 | kMessageTypeFrame, |
| 562 | }; |
| 563 | |
| 564 | struct AVFoundationFrame { |
| 565 | AVFoundationFrame(CVImageBufferRef buffer, int64_t time) |
| 566 | : image_buffer(buffer), capture_time(time) {} |
| 567 | CVImageBufferRef image_buffer; |
| 568 | int64_t capture_time; |
| 569 | }; |
| 570 | |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 571 | AVFoundationVideoCapturer::AVFoundationVideoCapturer() |
| 572 | : _capturer(nil), _startThread(nullptr) { |
| 573 | // Set our supported formats. This matches kDefaultPreset. |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 574 | std::vector<cricket::VideoFormat> supported_formats; |
| 575 | #if TARGET_OS_IPHONE |
| 576 | if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { |
| 577 | supported_formats.push_back(cricket::VideoFormat(kIPhone4SFormat)); |
| 578 | } else { |
| 579 | supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); |
| 580 | } |
| 581 | #else |
| 582 | supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); |
| 583 | #endif |
| 584 | SetSupportedFormats(supported_formats); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 585 | _capturer = |
| 586 | [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this]; |
| 587 | } |
| 588 | |
| 589 | AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { |
| 590 | _capturer = nil; |
| 591 | } |
| 592 | |
| 593 | cricket::CaptureState AVFoundationVideoCapturer::Start( |
| 594 | const cricket::VideoFormat& format) { |
| 595 | if (!_capturer) { |
| 596 | LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; |
| 597 | return cricket::CaptureState::CS_FAILED; |
| 598 | } |
| 599 | if (_capturer.isRunning) { |
| 600 | LOG(LS_ERROR) << "The capturer is already running."; |
| 601 | return cricket::CaptureState::CS_FAILED; |
| 602 | } |
tkchin | d762910 | 2016-07-28 14:52:55 -0700 | [diff] [blame^] | 603 | if (format != kDefaultFormat && format != kIPhone4SFormat) { |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 604 | LOG(LS_ERROR) << "Unsupported format provided."; |
| 605 | return cricket::CaptureState::CS_FAILED; |
| 606 | } |
| 607 | |
| 608 | // Keep track of which thread capture started on. This is the thread that |
| 609 | // frames need to be sent to. |
| 610 | RTC_DCHECK(!_startThread); |
| 611 | _startThread = rtc::Thread::Current(); |
| 612 | |
| 613 | SetCaptureFormat(&format); |
| 614 | // This isn't super accurate because it takes a while for the AVCaptureSession |
| 615 | // to spin up, and this call returns async. |
| 616 | // TODO(tkchin): make this better. |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 617 | [_capturer start]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 618 | SetCaptureState(cricket::CaptureState::CS_RUNNING); |
| 619 | |
| 620 | return cricket::CaptureState::CS_STARTING; |
| 621 | } |
| 622 | |
| 623 | void AVFoundationVideoCapturer::Stop() { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 624 | [_capturer stop]; |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 625 | SetCaptureFormat(NULL); |
| 626 | _startThread = nullptr; |
| 627 | } |
| 628 | |
| 629 | bool AVFoundationVideoCapturer::IsRunning() { |
| 630 | return _capturer.isRunning; |
| 631 | } |
| 632 | |
| 633 | AVCaptureSession* AVFoundationVideoCapturer::GetCaptureSession() { |
| 634 | return _capturer.captureSession; |
| 635 | } |
| 636 | |
hjon | a1cf366 | 2016-03-14 20:55:22 -0700 | [diff] [blame] | 637 | bool AVFoundationVideoCapturer::CanUseBackCamera() const { |
| 638 | return _capturer.canUseBackCamera; |
| 639 | } |
| 640 | |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 641 | void AVFoundationVideoCapturer::SetUseBackCamera(bool useBackCamera) { |
| 642 | _capturer.useBackCamera = useBackCamera; |
| 643 | } |
| 644 | |
| 645 | bool AVFoundationVideoCapturer::GetUseBackCamera() const { |
| 646 | return _capturer.useBackCamera; |
| 647 | } |
| 648 | |
| 649 | void AVFoundationVideoCapturer::CaptureSampleBuffer( |
| 650 | CMSampleBufferRef sampleBuffer) { |
| 651 | if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || |
| 652 | !CMSampleBufferIsValid(sampleBuffer) || |
| 653 | !CMSampleBufferDataIsReady(sampleBuffer)) { |
| 654 | return; |
| 655 | } |
| 656 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 657 | CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 658 | if (image_buffer == NULL) { |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 662 | // Retain the buffer and post it to the webrtc thread. It will be released |
| 663 | // after it has successfully been signaled. |
| 664 | CVBufferRetain(image_buffer); |
| 665 | AVFoundationFrame frame(image_buffer, rtc::TimeNanos()); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 666 | _startThread->Post(RTC_FROM_HERE, this, kMessageTypeFrame, |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 667 | new rtc::TypedMessageData<AVFoundationFrame>(frame)); |
| 668 | } |
| 669 | |
| 670 | void AVFoundationVideoCapturer::OnMessage(rtc::Message *msg) { |
| 671 | switch (msg->message_id) { |
| 672 | case kMessageTypeFrame: { |
| 673 | rtc::TypedMessageData<AVFoundationFrame>* data = |
| 674 | static_cast<rtc::TypedMessageData<AVFoundationFrame>*>(msg->pdata); |
| 675 | const AVFoundationFrame& frame = data->data(); |
| 676 | OnFrameMessage(frame.image_buffer, frame.capture_time); |
| 677 | delete data; |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | void AVFoundationVideoCapturer::OnFrameMessage(CVImageBufferRef image_buffer, |
magjed | 39607c9 | 2016-07-14 08:12:17 -0700 | [diff] [blame] | 684 | int64_t capture_time_ns) { |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 685 | RTC_DCHECK(_startThread->IsCurrent()); |
| 686 | |
magjed | 39607c9 | 2016-07-14 08:12:17 -0700 | [diff] [blame] | 687 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 688 | new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(image_buffer); |
| 689 | |
| 690 | const int captured_width = buffer->width(); |
| 691 | const int captured_height = buffer->height(); |
| 692 | |
| 693 | int adapted_width; |
| 694 | int adapted_height; |
| 695 | int crop_width; |
| 696 | int crop_height; |
| 697 | int crop_x; |
| 698 | int crop_y; |
| 699 | int64_t translated_camera_time_us; |
| 700 | |
| 701 | if (!AdaptFrame(captured_width, captured_height, |
| 702 | capture_time_ns / rtc::kNumNanosecsPerMicrosec, |
| 703 | rtc::TimeMicros(), &adapted_width, &adapted_height, |
| 704 | &crop_width, &crop_height, &crop_x, &crop_y, |
| 705 | &translated_camera_time_us)) { |
| 706 | CVBufferRelease(image_buffer); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
magjed | 39607c9 | 2016-07-14 08:12:17 -0700 | [diff] [blame] | 710 | if (adapted_width != captured_width || crop_width != captured_width || |
| 711 | adapted_height != captured_height || crop_height != captured_height) { |
| 712 | // TODO(magjed): Avoid converting to I420. |
| 713 | rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer( |
| 714 | _buffer_pool.CreateBuffer(adapted_width, adapted_height)); |
| 715 | scaled_buffer->CropAndScaleFrom(buffer->NativeToI420Buffer(), crop_x, |
| 716 | crop_y, crop_width, crop_height); |
| 717 | buffer = scaled_buffer; |
| 718 | } |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 719 | |
magjed | 39607c9 | 2016-07-14 08:12:17 -0700 | [diff] [blame] | 720 | OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, |
| 721 | translated_camera_time_us), |
| 722 | captured_width, captured_height); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 723 | |
tkchin | 89717aa | 2016-03-31 17:14:04 -0700 | [diff] [blame] | 724 | CVBufferRelease(image_buffer); |
Jon Hjelle | 7ac8bab | 2016-01-21 11:44:55 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | } // namespace webrtc |