谢谢
解决方法 我解决了这个问题我需要长时间访问样本数据.
尝试很多方式:
CVPixelBufferretain —–>程序坏了
CVPixelBufferPool —–>程序坏了
CVPixelBufferCreateWithBytes —->它可以解决这个程序,但这会降低性能,不建议Apple这样做
CMSampleBufferCreatecopy —>没关系,苹果推荐它.
列表:为了保持最佳性能,某些样本缓冲区直接引用可能需要由设备系统和其他捕获输入重用的内存池.对于未压缩的设备本机捕获,通常会出现这种情况,其中尽可能少地复制内存块.如果多个样本缓冲区长时间引用此类内存池,则输入将无法再将新样本复制到内存中,并且这些样本将被丢弃.如果您的应用程序通过保留提供的CMSampleBuffer对象太久而导致删除样本,但需要长时间访问样本数据,请考虑将数据复制到新缓冲区中,然后在样本缓冲区上调用CFRelease (如果以前保留过),以便可以重用它引用的内存.
REF:https://developer.apple.com/reference/avfoundation/avcapturefileoutputdelegate/1390096-captureoutput
这可能是你需要的:
pragma mark -captureOutput
- (voID)captureOutput:(AVCaptureOutput *)captureOutput dIDOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{ if (connection == m_vIDeoConnection) { /* if you dID not read m_sampleBuffer,here you must CFRelease m_sampleBuffer,it is causing samples to be dropped */ if (m_sampleBuffer) { CFRelease(m_sampleBuffer); m_sampleBuffer = nil; } Osstatus status = CMSampleBufferCreatecopy(kcfAllocatorDefault,sampleBuffer,&m_sampleBuffer); if (noErr != status) { m_sampleBuffer = nil; } NSLog(@"m_sampleBuffer = %p sampleBuffer= %p",m_sampleBuffer,sampleBuffer); }}
pragma mark -get CVPixelBufferRef可以使用很长一段时间
- (ACResult) readVIDeoFrame: (CVPixelBufferRef *)pixelBuffer{ while (1) { dispatch_sync(m_readVIDeoData,^{ if (!m_sampleBuffer) { _readDataSuccess = NO; return; } CMSampleBufferRef sampleBuffercopy = nil; Osstatus status = CMSampleBufferCreatecopy(kcfAllocatorDefault,&sampleBuffercopy); if ( noErr == status) { CVPixelBufferRef buffer = CMSampleBufferGetimageBuffer(sampleBuffercopy); *pixelBuffer = buffer; _readDataSuccess = YES; NSLog(@"m_sampleBuffer = %p ",m_sampleBuffer); CFRelease(m_sampleBuffer); m_sampleBuffer = nil; } else{ _readDataSuccess = NO; CFRelease(m_sampleBuffer); m_sampleBuffer = nil; } }); if (_readDataSuccess) { _readDataSuccess = NO; return ACResultNoErr; } else{ usleep(15*1000); continue; } }}
然后你可以这样使用它:
-(voID)getCaptureVIDeoDataToEncode{dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0),^(){ while (1) { CVPixelBufferRef buffer = NulL; ACResult result= [vIDeoCapture readVIDeoFrame:&buffer]; if (ACResultNoErr == result) { ACResult error = [vIDeoEncode encoder:buffer outputPacket:&streamPacket]; if (buffer) { CVPixelBufferRelease(buffer); buffer = NulL; } if (ACResultNoErr == error) { NSLog(@"encode success"); } } } });}总结
以上是内存溢出为你收集整理的ios – CMSampleBufferRef的深层副本全部内容,希望文章能够帮你解决ios – CMSampleBufferRef的深层副本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)