Problem
Themes created during my AVCaptureSession do not close when I stop starting AVCaptureSession.
Symptoms
Usually my dispatch_queue, which receives frames from the camera, starts instantly. But after about four times opening and closing the ViewController, which opens / closes the AVCaptureSession, dispatch_queue takes about ten seconds to start.
Forecast
It seems that the streams associated with AVCaptureSession are not cleared.
After closing AVCaptureSession, I see that these threads remain:
com.apple.coremedia.capturesource.connections(serial) 1 Pending Block
com.apple.coremedia.capturesession.connections(serial) 1 Pending Block
<AVCMNotificationDispatcher: 0x16bce00> serial queue(serial) 4 Pending Blocks
com.apple.avfoundation.videocapturedevice.observed_properties_queue(serial)
com.apple.tcc.cache_queue(serial) 1 Pending Block
com.apple.tcc.preflight.kTCCServiceCamera(serial) 1 Pending Block
And after I open / close the ViewController using AVCaptureSession, the same threads remain, but these three threads have increased the number of waiting blocks
<AVCMNotificationDispatcher: 0x17c441a0> serial queue (serial) 9 Pending Blocks
com.apple.avfoundation.videocapturedevice.observed_properties_queue(serial)
com.apple.tcc.preflight.kTCCServiceCamera(serial) 5 Pending Blocks
Code setting
VideoSource.h VideoSource.mm
ViewController :
self.videoSource = [[VideoSource alloc] init];
self.videoSource.delegate = self;
[self.videoSource setResolution:AVCaptureSessionPreset352x288]
[self.videoSource startWithDevicePosition:AVCaptureDevicePositionFront]
captureSession , . .
[self.videoSource.captureSession startRunning]
[self.videoSource.captureSession stopRunning]
VideoSource, , , .
VideoSource.mm
- (void)dealloc {
NSLog(@"Cleaning Up Video Source");
[_captureSession stopRunning];
AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0];
[_captureSession removeInput:input];
input = nil;
AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0];
[_captureSession removeOutput:output];
output = nil;
_captureSession = nil;
_deviceInput = nil;
_delegate = nil;
}
- (void) addVideoDataOutput {
AVCaptureVideoDataOutput * captureOutput = [[AVCaptureVideoDataOutput alloc] init ];
NSLog(@"Create Dispatch Queue");
dispatch_queue_t queue;
queue = dispatch_queue_create("com.name.test", DISPATCH_QUEUE_SERIAL);
[captureOutput setSampleBufferDelegate:self queue:queue];
NSString * key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber * value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary * settings = @{key:value};
NSLog(@"Set Video Settings");
[captureOutput setVideoSettings:settings];
NSLog(@"Always Discard Late Video Frames");
[captureOutput setAlwaysDiscardsLateVideoFrames:YES];
[self.captureSession addOutput:captureOutput];
}
VideoSource.h
@interface VideoSource : NSObject
@property (nonatomic, strong) AVCaptureSession * captureSession;
@property (nonatomic, strong) AVCaptureDeviceInput * deviceInput;
@property (nonatomic, weak) id<VideoSourceDelegate> delegate;
- (BOOL)startWithDevicePosition:(AVCaptureDevicePosition)devicePosition;
- (void) setResolution:(NSString*)resolution;
@end
, , VideoSource?