Moving src/webrtc into src/.

In order to eliminate the WebRTC Subtree mirror in Chromium, 
WebRTC is moving the content of the src/webrtc directory up
to the src/ directory.

NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
TBR=tommi@webrtc.org

Bug: chromium:611808
Change-Id: Iac59c5b51b950f174119565bac87955a7994bc38
Reviewed-on: https://webrtc-review.googlesource.com/1560
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Henrik Kjellander <kjellander@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#19845}
This commit is contained in:
Mirko Bonadei
2017-09-15 06:15:48 +02:00
committed by Commit Bot
parent 6674846b4a
commit bb547203bf
4576 changed files with 1092 additions and 1196 deletions

View File

@ -0,0 +1,23 @@
/*
* Copyright 2017 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.
*/
#import <AVFoundation/AVFoundation.h>
#import <CoreMedia/CoreMedia.h>
NS_ASSUME_NONNULL_BEGIN
@interface AVCaptureSession (DevicePosition)
// Check the image's EXIF for the camera the image came from.
+ (AVCaptureDevicePosition)devicePositionForSampleBuffer:(CMSampleBufferRef)sampleBuffer;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,51 @@
/*
* Copyright 2017 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.
*/
#import "AVCaptureSession+DevicePosition.h"
BOOL CFStringContainsString(CFStringRef theString, CFStringRef stringToFind) {
return CFStringFindWithOptions(theString,
stringToFind,
CFRangeMake(0, CFStringGetLength(theString)),
kCFCompareCaseInsensitive,
nil);
}
@implementation AVCaptureSession (DevicePosition)
+ (AVCaptureDevicePosition)devicePositionForSampleBuffer:(CMSampleBufferRef)sampleBuffer {
// Check the image's EXIF for the camera the image came from.
AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionUnspecified;
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(
kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
if (attachments) {
int size = CFDictionaryGetCount(attachments);
if (size > 0) {
CFDictionaryRef cfExifDictVal = nil;
if (CFDictionaryGetValueIfPresent(
attachments, (const void *)CFSTR("{Exif}"), (const void **)&cfExifDictVal)) {
CFStringRef cfLensModelStrVal;
if (CFDictionaryGetValueIfPresent(cfExifDictVal,
(const void *)CFSTR("LensModel"),
(const void **)&cfLensModelStrVal)) {
if (CFStringContainsString(cfLensModelStrVal, CFSTR("front"))) {
cameraPosition = AVCaptureDevicePositionFront;
} else if (CFStringContainsString(cfLensModelStrVal, CFSTR("back"))) {
cameraPosition = AVCaptureDevicePositionBack;
}
}
}
}
CFRelease(attachments);
}
return cameraPosition;
}
@end

View File

@ -0,0 +1,49 @@
/*
* Copyright 2016 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.
*/
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#ifdef __cplusplus
#include "avfoundationvideocapturer.h"
#endif
NS_ASSUME_NONNULL_BEGIN
// This class is an implementation detail of AVFoundationVideoCapturer and handles
// the ObjC integration with the AVFoundation APIs.
// It is meant to be owned by an instance of AVFoundationVideoCapturer.
// The reason for this is 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;
@property(nonatomic, readonly) dispatch_queue_t frameQueue;
@property(nonatomic, readonly) BOOL canUseBackCamera;
@property(nonatomic, assign) BOOL useBackCamera; // Defaults to NO.
@property(atomic, assign) BOOL isRunning; // Whether the capture session is running.
@property(atomic, assign) BOOL hasStarted; // Whether we have an unmatched start.
// 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;
- (AVCaptureDevice *)getActiveCaptureDevice;
- (nullable AVCaptureDevice *)frontCaptureDevice;
- (nullable AVCaptureDevice *)backCaptureDevice;
// Starts and stops the capture session asynchronously. We cannot do this
// synchronously without blocking a WebRTC thread.
- (void)start;
- (void)stop;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,518 @@
/*
* Copyright 2016 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.
*/
#import "RTCAVFoundationVideoCapturerInternal.h"
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#import "WebRTC/UIDevice+RTCDevice.h"
#endif
#import "AVCaptureSession+DevicePosition.h"
#import "RTCDispatcher+Private.h"
#import "WebRTC/RTCLogging.h"
#include "avfoundationformatmapper.h"
@implementation RTCAVFoundationVideoCapturerInternal {
// Keep pointers to inputs for convenience.
AVCaptureDeviceInput *_frontCameraInput;
AVCaptureDeviceInput *_backCameraInput;
AVCaptureVideoDataOutput *_videoDataOutput;
// The cricket::VideoCapturer that owns this class. Should never be NULL.
webrtc::AVFoundationVideoCapturer *_capturer;
BOOL _hasRetriedOnFatalError;
BOOL _isRunning;
BOOL _hasStarted;
rtc::CriticalSection _crit;
#if TARGET_OS_IPHONE
UIDeviceOrientation _orientation;
#endif
}
@synthesize captureSession = _captureSession;
@synthesize frameQueue = _frameQueue;
@synthesize useBackCamera = _useBackCamera;
@synthesize isRunning = _isRunning;
@synthesize hasStarted = _hasStarted;
// This is called from the thread that creates the video source, which is likely
// the main thread.
- (instancetype)initWithCapturer:(webrtc::AVFoundationVideoCapturer *)capturer {
RTC_DCHECK(capturer);
if (self = [super init]) {
_capturer = capturer;
// 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.
if (![self setupCaptureSession]) {
return nil;
}
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if TARGET_OS_IPHONE
_orientation = UIDeviceOrientationPortrait;
[center addObserver:self
selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[center addObserver:self
selector:@selector(handleCaptureSessionInterruption:)
name:AVCaptureSessionWasInterruptedNotification
object:_captureSession];
[center addObserver:self
selector:@selector(handleCaptureSessionInterruptionEnded:)
name:AVCaptureSessionInterruptionEndedNotification
object:_captureSession];
[center addObserver:self
selector:@selector(handleApplicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
#endif
[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];
}
return self;
}
- (void)dealloc {
RTC_DCHECK(!self.hasStarted);
[[NSNotificationCenter defaultCenter] removeObserver:self];
_capturer = nullptr;
}
- (AVCaptureSession *)captureSession {
return _captureSession;
}
- (AVCaptureDevice *)getActiveCaptureDevice {
return self.useBackCamera ? _backCameraInput.device : _frontCameraInput.device;
}
- (nullable AVCaptureDevice *)frontCaptureDevice {
return _frontCameraInput.device;
}
- (nullable AVCaptureDevice *)backCaptureDevice {
return _backCameraInput.device;
}
- (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;
}
// Called from any thread (likely main thread).
- (BOOL)canUseBackCamera {
return _backCameraInput != nil;
}
// Called from any thread (likely main thread).
- (BOOL)useBackCamera {
@synchronized(self) {
return _useBackCamera;
}
}
// Called from any thread (likely main thread).
- (void)setUseBackCamera:(BOOL)useBackCamera {
if (!self.canUseBackCamera) {
if (useBackCamera) {
RTCLogWarning(@"No rear-facing camera exists or it cannot be used;"
"not switching.");
}
return;
}
@synchronized(self) {
if (_useBackCamera == useBackCamera) {
return;
}
_useBackCamera = useBackCamera;
[self updateSessionInputForUseBackCamera:useBackCamera];
}
}
// Called from WebRTC thread.
- (void)start {
if (self.hasStarted) {
return;
}
self.hasStarted = YES;
[RTCDispatcher
dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
block:^{
[self updateOrientation];
#if TARGET_OS_IPHONE
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
#endif
AVCaptureSession *captureSession = self.captureSession;
[captureSession startRunning];
}];
}
// Called from same thread as start.
- (void)stop {
if (!self.hasStarted) {
return;
}
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.
[RTCDispatcher
dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
block:^{
[_videoDataOutput setSampleBufferDelegate:nil queue:nullptr];
[_captureSession stopRunning];
#if TARGET_OS_IPHONE
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
#endif
}];
}
#pragma mark iOS notifications
#if TARGET_OS_IPHONE
- (void)deviceOrientationDidChange:(NSNotification *)notification {
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
block:^{
[self updateOrientation];
}];
}
#endif
#pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
NSParameterAssert(captureOutput == _videoDataOutput);
if (!self.hasStarted) {
return;
}
#if TARGET_OS_IPHONE
// Default to portrait orientation on iPhone.
webrtc::VideoRotation rotation = webrtc::kVideoRotation_90;
BOOL usingFrontCamera = NO;
// Check the image's EXIF for the camera the image came from as the image could have been
// delayed as we set alwaysDiscardsLateVideoFrames to NO.
AVCaptureDevicePosition cameraPosition =
[AVCaptureSession devicePositionForSampleBuffer:sampleBuffer];
if (cameraPosition != AVCaptureDevicePositionUnspecified) {
usingFrontCamera = AVCaptureDevicePositionFront == cameraPosition;
} else {
AVCaptureDeviceInput *deviceInput =
(AVCaptureDeviceInput *)((AVCaptureInputPort *)connection.inputPorts.firstObject).input;
usingFrontCamera = AVCaptureDevicePositionFront == deviceInput.device.position;
}
switch (_orientation) {
case UIDeviceOrientationPortrait:
rotation = webrtc::kVideoRotation_90;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = webrtc::kVideoRotation_270;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = usingFrontCamera ? webrtc::kVideoRotation_180 : webrtc::kVideoRotation_0;
break;
case UIDeviceOrientationLandscapeRight:
rotation = usingFrontCamera ? webrtc::kVideoRotation_0 : webrtc::kVideoRotation_180;
break;
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationUnknown:
// Ignore.
break;
}
#else
// No rotation on Mac.
webrtc::VideoRotation rotation = webrtc::kVideoRotation_0;
#endif
_capturer->CaptureSampleBuffer(sampleBuffer, rotation);
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
RTCLogError(@"Dropped sample buffer.");
}
#pragma mark - AVCaptureSession notifications
- (void)handleCaptureSessionInterruption:(NSNotification *)notification {
NSString *reasonString = nil;
#if defined(__IPHONE_9_0) && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
if ([UIDevice isIOS9OrLater]) {
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 {
NSError *error = [notification.userInfo objectForKey:AVCaptureSessionErrorKey];
RTCLogError(@"Capture session runtime error: %@", error);
[RTCDispatcher dispatchAsyncOnType:RTCDispatcherTypeCaptureSession
block:^{
#if TARGET_OS_IPHONE
if (error.code == AVErrorMediaServicesWereReset) {
[self handleNonFatalError];
} else {
[self handleFatalError];
}
#else
[self handleFatalError];
#endif
}];
}
- (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];
}
}];
}
#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
#pragma mark - Private
- (BOOL)setupCaptureSession {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
#if defined(WEBRTC_IOS)
captureSession.usesApplicationAudioSession = NO;
#endif
// Add the output.
AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput];
if (![captureSession canAddOutput:videoDataOutput]) {
RTCLogError(@"Video data output unsupported.");
return NO;
}
[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;
}
// Add the inputs.
if (![captureSession canAddInput:frontCameraInput] ||
(backCameraInput && ![captureSession canAddInput:backCameraInput])) {
RTCLogError(@"Session does not support capture inputs.");
return NO;
}
AVCaptureDeviceInput *input = self.useBackCamera ? backCameraInput : frontCameraInput;
[captureSession addInput:input];
_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 *)
// TODO(denicija): Remove this color conversion and use the original capture format directly.
kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
};
videoDataOutput.alwaysDiscardsLateVideoFrames = NO;
[videoDataOutput setSampleBufferDelegate:self queue:self.frameQueue];
_videoDataOutput = videoDataOutput;
}
return _videoDataOutput;
}
- (AVCaptureDevice *)videoCaptureDeviceForPosition:(AVCaptureDevicePosition)position {
for (AVCaptureDevice *captureDevice in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if (captureDevice.position == position) {
return captureDevice;
}
}
return nil;
}
- (AVCaptureDeviceInput *)frontCameraInput {
if (!_frontCameraInput) {
#if TARGET_OS_IPHONE
AVCaptureDevice *frontCameraDevice =
[self videoCaptureDeviceForPosition:AVCaptureDevicePositionFront];
#else
AVCaptureDevice *frontCameraDevice =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
#endif
if (!frontCameraDevice) {
RTCLogWarning(@"Failed to find front capture device.");
return nil;
}
NSError *error = nil;
AVCaptureDeviceInput *frontCameraInput =
[AVCaptureDeviceInput deviceInputWithDevice:frontCameraDevice error:&error];
if (!frontCameraInput) {
RTCLogError(@"Failed to create front camera input: %@", error.localizedDescription);
return nil;
}
_frontCameraInput = frontCameraInput;
}
return _frontCameraInput;
}
- (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;
}
return _backCameraInput;
}
// Called from capture session queue.
- (void)updateOrientation {
#if TARGET_OS_IPHONE
_orientation = [UIDevice currentDevice].orientation;
#endif
}
// 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];
AVCaptureDevice *newDevice = newInput.device;
const cricket::VideoFormat *format = _capturer->GetCaptureFormat();
webrtc::SetFormatForCaptureDevice(newDevice, _captureSession, *format);
[_captureSession commitConfiguration];
}];
}
@end

