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