-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraWindowController.m
226 lines (184 loc) · 5.89 KB
/
CameraWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// CameraWindowController.m
// Kineo
//
// Created by Ben Yellin on 3/20/11.
// Copyright 2011 Been Yelling. All rights reserved.
//
#import "CameraWindowController.h"
#import "CameraView.h"
#import <QTKit/QTKit.h>
#import <QuartzCore/CIImage.h>
#import "FlipSeries.h"
#import "FlipViewerController.h"
#import "NSImage+Extras.h"
#import "PreferencesController.h"
#define kFrameCount 5
#define kRecordRate 0.5
@interface CameraWindowController (Private)
- (void)setupCamera;
- (void)setupDevices;
- (void)updateDevice;
- (void)devicesChanged:(NSNotification *)notification;
- (void)setRecording:(BOOL)flag;
- (void)prefsChanged:(NSNotification *)notification;
@end
@implementation CameraWindowController
@synthesize isRecording;
@synthesize currentFlipSeries;
+ (CameraWindowController *)sharedCameraWindow {
static CameraWindowController *shared = nil;
@synchronized(self) {
if (!shared)
shared = [[CameraWindowController alloc] initWithWindowNibName:@"Camera"];
}
return shared;
}
- (void)windowDidLoad {
isRecording = NO;
[self setupCamera];
[frameCountLabel setHidden:YES];
[devicePicker removeAllItems];
numFrames = [[[NSUserDefaults standardUserDefaults]
objectForKey:@"numFrames"] integerValue];
frameRate = (NSTimeInterval)[[[NSUserDefaults standardUserDefaults]
objectForKey:@"frameRate"] floatValue];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(prefsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
}
- (void)dealloc {
[deviceList release];
self.currentFlipSeries = nil;
[super dealloc];
}
- (void)setupCamera {
videoSession = [[QTCaptureSession alloc] init];
QTCaptureDevice *device = [QTCaptureDevice
defaultInputDeviceWithMediaType:QTMediaTypeVideo];
[device open:nil];
[self setupDevices];
videoOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
[videoOutput setDelegate:self];
[videoSession addOutput:videoOutput error:nil];
[videoSession startRunning];
[camView setCaptureSession:videoSession];
}
- (void)setupDevices {
currentVideoDevice = [[QTCaptureDevice
defaultInputDeviceWithMediaType:QTMediaTypeVideo] retain];
if (deviceList) {
[deviceList release];
deviceList = nil;
}
deviceList = [[NSMutableArray alloc]
initWithArray:[QTCaptureDevice
inputDevicesWithMediaType:QTMediaTypeVideo]];
if ([deviceList count] > 1) {
for (QTCaptureDevice *device in deviceList) {
[devicePicker addItemWithTitle:[device description]];
}
[devicePicker setHidden:NO];
} else {
[devicePicker setHidden:YES];
}
[self updateDevice];
// Device Notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(devicesChanged:)
name:QTCaptureDeviceWasConnectedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(devicesChanged:)
name:QTCaptureDeviceWasDisconnectedNotification
object:nil];
}
- (void)devicesChanged:(NSNotification *)notification {
[self setupDevices];
}
- (void)updateDevice {
// Remove Current Device
[videoSession removeInput:[QTCaptureDeviceInput deviceInputWithDevice:currentVideoDevice]];
QTCaptureDeviceInput *deviceInput = [QTCaptureDeviceInput deviceInputWithDevice:currentVideoDevice];
NSError *err = nil;
[videoSession addInput:deviceInput error:&err];
if (err) {
NSLog(@"Video Error: %@", [err description]);
}
}
- (IBAction)changeDevice:(id)sender {
NSInteger selectedIndex = [devicePicker indexOfSelectedItem];
currentVideoDevice = [[deviceList objectAtIndex:selectedIndex] retain];
[self updateDevice];
}
- (void)setRecording:(BOOL)flag {
isRecording = flag;
if (isRecording) {
[videoSession startRunning];
self.currentFlipSeries = [[FlipSeries alloc] init];
currentFrameCount = 0;
[frameCountLabel setIntValue:numFrames];
[NSTimer scheduledTimerWithTimeInterval:frameRate
target:self
selector:@selector(recordingTimer:)
userInfo:nil
repeats:YES];
[recordButton setTitle:@"Stop"];
} else {
//[videoSession stopRunning];
[recordButton setTitle:@"Make Flipbook"];
}
[frameCountLabel setHidden:!isRecording];
}
- (IBAction)recordingAction:(id)sender {
[self setRecording:!isRecording];
}
#pragma mark Capture Delegate
- (void)captureOutput:(QTCaptureOutput *)captureOutput
didOutputVideoFrame:(CVImageBufferRef)videoFrame
withSampleBuffer:(QTSampleBuffer *)sampleBuffer
fromConnection:(QTCaptureConnection *)connection {
CVImageBufferRef previousBuffer;
CVBufferRetain(videoFrame);
@synchronized (self) {
previousBuffer = currentImageBuffer;
currentImageBuffer = videoFrame;
}
CVBufferRelease(previousBuffer);
}
#pragma mark Recording
- (void)recordingTimer:(NSTimer *)timer {
if (isRecording) {
if (currentFrameCount <= numFrames) {
if (currentImageBuffer) {
CIImage *camImage = [CIImage imageWithCVImageBuffer:currentImageBuffer];
[currentFlipSeries addImage:
[NSImage imageFromCIImage:camImage]];
[frameCountLabel setIntValue:(numFrames-currentFrameCount)];
currentFrameCount++;
}
} else {
[timer invalidate];
[self setRecording:NO];
// Open Flip Viewer
FlipViewerController *flipViewer = [[FlipViewerController alloc] initWithFlipSeries:currentFlipSeries];
[flipViewer showWindow:nil];
self.currentFlipSeries = nil;
currentFrameCount = 0;
}
} else {
[timer invalidate];
currentFrameCount = 0;
}
}
- (IBAction)showPreferences:(id)sender {
[[PreferencesController sharedPreferences] showWindow:nil];
}
- (void)prefsChanged:(NSNotification *)notification {
numFrames = [[[NSUserDefaults standardUserDefaults]
objectForKey:@"numFrames"] integerValue];
frameRate = (NSTimeInterval)[[[NSUserDefaults standardUserDefaults]
objectForKey:@"frameRate"] floatValue];
}
@end