View File

@ -0,0 +1,188 @@
/*
* Copyright 2017 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.
*/
#import "WebRTC/RTCVideoFrameBuffer.h"
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/logging.h"
@implementation RTCCVPixelBuffer {
int _width;
int _height;
int _bufferWidth;
int _bufferHeight;
int _cropWidth;
int _cropHeight;
}
@synthesize pixelBuffer = _pixelBuffer;
@synthesize cropX = _cropX;
@synthesize cropY = _cropY;
- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer {
return [self initWithPixelBuffer:pixelBuffer
adaptedWidth:CVPixelBufferGetWidth(pixelBuffer)
adaptedHeight:CVPixelBufferGetHeight(pixelBuffer)
cropWidth:CVPixelBufferGetWidth(pixelBuffer)
cropHeight:CVPixelBufferGetHeight(pixelBuffer)
cropX:0
cropY:0];
}
- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
adaptedWidth:(int)adaptedWidth
adaptedHeight:(int)adaptedHeight
cropWidth:(int)cropWidth
cropHeight:(int)cropHeight
cropX:(int)cropX
cropY:(int)cropY {
if (self = [super init]) {
_width = adaptedWidth;
_height = adaptedHeight;
_pixelBuffer = pixelBuffer;
_bufferWidth = CVPixelBufferGetWidth(_pixelBuffer);
_bufferHeight = CVPixelBufferGetHeight(_pixelBuffer);
_cropWidth = cropWidth;
_cropHeight = cropHeight;
// Can only crop at even pixels.
_cropX = cropX & ~1;
_cropY = cropY & ~1;
CVBufferRetain(_pixelBuffer);
}
return self;
}
- (void)dealloc {
CVBufferRelease(_pixelBuffer);
}
- (int)width {
return _width;
}
- (int)height {
return _height;
}
- (BOOL)requiresCropping {
return _cropWidth != _bufferWidth || _cropHeight != _bufferHeight;
}
- (BOOL)requiresScalingToWidth:(int)width height:(int)height {
return _cropWidth != width || _cropHeight != height;
}
- (int)bufferSizeForCroppingAndScalingToWidth:(int)width height:(int)height {
int srcChromaWidth = (_cropWidth + 1) / 2;
int srcChromaHeight = (_cropHeight + 1) / 2;
int dstChromaWidth = (width + 1) / 2;
int dstChromaHeight = (height + 1) / 2;
return srcChromaWidth * srcChromaHeight * 2 + dstChromaWidth * dstChromaHeight * 2;
}
- (BOOL)cropAndScaleTo:(CVPixelBufferRef)outputPixelBuffer withTempBuffer:(uint8_t*)tmpBuffer {
// Prepare output pointers.
RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(outputPixelBuffer),
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
CVReturn cvRet = CVPixelBufferLockBaseAddress(outputPixelBuffer, 0);
if (cvRet != kCVReturnSuccess) {
LOG(LS_ERROR) << "Failed to lock base address: " << cvRet;
return NO;
}
const int dstWidth = CVPixelBufferGetWidth(outputPixelBuffer);
const int dstHeight = CVPixelBufferGetHeight(outputPixelBuffer);
uint8_t* dstY =
reinterpret_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(outputPixelBuffer, 0));
const int dstYStride = CVPixelBufferGetBytesPerRowOfPlane(outputPixelBuffer, 0);
uint8_t* dstUV =
reinterpret_cast<uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(outputPixelBuffer, 1));
const int dstUVStride = CVPixelBufferGetBytesPerRowOfPlane(outputPixelBuffer, 1);
// Prepare source pointers.
const OSType srcPixelFormat = CVPixelBufferGetPixelFormatType(_pixelBuffer);
RTC_DCHECK(srcPixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ||
srcPixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
CVPixelBufferLockBaseAddress(_pixelBuffer, kCVPixelBufferLock_ReadOnly);
const uint8_t* srcY =
static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(_pixelBuffer, 0));
const int srcYStride = CVPixelBufferGetBytesPerRowOfPlane(_pixelBuffer, 0);
const uint8_t* srcUV =
static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(_pixelBuffer, 1));
const int srcUVStride = CVPixelBufferGetBytesPerRowOfPlane(_pixelBuffer, 1);
// Crop just by modifying pointers.
srcY += srcYStride * _cropY + _cropX;
srcUV += srcUVStride * (_cropY / 2) + _cropX;
webrtc::NV12Scale(tmpBuffer,
srcY,
srcYStride,
srcUV,
srcUVStride,
_cropWidth,
_cropHeight,
dstY,
dstYStride,
dstUV,
dstUVStride,
dstWidth,
dstHeight);
CVPixelBufferUnlockBaseAddress(_pixelBuffer, kCVPixelBufferLock_ReadOnly);
CVPixelBufferUnlockBaseAddress(outputPixelBuffer, 0);
return YES;
}
- (id<RTCI420Buffer>)toI420 {
const OSType pixelFormat = CVPixelBufferGetPixelFormatType(_pixelBuffer);
RTC_DCHECK(pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ||
pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
CVPixelBufferLockBaseAddress(_pixelBuffer, kCVPixelBufferLock_ReadOnly);
const uint8_t* srcY =
static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(_pixelBuffer, 0));
const int srcYStride = CVPixelBufferGetBytesPerRowOfPlane(_pixelBuffer, 0);
const uint8_t* srcUV =
static_cast<const uint8_t*>(CVPixelBufferGetBaseAddressOfPlane(_pixelBuffer, 1));
const int srcUVStride = CVPixelBufferGetBytesPerRowOfPlane(_pixelBuffer, 1);
// Crop just by modifying pointers.
srcY += srcYStride * _cropY + _cropX;
srcUV += srcUVStride * (_cropY / 2) + _cropX;
// TODO(magjed): Use a frame buffer pool.
webrtc::NV12ToI420Scaler nv12ToI420Scaler;
RTCMutableI420Buffer* i420Buffer =
[[RTCMutableI420Buffer alloc] initWithWidth:[self width] height:[self height]];
nv12ToI420Scaler.NV12ToI420Scale(srcY,
srcYStride,
srcUV,
srcUVStride,
_cropWidth,
_cropHeight,
i420Buffer.mutableDataY,
i420Buffer.strideY,
i420Buffer.mutableDataU,
i420Buffer.strideU,
i420Buffer.mutableDataV,
i420Buffer.strideV,
i420Buffer.width,
i420Buffer.height);
CVPixelBufferUnlockBaseAddress(_pixelBuffer, kCVPixelBufferLock_ReadOnly);
return i420Buffer;
}
@end

