关于IOSurface录制视频

时间:2015-10-27 08:14:57

标签: ios video background recording iosurface

需要解决的功能是:后台桌面视频录制 我使用NSTimer循环调用IOSurface进行截图,代码如下:

NSTimer * Timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(ScreenShot) userInfo:nil repeats:YES];

-(void)ScreenShot{io_service_t framebufferService;
framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleH1CLCD"));
if (!framebufferService) {
    framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
}
if (!framebufferService) {
    framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
}
IOMobileFramebufferConnection connect;
kern_return_t result;
IOSurfaceRef ScreenSurface;
result = IOMobileFramebufferOpen(framebufferService, mach_task_self(), 0, &connect);
result = IOMobileFramebufferGetLayerDefaultSurface(connect, 0, &ScreenSurface);uint32_t aseed;
uint32_t Width;
uint32_t Height;
int Pitch;
int size;
int bPE;
CFMutableDictionaryRef dict;
IOSurfaceLock(ScreenSurface, kIOSurfaceLockReadOnly, &aseed);
Width = IOSurfaceGetWidth(ScreenSurface);
Height = IOSurfaceGetHeight(ScreenSurface);
Pitch = Width * 4;
size = Width * Height * 4;
bPE = 4;
char pixelFormat[4] = {'A','R','G','B'};
dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(dict, kIOSurfaceIsGlobal, kCFBooleanTrue);
CFDictionarySetValue(dict, kIOSurfaceBytesPerRow, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Pitch));
CFDictionarySetValue(dict, kIOSurfaceBytesPerElement, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bPE));
CFDictionarySetValue(dict, kIOSurfaceWidth, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Width));
CFDictionarySetValue(dict, kIOSurfaceHeight, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &Height));
CFDictionarySetValue(dict, kIOSurfacePixelFormat, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, pixelFormat));
CFDictionarySetValue(dict, kIOSurfaceAllocSize, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &size));

CGDataProviderRef provider;
IOSurfaceRef destSurf;
void* outAcc;
CGImageRef cgImage;
destSurf = IOSurfaceCreate(dict);
IOSurfaceAcceleratorCreate(NULL, 0, &outAcc);
IOSurfaceAcceleratorTransferSurface(outAcc, ScreenSurface, destSurf, dict, NULL);
IOSurfaceUnlock(ScreenSurface, kIOSurfaceLockReadOnly, &aseed);
CFRelease(outAcc);
provider = CGDataProviderCreateWithData(NULL, IOSurfaceGetBaseAddress(destSurf), (Width * Height * 4), NULL);
cgImage = CGImageCreate(Width, Height, 8, 8*4, IOSurfaceGetBytesPerRow(destSurf), CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, provider, NULL, YES, kCGRenderingIntentDefault);}

可以截图成功。

现在的问题是:

  1. 但可能会跑1分钟左右,提示“收到内存警告。”
  2. 滑动屏幕,屏幕方法会延迟
  3. 或者后台桌面视频录制的方法是什么?

0 个答案:

没有答案