關(guān)于拓幻的SDK基本使用: 拓幻美顏對接進(jìn)騰訊云直播過程以及問題
新項目中有一個美顏設(shè)置界面腻惠, 可提前設(shè)置視頻時的美顏效果环肘,主要是預(yù)覽+美顏, 所以找了下集灌, 使用GPUImage圖像處理框架來對接拓幻SDK悔雹。
一個簡單的效果:
想要圖像預(yù)覽, 那就要有一個圖像視頻流绝页。
在GPUImage框架中荠商, GPUImageVideoCamera
作為GPUImageOutput
的子類寂恬,會把圖像作為紋理续誉, 傳給OpenGL ES處理,然后把紋理往下傳遞初肉, 所以我們需要創(chuàng)建一個GPUImageVideoCamera
的對象:
@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;
- (GPUImageVideoCamera *)videoCamera {
if (!_videoCamera) {
_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionFront];
_videoCamera.frameRate = 30; // 調(diào)整幀率
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
_videoCamera.horizontallyMirrorFrontFacingCamera = true;
_videoCamera.horizontallyMirrorRearFacingCamera = false;
[_videoCamera addAudioInputsAndOutputs];
_videoCamera.delegate = self;
[_videoCamera addTarget:self.previewView];
}
return _videoCamera;
}
視頻流有了酷鸦, 還需要一個視圖來進(jìn)行渲染,GPUImageView
一個用作GPUImage 輸出的端點(diǎn)的UIView子類牙咏, 所以我們再來創(chuàng)建一個GPUImageView
的對象:
@property (nonatomic, strong) GPUImageView *previewView;
- (GPUImageView *)previewView {
if (!_previewView) {
_previewView = [[GPUImageView alloc] initWithFrame:self.view.frame];
_previewView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
}
return _previewView;
}
準(zhǔn)備就緒后臼隔, 初始化TiSDK
,將美顏UI 添加到self. previewView
上加載妄壶,并設(shè)置代理摔握, 開始相機(jī)拍攝:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 美顏設(shè)置
[TiSDK init:kTuoHuanKey CallBack:^(InitStatus callBack) {
}];
[[TiUIManager shareManager] loadToView:self.previewView forDelegate:self];
[self setUI];
[self.videoCamera startCameraCapture];
}
GPUImageVideoCamera
有個代理GPUImageVideoCameraDelegate
,其實(shí)就是一個攝像頭采集協(xié)議丁寄,實(shí)現(xiàn)協(xié)議方法-(void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
這個方法用于對幀動畫的手動處理氨淌, 它會在捕獲每一幀畫面的時候被調(diào)用,此時就可以對畫面進(jìn)行一些處理伊磺, 那對上我們的需求盛正, 就可以使用TiSDK對每一幀進(jìn)行紋理渲染:
-(void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer {
// CoreMedia.framework: CMSampleBufferGetImageBuffer
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
BOOL isMirror = (self.videoCamera.cameraPosition == AVCaptureDevicePositionFront);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
unsigned char *baseAddress = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
TiRotationEnum rotation;
switch (orientation) {
case UIDeviceOrientationPortrait:
rotation = CLOCKWISE_90;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = isMirror ? CLOCKWISE_180 : CLOCKWISE_0;
break;
case UIDeviceOrientationLandscapeRight:
rotation = isMirror ? CLOCKWISE_0 : CLOCKWISE_180;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = CLOCKWISE_270;
break;
default:
rotation = CLOCKWISE_90;
break;
}
// 視頻幀格式
TiImageFormatEnum format;
switch (CVPixelBufferGetPixelFormatType(pixelBuffer)) {
case kCVPixelFormatType_32BGRA:
format = BGRA;
break;
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
format = NV12;
break;
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
format = NV12;
break;
default:
NSLog(@"錯誤的視頻幀格式!");
format = BGRA;
break;
}
int imageWidth, imageHeight;
if (format == BGRA) {
imageWidth = (int)CVPixelBufferGetBytesPerRow(pixelBuffer) / 4;
imageHeight = (int)CVPixelBufferGetHeight(pixelBuffer);
} else {
imageWidth = (int)CVPixelBufferGetWidthOfPlane(pixelBuffer , 0);
imageHeight = (int)CVPixelBufferGetHeightOfPlane(pixelBuffer , 0);
}
//todo --- tillusory start ---
[[TiSDKManager shareManager] renderPixels:baseAddress Format:format Width:imageWidth Height:imageHeight Rotation:rotation Mirror:isMirror];
//todo --- tillusory end ---
// self.outputImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
self.outputImagePixelBuffer = pixelBuffer;
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
}
@property(nonatomic, assign) CVPixelBufferRef outputImagePixelBuffer;
在iOS里屑埋,我們經(jīng)常能看到 CVPixelBufferRef 這個類型豪筝,在Camera 采集返回的數(shù)據(jù)里得到一個CMSampleBufferRef,而每個CMSampleBufferRef里則包含一個 CVPixelBufferRef摘能,在視頻硬解碼的返回數(shù)據(jù)里也是一個 CVPixelBufferRef续崖。
顧名思義,CVPixelBufferRef 是一種像素圖片類型团搞,由于CV開頭严望,所以它是屬于 CoreVideo 模塊的。
CVPixelBufferRef里包含很多圖片相關(guān)屬性莺丑,比較重要的有 width著蟹,height墩蔓,PixelFormatType等。
到這里萧豆,預(yù)覽+拓幻美顏的功能基本就實(shí)現(xiàn)了奸披, 當(dāng)設(shè)置了不同的美顏, 就會實(shí)時進(jìn)行紋理渲染涮雷。
由于拓幻SDK有緩存阵面, 即每次設(shè)置之后都會做緩存,下次使用時會自動讀取緩存洪鸭, 所以我們設(shè)置完成后样刷,在視頻時就可以直接使用設(shè)置好的美顏參數(shù)。
GPUImageVideoCamera
的幾個常用方法:
切換攝像頭:[self.videoCamera rotateCamera];
開始視頻采集:[self.videoCamera startCameraCapture];
停止視頻采集:[self.videoCamera stopCameraCapture];
以上览爵, 結(jié)束置鼻。
如果對你有幫助, 就給一個贊吧~