1小時學會:最簡單的iOS直播推流(三)使用系統介面捕獲音視訊資料

hard_man發表於2018-01-12

最簡單的iOS 推流程式碼,視訊捕獲,軟編碼(faac,x264),硬編碼(aac,h264),美顏,flv編碼,rtmp協議,陸續更新程式碼解析,你想學的知識這裡都有,願意懂直播技術的同學快來看!!

原始碼:https://github.com/hardman/AWLive

通過系統相機錄製視訊獲取音視訊資料,是推流的第一步。 原始碼中提供2種獲取音視訊資料的方法:一是使用系統自帶介面;二是使用GPUImage。

本篇首先介紹第一種。

網路上關於獲取視訊資料的程式碼有不少,但是為了方便程式碼閱讀,這裡簡要介紹一下。

[注意]請仔細閱讀程式碼註釋

相關程式碼入口

整套推流程式碼的入口:AWAVCaptureManager,它是根據引數建立上述2種獲取資料方法的一個工廠類。

可以通過設定 captureType 來決定使用哪種資料獲取方式。

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獲取到正確的捕獲視訊資料的物件了。

AWAVCapture 是一個虛基類(c++中的說法,不會直接產生物件,只用來繼承的類,java中叫做抽象類)。 它的兩個子類分別是 AWSystemAVCapture 和 AWGPUImageAVCapture。

這裡使用了多型。

如果 captureType設定的是 AWAVCaptureTypeSystem,avCapture獲取到的真實物件就是 AWSystemAVCapture型別; 如果 captureType設定的是 AWAVCaptureTypeGPUImage,avCapture獲取到的真實物件就是 AWGPUImageAVCapture型別。

AWSystemAVCapture類的功能只有一個:呼叫系統相機,獲取音視訊資料。

相機資料獲取的方法

分為3步驟:

  1. 初始化輸入輸出裝置。
  2. 建立AVCaptureSession,用來管理視訊與資料的捕獲。
  3. 建立預覽UI。 還包括一些其他功能:
  4. 切換攝像頭
  5. 更改fps

在程式碼中對應的是 AWSystemAVCapture中的 onInit方法。只要初始化就會呼叫。

【注意】請仔細閱讀下文程式碼中的註釋 初始化輸入裝置

-(void) createCaptureDevice{
    // 初始化前後攝像頭
    // 執行這幾句程式碼後,系統會彈框提示:應用想要訪問您的相機。請點選同意
    // 另外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];
    
    // 初始化麥克風
    // 執行這幾句程式碼後,系統會彈框提示:應用想要訪問您的麥克風。請點選同意
    // 另外iOS10 需要在info.plist中新增欄位NSMicrophoneUsageDescription。否則會閃退,具體請自行baidu。
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    self.audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
    
    //省略其他程式碼
    ...
}
複製程式碼

初始化輸出裝置

-(void) createOutput{   
	//建立資料獲取執行緒
    dispatch_queue_t captureQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //視訊資料輸出
    self.videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    //設定代理,需要當前類實現protocol:AVCaptureVideoDataOutputSampleBufferDelegate
    [self.videoDataOutput setSampleBufferDelegate:self queue:captureQueue];
    //拋棄過期幀,保證實時性
    [self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];
    //設定輸出格式為 yuv420
    [self.videoDataOutput setVideoSettings:@{
                                             (__bridge NSString *)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)
                                             }];

    //音訊資料輸出
    self.audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
    //設定代理,需要當前類實現protocol:AVCaptureAudioDataOutputSampleBufferDelegate
    [self.audioDataOutput setSampleBufferDelegate:self queue:captureQueue];

    // AVCaptureVideoDataOutputSampleBufferDelegate 和 AVCaptureAudioDataOutputSampleBufferDelegate 回撥方法名相同都是:
    // captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    // 最終視訊和音訊資料都可以在此方法中獲取。
}
複製程式碼

建立 captureSession

// AVCaptureSession 建立邏輯很簡單,它像是一箇中介者,從音視訊輸入裝置獲取資料,處理後,傳遞給輸出裝置(資料代理/預覽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
    //諸如此類的限制,所以需要寫一些對解析度進行管理的程式碼。
    //目前的處理是,對於不支援的解析度會丟擲一個異常
    //但是這樣做是不夠、不完整的,最好的方案是,根據裝置,提供不同的解析度。
    //如果必須要用一個不支援的解析度,那麼需要根據需求對資料和預覽進行裁剪,縮放。
    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將從輸入裝置獲取資料,處理後,傳遞給輸出裝置。
    [self.captureSession startRunning];
}
複製程式碼

建立預覽UI

// 其實只有一句程式碼:CALayer layer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
// 它其實是 AVCaptureSession的一個輸出方式而已。
// CaptureSession會將從input裝置得到的資料,處理後,顯示到此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];
            }
        }
    }
}
複製程式碼

獲取音視訊資料

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    if (self.isCapturing) {
        if ([self.videoDataOutput isEqual:captureOutput]) {
        	//捕獲到視訊資料,通過sendVideoSampleBuffer傳送出去,後續文章會解釋接下來的詳細流程。
            [self sendVideoSampleBuffer:sampleBuffer];
        }else if([self.audioDataOutput isEqual:captureOutput]){
        	//捕獲到音訊資料,通過sendVideoSampleBuffer傳送出去
            [self sendAudioSampleBuffer:sampleBuffer];
        }
    }
}
複製程式碼

至此,我們達到了所有目標:能夠錄製視訊,預覽,獲取音視訊資料,切換前後攝像頭,修改捕獲視訊的fps。

文章列表

  1. 1小時學會:最簡單的iOS直播推流(一)專案介紹
  2. 1小時學會:最簡單的iOS直播推流(二)程式碼架構概述
  3. 1小時學會:最簡單的iOS直播推流(三)使用系統介面捕獲音視訊
  4. 1小時學會:最簡單的iOS直播推流(四)如何使用GPUImage,如何美顏
  5. 1小時學會:最簡單的iOS直播推流(五)yuv、pcm資料的介紹和獲取
  6. 1小時學會:最簡單的iOS直播推流(六)h264、aac、flv介紹
  7. 1小時學會:最簡單的iOS直播推流(七)h264/aac 硬編碼
  8. 1小時學會:最簡單的iOS直播推流(八)h264/aac 軟編碼
  9. 1小時學會:最簡單的iOS直播推流(九)flv 編碼與音視訊時間戳同步
  10. 1小時學會:最簡單的iOS直播推流(十)librtmp使用介紹
  11. 1小時學會:最簡單的iOS直播推流(十一)sps&pps和AudioSpecificConfig介紹(完結)

相關文章