Commits

Cliff Biffle committed d26d9f0

Factored depth playback and recording into separate objects, and simplified the app delegate.

Comments (0)

Files changed (6)

+/*
+ * 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 <Cocoa/Cocoa.h>
+
+
+@interface KVDepthPlayer : NSObject {
+
+}
+
+- (void) open: (NSString *) path;
+
+- (void) close;
+
+@property (readonly) NSData *latestDepthSamples;
+
+@end
+/*
+ * 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

KVDepthRecorder.h

+/*
+ * 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 <Cocoa/Cocoa.h>
+
+
+@interface KVDepthRecorder : NSObject {
+}
+
+- (void) open: (NSString *) path;
+
+- (void) close;
+
+@property (assign) NSData *latestDepthSamples;
+
+@end

KVDepthRecorder.m

+/*
+ * 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 "KVDepthRecorder.h"
+
+@interface KVDepthRecorder ()
+
+@property NSFileHandle *fileHandle;
+
+@end
+
+
+@implementation KVDepthRecorder
+@synthesize fileHandle;
+
+- (void) open: (NSString *) path {
+  fileHandle = [NSFileHandle fileHandleForWritingAtPath: path];
+}
+
+- (void) close {
+  NSFileHandle *h = fileHandle;
+  fileHandle = nil;
+  [h closeFile];
+}
+
+- (void) setLatestDepthSamples: (NSData *) data {
+  [fileHandle writeData: data];
+  [fileHandle synchronizeFile];
+}
+
+- (NSData *) latestDepthSamples {
+  return nil;
+}
+
+@end

KinectViewer.xcodeproj/project.pbxproj

 		C70E66CA129C9121004A44B3 /* libusb-1.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = C70E66C9129C9121004A44B3 /* libusb-1.0.dylib */; };
 		C758083412A4C46700391C33 /* KVRayFieldSpaceModel.m in Sources */ = {isa = PBXBuildFile; fileRef = C758083312A4C46700391C33 /* KVRayFieldSpaceModel.m */; };
 		C7759B4612A8B2D3003479EF /* KVKinectHardware.m in Sources */ = {isa = PBXBuildFile; fileRef = C7759B4512A8B2D3003479EF /* KVKinectHardware.m */; };
+		C7759C0712A97873003479EF /* KVDepthRecorder.m in Sources */ = {isa = PBXBuildFile; fileRef = C7759C0612A97873003479EF /* KVDepthRecorder.m */; };
+		C7759C1012A97A3B003479EF /* KVDepthPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
 		C7759B3F12A8B1EB003479EF /* KVDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KVDataSource.h; sourceTree = "<group>"; };
 		C7759B4412A8B2D3003479EF /* KVKinectHardware.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KVKinectHardware.h; sourceTree = "<group>"; };
 		C7759B4512A8B2D3003479EF /* KVKinectHardware.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KVKinectHardware.m; sourceTree = "<group>"; };
+		C7759C0512A97873003479EF /* KVDepthRecorder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KVDepthRecorder.h; sourceTree = "<group>"; };
+		C7759C0612A97873003479EF /* KVDepthRecorder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KVDepthRecorder.m; sourceTree = "<group>"; };
+		C7759C0E12A97A3B003479EF /* KVDepthPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KVDepthPlayer.h; sourceTree = "<group>"; };
+		C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KVDepthPlayer.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
 			isa = PBXGroup;
 			children = (
 				C70E6683129C8FD5004A44B3 /* driver */,
+				C7759C0012A8CBE9003479EF /* Utils */,
 				C7F36B8812A19EBD00EBC3AB /* Model */,
 				080E96DDFE201D6D7F000001 /* App */,
 				29B97315FDCFA39411CA2CEA /* Other Sources */,
 			path = libusb;
 			sourceTree = "<group>";
 		};
+		C7759C0012A8CBE9003479EF /* Utils */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			name = Utils;
+			sourceTree = "<group>";
+		};
 		C7F36B8812A19EBD00EBC3AB /* Model */ = {
 			isa = PBXGroup;
 			children = (
 				C758081A12A4C22400391C33 /* KVSpaceModel.h */,
 				C758083212A4C46700391C33 /* KVRayFieldSpaceModel.h */,
 				C758083312A4C46700391C33 /* KVRayFieldSpaceModel.m */,
+				C7759C0512A97873003479EF /* KVDepthRecorder.h */,
+				C7759C0612A97873003479EF /* KVDepthRecorder.m */,
+				C7759C0E12A97A3B003479EF /* KVDepthPlayer.h */,
+				C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */,
 			);
 			name = Model;
 			sourceTree = "<group>";
 				C70E6691129C8FD5004A44B3 /* KFKinect.m in Sources */,
 				C758083412A4C46700391C33 /* KVRayFieldSpaceModel.m in Sources */,
 				C7759B4612A8B2D3003479EF /* KVKinectHardware.m in Sources */,
+				C7759C0712A97873003479EF /* KVDepthRecorder.m in Sources */,
+				C7759C1012A97A3B003479EF /* KVDepthPlayer.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

KinectViewerAppDelegate.m

 
 #import "KinectViewerAppDelegate.h"
 #import "KVDepthView.h"
-
-@interface KinectViewerAppDelegate ()
-- (void)replayThread: (NSString *)replayFile;
-@end
+#import "KVDepthPlayer.h"
 
 @implementation KinectViewerAppDelegate
 
   
   if (replayFile) {
     NSLog(@"Playing %@", replayFile);
-    [NSThread detachNewThreadSelector:@selector(replayThread:) toTarget:self withObject:replayFile];
+    KVDepthPlayer *player = [[KVDepthPlayer alloc] init];
+    [depthView bind: @"latestDepthSamples" toObject: player withKeyPath: @"latestDepthSamples" options: nil];
+    [player open: replayFile];
   } else {
     kinect = [[KVKinectHardware alloc] init];
     [depthView bind: @"latestDepthSamples" toObject: kinect withKeyPath: @"latestDepthSamples" options: nil];
   }
 }
 
-- (void)replayThread: (NSString *)replayFile {
-  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-  
-  depth_t *bytes = calloc(1, sizeof(depth_t));
-  while (true) {
-    int fd = open([replayFile cStringUsingEncoding: NSASCIIStringEncoding], O_RDONLY);
-    do {
-      size_t r = 0;
-      r = read(fd, bytes, sizeof(depth_t));
-      if (r < sizeof(depth_t)) break;
-      [depthView setLatestDepthSamples: [NSData dataWithBytesNoCopy: bytes length: sizeof(depth_t) freeWhenDone: NO]];
-      usleep(1000000 / 30);
-    } while (YES);
-    close(fd);
-  }
-  
-  [pool drain];
-  
-  [NSApp terminate: self];
-}
-
 @end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.