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