View File

@ -0,0 +1,23 @@
/*
* Copyright 2017 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.
*/
#import "WebRTC/RTCVideoViewShading.h"
NS_ASSUME_NONNULL_BEGIN
/** Default RTCVideoViewShading that will be used in RTCNSGLVideoView and
* RTCEAGLVideoView if no external shader is specified. This shader will render
* the video in a rectangle without any color or geometric transformations.
*/
@interface RTCDefaultShader : NSObject<RTCVideoViewShading>
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,207 @@
/*
* Copyright 2017 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.
*/
#import "RTCDefaultShader.h"
#if TARGET_OS_IPHONE
#import <OpenGLES/ES3/gl.h>
#else
#import <OpenGL/gl3.h>
#endif
#import "RTCOpenGLDefines.h"
#import "RTCShader.h"
#import "WebRTC/RTCLogging.h"
#include "webrtc/api/optional.h"
static const int kYTextureUnit = 0;
static const int kUTextureUnit = 1;
static const int kVTextureUnit = 2;
static const int kUvTextureUnit = 1;
// Fragment shader converts YUV values from input textures into a final RGB
// pixel. The conversion formula is from http://www.fourcc.org/fccyvrgb.php.
static const char kI420FragmentShaderSource[] =
SHADER_VERSION
"precision highp float;"
FRAGMENT_SHADER_IN " vec2 v_texcoord;\n"
"uniform lowp sampler2D s_textureY;\n"
"uniform lowp sampler2D s_textureU;\n"
"uniform lowp sampler2D s_textureV;\n"
FRAGMENT_SHADER_OUT
"void main() {\n"
" float y, u, v, r, g, b;\n"
" y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n"
" u = " FRAGMENT_SHADER_TEXTURE "(s_textureU, v_texcoord).r;\n"
" v = " FRAGMENT_SHADER_TEXTURE "(s_textureV, v_texcoord).r;\n"
" u = u - 0.5;\n"
" v = v - 0.5;\n"
" r = y + 1.403 * v;\n"
" g = y - 0.344 * u - 0.714 * v;\n"
" b = y + 1.770 * u;\n"
" " FRAGMENT_SHADER_COLOR " = vec4(r, g, b, 1.0);\n"
" }\n";
static const char kNV12FragmentShaderSource[] =
SHADER_VERSION
"precision mediump float;"
FRAGMENT_SHADER_IN " vec2 v_texcoord;\n"
"uniform lowp sampler2D s_textureY;\n"
"uniform lowp sampler2D s_textureUV;\n"
FRAGMENT_SHADER_OUT
"void main() {\n"
" mediump float y;\n"
" mediump vec2 uv;\n"
" y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n"
" uv = " FRAGMENT_SHADER_TEXTURE "(s_textureUV, v_texcoord).ra -\n"
" vec2(0.5, 0.5);\n"
" " FRAGMENT_SHADER_COLOR " = vec4(y + 1.403 * uv.y,\n"
" y - 0.344 * uv.x - 0.714 * uv.y,\n"
" y + 1.770 * uv.x,\n"
" 1.0);\n"
" }\n";
@implementation RTCDefaultShader {
GLuint _vertexBuffer;
GLuint _vertexArray;
// Store current rotation and only upload new vertex data when rotation changes.
rtc::Optional<RTCVideoRotation> _currentRotation;
GLuint _i420Program;
GLuint _nv12Program;
}
- (void)dealloc {
glDeleteProgram(_i420Program);
glDeleteProgram(_nv12Program);
glDeleteBuffers(1, &_vertexBuffer);
glDeleteVertexArrays(1, &_vertexArray);
}
- (BOOL)createAndSetupI420Program {
NSAssert(!_i420Program, @"I420 program already created");
_i420Program = RTCCreateProgramFromFragmentSource(kI420FragmentShaderSource);
if (!_i420Program) {
return NO;
}
GLint ySampler = glGetUniformLocation(_i420Program, "s_textureY");
GLint uSampler = glGetUniformLocation(_i420Program, "s_textureU");
GLint vSampler = glGetUniformLocation(_i420Program, "s_textureV");
if (ySampler < 0 || uSampler < 0 || vSampler < 0) {
RTCLog(@"Failed to get uniform variable locations in I420 shader");
glDeleteProgram(_i420Program);
_i420Program = 0;
return NO;
}
glUseProgram(_i420Program);
glUniform1i(ySampler, kYTextureUnit);
glUniform1i(uSampler, kUTextureUnit);
glUniform1i(vSampler, kVTextureUnit);
return YES;
}
- (BOOL)createAndSetupNV12Program {
NSAssert(!_nv12Program, @"NV12 program already created");
_nv12Program = RTCCreateProgramFromFragmentSource(kNV12FragmentShaderSource);
if (!_nv12Program) {
return NO;
}
GLint ySampler = glGetUniformLocation(_nv12Program, "s_textureY");
GLint uvSampler = glGetUniformLocation(_nv12Program, "s_textureUV");
if (ySampler < 0 || uvSampler < 0) {
RTCLog(@"Failed to get uniform variable locations in NV12 shader");
glDeleteProgram(_nv12Program);
_nv12Program = 0;
return NO;
}
glUseProgram(_nv12Program);
glUniform1i(ySampler, kYTextureUnit);
glUniform1i(uvSampler, kUvTextureUnit);
return YES;
}
- (BOOL)prepareVertexBufferWithRotation:(RTCVideoRotation)rotation {
if (!_vertexBuffer && !RTCCreateVertexBuffer(&_vertexBuffer, &_vertexArray)) {
RTCLog(@"Failed to setup vertex buffer");
return NO;
}
#if !TARGET_OS_IPHONE
glBindVertexArray(_vertexArray);
#endif
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
if (!_currentRotation || rotation != *_currentRotation) {
_currentRotation = rtc::Optional<RTCVideoRotation>(rotation);
RTCSetVertexData(*_currentRotation);
}
return YES;
}
- (void)applyShadingForFrameWithWidth:(int)width
height:(int)height
rotation:(RTCVideoRotation)rotation
yPlane:(GLuint)yPlane
uPlane:(GLuint)uPlane
vPlane:(GLuint)vPlane {
if (![self prepareVertexBufferWithRotation:rotation]) {
return;
}
if (!_i420Program && ![self createAndSetupI420Program]) {
RTCLog(@"Failed to setup I420 program");
return;
}
glUseProgram(_i420Program);
glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + kYTextureUnit));
glBindTexture(GL_TEXTURE_2D, yPlane);
glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + kUTextureUnit));
glBindTexture(GL_TEXTURE_2D, uPlane);
glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + kVTextureUnit));
glBindTexture(GL_TEXTURE_2D, vPlane);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
- (void)applyShadingForFrameWithWidth:(int)width
height:(int)height
rotation:(RTCVideoRotation)rotation
yPlane:(GLuint)yPlane
uvPlane:(GLuint)uvPlane {
if (![self prepareVertexBufferWithRotation:rotation]) {
return;
}
if (!_nv12Program && ![self createAndSetupNV12Program]) {
RTCLog(@"Failed to setup NV12 shader");
return;
}
glUseProgram(_nv12Program);
glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + kYTextureUnit));
glBindTexture(GL_TEXTURE_2D, yPlane);
glActiveTexture(static_cast<GLenum>(GL_TEXTURE0 + kUvTextureUnit));
glBindTexture(GL_TEXTURE_2D, uvPlane);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
@end

