AVCaptureSession
采集架構(gòu)圖
通過AVCaptureSession把 input和output關(guān)聯(lián)起來。
輸入設(shè)備
- Input
- Mic
- Camare
輸出設(shè)備
- outPut
- AVCaptureMovieFileOutput
- AVCaptureStillImageOutput
- AVCaptureVideoPreViewLayer
AVCaptureConnection 提供把輸入和輸出連接起來的功能
采集圖二
AVCaptureSessionPreset
使用AVCaptureSessionPreset 指定攝像頭采集的分辨率
AVCaptureVideoPreviewLayer
創(chuàng)建預(yù)覽視圖贩幻,可以直接將采集到的畫面渲染出來止状。
關(guān)鍵代碼參考
- (void) setupVideoCamareWithPosition:(AVCaptureDevicePosition) cameraPosition{
// 1. get input device
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if (device.position == cameraPosition) {
self.inputCamareDevice = device;
}
}
// 2. captureSession
BOOL enableApplicationSession = YES;
if (enableApplicationSession) {
self.captureSession.automaticallyConfiguresApplicationAudioSession = YES;
self.captureSession.usesApplicationAudioSession = YES;
}
[self.captureSession beginConfiguration];
// 3. add input
NSError * error = nil;
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamareDevice error:&error];
if ([self.captureSession canAddInput:self.videoInput]) {
[self.captureSession addInput:_videoInput];
}
// 4. add outPut
[self.videoDataOutPut setSampleBufferDelegate:self queue:_cameraProcessingQueue];
if ([self.captureSession canAddOutput:_videoDataOutPut]) {
[self.captureSession addOutput:_videoDataOutPut];
} else {
NSLog(@"can't add video output");
}
if ([self.captureSession canSetSessionPreset:AVCaptureSessionPreset640x480]) {
[self.captureSession setSessionPreset:AVCaptureSessionPreset640x480];
}
[self.captureSession commitConfiguration];
}
// 5. setupPreivew
- (void) setupPreviewWithView:(UIView *) view{
if (!view) {
return;
}
CALayer *layer = view.layer;
layer.masksToBounds = YES;
self.previewLayer.frame = layer.bounds;
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[layer insertSublayer:self.previewLayer atIndex:0];
}
- (void) startCapture{
if (![_captureSession isRunning]) {
[_captureSession startRunning];
}
}
demo 地址:https://github.com/zjunchao/streaming_tutorial/tree/master/tutorial01