Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Commit

Permalink
Trying to add support for remote and self views and I've got some
Browse files Browse the repository at this point in the history
funny GL problems. Not ready yet.
  • Loading branch information
gabriel committed Aug 29, 2011
1 parent 5c9750f commit cb87460
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 115 deletions.
Expand Up @@ -110,16 +110,15 @@ - (void)_refresh {
[_searchService search];
}

- (void)_camera {
KBKegTimeViewController *kegTimeViewController = [[KBKegTimeViewController alloc] init];
[kegTimeViewController enableCamera];
[self.navigationController pushViewController:kegTimeViewController animated:YES];
[kegTimeViewController release];
}

- (KBKegTimeViewController *)_kegTimeViewController {
KBKegTimeViewController *kegTimeViewController = [[KBKegTimeViewController alloc] init];
//kegTimeViewController.videoSize = CGSizeMake(540, 720);

FFAVCaptureService *captureService = [[KBApplication sharedDelegate] captureService];
if (captureService) {
[kegTimeViewController setSecondaryReader:captureService.videoCapture];
[captureService startCapture];
}

[self.navigationController pushViewController:kegTimeViewController animated:YES];
[kegTimeViewController release];
return kegTimeViewController;
Expand Down
10 changes: 5 additions & 5 deletions KegPad/Classes/Controllers/KegTime/KBKegTimeViewController.h
Expand Up @@ -26,17 +26,17 @@

@interface KBKegTimeViewController : UIViewController {
FFAVCaptureClient *videoServer_;
FFReaderView *videoView_;

FFAVCaptureSessionReader *videoCapture_;
FFReaderView *videoView_;
}

- (void)enableCamera;

- (void)close;

- (void)connectToService:(NSNetService *)service;

- (void)connectToHost:(KBKegTimeHost *)host;

- (void)setReader:(id<FFReader>)reader;

- (void)setSecondaryReader:(id<FFReader>)secondaryReader;

@end
14 changes: 3 additions & 11 deletions KegPad/Classes/Controllers/KegTime/KBKegTimeViewController.m
Expand Up @@ -55,9 +55,6 @@ - (void)viewDidDisappear:(BOOL)animated {

- (void)close {
self.title = @"KegTime";
[videoCapture_ close];
[videoCapture_ release];
videoCapture_ = nil;
[videoServer_ close];
[videoServer_ release];
videoServer_ = nil;
Expand All @@ -66,16 +63,11 @@ - (void)close {
- (void)setReader:(id<FFReader>)reader {
[self view];
[videoView_ setReader:reader];
[videoView_ startAnimation];
}

- (void)enableCamera {
[videoCapture_ close];
[videoCapture_ release];
videoCapture_ = nil;
videoCapture_ = [[FFAVCaptureSessionReader alloc] init];
[videoCapture_ start:nil];
[self setReader:videoCapture_];
- (void)setSecondaryReader:(id<FFReader>)secondaryReader {
[self view];
[videoView_ setSecondaryReader:secondaryReader];
}

- (void)connectToService:(NSNetService *)service {
Expand Down
1 change: 0 additions & 1 deletion KegPad/Classes/KBApplication.m
Expand Up @@ -49,7 +49,6 @@ + (KBTwitterShare *)twitterShare {

+ (KBDataStore *)dataStore {
return [[self sharedDelegate] dataStore];
//return [[self sharedDelegate] kegManager].dataStore;
}

@end
Expand Up @@ -26,6 +26,8 @@
dispatch_queue_t _queue;
}

- (BOOL)isStarted;

@end
#else
@interface FFAVCaptureSessionReader : NSObject <FFReader> {
Expand Down
Expand Up @@ -38,6 +38,10 @@ - (void)_setupVideoDevice:(AVCaptureDevice *)videoCaptureDevice {
[videoCaptureDevice unlockForConfiguration];
}

- (BOOL)isStarted {
return (!!_captureSession);
}

- (BOOL)start:(NSError **)error {
if (_captureSession) {
FFSetError(error, 0, @"Capture session already started");
Expand Down Expand Up @@ -117,8 +121,7 @@ - (void)close {
- (FFVFrameRef)nextFrame:(NSError **)error {
_wantsData = YES;
if (!_captureSession) {
//[self start:error];
NSAssert(NO, @"Call start: before requesting frame");
FFDebug(@"Next frame called with no capture session (forgot to start or stop?)");
return NULL;
}

Expand Down
5 changes: 3 additions & 2 deletions KegPad/Libraries/FFProcessing/FFGLDrawable.h
Expand Up @@ -21,13 +21,14 @@
FFGLImaging *_imaging;
FFGLImagingOptions _imagingOptions;

NSUInteger _frameCount;

CGRect _textureFrame; // If not set, defaults to the size of the view
}

@property (retain, nonatomic) id<FFFilter> filter;
@property (retain, nonatomic) id<FFReader> reader;

@property (assign, nonatomic) CGRect textureFrame;

- (id)initWithReader:(id<FFReader>)reader filter:(id<FFFilter>)filter;

- (void)setImagingOptions:(FFGLImagingOptions)imagingOptions;
Expand Down
37 changes: 16 additions & 21 deletions KegPad/Libraries/FFProcessing/FFGLDrawable.m
Expand Up @@ -12,11 +12,13 @@

@implementation FFGLDrawable

@synthesize filter=_filter, reader=_reader;
@synthesize filter=_filter, reader=_reader, textureFrame=_textureFrame;

- (id)init {
if ((self = [super init])) {
_GLFormat = GL_BGRA;
_imagingOptions = FFGLImagingOptionsMake(FFGLImagingNone, 0);
_textureFrame = CGRectZero;
}
return self;
}
Expand Down Expand Up @@ -63,7 +65,7 @@ - (BOOL)drawView:(GHGLView *)view {
//FFDebug(@"No frame from FFReader");
return NO;
}

if (_filter) {
frame = [_filter filterFrame:frame error:nil];
if (frame == NULL) return NO;
Expand All @@ -76,15 +78,6 @@ - (BOOL)drawView:(GHGLView *)view {
FFDebug(@"No data");
return NO;
}

// Testing JPEG encode/decode frames
/*
NSData *JPEGData = JPEGDataCreateFromFFVFrame(frame, 0.6);
FFDebug(@"JPEG data length: %d", [JPEGData length]);
for (int i = 0, size = FFVFormatGetSize(frame->format); i < size; i++) data[i] = 0;
FFConvertFromJPEGDataToBGRA(JPEGData, data);
[JPEGData release];
*/

//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Expand All @@ -97,6 +90,7 @@ - (BOOL)drawView:(GHGLView *)view {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Create texture with data or update existing texture data
if (!_textureLoaded) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, format.width, format.height, 0, _GLFormat, GL_UNSIGNED_BYTE, data);
_textureLoaded = YES;
Expand All @@ -112,25 +106,26 @@ - (BOOL)drawView:(GHGLView *)view {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GHGLCheckError();

CGRect texRect = CGRectMake(0, 0, view.frame.size.height, view.frame.size.width); // TODO(gabe): ??
// TODO(gabe): Height and width, x and y are flipped
CGRect textureFrame = CGRectMake(0, 0, view.frame.size.height, view.frame.size.width);
if (_textureFrame.size.width > 0 && _textureFrame.size.height > 0) {
textureFrame = CGRectMake(_textureFrame.origin.y, _textureFrame.origin.x, _textureFrame.size.height, _textureFrame.size.width);;
}

TexturedVertexData2D quad[4] = {
{{texRect.origin.y, texRect.origin.x}, {0, 1}},
{{texRect.origin.y, texRect.origin.x + texRect.size.width}, {1, 1}},
{{texRect.origin.y + texRect.size.height, texRect.origin.x}, {0, 0}},
{{texRect.origin.y + texRect.size.height, texRect.origin.x + texRect.size.width}, {1, 0}}
{{textureFrame.origin.y, textureFrame.origin.x}, {0, 1}},
{{textureFrame.origin.y, textureFrame.origin.x + textureFrame.size.width}, {1, 1}},
{{textureFrame.origin.y + textureFrame.size.height, textureFrame.origin.x}, {0, 0}},
{{textureFrame.origin.y + textureFrame.size.height, textureFrame.origin.x + textureFrame.size.width}, {1, 0}}
};

NSAssert(_imaging, @"No imaging");
[_imaging apply:quad options:_imagingOptions];

/*
[_imageEncoder GLReadPixels];
if (_frameCount == 30) {
[_imageEncoder writeToPhotosAlbum];
}
[_imageEncoder writeToPhotosAlbum];
*/

_frameCount++;
return YES;
}

Expand Down
1 change: 1 addition & 0 deletions KegPad/Libraries/FFProcessing/FFImaging/FFGLImaging.h
Expand Up @@ -9,6 +9,7 @@
#import "GHGLCommon.h"

typedef enum {
FFGLImagingNone,
FFGLImagingHue = 1 << 0,
FFGLImagingBrightness = 1 << 1,
FFGLImagingBlur = 1 << 2,
Expand Down
10 changes: 7 additions & 3 deletions KegPad/Libraries/FFProcessing/OpenGL/GHGLView.h
Expand Up @@ -32,22 +32,26 @@
BOOL _supportsBGRA8888;
BOOL _supportsNPOT;

id<GHGLViewDrawable> _drawable; // weak
NSMutableArray /*of id<GHGLViewDrawable>*/*_drawables;

}

@property (retain, nonatomic) id<GHGLViewDrawable> drawable;
@property (readonly, nonatomic) GLint backingWidth;
@property (readonly, nonatomic) GLint backingHeight;


- (void)startAnimation;
- (void)stopAnimation;
- (BOOL)isAnimating;
- (void)drawView;

- (void)setFrameInterval:(NSInteger)frameInterval;

- (NSArray *)drawables;
- (void)addDrawable:(id<GHGLViewDrawable>)drawable;
- (void)removeDrawable:(id<GHGLViewDrawable>)drawable;
- (void)removeDrawables;
- (void)setDrawable:(id<GHGLViewDrawable>)drawable;

@end


Expand Down
70 changes: 49 additions & 21 deletions KegPad/Libraries/FFProcessing/OpenGL/GHGLView.m
Expand Up @@ -17,7 +17,7 @@ - (void)_destroyFramebuffer;

@implementation GHGLView

@synthesize drawable=_drawable, backingWidth=_backingWidth, backingHeight=_backingHeight;
@synthesize backingWidth=_backingWidth, backingHeight=_backingHeight;
@synthesize context=_context; // Private properties

+ (Class)layerClass {
Expand Down Expand Up @@ -60,6 +60,8 @@ - (id)initWithFrame:(CGRect)frame {
GHGLDebug(@"GL_MAX_TEXTURE_SIZE: %d", _maxTextureSize);
GHGLDebug(@"Supports BGRA8888 textures: %d", _supportsBGRA8888);
GHGLDebug(@"Supports NPOT textures: %d", _supportsNPOT);

_drawables = [[NSMutableArray alloc] init];
}
return self;
}
Expand All @@ -69,38 +71,58 @@ - (void)dealloc {

if ([EAGLContext currentContext] == _context)
[EAGLContext setCurrentContext:nil];

[_context release];
[_drawable release];

[_displayLink invalidate];
[_context release];
[_drawables release];
[super dealloc];
}

- (void)drawView {
if (!_drawable) return;
if ([_drawables count] == 0) return;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
BOOL render = YES;
while (render) {
render = [_drawable drawView:self];
if (render) {
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
[_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
BOOL render = NO;
for (id<GHGLViewDrawable> drawable in _drawables) {
render |= [drawable drawView:self];
}
if (render) {
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _viewRenderbuffer);
[_context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
}

- (void)layoutSubviews {
if (!_drawable) return;
if ([_drawables count] == 0) return;
[EAGLContext setCurrentContext:_context];
[self _destroyFramebuffer];
[self _createFramebuffer];
[self drawView];
}

- (NSArray *)drawables {
return _drawables;
}

- (void)setDrawable:(id<GHGLViewDrawable>)drawable {
[drawable retain];
[_drawable release];
_drawable = drawable;
[self removeDrawables];
[self addDrawable:drawable];
}

- (void)removeDrawables {
for (id<GHGLViewDrawable> drawable in _drawables) {
[drawable stop];
}
[_drawables removeAllObjects];
}

- (void)removeDrawable:(id<GHGLViewDrawable>)drawable {
[drawable stop];
[_drawables removeObject:drawable];
}

- (void)addDrawable:(id<GHGLViewDrawable>)drawable {
[_drawables addObject:drawable];
if (_displayLink) [drawable start]; // Start if we are running
[self setNeedsLayout];
}

Expand Down Expand Up @@ -128,7 +150,9 @@ - (BOOL)_createFramebuffer {
return NO;
}

[_drawable setupView:self];
for (id<GHGLViewDrawable> drawable in _drawables) {
[drawable setupView:self];
}
return YES;
}

Expand All @@ -148,11 +172,13 @@ - (void)startAnimation {
if (!_displayLink) {
GHGLDebug(@"Start animation");
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
_displayLink.frameInterval = 3;
//_displayLink.frameInterval = 3;
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
GHGLDebug(@"Display link; frame interval %d", _displayLink.frameInterval);
if (!_drawable) GHGLError(@"No drawable set");
[_drawable start];
}
if ([_drawables count] == 0) GHGLError(@"No drawable set, can't start it");
for (id<GHGLViewDrawable> drawable in _drawables) {
[drawable start];
}
}

Expand All @@ -166,7 +192,9 @@ - (void)stopAnimation {
[_displayLink invalidate];
_displayLink = nil;
}
[_drawable stop];
for (id<GHGLViewDrawable> drawable in _drawables) {
[drawable stop];
}
}

- (void)setFrameInterval:(NSInteger)frameInterval {
Expand Down
3 changes: 2 additions & 1 deletion KegPad/Libraries/FFProcessing/OpenGL/GHGLViewController.m
Expand Up @@ -25,10 +25,11 @@ - (void)dealloc {
}

- (void)loadView {
[_GLView release];
_GLView = [[GHGLView alloc] init];
NSAssert(_GLView, @"No GL view");
_GLView.frame = CGRectMake(0, 0, 320, 480);
_GLView.drawable = _drawable;
[_GLView addDrawable:_drawable];
self.view = _GLView;
}

Expand Down

0 comments on commit cb87460

Please sign in to comment.