Source

kinect-viewer / KVDepthPlayer.m

Full commit
/*
 * Copyright 2010 Cliff L. Biffle.  All Rights Reserved.
 * Use of this source code is governed by the Apache License 2.0,
 * which can be found in the LICENSE file.
 */

#import "KVDepthPlayer.h"

@interface KVDepthPlayer ()

@property NSFileHandle *fileHandle;
@property (assign) NSData *latestDepthSamples;
@property BOOL running;

- (void) replayThread;

@end


@implementation KVDepthPlayer

@synthesize fileHandle;
@synthesize latestDepthSamples;
@synthesize running;

- (void) open: (NSString *) path {
  fileHandle = [NSFileHandle fileHandleForReadingAtPath: path];
  [NSThread detachNewThreadSelector: @selector(replayThread) toTarget: self withObject: nil];
}

- (void) close {
  self.running = NO;
  NSFileHandle *h = fileHandle;
  self.fileHandle = nil;
  [h closeFile];
}

- (void) replayThread {
  NSUInteger frameSize = (640 * 480 * sizeof(uint16_t));
  while (YES) {  // Repeat file
    [fileHandle seekToFileOffset: 0];
    while (YES) {  // Play each frame
      NSData *frame = [fileHandle readDataOfLength: frameSize];
      if ([frame length] < frameSize) break;
      
      self.latestDepthSamples = frame;
      usleep(1000000 / 30);
    }
  }
}

@end