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 "RTCMTLNV12Renderer.h" | 19 #import "RTCMTLNV12Renderer.h" |
20 | 20 |
21 // 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 #define MTKViewClass NSClassFromString(@"MTKView") | |
24 #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") | |
kthelgason
2017/03/02 08:23:05
This frightens me a little bit. Can't we just not
| |
25 | |
21 @interface RTCMTLVideoView () <MTKViewDelegate> | 26 @interface RTCMTLVideoView () <MTKViewDelegate> |
22 @property(nonatomic, strong) id<RTCMTLRenderer> renderer; | 27 @property(nonatomic, strong) id<RTCMTLRenderer> renderer; |
23 @property(nonatomic, strong) MTKView *metalView; | 28 @property(nonatomic, strong) UIView *metalView; |
magjed_webrtc
2017/03/02 13:28:18
Why can't this still be an MTKView when we mock it
daniela-webrtc
2017/03/03 08:47:01
I thought it will give linking errors when using t
| |
24 @property(atomic, strong) RTCVideoFrame *videoFrame; | 29 @property(atomic, strong) RTCVideoFrame *videoFrame; |
25 @end | 30 @end |
26 | 31 |
27 @implementation RTCMTLVideoView { | 32 @implementation RTCMTLVideoView |
28 id<RTCMTLRenderer> _renderer; | |
29 } | |
30 | 33 |
31 @synthesize renderer = _renderer; | 34 @synthesize renderer = _renderer; |
32 @synthesize metalView = _metalView; | 35 @synthesize metalView = _metalView; |
33 @synthesize videoFrame = _videoFrame; | 36 @synthesize videoFrame = _videoFrame; |
34 | 37 |
35 - (instancetype)initWithFrame:(CGRect)frameRect { | 38 - (instancetype)initWithFrame:(CGRect)frameRect { |
36 self = [super initWithFrame:frameRect]; | 39 self = [super initWithFrame:frameRect]; |
37 if (self) { | 40 if (self) { |
38 [self configure]; | 41 [self configure]; |
39 } | 42 } |
(...skipping 11 matching lines...) Expand all Loading... | |
51 #pragma mark - Private | 54 #pragma mark - Private |
52 | 55 |
53 + (BOOL)isMetalAvailable { | 56 + (BOOL)isMetalAvailable { |
54 #if defined(__OBJC__) && COREVIDEO_SUPPORTS_METAL | 57 #if defined(__OBJC__) && COREVIDEO_SUPPORTS_METAL |
55 return YES; | 58 return YES; |
56 #elif | 59 #elif |
57 return NO; | 60 return NO; |
58 #endif | 61 #endif |
59 } | 62 } |
60 | 63 |
64 + (UIView *)createMetalView:(CGRect)frame { | |
65 UIView *view = [[MTKViewClass alloc] initWithFrame:frame]; | |
66 return view; | |
67 } | |
68 | |
69 + (id<RTCMTLRenderer>)createMetalRenderer { | |
70 id<RTCMTLRenderer> renderer = [[RTCMTLNV12RendererClass alloc] init]; | |
71 return renderer; | |
72 } | |
73 | |
61 - (void)configure { | 74 - (void)configure { |
62 if ([RTCMTLVideoView isMetalAvailable]) { | 75 if (![RTCMTLVideoView isMetalAvailable]) { |
63 _metalView = [[MTKView alloc] initWithFrame:self.bounds]; | 76 RTCLog("Metal unavailable"); |
64 [self addSubview:_metalView]; | 77 return; |
65 _metalView.delegate = self; | 78 } |
66 _metalView.contentMode = UIViewContentModeScaleAspectFit; | |
67 _metalView.translatesAutoresizingMaskIntoConstraints = NO; | |
68 UILayoutGuide *margins = self.layoutMarginsGuide; | |
69 [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YE S; | |
70 [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].activ e = YES; | |
71 [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES; | |
72 [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; | |
73 | 79 |
74 _renderer = [[RTCMTLNV12Renderer alloc] init]; | 80 _metalView = [RTCMTLVideoView createMetalView:self.bounds]; |
75 if (![(RTCMTLNV12Renderer *)_renderer addRenderingDestination:_metalView]) { | 81 _renderer = [RTCMTLVideoView createMetalRenderer]; |
76 _renderer = nil; | 82 |
77 }; | 83 if ([self configureMetalRenderer]) { |
84 [self configureMetalView]; | |
78 } else { | 85 } else { |
86 _renderer = nil; | |
79 RTCLogError("Metal configuration falied."); | 87 RTCLogError("Metal configuration falied."); |
80 } | 88 } |
81 } | 89 } |
90 | |
91 - (BOOL)configureMetalRenderer { | |
92 BOOL configurationSuccessful = NO; | |
93 if ([_renderer addRenderingDestination:_metalView]) { | |
magjed_webrtc
2017/03/02 13:28:18
do:
return [_renderer addRenderingDestination:_met
daniela-webrtc
2017/03/03 08:47:01
Done.
| |
94 configurationSuccessful = YES; | |
95 }; | |
96 return configurationSuccessful; | |
97 } | |
98 | |
99 - (void)configureMetalView { | |
100 if (_metalView) { | |
101 MTKView *metalView = (MTKView *)_metalView; | |
102 metalView.delegate = self; | |
103 [self addSubview:metalView]; | |
104 metalView.contentMode = UIViewContentModeScaleAspectFit; | |
105 metalView.translatesAutoresizingMaskIntoConstraints = NO; | |
106 UILayoutGuide *margins = self.layoutMarginsGuide; | |
107 [metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES ; | |
108 [metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES; | |
109 [metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = Y ES; | |
110 [metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; | |
111 } | |
112 } | |
113 | |
82 #pragma mark - MTKViewDelegate methods | 114 #pragma mark - MTKViewDelegate methods |
83 | 115 |
84 - (void)drawInMTKView:(nonnull MTKView *)view { | 116 - (void)drawInMTKView:(nonnull MTKView *)view { |
85 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce."); | 117 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce."); |
86 [_renderer drawFrame:self.videoFrame]; | 118 [self.renderer drawFrame:self.videoFrame]; |
87 } | 119 } |
88 | 120 |
89 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { | 121 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { |
90 } | 122 } |
91 | 123 |
92 #pragma mark - RTCVideoRenderer | 124 #pragma mark - RTCVideoRenderer |
93 | 125 |
94 - (void)setSize:(CGSize)size { | 126 - (void)setSize:(CGSize)size { |
95 _metalView.drawableSize = size; | 127 MTKView *metalView = (MTKView *)self.metalView; |
96 [_metalView draw]; | 128 metalView.drawableSize = size; |
129 [metalView draw]; | |
97 } | 130 } |
98 | 131 |
99 - (void)renderFrame:(nullable RTCVideoFrame *)frame { | 132 - (void)renderFrame:(nullable RTCVideoFrame *)frame { |
100 if (frame == nil) { | 133 if (frame == nil) { |
101 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); | 134 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); |
102 return; | 135 return; |
103 } | 136 } |
104 self.videoFrame = frame; | 137 self.videoFrame = frame; |
105 } | 138 } |
106 | 139 |
107 @end | 140 @end |
OLD | NEW |