最簡單的iOS 推流代碼羔味,視頻捕獲,軟編碼(faac钠右,x264)赋元,硬編碼(aac,h264)爬舰,美顏,flv編碼寒瓦,rtmp協(xié)議情屹,陸續(xù)更新代碼解析,你想學的知識這里都有杂腰,愿意懂直播技術的同學快來看@恪!
通過系統(tǒng)相機錄制視頻獲取音視頻數(shù)據(jù),是推流的第一步惜颇。
源碼中提供2種獲取音視頻數(shù)據(jù)的方法:一是使用系統(tǒng)自帶接口皆刺;二是使用GPUImage。
本篇首先介紹第一種凌摄。
網(wǎng)絡上關于獲取視頻數(shù)據(jù)的代碼有不少羡蛾,但是為了方便代碼閱讀,這里簡要介紹一下锨亏。
[注意]請仔細閱讀代碼注釋
相關代碼入口
整套推流代碼的入口:AWAVCaptureManager痴怨,它是根據(jù)參數(shù)創(chuàng)建上述2種獲取數(shù)據(jù)方法的一個工廠類。
可以通過設置 captureType 來決定使用哪種數(shù)據(jù)獲取方式器予。
AWAVCaptureManager部分代碼如下:
typedef enum : NSUInteger {
AWAVCaptureTypeNone,
AWAVCaptureTypeSystem,
AWAVCaptureTypeGPUImage,
} AWAVCaptureType;
@interface AWAVCaptureManager : NSObject
//視頻捕獲類型
@property (nonatomic, unsafe_unretained) AWAVCaptureType captureType;
@property (nonatomic, weak) AWAVCapture *avCapture;
//省略其他代碼
......
@end
設置了captureType之后浪藻,直接可以通過avCapture獲取到正確的捕獲視頻數(shù)據(jù)的對象了。
AWAVCapture 是一個虛基類(c++中的說法乾翔,不會直接產(chǎn)生對象爱葵,只用來繼承的類,java中叫做抽象類)反浓。
它的兩個子類分別是 AWSystemAVCapture 和 AWGPUImageAVCapture萌丈。
這里使用了多態(tài)。
如果 captureType設置的是 AWAVCaptureTypeSystem勾习,avCapture獲取到的真實對象就是 AWSystemAVCapture類型浓瞪;
如果 captureType設置的是 AWAVCaptureTypeGPUImage,avCapture獲取到的真實對象就是 AWGPUImageAVCapture類型巧婶。
AWSystemAVCapture類的功能只有一個:調(diào)用系統(tǒng)相機乾颁,獲取音視頻數(shù)據(jù)。
相機數(shù)據(jù)獲取的方法
分為3步驟:
- 初始化輸入輸出設備艺栈。
- 創(chuàng)建AVCaptureSession英岭,用來管理視頻與數(shù)據(jù)的捕獲。
- 創(chuàng)建預覽UI湿右。
還包括一些其他功能: - 切換攝像頭
- 更改fps
在代碼中對應的是 AWSystemAVCapture中的 onInit方法诅妹。只要初始化就會調(diào)用。
【注意】請仔細閱讀下文代碼中的注釋
初始化輸入設備
-(void) createCaptureDevice{
// 初始化前后攝像頭
// 執(zhí)行這幾句代碼后毅人,系統(tǒng)會彈框提示:應用想要訪問您的相機吭狡。請點擊同意
// 另外iOS10 需要在info.plist中添加字段NSCameraUsageDescription。否則會閃退丈莺,具體請自行baidu划煮。
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
self.frontCamera = [AVCaptureDeviceInput deviceInputWithDevice:videoDevices.firstObject error:nil];
self.backCamera =[AVCaptureDeviceInput deviceInputWithDevice:videoDevices.lastObject error:nil];
// 初始化麥克風
// 執(zhí)行這幾句代碼后,系統(tǒng)會彈框提示:應用想要訪問您的麥克風缔俄。請點擊同意
// 另外iOS10 需要在info.plist中添加字段NSMicrophoneUsageDescription弛秋。否則會閃退器躏,具體請自行baidu。
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
self.audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
//省略其他代碼
...
}
初始化輸出設備
-(void) createOutput{
//創(chuàng)建數(shù)據(jù)獲取線程
dispatch_queue_t captureQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//視頻數(shù)據(jù)輸出
self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
//設置代理蟹略,需要當前類實現(xiàn)protocol:AVCaptureVideoDataOutputSampleBufferDelegate
[self.videoDataOutput setSampleBufferDelegate:self queue:captureQueue];
//拋棄過期幀登失,保證實時性
[self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];
//設置輸出格式為 yuv420
[self.videoDataOutput setVideoSettings:@{
(__bridge NSString *)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
}];
//音頻數(shù)據(jù)輸出
self.audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
//設置代理,需要當前類實現(xiàn)protocol:AVCaptureAudioDataOutputSampleBufferDelegate
[self.audioDataOutput setSampleBufferDelegate:self queue:captureQueue];
// AVCaptureVideoDataOutputSampleBufferDelegate 和 AVCaptureAudioDataOutputSampleBufferDelegate 回調(diào)方法名相同都是:
// captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
// 最終視頻和音頻數(shù)據(jù)都可以在此方法中獲取挖炬。
}
創(chuàng)建 captureSession
// AVCaptureSession 創(chuàng)建邏輯很簡單揽浙,它像是一個中介者,從音視頻輸入設備獲取數(shù)據(jù)茅茂,處理后捏萍,傳遞給輸出設備(數(shù)據(jù)代理/預覽layer)。
-(void) createCaptureSession{
//初始化
self.captureSession = [AVCaptureSession new];
//修改配置
[self.captureSession beginConfiguration];
//加入視頻輸入設備
if ([self.captureSession canAddInput:self.videoInputDevice]) {
[self.captureSession addInput:self.videoInputDevice];
}
//加入音頻輸入設備
if ([self.captureSession canAddInput:self.audioInputDevice]) {
[self.captureSession addInput:self.audioInputDevice];
}
//加入視頻輸出
if([self.captureSession canAddOutput:self.videoDataOutput]){
[self.captureSession addOutput:self.videoDataOutput];
[self setVideoOutConfig];
}
//加入音頻輸出
if([self.captureSession canAddOutput:self.audioDataOutput]){
[self.captureSession addOutput:self.audioDataOutput];
}
//設置預覽分辨率
//這個分辨率有一個值得注意的點:
//iphone4錄制視頻時 前置攝像頭只能支持 480*640 后置攝像頭不支持 540*960 但是支持 720*1280
//諸如此類的限制空闲,所以需要寫一些對分辨率進行管理的代碼令杈。
//目前的處理是,對于不支持的分辨率會拋出一個異常
//但是這樣做是不夠碴倾、不完整的逗噩,最好的方案是,根據(jù)設備跌榔,提供不同的分辨率异雁。
//如果必須要用一個不支持的分辨率,那么需要根據(jù)需求對數(shù)據(jù)和預覽進行裁剪僧须,縮放纲刀。
if (![self.captureSession canSetSessionPreset:self.captureSessionPreset]) {
@throw [NSException exceptionWithName:@"Not supported captureSessionPreset" reason:[NSString stringWithFormat:@"captureSessionPreset is [%@]", self.captureSessionPreset] userInfo:nil];
}
self.captureSession.sessionPreset = self.captureSessionPreset;
//提交配置變更
[self.captureSession commitConfiguration];
//開始運行,此時担平,CaptureSession將從輸入設備獲取數(shù)據(jù)示绊,處理后,傳遞給輸出設備暂论。
[self.captureSession startRunning];
}
創(chuàng)建預覽UI
// 其實只有一句代碼:CALayer layer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
// 它其實是 AVCaptureSession的一個輸出方式而已面褐。
// CaptureSession會將從input設備得到的數(shù)據(jù),處理后取胎,顯示到此layer上展哭。
// 我們可以將此layer變換后加入到任意UIView中。
-(void) createPreviewLayer{
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
self.previewLayer.frame = self.preview.bounds;
[self.preview.layer addSublayer:self.previewLayer];
}
切換攝像頭
-(void)setVideoInputDevice:(AVCaptureDeviceInput *)videoInputDevice{
if ([videoInputDevice isEqual:_videoInputDevice]) {
return;
}
//captureSession 修改配置
[self.captureSession beginConfiguration];
//移除當前輸入設備
if (_videoInputDevice) {
[self.captureSession removeInput:_videoInputDevice];
}
//增加新的輸入設備
if (videoInputDevice) {
[self.captureSession addInput:videoInputDevice];
}
//提交配置闻蛀,至此前后攝像頭切換完畢
[self.captureSession commitConfiguration];
_videoInputDevice = videoInputDevice;
}
設置fps
-(void) updateFps:(NSInteger) fps{
//獲取當前capture設備
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
//遍歷所有設備(前后攝像頭)
for (AVCaptureDevice *vDevice in videoDevices) {
//獲取當前支持的最大fps
float maxRate = [(AVFrameRateRange *)[vDevice.activeFormat.videoSupportedFrameRateRanges objectAtIndex:0] maxFrameRate];
//如果想要設置的fps小于或等于做大fps匪傍,就進行修改
if (maxRate >= fps) {
//實際修改fps的代碼
if ([vDevice lockForConfiguration:NULL]) {
vDevice.activeVideoMinFrameDuration = CMTimeMake(10, (int)(fps * 10));
vDevice.activeVideoMaxFrameDuration = vDevice.activeVideoMinFrameDuration;
[vDevice unlockForConfiguration];
}
}
}
}
獲取音視頻數(shù)據(jù)
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
if (self.isCapturing) {
if ([self.videoDataOutput isEqual:captureOutput]) {
//捕獲到視頻數(shù)據(jù),通過sendVideoSampleBuffer發(fā)送出去觉痛,后續(xù)文章會解釋接下來的詳細流程役衡。
[self sendVideoSampleBuffer:sampleBuffer];
}else if([self.audioDataOutput isEqual:captureOutput]){
//捕獲到音頻數(shù)據(jù),通過sendVideoSampleBuffer發(fā)送出去
[self sendAudioSampleBuffer:sampleBuffer];
}
}
}
至此秧饮,我們達到了所有目標:能夠錄制視頻映挂,預覽,獲取音視頻數(shù)據(jù)盗尸,切換前后攝像頭柑船,修改捕獲視頻的fps。
文章列表
- 1小時學會:最簡單的iOS直播推流(一)項目介紹
- 1小時學會:最簡單的iOS直播推流(二)代碼架構概述
- 1小時學會:最簡單的iOS直播推流(三)使用系統(tǒng)接口捕獲音視頻
- 1小時學會:最簡單的iOS直播推流(四)如何使用GPUImage泼各,如何美顏
- 1小時學會:最簡單的iOS直播推流(五)yuv鞍时、pcm數(shù)據(jù)的介紹和獲取
- 1小時學會:最簡單的iOS直播推流(六)h264、aac扣蜻、flv介紹
- 1小時學會:最簡單的iOS直播推流(七)h264/aac 硬編碼
- 1小時學會:最簡單的iOS直播推流(八)h264/aac 軟編碼
- 1小時學會:最簡單的iOS直播推流(九)flv 編碼與音視頻時間戳同步
- 1小時學會:最簡單的iOS直播推流(十)librtmp使用介紹
- 1小時學會:最簡單的iOS直播推流(十一)sps&pps和AudioSpecificConfig介紹(完結(jié))