View File

@ -0,0 +1,24 @@
/*
* Copyright 2017 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.
*/
#import "WebRTC/RTCVideoFrameBuffer.h"
#include "webrtc/api/video/i420_buffer.h"
NS_ASSUME_NONNULL_BEGIN
@interface RTCI420Buffer ()
/** Initialize an RTCI420Buffer with its backing I420BufferInterface. */
- (instancetype)initWithFrameBuffer:(rtc::scoped_refptr<webrtc::I420BufferInterface>)i420Buffer;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,108 @@
/*
* Copyright 2017 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.
*/
#import "WebRTC/RTCVideoFrameBuffer.h"
#include "webrtc/api/video/i420_buffer.h"
@implementation RTCI420Buffer {
@protected
rtc::scoped_refptr<webrtc::I420BufferInterface> _i420Buffer;
}
- (instancetype)initWithWidth:(int)width height:(int)height {
if (self = [super init]) {
_i420Buffer = webrtc::I420Buffer::Create(width, height);
}
return self;
}
- (instancetype)initWithWidth:(int)width
height:(int)height
strideY:(int)strideY
strideU:(int)strideU
strideV:(int)strideV {
if (self = [super init]) {
_i420Buffer = webrtc::I420Buffer::Create(width, height, strideY, strideU, strideV);
}
return self;
}
- (instancetype)initWithFrameBuffer:(rtc::scoped_refptr<webrtc::I420BufferInterface>)i420Buffer {
if (self = [super init]) {
_i420Buffer = i420Buffer;
}
return self;
}
- (int)width {
return _i420Buffer->width();
}
- (int)height {
return _i420Buffer->height();
}
- (int)strideY {
return _i420Buffer->StrideY();
}
- (int)strideU {
return _i420Buffer->StrideU();
}
- (int)strideV {
return _i420Buffer->StrideV();
}
- (int)chromaWidth {
return _i420Buffer->ChromaWidth();
}
- (int)chromaHeight {
return _i420Buffer->ChromaHeight();
}
- (const uint8_t *)dataY {
return _i420Buffer->DataY();
}
- (const uint8_t *)dataU {
return _i420Buffer->DataU();
}
- (const uint8_t *)dataV {
return _i420Buffer->DataV();
}
- (id<RTCI420Buffer>)toI420 {
return self;
}
@end
@implementation RTCMutableI420Buffer
- (uint8_t *)mutableDataY {
return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataY();
}
- (uint8_t *)mutableDataU {
return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataU();
}
- (uint8_t *)mutableDataV {
return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataV();
}
@end

View File

@ -0,0 +1,25 @@
/*
* Copyright 2017 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.
*/
#import "RTCOpenGLDefines.h"
#import "WebRTC/RTCVideoFrame.h"
@interface RTCI420TextureCache : NSObject
@property(nonatomic, readonly) GLuint yTexture;
@property(nonatomic, readonly) GLuint uTexture;
@property(nonatomic, readonly) GLuint vTexture;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithContext:(GlContextType *)context NS_DESIGNATED_INITIALIZER;
- (void)uploadFrameToTextures:(RTCVideoFrame *)frame;
@end

View File

@ -0,0 +1,155 @@
/*
* Copyright 2017 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.
*/
#import "RTCI420TextureCache.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
#if TARGET_OS_IPHONE
#import <OpenGLES/ES3/gl.h>
#else
#import <OpenGL/gl3.h>
#endif
#include <vector>
// Two sets of 3 textures are used here, one for each of the Y, U and V planes. Having two sets
// alleviates CPU blockage in the event that the GPU is asked to render to a texture that is already
// in use.
static const GLsizei kNumTextureSets = 2;
static const GLsizei kNumTexturesPerSet = 3;
static const GLsizei kNumTextures = kNumTexturesPerSet * kNumTextureSets;
@implementation RTCI420TextureCache {
BOOL _hasUnpackRowLength;
GLint _currentTextureSet;
// Handles for OpenGL constructs.
GLuint _textures[kNumTextures];
// Used to create a non-padded plane for GPU upload when we receive padded frames.
std::vector<uint8_t> _planeBuffer;
}
- (GLuint)yTexture {
return _textures[_currentTextureSet * kNumTexturesPerSet];
}
- (GLuint)uTexture {
return _textures[_currentTextureSet * kNumTexturesPerSet + 1];
}
- (GLuint)vTexture {
return _textures[_currentTextureSet * kNumTexturesPerSet + 2];
}
- (instancetype)initWithContext:(GlContextType *)context {
if (self = [super init]) {
#if TARGET_OS_IPHONE
_hasUnpackRowLength = (context.API == kEAGLRenderingAPIOpenGLES3);
#else
_hasUnpackRowLength = YES;
#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
[self setupTextures];
}
return self;
}
- (void)dealloc {
glDeleteTextures(kNumTextures, _textures);
}
- (void)setupTextures {
glGenTextures(kNumTextures, _textures);
// Set parameters for each of the textures we created.
for (GLsizei i = 0; i < kNumTextures; i++) {
glBindTexture(GL_TEXTURE_2D, _textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
}
- (void)uploadPlane:(const uint8_t *)plane
texture:(GLuint)texture
width:(size_t)width
height:(size_t)height
stride:(int32_t)stride {
glBindTexture(GL_TEXTURE_2D, texture);
const uint8_t *uploadPlane = plane;
if ((size_t)stride != width) {
if (_hasUnpackRowLength) {
// GLES3 allows us to specify stride.
glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
glTexImage2D(GL_TEXTURE_2D,
0,
RTC_PIXEL_FORMAT,
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
0,
RTC_PIXEL_FORMAT,
GL_UNSIGNED_BYTE,
uploadPlane);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
return;
} else {
// Make an unpadded copy and upload that instead. Quick profiling showed
// that this is faster than uploading row by row using glTexSubImage2D.
uint8_t *unpaddedPlane = _planeBuffer.data();
for (size_t y = 0; y < height; ++y) {
memcpy(unpaddedPlane + y * width, plane + y * stride, width);
}
uploadPlane = unpaddedPlane;
}
}
glTexImage2D(GL_TEXTURE_2D,
0,
RTC_PIXEL_FORMAT,
static_cast<GLsizei>(width),
static_cast<GLsizei>(height),
0,
RTC_PIXEL_FORMAT,
GL_UNSIGNED_BYTE,
uploadPlane);
}
- (void)uploadFrameToTextures:(RTCVideoFrame *)frame {
_currentTextureSet = (_currentTextureSet + 1) % kNumTextureSets;
id<RTCI420Buffer> buffer = [frame.buffer toI420];
const int chromaWidth = buffer.chromaWidth;
const int chromaHeight = buffer.chromaHeight;
if (buffer.strideY != frame.width || buffer.strideU != chromaWidth ||
buffer.strideV != chromaWidth) {
_planeBuffer.resize(buffer.width * buffer.height);
}
[self uploadPlane:buffer.dataY
texture:self.yTexture
width:buffer.width
height:buffer.height
stride:buffer.strideY];
[self uploadPlane:buffer.dataU
texture:self.uTexture
width:chromaWidth
height:chromaHeight
stride:buffer.strideU];
[self uploadPlane:buffer.dataV
texture:self.vTexture
width:chromaWidth
height:chromaHeight
stride:buffer.strideV];
}
@end

View File

@ -0,0 +1,31 @@
/*
* Copyright 2017 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.
*/
#import <GLKit/GLKit.h>
@class RTCVideoFrame;
NS_ASSUME_NONNULL_BEGIN
@interface RTCNV12TextureCache : NSObject
@property(nonatomic, readonly) GLuint yTexture;
@property(nonatomic, readonly) GLuint uvTexture;
- (instancetype)init NS_UNAVAILABLE;
- (nullable instancetype)initWithContext:(EAGLContext *)context NS_DESIGNATED_INITIALIZER;
- (BOOL)uploadFrameToTextures:(RTCVideoFrame *)frame;
- (void)releaseTextures;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,111 @@
/*
* Copyright 2017 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.
*/
#import "RTCNV12TextureCache.h"
#import "WebRTC/RTCVideoFrame.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
@implementation RTCNV12TextureCache {
CVOpenGLESTextureCacheRef _textureCache;
CVOpenGLESTextureRef _yTextureRef;
CVOpenGLESTextureRef _uvTextureRef;
}
- (GLuint)yTexture {
return CVOpenGLESTextureGetName(_yTextureRef);
}
- (GLuint)uvTexture {
return CVOpenGLESTextureGetName(_uvTextureRef);
}
- (instancetype)initWithContext:(EAGLContext *)context {
if (self = [super init]) {
CVReturn ret = CVOpenGLESTextureCacheCreate(
kCFAllocatorDefault, NULL,
#if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API
context,
#else
(__bridge void *)context,
#endif
NULL, &_textureCache);
if (ret != kCVReturnSuccess) {
self = nil;
}
}
return self;
}
- (BOOL)loadTexture:(CVOpenGLESTextureRef *)textureOut
pixelBuffer:(CVPixelBufferRef)pixelBuffer
planeIndex:(int)planeIndex
pixelFormat:(GLenum)pixelFormat {
const int width = CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex);
const int height = CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex);
if (*textureOut) {
CFRelease(*textureOut);
*textureOut = nil;
}
CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage(
kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, pixelFormat, width,
height, pixelFormat, GL_UNSIGNED_BYTE, planeIndex, textureOut);
if (ret != kCVReturnSuccess) {
CFRelease(*textureOut);
*textureOut = nil;
return NO;
}
NSAssert(CVOpenGLESTextureGetTarget(*textureOut) == GL_TEXTURE_2D,
@"Unexpected GLES texture target");
glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(*textureOut));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return YES;
}
- (BOOL)uploadFrameToTextures:(RTCVideoFrame *)frame {
NSAssert([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]],
@"frame must be CVPixelBuffer backed");
RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
CVPixelBufferRef pixelBuffer = rtcPixelBuffer.pixelBuffer;
return [self loadTexture:&_yTextureRef
pixelBuffer:pixelBuffer
planeIndex:0
pixelFormat:GL_LUMINANCE] &&
[self loadTexture:&_uvTextureRef
pixelBuffer:pixelBuffer
planeIndex:1
pixelFormat:GL_LUMINANCE_ALPHA];
}
- (void)releaseTextures {
if (_uvTextureRef) {
CFRelease(_uvTextureRef);
_uvTextureRef = nil;
}
if (_yTextureRef) {
CFRelease(_yTextureRef);
_yTextureRef = nil;
}
}
- (void)dealloc {
[self releaseTextures];
if (_textureCache) {
CFRelease(_textureCache);
_textureCache = nil;
}
}
@end

