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 ...
随机推荐
- CentOS 安装openssl
https://blog.csdn.net/ydyang1126/article/details/72902113 安装环境: 操作系统:CentOS 7 OpenSSL Version:openss ...
- Activiti工作流(4):编写一个HelloWorld
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.使用eclipse的activiti插件画流程图 在resource文件夹下新建一个工作流diagram 右键——new——other...— ...
- [Python] Python list slice syntax fun
# Python's list slice syntax can be used without indices # for a few fun and useful things: # You ca ...
- (一一〇)正則表達式的基本使用与RegexKitLite的使用
正則表達式经常常使用于匹配keyword,以下先介绍基本的语法. [基本的语法] ①中括号表示满足当中之中的一个就可以,比如[abc],则这个位置能够是a.b.c中随意一个. ②在中括号里,能够通过- ...
- C++语言笔记系列之十三——派生类构造函数的调用
1.派生类构造函数的调用 (1)一个基类的全部数据成员均被派生类继承.创建一个派生类对象时.系统在为派生类对象分配单元时一定要为其基类数据成员分配子空间. (2)一个派生类对象在创建时不仅要调用派生类 ...
- vim 计算器寄存器使用
我们可能会在vim的使用中,碰到下面的情况 当我正在写一周预算的时候,我想计算下每天我买菜花2.7,每天买两顿,周死晚上出去吃,周六额外买1.5斤14.8一斤的猪肉... 这时候你打算怎么办呢,是不是 ...
- Tachyon的配置详解
Tachyon的配置 Tachyon环境变量 Tachyon通用配置 TachyonMaster配置 TachyonWorker配置 用户配置 1 Tachyon的配置 这里以0.5.0版本为例,介绍 ...
- Android ijkplayer详解使用教程
1.认识ijkplayer 最近公司准备开发一款视频播放及直播的应用,找了许多开源的框架,大部分都是基于ffmpeg开发的.最开始准备用Vitamio框架开发的,相关的文章也比较丰富,结果对于非个人移 ...
- OpenCV —— 视频播放控制
创建滚动条,实现滚动条随视频播放移动. #include "cv.h" #include "highgui.h" CvCapture* capture=NULL ...
- Java证书通信
一.概念介绍: 加密是将数据资料加密,使得非法用户即使取得加密过的资料,也无法获取正确的资料内容,所以数据加密可以保护数据,防止监听攻击.其重点在于数据的安全性.身份认证是用来判断某个身份的真实性 ...