denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #import "WebRTC/RTCMTLVideoView.h" |
| 12 | |
| 13 | #import <Metal/Metal.h> |
| 14 | #import <MetalKit/MetalKit.h> |
| 15 | |
| 16 | #import "WebRTC/RTCLogging.h" |
| 17 | #import "WebRTC/RTCVideoFrame.h" |
| 18 | |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 19 | #import "RTCMTLI420Renderer.h" |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 20 | #import "RTCMTLNV12Renderer.h" |
| 21 | |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 22 | // To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime. |
| 23 | // Linking errors occur when compiling for architectures that don't support Metal. |
| 24 | #define MTKViewClass NSClassFromString(@"MTKView") |
| 25 | #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 26 | #define RTCMTLI420RendererClass NSClassFromString(@"RTCMTLI420Renderer") |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 27 | |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 28 | @interface RTCMTLVideoView () <MTKViewDelegate> |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 29 | @property(nonatomic, strong) RTCMTLI420Renderer *rendererI420; |
| 30 | @property(nonatomic, strong) RTCMTLNV12Renderer *rendererNV12; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 31 | @property(nonatomic, strong) MTKView *metalView; |
| 32 | @property(atomic, strong) RTCVideoFrame *videoFrame; |
| 33 | @end |
| 34 | |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 35 | @implementation RTCMTLVideoView |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 36 | |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 37 | @synthesize rendererI420 = _rendererI420; |
| 38 | @synthesize rendererNV12 = _rendererNV12; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 39 | @synthesize metalView = _metalView; |
| 40 | @synthesize videoFrame = _videoFrame; |
| 41 | |
| 42 | - (instancetype)initWithFrame:(CGRect)frameRect { |
| 43 | self = [super initWithFrame:frameRect]; |
| 44 | if (self) { |
| 45 | [self configure]; |
| 46 | } |
| 47 | return self; |
| 48 | } |
| 49 | |
| 50 | - (instancetype)initWithCoder:(NSCoder *)aCoder { |
| 51 | self = [super initWithCoder:aCoder]; |
| 52 | if (self) { |
| 53 | [self configure]; |
| 54 | } |
| 55 | return self; |
| 56 | } |
| 57 | |
| 58 | #pragma mark - Private |
| 59 | |
| 60 | + (BOOL)isMetalAvailable { |
kthelgason | a2fb30c | 2017-03-09 11:34:27 | [diff] [blame] | 61 | #if defined(RTC_SUPPORTS_METAL) |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 62 | return YES; |
kthelgason | a2fb30c | 2017-03-09 11:34:27 | [diff] [blame] | 63 | #else |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 64 | return NO; |
| 65 | #endif |
| 66 | } |
| 67 | |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 68 | + (MTKView *)createMetalView:(CGRect)frame { |
| 69 | MTKView *view = [[MTKViewClass alloc] initWithFrame:frame]; |
| 70 | return view; |
| 71 | } |
| 72 | |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 73 | + (RTCMTLNV12Renderer *)createNV12Renderer { |
| 74 | return [[RTCMTLNV12RendererClass alloc] init]; |
| 75 | } |
| 76 | |
| 77 | + (RTCMTLI420Renderer *)createI420Renderer { |
| 78 | return [[RTCMTLI420RendererClass alloc] init]; |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 79 | } |
| 80 | |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 81 | - (void)configure { |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 82 | NSAssert([RTCMTLVideoView isMetalAvailable], @"Metal not availiable on this device"); |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 83 | |
| 84 | _metalView = [RTCMTLVideoView createMetalView:self.bounds]; |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 85 | [self configureMetalView]; |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | - (void)configureMetalView { |
| 89 | if (_metalView) { |
kthelgason | 96d9152 | 2017-03-08 14:33:52 | [diff] [blame] | 90 | _metalView.delegate = self; |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 91 | [self addSubview:_metalView]; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 92 | _metalView.contentMode = UIViewContentModeScaleAspectFit; |
| 93 | _metalView.translatesAutoresizingMaskIntoConstraints = NO; |
| 94 | UILayoutGuide *margins = self.layoutMarginsGuide; |
| 95 | [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES; |
| 96 | [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES; |
| 97 | [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES; |
| 98 | [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 99 | } |
| 100 | } |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 101 | |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 102 | #pragma mark - MTKViewDelegate methods |
| 103 | |
| 104 | - (void)drawInMTKView:(nonnull MTKView *)view { |
| 105 | NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign instance."); |
denicija | d208815 | 2017-04-28 09:14:54 | [diff] [blame] | 106 | if (!self.videoFrame) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | id<RTCMTLRenderer> renderer = nil; |
| 111 | if (self.videoFrame.nativeHandle) { |
| 112 | if (!self.rendererNV12) { |
| 113 | self.rendererNV12 = [RTCMTLVideoView createNV12Renderer]; |
| 114 | if (![self.rendererNV12 addRenderingDestination:self.metalView]) { |
| 115 | self.rendererNV12 = nil; |
| 116 | RTCLogError(@"Failed to create NV12 renderer"); |
| 117 | } |
| 118 | } |
| 119 | renderer = self.rendererNV12; |
| 120 | } else { |
| 121 | if (!self.rendererI420) { |
| 122 | self.rendererI420 = [RTCMTLVideoView createI420Renderer]; |
| 123 | if (![self.rendererI420 addRenderingDestination:self.metalView]) { |
| 124 | self.rendererI420 = nil; |
| 125 | RTCLogError(@"Failed to create I420 renderer"); |
| 126 | } |
| 127 | } |
| 128 | renderer = self.rendererI420; |
| 129 | } |
| 130 | |
| 131 | [renderer drawFrame:self.videoFrame]; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { |
| 135 | } |
| 136 | |
| 137 | #pragma mark - RTCVideoRenderer |
| 138 | |
| 139 | - (void)setSize:(CGSize)size { |
kthelgason | 954d9b9 | 2017-03-09 11:36:58 | [diff] [blame] | 140 | self.metalView.drawableSize = size; |
denicija | 070d5e3 | 2017-02-26 19:44:13 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | - (void)renderFrame:(nullable RTCVideoFrame *)frame { |
| 144 | if (frame == nil) { |
| 145 | RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); |
| 146 | return; |
| 147 | } |
| 148 | self.videoFrame = frame; |
| 149 | } |
| 150 | |
| 151 | @end |