View File

@ -0,0 +1,37 @@
/*
* Copyright 2016 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.
*/
#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#define RTC_PIXEL_FORMAT GL_LUMINANCE
#define SHADER_VERSION
#define VERTEX_SHADER_IN "attribute"
#define VERTEX_SHADER_OUT "varying"
#define FRAGMENT_SHADER_IN "varying"
#define FRAGMENT_SHADER_OUT
#define FRAGMENT_SHADER_COLOR "gl_FragColor"
#define FRAGMENT_SHADER_TEXTURE "texture2D"
@class EAGLContext;
typedef EAGLContext GlContextType;
#else
#define RTC_PIXEL_FORMAT GL_RED
#define SHADER_VERSION "#version 150\n"
#define VERTEX_SHADER_IN "in"
#define VERTEX_SHADER_OUT "out"
#define FRAGMENT_SHADER_IN "in"
#define FRAGMENT_SHADER_OUT "out vec4 fragColor;\n"
#define FRAGMENT_SHADER_COLOR "fragColor"
#define FRAGMENT_SHADER_TEXTURE "texture"
@class NSOpenGLContext;
typedef NSOpenGLContext GlContextType;
#endif

View File

@ -0,0 +1,20 @@
/*
* Copyright 2016 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.
*/
#import "WebRTC/RTCVideoFrame.h"
RTC_EXTERN const char kRTCVertexShaderSource[];
RTC_EXTERN GLuint RTCCreateShader(GLenum type, const GLchar *source);
RTC_EXTERN GLuint RTCCreateProgram(GLuint vertexShader, GLuint fragmentShader);
RTC_EXTERN GLuint RTCCreateProgramFromFragmentSource(const char fragmentShaderSource[]);
RTC_EXTERN BOOL RTCCreateVertexBuffer(GLuint* vertexBuffer,
GLuint* vertexArray);
RTC_EXTERN void RTCSetVertexData(RTCVideoRotation rotation);

View File

