OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #import "WebRTC/RTCMTLVideoView.h" | 11 #import "WebRTC/RTCMTLVideoView.h" |
12 | 12 |
13 #import <Metal/Metal.h> | 13 #import <Metal/Metal.h> |
14 #import <MetalKit/MetalKit.h> | 14 #import <MetalKit/MetalKit.h> |
15 | 15 |
16 #import "WebRTC/RTCLogging.h" | 16 #import "WebRTC/RTCLogging.h" |
17 #import "WebRTC/RTCVideoFrame.h" | 17 #import "WebRTC/RTCVideoFrame.h" |
18 | 18 |
19 #import "RTCMTLI420Renderer.h" | |
19 #import "RTCMTLNV12Renderer.h" | 20 #import "RTCMTLNV12Renderer.h" |
20 | 21 |
21 // To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime. | 22 // To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime. |
22 // Linking errors occur when compiling for architectures that don't support Meta l. | 23 // Linking errors occur when compiling for architectures that don't support Meta l. |
23 #define MTKViewClass NSClassFromString(@"MTKView") | 24 #define MTKViewClass NSClassFromString(@"MTKView") |
24 #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") | 25 #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") |
26 #define RTCMTLI420RendererClass NSClassFromString(@"RTCMTLI420Renderer") | |
25 | 27 |
26 @interface RTCMTLVideoView () <MTKViewDelegate> | 28 @interface RTCMTLVideoView () <MTKViewDelegate> |
27 @property(nonatomic, strong) RTCMTLNV12Renderer *renderer; | 29 @property(nonatomic, strong) RTCMTLI420Renderer *rendererI420; |
30 @property(nonatomic, strong) RTCMTLNV12Renderer *rendererNV12; | |
28 @property(nonatomic, strong) MTKView *metalView; | 31 @property(nonatomic, strong) MTKView *metalView; |
29 @property(atomic, strong) RTCVideoFrame *videoFrame; | 32 @property(atomic, strong) RTCVideoFrame *videoFrame; |
30 @end | 33 @end |
31 | 34 |
32 @implementation RTCMTLVideoView | 35 @implementation RTCMTLVideoView |
33 | 36 |
34 @synthesize renderer = _renderer; | 37 @synthesize rendererI420 = _rendererI420; |
38 @synthesize rendererNV12 = _rendererNV12; | |
35 @synthesize metalView = _metalView; | 39 @synthesize metalView = _metalView; |
36 @synthesize videoFrame = _videoFrame; | 40 @synthesize videoFrame = _videoFrame; |
37 | 41 |
38 - (instancetype)initWithFrame:(CGRect)frameRect { | 42 - (instancetype)initWithFrame:(CGRect)frameRect { |
39 self = [super initWithFrame:frameRect]; | 43 self = [super initWithFrame:frameRect]; |
40 if (self) { | 44 if (self) { |
41 [self configure]; | 45 [self configure]; |
42 } | 46 } |
43 return self; | 47 return self; |
44 } | 48 } |
(...skipping 14 matching lines...) Expand all Loading... | |
59 #else | 63 #else |
60 return NO; | 64 return NO; |
61 #endif | 65 #endif |
62 } | 66 } |
63 | 67 |
64 + (MTKView *)createMetalView:(CGRect)frame { | 68 + (MTKView *)createMetalView:(CGRect)frame { |
65 MTKView *view = [[MTKViewClass alloc] initWithFrame:frame]; | 69 MTKView *view = [[MTKViewClass alloc] initWithFrame:frame]; |
66 return view; | 70 return view; |
67 } | 71 } |
68 | 72 |
69 + (RTCMTLNV12Renderer *)createMetalRenderer { | 73 + (RTCMTLNV12Renderer *)createNV12Renderer { |
70 RTCMTLNV12Renderer *renderer = [[RTCMTLNV12RendererClass alloc] init]; | 74 RTCMTLNV12Renderer *renderer = [[RTCMTLNV12RendererClass alloc] init]; |
magjed_webrtc
2017/04/04 08:07:36
nit: Maybe just return the result directly?
| |
71 return renderer; | 75 return renderer; |
72 } | 76 } |
73 | 77 |
78 + (RTCMTLI420Renderer *)createI420Renderer { | |
79 RTCMTLI420Renderer *renderer = [[RTCMTLI420RendererClass alloc] init]; | |
80 return renderer; | |
81 } | |
82 | |
74 - (void)configure { | 83 - (void)configure { |
75 if (![RTCMTLVideoView isMetalAvailable]) { | 84 if (![RTCMTLVideoView isMetalAvailable]) { |
76 RTCLog("Metal unavailable"); | 85 RTCLog("Metal unavailable"); |
77 return; | 86 return; |
78 } | 87 } |
79 | 88 |
80 _metalView = [RTCMTLVideoView createMetalView:self.bounds]; | 89 _metalView = [RTCMTLVideoView createMetalView:self.bounds]; |
81 _renderer = [RTCMTLVideoView createMetalRenderer]; | 90 _rendererI420 = [RTCMTLVideoView createI420Renderer]; |
magjed_webrtc
2017/04/04 08:07:36
Are these expensive to set up? If they are, we cou
daniela-webrtc
2017/04/05 11:49:31
Sadly the configuration is quite expensive and ent
magjed_webrtc
2017/04/05 12:10:38
Ok, so can we try to move the configuration of the
| |
91 _rendererNV12 = [RTCMTLVideoView createNV12Renderer]; | |
82 | 92 |
83 if ([self configureMetalRenderer]) { | 93 if ([self configureMetalRenderers]) { |
84 [self configureMetalView]; | 94 [self configureMetalView]; |
85 } else { | 95 } else { |
86 _renderer = nil; | |
87 RTCLogError("Metal configuration falied."); | 96 RTCLogError("Metal configuration falied."); |
88 } | 97 } |
89 } | 98 } |
90 | 99 |
91 - (BOOL)configureMetalRenderer { | 100 - (BOOL)configureMetalRenderers { |
92 return [_renderer addRenderingDestination:_metalView]; | 101 if (![_rendererI420 addRenderingDestination:_metalView]) { |
102 _rendererI420 = nil; | |
103 } | |
104 | |
105 if (![_rendererNV12 addRenderingDestination:_metalView]) { | |
106 _rendererNV12 = nil; | |
107 } | |
108 return _rendererNV12 || _rendererI420; | |
93 } | 109 } |
94 | 110 |
95 - (void)configureMetalView { | 111 - (void)configureMetalView { |
96 if (_metalView) { | 112 if (_metalView) { |
97 _metalView.delegate = self; | 113 _metalView.delegate = self; |
98 [self addSubview:_metalView]; | 114 [self addSubview:_metalView]; |
99 _metalView.contentMode = UIViewContentModeScaleAspectFit; | 115 _metalView.contentMode = UIViewContentModeScaleAspectFit; |
100 _metalView.translatesAutoresizingMaskIntoConstraints = NO; | 116 _metalView.translatesAutoresizingMaskIntoConstraints = NO; |
101 UILayoutGuide *margins = self.layoutMarginsGuide; | 117 UILayoutGuide *margins = self.layoutMarginsGuide; |
102 [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YE S; | 118 [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YE S; |
103 [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].activ e = YES; | 119 [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].activ e = YES; |
104 [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES; | 120 [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES; |
105 [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; | 121 [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; |
106 } | 122 } |
107 } | 123 } |
108 | 124 |
109 #pragma mark - MTKViewDelegate methods | 125 #pragma mark - MTKViewDelegate methods |
110 | 126 |
111 - (void)drawInMTKView:(nonnull MTKView *)view { | 127 - (void)drawInMTKView:(nonnull MTKView *)view { |
112 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce."); | 128 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce."); |
113 [self.renderer drawFrame:self.videoFrame]; | 129 if (!self.videoFrame) { |
130 return; | |
131 } | |
132 | |
133 if (self.videoFrame.nativeHandle) { | |
134 [self.rendererNV12 drawFrame:self.videoFrame]; | |
135 } else { | |
136 [self.rendererI420 drawFrame:self.videoFrame]; | |
137 } | |
114 } | 138 } |
115 | 139 |
116 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { | 140 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { |
117 } | 141 } |
118 | 142 |
119 #pragma mark - RTCVideoRenderer | 143 #pragma mark - RTCVideoRenderer |
120 | 144 |
121 - (void)setSize:(CGSize)size { | 145 - (void)setSize:(CGSize)size { |
122 self.metalView.drawableSize = size; | 146 self.metalView.drawableSize = size; |
123 [self.metalView draw]; | 147 [self.metalView draw]; |
124 } | 148 } |
125 | 149 |
126 - (void)renderFrame:(nullable RTCVideoFrame *)frame { | 150 - (void)renderFrame:(nullable RTCVideoFrame *)frame { |
127 if (frame == nil) { | 151 if (frame == nil) { |
128 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); | 152 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); |
129 return; | 153 return; |
130 } | 154 } |
131 self.videoFrame = frame; | 155 self.videoFrame = frame; |
132 } | 156 } |
133 | 157 |
134 @end | 158 @end |
OLD | NEW |