c语言 display,objective-C语言 使用CGDisplayStream编码H.264压缩会话

我正在尝试使用屏幕上的数据创建一个H.264压缩会话.我已经创建了一个CGDisplayStreamRef实例,如下所示:

displayStream = CGDisplayStreamCreateWithDispatchQueue(0, 100, 100, k32BGRAPixelFormat, nil, self.screenCaptureQueue, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {

//Call encoding session here

});

以下是我目前如何设置编码功能:

- (void) encode:(CMSampleBufferRef )sampleBuffer {

CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);

CMTime presentationTimeStamp = CMTimeMake(frameID++, 1000);

VTEncodeInfoFlags flags;

OSStatus statusCode = VTCompressionSessionEncodeFrame(EncodingSession,

imageBuffer,

presentationTimeStamp,

kCMTimeInvalid,

NULL, NULL, &flags);

if (statusCode != noErr) {

NSLog(@"H264: VTCompressionSessionEncodeFrame failed with %d", (int)statusCode);

VTCompressionSessionInvalidate(EncodingSession);

CFRelease(EncodingSession);

EncodingSession = NULL;

return;

}

NSLog(@"H264: VTCompressionSessionEncodeFrame Success");

}

我试图了解如何将数据从我的屏幕转换为CMSampleBufferRef,以便我可以正确调用我的编码功能.到目前为止,我还无法确定这是否可行,或者是我正在尝试做的正确方法.有没有人有什么建议?

编辑:我已经将我的IOSurface转换为CMBlockBuffer,但尚未弄清楚如何将其转换为CMSampleBufferRef:

void *mem = IOSurfaceGetBaseAddress(frameSurface);

size_t bytesPerRow = IOSurfaceGetBytesPerRow(frameSurface);

size_t height = IOSurfaceGetHeight(frameSurface);

size_t totalBytes = bytesPerRow * height;

CMBlockBufferRef blockBuffer;

CMBlockBufferCreateWithMemoryBlock(kCFAllocatorNull, mem, totalBytes, kCFAllocatorNull, NULL, 0, totalBytes, 0, &blockBuffer);

编辑2

更进一步:

CMSampleBufferRef *sampleBuffer;

OSStatus sampleStatus = CMSampleBufferCreate(

NULL, blockBuffer, TRUE, NULL, NULL,

NULL, 1, 1, NULL,

0, NULL, sampleBuffer);

[self encode:*sampleBuffer];

可能,我有点迟了但是,它可能对其他人有帮助:

CGDisplayStreamCreateWithDispatchQueue(CGMainDisplayID(), 100, 100, k32BGRAPixelFormat, nil, self.screenCaptureQueue, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {

// The created pixel buffer retains the surface object.

CVPixelBufferRef pixelBuffer;

CVPixelBufferCreateWithIOSurface(NULL, frameSurface, NULL, &pixelBuffer);

// Create the video-type-specific description for the pixel buffer.

CMVideoFormatDescriptionRef videoFormatDescription;

CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixelBuffer, &videoFormatDescription);

// All the necessary parts for creating a `CMSampleBuffer` are ready.

CMSampleBufferRef sampleBuffer;

CMSampleTimingInfo timingInfo;

CMSampleBufferCreateReadyWithImageBuffer(NULL, pixelBuffer, videoFormatDescription, &timingInfo, &sampleBuffer);

// Do the stuff

// Release the resources to let the frame surface be reused in the queue

// `kCGDisplayStreamQueueDepth` is responsible for the size of the queue

CFRelease(sampleBuffer);

CFRelease(pixelBuffer);

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值