Skip to content

Commit 1ab7843

Browse files
committed
CameraViewController supported capturing on multi-threading.
1 parent 3df7d5f commit 1ab7843

File tree

5 files changed

+1534
-1484
lines changed

5 files changed

+1534
-1484
lines changed

CameraViewController.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#import <AVFoundation/AVFoundation.h>
3333
#import <CoreGraphics/CoreGraphics.h>
3434

35-
#define _MULTI_THREADING // support multi-threading or not.
36-
3735
void _tic(); // not thread safe
3836
double _toc();
3937
double _tocp(); // with printf
@@ -54,8 +52,8 @@ typedef enum {
5452

5553
typedef enum {
5654
MultiThreadingMask = 0x100,
57-
NotSupportMultiThreading = 0 << 5,
58-
SupportMultiThreading = 1 << 5,
55+
NotSupportMultiThreading = 0 << 8,
56+
SupportMultiThreading = 1 << 8,
5957
}CameraViewControllerMultiThreading;
6058

6159
@class CameraViewController;

CameraViewController.m

+15-13
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ - (void)prepareWithCameraViewControllerType:(CameraViewControllerType)value {
128128
[videoDataOutput setVideoSettings:settingInfo];
129129

130130
// support multi-threading
131-
#ifdef _MULTI_THREADING
132-
dispatch_queue_t queue = dispatch_queue_create("captureQueue", NULL);
133-
[videoDataOutput setSampleBufferDelegate:self queue:queue];
134-
dispatch_release(queue);
135-
#else
136-
[videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
137-
#endif
131+
if ((type & MultiThreadingMask) == SupportMultiThreading) {
132+
dispatch_queue_t queue = dispatch_queue_create("captureQueue", NULL);
133+
[videoDataOutput setSampleBufferDelegate:self queue:queue];
134+
dispatch_release(queue);
135+
}
136+
else {
137+
[videoDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
138+
}
138139

139140
// attach video to session
140141
[session beginConfiguration];
@@ -262,10 +263,12 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
262263
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
263264

264265
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
266+
NSAutoreleasePool *pool = nil;
267+
if (![NSThread isMainThread])
268+
pool = [NSAutoreleasePool new];
269+
265270
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
266-
#ifdef _MULTI_THREADING
267-
NSAutoreleasePool *pool = [NSAutoreleasePool new];
268-
#endif
271+
269272
if ([session isRunning]) {
270273
if ((type & BufferTypeMask) == BufferGrayColor) {
271274
size_t width= CVPixelBufferGetWidth(imageBuffer);
@@ -301,9 +304,8 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CM
301304
[delegate didUpdateBufferCameraViewController:self];
302305

303306
}
304-
#ifdef _MULTI_THREADING
305-
[pool release];
306-
#endif
307+
if (![NSThread isMainThread])
308+
[pool release];
307309
}
308310

309311
#pragma mark - dealloc

0 commit comments

Comments
 (0)