@ -0,0 +1,189 @@
/*
* Copyright 2016 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.
*/
#import "RTCShader.h"
#if TARGET_OS_IPHONE
#import <OpenGLES/ES3/gl.h>
#else
#import <OpenGL/gl3.h>
#endif
#include <algorithm>
#include <array>
#include <memory>
#import "RTCOpenGLDefines.h"
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/logging.h"
// Vertex shader doesn't do anything except pass coordinates through.
const char kRTCVertexShaderSource[] =
SHADER_VERSION
VERTEX_SHADER_IN " vec2 position;\n"
VERTEX_SHADER_IN " vec2 texcoord;\n"
VERTEX_SHADER_OUT " vec2 v_texcoord;\n"
"void main() {\n"
" gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"
" v_texcoord = texcoord;\n"
"}\n";
// Compiles a shader of the given |type| with GLSL source |source| and returns
// the shader handle or 0 on error.
GLuint RTCCreateShader(GLenum type, const GLchar *source) {
GLuint shader = glCreateShader(type);
if (!shader) {
return 0;
}
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint compileStatus = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == GL_FALSE) {
GLint logLength = 0;
// The null termination character is included in the returned log length.
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
std::unique_ptr<char[]> compileLog(new char[logLength]);
// The returned string is null terminated.
glGetShaderInfoLog(shader, logLength, NULL, compileLog.get());
LOG(LS_ERROR) << "Shader compile error: " << compileLog.get();
}
glDeleteShader(shader);
shader = 0;
}
return shader;
}
// Links a shader program with the given vertex and fragment shaders and
// returns the program handle or 0 on error.
GLuint RTCCreateProgram(GLuint vertexShader, GLuint fragmentShader) {
if (vertexShader == 0 || fragmentShader == 0) {
return 0;
}
GLuint program = glCreateProgram();
if (!program) {
return 0;
}
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE) {
glDeleteProgram(program);
program = 0;
}
return program;
}
// Creates and links a shader program with the given fragment shader source and
// a plain vertex shader. Returns the program handle or 0 on error.
GLuint RTCCreateProgramFromFragmentSource(const char fragmentShaderSource[]) {
GLuint vertexShader = RTCCreateShader(GL_VERTEX_SHADER, kRTCVertexShaderSource);
RTC_CHECK(vertexShader) << "failed to create vertex shader";
GLuint fragmentShader =
RTCCreateShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
RTC_CHECK(fragmentShader) << "failed to create fragment shader";
GLuint program = RTCCreateProgram(vertexShader, fragmentShader);
// Shaders are created only to generate program.
if (vertexShader) {
glDeleteShader(vertexShader);
}
if (fragmentShader) {
glDeleteShader(fragmentShader);
}
// Set vertex shader variables 'position' and 'texcoord' in program.
GLint position = glGetAttribLocation(program, "position");
GLint texcoord = glGetAttribLocation(program, "texcoord");
if (position < 0 || texcoord < 0) {
glDeleteProgram(program);
return 0;
}
// Read position attribute with size of 2 and stride of 4 beginning at the start of the array. The
// last argument indicates offset of data within the vertex buffer.
glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(position);
// Read texcoord attribute with size of 2 and stride of 4 beginning at the first texcoord in the
// array. The last argument indicates offset of data within the vertex buffer.
glVertexAttribPointer(
texcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(texcoord);
return program;
}
BOOL RTCCreateVertexBuffer(GLuint *vertexBuffer, GLuint *vertexArray) {
#if !TARGET_OS_IPHONE
glGenVertexArrays(1, vertexArray);
if (*vertexArray == 0) {
return NO;
}
glBindVertexArray(*vertexArray);
#endif
glGenBuffers(1, vertexBuffer);
if (*vertexBuffer == 0) {
glDeleteVertexArrays(1, vertexArray);
return NO;
}
glBindBuffer(GL_ARRAY_BUFFER, *vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(GLfloat), NULL, GL_DYNAMIC_DRAW);
return YES;
}
// Set vertex data to the currently bound vertex buffer.
void RTCSetVertexData(RTCVideoRotation rotation) {
// When modelview and projection matrices are identity (default) the world is
// contained in the square around origin with unit size 2. Drawing to these
// coordinates is equivalent to drawing to the entire screen. The texture is
// stretched over that square using texture coordinates (u, v) that range
// from (0, 0) to (1, 1) inclusive. Texture coordinates are flipped vertically
// here because the incoming frame has origin in upper left hand corner but
// OpenGL expects origin in bottom left corner.
std::array<std::array<GLfloat, 2>, 4> UVCoords = {{
{{0, 1}}, // Lower left.
{{1, 1}}, // Lower right.
{{1, 0}}, // Upper right.
{{0, 0}}, // Upper left.
}};
// Rotate the UV coordinates.
int rotation_offset;
switch (rotation) {
case RTCVideoRotation_0:
rotation_offset = 0;
break;
case RTCVideoRotation_90:
rotation_offset = 1;
break;
case RTCVideoRotation_180:
rotation_offset = 2;
break;
case RTCVideoRotation_270:
rotation_offset = 3;
break;
}
std::rotate(UVCoords.begin(), UVCoords.begin() + rotation_offset,
UVCoords.end());
const GLfloat gVertices[] = {
// X, Y, U, V.
-1, -1, UVCoords[0][0], UVCoords[0][1],
1, -1, UVCoords[1][0], UVCoords[1][1],
1, 1, UVCoords[2][0], UVCoords[2][1],
-1, 1, UVCoords[3][0], UVCoords[3][1],
};
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(gVertices), gVertices);
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2016 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.
*/
#include <set>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#include "webrtc/media/base/videocapturer.h"
namespace webrtc {
// Mapping from AVCaptureDeviceFormat to cricket::VideoFormat for given input
// device.
std::set<cricket::VideoFormat> GetSupportedVideoFormatsForDevice(
AVCaptureDevice* device);
// Sets device format for the provided capture device. Returns YES/NO depending
// on success.
bool SetFormatForCaptureDevice(AVCaptureDevice* device,
AVCaptureSession* session,
const cricket::VideoFormat& format);
}

View File

@ -0,0 +1,135 @@
/*
* Copyright 2016 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.
*/
#include "avfoundationformatmapper.h"
#import "WebRTC/RTCLogging.h"
// TODO(denicija): add support for higher frame rates.
// See http://crbug/webrtc/6355 for more info.
static const int kFramesPerSecond = 30;
static inline BOOL IsMediaSubTypeSupported(FourCharCode mediaSubType) {
return (mediaSubType == kCVPixelFormatType_420YpCbCr8PlanarFullRange ||
mediaSubType == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
}
static inline BOOL IsFrameRateWithinRange(int fps, AVFrameRateRange* range) {
return range.minFrameRate <= fps && range.maxFrameRate >= fps;
}
// Returns filtered array of device formats based on predefined constraints our
// stack imposes.
static NSArray<AVCaptureDeviceFormat*>* GetEligibleDeviceFormats(
const AVCaptureDevice* device,
int supportedFps) {
NSMutableArray<AVCaptureDeviceFormat*>* eligibleDeviceFormats =
[NSMutableArray array];
for (AVCaptureDeviceFormat* format in device.formats) {
// Filter out subTypes that we currently don't support in the stack
FourCharCode mediaSubType =
CMFormatDescriptionGetMediaSubType(format.formatDescription);
if (!IsMediaSubTypeSupported(mediaSubType)) {
continue;
}
// Filter out frame rate ranges that we currently don't support in the stack
for (AVFrameRateRange* frameRateRange in format.videoSupportedFrameRateRanges) {
if (IsFrameRateWithinRange(supportedFps, frameRateRange)) {
[eligibleDeviceFormats addObject:format];
break;
}
}
}
return [eligibleDeviceFormats copy];
}
// Mapping from cricket::VideoFormat to AVCaptureDeviceFormat.
static AVCaptureDeviceFormat* GetDeviceFormatForVideoFormat(
const AVCaptureDevice* device,
const cricket::VideoFormat& videoFormat) {
AVCaptureDeviceFormat* desiredDeviceFormat = nil;
NSArray<AVCaptureDeviceFormat*>* eligibleFormats =
GetEligibleDeviceFormats(device, videoFormat.framerate());
for (AVCaptureDeviceFormat* deviceFormat in eligibleFormats) {
CMVideoDimensions dimension =
CMVideoFormatDescriptionGetDimensions(deviceFormat.formatDescription);
FourCharCode mediaSubType =
CMFormatDescriptionGetMediaSubType(deviceFormat.formatDescription);
if (videoFormat.width == dimension.width &&
videoFormat.height == dimension.height) {
if (mediaSubType == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) {
// This is the preferred format so no need to wait for better option.
return deviceFormat;
} else {
// This is good candidate, but let's wait for something better.
desiredDeviceFormat = deviceFormat;
}
}
}
return desiredDeviceFormat;
}
namespace webrtc {
std::set<cricket::VideoFormat> GetSupportedVideoFormatsForDevice(
AVCaptureDevice* device) {
std::set<cricket::VideoFormat> supportedFormats;
NSArray<AVCaptureDeviceFormat*>* eligibleFormats =
GetEligibleDeviceFormats(device, kFramesPerSecond);
for (AVCaptureDeviceFormat* deviceFormat in eligibleFormats) {
CMVideoDimensions dimension =
CMVideoFormatDescriptionGetDimensions(deviceFormat.formatDescription);
cricket::VideoFormat format = cricket::VideoFormat(
dimension.width, dimension.height,
cricket::VideoFormat::FpsToInterval(kFramesPerSecond),
cricket::FOURCC_NV12);
supportedFormats.insert(format);
}
return supportedFormats;
}
bool SetFormatForCaptureDevice(AVCaptureDevice* device,
AVCaptureSession* session,
const cricket::VideoFormat& format) {
AVCaptureDeviceFormat* deviceFormat =
GetDeviceFormatForVideoFormat(device, format);
const int fps = cricket::VideoFormat::IntervalToFps(format.interval);
NSError* error = nil;
bool success = true;
[session beginConfiguration];
if ([device lockForConfiguration:&error]) {
@try {
device.activeFormat = deviceFormat;
device.activeVideoMinFrameDuration = CMTimeMake(1, fps);
} @catch (NSException* exception) {
RTCLogError(@"Failed to set active format!\n User info:%@",
exception.userInfo);
success = false;
}
[device unlockForConfiguration];
} else {
RTCLogError(@"Failed to lock device %@. Error: %@", device, error.userInfo);
success = false;
}
[session commitConfiguration];
return success;
}
} // namespace webrtc

View File

@ -0,0 +1,72 @@
/*
* 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.
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_AVFOUNDATIONVIDEOCAPTURER_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_AVFOUNDATIONVIDEOCAPTURER_H_
#import <AVFoundation/AVFoundation.h>
#include "webrtc/api/video/video_frame.h"
#include "webrtc/common_video/include/i420_buffer_pool.h"
#include "webrtc/media/base/videocapturer.h"
@class RTCAVFoundationVideoCapturerInternal;
namespace rtc {
class Thread;
} // namespace rtc
namespace webrtc {
class AVFoundationVideoCapturer : public cricket::VideoCapturer {
public:
AVFoundationVideoCapturer();
~AVFoundationVideoCapturer();
cricket::CaptureState Start(const cricket::VideoFormat& format) override;
void Stop() override;
bool IsRunning() override;
bool IsScreencast() const override {
return false;
}
bool GetPreferredFourccs(std::vector<uint32_t> *fourccs) override {
fourccs->push_back(cricket::FOURCC_NV12);
return true;
}
// Returns the active capture session. Calls to the capture session should
// occur on the RTCDispatcherTypeCaptureSession queue in RTCDispatcher.
AVCaptureSession* GetCaptureSession();
// Returns whether the rear-facing camera can be used.
// e.g. It can't be used because it doesn't exist.
bool CanUseBackCamera() const;
// Switches the camera being used (either front or back).
void SetUseBackCamera(bool useBackCamera);
bool GetUseBackCamera() const;
// Converts the sample buffer into a cricket::CapturedFrame and signals the
// frame for capture.
void CaptureSampleBuffer(CMSampleBufferRef sample_buffer,
webrtc::VideoRotation rotation);
// Called to adjust the size of output frames to supplied |width| and
// |height|. Also drops frames to make the output match |fps|.
void AdaptOutputFormat(int width, int height, int fps);
private:
RTCAVFoundationVideoCapturerInternal *_capturer;
webrtc::I420BufferPool _buffer_pool;
}; // AVFoundationVideoCapturer
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_AVFOUNDATIONVIDEOCAPTURER_H_

View File

@ -0,0 +1,179 @@
/*
* 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.
*/
#include "avfoundationvideocapturer.h"
#import <AVFoundation/AVFoundation.h>
#import "RTCAVFoundationVideoCapturerInternal.h"
#import "RTCDispatcher+Private.h"
#import "WebRTC/RTCLogging.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
#include "avfoundationformatmapper.h"
#include "webrtc/api/video/video_rotation.h"
#include "webrtc/rtc_base/bind.h"
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/logging.h"
#include "webrtc/rtc_base/thread.h"
#include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h"
namespace webrtc {
enum AVFoundationVideoCapturerMessageType : uint32_t {
kMessageTypeFrame,
};
AVFoundationVideoCapturer::AVFoundationVideoCapturer() : _capturer(nil) {
_capturer =
[[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this];
std::set<cricket::VideoFormat> front_camera_video_formats =
GetSupportedVideoFormatsForDevice([_capturer frontCaptureDevice]);
std::set<cricket::VideoFormat> back_camera_video_formats =
GetSupportedVideoFormatsForDevice([_capturer backCaptureDevice]);
std::vector<cricket::VideoFormat> intersection_video_formats;
if (back_camera_video_formats.empty()) {
intersection_video_formats.assign(front_camera_video_formats.begin(),
front_camera_video_formats.end());
} else if (front_camera_video_formats.empty()) {
intersection_video_formats.assign(back_camera_video_formats.begin(),
back_camera_video_formats.end());
} else {
std::set_intersection(
front_camera_video_formats.begin(), front_camera_video_formats.end(),
back_camera_video_formats.begin(), back_camera_video_formats.end(),
std::back_inserter(intersection_video_formats));
}
SetSupportedFormats(intersection_video_formats);
}
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;
}
AVCaptureDevice* device = [_capturer getActiveCaptureDevice];
AVCaptureSession* session = _capturer.captureSession;
if (!SetFormatForCaptureDevice(device, session, format)) {
return cricket::CaptureState::CS_FAILED;
}
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.
[_capturer start];
SetCaptureState(cricket::CaptureState::CS_RUNNING);
return cricket::CaptureState::CS_STARTING;
}
void AVFoundationVideoCapturer::Stop() {
[_capturer stop];
SetCaptureFormat(NULL);
}
bool AVFoundationVideoCapturer::IsRunning() {
return _capturer.isRunning;
}
AVCaptureSession* AVFoundationVideoCapturer::GetCaptureSession() {
return _capturer.captureSession;
}
bool AVFoundationVideoCapturer::CanUseBackCamera() const {
return _capturer.canUseBackCamera;
}
void AVFoundationVideoCapturer::SetUseBackCamera(bool useBackCamera) {
_capturer.useBackCamera = useBackCamera;
}
bool AVFoundationVideoCapturer::GetUseBackCamera() const {
return _capturer.useBackCamera;
}
void AVFoundationVideoCapturer::AdaptOutputFormat(int width, int height, int fps) {
cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0);
video_adapter()->OnOutputFormatRequest(format);
}
void AVFoundationVideoCapturer::CaptureSampleBuffer(
CMSampleBufferRef sample_buffer, VideoRotation rotation) {
if (CMSampleBufferGetNumSamples(sample_buffer) != 1 ||
!CMSampleBufferIsValid(sample_buffer) ||
!CMSampleBufferDataIsReady(sample_buffer)) {
return;
}
CVImageBufferRef image_buffer = CMSampleBufferGetImageBuffer(sample_buffer);
if (image_buffer == NULL) {
return;
}
int captured_width = CVPixelBufferGetWidth(image_buffer);
int captured_height = CVPixelBufferGetHeight(image_buffer);
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,
rtc::TimeNanos() / rtc::kNumNanosecsPerMicrosec,
rtc::TimeMicros(), &adapted_width, &adapted_height,
&crop_width, &crop_height, &crop_x, &crop_y,
&translated_camera_time_us)) {
return;
}
RTCCVPixelBuffer* rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:image_buffer
adaptedWidth:adapted_width
adaptedHeight:adapted_height
cropWidth:crop_width
cropHeight:crop_height
cropX:crop_x
cropY:crop_y];
rtc::scoped_refptr<VideoFrameBuffer> buffer =
new rtc::RefCountedObject<ObjCFrameBuffer>(rtcPixelBuffer);
// Applying rotation is only supported for legacy reasons and performance is
// not critical here.
if (apply_rotation() && rotation != kVideoRotation_0) {
buffer = I420Buffer::Rotate(*buffer->ToI420(), rotation);
if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) {
std::swap(captured_width, captured_height);
}
rotation = kVideoRotation_0;
}
OnFrame(webrtc::VideoFrame(buffer, rotation, translated_camera_time_us),
captured_width, captured_height);
}
} // namespace webrtc

