捕捉媒体的核心是AVCaptureSession捕获会话,通过它来管理咱们的输入设备,它可以同时连接多个输入设备,比如摄像头和麦克,并且为媒体捕获做些预设配置(格式、质量),还可以动态的配置输入的线路,最重要的是它可以控制捕获的开始和停止,并且可以调控设备的切换。但需要注意!这些操作都比较好时,尽量异步调用。
1. 创建session
@property (nonatomic, strong) AVCaptureSession *session;
_session = [[AVCaptureSession alloc] init];
_session.sessionPreset = AVCaptureSessionPresetHigh;//宏 选择画质
2. 给Session添加input输入和Output输出
1>视频
@property (nonatomic, strong) AVCaptureScreenInput *input;//macOSX专有输入源是电脑屏幕
@property (nonatomic, strong) AVCaptureVideoDataOutput *NewOutput;
_input.capturesMouseClicks = YES; //鼠标
[_session addInput:_input];
_NewOutput = [[AVCaptureVideoDataOutput alloc] init];
[_session addOutput:_NewOutput];//视频的输出加到session
dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[_NewOutput setSampleBufferDelegate:self queue:queue];
[_NewOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];//输出的图片的样式,其实视频本质就是一帧一帧的图片,每一帧的图片用32 bit BGRA构成。常见的有yuv等
2>声音
@property (nonatomic, strong) AVCaptureConnection *audioConnection;
NSError *deviceError;
AVCaptureDevice *microphoneDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];//音频输入设备
AVCaptureDeviceInput *inputMicrophoneDevice = [AVCaptureDeviceInput deviceInputWithDevice:microphoneDevice error:&deviceError];
AVCaptureAudioDataOutput *outputAudioDevice = [[AVCaptureAudioDataOutput alloc] init];//输出的数据
NSDictionary *audioSettings = @{AVFormatIDKey : @(kAudioFormatMPEG4AAC), AVSampleRateKey : @48000, AVEncoderBitRateKey : @12800, AVNumberOfChannelsKey : @1};//设置输出的参数 AAC类型 48000频率 12800是编解码的速率 1是单双声道
outputAudioDevice.audioSettings = audioSettings;
[outputAudioDevice setSampleBufferDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)];
[_session addInput:inputMicrophoneDevice];//声音输入加到_session
[_session addOutput:outputAudioDevice];//数据输出加到_session
// begin configuration for the AVCaptureSession
[_session beginConfiguration];
// picture resolution
self.audioConnection = [outputAudioDevice connectionWithMediaType:AVMediaTypeAudio];
[_session commitConfiguration];
注意MacOSX不支持内录,就是说声音只能从麦克风获取。比如你QQ音乐的声音是拿不到的,只能是麦克风听到之后才可以拿到。那么怎么解决,需要一个虚拟声卡驱动。用一些插件可以拿到内录声音。
3.代理主要是获得到数据
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
// 如果是声音转成aac
if (connection == self.audioConnection) {
CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t length, totalLength;
char *dataPointer;
CMBlockBufferGetDataPointer(dataBuffer, 0, &length, &totalLength, &dataPointer);
NSData *rawAAC = [NSData dataWithBytes:dataPointer length:totalLength];
NSData *adtsHeader = [self adtsDataForPacketLength:totalLength];
NSMutableData *fullData = [NSMutableData dataWithData:adtsHeader];
[fullData appendData:rawAAC];//fullData就是最后的二进制数据
CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
}else {
//得到视频 之后去编码
// [_videoEncoder encodeVideoSampleBuffer:sampleBuffer];
}
}
最后获得每一帧数据可以直接传输,但是数据量很大。因此需要对数据做压缩。视频常见的H264,265等。之后主要了解包括H264的编解码,ATSP或者RTMP的传输。