2016-01-21 11:44:55 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-04-27 01:54:20 -07:00
|
|
|
#include "avfoundationvideocapturer.h"
|
2016-01-21 11:44:55 -08:00
|
|
|
|
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
#import <Foundation/Foundation.h>
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-01-21 11:44:55 -08:00
|
|
|
#import <UIKit/UIKit.h>
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-01-21 11:44:55 -08:00
|
|
|
|
2016-04-27 01:54:20 -07:00
|
|
|
#import "RTCDispatcher+Private.h"
|
|
|
|
|
#import "WebRTC/RTCLogging.h"
|
2016-07-28 14:52:55 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
#import "WebRTC/UIDevice+RTCDevice.h"
|
|
|
|
|
#endif
|
2016-04-27 01:54:20 -07:00
|
|
|
|
2016-08-25 03:25:04 -07:00
|
|
|
#include "libyuv/rotate.h"
|
|
|
|
|
|
2016-04-27 01:54:20 -07:00
|
|
|
#include "webrtc/base/bind.h"
|
|
|
|
|
#include "webrtc/base/checks.h"
|
|
|
|
|
#include "webrtc/base/thread.h"
|
2016-07-14 08:12:17 -07:00
|
|
|
#include "webrtc/common_video/include/corevideo_frame_buffer.h"
|
2016-08-25 03:25:04 -07:00
|
|
|
#include "webrtc/common_video/rotation.h"
|
2016-01-21 11:44:55 -08:00
|
|
|
|
2016-08-19 01:24:46 -07:00
|
|
|
struct AVCaptureSessionPresetResolution {
|
|
|
|
|
NSString *sessionPreset;
|
|
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
static const AVCaptureSessionPresetResolution kAvailablePresets[] = {
|
|
|
|
|
{ AVCaptureSessionPreset352x288, 352, 288},
|
|
|
|
|
{ AVCaptureSessionPreset640x480, 640, 480},
|
|
|
|
|
{ AVCaptureSessionPreset1280x720, 1280, 720},
|
|
|
|
|
{ AVCaptureSessionPreset1920x1080, 1920, 1080},
|
|
|
|
|
};
|
|
|
|
|
#else // macOS
|
|
|
|
|
static const AVCaptureSessionPresetResolution kAvailablePresets[] = {
|
|
|
|
|
{ AVCaptureSessionPreset320x240, 320, 240},
|
|
|
|
|
{ AVCaptureSessionPreset352x288, 352, 288},
|
|
|
|
|
{ AVCaptureSessionPreset640x480, 640, 480},
|
|
|
|
|
{ AVCaptureSessionPreset960x540, 960, 540},
|
|
|
|
|
{ AVCaptureSessionPreset1280x720, 1280, 720},
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Mapping from cricket::VideoFormat to AVCaptureSession presets.
|
|
|
|
|
static NSString *GetSessionPresetForVideoFormat(
|
|
|
|
|
const cricket::VideoFormat& format) {
|
|
|
|
|
for (const auto preset : kAvailablePresets) {
|
|
|
|
|
// Check both orientations
|
|
|
|
|
if ((format.width == preset.width && format.height == preset.height) ||
|
|
|
|
|
(format.width == preset.height && format.height == preset.width)) {
|
|
|
|
|
return preset.sessionPreset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If no matching preset is found, use a default one.
|
|
|
|
|
return AVCaptureSessionPreset640x480;
|
|
|
|
|
}
|
2016-01-21 11:44:55 -08:00
|
|
|
|
|
|
|
|
// This class used to capture frames using AVFoundation APIs on iOS. It is meant
|
|
|
|
|
// to be owned by an instance of AVFoundationVideoCapturer. The reason for this
|
|
|
|
|
// because other webrtc objects own cricket::VideoCapturer, which is not
|
|
|
|
|
// ref counted. To prevent bad behavior we do not expose this class directly.
|
|
|
|
|
@interface RTCAVFoundationVideoCapturerInternal : NSObject
|
|
|
|
|
<AVCaptureVideoDataOutputSampleBufferDelegate>
|
|
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) AVCaptureSession *captureSession;
|
2016-06-03 11:59:22 -07:00
|
|
|
@property(nonatomic, readonly) dispatch_queue_t frameQueue;
|
2016-03-14 20:55:22 -07:00
|
|
|
@property(nonatomic, readonly) BOOL canUseBackCamera;
|
2016-01-21 11:44:55 -08:00
|
|
|
@property(nonatomic, assign) BOOL useBackCamera; // Defaults to NO.
|
2016-06-03 11:59:22 -07:00
|
|
|
@property(nonatomic, assign) BOOL isRunning; // Whether the capture session is running.
|
|
|
|
|
@property(atomic, assign) BOOL hasStarted; // Whether we have an unmatched start.
|
2016-01-21 11:44:55 -08:00
|
|
|
|
|
|
|
|
// We keep a pointer back to AVFoundationVideoCapturer to make callbacks on it
|
|
|
|
|
// when we receive frames. This is safe because this object should be owned by
|
|
|
|
|
// it.
|
|
|
|
|
- (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer;
|
2016-08-19 01:24:46 -07:00
|
|
|
- (AVCaptureDevice *)getActiveCaptureDevice;
|
2016-03-31 17:14:04 -07:00
|
|
|
|
|
|
|
|
// Starts and stops the capture session asynchronously. We cannot do this
|
|
|
|
|
// synchronously without blocking a WebRTC thread.
|
|
|
|
|
- (void)start;
|
|
|
|
|
- (void)stop;
|
2016-01-21 11:44:55 -08:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation RTCAVFoundationVideoCapturerInternal {
|
|
|
|
|
// Keep pointers to inputs for convenience.
|
2016-03-31 17:14:04 -07:00
|
|
|
AVCaptureDeviceInput *_frontCameraInput;
|
|
|
|
|
AVCaptureDeviceInput *_backCameraInput;
|
|
|
|
|
AVCaptureVideoDataOutput *_videoDataOutput;
|
2016-01-21 11:44:55 -08:00
|
|
|
// The cricket::VideoCapturer that owns this class. Should never be NULL.
|
|
|
|
|
webrtc::AVFoundationVideoCapturer *_capturer;
|
2016-08-25 03:25:04 -07:00
|
|
|
webrtc::VideoRotation _rotation;
|
2016-06-03 11:59:22 -07:00
|
|
|
BOOL _hasRetriedOnFatalError;
|
|
|
|
|
BOOL _isRunning;
|
|
|
|
|
BOOL _hasStarted;
|
|
|
|
|
rtc::CriticalSection _crit;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@synthesize captureSession = _captureSession;
|
2016-06-03 11:59:22 -07:00
|
|
|
@synthesize frameQueue = _frameQueue;
|
2016-03-31 17:14:04 -07:00
|
|
|
@synthesize useBackCamera = _useBackCamera;
|
2016-06-03 11:59:22 -07:00
|
|
|
@synthesize hasStarted = _hasStarted;
|
2016-01-21 11:44:55 -08:00
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// This is called from the thread that creates the video source, which is likely
|
|
|
|
|
// the main thread.
|
2016-01-21 11:44:55 -08:00
|
|
|
- (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer {
|
2016-03-31 17:14:04 -07:00
|
|
|
RTC_DCHECK(capturer);
|
2016-01-21 11:44:55 -08:00
|
|
|
if (self = [super init]) {
|
|
|
|
|
_capturer = capturer;
|
2016-03-31 17:14:04 -07:00
|
|
|
// Create the capture session and all relevant inputs and outputs. We need
|
|
|
|
|
// to do this in init because the application may want the capture session
|
|
|
|
|
// before we start the capturer for e.g. AVCapturePreviewLayer. All objects
|
|
|
|
|
// created here are retained until dealloc and never recreated.
|
2016-01-21 11:44:55 -08:00
|
|
|
if (![self setupCaptureSession]) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-01-21 11:44:55 -08:00
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(deviceOrientationDidChange:)
|
|
|
|
|
name:UIDeviceOrientationDidChangeNotification
|
|
|
|
|
object:nil];
|
2016-06-03 11:59:22 -07:00
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleCaptureSessionInterruption:)
|
|
|
|
|
name:AVCaptureSessionWasInterruptedNotification
|
|
|
|
|
object:_captureSession];
|
|
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleCaptureSessionInterruptionEnded:)
|
|
|
|
|
name:AVCaptureSessionInterruptionEndedNotification
|
|
|
|
|
object:_captureSession];
|
2016-08-24 12:05:56 -07:00
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleApplicationDidBecomeActive:)
|
|
|
|
|
name:UIApplicationDidBecomeActiveNotification
|
|
|
|
|
object:[UIApplication sharedApplication]];
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-06-03 11:59:22 -07:00
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleCaptureSessionRuntimeError:)
|
|
|
|
|
name:AVCaptureSessionRuntimeErrorNotification
|
|
|
|
|
object:_captureSession];
|
|
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleCaptureSessionDidStartRunning:)
|
|
|
|
|
name:AVCaptureSessionDidStartRunningNotification
|
|
|
|
|
object:_captureSession];
|
|
|
|
|
[center addObserver:self
|
|
|
|
|
selector:@selector(handleCaptureSessionDidStopRunning:)
|
|
|
|
|
name:AVCaptureSessionDidStopRunningNotification
|
|
|
|
|
object:_captureSession];
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2016-06-03 11:59:22 -07:00
|
|
|
RTC_DCHECK(!self.hasStarted);
|
2016-01-21 11:44:55 -08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
_capturer = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
- (AVCaptureSession *)captureSession {
|
|
|
|
|
return _captureSession;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 01:24:46 -07:00
|
|
|
- (AVCaptureDevice *)getActiveCaptureDevice {
|
|
|
|
|
return self.useBackCamera ? _backCameraInput.device : _frontCameraInput.device;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 11:59:22 -07:00
|
|
|
- (dispatch_queue_t)frameQueue {
|
|
|
|
|
if (!_frameQueue) {
|
|
|
|
|
_frameQueue =
|
|
|
|
|
dispatch_queue_create("org.webrtc.avfoundationvideocapturer.video",
|
|
|
|
|
DISPATCH_QUEUE_SERIAL);
|
|
|
|
|
dispatch_set_target_queue(
|
|
|
|
|
_frameQueue,
|
|
|
|
|
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
|
|
|
|
|
}
|
|
|
|
|
return _frameQueue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Called from any thread (likely main thread).
|
2016-03-14 20:55:22 -07:00
|
|
|
- (BOOL)canUseBackCamera {
|
2016-03-31 17:14:04 -07:00
|
|
|
return _backCameraInput != nil;
|
2016-03-14 20:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Called from any thread (likely main thread).
|
|
|
|
|
- (BOOL)useBackCamera {
|
|
|
|
|
@synchronized(self) {
|
|
|
|
|
return _useBackCamera;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called from any thread (likely main thread).
|
|
|
|
|
- (void)setUseBackCamera:(BOOL)useBackCamera {
|
2016-03-14 20:55:22 -07:00
|
|
|
if (!self.canUseBackCamera) {
|
2016-03-31 17:14:04 -07:00
|
|
|
if (useBackCamera) {
|
|
|
|
|
RTCLogWarning(@"No rear-facing camera exists or it cannot be used;"
|
|
|
|
|
"not switching.");
|
|
|
|
|
}
|
2016-03-14 20:55:22 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
@synchronized(self) {
|
|
|
|
|
if (_useBackCamera == useBackCamera) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_useBackCamera = useBackCamera;
|
|
|
|
|
[self updateSessionInputForUseBackCamera:useBackCamera];
|
|
|
|
|
}
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-03 11:59:22 -07:00
|
|
|
- (BOOL)isRunning {
|
|
|
|
|
rtc::CritScope cs(&_crit);
|
|
|
|
|
return _isRunning;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)setIsRunning:(BOOL)isRunning {
|
|
|
|
|
rtc::CritScope cs(&_crit);
|
|
|
|
|
_isRunning = isRunning;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Called from WebRTC thread.
|
|
|
|
|
- (void)start {
|
2016-06-03 11:59:22 -07:00
|
|
|
if (self.hasStarted) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-03 11:59:22 -07:00
|
|
|
self.hasStarted = YES;
|
2016-01-21 11:44:55 -08:00
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
2016-08-25 03:25:04 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
// Default to portrait orientation on iPhone. This will be reset in
|
|
|
|
|
// updateOrientation unless orientation is unknown/faceup/facedown.
|
|
|
|
|
_rotation = webrtc::kVideoRotation_90;
|
|
|
|
|
#else
|
|
|
|
|
// No rotation on Mac.
|
|
|
|
|
_rotation = webrtc::kVideoRotation_0;
|
|
|
|
|
#endif
|
2016-03-31 17:14:04 -07:00
|
|
|
[self updateOrientation];
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-03-31 17:14:04 -07:00
|
|
|
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-03-31 17:14:04 -07:00
|
|
|
AVCaptureSession *captureSession = self.captureSession;
|
|
|
|
|
[captureSession startRunning];
|
2016-01-21 11:44:55 -08:00
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Called from same thread as start.
|
|
|
|
|
- (void)stop {
|
2016-06-03 11:59:22 -07:00
|
|
|
if (!self.hasStarted) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-03 11:59:22 -07:00
|
|
|
self.hasStarted = NO;
|
|
|
|
|
// Due to this async block, it's possible that the ObjC object outlives the
|
|
|
|
|
// C++ one. In order to not invoke functions on the C++ object, we set
|
|
|
|
|
// hasStarted immediately instead of dispatching it async.
|
2016-01-21 11:44:55 -08:00
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
2016-03-31 17:14:04 -07:00
|
|
|
[_videoDataOutput setSampleBufferDelegate:nil queue:nullptr];
|
|
|
|
|
[_captureSession stopRunning];
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-03-31 17:14:04 -07:00
|
|
|
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-01-21 11:44:55 -08:00
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-03 11:59:22 -07:00
|
|
|
#pragma mark iOS notifications
|
|
|
|
|
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-06-03 11:59:22 -07:00
|
|
|
- (void)deviceOrientationDidChange:(NSNotification *)notification {
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
[self updateOrientation];
|
|
|
|
|
}];
|
|
|
|
|
}
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-06-03 11:59:22 -07:00
|
|
|
|
2016-01-21 11:44:55 -08:00
|
|
|
#pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
|
|
|
|
|
|
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
|
|
|
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
|
|
|
|
fromConnection:(AVCaptureConnection *)connection {
|
2016-03-31 17:14:04 -07:00
|
|
|
NSParameterAssert(captureOutput == _videoDataOutput);
|
2016-06-03 11:59:22 -07:00
|
|
|
if (!self.hasStarted) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-25 03:25:04 -07:00
|
|
|
_capturer->CaptureSampleBuffer(sampleBuffer, _rotation);
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)captureOutput:(AVCaptureOutput *)captureOutput
|
|
|
|
|
didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
|
2016-03-31 17:14:04 -07:00
|
|
|
fromConnection:(AVCaptureConnection *)connection {
|
|
|
|
|
RTCLogError(@"Dropped sample buffer.");
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-03 11:59:22 -07:00
|
|
|
#pragma mark - AVCaptureSession notifications
|
|
|
|
|
|
|
|
|
|
- (void)handleCaptureSessionInterruption:(NSNotification *)notification {
|
|
|
|
|
NSString *reasonString = nil;
|
2016-09-28 17:42:01 -07:00
|
|
|
#if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) \
|
|
|
|
|
&& __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
|
2016-06-03 11:59:22 -07:00
|
|
|
NSNumber *reason =
|
|
|
|
|
notification.userInfo[AVCaptureSessionInterruptionReasonKey];
|
|
|
|
|
if (reason) {
|
|
|
|
|
switch (reason.intValue) {
|
|
|
|
|
case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableInBackground:
|
|
|
|
|
reasonString = @"VideoDeviceNotAvailableInBackground";
|
|
|
|
|
break;
|
|
|
|
|
case AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient:
|
|
|
|
|
reasonString = @"AudioDeviceInUseByAnotherClient";
|
|
|
|
|
break;
|
|
|
|
|
case AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient:
|
|
|
|
|
reasonString = @"VideoDeviceInUseByAnotherClient";
|
|
|
|
|
break;
|
|
|
|
|
case AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps:
|
|
|
|
|
reasonString = @"VideoDeviceNotAvailableWithMultipleForegroundApps";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
RTCLog(@"Capture session interrupted: %@", reasonString);
|
|
|
|
|
// TODO(tkchin): Handle this case.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleCaptureSessionInterruptionEnded:(NSNotification *)notification {
|
|
|
|
|
RTCLog(@"Capture session interruption ended.");
|
|
|
|
|
// TODO(tkchin): Handle this case.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleCaptureSessionRuntimeError:(NSNotification *)notification {
|
2016-06-08 17:24:37 -07:00
|
|
|
NSError *error =
|
|
|
|
|
[notification.userInfo objectForKey:AVCaptureSessionErrorKey];
|
2016-07-29 12:03:51 -07:00
|
|
|
RTCLogError(@"Capture session runtime error: %@", error);
|
2016-06-03 11:59:22 -07:00
|
|
|
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-06-03 11:59:22 -07:00
|
|
|
if (error.code == AVErrorMediaServicesWereReset) {
|
|
|
|
|
[self handleNonFatalError];
|
|
|
|
|
} else {
|
|
|
|
|
[self handleFatalError];
|
|
|
|
|
}
|
2016-06-08 17:24:37 -07:00
|
|
|
#else
|
|
|
|
|
[self handleFatalError];
|
|
|
|
|
#endif
|
2016-06-03 11:59:22 -07:00
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleCaptureSessionDidStartRunning:(NSNotification *)notification {
|
|
|
|
|
RTCLog(@"Capture session started.");
|
|
|
|
|
self.isRunning = YES;
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
// If we successfully restarted after an unknown error, allow future
|
|
|
|
|
// retries on fatal errors.
|
|
|
|
|
_hasRetriedOnFatalError = NO;
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleCaptureSessionDidStopRunning:(NSNotification *)notification {
|
|
|
|
|
RTCLog(@"Capture session stopped.");
|
|
|
|
|
self.isRunning = NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleFatalError {
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
if (!_hasRetriedOnFatalError) {
|
|
|
|
|
RTCLogWarning(@"Attempting to recover from fatal capture error.");
|
|
|
|
|
[self handleNonFatalError];
|
|
|
|
|
_hasRetriedOnFatalError = YES;
|
|
|
|
|
} else {
|
|
|
|
|
RTCLogError(@"Previous fatal error recovery failed.");
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleNonFatalError {
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
if (self.hasStarted) {
|
|
|
|
|
RTCLog(@"Restarting capture session after error.");
|
|
|
|
|
[self.captureSession startRunning];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 12:05:56 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
|
|
|
|
|
#pragma mark - UIApplication notifications
|
|
|
|
|
|
|
|
|
|
- (void)handleApplicationDidBecomeActive:(NSNotification *)notification {
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
if (self.hasStarted && !self.captureSession.isRunning) {
|
|
|
|
|
RTCLog(@"Restarting capture session on active.");
|
|
|
|
|
[self.captureSession startRunning];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // TARGET_OS_IPHONE
|
|
|
|
|
|
2016-01-21 11:44:55 -08:00
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
|
|
- (BOOL)setupCaptureSession {
|
2016-03-31 17:14:04 -07:00
|
|
|
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
|
2016-09-30 08:56:33 +02:00
|
|
|
#if defined(WEBRTC_IOS)
|
|
|
|
|
captureSession.usesApplicationAudioSession = NO;
|
2016-01-21 11:44:55 -08:00
|
|
|
#endif
|
2016-03-31 17:14:04 -07:00
|
|
|
// Add the output.
|
|
|
|
|
AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput];
|
|
|
|
|
if (![captureSession canAddOutput:videoDataOutput]) {
|
|
|
|
|
RTCLogError(@"Video data output unsupported.");
|
2016-01-21 11:44:55 -08:00
|
|
|
return NO;
|
|
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
[captureSession addOutput:videoDataOutput];
|
|
|
|
|
|
|
|
|
|
// Get the front and back cameras. If there isn't a front camera
|
|
|
|
|
// give up.
|
|
|
|
|
AVCaptureDeviceInput *frontCameraInput = [self frontCameraInput];
|
|
|
|
|
AVCaptureDeviceInput *backCameraInput = [self backCameraInput];
|
|
|
|
|
if (!frontCameraInput) {
|
|
|
|
|
RTCLogError(@"No front camera for capture session.");
|
|
|
|
|
return NO;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
|
|
|
|
|
// Add the inputs.
|
|
|
|
|
if (![captureSession canAddInput:frontCameraInput] ||
|
|
|
|
|
(backCameraInput && ![captureSession canAddInput:backCameraInput])) {
|
|
|
|
|
RTCLogError(@"Session does not support capture inputs.");
|
2016-01-21 11:44:55 -08:00
|
|
|
return NO;
|
|
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
AVCaptureDeviceInput *input = self.useBackCamera ?
|
|
|
|
|
backCameraInput : frontCameraInput;
|
|
|
|
|
[captureSession addInput:input];
|
2016-08-19 01:24:46 -07:00
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
_captureSession = captureSession;
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (AVCaptureVideoDataOutput *)videoDataOutput {
|
|
|
|
|
if (!_videoDataOutput) {
|
|
|
|
|
// Make the capturer output NV12. Ideally we want I420 but that's not
|
|
|
|
|
// currently supported on iPhone / iPad.
|
|
|
|
|
AVCaptureVideoDataOutput *videoDataOutput =
|
|
|
|
|
[[AVCaptureVideoDataOutput alloc] init];
|
|
|
|
|
videoDataOutput.videoSettings = @{
|
|
|
|
|
(NSString *)kCVPixelBufferPixelFormatTypeKey :
|
|
|
|
|
@(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
|
|
|
|
|
};
|
|
|
|
|
videoDataOutput.alwaysDiscardsLateVideoFrames = NO;
|
2016-06-03 11:59:22 -07:00
|
|
|
[videoDataOutput setSampleBufferDelegate:self queue:self.frameQueue];
|
2016-03-31 17:14:04 -07:00
|
|
|
_videoDataOutput = videoDataOutput;
|
2016-03-14 20:55:22 -07:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
return _videoDataOutput;
|
|
|
|
|
}
|
2016-01-21 11:44:55 -08:00
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
- (AVCaptureDevice *)videoCaptureDeviceForPosition:
|
|
|
|
|
(AVCaptureDevicePosition)position {
|
|
|
|
|
for (AVCaptureDevice *captureDevice in
|
|
|
|
|
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
|
|
|
|
|
if (captureDevice.position == position) {
|
|
|
|
|
return captureDevice;
|
|
|
|
|
}
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (AVCaptureDeviceInput *)frontCameraInput {
|
|
|
|
|
if (!_frontCameraInput) {
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-03-31 17:14:04 -07:00
|
|
|
AVCaptureDevice *frontCameraDevice =
|
|
|
|
|
[self videoCaptureDeviceForPosition:AVCaptureDevicePositionFront];
|
2016-06-08 17:24:37 -07:00
|
|
|
#else
|
|
|
|
|
AVCaptureDevice *frontCameraDevice =
|
|
|
|
|
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
|
|
|
|
|
#endif
|
2016-03-31 17:14:04 -07:00
|
|
|
if (!frontCameraDevice) {
|
|
|
|
|
RTCLogWarning(@"Failed to find front capture device.");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
NSError *error = nil;
|
|
|
|
|
AVCaptureDeviceInput *frontCameraInput =
|
|
|
|
|
[AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice
|
2016-03-14 20:55:22 -07:00
|
|
|
error:&error];
|
2016-03-31 17:14:04 -07:00
|
|
|
if (!frontCameraInput) {
|
|
|
|
|
RTCLogError(@"Failed to create front camera input: %@",
|
|
|
|
|
error.localizedDescription);
|
|
|
|
|
return nil;
|
2016-03-14 20:55:22 -07:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
_frontCameraInput = frontCameraInput;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
return _frontCameraInput;
|
|
|
|
|
}
|
2016-01-21 11:44:55 -08:00
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
- (AVCaptureDeviceInput *)backCameraInput {
|
|
|
|
|
if (!_backCameraInput) {
|
|
|
|
|
AVCaptureDevice *backCameraDevice =
|
|
|
|
|
[self videoCaptureDeviceForPosition:AVCaptureDevicePositionBack];
|
|
|
|
|
if (!backCameraDevice) {
|
|
|
|
|
RTCLogWarning(@"Failed to find front capture device.");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
NSError *error = nil;
|
|
|
|
|
AVCaptureDeviceInput *backCameraInput =
|
|
|
|
|
[AVCaptureDeviceInput deviceInputWithDevice:backCameraDevice
|
|
|
|
|
error:&error];
|
|
|
|
|
if (!backCameraInput) {
|
|
|
|
|
RTCLogError(@"Failed to create front camera input: %@",
|
|
|
|
|
error.localizedDescription);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
_backCameraInput = backCameraInput;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-03-31 17:14:04 -07:00
|
|
|
return _backCameraInput;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-07-28 14:52:55 -07:00
|
|
|
- (void)setMinFrameDuration:(CMTime)minFrameDuration
|
|
|
|
|
forDevice:(AVCaptureDevice *)device {
|
|
|
|
|
NSError *error = nil;
|
|
|
|
|
if (![device lockForConfiguration:&error]) {
|
|
|
|
|
RTCLogError(@"Failed to lock device for configuration. Error: %@", error.localizedDescription);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
device.activeVideoMinFrameDuration = minFrameDuration;
|
|
|
|
|
[device unlockForConfiguration];
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Called from capture session queue.
|
2016-01-21 11:44:55 -08:00
|
|
|
- (void)updateOrientation {
|
2016-06-08 17:24:37 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
2016-01-21 11:44:55 -08:00
|
|
|
switch ([UIDevice currentDevice].orientation) {
|
|
|
|
|
case UIDeviceOrientationPortrait:
|
2016-08-25 03:25:04 -07:00
|
|
|
_rotation = webrtc::kVideoRotation_90;
|
2016-01-21 11:44:55 -08:00
|
|
|
break;
|
|
|
|
|
case UIDeviceOrientationPortraitUpsideDown:
|
2016-08-25 03:25:04 -07:00
|
|
|
_rotation = webrtc::kVideoRotation_270;
|
2016-01-21 11:44:55 -08:00
|
|
|
break;
|
|
|
|
|
case UIDeviceOrientationLandscapeLeft:
|
2016-08-25 03:25:04 -07:00
|
|
|
_rotation = webrtc::kVideoRotation_180;
|
2016-01-21 11:44:55 -08:00
|
|
|
break;
|
|
|
|
|
case UIDeviceOrientationLandscapeRight:
|
2016-08-25 03:25:04 -07:00
|
|
|
_rotation = webrtc::kVideoRotation_0;
|
2016-01-21 11:44:55 -08:00
|
|
|
break;
|
|
|
|
|
case UIDeviceOrientationFaceUp:
|
|
|
|
|
case UIDeviceOrientationFaceDown:
|
|
|
|
|
case UIDeviceOrientationUnknown:
|
2016-08-25 03:25:04 -07:00
|
|
|
// Ignore.
|
|
|
|
|
break;
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
2016-06-08 17:24:37 -07:00
|
|
|
#endif
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
// Update the current session input to match what's stored in _useBackCamera.
|
|
|
|
|
- (void)updateSessionInputForUseBackCamera:(BOOL)useBackCamera {
|
|
|
|
|
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
|
|
|
|
|
block:^{
|
|
|
|
|
[_captureSession beginConfiguration];
|
|
|
|
|
AVCaptureDeviceInput *oldInput = _backCameraInput;
|
|
|
|
|
AVCaptureDeviceInput *newInput = _frontCameraInput;
|
|
|
|
|
if (useBackCamera) {
|
|
|
|
|
oldInput = _frontCameraInput;
|
|
|
|
|
newInput = _backCameraInput;
|
|
|
|
|
}
|
|
|
|
|
if (oldInput) {
|
|
|
|
|
// Ok to remove this even if it's not attached. Will be no-op.
|
|
|
|
|
[_captureSession removeInput:oldInput];
|
|
|
|
|
}
|
|
|
|
|
if (newInput) {
|
|
|
|
|
[_captureSession addInput:newInput];
|
|
|
|
|
}
|
|
|
|
|
[self updateOrientation];
|
|
|
|
|
[_captureSession commitConfiguration];
|
2016-08-19 01:24:46 -07:00
|
|
|
|
|
|
|
|
const auto fps = cricket::VideoFormat::IntervalToFps(_capturer->GetCaptureFormat()->interval);
|
|
|
|
|
[self setMinFrameDuration:CMTimeMake(1, fps)forDevice:newInput.device];
|
2016-03-31 17:14:04 -07:00
|
|
|
}];
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-03-31 17:14:04 -07:00
|
|
|
enum AVFoundationVideoCapturerMessageType : uint32_t {
|
|
|
|
|
kMessageTypeFrame,
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-01 15:15:00 +02:00
|
|
|
AVFoundationVideoCapturer::AVFoundationVideoCapturer() : _capturer(nil) {
|
2016-08-19 01:24:46 -07:00
|
|
|
// Set our supported formats. This matches kAvailablePresets.
|
|
|
|
|
_capturer =
|
|
|
|
|
[[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this];
|
|
|
|
|
|
2016-07-28 14:52:55 -07:00
|
|
|
std::vector<cricket::VideoFormat> supported_formats;
|
2016-08-19 01:24:46 -07:00
|
|
|
int framerate = 30;
|
|
|
|
|
|
2016-07-28 14:52:55 -07:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
|
if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) {
|
2016-08-03 12:57:09 -07:00
|
|
|
set_enable_video_adapter(false);
|
2016-08-19 01:24:46 -07:00
|
|
|
framerate = 15;
|
2016-07-28 14:52:55 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
2016-08-19 01:24:46 -07:00
|
|
|
|
|
|
|
|
for (const auto preset : kAvailablePresets) {
|
|
|
|
|
if ([_capturer.captureSession canSetSessionPreset:preset.sessionPreset]) {
|
|
|
|
|
const auto format = cricket::VideoFormat(
|
|
|
|
|
preset.width,
|
|
|
|
|
preset.height,
|
|
|
|
|
cricket::VideoFormat::FpsToInterval(framerate),
|
|
|
|
|
cricket::FOURCC_NV12);
|
|
|
|
|
supported_formats.push_back(format);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-28 14:52:55 -07:00
|
|
|
SetSupportedFormats(supported_formats);
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AVFoundationVideoCapturer::~AVFoundationVideoCapturer() {
|
|
|
|
|
_capturer = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cricket::CaptureState AVFoundationVideoCapturer::Start(
|
|
|
|
|
const cricket::VideoFormat& format) {
|
|
|
|
|
if (!_capturer) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to create AVFoundation capturer.";
|
|
|
|
|
return cricket::CaptureState::CS_FAILED;
|
|
|
|
|
}
|
|
|
|
|
if (_capturer.isRunning) {
|
|
|
|
|
LOG(LS_ERROR) << "The capturer is already running.";
|
|
|
|
|
return cricket::CaptureState::CS_FAILED;
|
|
|
|
|
}
|
2016-08-19 01:24:46 -07:00
|
|
|
|
|
|
|
|
NSString *desiredPreset = GetSessionPresetForVideoFormat(format);
|
|
|
|
|
RTC_DCHECK(desiredPreset);
|
|
|
|
|
|
|
|
|
|
[_capturer.captureSession beginConfiguration];
|
|
|
|
|
if (![_capturer.captureSession canSetSessionPreset:desiredPreset]) {
|
|
|
|
|
LOG(LS_ERROR) << "Unsupported video format.";
|
|
|
|
|
[_capturer.captureSession commitConfiguration];
|
2016-01-21 11:44:55 -08:00
|
|
|
return cricket::CaptureState::CS_FAILED;
|
|
|
|
|
}
|
2016-08-19 01:24:46 -07:00
|
|
|
_capturer.captureSession.sessionPreset = desiredPreset;
|
|
|
|
|
[_capturer.captureSession commitConfiguration];
|
2016-01-21 11:44:55 -08:00
|
|
|
|
|
|
|
|
SetCaptureFormat(&format);
|
|
|
|
|
// This isn't super accurate because it takes a while for the AVCaptureSession
|
|
|
|
|
// to spin up, and this call returns async.
|
|
|
|
|
// TODO(tkchin): make this better.
|
2016-03-31 17:14:04 -07:00
|
|
|
[_capturer start];
|
2016-01-21 11:44:55 -08:00
|
|
|
SetCaptureState(cricket::CaptureState::CS_RUNNING);
|
|
|
|
|
|
2016-08-19 01:24:46 -07:00
|
|
|
// Adjust the framerate for all capture devices.
|
|
|
|
|
const auto fps = cricket::VideoFormat::IntervalToFps(format.interval);
|
|
|
|
|
AVCaptureDevice *activeDevice = [_capturer getActiveCaptureDevice];
|
|
|
|
|
[_capturer setMinFrameDuration:CMTimeMake(1, fps)forDevice:activeDevice];
|
|
|
|
|
|
2016-01-21 11:44:55 -08:00
|
|
|
return cricket::CaptureState::CS_STARTING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AVFoundationVideoCapturer::Stop() {
|
2016-03-31 17:14:04 -07:00
|
|
|
[_capturer stop];
|
2016-01-21 11:44:55 -08:00
|
|
|
SetCaptureFormat(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AVFoundationVideoCapturer::IsRunning() {
|
|
|
|
|
return _capturer.isRunning;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AVCaptureSession* AVFoundationVideoCapturer::GetCaptureSession() {
|
|
|
|
|
return _capturer.captureSession;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 20:55:22 -07:00
|
|
|
bool AVFoundationVideoCapturer::CanUseBackCamera() const {
|
|
|
|
|
return _capturer.canUseBackCamera;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 11:44:55 -08:00
|
|
|
void AVFoundationVideoCapturer::SetUseBackCamera(bool useBackCamera) {
|
|
|
|
|
_capturer.useBackCamera = useBackCamera;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AVFoundationVideoCapturer::GetUseBackCamera() const {
|
|
|
|
|
return _capturer.useBackCamera;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AVFoundationVideoCapturer::CaptureSampleBuffer(
|
2016-08-25 03:25:04 -07:00
|
|
|
CMSampleBufferRef sample_buffer, webrtc::VideoRotation rotation) {
|
|
|
|
|
if (CMSampleBufferGetNumSamples(sample_buffer) != 1 ||
|
|
|
|
|
!CMSampleBufferIsValid(sample_buffer) ||
|
|
|
|
|
!CMSampleBufferDataIsReady(sample_buffer)) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 03:25:04 -07:00
|
|
|
CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(sample_buffer);
|
2016-03-31 17:14:04 -07:00
|
|
|
if (image_buffer == NULL) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 08:12:17 -07:00
|
|
|
rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer =
|
|
|
|
|
new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(image_buffer);
|
|
|
|
|
|
|
|
|
|
const int captured_width = buffer->width();
|
|
|
|
|
const int captured_height = buffer->height();
|
|
|
|
|
|
|
|
|
|
int adapted_width;
|
|
|
|
|
int adapted_height;
|
|
|
|
|
int crop_width;
|
|
|
|
|
int crop_height;
|
|
|
|
|
int crop_x;
|
|
|
|
|
int crop_y;
|
|
|
|
|
int64_t translated_camera_time_us;
|
|
|
|
|
|
|
|
|
|
if (!AdaptFrame(captured_width, captured_height,
|
2016-09-01 15:15:00 +02:00
|
|
|
rtc::TimeNanos() / rtc::kNumNanosecsPerMicrosec,
|
2016-07-14 08:12:17 -07:00
|
|
|
rtc::TimeMicros(), &adapted_width, &adapted_height,
|
|
|
|
|
&crop_width, &crop_height, &crop_x, &crop_y,
|
|
|
|
|
&translated_camera_time_us)) {
|
2016-01-21 11:44:55 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 08:12:17 -07:00
|
|
|
if (adapted_width != captured_width || crop_width != captured_width ||
|
2016-08-25 03:25:04 -07:00
|
|
|
adapted_height != captured_height || crop_height != captured_height ||
|
|
|
|
|
(apply_rotation() && rotation != webrtc::kVideoRotation_0)) {
|
2016-07-14 08:12:17 -07:00
|
|
|
// TODO(magjed): Avoid converting to I420.
|
|
|
|
|
rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer(
|
|
|
|
|
_buffer_pool.CreateBuffer(adapted_width, adapted_height));
|
|
|
|
|
scaled_buffer->CropAndScaleFrom(buffer->NativeToI420Buffer(), crop_x,
|
|
|
|
|
crop_y, crop_width, crop_height);
|
2016-08-25 03:25:04 -07:00
|
|
|
if (!apply_rotation() || rotation == webrtc::kVideoRotation_0) {
|
|
|
|
|
buffer = scaled_buffer;
|
|
|
|
|
} else {
|
|
|
|
|
// Applying rotation is only supported for legacy reasons and performance
|
|
|
|
|
// is not critical here.
|
2016-09-15 07:20:40 -07:00
|
|
|
rtc::scoped_refptr<webrtc::I420Buffer> rotated_buffer(
|
|
|
|
|
(rotation == webrtc::kVideoRotation_180)
|
|
|
|
|
? I420Buffer::Create(adapted_width, adapted_height)
|
|
|
|
|
: I420Buffer::Create(adapted_height, adapted_width));
|
|
|
|
|
libyuv::I420Rotate(
|
|
|
|
|
scaled_buffer->DataY(), scaled_buffer->StrideY(),
|
|
|
|
|
scaled_buffer->DataU(), scaled_buffer->StrideU(),
|
|
|
|
|
scaled_buffer->DataV(), scaled_buffer->StrideV(),
|
|
|
|
|
rotated_buffer->MutableDataY(), rotated_buffer->StrideY(),
|
|
|
|
|
rotated_buffer->MutableDataU(), rotated_buffer->StrideU(),
|
|
|
|
|
rotated_buffer->MutableDataV(), rotated_buffer->StrideV(),
|
|
|
|
|
crop_width, crop_height,
|
|
|
|
|
static_cast<libyuv::RotationMode>(rotation));
|
|
|
|
|
buffer = rotated_buffer;
|
2016-08-25 03:25:04 -07:00
|
|
|
}
|
2016-07-14 08:12:17 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-25 03:25:04 -07:00
|
|
|
OnFrame(cricket::WebRtcVideoFrame(buffer, rotation,
|
2016-08-01 13:35:55 -07:00
|
|
|
translated_camera_time_us, 0),
|
2016-07-14 08:12:17 -07:00
|
|
|
captured_width, captured_height);
|
2016-01-21 11:44:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|