视频采集
相关类的介绍:
- [ ] AVCaptureDevice:硬件设备,包括麦克风、摄像头,通过该对象可以设置物理设备的一些属性(例如相机聚焦、白平衡等)
- [ ] AVCaptureDeviceInput:硬件输入对象,可以根据AVCaptureDevice创建对应的AVCaptureDeviceInput对象,用于管理硬件输入数据
- [ ] AVCaptureOutput:硬件输出对象,用于接收各类输出数据,通常使用对应的子类AVCaptureAudioDataOutput(声音数据输出对象)、AVCaptureVideoDataOutput(视频数据输出对象)
- [ ] AVCaptionConnection:当把一个输入和输出添加到AVCaptureSession之后,AVCaptureSession就会在输入、输出设备之间建立连接,而且通过AVCaptureOutput可以获取这个连接对象
- [ ] AVCaptureVideoPreviewLayer:相机拍摄预览图层,能实时查看拍照或视频录制效果,创建该对象需要指定对应的AVCaptureSession对象,因为AVCaptureSession包含视频输入数据,有视频数据才能展示
- [ ] AVCaptureSession: 协调输入与输出之间传输数据系统作用:可以操作硬件设备
工作原理:让App与系统之间产生一个捕获会话,相当于App与硬件设备有联系了, 我们只需要把硬件输入对象和输出对象添加到会话中,会话就会自动把硬件输入对象和输出产生连接,这样硬件输入与输出设备就能传输音视频数据。
采集视频具体步骤:
需要的属性
<AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate>
@property(nonatomic,strong) AVCaptureSession *captureSession; // AVCaptureSession
@property(nonatomic,strong) AVCaptureDeviceInput *currentVideoDeviceInput ; // AVCaptureDeviceInput
@property (nonatomic, weak) AVCaptureConnection *videoConnection;
@property (nonatomic, weak) AVCaptureVideoPreviewLayer *previedLayer;
.h
/**
开启相机
*/
- (void)startRunning;
@property(nonatomic,copy) void(^sampleBufferCallBack)(CMSampleBufferRef sampleBuffer); // 毁掉数据
.m
// 创建session
- (void)setupSession
{
// 1, 创建步骤绘画,设置分辨率
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
self.captureSession = captureSession;
captureSession.sessionPreset = AVCaptureSessionPreset1280x720; // 设置分辨率
// 2.获取摄像头设备,默认是后置摄像头
AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront];
// 3, 获取声音设备
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
// 4.创建对应视频设备输入对象
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
_currentVideoDeviceInput = videoDeviceInput;
// 5.创建对应音频设备输入对象
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
// 6.添加到会话中
// 注意“最好要判断是否能添加输入,会话不能添加空的
// 6.1 添加视频
if ([captureSession canAddInput:videoDeviceInput])
{
[captureSession addInput:videoDeviceInput];
}
// 6.2 添加音频
if ([captureSession canAddInput:audioDeviceInput])
{
[captureSession addInput:audioDeviceInput];
}
// 7.获取视频数据输出设备
AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
// 7.1 设置代理,捕获视频样品数据
// 注意:队列必须是串行队列,才能获取到数据,而且不能为空
dispatch_queue_t videoQueue = dispatch_queue_create("Video Capture Queue", DISPATCH_QUEUE_SERIAL);
[videoOutput setSampleBufferDelegate:self queue:videoQueue]; // 可以获取视频的帧数据
if ([captureSession canAddOutput:videoOutput])
{
[captureSession addOutput:videoOutput];
}
// 8.获取音频数据输出设备
AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init];
// 8.2 设置代理,捕获视频样品数据
// 注意:队列必须是串行队列,才能获取到数据,而且不能为空
dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capture Queue", DISPATCH_QUEUE_SERIAL);
[audioOutput setSampleBufferDelegate:self queue:audioQueue];
if ([captureSession canAddOutput:audioOutput])
{
[captureSession addOutput:audioOutput];
}
// 9.获取视频输入与输出连接,用于分辨音视频数据
self.videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo];
self.videoConnection.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
}
- (void)startRunning
{
//10 开启绘画
[_captureSession startRunning];
}
// 指定摄像头方向获取摄像头
- (AVCaptureDevice *)getVideoDevice:(AVCaptureDevicePosition)position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if (device.position == position)
{
return device;
}
}
return nil;
}
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
// 获取输入设备数据,有可能是音频有可能是视频
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
if (_videoConnection == connection)
{
if (self.sampleBufferCallBack)
{
self.sampleBufferCallBack(sampleBuffer);
}
}
else
{
}
}
结合之前的opengl的渲染综合使用
-(void)setupOpengl
{
self.ijsVideo =[[IJSOpenGLView alloc]initWithFrame:[UIScreen mainScreen].bounds];
IJSVideoCamera *camera = [[IJSVideoCamera alloc]init];
self.camera = camera;
__weak typeof (self) weakSelf = self;
camera.sampleBufferCallBack = ^(CMSampleBufferRef sampleBuffer) {
[weakSelf.ijsVideo displayFramebuffer:sampleBuffer];
};
self.view = self.ijsVideo;
[self.camera startRunning];
}