看完熊皮皮的文章后,個人理解的一點小筆記
前記:AVCaptureSession作為一個管理員砍艾,管理著input 和output; 由于筆者對視頻音頻的理解實在是白得不能再白的小白了所禀,因此這里在音視頻上不做解釋何址,如果想了解可以查看原文
1, input 對象:AVCaptureDeviceInput 主要與AVCaptureDevice綁定;
AVCaptureDevice 有攝像頭和麥克風(fēng)等;如何獲燃缆:
AVCaptureDevice *avCaptureDevice;
//這里MediaType媒體類型涨岁,決定獲取怎樣的硬件源,video為攝像頭冯凹,audio為
麥克風(fēng)
NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in cameras) {
//這里取到后置攝像頭
if (device.position == AVCaptureDevicePositionBack) {
avCaptureDevice = device;
}
}
后面再補充如何切換攝像頭
2, 創(chuàng)建管理者session谎亩,添加input
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:avCaptureDevice error:&error];
if (!videoInput)
{
return;
}
AVCaptureSession *avCaptureSession = [[AVCaptureSession alloc] init];
avCaptureSession.sessionPreset = AVCaptureSessionPresetHigh; // sessionPreset為AVCaptureSessionPresetHigh,可不顯式指定
[avCaptureSession addInput:videoInput];
3, output對象:AVCaptureVideoDataOutput,需要設(shè)置輸出相關(guān)參數(shù)匈庭,例如分辨率什么的夫凸,還有代理協(xié)議用來處理輸出數(shù)據(jù)
AVCaptureVideoDataOutput *avCaptureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
//YUV420一般用于標清視頻,YUV422用于高清視頻
NSDictionary*settings = @{(__bridge id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)};
avCaptureVideoDataOutput.videoSettings = settings;
dispatch_queue_t queue = dispatch_queue_create("com.github.michael-lfx.back_camera_io", NULL);
//這里添加代理阱持,在代理中處理數(shù)據(jù), 由于這里是在子線程中處理夭拌,后面將會涉及到時間戳幀的排序
[avCaptureVideoDataOutput setSampleBufferDelegate:self queue:queue];
[avCaptureSession addOutput:avCaptureVideoDataOutput];
4, 添加預(yù)覽界面到view上
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:avCaptureSession];
previewLayer.frame = self.view.bounds;
previewLayer.videoGravity= AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:previewLayer];
5, 啟動會話。
[avCaptureSession startRunning];
直到這里衷咽,你就可以看到通過攝像頭獲取到的圖像了
再來說說代理中數(shù)據(jù)的處理
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
//這里從sampleBuffer拿到未壓縮的數(shù)據(jù)鸽扁,pixelBuffer; 小白我嘗試audiolist,但是一堆的參數(shù)兵罢,實在是不解献烦,暫且放棄
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if (CVPixelBufferIsPlanar(pixelBuffer)) {
NSLog(@"kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange -> planar buffer");
}
CMVideoFormatDescriptionRef desc = NULL;
CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &desc);
CFDictionaryRef extensions = CMFormatDescriptionGetExtensions(desc);
NSLog(@"extensions = %@", extensions);
}
輸出結(jié)果
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange -> planar buffer
extensions = {
CVBytesPerRow = 2904;
CVImageBufferColorPrimaries = "ITU_R_709_2";
CVImageBufferTransferFunction = "ITU_R_709_2";
CVImageBufferYCbCrMatrix = "ITU_R_709_2";
Version = 2;
}
編碼
// 獲取攝像頭輸出圖像的寬高
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
static VTCompressionSessionRef compressionSession;
OSStatus status = VTCompressionSessionCreate(NULL,
width, height,
kCMVideoCodecType_H264,
NULL,
NULL,
NULL, &compressionOutputCallback, NULL, &compressionSession);
CMTime presentationTimeStamp = CMTimeMake(frameCount, 1000);
VTEncodeInfoFlags flags;
//開始硬編碼
VTCompressionSessionEncodeFrame(compressionSession, pixelBuffer, presentationTimeStamp, kCMTimeInvalid, NULL, NULL, &flags);
編碼回調(diào)函數(shù)實現(xiàn)如下:
static void compressionOutputCallback(void * CM_NULLABLE outputCallbackRefCon,
void * CM_NULLABLE sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CM_NULLABLE CMSampleBufferRef sampleBuffer ) {
//這里的sampleBuffer中的MBlockData不為空
if (status != noErr) {
NSLog(@"%s with status(%d)", __FUNCTION__, status);
return;
}
if (infoFlags == kVTEncodeInfo_FrameDropped) {
NSLog(@"%s with frame dropped.", __FUNCTION__);
return;
}
/* ------ 輔助調(diào)試 ------ */
CMFormatDescriptionRef fmtDesc = CMSampleBufferGetFormatDescription(sampleBuffer);
CFDictionaryRef extensions = CMFormatDescriptionGetExtensions(fmtDesc);
NSLog(@"extensions = %@", extensions);
CMItemCount count = CMSampleBufferGetNumSamples(sampleBuffer);
NSLog(@"samples count = %d", count);
/* ====== 輔助調(diào)試 ====== */
// 推流或?qū)懭胛募?}
打印信息如下:
extensions = {
FormatName = "H.264";
SampleDescriptionExtensionAtoms = {
avcC = <014d0028 ffe1000b 274d0028 ab603c01 13f2a001 000428ee 3c30>;
};
}
samples count = 1
sampleBuffer的詳細信息如下:
CMSampleBuffer 0x126e9fd80 retainCount: 1 allocator: 0x1a227cb68
invalid = NO
dataReady = YES
makeDataReadyCallback = 0x0
makeDataReadyRefcon = 0x0
formatDescription = <CMVideoFormatDescription 0x126e9fd50 [0x1a227cb68]> {
mediaType:'vide'
mediaSubType:'avc1'
mediaSpecific: {
codecType: 'avc1' dimensions: 1920 x 1080
}
extensions: {<CFBasicHash 0x126e9eae0 [0x1a227cb68]>{type = immutable dict, count = 2,
entries =>
0 : <CFString 0x19dd523e0 [0x1a227cb68]>{contents = "SampleDescriptionExtensionAtoms"} = <CFBasicHash 0x126e9e090 [0x1a227cb68]>{type = immutable dict, count = 1,
entries =>
2 : <CFString 0x19dd57c20 [0x1a227cb68]>{contents = "avcC"} = <CFData 0x126e9e1b0 [0x1a227cb68]>{length = 26, capacity = 26, bytes = 0x014d0028ffe1000b274d0028ab603c01 ... a001000428ee3c30}
}
2 : <CFString 0x19dd52440 [0x1a227cb68]>{contents = "FormatName"} = H.264
}
}
}
sbufToTrackReadiness = 0x0
numSamples = 1
sampleTimingArray[1] = {
{PTS = {196709596065916/1000000000 = 196709.596}, DTS = {INVALID}, duration = {INVALID}},
}
sampleSizeArray[1] = {
sampleSize = 5707,
}
sampleAttachmentsArray[1] = {
sample 0:
DependsOnOthers = false
}
dataBuffer = 0x126e9fc50