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:
committed by
Commit Bot
parent
6674846b4a
commit
bb547203bf
17
sdk/objc/Framework/Classes/Metal/RTCMTLI420Renderer.h
Normal file
17
sdk/objc/Framework/Classes/Metal/RTCMTLI420Renderer.h
Normal file
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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 <Foundation/Foundation.h>
|
||||
|
||||
#import "RTCMTLRenderer.h"
|
||||
|
||||
NS_AVAILABLE(10_11, 9_0)
|
||||
@interface RTCMTLI420Renderer : RTCMTLRenderer
|
||||
@end
|
||||
152
sdk/objc/Framework/Classes/Metal/RTCMTLI420Renderer.mm
Normal file
152
sdk/objc/Framework/Classes/Metal/RTCMTLI420Renderer.mm
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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 "RTCMTLI420Renderer.h"
|
||||
#import "WebRTC/RTCVideoFrameBuffer.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#import "WebRTC/RTCLogging.h"
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
|
||||
#import "RTCMTLRenderer+Private.h"
|
||||
|
||||
#define MTL_STRINGIFY(s) @ #s
|
||||
|
||||
static NSString *const shaderSource = MTL_STRINGIFY(
|
||||
using namespace metal; typedef struct {
|
||||
packed_float2 position;
|
||||
packed_float2 texcoord;
|
||||
} Vertex;
|
||||
|
||||
typedef struct {
|
||||
float4 position[[position]];
|
||||
float2 texcoord;
|
||||
} Varyings;
|
||||
|
||||
vertex Varyings vertexPassthrough(device Vertex * verticies[[buffer(0)]],
|
||||
unsigned int vid[[vertex_id]]) {
|
||||
Varyings out;
|
||||
device Vertex &v = verticies[vid];
|
||||
out.position = float4(float2(v.position), 0.0, 1.0);
|
||||
out.texcoord = v.texcoord;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
fragment half4 fragmentColorConversion(
|
||||
Varyings in[[stage_in]], texture2d<float, access::sample> textureY[[texture(0)]],
|
||||
texture2d<float, access::sample> textureU[[texture(1)]],
|
||||
texture2d<float, access::sample> textureV[[texture(2)]]) {
|
||||
constexpr sampler s(address::clamp_to_edge, filter::linear);
|
||||
float y;
|
||||
float u;
|
||||
float v;
|
||||
float r;
|
||||
float g;
|
||||
float b;
|
||||
// Conversion for YUV to rgb from http://www.fourcc.org/fccyvrgb.php
|
||||
y = textureY.sample(s, in.texcoord).r;
|
||||
u = textureU.sample(s, in.texcoord).r;
|
||||
v = textureV.sample(s, in.texcoord).r;
|
||||
u = u - 0.5;
|
||||
v = v - 0.5;
|
||||
r = y + 1.403 * v;
|
||||
g = y - 0.344 * u - 0.714 * v;
|
||||
b = y + 1.770 * u;
|
||||
|
||||
float4 out = float4(r, g, b, 1.0);
|
||||
|
||||
return half4(out);
|
||||
});
|
||||
|
||||
@implementation RTCMTLI420Renderer {
|
||||
// Textures.
|
||||
id<MTLTexture> _yTexture;
|
||||
id<MTLTexture> _uTexture;
|
||||
id<MTLTexture> _vTexture;
|
||||
|
||||
MTLTextureDescriptor *_descriptor;
|
||||
MTLTextureDescriptor *_chromaDescriptor;
|
||||
|
||||
int _width;
|
||||
int _height;
|
||||
int _chromaWidth;
|
||||
int _chromaHeight;
|
||||
}
|
||||
|
||||
#pragma mark - Virtual
|
||||
|
||||
- (NSString *)shaderSource {
|
||||
return shaderSource;
|
||||
}
|
||||
|
||||
- (BOOL)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame {
|
||||
[super setupTexturesForFrame:frame];
|
||||
|
||||
id<MTLDevice> device = [self currentMetalDevice];
|
||||
if (!device) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
id<RTCI420Buffer> buffer = [frame.buffer toI420];
|
||||
|
||||
// Luma (y) texture.
|
||||
if (!_descriptor || (_width != frame.width && _height != frame.height)) {
|
||||
_width = frame.width;
|
||||
_height = frame.height;
|
||||
_descriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR8Unorm
|
||||
width:_width
|
||||
height:_height
|
||||
mipmapped:NO];
|
||||
_descriptor.usage = MTLTextureUsageShaderRead;
|
||||
_yTexture = [device newTextureWithDescriptor:_descriptor];
|
||||
}
|
||||
|
||||
// Chroma (u,v) textures
|
||||
[_yTexture replaceRegion:MTLRegionMake2D(0, 0, _width, _height)
|
||||
mipmapLevel:0
|
||||
withBytes:buffer.dataY
|
||||
bytesPerRow:buffer.strideY];
|
||||
|
||||
if (!_chromaDescriptor ||
|
||||
(_chromaWidth != frame.width / 2 && _chromaHeight != frame.height / 2)) {
|
||||
_chromaWidth = frame.width / 2;
|
||||
_chromaHeight = frame.height / 2;
|
||||
_chromaDescriptor =
|
||||
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR8Unorm
|
||||
width:_chromaWidth
|
||||
height:_chromaHeight
|
||||
mipmapped:NO];
|
||||
_chromaDescriptor.usage = MTLTextureUsageShaderRead;
|
||||
_uTexture = [device newTextureWithDescriptor:_chromaDescriptor];
|
||||
_vTexture = [device newTextureWithDescriptor:_chromaDescriptor];
|
||||
}
|
||||
|
||||
[_uTexture replaceRegion:MTLRegionMake2D(0, 0, _chromaWidth, _chromaHeight)
|
||||
mipmapLevel:0
|
||||
withBytes:buffer.dataU
|
||||
bytesPerRow:buffer.strideU];
|
||||
[_vTexture replaceRegion:MTLRegionMake2D(0, 0, _chromaWidth, _chromaHeight)
|
||||
mipmapLevel:0
|
||||
withBytes:buffer.dataV
|
||||
bytesPerRow:buffer.strideV];
|
||||
|
||||
return (_uTexture != nil) && (_yTexture != nil) && (_vTexture != nil);
|
||||
}
|
||||
|
||||
- (void)uploadTexturesToRenderEncoder:(id<MTLRenderCommandEncoder>)renderEncoder {
|
||||
[renderEncoder setFragmentTexture:_yTexture atIndex:0];
|
||||
[renderEncoder setFragmentTexture:_uTexture atIndex:1];
|
||||
[renderEncoder setFragmentTexture:_vTexture atIndex:2];
|
||||
}
|
||||
|
||||
@end
|
||||
118
sdk/objc/Framework/Classes/Metal/RTCMTLNSVideoView.m
Normal file
118
sdk/objc/Framework/Classes/Metal/RTCMTLNSVideoView.m
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* 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/RTCMTLNSVideoView.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
|
||||
#import "RTCMTLI420Renderer.h"
|
||||
|
||||
@interface RTCMTLNSVideoView ()<MTKViewDelegate>
|
||||
@property(nonatomic) id<RTCMTLRenderer> renderer;
|
||||
@property(nonatomic, strong) MTKView *metalView;
|
||||
@property(atomic, strong) RTCVideoFrame *videoFrame;
|
||||
@end
|
||||
|
||||
@implementation RTCMTLNSVideoView {
|
||||
id<RTCMTLRenderer> _renderer;
|
||||
}
|
||||
|
||||
@synthesize renderer = _renderer;
|
||||
@synthesize metalView = _metalView;
|
||||
@synthesize videoFrame = _videoFrame;
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frameRect {
|
||||
self = [super initWithFrame:frameRect];
|
||||
if (self) {
|
||||
[self configure];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)aCoder {
|
||||
self = [super initWithCoder:aCoder];
|
||||
if (self) {
|
||||
[self configure];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
+ (BOOL)isMetalAvailable {
|
||||
return [MTLCopyAllDevices() count] > 0;
|
||||
}
|
||||
|
||||
- (void)configure {
|
||||
if ([[self class] isMetalAvailable]) {
|
||||
_metalView = [[MTKView alloc] initWithFrame:self.bounds];
|
||||
[self addSubview:_metalView];
|
||||
_metalView.layerContentsPlacement = NSViewLayerContentsPlacementScaleProportionallyToFit;
|
||||
_metalView.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
_metalView.framebufferOnly = YES;
|
||||
_metalView.delegate = self;
|
||||
|
||||
_renderer = [[RTCMTLI420Renderer alloc] init];
|
||||
if (![(RTCMTLI420Renderer *)_renderer addRenderingDestination:_metalView]) {
|
||||
_renderer = nil;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateConstraints {
|
||||
NSDictionary *views = NSDictionaryOfVariableBindings(_metalView);
|
||||
|
||||
NSArray *constraintsHorizontal =
|
||||
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_metalView]-0-|"
|
||||
options:0
|
||||
metrics:nil
|
||||
views:views];
|
||||
[self addConstraints:constraintsHorizontal];
|
||||
|
||||
NSArray *constraintsVertical =
|
||||
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_metalView]-0-|"
|
||||
options:0
|
||||
metrics:nil
|
||||
views:views];
|
||||
[self addConstraints:constraintsVertical];
|
||||
[super updateConstraints];
|
||||
}
|
||||
|
||||
#pragma mark - MTKViewDelegate methods
|
||||
- (void)drawInMTKView:(nonnull MTKView *)view {
|
||||
if (self.videoFrame == nil) {
|
||||
return;
|
||||
}
|
||||
if (view == self.metalView) {
|
||||
[_renderer drawFrame:self.videoFrame];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
|
||||
}
|
||||
|
||||
#pragma mark - RTCVideoRenderer
|
||||
|
||||
- (void)setSize:(CGSize)size {
|
||||
_metalView.drawableSize = size;
|
||||
[_metalView draw];
|
||||
}
|
||||
|
||||
- (void)renderFrame:(nullable RTCVideoFrame *)frame {
|
||||
if (frame == nil) {
|
||||
return;
|
||||
}
|
||||
self.videoFrame = [frame newI420VideoFrame];
|
||||
}
|
||||
|
||||
@end
|
||||
18
sdk/objc/Framework/Classes/Metal/RTCMTLNV12Renderer.h
Normal file
18
sdk/objc/Framework/Classes/Metal/RTCMTLNV12Renderer.h
Normal file
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* 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 <Foundation/Foundation.h>
|
||||
|
||||
#import "RTCMTLRenderer.h"
|
||||
|
||||
NS_AVAILABLE(10_11, 9_0)
|
||||
@interface RTCMTLNV12Renderer : RTCMTLRenderer
|
||||
|
||||
@end
|
||||
135
sdk/objc/Framework/Classes/Metal/RTCMTLNV12Renderer.mm
Normal file
135
sdk/objc/Framework/Classes/Metal/RTCMTLNV12Renderer.mm
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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 "RTCMTLNV12Renderer.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#import "WebRTC/RTCLogging.h"
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
#import "WebRTC/RTCVideoFrameBuffer.h"
|
||||
|
||||
#import "RTCMTLRenderer+Private.h"
|
||||
|
||||
#define MTL_STRINGIFY(s) @ #s
|
||||
|
||||
static NSString *const shaderSource = MTL_STRINGIFY(
|
||||
using namespace metal; typedef struct {
|
||||
packed_float2 position;
|
||||
packed_float2 texcoord;
|
||||
} Vertex;
|
||||
|
||||
typedef struct {
|
||||
float4 position[[position]];
|
||||
float2 texcoord;
|
||||
} Varyings;
|
||||
|
||||
vertex Varyings vertexPassthrough(device Vertex * verticies[[buffer(0)]],
|
||||
unsigned int vid[[vertex_id]]) {
|
||||
Varyings out;
|
||||
device Vertex &v = verticies[vid];
|
||||
out.position = float4(float2(v.position), 0.0, 1.0);
|
||||
out.texcoord = v.texcoord;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Receiving YCrCb textures.
|
||||
fragment half4 fragmentColorConversion(
|
||||
Varyings in[[stage_in]], texture2d<float, access::sample> textureY[[texture(0)]],
|
||||
texture2d<float, access::sample> textureCbCr[[texture(1)]]) {
|
||||
constexpr sampler s(address::clamp_to_edge, filter::linear);
|
||||
float y;
|
||||
float2 uv;
|
||||
y = textureY.sample(s, in.texcoord).r;
|
||||
uv = textureCbCr.sample(s, in.texcoord).rg - float2(0.5, 0.5);
|
||||
|
||||
// Conversion for YUV to rgb from http://www.fourcc.org/fccyvrgb.php
|
||||
float4 out = float4(y + 1.403 * uv.y, y - 0.344 * uv.x - 0.714 * uv.y, y + 1.770 * uv.x, 1.0);
|
||||
|
||||
return half4(out);
|
||||
});
|
||||
|
||||
@implementation RTCMTLNV12Renderer {
|
||||
// Textures.
|
||||
CVMetalTextureCacheRef _textureCache;
|
||||
id<MTLTexture> _yTexture;
|
||||
id<MTLTexture> _CrCbTexture;
|
||||
}
|
||||
|
||||
- (BOOL)addRenderingDestination:(__kindof MTKView *)view {
|
||||
if ([super addRenderingDestination:view]) {
|
||||
[self initializeTextureCache];
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)initializeTextureCache {
|
||||
CVReturn status = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, [self currentMetalDevice],
|
||||
nil, &_textureCache);
|
||||
if (status != kCVReturnSuccess) {
|
||||
RTCLogError(@"Metal: Failed to initialize metal texture cache. Return status is %d", status);
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)shaderSource {
|
||||
return shaderSource;
|
||||
}
|
||||
|
||||
- (BOOL)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame {
|
||||
[super setupTexturesForFrame:frame];
|
||||
CVPixelBufferRef pixelBuffer = ((RTCCVPixelBuffer *)frame.buffer).pixelBuffer;
|
||||
|
||||
id<MTLTexture> lumaTexture = nil;
|
||||
id<MTLTexture> chromaTexture = nil;
|
||||
CVMetalTextureRef outTexture = nullptr;
|
||||
|
||||
// Luma (y) texture.
|
||||
int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
|
||||
int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
|
||||
|
||||
int indexPlane = 0;
|
||||
CVReturn result = CVMetalTextureCacheCreateTextureFromImage(
|
||||
kCFAllocatorDefault, _textureCache, pixelBuffer, nil, MTLPixelFormatR8Unorm, lumaWidth,
|
||||
lumaHeight, indexPlane, &outTexture);
|
||||
|
||||
if (result == kCVReturnSuccess) {
|
||||
lumaTexture = CVMetalTextureGetTexture(outTexture);
|
||||
}
|
||||
|
||||
// Same as CFRelease except it can be passed NULL without crashing.
|
||||
CVBufferRelease(outTexture);
|
||||
outTexture = nullptr;
|
||||
|
||||
// Chroma (CrCb) texture.
|
||||
indexPlane = 1;
|
||||
result = CVMetalTextureCacheCreateTextureFromImage(
|
||||
kCFAllocatorDefault, _textureCache, pixelBuffer, nil, MTLPixelFormatRG8Unorm, lumaWidth / 2,
|
||||
lumaHeight / 2, indexPlane, &outTexture);
|
||||
if (result == kCVReturnSuccess) {
|
||||
chromaTexture = CVMetalTextureGetTexture(outTexture);
|
||||
}
|
||||
CVBufferRelease(outTexture);
|
||||
|
||||
if (lumaTexture != nil && chromaTexture != nil) {
|
||||
_yTexture = lumaTexture;
|
||||
_CrCbTexture = chromaTexture;
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)uploadTexturesToRenderEncoder:(id<MTLRenderCommandEncoder>)renderEncoder {
|
||||
[renderEncoder setFragmentTexture:_yTexture atIndex:0];
|
||||
[renderEncoder setFragmentTexture:_CrCbTexture atIndex:1];
|
||||
}
|
||||
|
||||
@end
|
||||
21
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer+Private.h
Normal file
21
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer+Private.h
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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 <Metal/Metal.h>
|
||||
#import "RTCMTLRenderer.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
@interface RTCMTLRenderer (Private)
|
||||
- (nullable id<MTLDevice>)currentMetalDevice;
|
||||
- (NSString *)shaderSource;
|
||||
- (BOOL)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame;
|
||||
- (void)uploadTexturesToRenderEncoder:(id<MTLRenderCommandEncoder>)renderEncoder;
|
||||
@end
|
||||
NS_ASSUME_NONNULL_END
|
||||
55
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer.h
Normal file
55
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 <Foundation/Foundation.h>
|
||||
#if TARGET_OS_IPHONE
|
||||
#import <UIKit/UIKit.h>
|
||||
#else
|
||||
#import <AppKit/AppKit.h>
|
||||
#endif
|
||||
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
/**
|
||||
* Protocol defining ability to render RTCVideoFrame in Metal enabled views.
|
||||
*/
|
||||
@protocol RTCMTLRenderer<NSObject>
|
||||
|
||||
/**
|
||||
* Method to be implemented to perform actual rendering of the provided frame.
|
||||
*
|
||||
* @param frame The frame to be rendered.
|
||||
*/
|
||||
- (void)drawFrame:(RTCVideoFrame *)frame;
|
||||
|
||||
/**
|
||||
* Sets the provided view as rendering destination if possible.
|
||||
*
|
||||
* If not possible method returns NO and callers of the method are responisble for performing
|
||||
* cleanups.
|
||||
*/
|
||||
|
||||
#if TARGET_OS_IOS
|
||||
- (BOOL)addRenderingDestination:(__kindof UIView *)view;
|
||||
#else
|
||||
- (BOOL)addRenderingDestination:(__kindof NSView *)view;
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* Implementation of RTCMTLRenderer protocol for rendering native nv12 video frames.
|
||||
*/
|
||||
NS_AVAILABLE(10_11, 9_0)
|
||||
@interface RTCMTLRenderer : NSObject<RTCMTLRenderer>
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
244
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer.mm
Normal file
244
sdk/objc/Framework/Classes/Metal/RTCMTLRenderer.mm
Normal file
@ -0,0 +1,244 @@
|
||||
/*
|
||||
* 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 "RTCMTLRenderer+Private.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#import "WebRTC/RTCLogging.h"
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
|
||||
#include "webrtc/api/video/video_rotation.h"
|
||||
#include "webrtc/rtc_base/checks.h"
|
||||
|
||||
// As defined in shaderSource.
|
||||
static NSString *const vertexFunctionName = @"vertexPassthrough";
|
||||
static NSString *const fragmentFunctionName = @"fragmentColorConversion";
|
||||
|
||||
static NSString *const pipelineDescriptorLabel = @"RTCPipeline";
|
||||
static NSString *const commandBufferLabel = @"RTCCommandBuffer";
|
||||
static NSString *const renderEncoderLabel = @"RTCEncoder";
|
||||
static NSString *const renderEncoderDebugGroup = @"RTCDrawFrame";
|
||||
|
||||
static const float cubeVertexData[64] = {
|
||||
-1.0, -1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0,
|
||||
|
||||
// rotation = 90, offset = 16.
|
||||
-1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0,
|
||||
|
||||
// rotation = 180, offset = 32.
|
||||
-1.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0,
|
||||
|
||||
// rotation = 270, offset = 48.
|
||||
-1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0,
|
||||
};
|
||||
|
||||
static inline int offsetForRotation(RTCVideoRotation rotation) {
|
||||
switch (rotation) {
|
||||
case RTCVideoRotation_0:
|
||||
return 0;
|
||||
case RTCVideoRotation_90:
|
||||
return 16;
|
||||
case RTCVideoRotation_180:
|
||||
return 32;
|
||||
case RTCVideoRotation_270:
|
||||
return 48;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The max number of command buffers in flight (submitted to GPU).
|
||||
// For now setting it up to 1.
|
||||
// In future we might use triple buffering method if it improves performance.
|
||||
static const NSInteger kMaxInflightBuffers = 1;
|
||||
|
||||
@implementation RTCMTLRenderer {
|
||||
__kindof MTKView *_view;
|
||||
|
||||
// Controller.
|
||||
dispatch_semaphore_t _inflight_semaphore;
|
||||
|
||||
// Renderer.
|
||||
id<MTLDevice> _device;
|
||||
id<MTLCommandQueue> _commandQueue;
|
||||
id<MTLLibrary> _defaultLibrary;
|
||||
id<MTLRenderPipelineState> _pipelineState;
|
||||
|
||||
// Buffers.
|
||||
id<MTLBuffer> _vertexBuffer;
|
||||
|
||||
// RTC Frame parameters.
|
||||
int _offset;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
// _offset of 0 is equal to rotation of 0.
|
||||
_offset = 0;
|
||||
_inflight_semaphore = dispatch_semaphore_create(kMaxInflightBuffers);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)addRenderingDestination:(__kindof MTKView *)view {
|
||||
return [self setupWithView:view];
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
- (BOOL)setupWithView:(__kindof MTKView *)view {
|
||||
BOOL success = NO;
|
||||
if ([self setupMetal]) {
|
||||
[self setupView:view];
|
||||
[self loadAssets];
|
||||
[self setupBuffers];
|
||||
success = YES;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
#pragma mark - Inheritance
|
||||
|
||||
- (id<MTLDevice>)currentMetalDevice {
|
||||
return _device;
|
||||
}
|
||||
|
||||
- (NSString *)shaderSource {
|
||||
RTC_NOTREACHED() << "Virtual method not implemented in subclass.";
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)uploadTexturesToRenderEncoder:(id<MTLRenderCommandEncoder>)renderEncoder {
|
||||
RTC_NOTREACHED() << "Virtual method not implemented in subclass.";
|
||||
}
|
||||
|
||||
- (BOOL)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame {
|
||||
_offset = offsetForRotation(frame.rotation);
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark - GPU methods
|
||||
|
||||
- (BOOL)setupMetal {
|
||||
// Set the view to use the default device.
|
||||
_device = MTLCreateSystemDefaultDevice();
|
||||
if (!_device) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
// Create a new command queue.
|
||||
_commandQueue = [_device newCommandQueue];
|
||||
|
||||
// Load metal library from source.
|
||||
NSError *libraryError = nil;
|
||||
|
||||
id<MTLLibrary> sourceLibrary =
|
||||
[_device newLibraryWithSource:[self shaderSource] options:NULL error:&libraryError];
|
||||
|
||||
if (libraryError) {
|
||||
RTCLogError(@"Metal: Library with source failed\n%@", libraryError);
|
||||
return NO;
|
||||
}
|
||||
|
||||
if (!sourceLibrary) {
|
||||
RTCLogError(@"Metal: Failed to load library. %@", libraryError);
|
||||
return NO;
|
||||
}
|
||||
_defaultLibrary = sourceLibrary;
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)setupView:(__kindof MTKView *)view {
|
||||
view.device = _device;
|
||||
|
||||
view.preferredFramesPerSecond = 30;
|
||||
view.autoResizeDrawable = NO;
|
||||
|
||||
// We need to keep reference to the view as it's needed down the rendering pipeline.
|
||||
_view = view;
|
||||
}
|
||||
|
||||
- (void)loadAssets {
|
||||
id<MTLFunction> vertexFunction = [_defaultLibrary newFunctionWithName:vertexFunctionName];
|
||||
id<MTLFunction> fragmentFunction = [_defaultLibrary newFunctionWithName:fragmentFunctionName];
|
||||
|
||||
MTLRenderPipelineDescriptor *pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
|
||||
pipelineDescriptor.label = pipelineDescriptorLabel;
|
||||
pipelineDescriptor.vertexFunction = vertexFunction;
|
||||
pipelineDescriptor.fragmentFunction = fragmentFunction;
|
||||
pipelineDescriptor.colorAttachments[0].pixelFormat = _view.colorPixelFormat;
|
||||
pipelineDescriptor.depthAttachmentPixelFormat = MTLPixelFormatInvalid;
|
||||
NSError *error = nil;
|
||||
_pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineDescriptor error:&error];
|
||||
|
||||
if (!_pipelineState) {
|
||||
RTCLogError(@"Metal: Failed to create pipeline state. %@", error);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setupBuffers {
|
||||
_vertexBuffer = [_device newBufferWithBytes:cubeVertexData
|
||||
length:sizeof(cubeVertexData)
|
||||
options:MTLResourceOptionCPUCacheModeDefault];
|
||||
}
|
||||
|
||||
- (void)render {
|
||||
// Wait until the inflight (curently sent to GPU) command buffer
|
||||
// has completed the GPU work.
|
||||
dispatch_semaphore_wait(_inflight_semaphore, DISPATCH_TIME_FOREVER);
|
||||
|
||||
id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
|
||||
commandBuffer.label = commandBufferLabel;
|
||||
|
||||
__block dispatch_semaphore_t block_semaphore = _inflight_semaphore;
|
||||
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) {
|
||||
// GPU work completed.
|
||||
dispatch_semaphore_signal(block_semaphore);
|
||||
}];
|
||||
|
||||
MTLRenderPassDescriptor *renderPassDescriptor = _view.currentRenderPassDescriptor;
|
||||
if (renderPassDescriptor) { // Valid drawable.
|
||||
id<MTLRenderCommandEncoder> renderEncoder =
|
||||
[commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
|
||||
renderEncoder.label = renderEncoderLabel;
|
||||
|
||||
// Set context state.
|
||||
[renderEncoder pushDebugGroup:renderEncoderDebugGroup];
|
||||
[renderEncoder setRenderPipelineState:_pipelineState];
|
||||
[renderEncoder setVertexBuffer:_vertexBuffer offset:_offset * sizeof(float) atIndex:0];
|
||||
[self uploadTexturesToRenderEncoder:renderEncoder];
|
||||
|
||||
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip
|
||||
vertexStart:0
|
||||
vertexCount:4
|
||||
instanceCount:1];
|
||||
[renderEncoder popDebugGroup];
|
||||
[renderEncoder endEncoding];
|
||||
|
||||
[commandBuffer presentDrawable:_view.currentDrawable];
|
||||
}
|
||||
|
||||
// CPU work is completed, GPU work can be started.
|
||||
[commandBuffer commit];
|
||||
}
|
||||
|
||||
#pragma mark - RTCMTLRenderer
|
||||
|
||||
- (void)drawFrame:(RTCVideoFrame *)frame {
|
||||
@autoreleasepool {
|
||||
if ([self setupTexturesForFrame:frame]) {
|
||||
[self render];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
152
sdk/objc/Framework/Classes/Metal/RTCMTLVideoView.m
Normal file
152
sdk/objc/Framework/Classes/Metal/RTCMTLVideoView.m
Normal file
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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/RTCMTLVideoView.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <MetalKit/MetalKit.h>
|
||||
|
||||
#import "WebRTC/RTCLogging.h"
|
||||
#import "WebRTC/RTCVideoFrame.h"
|
||||
#import "WebRTC/RTCVideoFrameBuffer.h"
|
||||
|
||||
#import "RTCMTLI420Renderer.h"
|
||||
#import "RTCMTLNV12Renderer.h"
|
||||
|
||||
// To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime.
|
||||
// Linking errors occur when compiling for architectures that don't support Metal.
|
||||
#define MTKViewClass NSClassFromString(@"MTKView")
|
||||
#define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer")
|
||||
#define RTCMTLI420RendererClass NSClassFromString(@"RTCMTLI420Renderer")
|
||||
|
||||
@interface RTCMTLVideoView () <MTKViewDelegate>
|
||||
@property(nonatomic, strong) RTCMTLI420Renderer *rendererI420;
|
||||
@property(nonatomic, strong) RTCMTLNV12Renderer *rendererNV12;
|
||||
@property(nonatomic, strong) MTKView *metalView;
|
||||
@property(atomic, strong) RTCVideoFrame *videoFrame;
|
||||
@end
|
||||
|
||||
@implementation RTCMTLVideoView
|
||||
|
||||
@synthesize rendererI420 = _rendererI420;
|
||||
@synthesize rendererNV12 = _rendererNV12;
|
||||
@synthesize metalView = _metalView;
|
||||
@synthesize videoFrame = _videoFrame;
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frameRect {
|
||||
self = [super initWithFrame:frameRect];
|
||||
if (self) {
|
||||
[self configure];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)aCoder {
|
||||
self = [super initWithCoder:aCoder];
|
||||
if (self) {
|
||||
[self configure];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - Private
|
||||
|
||||
+ (BOOL)isMetalAvailable {
|
||||
#if defined(RTC_SUPPORTS_METAL)
|
||||
return YES;
|
||||
#else
|
||||
return NO;
|
||||
#endif
|
||||
}
|
||||
|
||||
+ (MTKView *)createMetalView:(CGRect)frame {
|
||||
MTKView *view = [[MTKViewClass alloc] initWithFrame:frame];
|
||||
return view;
|
||||
}
|
||||
|
||||
+ (RTCMTLNV12Renderer *)createNV12Renderer {
|
||||
return [[RTCMTLNV12RendererClass alloc] init];
|
||||
}
|
||||
|
||||
+ (RTCMTLI420Renderer *)createI420Renderer {
|
||||
return [[RTCMTLI420RendererClass alloc] init];
|
||||
}
|
||||
|
||||
- (void)configure {
|
||||
NSAssert([RTCMTLVideoView isMetalAvailable], @"Metal not availiable on this device");
|
||||
|
||||
_metalView = [RTCMTLVideoView createMetalView:self.bounds];
|
||||
[self configureMetalView];
|
||||
}
|
||||
|
||||
- (void)configureMetalView {
|
||||
if (_metalView) {
|
||||
_metalView.delegate = self;
|
||||
[self addSubview:_metalView];
|
||||
_metalView.contentMode = UIViewContentModeScaleAspectFit;
|
||||
_metalView.translatesAutoresizingMaskIntoConstraints = NO;
|
||||
UILayoutGuide *margins = self.layoutMarginsGuide;
|
||||
[_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES;
|
||||
[_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES;
|
||||
[_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES;
|
||||
[_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - MTKViewDelegate methods
|
||||
|
||||
- (void)drawInMTKView:(nonnull MTKView *)view {
|
||||
NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign instance.");
|
||||
if (!self.videoFrame) {
|
||||
return;
|
||||
}
|
||||
|
||||
id<RTCMTLRenderer> renderer = nil;
|
||||
if ([self.videoFrame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
|
||||
if (!self.rendererNV12) {
|
||||
self.rendererNV12 = [RTCMTLVideoView createNV12Renderer];
|
||||
if (![self.rendererNV12 addRenderingDestination:self.metalView]) {
|
||||
self.rendererNV12 = nil;
|
||||
RTCLogError(@"Failed to create NV12 renderer");
|
||||
}
|
||||
}
|
||||
renderer = self.rendererNV12;
|
||||
} else {
|
||||
if (!self.rendererI420) {
|
||||
self.rendererI420 = [RTCMTLVideoView createI420Renderer];
|
||||
if (![self.rendererI420 addRenderingDestination:self.metalView]) {
|
||||
self.rendererI420 = nil;
|
||||
RTCLogError(@"Failed to create I420 renderer");
|
||||
}
|
||||
}
|
||||
renderer = self.rendererI420;
|
||||
}
|
||||
|
||||
[renderer drawFrame:self.videoFrame];
|
||||
}
|
||||
|
||||
- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
|
||||
}
|
||||
|
||||
#pragma mark - RTCVideoRenderer
|
||||
|
||||
- (void)setSize:(CGSize)size {
|
||||
self.metalView.drawableSize = size;
|
||||
}
|
||||
|
||||
- (void)renderFrame:(nullable RTCVideoFrame *)frame {
|
||||
if (frame == nil) {
|
||||
RTCLogInfo(@"Incoming frame is nil. Exiting render callback.");
|
||||
return;
|
||||
}
|
||||
self.videoFrame = frame;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user