| 1 |
#import <QTKit/QTKit.h> |
| 2 |
#include "video_capture.h" |
| 3 |
|
| 4 |
#define DebugLog if (0) NSLog |
| 5 |
|
| 6 |
// -*- mode: objc -*- |
| 7 |
|
| 8 |
@interface VideoCapture: NSObject |
| 9 |
{ |
| 10 |
QTCaptureSession* captureSession; |
| 11 |
NSMutableArray* videoDevices; |
| 12 |
QTCaptureDeviceInput* deviceInput; |
| 13 |
QTCaptureDecompressedVideoOutput* videoOutput; |
| 14 |
|
| 15 |
vcapture_frame_callback callback; |
| 16 |
void* userdata; |
| 17 |
|
| 18 |
struct vcapture_frame_format requested; |
| 19 |
struct vcapture_frame_format actual; |
| 20 |
BOOL needActual; |
| 21 |
|
| 22 |
} |
| 23 |
|
| 24 |
- (NSArray*) devices; |
| 25 |
- (int) device; |
| 26 |
- (int) setDevice: (int)idx; |
| 27 |
|
| 28 |
- (BOOL) active; |
| 29 |
- (int) start; |
| 30 |
- (int) stop; |
| 31 |
|
| 32 |
- (vcapture_frame_callback) callback; |
| 33 |
- (void) setCallback: (vcapture_frame_callback)callback withUserData:(void*)userdata; |
| 34 |
|
| 35 |
- (void) getRequestedFormat:(struct vcapture_frame_format*)format; |
| 36 |
- (int) setRequestedFormat:(const struct vcapture_frame_format*)format; |
| 37 |
|
| 38 |
- (void) captureOutput:(QTCaptureOutput *)captureOutput |
| 39 |
didOutputVideoFrame:(CVImageBufferRef)videoFrame |
| 40 |
withSampleBuffer:(QTSampleBuffer *)sampleBuffer |
| 41 |
fromConnection:(QTCaptureConnection *)connection; |
| 42 |
|
| 43 |
@end |
| 44 |
|
| 45 |
@implementation VideoCapture |
| 46 |
|
| 47 |
- (id) init { |
| 48 |
if ( (self = [super init]) ) { |
| 49 |
|
| 50 |
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; |
| 51 |
|
| 52 |
captureSession = [[QTCaptureSession alloc] init]; |
| 53 |
videoDevices = [[NSMutableArray alloc] init]; |
| 54 |
deviceInput = nil; |
| 55 |
videoOutput = nil; |
| 56 |
callback = 0; |
| 57 |
userdata = 0; |
| 58 |
|
| 59 |
memset(&requested, 0, sizeof(struct vcapture_frame_format)); |
| 60 |
memset(&actual, 0, sizeof(struct vcapture_frame_format)); |
| 61 |
|
| 62 |
requested.type = VC_TYPE_RGB; |
| 63 |
needActual = YES; |
| 64 |
|
| 65 |
NSArray* allInputDevices = [QTCaptureDevice inputDevices]; |
| 66 |
|
| 67 |
for (QTCaptureDevice* device in allInputDevices) { |
| 68 |
DebugLog(@"device: %@", device); |
| 69 |
if ([device hasMediaType:QTMediaTypeVideo] || |
| 70 |
[device hasMediaType:QTMediaTypeMuxed]) { |
| 71 |
DebugLog(@" it is video!"); |
| 72 |
[videoDevices addObject:device]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
[pool release]; |
| 77 |
|
| 78 |
} |
| 79 |
return self; |
| 80 |
} |
| 81 |
|
| 82 |
- (void) resetDevice { |
| 83 |
|
| 84 |
if ([self active]) { |
| 85 |
[self stop]; |
| 86 |
} |
| 87 |
|
| 88 |
if (videoOutput) { |
| 89 |
[captureSession removeOutput:videoOutput]; |
| 90 |
[videoOutput release]; |
| 91 |
videoOutput = nil; |
| 92 |
} |
| 93 |
|
| 94 |
if (deviceInput) { |
| 95 |
[captureSession removeInput:deviceInput]; |
| 96 |
QTCaptureDevice* device = [deviceInput device]; |
| 97 |
if ([device isOpen]) { |
| 98 |
[device close]; |
| 99 |
} |
| 100 |
[deviceInput release]; |
| 101 |
deviceInput = nil; |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
- (void) dealloc { |
| 107 |
DebugLog(@"deallocating a %@", [self className]); |
| 108 |
if (deviceInput) { |
| 109 |
if ([self active]) { |
| 110 |
DebugLog(@"stopping device in dealloc"); |
| 111 |
} |
| 112 |
DebugLog(@"resetting device in dealloc"); |
| 113 |
} |
| 114 |
[self resetDevice]; |
| 115 |
[videoDevices release]; |
| 116 |
[captureSession release]; |
| 117 |
[super dealloc]; |
| 118 |
} |
| 119 |
|
| 120 |
- (int) device { |
| 121 |
if (deviceInput == nil) { |
| 122 |
return -1; |
| 123 |
} else { |
| 124 |
return [videoDevices indexOfObject:[deviceInput device]]; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
- (int) setDevice: (int)idx { |
| 129 |
|
| 130 |
if ([self device] == idx) { |
| 131 |
return VC_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
[self resetDevice]; |
| 135 |
|
| 136 |
if (idx < 0) { return VC_SUCCESS; } |
| 137 |
if (idx >= [videoDevices count]) { return VC_INVALID_DEVICE; } |
| 138 |
|
| 139 |
|
| 140 |
QTCaptureDevice* device = [videoDevices objectAtIndex:idx]; |
| 141 |
NSError* error = nil; |
| 142 |
|
| 143 |
BOOL success = [device open:&error]; |
| 144 |
|
| 145 |
if (!success) { |
| 146 |
NSLog(@"Error opening device: %@", error); |
| 147 |
return VC_DEVICE_OPEN_ERROR; |
| 148 |
} |
| 149 |
|
| 150 |
deviceInput = [[QTCaptureDeviceInput alloc] initWithDevice: device]; |
| 151 |
|
| 152 |
success = [captureSession addInput:deviceInput error:&error]; |
| 153 |
if (!success) { |
| 154 |
NSLog(@"Error adding input: %@", error); |
| 155 |
[self resetDevice]; |
| 156 |
return VC_DEVICE_OPEN_ERROR; |
| 157 |
} |
| 158 |
|
| 159 |
videoOutput = [[QTCaptureDecompressedVideoOutput alloc] init]; |
| 160 |
success = [captureSession addOutput:videoOutput error:&error]; |
| 161 |
if (!success) { |
| 162 |
NSLog(@"Error adding output: %@", error); |
| 163 |
[self resetDevice]; |
| 164 |
return VC_DEVICE_OPEN_ERROR; |
| 165 |
} |
| 166 |
|
| 167 |
[videoOutput setDelegate:self]; |
| 168 |
[videoOutput setAutomaticallyDropsLateVideoFrames:YES]; |
| 169 |
|
| 170 |
needActual = YES; |
| 171 |
|
| 172 |
|
| 173 |
NSMutableDictionary* pixelBufferAttributes = [[NSMutableDictionary alloc] init]; |
| 174 |
NSString* key; |
| 175 |
NSNumber* val; |
| 176 |
|
| 177 |
|
| 178 |
if (requested.fourcc || requested.type) { |
| 179 |
|
| 180 |
unsigned int type; |
| 181 |
if (requested.fourcc) { |
| 182 |
type = requested.fourcc; |
| 183 |
} else { |
| 184 |
type = vcapture_type_to_fourcc(requested.type); |
| 185 |
} |
| 186 |
|
| 187 |
key = (NSString*)kCVPixelBufferPixelFormatTypeKey; |
| 188 |
val = [NSNumber numberWithUnsignedInt:type]; |
| 189 |
|
| 190 |
DebugLog(@"set pixelBufferAttributes %@->%@", key, val); |
| 191 |
|
| 192 |
[pixelBufferAttributes setObject:val forKey:key]; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
if (requested.width && requested.height) { |
| 197 |
|
| 198 |
key = (NSString*)kCVPixelBufferWidthKey; |
| 199 |
val = [NSNumber numberWithInt:requested.width]; |
| 200 |
[pixelBufferAttributes setObject:val forKey:key]; |
| 201 |
DebugLog(@"set pixelBufferAttributes %@->%@", key, val); |
| 202 |
|
| 203 |
key = (NSString*)kCVPixelBufferHeightKey; |
| 204 |
val = [NSNumber numberWithInt:requested.height]; |
| 205 |
[pixelBufferAttributes setObject:val forKey:key]; |
| 206 |
DebugLog(@"set pixelBufferAttributes %@->%@", key, val); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
if ([pixelBufferAttributes count]) { |
| 211 |
[videoOutput setPixelBufferAttributes:pixelBufferAttributes]; |
| 212 |
} else { |
| 213 |
[pixelBufferAttributes release]; |
| 214 |
} |
| 215 |
|
| 216 |
return VC_SUCCESS; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
- (NSArray*) devices { |
| 221 |
return videoDevices; |
| 222 |
} |
| 223 |
|
| 224 |
- (vcapture_frame_callback) callback { |
| 225 |
return callback; |
| 226 |
} |
| 227 |
|
| 228 |
- (void)captureOutput:(QTCaptureOutput *)captureOutput |
| 229 |
didOutputVideoFrame:(CVImageBufferRef)videoFrame |
| 230 |
withSampleBuffer:(QTSampleBuffer *)sampleBuffer |
| 231 |
fromConnection:(QTCaptureConnection *)connection |
| 232 |
|
| 233 |
{ |
| 234 |
|
| 235 |
[sampleBuffer incrementSampleUseCount]; |
| 236 |
|
| 237 |
CVReturn status = CVPixelBufferLockBaseAddress(videoFrame, 0); |
| 238 |
|
| 239 |
if (status == 0) { |
| 240 |
|
| 241 |
if (needActual) { |
| 242 |
|
| 243 |
needActual = NO; |
| 244 |
|
| 245 |
actual.fourcc = CVPixelBufferGetPixelFormatType(videoFrame); |
| 246 |
actual.type = vcapture_fourcc_to_type(actual.fourcc); |
| 247 |
actual.width = CVPixelBufferGetWidth(videoFrame); |
| 248 |
actual.height = CVPixelBufferGetHeight(videoFrame); |
| 249 |
|
| 250 |
size_t l, r, t, b; |
| 251 |
|
| 252 |
CVPixelBufferGetExtendedPixels(videoFrame, &l, &r, &t, &b); |
| 253 |
|
| 254 |
actual.pad_left = l; |
| 255 |
actual.pad_right = r; |
| 256 |
actual.pad_top = t; |
| 257 |
actual.pad_bottom = b; |
| 258 |
|
| 259 |
actual.bytes_per_row = CVPixelBufferGetBytesPerRow(videoFrame); |
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
struct vcapture_frame frame; |
| 264 |
|
| 265 |
memset(frame.planes, 0, sizeof(frame.planes)); |
| 266 |
|
| 267 |
unsigned long long timestamp = 0; |
| 268 |
|
| 269 |
|
| 270 |
if (CVPixelBufferIsPlanar(videoFrame)) { |
| 271 |
frame.count = CVPixelBufferGetPlaneCount(videoFrame); |
| 272 |
if (frame.count > 4) { frame.count = 4; } |
| 273 |
int i; |
| 274 |
for (i=0; i<frame.count; ++i) { |
| 275 |
frame.planes[i] = CVPixelBufferGetBaseAddressOfPlane(videoFrame, i); |
| 276 |
} |
| 277 |
} else { |
| 278 |
frame.count = 0; |
| 279 |
frame.planes[0] = CVPixelBufferGetBaseAddress(videoFrame); |
| 280 |
} |
| 281 |
|
| 282 |
callback(&actual, &frame, timestamp, userdata); |
| 283 |
|
| 284 |
CVPixelBufferUnlockBaseAddress(videoFrame, 0); |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
[sampleBuffer decrementSampleUseCount]; |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
- (BOOL) active { |
| 293 |
return [captureSession isRunning]; |
| 294 |
} |
| 295 |
|
| 296 |
- (int) start { |
| 297 |
if ([self device] < 0) { return VC_NO_ACTIVE_DEVICE; } |
| 298 |
[captureSession startRunning]; |
| 299 |
return VC_SUCCESS; |
| 300 |
} |
| 301 |
|
| 302 |
- (int) stop { |
| 303 |
if ([captureSession isRunning]) { |
| 304 |
[captureSession stopRunning]; |
| 305 |
} |
| 306 |
return VC_SUCCESS; |
| 307 |
} |
| 308 |
|
| 309 |
- (void) setCallback: (vcapture_frame_callback)c withUserData:(void*)u { |
| 310 |
callback = c; |
| 311 |
userdata = u; |
| 312 |
} |
| 313 |
|
| 314 |
- (void) getRequestedFormat:(struct vcapture_frame_format*)format { |
| 315 |
memcpy(format, &requested, sizeof(struct vcapture_frame_format)); |
| 316 |
} |
| 317 |
|
| 318 |
- (int) setRequestedFormat:(const struct vcapture_frame_format*)format { |
| 319 |
if ([self active]) { |
| 320 |
return VC_DEVICE_RUNNING; |
| 321 |
} |
| 322 |
int oldDevice = [self device]; |
| 323 |
[self resetDevice]; |
| 324 |
memcpy(&requested, format, sizeof(struct vcapture_frame_format)); |
| 325 |
return [self setDevice:oldDevice]; |
| 326 |
} |
| 327 |
|
| 328 |
@end |
| 329 |
|
| 330 |
////////////////////////////////////////////////////////////////////// |
| 331 |
|
| 332 |
unsigned int vcapture_type_to_fourcc(enum vcapture_format_type t) { |
| 333 |
switch (t) { |
| 334 |
case VC_TYPE_RGB: return k24RGBPixelFormat; |
| 335 |
case VC_TYPE_RGBA: return kCVPixelFormatType_32RGBA; |
| 336 |
case VC_TYPE_ARGB: return kCVPixelFormatType_32ARGB; |
| 337 |
case VC_TYPE_FOURCC_2uvy: return '2uvy'; |
| 338 |
case VC_TYPE_FOURCC_yuvs: return 'yuvs'; |
| 339 |
case VC_TYPE_FOURCC_YVYU: return 'YVYU'; |
| 340 |
case VC_TYPE_FOURCC_yuvu: return 'yuvu'; |
| 341 |
default: |
| 342 |
break; |
| 343 |
} |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
enum vcapture_format_type vcapture_fourcc_to_type(unsigned int fourcc) { |
| 348 |
switch (fourcc) { |
| 349 |
case k24RGBPixelFormat: return VC_TYPE_RGB; |
| 350 |
case kCVPixelFormatType_32ARGB: return VC_TYPE_ARGB; |
| 351 |
case kCVPixelFormatType_32RGBA: return VC_TYPE_RGBA; |
| 352 |
case '2uvy': return VC_TYPE_FOURCC_2uvy; |
| 353 |
case 'yuvs': return VC_TYPE_FOURCC_yuvs; |
| 354 |
case 'YVYU': return VC_TYPE_FOURCC_YVYU; |
| 355 |
case 'yuvu': return VC_TYPE_FOURCC_yuvu; |
| 356 |
default: |
| 357 |
break; |
| 358 |
} |
| 359 |
return VC_TYPE_UNKNOWN; |
| 360 |
} |
| 361 |
|
| 362 |
////////////////////////////////////////////////////////////////////// |
| 363 |
|
| 364 |
void vcapture_poll(int msec) { |
| 365 |
NSTimeInterval interval = msec / 1000.0; |
| 366 |
NSDate* date = [[NSDate alloc] initWithTimeIntervalSinceNow:interval]; |
| 367 |
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:date]; |
| 368 |
[date release]; |
| 369 |
} |
| 370 |
|
| 371 |
////////////////////////////////////////////////////////////////////// |
| 372 |
|
| 373 |
struct video_capture { |
| 374 |
NSAutoreleasePool* pool; |
| 375 |
VideoCapture* instance; |
| 376 |
}; |
| 377 |
|
| 378 |
////////////////////////////////////////////////////////////////////// |
| 379 |
|
| 380 |
vcapture vcapture_alloc() { |
| 381 |
vcapture rval = (vcapture)malloc(sizeof(struct video_capture)); |
| 382 |
if (!rval) { return 0; } |
| 383 |
rval->pool = [[NSAutoreleasePool alloc] init]; |
| 384 |
rval->instance = [[VideoCapture alloc] init]; |
| 385 |
if (!rval->instance) { |
| 386 |
vcapture_free(rval); |
| 387 |
rval = 0; |
| 388 |
} |
| 389 |
return rval; |
| 390 |
} |
| 391 |
|
| 392 |
void vcapture_free(vcapture v) { |
| 393 |
[v->instance release]; |
| 394 |
[v->pool release]; |
| 395 |
free(v); |
| 396 |
} |
| 397 |
|
| 398 |
////////////////////////////////////////////////////////////////////// |
| 399 |
|
| 400 |
int vcapture_device_count(vcapture v) { |
| 401 |
return [[v->instance devices] count]; |
| 402 |
} |
| 403 |
|
| 404 |
const char* vcapture_device_name(vcapture v, int idx) { |
| 405 |
|
| 406 |
NSArray* devices = [v->instance devices]; |
| 407 |
if (idx >= [devices count]) { |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
QTCaptureDevice* device = [devices objectAtIndex:idx]; |
| 412 |
NSString* str = [device description]; |
| 413 |
|
| 414 |
if (!str) { return 0; } |
| 415 |
|
| 416 |
return [str cStringUsingEncoding:NSUTF8StringEncoding]; |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
////////////////////////////////////////////////////////////////////// |
| 421 |
|
| 422 |
int vcapture_get_device(vcapture v) { |
| 423 |
return [v->instance device]; |
| 424 |
} |
| 425 |
|
| 426 |
int vcapture_set_device(vcapture v, int device) { |
| 427 |
return [v->instance setDevice:device]; |
| 428 |
} |
| 429 |
|
| 430 |
////////////////////////////////////////////////////////////////////// |
| 431 |
|
| 432 |
int vcapture_get_requested_format(vcapture v, struct vcapture_frame_format* f) { |
| 433 |
[v->instance getRequestedFormat:f]; |
| 434 |
return VC_SUCCESS; |
| 435 |
} |
| 436 |
|
| 437 |
int vcapture_set_requested_format(vcapture v, const struct vcapture_frame_format* f) { |
| 438 |
return [v->instance setRequestedFormat:f]; |
| 439 |
} |
| 440 |
|
| 441 |
////////////////////////////////////////////////////////////////////// |
| 442 |
|
| 443 |
int vcapture_set_callback(vcapture v, |
| 444 |
vcapture_frame_callback callback, |
| 445 |
void* userdata) { |
| 446 |
[v->instance setCallback:callback withUserData:userdata]; |
| 447 |
return VC_SUCCESS; |
| 448 |
} |
| 449 |
|
| 450 |
vcapture_frame_callback vcapture_get_callback(vcapture v) { |
| 451 |
return [v->instance callback]; |
| 452 |
} |
| 453 |
|
| 454 |
////////////////////////////////////////////////////////////////////// |
| 455 |
|
| 456 |
int vcapture_active(vcapture v) { |
| 457 |
return [v->instance active]; |
| 458 |
} |
| 459 |
|
| 460 |
int vcapture_start(vcapture v) { |
| 461 |
return [v->instance start]; |
| 462 |
} |
| 463 |
|
| 464 |
int vcapture_stop(vcapture v) { |
| 465 |
return [v->instance stop]; |
| 466 |
} |