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 "RTCNV12TextureCache.h" | |
12 | |
13 #import "WebRTC/RTCVideoFrame.h" | |
14 | |
15 @implementation RTCNV12TextureCache { | |
16 CVOpenGLESTextureCacheRef _textureCache; | |
17 CVOpenGLESTextureRef _yTextureRef; | |
18 CVOpenGLESTextureRef _uvTextureRef; | |
19 } | |
20 | |
21 - (GLuint)yTexture { | |
22 return CVOpenGLESTextureGetName(_yTextureRef); | |
23 } | |
24 | |
25 - (GLuint)uvTexture { | |
26 return CVOpenGLESTextureGetName(_uvTextureRef); | |
27 } | |
28 | |
29 - (instancetype)initWithContext:(EAGLContext *)context { | |
30 if (self = [super init]) { | |
31 CVReturn ret = CVOpenGLESTextureCacheCreate( | |
32 kCFAllocatorDefault, NULL, | |
33 #if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API | |
34 context, | |
35 #else | |
36 (__bridge void *)context, | |
37 #endif | |
38 NULL, &_textureCache); | |
39 if (ret != kCVReturnSuccess) { | |
40 self = nil; | |
41 } | |
42 } | |
43 return self; | |
44 } | |
45 | |
46 - (BOOL)loadTexture:(CVOpenGLESTextureRef *)textureOut | |
47 pixelBuffer:(CVPixelBufferRef)pixelBuffer | |
48 planeIndex:(int)planeIndex | |
49 pixelFormat:(GLenum)pixelFormat { | |
50 const int width = CVPixelBufferGetWidthOfPlane(pixelBuffer, planeIndex); | |
51 const int height = CVPixelBufferGetHeightOfPlane(pixelBuffer, planeIndex); | |
52 | |
53 *textureOut = nil; | |
sakal
2017/04/26 13:31:56
I think we should call CFRelease if textureOut is
magjed_webrtc
2017/04/26 14:40:41
Looks like it. Done.
| |
54 CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage( | |
55 kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, pixe lFormat, width, | |
56 height, pixelFormat, GL_UNSIGNED_BYTE, planeIndex, textureOut); | |
57 if (ret != kCVReturnSuccess) { | |
58 CFRelease(*textureOut); | |
59 *textureOut = nil; | |
60 return NO; | |
61 } | |
62 NSAssert(CVOpenGLESTextureGetTarget(*textureOut) == GL_TEXTURE_2D, | |
63 @"Unexpected GLES texture target"); | |
64 glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(*textureOut)); | |
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
69 return YES; | |
70 } | |
71 | |
72 - (BOOL)uploadFrameToTextures:(RTCVideoFrame *)frame { | |
73 CVPixelBufferRef pixelBuffer = frame.nativeHandle; | |
74 NSParameterAssert(pixelBuffer); | |
75 return [self loadTexture:&_yTextureRef | |
76 pixelBuffer:pixelBuffer | |
77 planeIndex:0 | |
78 pixelFormat:GL_LUMINANCE] && | |
79 [self loadTexture:&_uvTextureRef | |
80 pixelBuffer:pixelBuffer | |
81 planeIndex:1 | |
82 pixelFormat:GL_LUMINANCE_ALPHA]; | |
83 } | |
84 | |
85 - (void)releaseTextures { | |
86 if (_uvTextureRef) { | |
87 CFRelease(_uvTextureRef); | |
88 _uvTextureRef = nil; | |
89 } | |
90 if (_yTextureRef) { | |
91 CFRelease(_yTextureRef); | |
92 _yTextureRef = nil; | |
93 } | |
94 } | |
95 | |
96 - (void)dealloc { | |
97 [self releaseTextures]; | |
98 if (_textureCache) { | |
99 CFRelease(_textureCache); | |
100 _textureCache = nil; | |
101 } | |
102 } | |
103 | |
104 @end | |
105 | |
OLD | NEW |