OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * Copyright 2014 Google Inc. | |
4 * | 3 * |
5 * Redistribution and use in source and binary forms, with or without | 4 * Use of this source code is governed by a BSD-style license |
6 * modification, are permitted provided that the following conditions are met: | 5 * that can be found in the LICENSE file in the root of the source |
7 * | 6 * tree. An additional intellectual property rights grant can be found |
8 * 1. Redistributions of source code must retain the above copyright notice, | 7 * in the file PATENTS. All contributing project authors may |
9 * this list of conditions and the following disclaimer. | 8 * be found in the AUTHORS file in the root of the source tree. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | 9 */ |
27 | 10 |
28 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
29 #error "This file requires ARC support." | |
30 #endif | |
31 | |
32 #import "RTCNSGLVideoView.h" | 11 #import "RTCNSGLVideoView.h" |
33 | 12 |
34 #import <CoreVideo/CVDisplayLink.h> | 13 #import <CoreVideo/CVDisplayLink.h> |
35 #import <OpenGL/gl3.h> | 14 #import <OpenGL/gl3.h> |
36 #import "RTCI420Frame.h" | 15 #import "RTCVideoFrame.h" |
37 #import "RTCOpenGLVideoRenderer.h" | 16 #import "RTCOpenGLVideoRenderer.h" |
38 | 17 |
39 @interface RTCNSGLVideoView () | 18 @interface RTCNSGLVideoView () |
40 // |i420Frame| is set when we receive a frame from a worker thread and is read | 19 // |videoFrame| is set when we receive a frame from a worker thread and is read |
41 // from the display link callback so atomicity is required. | 20 // from the display link callback so atomicity is required. |
42 @property(atomic, strong) RTCI420Frame* i420Frame; | 21 @property(atomic, strong) RTCVideoFrame *videoFrame; |
43 @property(atomic, strong) RTCOpenGLVideoRenderer* glRenderer; | 22 @property(atomic, strong) RTCOpenGLVideoRenderer *glRenderer; |
44 - (void)drawFrame; | 23 - (void)drawFrame; |
45 @end | 24 @end |
46 | 25 |
47 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, | 26 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
48 const CVTimeStamp* now, | 27 const CVTimeStamp *now, |
49 const CVTimeStamp* outputTime, | 28 const CVTimeStamp *outputTime, |
50 CVOptionFlags flagsIn, | 29 CVOptionFlags flagsIn, |
51 CVOptionFlags* flagsOut, | 30 CVOptionFlags *flagsOut, |
52 void* displayLinkContext) { | 31 void *displayLinkContext) { |
53 RTCNSGLVideoView* view = (__bridge RTCNSGLVideoView*)displayLinkContext; | 32 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; |
54 [view drawFrame]; | 33 [view drawFrame]; |
55 return kCVReturnSuccess; | 34 return kCVReturnSuccess; |
56 } | 35 } |
57 | 36 |
58 @implementation RTCNSGLVideoView { | 37 @implementation RTCNSGLVideoView { |
59 CVDisplayLinkRef _displayLink; | 38 CVDisplayLinkRef _displayLink; |
60 } | 39 } |
61 | 40 |
| 41 @synthesize delegate = _delegate; |
| 42 @synthesize videoFrame = _videoFrame; |
| 43 @synthesize glRenderer = _glRenderer; |
| 44 |
62 - (void)dealloc { | 45 - (void)dealloc { |
63 [self teardownDisplayLink]; | 46 [self teardownDisplayLink]; |
64 } | 47 } |
65 | 48 |
66 - (void)drawRect:(NSRect)rect { | 49 - (void)drawRect:(NSRect)rect { |
67 [self drawFrame]; | 50 [self drawFrame]; |
68 } | 51 } |
69 | 52 |
70 - (void)reshape { | 53 - (void)reshape { |
71 [super reshape]; | 54 [super reshape]; |
72 NSRect frame = [self frame]; | 55 NSRect frame = [self frame]; |
73 CGLLockContext([[self openGLContext] CGLContextObj]); | 56 CGLLockContext([[self openGLContext] CGLContextObj]); |
74 glViewport(0, 0, frame.size.width, frame.size.height); | 57 glViewport(0, 0, frame.size.width, frame.size.height); |
75 CGLUnlockContext([[self openGLContext] CGLContextObj]); | 58 CGLUnlockContext([[self openGLContext] CGLContextObj]); |
76 } | 59 } |
77 | 60 |
78 - (void)lockFocus { | 61 - (void)lockFocus { |
79 NSOpenGLContext* context = [self openGLContext]; | 62 NSOpenGLContext *context = [self openGLContext]; |
80 [super lockFocus]; | 63 [super lockFocus]; |
81 if ([context view] != self) { | 64 if ([context view] != self) { |
82 [context setView:self]; | 65 [context setView:self]; |
83 } | 66 } |
84 [context makeCurrentContext]; | 67 [context makeCurrentContext]; |
85 } | 68 } |
86 | 69 |
87 - (void)prepareOpenGL { | 70 - (void)prepareOpenGL { |
88 [super prepareOpenGL]; | 71 [super prepareOpenGL]; |
89 if (!self.glRenderer) { | 72 if (!self.glRenderer) { |
(...skipping 12 matching lines...) Expand all Loading... |
102 | 85 |
103 #pragma mark - RTCVideoRenderer | 86 #pragma mark - RTCVideoRenderer |
104 | 87 |
105 // These methods may be called on non-main thread. | 88 // These methods may be called on non-main thread. |
106 - (void)setSize:(CGSize)size { | 89 - (void)setSize:(CGSize)size { |
107 dispatch_async(dispatch_get_main_queue(), ^{ | 90 dispatch_async(dispatch_get_main_queue(), ^{ |
108 [self.delegate videoView:self didChangeVideoSize:size]; | 91 [self.delegate videoView:self didChangeVideoSize:size]; |
109 }); | 92 }); |
110 } | 93 } |
111 | 94 |
112 - (void)renderFrame:(RTCI420Frame*)frame { | 95 - (void)renderFrame:(RTCVideoFrame *)frame { |
113 self.i420Frame = frame; | 96 self.videoFrame = frame; |
114 } | 97 } |
115 | 98 |
116 #pragma mark - Private | 99 #pragma mark - Private |
117 | 100 |
118 - (void)drawFrame { | 101 - (void)drawFrame { |
119 RTCI420Frame* i420Frame = self.i420Frame; | 102 RTCVideoFrame *videoFrame = self.videoFrame; |
120 if (self.glRenderer.lastDrawnFrame != i420Frame) { | 103 if (self.glRenderer.lastDrawnFrame != videoFrame) { |
121 // This method may be called from CVDisplayLink callback which isn't on the | 104 // This method may be called from CVDisplayLink callback which isn't on the |
122 // main thread so we have to lock the GL context before drawing. | 105 // main thread so we have to lock the GL context before drawing. |
123 CGLLockContext([[self openGLContext] CGLContextObj]); | 106 CGLLockContext([[self openGLContext] CGLContextObj]); |
124 [self.glRenderer drawFrame:i420Frame]; | 107 [self.glRenderer drawFrame:videoFrame]; |
125 CGLUnlockContext([[self openGLContext] CGLContextObj]); | 108 CGLUnlockContext([[self openGLContext] CGLContextObj]); |
126 } | 109 } |
127 } | 110 } |
128 | 111 |
129 - (void)setupDisplayLink { | 112 - (void)setupDisplayLink { |
130 if (_displayLink) { | 113 if (_displayLink) { |
131 return; | 114 return; |
132 } | 115 } |
133 // Synchronize buffer swaps with vertical refresh rate. | 116 // Synchronize buffer swaps with vertical refresh rate. |
134 GLint swapInt = 1; | 117 GLint swapInt = 1; |
135 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; | 118 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
136 | 119 |
137 // Create display link. | 120 // Create display link. |
138 CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); | 121 CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); |
139 CVDisplayLinkSetOutputCallback(_displayLink, | 122 CVDisplayLinkSetOutputCallback(_displayLink, |
140 &OnDisplayLinkFired, | 123 &OnDisplayLinkFired, |
141 (__bridge void*)self); | 124 (__bridge void *)self); |
142 // Set the display link for the current renderer. | 125 // Set the display link for the current renderer. |
143 CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; | 126 CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; |
144 CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; | 127 CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; |
145 CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext( | 128 CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext( |
146 _displayLink, cglContext, cglPixelFormat); | 129 _displayLink, cglContext, cglPixelFormat); |
147 CVDisplayLinkStart(_displayLink); | 130 CVDisplayLinkStart(_displayLink); |
148 } | 131 } |
149 | 132 |
150 - (void)teardownDisplayLink { | 133 - (void)teardownDisplayLink { |
151 if (!_displayLink) { | 134 if (!_displayLink) { |
152 return; | 135 return; |
153 } | 136 } |
154 CVDisplayLinkRelease(_displayLink); | 137 CVDisplayLinkRelease(_displayLink); |
155 _displayLink = NULL; | 138 _displayLink = NULL; |
156 } | 139 } |
157 | 140 |
158 @end | 141 @end |
OLD | NEW |