美文网首页
CMSampleBuffer深拷贝

CMSampleBuffer深拷贝

作者: Leoeoo | 来源:发表于2021-06-23 10:58 被阅读0次

    一:CMSampleBufferRef浅拷贝

    通过AVCaptureVideoDataOutput获取到的CMSampleBufferRef是放在缓冲区中的,会自动释放,不需要主动释放。

    - (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    
    }
    

    一般情况下我们会对获取的CMSampleBufferRef做操作,此时可能会想先copy一份CMSampleBufferRef,调用:

    CMSampleBufferRef tempBufferRef;
    OSStatus status = CMSampleBufferCreateCopy(kCFAllocatorDefault, nv12Buffer, &tempBufferRef);
    if (noErr == status) {
        CVImageBufferRef tempPixelBuffer = CMSampleBufferGetImageBuffer(tempBufferRef);
        return tempPixelBuffer;
    }
    

    但是CMSampleBufferCreateCopy是浅拷贝,如果拷贝出来的对象没有及时释放,会导致缓冲区占满,图像就卡住了:
    如果多个样本缓冲区长时间引用此类内存池,则输入将无法再将新样本复制到内存中,并且这些样本将被丢弃。如果您的应用程序通过保留提供的CMSampleBuffer对象太久而导致删除样本,但需要长时间访问样本数据,请考虑将数据复制到新缓冲区中,然后在样本缓冲区上调用CFRelease (如果以前保留过),以便可以重用它引用的内存。

    解决方案:
    1.不拷贝,直接操作CMSampleBufferRef
    1.使用CMSampleBufferRef的深拷贝(不建议使用)。

    浅拷贝使用示例:
    http://www.voidcn.com/article/p-kpbktkmi-byq.html

    二:CMSampleBufferRef深拷贝:

    https://blog.csdn.net/weixin_30278311/article/details/95405935

    相关文章

      网友评论

          本文标题:CMSampleBuffer深拷贝

          本文链接:https://www.haomeiwen.com/subject/jkxbyltx.html