錄制游戲app效果圖.gif
從replaykit2 extension獲取的samplebuffer有3種掏觉,分別是RPSampleBufferTypeVideo
視頻幀瞎饲、RPSampleBufferTypeAudioApp
app內(nèi)錄音、RPSampleBufferTypeAudioMic
麥克風(fēng)乡数,但是從extension獲取的視頻幀尺寸是豎屏的椭蹄,當(dāng)用戶需要橫屏直播的時候,我們需要把視頻幀旋轉(zhuǎn)净赴。
以下是旋轉(zhuǎn)的方法:
- (void)dealWithSampleBuffer:(CMSampleBufferRef)buffer {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);
CIImage *ciimage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
// 旋轉(zhuǎn)的方法
CIImage *wImage = [ciimage imageByApplyingCGOrientation:kCGImagePropertyOrientationLeft];
CIImage *newImage = [wImage imageByApplyingTransform:CGAffineTransformMakeScale(0.5, 0.5)];
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
CVPixelBufferRef newPixcelBuffer = nil;
CVPixelBufferCreate(kCFAllocatorDefault, height * 0.5, width * 0.5, kCVPixelFormatType_32BGRA, nil, &newPixcelBuffer);
[_ciContext render:newImage toCVPixelBuffer:newPixcelBuffer];
//
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
CMVideoFormatDescriptionRef videoInfo = nil;
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, newPixcelBuffer, &videoInfo);
CMTime duration = CMSampleBufferGetDuration(buffer);
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(buffer);
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(buffer);
CMSampleTimingInfo sampleTimingInfo;
sampleTimingInfo.duration = duration;
sampleTimingInfo.presentationTimeStamp = presentationTimeStamp;
sampleTimingInfo.decodeTimeStamp = decodeTimeStamp;
//
CMSampleBufferRef newSampleBuffer = nil;
CMSampleBufferCreateForImageBuffer(kCFAllocatorMalloc, newPixcelBuffer, true, nil, nil, videoInfo, &sampleTimingInfo, &newSampleBuffer);
// 對新buffer做處理
// release
CVPixelBufferRelease(newPixcelBuffer);
CFRelease(newSampleBuffer);
}
對于CIImage的旋轉(zhuǎn)方法可以使用imageByApplyingCGOrientation:
(iOS 11的API) 和 imageByApplyingTransform:
此外绳矩,對于replaykit2返回的samplebuffer有兩種尺寸需要注意,一種是游戲app的幀玖翅,尺寸為608 * 1080
翼馆,還有一種是非游戲app的幀割以,尺寸為1080 * 1920
。