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 <AppKit/NSOpenGL.h> |
18 #import <CoreVideo/CVDisplayLink.h> | 18 #import <CoreVideo/CVDisplayLink.h> |
19 #import <OpenGL/gl3.h> | 19 #import <OpenGL/gl3.h> |
20 | 20 |
21 #import "RTCShader+Private.h" | 21 #import "RTCI420TextureCache.h" |
22 #import "RTCDefaultShaderDelegate.h" | |
22 #import "WebRTC/RTCLogging.h" | 23 #import "WebRTC/RTCLogging.h" |
23 #import "WebRTC/RTCVideoFrame.h" | 24 #import "WebRTC/RTCVideoFrame.h" |
24 | 25 |
25 @interface RTCNSGLVideoView () | 26 @interface RTCNSGLVideoView () |
26 // |videoFrame| is set when we receive a frame from a worker thread and is read | 27 // |videoFrame| is set when we receive a frame from a worker thread and is read |
27 // from the display link callback so atomicity is required. | 28 // from the display link callback so atomicity is required. |
28 @property(atomic, strong) RTCVideoFrame *videoFrame; | 29 @property(atomic, strong) RTCVideoFrame *videoFrame; |
29 @property(atomic, strong) id<RTCShader> i420Shader; | 30 @property(atomic, strong) RTCI420TextureCache *i420TextureCache; |
30 | 31 |
31 - (void)drawFrame; | 32 - (void)drawFrame; |
32 @end | 33 @end |
33 | 34 |
34 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, | 35 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
35 const CVTimeStamp *now, | 36 const CVTimeStamp *now, |
36 const CVTimeStamp *outputTime, | 37 const CVTimeStamp *outputTime, |
37 CVOptionFlags flagsIn, | 38 CVOptionFlags flagsIn, |
38 CVOptionFlags *flagsOut, | 39 CVOptionFlags *flagsOut, |
39 void *displayLinkContext) { | 40 void *displayLinkContext) { |
40 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; | 41 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; |
41 [view drawFrame]; | 42 [view drawFrame]; |
42 return kCVReturnSuccess; | 43 return kCVReturnSuccess; |
43 } | 44 } |
44 | 45 |
45 @implementation RTCNSGLVideoView { | 46 @implementation RTCNSGLVideoView { |
46 CVDisplayLinkRef _displayLink; | 47 CVDisplayLinkRef _displayLink; |
47 RTCVideoFrame *_lastDrawnFrame; | 48 RTCVideoFrame *_lastDrawnFrame; |
49 RTCDefaultShaderDelegate *_defaultShaderDelegate; | |
50 __weak id<RTCVideoViewShaderDelegate> _shaderDelegate; | |
48 } | 51 } |
49 | 52 |
50 @synthesize delegate = _delegate; | 53 @synthesize delegate = _delegate; |
51 @synthesize videoFrame = _videoFrame; | 54 @synthesize videoFrame = _videoFrame; |
52 @synthesize i420Shader = _i420Shader; | 55 @synthesize i420TextureCache = _i420TextureCache; |
56 | |
57 - (instancetype)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)f ormat { | |
58 _defaultShaderDelegate = [[RTCDefaultShaderDelegate alloc] init]; | |
59 return [self initWithFrame:frame | |
60 pixelFormat:format | |
61 shaderDelegate:_defaultShaderDelegate]; | |
62 } | |
63 | |
64 - (instancetype)initWithFrame:(NSRect)frame | |
65 pixelFormat:(NSOpenGLPixelFormat *)format | |
66 shaderDelegate:(id<RTCVideoViewShaderDelegate>)shaderDelegate { | |
67 if (self = [super initWithFrame:frame pixelFormat:format]) { | |
68 _shaderDelegate = shaderDelegate; | |
69 } | |
70 return self; | |
71 } | |
53 | 72 |
54 - (void)dealloc { | 73 - (void)dealloc { |
55 [self teardownDisplayLink]; | 74 [self teardownDisplayLink]; |
56 } | 75 } |
57 | 76 |
58 - (void)drawRect:(NSRect)rect { | 77 - (void)drawRect:(NSRect)rect { |
59 [self drawFrame]; | 78 [self drawFrame]; |
60 } | 79 } |
61 | 80 |
62 - (void)reshape { | 81 - (void)reshape { |
(...skipping 15 matching lines...) Expand all Loading... | |
78 | 97 |
79 - (void)prepareOpenGL { | 98 - (void)prepareOpenGL { |
80 [super prepareOpenGL]; | 99 [super prepareOpenGL]; |
81 [self ensureGLContext]; | 100 [self ensureGLContext]; |
82 glDisable(GL_DITHER); | 101 glDisable(GL_DITHER); |
83 [self setupDisplayLink]; | 102 [self setupDisplayLink]; |
84 } | 103 } |
85 | 104 |
86 - (void)clearGLContext { | 105 - (void)clearGLContext { |
87 [self ensureGLContext]; | 106 [self ensureGLContext]; |
88 self.i420Shader = nil; | 107 self.i420TextureCache = nil; |
89 [super clearGLContext]; | 108 [super clearGLContext]; |
90 } | 109 } |
91 | 110 |
92 #pragma mark - RTCVideoRenderer | 111 #pragma mark - RTCVideoRenderer |
93 | 112 |
94 // These methods may be called on non-main thread. | 113 // These methods may be called on non-main thread. |
95 - (void)setSize:(CGSize)size { | 114 - (void)setSize:(CGSize)size { |
96 dispatch_async(dispatch_get_main_queue(), ^{ | 115 dispatch_async(dispatch_get_main_queue(), ^{ |
97 [self.delegate videoView:self didChangeVideoSize:size]; | 116 [self.delegate videoView:self didChangeVideoSize:size]; |
98 }); | 117 }); |
(...skipping 12 matching lines...) Expand all Loading... | |
111 } | 130 } |
112 // This method may be called from CVDisplayLink callback which isn't on the | 131 // 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. | 132 // main thread so we have to lock the GL context before drawing. |
114 NSOpenGLContext *context = [self openGLContext]; | 133 NSOpenGLContext *context = [self openGLContext]; |
115 CGLLockContext([context CGLContextObj]); | 134 CGLLockContext([context CGLContextObj]); |
116 | 135 |
117 [self ensureGLContext]; | 136 [self ensureGLContext]; |
118 glClear(GL_COLOR_BUFFER_BIT); | 137 glClear(GL_COLOR_BUFFER_BIT); |
119 | 138 |
120 // Rendering native CVPixelBuffer is not supported on OS X. | 139 // Rendering native CVPixelBuffer is not supported on OS X. |
140 // TODO(magjed): Add support for NV12 texture cache on OS X. | |
121 frame = [frame newI420VideoFrame]; | 141 frame = [frame newI420VideoFrame]; |
122 if (!self.i420Shader) { | 142 if (!self.i420TextureCache) { |
123 self.i420Shader = [[RTCI420Shader alloc] initWithContext:context]; | 143 self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context ]; |
124 } | 144 } |
125 if (self.i420Shader && [self.i420Shader drawFrame:frame]) { | 145 RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
146 if (i420TextureCache) { | |
147 [i420TextureCache uploadFrameToTextures:frame]; | |
148 [_shaderDelegate videoView:self | |
daniela-webrtc
2017/05/11 17:31:20
What if this is nil? Shouldn't the defaultDelegate
| |
149 didReceiveFrameWithRotation:frame.rotation | |
150 yPlane:i420TextureCache.yTexture | |
151 uPlane:i420TextureCache.uTexture | |
152 vPlane:i420TextureCache.vTexture]; | |
126 [context flushBuffer]; | 153 [context flushBuffer]; |
127 _lastDrawnFrame = frame; | 154 _lastDrawnFrame = frame; |
128 } else { | |
129 RTCLog(@"Failed to draw frame."); | |
130 } | 155 } |
131 CGLUnlockContext([context CGLContextObj]); | 156 CGLUnlockContext([context CGLContextObj]); |
132 } | 157 } |
133 | 158 |
134 - (void)setupDisplayLink { | 159 - (void)setupDisplayLink { |
135 if (_displayLink) { | 160 if (_displayLink) { |
136 return; | 161 return; |
137 } | 162 } |
138 // Synchronize buffer swaps with vertical refresh rate. | 163 // Synchronize buffer swaps with vertical refresh rate. |
139 GLint swapInt = 1; | 164 GLint swapInt = 1; |
(...skipping 24 matching lines...) Expand all Loading... | |
164 NSOpenGLContext* context = [self openGLContext]; | 189 NSOpenGLContext* context = [self openGLContext]; |
165 NSAssert(context, @"context shouldn't be nil"); | 190 NSAssert(context, @"context shouldn't be nil"); |
166 if ([NSOpenGLContext currentContext] != context) { | 191 if ([NSOpenGLContext currentContext] != context) { |
167 [context makeCurrentContext]; | 192 [context makeCurrentContext]; |
168 } | 193 } |
169 } | 194 } |
170 | 195 |
171 @end | 196 @end |
172 | 197 |
173 #endif // !TARGET_OS_IPHONE | 198 #endif // !TARGET_OS_IPHONE |
OLD | NEW |