AVCaptureSession音频视频采集
//
// AudioVideoCaptureViewController.m
// live
//
// Created by lujunjie on 2016/10/31.
// Copyright © 2016年 lujunjie. All rights reserved.
// #import "AudioVideoCaptureViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface AudioVideoCaptureViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate,AVCaptureAudioDataOutputSampleBufferDelegate>
@property (nonatomic,strong) AVCaptureSession *mCaptureSession;
@property (nonatomic,strong) AVCaptureDeviceInput *mCaptureDeviceInput;
@property (nonatomic ,strong) AVCaptureDeviceInput *mCaptureAudioDeviceInput;//负责从AVCaptureDevice获得输入数据
@property (nonatomic,strong) AVCaptureVideoDataOutput *mCaptureVideoOutput;
@property (nonatomic , strong) AVCaptureAudioDataOutput *mCaptureAudioOutput;
@property (nonatomic,strong) dispatch_queue_t mProcessQueue;
@property (nonatomic,strong) dispatch_queue_t mCaptureQueue;
@property (nonatomic,strong) dispatch_queue_t mEncodeQueue; @property (nonatomic,strong) AVCaptureVideoPreviewLayer *mPreviewLayer;
@end @implementation AudioVideoCaptureViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. // a、AVCaptureDevice。这里代表抽象的硬件设备。
//
// b、AVCaptureInput。这里代表输入设备(可以是它的子类),它配置抽象硬件设备的ports。
//
// c、AVCaptureOutput。它代表输出数据,管理着输出到一个movie或者图像。
//
// d、AVCaptureSession。它是input和output的桥梁。它协调着intput到output的数据传输。
[self startCapture];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)startCapture {
// 新建会话,设置图像大小
self.mCaptureSession = [[AVCaptureSession alloc] init];
self.mCaptureSession.sessionPreset = AVCaptureSessionPreset640x480; self.mCaptureQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
self.mEncodeQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); AVCaptureDevice *inputCamera = nil;
// 获取前置摄像头
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices)
{
if ([device position] == AVCaptureDevicePositionFront)
{
inputCamera = device;
}
}
// 把摄像头设置到输入设备,并添加到会话
self.mCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:inputCamera error:nil]; if ([self.mCaptureSession canAddInput:self.mCaptureDeviceInput]) {
[self.mCaptureSession addInput:self.mCaptureDeviceInput];
} // 设置输出设备的参数,并把输出设备添加到会话
self.mCaptureVideoOutput = [[AVCaptureVideoDataOutput alloc] init];
[self.mCaptureVideoOutput setAlwaysDiscardsLateVideoFrames:NO]; [self.mCaptureVideoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]]; [self.mCaptureVideoOutput setSampleBufferDelegate:self queue:self.mCaptureQueue];
if ([self.mCaptureSession canAddOutput:self.mCaptureVideoOutput]) {
[self.mCaptureSession addOutput:self.mCaptureVideoOutput];
}
// 输出设置缩放裁剪系数设置、建立视频音频链接
AVCaptureConnection *connection = [self.mCaptureVideoOutput connectionWithMediaType:AVMediaTypeVideo];
[connection setVideoOrientation:AVCaptureVideoOrientationPortrait]; // 视频
self.mPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.mCaptureSession];
[self.mPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; //设置预览时的视频缩放方式
[self.mPreviewLayer setFrame:self.view.bounds];
[self.view.layer addSublayer:self.mPreviewLayer]; // 获取麦克风设备
AVCaptureDevice *audioDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] lastObject];
// 把设备设置到输入设备,并添加到会话
self.mCaptureAudioDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];
if ([self.mCaptureSession canAddInput:self.mCaptureAudioDeviceInput]) {
[self.mCaptureSession addInput:self.mCaptureAudioDeviceInput];
}
// 设置输出设备的参数,并把输出设备添加到会话
self.mCaptureAudioOutput = [[AVCaptureAudioDataOutput alloc] init];
if ([self.mCaptureSession canAddOutput:self.mCaptureAudioOutput]) {
[self.mCaptureSession addOutput:self.mCaptureAudioOutput];
}
[self.mCaptureAudioOutput setSampleBufferDelegate:self queue:self.mCaptureQueue]; [self.mCaptureSession startRunning];
} - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
if (captureOutput == self.mCaptureVideoOutput) {
dispatch_sync(self.mEncodeQueue, ^{
NSLog(@"视频:::sampleBuffer");
});
}
else {
dispatch_sync(self.mEncodeQueue, ^{
NSLog(@"音频:::sampleBuffer");
});
}
} @end
AVCaptureSession音频视频采集的更多相关文章
- iOS 直播-获取音频(视频)数据
iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...
- 第六十篇、音视频采集硬编码(H264+ACC)
使用 AVCaptureSession进行实时采集音视频(YUV.),编码 通过AVCaptureVideoDataOutputSampleBufferDelegate获取到音视频buffer- 数据 ...
- 直播软件开发关于Android、iOS中的视频采集步骤
很多人对直播软件开发还是抱有想法的,但是在这个资本冷静的市场下,直播平台该怎么玩,在直播软件开发过程中哪些功能是必须具备的,这都是值得关注的话题.今天我们给大家分享一份详细的直播软件开发关于Andro ...
- (三)WebRTC手记之本地视频采集
转自:http://www.cnblogs.com/fangkm/p/4374610.html 前面两篇文章介绍WebRTC的运行流程和使用框架接口,接下来就开始分析本地音视频的采集流程.由于篇幅较大 ...
- WebRTC手记之本地视频采集
转载请注明出处:http://www.cnblogs.com/fangkm/p/4374610.html 前面两篇文章介绍WebRTC的运行流程和使用框架接口,接下来就开始分析本地音视频的采集流程.由 ...
- Window 下 VFW 视频采集与显示
引言 经过几天的努力终于将VFW视频采集与显示功能完整实现了,不得不说网上对这方面完整的详细讲解文章是在太少了.所以就要本人来好好总结一下让后来者不再像我一样折腾好久.在本文中我将详细讲解VFW视频采 ...
- Delphi XE6 试用Android视频采集
FMX支持视频采集,具体见FMX.Media,提供了很类支持音频.视频的处理. 按帮助文档,用Note3做了测试,结果,效率太低,不可用. 具体可查询帮助Video Capturing一节,我就是按这 ...
- 嵌入式LINUX环境下视频采集知识
V4L2是Linux环境下开发视频采集设备驱动程序的一套规范(API),它为驱动程序的编写提供统一的接口,并将所有的视频采集设备的驱动程序都纳入其的管理之中.V4L2不仅给驱动程序编写者带来极大的方便 ...
- 音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw
音频视频编程相关:GStreamer/ffmpeg/directshow/vfw linux和window下几种流行的音频视频编程框架作一个总结,防止自己迷惘,免于晕头转向. 一.GStreamer ...
随机推荐
- MySQL Server 5.5.44免安装版配置详解
转载地址:http://wenku.baidu.com/view/2a8bfe6a25c52cc58bd6beff.html### 一 下载MySQL http://dev.mysql.com/dow ...
- VBA 字符串操作(基础篇)
转自:http://blog.csdn.net/jyh_jack/article/details/2315345 mid(字符串,从第几个开始,长度) 在[字符串]中[从第几个开始]取出[长度个字符串 ...
- mysql分页小结
mysql.select('*').from('books') .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id') .limit ...
- React开发实时聊天招聘工具 -第三章 React基础知识回顾
function a (props) { return <h1>hello world{this.props.asd}</h1> } class a extends React ...
- JSP页面的静态包含和动态包含的区别与联系
JSP中有两种包含: 静态包含:<%@include file="被包含页面"%> 动态包含:<jsp:include page="被包含页面" ...
- 实现人脸识别性别之路---try语句的使用
Try语句 用法:处理异常信息 存在的形式:try-except X-except T...-except-else-finally(其中X T为错误的类型) 表达意思:try语句是执行正常语句,如果 ...
- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-qvc66dfs/supervisor/
# 安装supervisor 出错 pip3 install supervisor # 解决 sudo pip3 install supervisor
- PostgreSQL递归查询实现树状结构查询
在Postgresql的使用过程中发现了一个非常有意思的功能,就是对于须要相似于树状结构的结果能够使用递归查询实现.比方说我们经常使用的公司部门这样的数据结构.一般我们设计表结构的时候都是相似以下的S ...
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
[121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Say you have ...
- jQuery11 data() : 数据缓存
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...