首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用CGDisplayStream编码H.264压缩会话

用CGDisplayStream编码H.264压缩会话
EN

Stack Overflow用户
提问于 2017-03-10 14:57:07
回答 1查看 1.3K关注 0票数 6

我正在尝试用我屏幕上的数据创建一个H.264压缩会话。我创建了一个类似于这样的CGDisplayStreamRef实例:

代码语言:javascript
复制
displayStream = CGDisplayStreamCreateWithDispatchQueue(0, 100, 100, k32BGRAPixelFormat, nil, self.screenCaptureQueue, ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
    //Call encoding session here
});

下面是我当前如何设置编码函数:

代码语言:javascript
复制
- (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

代码语言:javascript
复制
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

取得更多进展:

代码语言:javascript
复制
CMSampleBufferRef *sampleBuffer;

OSStatus sampleStatus = CMSampleBufferCreate(
                             NULL, blockBuffer, TRUE, NULL, NULL,
                             NULL, 1, 1, NULL,
                             0, NULL, sampleBuffer);

[self encode:*sampleBuffer];
EN

回答 1

Stack Overflow用户

发布于 2017-05-17 15:00:51

也许,我有点晚了,但无论如何,这可能对其他人有帮助:

代码语言:javascript
复制
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);
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42721360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档