/
ItemDocument.m
290 lines (228 loc) · 9.61 KB
/
ItemDocument.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#import "ItemDocument.h"
#import <AVFoundation/AVFoundation.h>
#define FRAMES_PER_SECOND 4
@implementation ItemDocument
@synthesize captureSession = captureSession, captureScreenInput = captureScreenInput;
- (id)initWithType:(NSString *)typeName error:(NSError **)outError
{
self = [super initWithType:typeName error:outError];
initializedAt = [[NSDate date] timeIntervalSince1970];
numFramesCaptured = 0;
numFramesDropped = 0;
if (self) {
BOOL success = [self createCaptureSession:outError];
if ( ! success) {
[self release];
return nil;
}
[NSTimer
scheduledTimerWithTimeInterval:(500 / 1000.0)
target:self
selector:@selector(printStats:)
userInfo:nil
repeats:YES];
}
return self;
}
- (void)printStats:(NSTimer*)timer
{
if (numFramesCaptured > 5) {
double elapsed = (double)[[NSDate date] timeIntervalSince1970] - (double)initializedAt;
double fps = numFramesCaptured / elapsed;
NSLog(@"%.4f FPS, %lld total, %lld dropped", fps, (long long)numFramesCaptured, (long long)numFramesDropped);
}
}
- (void)dealloc
{
[captureSession release];
[captureScreenInput release];
[captureVideoDataOutput release];
[super dealloc];
}
- (IBAction)start:(id)sender
{
}
- (IBAction)stop:(id)sender
{
}
#pragma mark Capture
- (BOOL)createCaptureSession:(NSError **)outError
{
captureSession = [[AVCaptureSession alloc] init];
if ( ! [captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
return NO;
}
[captureSession setSessionPreset:AVCaptureSessionPresetHigh];
display = CGMainDisplayID();
captureScreenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:display];
if ( ! [captureSession canAddInput:captureScreenInput]) {
return NO;
}
[captureSession addInput:captureScreenInput];
[self setMaximumScreenInputFramerate:[self maximumScreenInputFramerate]];
captureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
[captureVideoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
if ( ! [[captureVideoDataOutput availableVideoCodecTypes] containsObject:@"avc1"]) {
NSLog(@"avc1 not supported!");
return NO;
}
NSDictionary *settings;
if (true) {
settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:kCVPixelFormatType_24RGB], (NSString*)kCVPixelBufferPixelFormatTypeKey,
nil];
}
else {
settings = [NSDictionary dictionaryWithObjectsAndKeys:
@"avc1", AVVideoCodecKey,
[NSNumber numberWithInt:360], AVVideoWidthKey,
[NSNumber numberWithInt:225], AVVideoHeightKey,
// AVVideoMaxKeyFrameIntervalKey
// AVVideoAverageBitRateKey
// AVVideoCompressionPropertiesKey: ?
nil];
}
[captureVideoDataOutput setVideoSettings:settings];
if ( ! [captureSession canAddOutput:captureVideoDataOutput]) {
return NO;
}
[captureSession addOutput:captureVideoDataOutput];
return YES;
}
- (void)addCaptureVideoPreview
{
AVCaptureVideoPreviewLayer *videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
[videoPreviewLayer setFrame:[[captureView layer] bounds]];
[videoPreviewLayer setAutoresizingMask:kCALayerWidthSizable|kCALayerHeightSizable];
[[captureView layer] addSublayer:videoPreviewLayer];
[[captureView layer] setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
[videoPreviewLayer release];
}
- (float)maximumScreenInputFramerate
{
return FRAMES_PER_SECOND;
}
- (void)setMaximumScreenInputFramerate:(float)maximumFramerate
{
CMTime minimumFrameDuration = CMTimeMake(1, (int32_t)maximumFramerate);
[captureScreenInput setMinFrameDuration:minimumFrameDuration];
}
- (void)addDisplayInputToCaptureSession:(CGDirectDisplayID)newDisplay cropRect:(CGRect)cropRect
{
[captureSession beginConfiguration];
if (newDisplay != display) {
[captureSession removeInput:captureScreenInput];
AVCaptureScreenInput *newScreenInput = [[AVCaptureScreenInput alloc] initWithDisplayID:newDisplay];
[captureScreenInput release];
captureScreenInput = newScreenInput;
if ([captureSession canAddInput:captureScreenInput]) {
[captureSession addInput:captureScreenInput];
}
}
[captureScreenInput setCropRect:cropRect];
[captureSession commitConfiguration];
}
#pragma mark AVCaptureVideoDataOutputSampleBufferDelegate
// video frame was discarded
- (void)captureOutput:(AVCaptureOutput *)captureOutput didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
numFramesDropped += 1;
}
// video frame was written
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sb fromConnection:(AVCaptureConnection *)connection
{
if ( ! task) {
task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/local/bin/coffee"];
[task setEnvironment:[NSDictionary dictionaryWithObjectsAndKeys:
NSUserName(), @"USER",
@"/usr/local/bin:/usr/bin:/bin", @"PATH",
nil]];
[task setArguments:[NSArray arrayWithObjects:
[[NSBundle mainBundle] pathForResource:@"upload" ofType:@"coffee"],
[NSString stringWithFormat:@"%d", (int)FRAMES_PER_SECOND],
nil]];
[task setStandardInput:[NSPipe pipe]];
[task launch];
taskStdin = [[task standardInput] fileHandleForWriting];
}
if (numFramesCaptured == 0) {
initializedAt = [[NSDate date] timeIntervalSince1970];
}
numFramesCaptured += 1;
CVImageBufferRef img = CMSampleBufferGetImageBuffer(sb);
CGSize encodedSize = CVImageBufferGetEncodedSize(img);
CVPixelBufferLockBaseAddress(img, 0);
void *baseAddress = CVPixelBufferGetBaseAddress(img);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(img);
int width = (int)CVPixelBufferGetWidth(img);
int height = (int)CVPixelBufferGetHeight(img);
[taskStdin writeData:[[NSString stringWithFormat:@"P6\n%d %d\n255\n", (int)width, (int)height]
dataUsingEncoding:NSUTF8StringEncoding]];
NSData *pixels = [[NSData alloc] initWithBytesNoCopy:baseAddress length:(bytesPerRow * height) freeWhenDone:NO];
[taskStdin writeData:pixels];
CVPixelBufferUnlockBaseAddress(img, 0);
//NSLog(@"[SAMPLE] RGB [%d, %d]", (int)encodedSize.width, (int)encodedSize.height);
// NSLog(@"[SAMPLE] typeId=%lu, ns=%ld, %lld, %lld", typeId, numSamples, DecodeTimeStamp, PresentationTimeStamp);
/*
// If you need to reference the CMSampleBuffer object outside of the scope of this method, you must CFRetain it and then CFRelease it when you are finished with it.
long numSamples = (long)CMSampleBufferGetNumSamples(sb);
long long DecodeTimeStamp = (long long)CMTimeGetSeconds(CMSampleBufferGetOutputDecodeTimeStamp(sb));
long long PresentationTimeStamp = (long long)CMTimeGetSeconds(CMSampleBufferGetOutputPresentationTimeStamp(sb));
unsigned long typeId = 0;// (unsigned long)(CMSampleBufferGetTypeID(sb));
NSLog(@"[SAMPLE] typeId=%lu, ns=%ld, %lld, %lld", typeId, numSamples, DecodeTimeStamp, PresentationTimeStamp);
CMTime CMSampleBufferGetOutputDecodeTimeStamp
CMSampleBufferGetOutputDuration
CMSampleBufferGetOutputPresentationTimeStamp
*/
}
#pragma mark Document Stuff
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"ItemDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
[self addCaptureVideoPreview];
[captureSession startRunning];
}
- (void)close
{
[captureSession stopRunning];
[super close];
}
-(BOOL)isDocumentEdited
{
return NO;
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
/*
Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
*/
if (outError) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
/*
Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
*/
if (outError) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
}
return YES;
}
+ (BOOL)autosavesInPlace
{
return YES;
}
@end