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/RTCMTLNSVideoView.h" | |
12 | |
13 #import <Metal/Metal.h> | |
14 #import <MetalKit/MetalKit.h> | |
15 | |
16 #import "WebRTC/RTCVideoFrame.h" | |
17 | |
18 #import "RTCMTLI420Renderer.h" | |
19 | |
20 @interface RTCMTLNSVideoView () <MTKViewDelegate> | |
21 @property(nonatomic) id<RTCMTLRenderer> renderer; | |
22 @property(nonatomic, strong) MTKView *metalView; | |
23 @property(atomic, strong) RTCVideoFrame *videoFrame; | |
24 @end | |
25 | |
26 @implementation RTCMTLNSVideoView { | |
27 id<RTCMTLRenderer> _renderer; | |
28 } | |
29 | |
30 @synthesize renderer = _renderer; | |
31 @synthesize metalView = _metalView; | |
32 @synthesize videoFrame = _videoFrame; | |
33 | |
34 - (instancetype)initWithFrame:(CGRect)frameRect { | |
35 self = [super initWithFrame:frameRect]; | |
36 if (self) { | |
37 [self configure]; | |
38 } | |
39 return self; | |
40 } | |
41 | |
42 - (instancetype)initWithCoder:(NSCoder *)aCoder { | |
43 self = [super initWithCoder:aCoder]; | |
44 if (self) { | |
45 [self configure]; | |
46 } | |
47 return self; | |
48 } | |
49 | |
50 #pragma mark - Private | |
51 | |
52 + (BOOL)isMetalAvailable { | |
53 return YES; | |
54 } | |
55 | |
56 - (void)configure { | |
57 if ([[self class] isMetalAvailable]) { | |
58 _metalView = [[MTKView alloc] initWithFrame:self.bounds]; | |
59 [self addSubview:_metalView]; | |
60 _metalView.translatesAutoresizingMaskIntoConstraints = NO; | |
61 _metalView.framebufferOnly = YES; | |
62 _metalView.delegate = self; | |
63 | |
64 _renderer = [[RTCMTLI420Renderer alloc] init]; | |
65 if (![(RTCMTLI420Renderer *)_renderer addRenderingDestination:_metalView]) { | |
66 _renderer = nil; | |
67 }; | |
68 } | |
69 } | |
70 | |
71 - (void)updateConstraints { | |
72 NSDictionary *views = NSDictionaryOfVariableBindings(_metalView); | |
73 | |
74 NSArray *constraintsHorizontal = | |
75 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_metalView]-0-|" | |
76 options:0 | |
77 metrics:nil | |
78 views:views]; | |
79 [self addConstraints:constraintsHorizontal]; | |
80 | |
81 NSArray *constraintsVertical = | |
82 [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_metalView]-0-|" | |
83 options:0 | |
84 metrics:nil | |
85 views:views]; | |
86 [self addConstraints:constraintsVertical]; | |
87 [super updateConstraints]; | |
88 } | |
89 | |
90 #pragma mark - MTKViewDelegate methods | |
91 - (void)drawInMTKView:(nonnull MTKView *)view { | |
92 if (view == self.metalView) { | |
93 [_renderer drawFrame:self.videoFrame]; | |
94 } | |
95 } | |
96 | |
97 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { | |
98 } | |
99 | |
100 #pragma mark - RTCVideoRenderer | |
101 | |
102 - (void)setSize:(CGSize)size { | |
103 _metalView.drawableSize = size; | |
104 [_metalView draw]; | |
105 } | |
106 | |
107 - (void)renderFrame:(nullable RTCVideoFrame *)frame { | |
108 if (frame == nil) { | |
109 return; | |
110 } | |
111 self.videoFrame = [frame newI420VideoFrame]; | |
magjed_webrtc
2017/03/28 11:20:29
We should look at frame.nativeHandle instead and d
daniela-webrtc
2017/03/28 14:58:05
Is it ok to fix this in upcoming CL when I do the
magjed_webrtc
2017/03/30 08:29:28
Yes, that's fine.
| |
112 } | |
113 | |
114 @end | |
OLD | NEW |