View File

@ -0,0 +1,178 @@
/*
* Copyright 2016 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.
*/
#include "webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.h"
#include "libyuv/convert.h"
#include "webrtc/api/video/i420_buffer.h"
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
#include "webrtc/rtc_base/checks.h"
#include "webrtc/rtc_base/logging.h"
namespace webrtc {
CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer,
int adapted_width,
int adapted_height,
int crop_width,
int crop_height,
int crop_x,
int crop_y)
: pixel_buffer_(pixel_buffer),
width_(adapted_width),
height_(adapted_height),
buffer_width_(CVPixelBufferGetWidth(pixel_buffer)),
buffer_height_(CVPixelBufferGetHeight(pixel_buffer)),
crop_width_(crop_width),
crop_height_(crop_height),
// Can only crop at even pixels.
crop_x_(crop_x & ~1),
crop_y_(crop_y & ~1) {
CVBufferRetain(pixel_buffer_);
}
CoreVideoFrameBuffer::CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer)
: pixel_buffer_(pixel_buffer),
width_(CVPixelBufferGetWidth(pixel_buffer)),
height_(CVPixelBufferGetHeight(pixel_buffer)),
buffer_width_(width_),
buffer_height_(height_),
crop_width_(width_),
crop_height_(height_),
crop_x_(0),
crop_y_(0) {
CVBufferRetain(pixel_buffer_);
}
CoreVideoFrameBuffer::~CoreVideoFrameBuffer() {
CVBufferRelease(pixel_buffer_);
}
VideoFrameBuffer::Type CoreVideoFrameBuffer::type() const {
return Type::kNative;
}
int CoreVideoFrameBuffer::width() const {
return width_;
}
int CoreVideoFrameBuffer::height() const {
return height_;
}
rtc::scoped_refptr<I420BufferInterface> CoreVideoFrameBuffer::ToI420() {
const OSType pixel_format = CVPixelBufferGetPixelFormatType(pixel_buffer_);
RTC_DCHECK(pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ||
pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly);
const uint8_t* src_y = static_cast<const uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0));
const int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0);
const uint8_t* src_uv = static_cast<const uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1));
const int src_uv_stride =
CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1);
// Crop just by modifying pointers.
src_y += src_y_stride * crop_y_ + crop_x_;
src_uv += src_uv_stride * (crop_y_ / 2) + crop_x_;
// TODO(magjed): Use a frame buffer pool.
NV12ToI420Scaler nv12_to_i420_scaler;
rtc::scoped_refptr<I420Buffer> buffer =
new rtc::RefCountedObject<I420Buffer>(width_, height_);
nv12_to_i420_scaler.NV12ToI420Scale(
src_y, src_y_stride,
src_uv, src_uv_stride,
crop_width_, crop_height_,
buffer->MutableDataY(), buffer->StrideY(),
buffer->MutableDataU(), buffer->StrideU(),
buffer->MutableDataV(), buffer->StrideV(),
buffer->width(), buffer->height());
CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly);
return buffer;
}
bool CoreVideoFrameBuffer::RequiresCropping() const {
return crop_width_ != buffer_width_ || crop_height_ != buffer_height_;
}
bool CoreVideoFrameBuffer::CropAndScaleTo(
std::vector<uint8_t>* tmp_buffer,
CVPixelBufferRef output_pixel_buffer) const {
// Prepare output pointers.
RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(output_pixel_buffer),
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
CVReturn cv_ret = CVPixelBufferLockBaseAddress(output_pixel_buffer, 0);
if (cv_ret != kCVReturnSuccess) {
LOG(LS_ERROR) << "Failed to lock base address: " << cv_ret;
return false;
}
const int dst_width = CVPixelBufferGetWidth(output_pixel_buffer);
const int dst_height = CVPixelBufferGetHeight(output_pixel_buffer);
uint8_t* dst_y = reinterpret_cast<uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(output_pixel_buffer, 0));
const int dst_y_stride =
CVPixelBufferGetBytesPerRowOfPlane(output_pixel_buffer, 0);
uint8_t* dst_uv = reinterpret_cast<uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(output_pixel_buffer, 1));
const int dst_uv_stride =
CVPixelBufferGetBytesPerRowOfPlane(output_pixel_buffer, 1);
// Prepare source pointers.
const OSType src_pixel_format =
CVPixelBufferGetPixelFormatType(pixel_buffer_);
RTC_DCHECK(
src_pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange ||
src_pixel_format == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange);
CVPixelBufferLockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly);
const uint8_t* src_y = static_cast<const uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 0));
const int src_y_stride = CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 0);
const uint8_t* src_uv = static_cast<const uint8_t*>(
CVPixelBufferGetBaseAddressOfPlane(pixel_buffer_, 1));
const int src_uv_stride =
CVPixelBufferGetBytesPerRowOfPlane(pixel_buffer_, 1);
// Crop just by modifying pointers.
src_y += src_y_stride * crop_y_ + crop_x_;
src_uv += src_uv_stride * (crop_y_ / 2) + crop_x_;
if (crop_width_ == dst_width && crop_height_ == dst_height) {
tmp_buffer->clear();
tmp_buffer->shrink_to_fit();
} else {
const int src_chroma_width = (crop_width_ + 1) / 2;
const int src_chroma_height = (crop_height_ + 1) / 2;
const int dst_chroma_width = (dst_width + 1) / 2;
const int dst_chroma_height = (dst_height + 1) / 2;
tmp_buffer->resize(src_chroma_width * src_chroma_height * 2 +
dst_chroma_width * dst_chroma_height * 2);
tmp_buffer->shrink_to_fit();
}
NV12Scale(tmp_buffer->data(),
src_y, src_y_stride,
src_uv, src_uv_stride,
crop_width_, crop_height_,
dst_y, dst_y_stride,
dst_uv, dst_uv_stride,
dst_width, dst_height);
CVPixelBufferUnlockBaseAddress(pixel_buffer_, kCVPixelBufferLock_ReadOnly);
CVPixelBufferUnlockBaseAddress(output_pixel_buffer, 0);
return true;
}
} // namespace webrtc

