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 ...
随机推荐
- 手动删除oracle数据库
--===================== -- 手动删除oracle数据库 --===================== 杀掉进程用此方法比较好,能保证杀得干净,而不是用sql 里面的语句ki ...
- 使用iVMS-4200 存储录像数据时的设置
1.安装软件时,选择:存储服务器 2.对存储服务器进行配置,具体配置见 配置手册.
- Lightroom 学习笔记
16.8.28 白平衡: 夕阳照片,色温高大
- 威联通NAS 网站无法登录,可以ssh情况下重启设备方法
步骤: 1.VPN登录NAS 2.PUTTY SSH登录设备 3.reboot设备 等待重启约5分钟.
- noip 2018 day2 T1 旅行 基环树 tarjan
Code: #include<cstdio> #include<cstring> #include<string> #include<stack> #i ...
- mysql安装遇到的坑
安装mysql的三步: mysqld --initialize-insecure mysqld -install net start mysql 中间遇到了坑, 看这篇文章完美的解决了,记录一下 .以 ...
- JS 引擎基础之 Shapes and Inline Caches
阅读下面这篇文章,需要20分钟: 一起了解下 JS 引擎是如何运作的吧! JS 的运作机制可以分为 AST 分析.引擎执行两个步骤: JS 源码通过 parser(分析器)转化为 AST(抽象语法树) ...
- ES6学习基础
1.let和const 与var不同,新的变量声明方式带来了一些不一样的特性,其中最重要的两个特性就是提供了块级作用域与不再具备变量提升 { let a = 20; } console.log(a); ...
- mysql中group by和order by混用 结果不是理想结果
在使用mysql排序的时候会想到按照降序分组来获得一组数据,而使用order by往往得到的不是理想中的结果,那么怎么才能使用group by 和order by得到理想中的数据结果呢? 例如 有一个 ...
- JS关键字 import
今天开发时使用import作为方法名,报错 后查明报错原因:import是js中的关键字,在取方法名时不能取import