| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2013 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 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 12 #error "This file requires ARC support." | |
| 13 #endif | |
| 14 | |
| 15 #include <memory> | |
| 16 | |
| 17 #include "webrtc/modules/video_render/ios/video_render_ios_view.h" | |
| 18 #include "webrtc/system_wrappers/include/trace.h" | |
| 19 | |
| 20 using namespace webrtc; | |
| 21 | |
| 22 @implementation VideoRenderIosView { | |
| 23 EAGLContext* _context; | |
| 24 std::unique_ptr<webrtc::OpenGles20> _gles_renderer20; | |
| 25 int _frameBufferWidth; | |
| 26 int _frameBufferHeight; | |
| 27 unsigned int _defaultFrameBuffer; | |
| 28 unsigned int _colorRenderBuffer; | |
| 29 } | |
| 30 | |
| 31 @synthesize context = context_; | |
| 32 | |
| 33 + (Class)layerClass { | |
| 34 return [CAEAGLLayer class]; | |
| 35 } | |
| 36 | |
| 37 - (id)initWithCoder:(NSCoder*)coder { | |
| 38 // init super class | |
| 39 self = [super initWithCoder:coder]; | |
| 40 if (self) { | |
| 41 _gles_renderer20.reset(new OpenGles20()); | |
| 42 } | |
| 43 return self; | |
| 44 } | |
| 45 | |
| 46 - (id)init { | |
| 47 // init super class | |
| 48 self = [super init]; | |
| 49 if (self) { | |
| 50 _gles_renderer20.reset(new OpenGles20()); | |
| 51 } | |
| 52 return self; | |
| 53 } | |
| 54 | |
| 55 - (id)initWithFrame:(CGRect)frame { | |
| 56 // init super class | |
| 57 self = [super initWithFrame:frame]; | |
| 58 if (self) { | |
| 59 _gles_renderer20.reset(new OpenGles20()); | |
| 60 } | |
| 61 return self; | |
| 62 } | |
| 63 | |
| 64 - (void)dealloc { | |
| 65 if (_defaultFrameBuffer) { | |
| 66 glDeleteFramebuffers(1, &_defaultFrameBuffer); | |
| 67 _defaultFrameBuffer = 0; | |
| 68 } | |
| 69 | |
| 70 if (_colorRenderBuffer) { | |
| 71 glDeleteRenderbuffers(1, &_colorRenderBuffer); | |
| 72 _colorRenderBuffer = 0; | |
| 73 } | |
| 74 | |
| 75 [EAGLContext setCurrentContext:nil]; | |
| 76 } | |
| 77 | |
| 78 - (NSString*)description { | |
| 79 return [NSString stringWithFormat: | |
| 80 @"A WebRTC implemented subclass of UIView." | |
| 81 "+Class method is overwritten, along with custom methods"]; | |
| 82 } | |
| 83 | |
| 84 - (BOOL)createContext { | |
| 85 // create OpenGLES context from self layer class | |
| 86 CAEAGLLayer* eagl_layer = (CAEAGLLayer*)self.layer; | |
| 87 eagl_layer.opaque = YES; | |
| 88 eagl_layer.drawableProperties = | |
| 89 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], | |
| 90 kEAGLDrawablePropertyRetainedBacking, | |
| 91 kEAGLColorFormatRGBA8, | |
| 92 kEAGLDrawablePropertyColorFormat, | |
| 93 nil]; | |
| 94 _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; | |
| 95 | |
| 96 if (!_context) { | |
| 97 return NO; | |
| 98 } | |
| 99 | |
| 100 if (![EAGLContext setCurrentContext:_context]) { | |
| 101 return NO; | |
| 102 } | |
| 103 | |
| 104 // generates and binds the OpenGLES buffers | |
| 105 glGenFramebuffers(1, &_defaultFrameBuffer); | |
| 106 glBindFramebuffer(GL_FRAMEBUFFER, _defaultFrameBuffer); | |
| 107 | |
| 108 // Create color render buffer and allocate backing store. | |
| 109 glGenRenderbuffers(1, &_colorRenderBuffer); | |
| 110 glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer); | |
| 111 [_context renderbufferStorage:GL_RENDERBUFFER | |
| 112 fromDrawable:(CAEAGLLayer*)self.layer]; | |
| 113 glGetRenderbufferParameteriv( | |
| 114 GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &_frameBufferWidth); | |
| 115 glGetRenderbufferParameteriv( | |
| 116 GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &_frameBufferHeight); | |
| 117 glFramebufferRenderbuffer(GL_FRAMEBUFFER, | |
| 118 GL_COLOR_ATTACHMENT0, | |
| 119 GL_RENDERBUFFER, | |
| 120 _colorRenderBuffer); | |
| 121 | |
| 122 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { | |
| 123 return NO; | |
| 124 } | |
| 125 | |
| 126 // set the frame buffer | |
| 127 glBindFramebuffer(GL_FRAMEBUFFER, _defaultFrameBuffer); | |
| 128 glViewport(0, 0, self.frame.size.width, self.frame.size.height); | |
| 129 | |
| 130 return _gles_renderer20->Setup([self bounds].size.width, | |
| 131 [self bounds].size.height); | |
| 132 } | |
| 133 | |
| 134 - (BOOL)presentFramebuffer { | |
| 135 if (![_context presentRenderbuffer:GL_RENDERBUFFER]) { | |
| 136 WEBRTC_TRACE(kTraceWarning, | |
| 137 kTraceVideoRenderer, | |
| 138 0, | |
| 139 "%s:%d [context present_renderbuffer] " | |
| 140 "returned false", | |
| 141 __FUNCTION__, | |
| 142 __LINE__); | |
| 143 } | |
| 144 return YES; | |
| 145 } | |
| 146 | |
| 147 - (BOOL)renderFrame:(VideoFrame*)frameToRender { | |
| 148 if (![EAGLContext setCurrentContext:_context]) { | |
| 149 return NO; | |
| 150 } | |
| 151 | |
| 152 return _gles_renderer20->Render(*frameToRender); | |
| 153 } | |
| 154 | |
| 155 - (BOOL)setCoordinatesForZOrder:(const float)zOrder | |
| 156 Left:(const float)left | |
| 157 Top:(const float)top | |
| 158 Right:(const float)right | |
| 159 Bottom:(const float)bottom { | |
| 160 return _gles_renderer20->SetCoordinates(zOrder, left, top, right, bottom); | |
| 161 } | |
| 162 | |
| 163 @end | |
| OLD | NEW |