View File

@ -0,0 +1,66 @@
/*
* Copyright 2016 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.
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_COREVIDEO_FRAME_BUFFER_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_COREVIDEO_FRAME_BUFFER_H_
#include <CoreVideo/CoreVideo.h>
#include <vector>
#include "webrtc/common_video/include/video_frame_buffer.h"
namespace webrtc {
class CoreVideoFrameBuffer : public VideoFrameBuffer {
public:
explicit CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer);
CoreVideoFrameBuffer(CVPixelBufferRef pixel_buffer,
int adapted_width,
int adapted_height,
int crop_width,
int crop_height,
int crop_x,
int crop_y);
~CoreVideoFrameBuffer() override;
CVPixelBufferRef pixel_buffer() { return pixel_buffer_; }
// Returns true if the internal pixel buffer needs to be cropped.
bool RequiresCropping() const;
// Crop and scales the internal pixel buffer to the output pixel buffer. The
// tmp buffer is used for intermediary splitting the UV channels. This
// function returns true if successful.
bool CropAndScaleTo(std::vector<uint8_t>* tmp_buffer,
CVPixelBufferRef output_pixel_buffer) const;
private:
Type type() const override;
int width() const override;
int height() const override;
rtc::scoped_refptr<I420BufferInterface> ToI420() override;
CVPixelBufferRef pixel_buffer_;
// buffer_width/height is the actual pixel buffer resolution. The
// width_/height_ is the resolution we will scale to in ToI420(). Cropping
// happens before scaling, so: buffer_width >= crop_width >= width().
const int width_;
const int height_;
const int buffer_width_;
const int buffer_height_;
const int crop_width_;
const int crop_height_;
const int crop_x_;
const int crop_y_;
};
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_COREVIDEO_FRAME_BUFFER_H_

View File

@ -0,0 +1,44 @@
/*
* Copyright 2017 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.
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJC_FRAME_BUFFER_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJC_FRAME_BUFFER_H_
#import <CoreVideo/CoreVideo.h>
#include "webrtc/common_video/include/video_frame_buffer.h"
@protocol RTCVideoFrameBuffer;
namespace webrtc {
class ObjCFrameBuffer : public VideoFrameBuffer {
public:
explicit ObjCFrameBuffer(id<RTCVideoFrameBuffer>);
~ObjCFrameBuffer() override;
Type type() const override;
int width() const override;
int height() const override;
rtc::scoped_refptr<I420BufferInterface> ToI420() override;
id<RTCVideoFrameBuffer> wrapped_frame_buffer() const;
private:
id<RTCVideoFrameBuffer> frame_buffer_;
int width_;
int height_;
};
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJC_FRAME_BUFFER_H_

View File

@ -0,0 +1,78 @@
/*
* Copyright 2017 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.
*/
#include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
namespace webrtc {
namespace {
/** ObjCFrameBuffer that conforms to I420BufferInterface by wrapping RTCI420Buffer */
class ObjCI420FrameBuffer : public I420BufferInterface {
public:
explicit ObjCI420FrameBuffer(id<RTCI420Buffer> frame_buffer)
: frame_buffer_(frame_buffer), width_(frame_buffer.width), height_(frame_buffer.height) {}
~ObjCI420FrameBuffer() override{};
int width() const override { return width_; }
int height() const override { return height_; }
const uint8_t* DataY() const override { return frame_buffer_.dataY; }
const uint8_t* DataU() const override { return frame_buffer_.dataU; }
const uint8_t* DataV() const override { return frame_buffer_.dataV; }
int StrideY() const override { return frame_buffer_.strideY; }
int StrideU() const override { return frame_buffer_.strideU; }
int StrideV() const override { return frame_buffer_.strideV; }
private:
id<RTCI420Buffer> frame_buffer_;
int width_;
int height_;
};
} // namespace
ObjCFrameBuffer::ObjCFrameBuffer(id<RTCVideoFrameBuffer> frame_buffer)
: frame_buffer_(frame_buffer), width_(frame_buffer.width), height_(frame_buffer.height) {}
ObjCFrameBuffer::~ObjCFrameBuffer() {}
VideoFrameBuffer::Type ObjCFrameBuffer::type() const {
return Type::kNative;
}
int ObjCFrameBuffer::width() const {
return width_;
}
int ObjCFrameBuffer::height() const {
return height_;
}
rtc::scoped_refptr<I420BufferInterface> ObjCFrameBuffer::ToI420() {
rtc::scoped_refptr<I420BufferInterface> buffer =
new rtc::RefCountedObject<ObjCI420FrameBuffer>([frame_buffer_ toI420]);
return buffer;
}
id<RTCVideoFrameBuffer> ObjCFrameBuffer::wrapped_frame_buffer() const {
return frame_buffer_;
}
} // namespace webrtc

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2017 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.
*/
#ifndef WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJCVIDEOTRACKSOURCE_H_
#define WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJCVIDEOTRACKSOURCE_H_
#include "WebRTC/RTCMacros.h"
#include "webrtc/media/base/adaptedvideotracksource.h"
#include "webrtc/rtc_base/timestampaligner.h"
RTC_FWD_DECL_OBJC_CLASS(RTCVideoFrame);
namespace webrtc {
class ObjcVideoTrackSource : public rtc::AdaptedVideoTrackSource {
public:
ObjcVideoTrackSource();
// This class can not be used for implementing screen casting. Hopefully, this
// function will be removed before we add that to iOS/Mac.
bool is_screencast() const override { return false; }
// Indicates that the encoder should denoise video before encoding it.
// If it is not set, the default configuration is used which is different
// depending on video codec.
rtc::Optional<bool> needs_denoising() const override {
return rtc::Optional<bool>(false);
}
SourceState state() const override { return SourceState::kLive; }
bool remote() const override { return false; }
// Called by RTCVideoSource.
void OnCapturedFrame(RTCVideoFrame* frame);
void OnOutputFormatRequest(int width, int height, int fps);
private:
rtc::VideoBroadcaster broadcaster_;
rtc::TimestampAligner timestamp_aligner_;
};
} // namespace webrtc
#endif // WEBRTC_SDK_OBJC_FRAMEWORK_CLASSES_VIDEO_OBJCVIDEOTRACKSOURCE_H_

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2017 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.
*/
#include "webrtc/sdk/objc/Framework/Classes/Video/objcvideotracksource.h"
#import "WebRTC/RTCVideoFrame.h"
#import "WebRTC/RTCVideoFrameBuffer.h"
#include "webrtc/api/video/i420_buffer.h"
#include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h"
namespace webrtc {
ObjcVideoTrackSource::ObjcVideoTrackSource() {}
void ObjcVideoTrackSource::OnOutputFormatRequest(int width, int height, int fps) {
cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0);
video_adapter()->OnOutputFormatRequest(format);
}
void ObjcVideoTrackSource::OnCapturedFrame(RTCVideoFrame* frame) {
const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec;
const int64_t translated_timestamp_us =
timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros());
int adapted_width;
int adapted_height;
int crop_width;
int crop_height;
int crop_x;
int crop_y;
if (!AdaptFrame(frame.width, frame.height, timestamp_us, &adapted_width, &adapted_height,
&crop_width, &crop_height, &crop_x, &crop_y)) {
return;
}
rtc::scoped_refptr<VideoFrameBuffer> buffer;
if (adapted_width == frame.width && adapted_height == frame.height) {
// No adaption - optimized path.
buffer = new rtc::RefCountedObject<ObjCFrameBuffer>(frame.buffer);
} else if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
// Adapted CVPixelBuffer frame.
RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
buffer = new rtc::RefCountedObject<ObjCFrameBuffer>([[RTCCVPixelBuffer alloc]
initWithPixelBuffer:rtcPixelBuffer.pixelBuffer
adaptedWidth:adapted_width
adaptedHeight:adapted_height
cropWidth:crop_width
cropHeight:crop_height
cropX:crop_x + rtcPixelBuffer.cropX
cropY:crop_y + rtcPixelBuffer.cropY]);
} else {
// Adapted I420 frame.
// TODO(magjed): Optimize this I420 path.
rtc::scoped_refptr<I420Buffer> i420_buffer = I420Buffer::Create(adapted_width, adapted_height);
buffer = new rtc::RefCountedObject<ObjCFrameBuffer>(frame.buffer);
i420_buffer->CropAndScaleFrom(*buffer->ToI420(), crop_x, crop_y, crop_width, crop_height);
buffer = i420_buffer;
}
// Applying rotation is only supported for legacy reasons and performance is
// not critical here.
webrtc::VideoRotation rotation = static_cast<webrtc::VideoRotation>(frame.rotation);
if (apply_rotation() && rotation != kVideoRotation_0) {
buffer = I420Buffer::Rotate(*buffer->ToI420(), rotation);
rotation = kVideoRotation_0;
}
OnFrame(webrtc::VideoFrame(buffer, rotation, translated_timestamp_us));
}
} // namespace webrtc