OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 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 <Foundation/Foundation.h> | 11 #import <Foundation/Foundation.h> |
12 | 12 |
13 #if !TARGET_OS_IPHONE | 13 #if !TARGET_OS_IPHONE |
14 | 14 |
15 #import "WebRTC/RTCNSGLVideoView.h" | 15 #import "WebRTC/RTCNSGLVideoView.h" |
16 | 16 |
17 #import <AppKit/NSOpenGL.h> | |
17 #import <CoreVideo/CVDisplayLink.h> | 18 #import <CoreVideo/CVDisplayLink.h> |
18 #import <OpenGL/gl3.h> | 19 #import <OpenGL/gl3.h> |
19 | 20 |
20 #import "RTCOpenGLVideoRenderer.h" | 21 #import "RTCShader+Private.h" |
22 #import "WebRTC/RTCLogging.h" | |
21 #import "WebRTC/RTCVideoFrame.h" | 23 #import "WebRTC/RTCVideoFrame.h" |
22 | 24 |
23 @interface RTCNSGLVideoView () | 25 @interface RTCNSGLVideoView () |
24 // |videoFrame| is set when we receive a frame from a worker thread and is read | 26 // |videoFrame| is set when we receive a frame from a worker thread and is read |
25 // from the display link callback so atomicity is required. | 27 // from the display link callback so atomicity is required. |
26 @property(atomic, strong) RTCVideoFrame *videoFrame; | 28 @property(atomic, strong) RTCVideoFrame *videoFrame; |
27 @property(atomic, strong) RTCOpenGLVideoRenderer *glRenderer; | |
28 - (void)drawFrame; | 29 - (void)drawFrame; |
29 @end | 30 @end |
30 | 31 |
31 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, | 32 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
32 const CVTimeStamp *now, | 33 const CVTimeStamp *now, |
33 const CVTimeStamp *outputTime, | 34 const CVTimeStamp *outputTime, |
34 CVOptionFlags flagsIn, | 35 CVOptionFlags flagsIn, |
35 CVOptionFlags *flagsOut, | 36 CVOptionFlags *flagsOut, |
36 void *displayLinkContext) { | 37 void *displayLinkContext) { |
37 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; | 38 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; |
38 [view drawFrame]; | 39 [view drawFrame]; |
39 return kCVReturnSuccess; | 40 return kCVReturnSuccess; |
40 } | 41 } |
41 | 42 |
42 @implementation RTCNSGLVideoView { | 43 @implementation RTCNSGLVideoView { |
43 CVDisplayLinkRef _displayLink; | 44 CVDisplayLinkRef _displayLink; |
45 id<RTCShader> _i420Shader; | |
daniela-webrtc
2017/04/12 08:46:27
Should the shader be atomic property?
magjed_webrtc
2017/04/12 13:29:06
Good catch. Yes, it looks necessary.
| |
46 RTCVideoFrame *_lastDrawnFrame; | |
44 } | 47 } |
45 | 48 |
46 @synthesize delegate = _delegate; | 49 @synthesize delegate = _delegate; |
47 @synthesize videoFrame = _videoFrame; | 50 @synthesize videoFrame = _videoFrame; |
48 @synthesize glRenderer = _glRenderer; | |
49 | 51 |
50 - (void)dealloc { | 52 - (void)dealloc { |
51 [self teardownDisplayLink]; | 53 [self teardownDisplayLink]; |
52 } | 54 } |
53 | 55 |
54 - (void)drawRect:(NSRect)rect { | 56 - (void)drawRect:(NSRect)rect { |
55 [self drawFrame]; | 57 [self drawFrame]; |
56 } | 58 } |
57 | 59 |
58 - (void)reshape { | 60 - (void)reshape { |
59 [super reshape]; | 61 [super reshape]; |
60 NSRect frame = [self frame]; | 62 NSRect frame = [self frame]; |
61 CGLLockContext([[self openGLContext] CGLContextObj]); | 63 CGLLockContext([[self openGLContext] CGLContextObj]); |
62 glViewport(0, 0, frame.size.width, frame.size.height); | 64 glViewport(0, 0, frame.size.width, frame.size.height); |
63 CGLUnlockContext([[self openGLContext] CGLContextObj]); | 65 CGLUnlockContext([[self openGLContext] CGLContextObj]); |
64 } | 66 } |
65 | 67 |
66 - (void)lockFocus { | 68 - (void)lockFocus { |
67 NSOpenGLContext *context = [self openGLContext]; | 69 NSOpenGLContext *context = [self openGLContext]; |
68 [super lockFocus]; | 70 [super lockFocus]; |
69 if ([context view] != self) { | 71 if ([context view] != self) { |
70 [context setView:self]; | 72 [context setView:self]; |
71 } | 73 } |
72 [context makeCurrentContext]; | 74 [context makeCurrentContext]; |
73 } | 75 } |
74 | 76 |
75 - (void)prepareOpenGL { | 77 - (void)prepareOpenGL { |
76 [super prepareOpenGL]; | 78 [super prepareOpenGL]; |
77 if (!self.glRenderer) { | 79 [self ensureGLContext]; |
78 self.glRenderer = | 80 glDisable(GL_DITHER); |
79 [[RTCOpenGLVideoRenderer alloc] initWithContext:[self openGLContext]]; | |
80 } | |
81 [self.glRenderer setupGL]; | |
82 [self setupDisplayLink]; | 81 [self setupDisplayLink]; |
83 } | 82 } |
84 | 83 |
85 - (void)clearGLContext { | 84 - (void)clearGLContext { |
86 [self.glRenderer teardownGL]; | 85 [self ensureGLContext]; |
87 self.glRenderer = nil; | 86 _i420Shader = nil; |
88 [super clearGLContext]; | 87 [super clearGLContext]; |
89 } | 88 } |
90 | 89 |
91 #pragma mark - RTCVideoRenderer | 90 #pragma mark - RTCVideoRenderer |
92 | 91 |
93 // These methods may be called on non-main thread. | 92 // These methods may be called on non-main thread. |
94 - (void)setSize:(CGSize)size { | 93 - (void)setSize:(CGSize)size { |
95 dispatch_async(dispatch_get_main_queue(), ^{ | 94 dispatch_async(dispatch_get_main_queue(), ^{ |
96 [self.delegate videoView:self didChangeVideoSize:size]; | 95 [self.delegate videoView:self didChangeVideoSize:size]; |
97 }); | 96 }); |
98 } | 97 } |
99 | 98 |
100 - (void)renderFrame:(RTCVideoFrame *)frame { | 99 - (void)renderFrame:(RTCVideoFrame *)frame { |
101 self.videoFrame = frame; | 100 self.videoFrame = frame; |
102 } | 101 } |
103 | 102 |
104 #pragma mark - Private | 103 #pragma mark - Private |
105 | 104 |
106 - (void)drawFrame { | 105 - (void)drawFrame { |
107 RTCVideoFrame *videoFrame = self.videoFrame; | 106 RTCVideoFrame *frame = self.videoFrame; |
108 if (self.glRenderer.lastDrawnFrame != videoFrame) { | 107 if (!frame || frame == _lastDrawnFrame) { |
109 // This method may be called from CVDisplayLink callback which isn't on the | 108 return; |
110 // main thread so we have to lock the GL context before drawing. | |
111 CGLLockContext([[self openGLContext] CGLContextObj]); | |
112 [self.glRenderer drawFrame:videoFrame]; | |
113 CGLUnlockContext([[self openGLContext] CGLContextObj]); | |
114 } | 109 } |
110 // This method may be called from CVDisplayLink callback which isn't on the | |
111 // main thread so we have to lock the GL context before drawing. | |
112 NSOpenGLContext *context = [self openGLContext]; | |
113 CGLLockContext([context CGLContextObj]); | |
114 | |
115 [self ensureGLContext]; | |
116 glClear(GL_COLOR_BUFFER_BIT); | |
117 | |
118 // Rendering native CVPixelBuffer is not supported on OS X. | |
119 frame = [frame newI420VideoFrame]; | |
120 if (!_i420Shader) { | |
121 _i420Shader = [[RTCI420Shader alloc] initWithContext:context]; | |
122 NSAssert(_i420Shader, @"couldn't create i420 shader"); | |
123 } | |
124 if ([_i420Shader drawFrame:frame]) { | |
125 [context flushBuffer]; | |
126 _lastDrawnFrame = frame; | |
127 } else { | |
128 RTCLog(@"Failed to draw frame."); | |
129 } | |
130 CGLUnlockContext([context CGLContextObj]); | |
115 } | 131 } |
116 | 132 |
117 - (void)setupDisplayLink { | 133 - (void)setupDisplayLink { |
118 if (_displayLink) { | 134 if (_displayLink) { |
119 return; | 135 return; |
120 } | 136 } |
121 // Synchronize buffer swaps with vertical refresh rate. | 137 // Synchronize buffer swaps with vertical refresh rate. |
122 GLint swapInt = 1; | 138 GLint swapInt = 1; |
123 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; | 139 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
124 | 140 |
(...skipping 11 matching lines...) Expand all Loading... | |
136 } | 152 } |
137 | 153 |
138 - (void)teardownDisplayLink { | 154 - (void)teardownDisplayLink { |
139 if (!_displayLink) { | 155 if (!_displayLink) { |
140 return; | 156 return; |
141 } | 157 } |
142 CVDisplayLinkRelease(_displayLink); | 158 CVDisplayLinkRelease(_displayLink); |
143 _displayLink = NULL; | 159 _displayLink = NULL; |
144 } | 160 } |
145 | 161 |
162 - (void)ensureGLContext { | |
163 NSOpenGLContext* context = [self openGLContext]; | |
164 NSAssert(context, @"context shouldn't be nil"); | |
165 if ([NSOpenGLContext currentContext] != context) { | |
166 [context makeCurrentContext]; | |
167 } | |
168 } | |
169 | |
146 @end | 170 @end |
147 | 171 |
148 #endif // !TARGET_OS_IPHONE | 172 #endif // !TARGET_OS_IPHONE |
OLD | NEW |