Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera Renderer Memory Cleanup #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions src/ios/CameraRenderController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ @implementation CameraRenderController
@synthesize context = _context;

- (CameraRenderController *)init {
if (self = [super init])
if (self = [super init]) {
self.renderLock = [[NSLock alloc] init];
}
return self;
}

Expand All @@ -21,9 +22,10 @@ - (void)loadView {

- (void)viewDidLoad {
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context)
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
if (!self.context) {
NSLog(@"Failed to create ES context");
}

CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, self.context, NULL, &_videoTextureCache);
if (err) {
Expand All @@ -33,15 +35,15 @@ - (void)viewDidLoad {

GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableDepthFormat = GLKViewDrawableDepthFormatNone;
view.contentMode = UIViewContentModeScaleToFill;

glGenRenderbuffers(1, &_renderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
self.ciContext = [CIContext contextWithEAGLContext:self.context];
}

- (void) viewWillAppear:(BOOL)animated {
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appplicationIsActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
Expand All @@ -56,13 +58,20 @@ - (void) viewWillAppear:(BOOL)animated {
});
}

- (void) viewWillDisappear:(BOOL)animated {
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];

dispatch_async(self.sessionManager.sessionQueue, ^{
if (self.sessionManager.session.running) {
NSLog(@"Stopping session from viewWillDisappear");
[self.sessionManager.session stopRunning];
}
});
}

- (void) appplicationIsActive:(NSNotification *)notification {
- (void)applicationIsActive:(NSNotification *)notification {
dispatch_async(self.sessionManager.sessionQueue, ^{
if (!self.sessionManager.session.running){
NSLog(@"Starting session");
Expand All @@ -71,14 +80,15 @@ - (void) appplicationIsActive:(NSNotification *)notification {
});
}

- (void) applicationEnteredForeground:(NSNotification *)notification {
- (void)applicationEnteredForeground:(NSNotification *)notification {
// Uncomment if needed
// dispatch_async(self.sessionManager.sessionQueue, ^{
// NSLog(@"Stopping session");
// [self.sessionManager.session stopRunning];
// });
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if ([self.renderLock tryLock]) {
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
Expand Down Expand Up @@ -153,21 +163,32 @@ - (void)viewDidDisappear:(BOOL)animated {
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
[self cleanUpTextures];
self.context = nil;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self cleanUpTextures];

if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;

if (_videoTextureCache) {
CFRelease(_videoTextureCache);
_videoTextureCache = NULL;
}

self.sessionManager = nil;
}

- (BOOL)shouldAutorotate {
return YES;
}

-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
__block UIInterfaceOrientation toInterfaceOrientation;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
Expand All @@ -178,4 +199,14 @@ -(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVi
}];
}

- (void)cleanUpTextures {
if (_renderBuffer) {
glDeleteRenderbuffers(1, &_renderBuffer);
_renderBuffer = 0;
}
if (_videoTextureCache) {
CVOpenGLESTextureCacheFlush(_videoTextureCache, 0);
}
}

@end
Loading