OLD | NEW |
| (Empty) |
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 | |
19 #import "RTCMTLNV12Renderer.h" | |
20 | |
21 @interface RTCMTLVideoView () <MTKViewDelegate> | |
22 @property(nonatomic, strong) id<RTCMTLRenderer> renderer; | |
23 @property(nonatomic, strong) MTKView *metalView; | |
24 @property(atomic, strong) RTCVideoFrame *videoFrame; | |
25 @end | |
26 | |
27 @implementation RTCMTLVideoView { | |
28 id<RTCMTLRenderer> _renderer; | |
29 } | |
30 | |
31 @synthesize renderer = _renderer; | |
32 @synthesize metalView = _metalView; | |
33 @synthesize videoFrame = _videoFrame; | |
34 | |
35 - (instancetype)initWithFrame:(CGRect)frameRect { | |
36 self = [super initWithFrame:frameRect]; | |
37 if (self) { | |
38 [self configure]; | |
39 } | |
40 return self; | |
41 } | |
42 | |
43 - (instancetype)initWithCoder:(NSCoder *)aCoder { | |
44 self = [super initWithCoder:aCoder]; | |
45 if (self) { | |
46 [self configure]; | |
47 } | |
48 return self; | |
49 } | |
50 | |
51 #pragma mark - Private | |
52 | |
53 + (BOOL)isMetalAvailable { | |
54 #if defined(__OBJC__) && COREVIDEO_SUPPORTS_METAL | |
55 return YES; | |
56 #elif | |
57 return NO; | |
58 #endif | |
59 } | |
60 | |
61 - (void)configure { | |
62 if ([RTCMTLVideoView isMetalAvailable]) { | |
63 _metalView = [[MTKView alloc] initWithFrame:self.bounds]; | |
64 [self addSubview:_metalView]; | |
65 _metalView.delegate = self; | |
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 | |
74 _renderer = [[RTCMTLNV12Renderer alloc] init]; | |
75 if (![(RTCMTLNV12Renderer *)_renderer addRenderingDestination:_metalView]) { | |
76 _renderer = nil; | |
77 }; | |
78 } else { | |
79 RTCLogError("Metal configuration falied."); | |
80 } | |
81 } | |
82 #pragma mark - MTKViewDelegate methods | |
83 | |
84 - (void)drawInMTKView:(nonnull MTKView *)view { | |
85 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta
nce."); | |
86 [_renderer drawFrame:self.videoFrame]; | |
87 } | |
88 | |
89 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { | |
90 } | |
91 | |
92 #pragma mark - RTCVideoRenderer | |
93 | |
94 - (void)setSize:(CGSize)size { | |
95 _metalView.drawableSize = size; | |
96 [_metalView draw]; | |
97 } | |
98 | |
99 - (void)renderFrame:(nullable RTCVideoFrame *)frame { | |
100 if (frame == nil) { | |
101 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); | |
102 return; | |
103 } | |
104 self.videoFrame = frame; | |
105 } | |
106 | |
107 @end | |
OLD | NEW |