//
//  TestController.m
//  SequenceGrabberTest
//
//  Created by Tim Omernick on 3/18/05.
//  Copyright 2005 Tim Omernick. All rights reserved.
//

#import "TestController.h"

#import <CocoaSequenceGrabber/CocoaSequenceGrabber.h>

#import "MPT_RimageCamera.h"

@interface TestController(PrivateMethods)
- (void)setLastImage:(CSGImage *)value;
- (void)setLastRimage:(MPT_RimageAdaptor *)value;
- (void)setLastError:(NSError *)value;
@end

// Only define one of these at a time
//#define INSTANT_RATE
//#define AVG_RATE
#define PULL_RATE

static const int frameCount = 10;

@implementation TestController

// Init and dealloc
- (id)init
{
	if( self = [super init] )
	{
		// Start recording
		camera = [[MPT_RimageCamera alloc] init];
		[camera setDelegate:self];
		cameraStarted = NO;
		index = 0;
		for( int i=0; i<frameCount; ++i )
			rates[i] = 0.0;
		lastPulledTime = -1.0;
		[self setVideoWidth:320];
		[self setVideoHeight:240];
//		[camera startWithSize:NSMakeSize(640, 480)];		
	}
	return self;
}


- (void)dealloc;
{
	if( cameraStarted )
		[camera stop];
	[camera release];
	[lastImage release];
	[lastRimage release];
	[super dealloc];
}

- (CSGImage *)lastImage
{
#ifdef PULL_RATE
	NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
	if( lastPulledTime > -1.0 )
		rates[index++] = now - lastPulledTime;
	lastPulledTime = now;
	if( index >= frameCount ) index = 0;
#endif
    return [[lastImage retain] autorelease];
}

- (void)setLastImage:(CSGImage *)value
{
    if (lastImage != value)
	{
		if( lastImage )
		{
#ifdef INSTANT_RATE
			NSTimeInterval diff = [value sampleTime] - [lastImage sampleTime];
			rates[0] = 1.0/diff;
#elif AVG_RATE
			NSTimeInterval diff = [value sampleTime] - [lastImage sampleTime];
			rates[index++] = diff;
			if( index >= frameCount ) index = 0;
#endif
		}
		
        [lastImage release];
        lastImage = [value retain];
    }
}

- (void)setLastRimage:(MPT_RimageAdaptor *)value
{
    if( lastRimage != value )
	{		
        [lastRimage release];
        lastRimage = [value retain];
    }
}

- (MPT_RimageAdaptor *)lastRimage
{
	return lastRimage;
}

- (NSError *)lastError
{
    return lastError;
}

- (void)setLastError:(NSError *)value
{
    if( lastError != value )
    {
        [lastError release];
        lastError = [value retain];
    }
}

- (NSTimeInterval)frameRate
{
#ifdef INSTANT_RATE
	return rates[0];
#elifdef AVG_RATE
	NSTimeInterval rate = 0.0;
	for( int i=0; i<frameCount; ++i )
		rate += rates[i];
	return frameCount / rate;
#endif
#ifdef PULL_RATE
	NSTimeInterval rate = 0.0;
	for( int i=0; i<frameCount; ++i )
		rate += rates[i];
	return frameCount / rate;
#endif
}

- (float)videoWidth {
    return videoWidth;
}

- (void)setVideoWidth:(float)value {
    if (videoWidth != value) {
        videoWidth = value;
    }
}

- (float)videoHeight {
    return videoHeight;
}

- (void)setVideoHeight:(float)value {
    if (videoHeight != value) {
        videoHeight = value;
    }
}

- (BOOL)cameraStarted
{
    return cameraStarted;
}

- (void)start
{
	cameraStarted = [camera startWithSize:NSMakeSize( videoWidth, videoHeight )]; // Preference
}

- (void)stop
{
	if( cameraStarted )
		[camera stop];
	
	for( int i=0; i<frameCount; ++i )
		rates[i] = 0.0;
}

// MPT_RimageCamera delegate

- (void)camera:(CSGCamera *)aCamera didReceiveFrame:(CSGImage *)aFrame
{
	[self setLastImage:aFrame];
	[self setLastError:nil];
}

- (void)camera:(CSGCamera *)aCamera didReceiveRImage:(MPT_RimageAdaptor *)aFrame
{
	[self setLastRimage:aFrame];
	[self setLastError:nil];
}

- (void)camera:(CSGCamera *)aCamera didReceiveError:(OSErr)err
{
	[self setLastError:[NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:[NSDictionary dictionaryWithObject:[NSCalendarDate date] forKey:@"datetime"]]];
}

// NSWindow delegate

- (void)windowWillClose:(NSNotification *)notification;
{
	[camera stop];
}

@end
