-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQTKitCaptureImageControllerBase.m
More file actions
244 lines (204 loc) · 7.1 KB
/
QTKitCaptureImageControllerBase.m
File metadata and controls
244 lines (204 loc) · 7.1 KB
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
#import "QTKitCaptureImageControllerBase.h"
#import "CMCaptureImageModelCIImage.h"
@interface QTKitCaptureImageControllerBase()
@property (assign) CVImageBufferRef imageBufferRef;
@property (ARC_PROP_STRONG,readwrite) QTCaptureSession* captureSession;
@property (ARC_PROP_STRONG,readwrite) QTCaptureDeviceInput* captureVideoDeviceInput;
@property (ARC_PROP_STRONG,readwrite) QTCaptureDecompressedVideoOutput* captureVideoOutput;
- (BOOL)captureSetupSessionOutputForVideoCapture:(QTCaptureSession*)session;
@end
@implementation QTKitCaptureImageControllerBase
@dynamic delegate;
@dynamic outletCapturePreviewView;
ARC_SYNTHESIZE(imageBufferRef,__imageBufferRef);
ARC_SYNTHESIZE(captureSession,__captureSession);
ARC_SYNTHESIZE(captureVideoDeviceInput,__captureVideoDeviceInput);
ARC_SYNTHESIZE(captureVideoOutput,__captureVideoOutput);
- (void)dealloc
{
[self captureTeardown];
self.imageBufferRef = NULL;
ARC_DEALLOC_NIL(self.captureVideoOutput);
ARC_DEALLOC_NIL(self.captureVideoDeviceInput);
ARC_DEALLOC_NIL(self.captureSession);
ARC_SUPERDEALLOC(self);
}
#pragma mark -
- (BOOL)captureSetupNeeded
{
return (self.captureSession==nil);
}
- (void)captureSetup
{
[super captureSetup];
QTCaptureSession* session = [[QTCaptureSession alloc] init];
if (REQUIRED_DEBUG(session!=nil))
{
if (REQUIRED_DEBUG([self captureSetupSessionOutputForVideoCapture:session]))
{
self.captureSession = session;
[self captureSetupPreviewUI];
[self.captureSession startRunning];
}
ARC_RELEASE(session);
}
if (!REQUIRED_DEBUG(self.captureSession.isRunning))
[self captureTeardown];
NSLOG_METHOD(self.captureSession);
}
#pragma mark -
+(NSDictionary*) defaultIOSurfaceProperties
{
return [NSDictionary dictionary];
}
+(NSDictionary*) defaultCaptureVideoPixelBufferFormat
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, /* iSight native */
// [NSNumber numberWithUnsignedInt:k2vuyPixelFormat], kCVPixelBufferPixelFormatTypeKey, /* iSight native */
// [NSNumber numberWithUnsignedInt:kCVPixelFormatType_422YpCbCr8], (id)kCVPixelBufferPixelFormatTypeKey, /* iSight native? */
[self defaultIOSurfaceProperties], kCVPixelBufferIOSurfacePropertiesKey,
nil];
}
+(NSDictionary*) defaultCaptureVideoPixelBufferAttributes
{
return [self defaultCaptureVideoPixelBufferFormat];
}
+(NSDictionary*) defaultCaptureVideoPixelBufferAttributesWithFrameSize:(CGSize)videoFrameSize
{
NSMutableDictionary* format = [[self defaultCaptureVideoPixelBufferFormat] mutableCopy];
[format setObject:[NSNumber numberWithDouble:videoFrameSize.width] forKey:(id)kCVPixelBufferWidthKey];
[format setObject:[NSNumber numberWithDouble:videoFrameSize.height] forKey:(id)kCVPixelBufferHeightKey];
return [NSDictionary dictionaryWithDictionary:format];
}
-(NSDictionary*) captureVideoPixelBufferAttributes
{
return [[self class] defaultCaptureVideoPixelBufferAttributes];
}
#pragma mark -
- (BOOL)captureSetupSessionOutputForVideoCapture:(QTCaptureSession*)session
{
BOOL result = NO;
if (REQUIRED_DEBUG(session!=nil))
{
NSError *error = nil;
QTCaptureDevice* videoDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
if (REQUIRED_DEBUG(videoDevice!=nil) && REQUIRED_DEBUG([videoDevice open:&error]) && REQUIRED_NSERROR_NIL(error))
{
self.captureVideoDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:videoDevice];
if (REQUIRED_DEBUG(self.captureVideoDeviceInput!=nil))
{
if (REQUIRED_DEBUG([session addInput:self.captureVideoDeviceInput error:&error]) && REQUIRED_NSERROR_NIL(error))
{
QTCaptureDecompressedVideoOutput* captureVideoOutput = [[QTCaptureDecompressedVideoOutput alloc] init];
if (REQUIRED_DEBUG(captureVideoOutput!=nil))
{
self.captureVideoOutput = captureVideoOutput;
self.captureVideoOutput.automaticallyDropsLateVideoFrames = YES;
[self.captureVideoOutput setPixelBufferAttributes:self.captureVideoPixelBufferAttributes];
[self.captureVideoOutput setDelegate:self];
result = REQUIRED_DEBUG([session addOutput:self.captureVideoOutput error:&error]) && REQUIRED_NSERROR_NIL(error);
ARC_RELEASE(captureVideoOutput);
}
}
ARC_RELEASE(self.captureVideoDeviceInput);
}
}
}
return result;
}
- (BOOL)captureSetupPreviewUI
{
BOOL result = NO;
if (REQUIRED_DEBUG(self.outletCapturePreviewView!=nil) && REQUIRED_DEBUG(self.captureSession!=nil))
{
[self.outletCapturePreviewView setCaptureSession:self.captureSession];
result = YES;
}
return result;
}
#pragma mark -
- (BOOL)captureTeardownNeeded
{
return (self.captureSession!=nil);
}
- (void)captureTeardown
{
if (self.captureTeardownNeeded)
{
if (REQUIRED_DEBUG(self.captureVideoQueue!=NULL))
dispatch_sync(self.captureVideoQueue,^{});
if (REQUIRED_DEBUG(self.captureSession.isRunning))
{
[self.captureSession stopRunning];
}
if ([[self.captureVideoDeviceInput device] isOpen])
[[self.captureVideoDeviceInput device] close];
self.captureVideoOutput = nil;
self.captureVideoDeviceInput = nil;
self.captureSession = nil;
}
[super captureTeardown];
}
-(void) setImageBuffer:(CVImageBufferRef)imageBufferRef
{
@synchronized(self)
{
if (self.imageBufferRef!=NULL) CVBufferRelease(self.imageBufferRef);
self.imageBufferRef = imageBufferRef;
CVBufferRetain(self.imageBufferRef);
}
}
-(CVImageBufferRef) imageBuffer
{
@synchronized(self)
{
return self.imageBufferRef;
}
}
-(CVImageBufferRef) imageBufferCopy
{
CVImageBufferRef copyImageBuffer = NULL;
if (self.imageBufferRef!=NULL) @synchronized(self)
{
CVBufferRetain(self.imageBufferRef);
copyImageBuffer = self.imageBufferRef;
}
return copyImageBuffer;
}
-(void) captureSnapStillPictureFromImageBuffer:(CVImageBufferRef)imageBuffer
{
if (REQUIRED_DEBUG(imageBuffer!=NULL))
{
NSData* imageData = [CMCaptureImageModelCIImage copyStillImageDataFromImageBuffer:imageBuffer];
if (REQUIRED_DEBUG(imageData!=nil))
{
id imageMetadata = nil;
if (self.outletCaptureStillModel!=nil)
[self.outletCaptureStillModel setImageDataWithImageData:imageData imageMetadata:imageMetadata];
[self captureOutputImageUsingDelegate:self.outletCaptureStillModel imageBuffer:imageBuffer];
[self captureWriteImageData:imageData imageMetadata:imageMetadata];
ARC_RELEASE(imageData);
}
}
}
// main action method to take a still image -- if face detection has been turned on and a face has been detected
// the square overlay will be composited on top of the captured image and saved to the camera roll
- (IBAction) actionCaptureSnapshot:(id)sender
{
NSLOG_METHOD(sender);
CVImageBufferRef imageBuffer = [self imageBufferCopy];
if (REQUIRED_DEBUG(imageBuffer!=NULL))
{
[self captureSnapStillPictureFromImageBuffer:imageBuffer];
CVBufferRelease(imageBuffer);
}
}
#pragma mark -
- (void)captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection
{
// NSLOG_METHOD(captureOutput);
REQUIRED_DEBUG(videoFrame!=NULL);
[self setImageBuffer:videoFrame];
}
@end